Search in sources :

Example 1 with ReadPreference

use of com.mongodb.ReadPreference in project mongo-java-driver by mongodb.

the class MongoClientImpl method createOperationExecutor.

private static AsyncOperationExecutor createOperationExecutor(final MongoClientSettings settings, final Cluster cluster) {
    return new AsyncOperationExecutor() {

        @Override
        public <T> void execute(final AsyncReadOperation<T> operation, final ReadPreference readPreference, final SingleResultCallback<T> callback) {
            notNull("operation", operation);
            notNull("readPreference", readPreference);
            notNull("callback", callback);
            final SingleResultCallback<T> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
            final AsyncReadBinding binding = getReadWriteBinding(readPreference, cluster);
            operation.executeAsync(binding, new SingleResultCallback<T>() {

                @Override
                public void onResult(final T result, final Throwable t) {
                    try {
                        errHandlingCallback.onResult(result, t);
                    } finally {
                        binding.release();
                    }
                }
            });
        }

        @Override
        public <T> void execute(final AsyncWriteOperation<T> operation, final SingleResultCallback<T> callback) {
            notNull("operation", operation);
            notNull("callback", callback);
            final AsyncWriteBinding binding = getReadWriteBinding(ReadPreference.primary(), cluster);
            operation.executeAsync(binding, new SingleResultCallback<T>() {

                @Override
                public void onResult(final T result, final Throwable t) {
                    try {
                        errorHandlingCallback(callback, LOGGER).onResult(result, t);
                    } finally {
                        binding.release();
                    }
                }
            });
        }
    };
}
Also used : ReadPreference(com.mongodb.ReadPreference) AsyncOperationExecutor(com.mongodb.operation.AsyncOperationExecutor) AsyncReadBinding(com.mongodb.binding.AsyncReadBinding) SingleResultCallback(com.mongodb.async.SingleResultCallback) AsyncWriteBinding(com.mongodb.binding.AsyncWriteBinding) AsyncWriteOperation(com.mongodb.operation.AsyncWriteOperation) AsyncReadOperation(com.mongodb.operation.AsyncReadOperation)

Example 2 with ReadPreference

use of com.mongodb.ReadPreference in project mongo-java-driver by mongodb.

the class ServerSelectionSelectionTest method getServerSelector.

private ServerSelector getServerSelector() {
    if (definition.getString("operation", new BsonString("read")).getValue().equals("write")) {
        return new WritableServerSelector();
    } else {
        BsonDocument readPreferenceDefinition = definition.getDocument("read_preference");
        ReadPreference readPreference;
        if (readPreferenceDefinition.getString("mode").getValue().equals("Primary")) {
            readPreference = ReadPreference.valueOf("Primary");
        } else if (readPreferenceDefinition.containsKey("maxStalenessSeconds")) {
            readPreference = ReadPreference.valueOf(readPreferenceDefinition.getString("mode", new BsonString("Primary")).getValue(), buildTagSets(readPreferenceDefinition.getArray("tag_sets", new BsonArray())), Math.round(readPreferenceDefinition.getNumber("maxStalenessSeconds").doubleValue() * 1000), TimeUnit.MILLISECONDS);
        } else {
            readPreference = ReadPreference.valueOf(readPreferenceDefinition.getString("mode", new BsonString("Primary")).getValue(), buildTagSets(readPreferenceDefinition.getArray("tag_sets", new BsonArray())));
        }
        return new ReadPreferenceServerSelector(readPreference);
    }
}
Also used : ReadPreference(com.mongodb.ReadPreference) WritableServerSelector(com.mongodb.selector.WritableServerSelector) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ReadPreferenceServerSelector(com.mongodb.selector.ReadPreferenceServerSelector)

Example 3 with ReadPreference

use of com.mongodb.ReadPreference in project drill by apache.

the class MongoGroupScan method getPreferredHosts.

