Example usage for com.mongodb WriteConcern MAJORITY

List of usage examples for com.mongodb WriteConcern MAJORITY

Introduction

In this page you can find the example usage for com.mongodb WriteConcern MAJORITY.

Prototype

WriteConcern MAJORITY

To view the source code for com.mongodb WriteConcern MAJORITY.

Click Source Link

Document

Exceptions are raised for network issues, and server errors; waits on a majority of servers for the write operation.

Usage

From source file:ch.agent.crnickl.mongodb.MongoDB.java

License:Apache License

private WriteConcern getWriteConcernFromKeyword(String keyword) throws T2DBException {
    WriteConcern wc = null;//from   w w w.  j  a  v  a 2 s. c o m
    if (keyword == null)
        keyword = "SAFE";
    WriteConcernKeyword k = null;
    try {
        k = WriteConcernKeyword.valueOf(keyword);
    } catch (IllegalArgumentException e) {
        throw T2DBMMsg.exception(e, J.J81020, keyword);
    }
    switch (k) {
    case NONE:
        wc = WriteConcern.NONE;
        break;
    case NORMAL:
        wc = WriteConcern.NORMAL;
        break;
    case SAFE:
        wc = WriteConcern.SAFE;
        break;
    case MAJORITY:
        wc = WriteConcern.MAJORITY;
        break;
    case FSYNC_SAFE:
        wc = WriteConcern.FSYNC_SAFE;
        break;
    case JOURNAL_SAFE:
        wc = WriteConcern.JOURNAL_SAFE;
        break;
    case REPLICAS_SAFE:
        wc = WriteConcern.REPLICAS_SAFE;
        break;
    default:
        throw new RuntimeException("bug: " + k.name());
    }
    if (wc != WriteConcern.SAFE)
        throw T2DBMMsg.exception(J.J81021, keyword);
    return wc;
}

From source file:com.navercorp.pinpoint.plugin.mongodb.MongoDBIT_3_0_x_IT.java

License:Apache License

@Override
public void setClient() {
    mongoClient = new com.mongodb.MongoClient("localhost", 27018);

    database = mongoClient.getDatabase("myMongoDbFake").withReadPreference(ReadPreference.secondaryPreferred())
            .withWriteConcern(WriteConcern.MAJORITY);
}

From source file:com.navercorp.pinpoint.plugin.mongodb.MongoDBIT_3_2_x_IT.java

License:Apache License

@Override
public void setClient() {
    mongoClient = new com.mongodb.MongoClient("localhost", 27018);
    database = mongoClient.getDatabase("myMongoDbFake").withReadPreference(ReadPreference.secondaryPreferred())
            .withWriteConcern(WriteConcern.MAJORITY);
}

From source file:com.navercorp.pinpoint.plugin.mongodb.MongoDBIT_3_7_x_IT.java

License:Apache License

@Override
public void setClient() {
    mongoClient = MongoClients.create("mongodb://localhost:27018");
    database = mongoClient.getDatabase("myMongoDbFake").withReadPreference(ReadPreference.secondaryPreferred())
            .withWriteConcern(WriteConcern.MAJORITY);
}

From source file:com.picdrop.guice.RepositoryModule.java

@Override
protected MorphiaRepository<TokenSet> provideTokenSetRepo() {
    return MorphiaRepository.Builder.forType(TokenSet.class)
            .withWriteConcern(WriteConcern.MAJORITY.withJournal(true)).withDatastore(null).build();
}

From source file:com.picdrop.guice.RepositoryModule.java

@Override
protected MorphiaRepository<RegisteredUser> provideRegisteredUserRepo() {
    return MorphiaRepository.Builder.forType(RegisteredUser.class)
            .withWriteConcern(WriteConcern.MAJORITY.withJournal(true)).withDatastore(null).build();
}

From source file:com.ricardolorenzo.identity.user.impl.UserIdentityManagerMongoDB.java

License:Open Source License

