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();
}
}
});
}
};
}
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);
}
}
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;
}
}
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;
}
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);
}
}
Aggregations