@SuppressWarnings("unchecked")
private Set<ServerAddress> getPreferredHosts(MongoClient client, List<String> hosts) {
    Set<ServerAddress> addressList = Sets.newHashSet();
    MongoDatabase db = client.getDatabase(scanSpec.getDbName());
    ReadPreference readPreference = client.getReadPreference();
    Document command = db.runCommand(new Document("isMaster", 1));
    final String primaryHost = command.getString("primary");
    final List<String> hostsList = (List<String>) command.get("hosts");
    switch(readPreference.getName().toUpperCase()) {
        case "PRIMARY":
        case "PRIMARYPREFERRED":
            if (primaryHost == null) {
                return null;
            }
            addressList.add(new ServerAddress(primaryHost));
            return addressList;
        case "SECONDARY":
        case "SECONDARYPREFERRED":
            if (primaryHost == null || hostsList == null) {
                return null;
            }
            hostsList.remove(primaryHost);
            for (String host : hostsList) {
                addressList.add(new ServerAddress(host));
            }
            return addressList;
        case "NEAREST":
            if (hostsList == null) {
                return null;
            }
            for (String host : hostsList) {
                addressList.add(new ServerAddress(host));
            }
            return addressList;
        default:
            return null;
    }
}
Also used : ReadPreference(com.mongodb.ReadPreference) ServerAddress(com.mongodb.ServerAddress) List(java.util.List) ArrayList(java.util.ArrayList) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 4 with ReadPreference

use of com.mongodb.ReadPreference in project jackrabbit-oak by apache.

the class MongoBlobStore method getBlob.

private MongoBlob getBlob(String id, long lastMod) {
    DBObject query = getBlobQuery(id, lastMod);
    // try the secondary first
    // TODO add a configuration option for whether to try reading from secondary
    ReadPreference pref = ReadPreference.secondaryPreferred();
    DBObject fields = new BasicDBObject();
    fields.put(MongoBlob.KEY_DATA, 1);
    MongoBlob blob = (MongoBlob) getBlobCollection().findOne(query, fields, pref);
    if (blob == null) {
        // not found in the secondary: try the primary
        pref = ReadPreference.primary();
        blob = (MongoBlob) getBlobCollection().findOne(query, fields, pref);
    }
    return blob;
}
Also used : ReadPreference(com.mongodb.ReadPreference) BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 5 with ReadPreference

use of com.mongodb.ReadPreference in project jackrabbit-oak by apache.

the class MongoDocumentStore method setReadWriteMode.

@Override
public void setReadWriteMode(String readWriteMode) {
    if (readWriteMode == null || readWriteMode.equals(lastReadWriteMode)) {
        return;
    }
    lastReadWriteMode = readWriteMode;
    try {
        String rwModeUri = readWriteMode;
        if (!readWriteMode.startsWith("mongodb://")) {
            rwModeUri = String.format("mongodb://localhost/?%s", readWriteMode);
        }
        MongoClientURI uri = new MongoClientURI(rwModeUri);
        ReadPreference readPref = uri.getOptions().getReadPreference();
        if (!readPref.equals(nodes.getReadPreference())) {
            nodes.setReadPreference(readPref);
            LOG.info("Using ReadPreference {} ", readPref);
        }
        WriteConcern writeConcern = uri.getOptions().getWriteConcern();
        if (!writeConcern.equals(nodes.getWriteConcern())) {
            nodes.setWriteConcern(writeConcern);
            LOG.info("Using WriteConcern " + writeConcern);
        }
    } catch (Exception e) {
        LOG.error("Error setting readWriteMode " + readWriteMode, e);
    }
}
Also used : ReadPreference(com.mongodb.ReadPreference) WriteConcern(com.mongodb.WriteConcern) MongoClientURI(com.mongodb.MongoClientURI) MongoException(com.mongodb.MongoException) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) BulkWriteException(com.mongodb.BulkWriteException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ReadPreference (com.mongodb.ReadPreference)11 BasicDBObject (com.mongodb.BasicDBObject)3 DBObject (com.mongodb.DBObject)3 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)3 Stopwatch (com.google.common.base.Stopwatch)2 DBCollection (com.mongodb.DBCollection)2 WriteConcern (com.mongodb.WriteConcern)2 AbstractMongoConnectionTest (org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest)2 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)2 DocumentReadPreference (org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.DocumentReadPreference)2 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)2 NodeStore (org.apache.jackrabbit.oak.spi.state.NodeStore)2 Test (org.junit.Test)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 BulkWriteException (com.mongodb.BulkWriteException)1 DBCursor (com.mongodb.DBCursor)1 MongoClientURI (com.mongodb.MongoClientURI)1 MongoException (com.mongodb.MongoException)1 QueryBuilder (com.mongodb.QueryBuilder)1 ServerAddress (com.mongodb.ServerAddress)1