com.mongodb.ReadPreference Java Examples

The following examples show how to use com.mongodb.ReadPreference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EmbedMongoConfiguration.java    From syndesis with Apache License 2.0 8 votes vote down vote up
private static MongoClient getClient(Boolean useCredentials, String replicaSet) {
    MongoClientSettings.Builder settings = MongoClientSettings.builder();

    if (useCredentials) {
        MongoCredential credentials = MongoCredential.createCredential(
            USER, ADMIN_DB, PASSWORD.toCharArray());
        settings.credential(credentials);
    }
    StringBuilder connectionString = new StringBuilder(String.format("mongodb://%s:%d", HOST, PORT));
    if (replicaSet != null) {
        connectionString.append(String.format("/?replicaSet=%s", REPLICA_SET));
    }
    ConnectionString uri = new ConnectionString(connectionString.toString());
    settings.applyConnectionString(uri);

    settings.readPreference(ReadPreference.primaryPreferred());

    return MongoClients.create(settings.build());
}
 
Example #2
Source File: MongoDBConfig.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public void init(
    Stage.Context context,
    List<Stage.ConfigIssue> issues,
    ReadPreference readPreference,
    WriteConcern writeConcern
) {
  mongoClient = createClient(context, issues, readPreference, writeConcern);
  if (!issues.isEmpty()) {
    return;
  }

  mongoDatabase = createMongoDatabase(context, issues, readPreference, writeConcern);
  if (!issues.isEmpty()) {
    return;
  }

  mongoCollection = createMongoCollection(context, issues, readPreference, writeConcern);
}
 
Example #3
Source File: MongoConfiguration.java    From mongolastic with MIT License 6 votes vote down vote up
private void prepareClient() {
    try {
        ServerAddress address = new ServerAddress(config.getMongo().getHost(), config.getMongo().getPort());
        MongoClientOptions options = MongoClientOptions.builder()
                .serverSelectionTimeout(5000)
                .socketKeepAlive(false)
                .readPreference(ReadPreference.primaryPreferred())
                .sslInvalidHostNameAllowed(true)
                .build();

         client = connectToClient(address, options);
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
        System.exit(-1);
    }
}
 
Example #4
Source File: SecondaryReadRepositoryTest.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
/**
 * Test when an empty entry is specified in a tag set is set in the properties.
 *
 */
@Test
public void testEmptyEntry() {
    secondaryReadRepository.setFailOnPrimary(false);
    String tagSetProperty = "[{\"name\" : \"}, , {\"name1\" : \"value1\", \"name2\" : \"value2\"}]";
    secondaryReadRepository.setTagSet(tagSetProperty);
    DBObject[] tags = secondaryReadRepository.getTagSetsFromProperty();
    Assert.assertNull(tags);
    secondaryReadRepository.setReadProperties();
    Mockito.verify(template).setReadPreference(ReadPreference.secondaryPreferred());
}
 
Example #5
Source File: MongoDBDriver.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public static ReadPreferenceChoice getReadPreferenceChoice( ReadPreference readPref )
{
    if( readPref == null )
        return PRIMARY;     // default
   String readPrefName = readPref.getName();
    if( readPrefName == ReadPreference.primary().getName() )
        return PRIMARY;
    if( readPrefName == ReadPreference.primaryPreferred().getName() )
        return PRIMARY_PREFERRED;
    if( readPrefName == ReadPreference.secondary().getName() )
        return SECONDARY;
    if( readPrefName == ReadPreference.secondaryPreferred().getName() )
        return SECONDARY_PREFERRED;
    if( readPrefName == ReadPreference.nearest().getName() )
        return NEAREST;
    return PRIMARY;     // default
}
 