public UserIdentityManagerMongoDB(final Properties conf) throws UnknownHostException {
    this.properties = conf;

    this.databaseUsers = new Boolean(this.properties.getProperty("mongodb.databaseUsers", "true"));

    WriteConcern writeConcern = WriteConcern.MAJORITY;
    String writeConcernType = conf.getProperty("mongodb.writeConcern", "majority").toLowerCase();
    if ("majority".equals(writeConcernType)) {
        writeConcern = WriteConcern.UNACKNOWLEDGED;
    } else if ("unacknowledged".equals(writeConcernType)) {
        writeConcern = WriteConcern.UNACKNOWLEDGED;
    } else if ("acknowledged".equals(writeConcernType)) {
        writeConcern = WriteConcern.ACKNOWLEDGED;
    } else if ("journaled".equals(writeConcernType)) {
        writeConcern = WriteConcern.JOURNALED;
    } else if ("replica_acknowledged".equals(writeConcernType)) {
        writeConcern = WriteConcern.REPLICA_ACKNOWLEDGED;
    }/*  ww w.j  ava2  s . com*/

    ReadPreference readPreference = null;
    String readPreferenceType = conf.getProperty("mongodb.readPreference", "primary").toLowerCase();
    if ("primary".equals(readPreferenceType)) {
        readPreference = ReadPreference.primary();
    } else if ("primary_preferred".equals(readPreferenceType)) {
        readPreference = ReadPreference.primaryPreferred();
    } else if ("secondary".equals(readPreferenceType)) {
        readPreference = ReadPreference.secondary();
    } else if ("secondary_preferred".equals(readPreferenceType)) {
        readPreference = ReadPreference.secondaryPreferred();
    } else if ("nearest".equals(readPreferenceType)) {
        readPreference = ReadPreference.nearest();
    }

    MongoClientOptions.Builder options = MongoClientOptions.builder();
    options.writeConcern(writeConcern);
    options.readPreference(readPreference);
    try {
        options.connectionsPerHost(Integer.parseInt(conf.getProperty("mongodb.threads", "100")));
    } catch (NumberFormatException e) {
        options.connectionsPerHost(100);
    }

    MongoClientURI mongoClientURI = new MongoClientURI(
            conf.getProperty("mongodb.url", "mongodb://localhost:27017"), options);
    if (!this.properties.containsKey("mongodb.database")) {
        if (mongoClientURI.getDatabase() != null && !mongoClientURI.getDatabase().isEmpty()) {
            this.properties.setProperty("mongodb.database", mongoClientURI.getDatabase());
        } else {
            this.properties.setProperty("mongodb.database", DEFAULT_DATABASE);
        }
    }
    mongoClient = new MongoClient(mongoClientURI);
}

From source file:com.strategicgains.docussandra.controller.perf.remote.mongo.MongoLoader.java

License:Apache License

public static void loadMongoData(MongoClientURI uri, final int NUM_WORKERS, Database database,
        final int numDocs, final PerfTestParent clazz) {
    logger.info("------------Loading Data into: " + database.name() + " with MONGO!------------");
    try {//ww w  .j  a  va  2  s  .  com
        try {
            MongoClient mongoClient = new MongoClient(uri);
            mongoClient.setWriteConcern(WriteConcern.MAJORITY);
            DB db = mongoClient.getDB(database.name());
            final DBCollection coll = db.getCollection(database.name());
            ArrayList<Thread> workers = new ArrayList<>(NUM_WORKERS + 1);
            int docsPerWorker = numDocs / NUM_WORKERS;
            try {
                List<Document> docs = clazz.getDocumentsFromFS();
                ArrayList<List<Document>> documentQueues = new ArrayList<>(NUM_WORKERS + 1);
                int numDocsAssigned = 0;
                while ((numDocsAssigned + 1) < numDocs) {
                    int start = numDocsAssigned;
                    int end = numDocsAssigned + docsPerWorker;
                    if (end > numDocs) {
                        end = numDocs - 1;
                    }
                    documentQueues.add(new ArrayList(docs.subList(start, end)));
                    numDocsAssigned = end;
                }
                for (final List<Document> queue : documentQueues) {
                    workers.add(new Thread() {
                        @Override
                        public void run() {
                            for (Document d : queue) {
                                DBObject o = (DBObject) JSON.parse(d.object());
                                coll.save(o);
                            }
                            logger.info("Thread " + Thread.currentThread().getName() + " is done. It processed "
                                    + queue.size() + " documents.");
                        }
                    });
                }
            } catch (UnsupportedOperationException e)//we can't read everything in at once
            {
                //all we need to do in this block is find a way to set "workers"
                for (int i = 0; i < NUM_WORKERS; i++) {
                    workers.add(new Thread() {
                        private final int chunk = (int) (Math.random() * 100) + 150;//pick a random chunk so we are not going back to the FS all at the same time and potentially causing a bottle neck

                        @Override
                        public void run() {
                            ThreadLocal<Integer> counter = new ThreadLocal<>();
                            counter.set(new Integer(0));
                            try {
                                List<Document> docs = clazz.getDocumentsFromFS(chunk);//grab a handful of documents
                                while (docs.size() > 0) {
                                    for (Document d : docs)//process the documents we grabbed
                                    {
                                        DBObject o = (DBObject) JSON.parse(d.object());
                                        coll.save(o);
                                        counter.set(counter.get() + 1);
                                    }
                                    docs = clazz.getDocumentsFromFS(chunk);//grab another handful of documents
                                }
                                logger.info("Thread " + Thread.currentThread().getName()
                                        + " is done. It processed " + counter.get() + " documents.");
                            } catch (IOException | ParseException e) {
                                logger.error("Couldn't read from document", e);
                            }
                        }
                    });
                }
            }

            long start = new Date().getTime();
            //start your threads!
            for (Thread t : workers) {
                t.start();
            }
            logger.info("All threads started, waiting for completion.");
            boolean allDone = false;
            boolean first = true;
            while (!allDone || first) {
                first = false;
                boolean done = true;
                for (Thread t : workers) {
                    if (t.isAlive()) {
                        done = false;
                        logger.info("Thread " + t.getName() + " is still running.");
                        break;
                    }
                }
                if (done) {
                    allDone = true;
                } else {
                    logger.info("We still have workers running...");
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                    }
                }
            }
            long end = new Date().getTime();
            long miliseconds = end - start;
            double seconds = (double) miliseconds / 1000d;
            output.info("Done loading data using: " + NUM_WORKERS + ". Took: " + seconds + " seconds");
            double tpms = (double) numDocs / (double) miliseconds;
            double tps = tpms * 1000;
            double transactionTime = (double) miliseconds / (double) numDocs;
            output.info(database.name() + " Mongo Average Transactions Per Second: " + tps);
            output.info(
                    database.name() + " Mongo Average Transactions Time (in miliseconds): " + transactionTime);

        } catch (UnknownHostException e) {
            logger.error("Couldn't connect to Mongo Server", e);
        }
    } catch (IOException | ParseException e) {
        logger.error("Couldn't read data.", e);
    }
}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroLastError.java