Example #6
Source File: MongoHealthChecker.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
private MongoHealthChecker() {

        final DefaultMongoDbConfig mongoDbConfig = DefaultMongoDbConfig.of(
                DefaultScopedConfig.dittoScoped(getContext().getSystem().settings().config()));
        mongoClient = MongoClientWrapper.getBuilder(mongoDbConfig)
                .connectionPoolMaxSize(HEALTH_CHECK_MAX_POOL_SIZE)
                .build();

        /*
         * It's important to have the read preferences to primary preferred because the replication is to slow to retrieve
         * the inserted document from a secondary directly after inserting it on the primary.
         */
        collection = mongoClient.getCollection(TEST_COLLECTION_NAME)
                .withReadPreference(ReadPreference.primaryPreferred());

        materializer = ActorMaterializer.create(getContext());
    }
 
Example #7
Source File: MongoReadPreferenceInterceptor.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
    if (isDebug) {
        logger.afterInterceptor(target, args, result, throwable);
    }

    if (args == null) {
        return;
    }

    DatabaseInfo databaseInfo = DatabaseInfoUtils.getDatabaseInfo(target, UnKnownDatabaseInfo.MONGO_INSTANCE);

    String readPreference = ((ReadPreference) args[0]).getName();

    databaseInfo = new MongoDatabaseInfo(databaseInfo.getType(), databaseInfo.getExecuteQueryType()
            , databaseInfo.getRealUrl(), databaseInfo.getUrl(), databaseInfo.getHost(), databaseInfo.getDatabaseId()
            , ((MongoDatabaseInfo) databaseInfo).getCollectionName(), readPreference, ((MongoDatabaseInfo) databaseInfo).getWriteConcern());

    if (result instanceof DatabaseInfoAccessor) {
        ((DatabaseInfoAccessor) result)._$PINPOINT$_setDatabaseInfo(databaseInfo);
    }
}
 
Example #8
Source File: ReadPreferenceParserTest.java    From vertx-mongo-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testConnStringReadPreferenceTags() {
  final ConnectionString connString = new ConnectionString("mongodb://localhost:27017/mydb?replicaSet=myapp" +
      "&readPreference=nearest" +
      "&readPreferenceTags=dc:ny,rack:1" +
      "&readPreferenceTags=dc:ny" +
      "&readPreferenceTags=");

  List<TagSet> tagSets = new ArrayList<>();
  List<Tag> tags = new ArrayList<>();
  tags.add(new Tag("dc", "ny"));
  tags.add(new Tag("rack", "1"));
  tagSets.add(new TagSet(tags));
  tags = new ArrayList<>();
  tags.add(new Tag("dc", "ny"));
  tagSets.add(new TagSet(tags));
  tagSets.add(new TagSet());

  ReadPreference expected = ReadPreference.valueOf("nearest", tagSets);

  ReadPreference rp = new ReadPreferenceParser(connString, new JsonObject()).readPreference();
  assertNotNull(rp);
  assertEquals(expected, rp);
}
 
Example #9
Source File: MongoDBConfig.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private MongoCollection createMongoCollection(
    Stage.Context context,
    List<Stage.ConfigIssue> issues,
    ReadPreference readPreference,
    WriteConcern writeConcern
) {
  MongoCollection mongoCollection = null;
  try {
    if (readPreference != null) {
      mongoCollection = mongoDatabase.getCollection(collection).withReadPreference(readPreference);
    } else if (writeConcern != null) {
      mongoCollection = mongoDatabase.getCollection(collection).withWriteConcern(writeConcern);
    }
  } catch (MongoClientException e) {
    issues.add(context.createConfigIssue(
        Groups.MONGODB.name(),
        MONGO_CONFIG_PREFIX + "collection",
        Errors.MONGODB_03,
        collection,
        e.toString()
    ));
  }
  return mongoCollection;
}
 
Example #10
Source File: DbConfig.java    From render with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Configure using the specified parameters.
 */
public DbConfig(final List<ServerAddress> serverAddressList,
                final String userName,
                final String authenticationDatabase,
                final String password,
                final ReadPreference readPreference) {
    this.connectionString = null;
    this.serverAddressList = new ArrayList<>(serverAddressList);
    this.userName = userName;
    this.authenticationDatabase = authenticationDatabase;
    this.password = password;
    this.maxConnectionsPerHost = new MongoClientOptions.Builder().build().getConnectionsPerHost(); // 100
    this.maxConnectionIdleTime = 600000; // 10 minutes
    this.readPreference = readPreference;
}
 
Example #11
Source File: MongoClientWrapper.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private MongoClientWrapperBuilder() {
    mongoClientSettingsBuilder = MongoClientSettings.builder();
    mongoClientSettingsBuilder.readPreference(ReadPreference.primaryPreferred());
    dittoMongoClientSettingsBuilder = DittoMongoClientSettings.getBuilder();
    connectionString = null;
    defaultDatabaseName = null;
    sslEnabled = false;
    eventLoopGroup = null;
}
 
Example #12
Source File: MongoThingsSearchPersistence.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Initializes the things search persistence with a passed in {@code persistence}.
 *
 * @param mongoClient the mongoDB persistence wrapper.
 * @param actorSystem the Akka ActorSystem.
 * @since 1.0.0
 */
public MongoThingsSearchPersistence(final DittoMongoClient mongoClient, final ActorSystem actorSystem) {
    final MongoDatabase database = mongoClient.getDefaultDatabase();
    // configure search persistence to stress the primary as little as possible and tolerate inconsistency
    collection = database
            .getCollection(PersistenceConstants.THINGS_COLLECTION_NAME)
            .withReadPreference(ReadPreference.secondaryPreferred());

    log = Logging.getLogger(actorSystem, getClass());
    final ActorMaterializer materializer = ActorMaterializer.create(actorSystem);
    indexInitializer = IndexInitializer.of(database, materializer);
    maxQueryTime = mongoClient.getDittoSettings().getMaxQueryTime();
    hints = MongoHints.empty();
}
 
Example #13
Source File: EclipseLinkConfiguration.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private void configureClientOptions(final Map<String, Object> properties) {
    final MongoClientOptions.Builder builder = MongoClientOptions.builder();
    final String writeConcern = (String) properties.get(ECLIPSELINK_NOSQL_PROPERTY_MONGO_WRITE_CONCERN);
    final String readPreference = (String) properties.get(ECLIPSELINK_NOSQL_PROPERTY_MONGO_READ_PREFERENCE);

    if (writeConcern != null) {
        builder.writeConcern(WriteConcern.valueOf(writeConcern));
    }
    if (readPreference != null) {
        builder.readPreference(ReadPreference.valueOf(readPreference));
    }

    mongoClientOptions = builder.build();
}
 
Example #14
Source File: ReadPreferenceParser.java    From vertx-mongo-client with Apache License 2.0 5 votes vote down vote up
ReadPreferenceParser(ConnectionString connectionString, JsonObject config) {
  ReadPreference connStringReadPreference = connectionString != null ? connectionString.getReadPreference() : null;
  if (connStringReadPreference != null) {
    // Prefer connection string's read preference
    readPreference = connStringReadPreference;
  } else {
    ReadPreference rp;
    String rps = config.getString("readPreference");
    if (rps != null) {
      JsonArray readPreferenceTags = config.getJsonArray("readPreferenceTags");
      if (readPreferenceTags == null) {
        rp = ReadPreference.valueOf(rps);
        if (rp == null) throw new IllegalArgumentException("Invalid ReadPreference " + rps);
      } else {
        // Support advanced ReadPreference Tags
        List<TagSet> tagSet = new ArrayList<>();
        readPreferenceTags.forEach(o -> {
          String tagString = (String) o;
          List<Tag> tags = Stream.of(tagString.trim().split(","))
              .map(s -> s.split(":"))
              .filter(array -> {
                if (array.length != 2) {
                  throw new IllegalArgumentException("Invalid readPreferenceTags value '" + tagString + "'");
                }
                return true;
              }).map(array -> new Tag(array[0], array[1])).collect(Collectors.toList());

          tagSet.add(new TagSet(tags));
        });
        rp = ReadPreference.valueOf(rps, tagSet);
      }
    } else {
      rp = null;
    }

    readPreference = rp;
  }
}
 