License:Open Source License

@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
private WriteConcern getWriteConcern(Object w, boolean j, boolean fsync, int wtimeout) {
    WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;

    if (w instanceof Number) {
        if (((Number) w).intValue() <= 1 && wtimeout > 0) {
            throw new IllegalArgumentException("wtimeout cannot be grater than 0 for w <= 1");
        }//w w w .  j  a  va2  s. c  o  m

        writeConcern = new WriteConcern(((Number) w).intValue(), wtimeout, fsync, j);
    } else if (w instanceof String && w.equals("majority")) {
        if (wtimeout > 0) {
            throw new IllegalArgumentException("wtimeout cannot be grater than 0 for w <= 1");
        }

        writeConcern = new WriteConcern.Majority(wtimeout, fsync, j);
    } else {
        throw new IllegalArgumentException("w:" + w + " is not supported");
    }

    return writeConcern;
}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java

License:Open Source License

private WriteConcern getWriteConcern(BSONDocument document) {
    WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;
    if (document.hasKey("writeConcern")) {
        BSONObject writeConcernObject = (BSONObject) document.getValue("writeConcern");
        Object w = writeConcernObject.get("w");
        int wtimeout = 0;
        boolean fsync = false;
        boolean j = false;
        boolean continueOnError = false;
        Object jObject = writeConcernObject.get("j");
        if (jObject != null && jObject instanceof Boolean && (Boolean) jObject) {
            fsync = true;// www .j av a2 s.  c o m
            j = true;
            continueOnError = true;
        }
        Object wtimeoutObject = writeConcernObject.get("wtimneout");
        if (wtimeoutObject != null && wtimeoutObject instanceof Number) {
            wtimeout = ((Number) wtimeoutObject).intValue();
        }
        if (w != null) {
            if (w instanceof Number) {
                if (((Number) w).intValue() <= 1 && wtimeout > 0) {
                    throw new IllegalArgumentException("wtimeout cannot be grater than 0 for w <= 1");
                }

                writeConcern = new WriteConcern(((Number) w).intValue(), wtimeout, fsync, j);
            } else if (w instanceof String && w.equals("majority")) {
                if (wtimeout > 0) {
                    throw new IllegalArgumentException("wtimeout cannot be grater than 0 for w <= 1");
                }

                writeConcern = new WriteConcern.Majority(wtimeout, fsync, j);
            } else {
                throw new IllegalArgumentException("w:" + w + " is not supported");
            }
        }
    }
    return writeConcern;
}