Example #15
Source File: EclipseLinkConfigurationTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testMongoClientOptions() {
    // GIVEN
    final Map<String, Object> properties = new HashMap<>();
    when(descriptor.getProperties()).thenReturn(properties);

    properties.put("eclipselink.nosql.property.mongo.db", "foo");

    // it looks like only the two options below are supported by EclipseLink

    final ReadPreference readPreference = ReadPreference.nearest();
    final WriteConcern writeConcern = WriteConcern.JOURNALED;

    properties.put("eclipselink.nosql.property.mongo.read-preference", readPreference.getName());
    properties.put("eclipselink.nosql.property.mongo.write-concern", "JOURNALED");

    final ConfigurationFactory factory = new ConfigurationFactoryImpl();

    // WHEN
    final Configuration configuration = factory.createConfiguration(descriptor);

    // THEN
    assertThat(configuration, notNullValue());

    final MongoClientOptions clientOptions = configuration.getClientOptions();
    assertThat(clientOptions, notNullValue());
    assertThat(clientOptions.getReadPreference(), equalTo(readPreference));
    assertThat(clientOptions.getWriteConcern(), equalTo(writeConcern));
}
 
Example #16
Source File: QueryProperties.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public ReadPreference getQueryReadPreference()
{
    Object propValue = getPropertiesMap().get( QUERY_READ_PREF_PROP );
    if( propValue instanceof String )
        propValue = toReadPreference( ((String)propValue) );
    if( propValue instanceof ReadPreference )
    {
        // return explicit read preference mode to prevent confusion
        return (ReadPreference)propValue;
    }
    return ReadPreferenceChoice.DEFAULT_PREFERENCE;    // non-recognized data type; use default
}
 
Example #17
Source File: ReadPreferenceParserTest.java    From vertx-mongo-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadPreferenceCaseInsenitive() {
  JsonObject config = new JsonObject();
  config.put("readPreference", "PRIMARY");

  ReadPreference rp = new ReadPreferenceParser(null, config).readPreference();
  assertNotNull(rp);
  assertEquals(ReadPreference.primary(), rp);
}
 
Example #18
Source File: QueryProperties.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static void externalizePropValues( Map<String,Object> propertiesMap )
{
    // convert property value object not serializable by BSON serializer
    // to its string representation
    Object propValue = propertiesMap.get( QUERY_READ_PREF_PROP );
    if( propValue instanceof ReadPreference )
        propertiesMap.put( QUERY_READ_PREF_PROP, ((ReadPreference) propValue).getName() );
 
    propValue = propertiesMap.get( QUERY_OPERATION_TYPE_PROP );
    if( propValue instanceof CommandOperationType )
        propertiesMap.put( QUERY_OPERATION_TYPE_PROP, propValue.toString() );
}
 
Example #19
Source File: AggregationPipelineImpl.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public <U> Iterator<U> aggregate(final String collectionName, final Class<U> target,
                                 final AggregationOptions options,
                                 final ReadPreference readPreference) {
    LOG.debug("stages = " + stages);


    AggregateIterable<U> cursor = collection.aggregate(stages, target);
    return cursor.iterator();
}
 
Example #20
Source File: ReadPreferenceParserTest.java    From vertx-mongo-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadPreferenceTags() {
  List<TagSet> tagSets = new ArrayList<>();
  List<Tag> tags = new ArrayList<>();
  tags.add(new Tag("dc1", "ny"));
  tags.add(new Tag("dc2", "tx"));
  tags.add(new Tag("dc3", "ca"));
  tagSets.add(new TagSet(tags));

  tags = new ArrayList<>();
  tags.add(new Tag("ac1", "ny"));
  tags.add(new Tag("ac2", "tx"));
  tags.add(new Tag("ac3", "ca"));
  tagSets.add(new TagSet(tags));

  ReadPreference expected = ReadPreference.valueOf("nearest", tagSets);

  JsonObject config = new JsonObject();
  config.put("readPreference", "nearest");

  JsonArray array = new JsonArray();
  array.add("dc1:ny,dc2:tx,dc3:ca");
  array.add("ac1:ny,ac2:tx,ac3:ca");
  config.put("readPreferenceTags", array);

  ReadPreference rp = new ReadPreferenceParser(null, config).readPreference();
  assertNotNull(rp);
  assertEquals(expected, rp);
}
 
Example #21
Source File: MongoConfig.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
public MongoConfig(DataService dataService, String configId, Map<String, String> properties, boolean odataEnable)
        throws DataServiceFault {
    super(dataService, configId, DBConstants.DataSourceTypes.MONGODB, properties, odataEnable);
    String serversParam = properties.get(DBConstants.MongoDB.SERVERS);
    if (DBUtils.isEmptyString(serversParam)) {
        throw new DataServiceFault("The data source param '" + DBConstants.MongoDB.SERVERS + "' is required");
    }
    this.servers = serversParam.split(",");
    String database = properties.get(DBConstants.MongoDB.DATABASE);
    if (DBUtils.isEmptyString(database)) {
        throw new DataServiceFault("The data source param '" + DBConstants.MongoDB.DATABASE + "' is required");
    }
    try {
        this.mongoClientOptions = extractMongoOptions(properties);
        this.mongoClient = createNewMongo(properties);
        String writeConcern = properties.get(DBConstants.MongoDB.WRITE_CONCERN);
        if (!DBUtils.isEmptyString(writeConcern)) {
            this.getMongoClient().setWriteConcern(WriteConcern.valueOf(writeConcern));
        }
        String readPref = properties.get(DBConstants.MongoDB.READ_PREFERENCE);
        if (!DBUtils.isEmptyString(readPref)) {
            this.getMongoClient().setReadPreference(ReadPreference.valueOf(readPref));
        }
        this.getMongoClient().getDatabase(database);
        this.jongo = new Jongo(this.getMongoClient().getDB(database));
    } catch (Exception e) {
        throw new DataServiceFault(e, DBConstants.FaultCodes.CONNECTION_UNAVAILABLE_ERROR, e.getMessage());
    }

}
 
Example #22
Source File: SecondaryReadRepositoryTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
/**
 * Test read preference when multiple tag-set are set.
 */
@Test
public void testMultipleTagSets() {
    secondaryReadRepository.setFailOnPrimary(true);
    String tagSetProperty = "[{\"name\" : \"value\"}, {\"name1\" : \"value1\", \"name2\" : \"value2\"}, {\"name3\" : \"value3\"}]";
    secondaryReadRepository.setTagSet(tagSetProperty);
    DBObject firstTagSet = secondaryReadRepository.getFirstTagSet(secondaryReadRepository.getTagSetsFromProperty());
    DBObject[] remainingTagSets = secondaryReadRepository.getRemainingTagSets(secondaryReadRepository.getTagSetsFromProperty());
    secondaryReadRepository.setReadProperties();
    Mockito.verify(template).setReadPreference(ReadPreference.secondary(firstTagSet, remainingTagSets));
}
 
Example #23
Source File: MongoDBTestHelper.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
/** @return The {@link DBObject} representing the object with the given id */
public static DBObject getById(AbstractMongoDBServer entity, String id) {
    LOG.info("Getting {} from {}", new Object[]{id, entity});
    MongoClient mongoClient = clientForServer(entity);
    // Secondary preferred means the driver will let us read from secondaries too.
    mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
    try {
        DB db = mongoClient.getDB(TEST_DB);
        DBCollection testCollection = db.getCollection(TEST_COLLECTION);
        return testCollection.findOne(new BasicDBObject("_id", new ObjectId(id)));
    } finally {
        mongoClient.close();
    }
}
 
Example #24
Source File: SecondaryReadRepositoryTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
/**
 * Test failure on primary.
 */
@Test
public void testFailOnPrimary() {
    secondaryReadRepository.setFailOnPrimary(true);
    String tagSetProperty = "";
    secondaryReadRepository.setTagSet(tagSetProperty);
    secondaryReadRepository.setReadProperties();
    Mockito.verify(template).setReadPreference(ReadPreference.secondary());
}
 
Example #25
Source File: MongoClientTemplet.java    From mongodb-orm with Apache License 2.0 4 votes vote down vote up
@Override
public <T> List<T> distinct(String statement, String key, Object parameter) {
  return distinct(statement, key, parameter, null, ReadPreference.secondaryPreferred());
}
 
Example #26
Source File: MongoDatabaseImpl.java    From mongo-java-driver-reactivestreams with Apache License 2.0 4 votes vote down vote up
@Override
public ReadPreference getReadPreference() {
    return wrapped.getReadPreference();
}
 
Example #27
Source File: MongoClientTemplet.java    From mongodb-orm with Apache License 2.0 4 votes vote down vote up
@Override
public void group(String statement, ResultHandler handler) {
  group(statement, null, handler, ReadPreference.secondaryPreferred());
}
 
Example #28
Source File: MongoClientTemplet.java    From mongodb-orm with Apache License 2.0 4 votes vote down vote up
private <T> List<T> aggregate(String statement, Object parameter, ResultHandler handler, ReadPreference readPreference) {
  if (logger.isDebugEnabled()) {
    logger.debug("Execute 'aggregate' mongodb command. Statement '" + statement + "'.");
  }

  AggregateConfig config = (AggregateConfig) configuration.getStatement(statement);
  if (config == null) {
    throw new MongoDaoException(statement, "Aggregate statement id '" + statement + "' not found.");
  }

  String collection = config.getCollection();
  Map<String, NodeEntry> function = config.getFunction();
  NodeEntry field = config.getField();

  MongoDatabase db = getDatabase();
  MongoCollection<Document> coll = db.getCollection(collection).withReadPreference(readPreference);

  List<Document> operations = new ArrayList<Document>(function.size());
  for (Map.Entry<String, NodeEntry> entry : function.entrySet()) {
    NodeEntry ne = entry.getValue();
    Map<String, Object> op = (Map<String, Object>) ne.executorNode(config.getNamespace(), configuration, parameter);
    Document operation = new Document(op);
    operations.add(operation);
    if (logger.isDebugEnabled()) {
      logger.debug("Execute 'aggregate' mongodb command. Operation '" + operation + "'.");
    }
  }

  AggregateIterable<Document> iterable = coll.aggregate(operations);

  List<Document> list = new ArrayList<Document>();
  MongoCursor<Document> iterator = iterable.iterator();
  while (iterator.hasNext()) {
    list.add(iterator.next());
  }

  if (logger.isDebugEnabled()) {
    logger.debug("Execute 'aggregate' mongodb command. Result set '" + list + "'.");
  }

  if (handler != null) {
    handler.handleResult(new ResultContext() {
      @Override
      public Object getResultObject() {
        return list;
      }

      @Override
      public int getResultCount() {
        return list.size();
      }
    });
    return null;
  }

  List<T> result = new ArrayList<T>(list.size());
  for (Document doc : list) {
    T t = (T) helper.toResult(config.getNamespace(), field, doc);
    result.add(t);
  }
  return result;
}
 
Example #29
Source File: DbConfig.java    From render with GNU General Public License v2.0 4 votes vote down vote up
public ReadPreference getReadPreference() {
    return readPreference;
}
 
Example #30
Source File: SecondaryBlogDao.java    From bugu-mongo with Apache License 2.0 4 votes vote down vote up
public SecondaryBlogDao() {
    super(Blog.class);
    //set ReadPreference to secondaryPreferred
    super.setReadPreference(ReadPreference.secondaryPreferred());
}