Example usage for com.mongodb MongoClientURI getOptions

List of usage examples for com.mongodb MongoClientURI getOptions

Introduction

In this page you can find the example usage for com.mongodb MongoClientURI getOptions.

Prototype

public MongoClientOptions getOptions() 

Source Link

Document

Gets the options

Usage

From source file:com.streamsets.pipeline.stage.common.mongodb.MongoDBConfig.java

License:Apache License

private MongoClient createClient(Stage.Context context, List<Stage.ConfigIssue> issues,
        ReadPreference readPreference, WriteConcern writeConcern) {
    MongoClientOptions.Builder optionBuilder = new MongoClientOptions.Builder()
            .connectionsPerHost(connectionsPerHost).connectTimeout(connectTimeout)
            .cursorFinalizerEnabled(cursorFinalizerEnabled).heartbeatConnectTimeout(heartbeatConnectTimeout)
            .heartbeatFrequency(heartbeatFrequency).heartbeatSocketTimeout(heartbeatSocketTimeout)
            .localThreshold(localThreshold).maxConnectionIdleTime(maxConnectionIdleTime)
            .maxConnectionLifeTime(maxConnectionLifeTime).maxWaitTime(maxWaitTime)
            .minConnectionsPerHost(minConnectionsPerHost).minHeartbeatFrequency(minHeartbeatFrequency)
            .serverSelectionTimeout(serverSelectionTimeout).socketKeepAlive(socketKeepAlive)
            .socketTimeout(socketTimeout).sslEnabled(sslEnabled)
            .sslInvalidHostNameAllowed(sslInvalidHostNameAllowed)
            .threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier);

    // the default value of requiredReplicaSetName is null, so it should be set only if a non-empty string is provided
    if (!requiredReplicaSetName.isEmpty()) {
        optionBuilder = optionBuilder.requiredReplicaSetName(requiredReplicaSetName);
    }/*from   ww  w  .  ja  v a 2  s  . c o m*/
    // read preference is only set by the source
    if (readPreference != null) {
        optionBuilder = optionBuilder.readPreference(readPreference);
    }
    // write concern is only set by the target
    if (writeConcern != null) {
        optionBuilder = optionBuilder.writeConcern(writeConcern);
    }

    MongoClientURI mongoURI;
    List<ServerAddress> servers = new ArrayList<>();
    try {
        mongoURI = new MongoClientURI(connectionString, optionBuilder);
    } catch (IllegalArgumentException e) {
        issues.add(context.createConfigIssue(Groups.MONGODB.name(), MONGO_CONFIG_PREFIX + "connectionString",
                Errors.MONGODB_00, e.toString()));
        return null;
    }

    validateServerList(context, mongoURI.getHosts(), servers, issues);
    if (!issues.isEmpty()) {
        return null;
    }

    MongoClient mongoClient = null;
    List<MongoCredential> credentials;
    try {
        credentials = createCredentials();
    } catch (StageException ex) {
        issues.add(context.createConfigIssue(Groups.MONGODB.name(), MONGO_CONFIG_PREFIX + "connectionString",
                Errors.MONGODB_34, ex.toString()));
        return null;
    }

    if (credentials.isEmpty()) {
        Optional.ofNullable(mongoURI.getCredentials()).ifPresent(credentials::add);
    }

    try {
        if (isSingleMode) {
            mongoClient = new MongoClient(servers.get(0), credentials, mongoURI.getOptions());
        } else {
            mongoClient = new MongoClient(servers, credentials, mongoURI.getOptions());
        }
    } catch (MongoException e) {
        issues.add(context.createConfigIssue(Groups.MONGODB.name(), MONGO_CONFIG_PREFIX + "connectionString",
                Errors.MONGODB_01, e.toString()));
    }

    return mongoClient;
}

From source file:com.yahoo.ycsb.db3.MongoDbClient.java

License:Open Source License

/**
 * Initialize any state for this DB. Called once per DB instance; there is one
 * DB instance per client thread./*from ww  w  . j  av a 2  s .  c om*/
 */
@Override
public void init() throws DBException {
    INIT_COUNT.incrementAndGet();
    synchronized (INCLUDE) {
        if (mongoClient != null) {
            return;
        }

        Properties props = getProperties();

        // Set insert batchsize, default 1 - to be YCSB-original equivalent
        batchSize = Integer.parseInt(props.getProperty("batchsize", "1"));

        // Set is inserts are done as upserts. Defaults to false.
        useUpsert = Boolean.parseBoolean(props.getProperty("mongodb.upsert", "false"));

        // Just use the standard connection format URL
        // http://docs.mongodb.org/manual/reference/connection-string/
        // to configure the client.
        String url = props.getProperty("mongodb.url", null);
        boolean defaultedUrl = false;
        if (url == null) {
            defaultedUrl = true;
            url = "mongodb://localhost:27017/ycsb?w=1";
        }

        url = OptionsSupport.updateUrl(url, props);

        if (!url.startsWith("mongodb://")) {
            System.err.println("ERROR: Invalid URL: '" + url + "'. Must be of the form "
                    + "'mongodb://<host1>:<port1>,<host2>:<port2>/database?options'. "
                    + "http://docs.mongodb.org/manual/reference/connection-string/");
            System.exit(1);
        }

        try {
            MongoClientURI uri = new MongoClientURI(url);

            String uriDb = uri.getDatabase();
            if (!defaultedUrl && (uriDb != null) && !uriDb.isEmpty() && !"admin".equals(uriDb)) {
                databaseName = uriDb;
            } else {
                // If no database is specified in URI, use "ycsb"
                databaseName = "ycsb";

            }

            readPreference = uri.getOptions().getReadPreference();
            writeConcern = uri.getOptions().getWriteConcern();

            mongoClient = new MongoClient(uri);
            database = mongoClient.getDatabase(databaseName).withReadPreference(readPreference)
                    .withWriteConcern(writeConcern);

            System.out.println("mongo client connection created with " + url);
        } catch (Exception e1) {
            System.err.println("Could not initialize MongoDB connection pool for Loader: " + e1.toString());
            e1.printStackTrace();
            return;
        }
    }
}

From source file:io.opentracing.contrib.mongo.TracingMongoClient.java

License:Apache License

public TracingMongoClient(Tracer tracer, final MongoClientURI uri,
        final MongoDriverInformation mongoDriverInformation) {
    this(tracer, toServerAddressList(uri.getHosts()),
            uri.getCredentials() != null ? Collections.singletonList(uri.getCredentials())
                    : Collections.<MongoCredential>emptyList(),
            uri.getOptions(), mongoDriverInformation);
}

From source file:org.apache.jackrabbit.oak.plugins.document.DocumentNodeStoreService.java

License:Apache License

private void registerNodeStore() throws IOException {
    String uri = PropertiesUtil.toString(prop(PROP_URI, FWK_PROP_URI), DEFAULT_URI);
    String db = PropertiesUtil.toString(prop(PROP_DB, FWK_PROP_DB), DEFAULT_DB);

    int cacheSize = toInteger(prop(PROP_CACHE), DEFAULT_CACHE);
    int nodeCachePercentage = toInteger(prop(PROP_NODE_CACHE_PERCENTAGE), DEFAULT_NODE_CACHE_PERCENTAGE);
    int prevDocCachePercentage = toInteger(prop(PROP_PREV_DOC_CACHE_PERCENTAGE), DEFAULT_NODE_CACHE_PERCENTAGE);
    int childrenCachePercentage = toInteger(prop(PROP_CHILDREN_CACHE_PERCENTAGE),
            DEFAULT_CHILDREN_CACHE_PERCENTAGE);
    int docChildrenCachePercentage = toInteger(prop(PROP_DOC_CHILDREN_CACHE_PERCENTAGE),
            DEFAULT_DOC_CHILDREN_CACHE_PERCENTAGE);
    int diffCachePercentage = toInteger(prop(PROP_DIFF_CACHE_PERCENTAGE), DEFAULT_DIFF_CACHE_PERCENTAGE);
    int blobCacheSize = toInteger(prop(PROP_BLOB_CACHE_SIZE), DEFAULT_BLOB_CACHE_SIZE);
    String persistentCache = PropertiesUtil.toString(prop(PROP_PERSISTENT_CACHE), DEFAULT_PERSISTENT_CACHE);
    int cacheSegmentCount = toInteger(prop(PROP_CACHE_SEGMENT_COUNT), DEFAULT_CACHE_SEGMENT_COUNT);
    int cacheStackMoveDistance = toInteger(prop(PROP_CACHE_STACK_MOVE_DISTANCE),
            DEFAULT_CACHE_STACK_MOVE_DISTANCE);
    DocumentMK.Builder mkBuilder = new DocumentMK.Builder().setStatisticsProvider(statisticsProvider)
            .memoryCacheSize(cacheSize * MB)
            .memoryCacheDistribution(nodeCachePercentage, prevDocCachePercentage, childrenCachePercentage,
                    docChildrenCachePercentage, diffCachePercentage)
            .setCacheSegmentCount(cacheSegmentCount).setCacheStackMoveDistance(cacheStackMoveDistance)
            .setLeaseCheck(true /* OAK-2739: enabled by default */)
            .setLeaseFailureHandler(new LeaseFailureHandler() {

                @Override/*w  w  w . jav  a  2s. c  o  m*/
                public void handleLeaseFailure() {
                    try {
                        // plan A: try stopping oak-core
                        log.error("handleLeaseFailure: stopping oak-core...");
                        Bundle bundle = context.getBundleContext().getBundle();
                        bundle.stop();
                        log.error("handleLeaseFailure: stopped oak-core.");
                        // plan A worked, perfect!
                    } catch (BundleException e) {
                        log.error("handleLeaseFailure: exception while stopping oak-core: " + e, e);
                        // plan B: stop only DocumentNodeStoreService (to stop the background threads)
                        log.error("handleLeaseFailure: stopping DocumentNodeStoreService...");
                        context.disableComponent(DocumentNodeStoreService.class.getName());
                        log.error("handleLeaseFailure: stopped DocumentNodeStoreService");
                        // plan B succeeded.
                    }
                }
            });

    if (persistentCache != null && persistentCache.length() > 0) {
        mkBuilder.setPersistentCache(persistentCache);
    }

    boolean wrappingCustomBlobStore = customBlobStore && blobStore instanceof BlobStoreWrapper;

    //Set blobstore before setting the DB
    if (customBlobStore && !wrappingCustomBlobStore) {
        checkNotNull(blobStore,
                "Use of custom BlobStore enabled via  [%s] but blobStore reference not " + "initialized",
                CUSTOM_BLOB_STORE);
        mkBuilder.setBlobStore(blobStore);
    }

    if (documentStoreType == DocumentStoreType.RDB) {
        checkNotNull(dataSource, "DataStore type set [%s] but DataSource reference not initialized",
                PROP_DS_TYPE);
        if (!customBlobStore) {
            checkNotNull(blobDataSource, "DataStore type set [%s] but BlobDataSource reference not initialized",
                    PROP_DS_TYPE);
            mkBuilder.setRDBConnection(dataSource, blobDataSource);
            log.info("Connected to datasources {} {}", dataSource, blobDataSource);
        } else {
            if (blobDataSource != null && blobDataSource != dataSource) {
                log.info("Ignoring blobDataSource {} as custom blob store takes precedence.", blobDataSource);
            }
            mkBuilder.setRDBConnection(dataSource);
            log.info("Connected to datasource {}", dataSource);
        }
    } else {
        MongoClientURI mongoURI = new MongoClientURI(uri);

        if (log.isInfoEnabled()) {
            // Take care around not logging the uri directly as it
            // might contain passwords
            log.info(
                    "Starting DocumentNodeStore with host={}, db={}, cache size (MB)={}, persistentCache={}, "
                            + "blobCacheSize (MB)={}, maxReplicationLagInSecs={}",
                    mongoURI.getHosts(), db, cacheSize, persistentCache, blobCacheSize,
                    maxReplicationLagInSecs);
            log.info("Mongo Connection details {}", MongoConnection.toString(mongoURI.getOptions()));
        }

        mkBuilder.setMaxReplicationLag(maxReplicationLagInSecs, TimeUnit.SECONDS);
        mkBuilder.setMongoDB(uri, db, blobCacheSize);

        log.info("Connected to database '{}'", db);
    }

    if (!customBlobStore) {
        defaultBlobStore = mkBuilder.getBlobStore();
        log.info("Registering the BlobStore with ServiceRegistry");
        blobStoreReg = context.getBundleContext().registerService(BlobStore.class.getName(), defaultBlobStore,
                null);
    }

    //Set wrapping blob store after setting the DB
    if (wrappingCustomBlobStore) {
        ((BlobStoreWrapper) blobStore).setBlobStore(mkBuilder.getBlobStore());
        mkBuilder.setBlobStore(blobStore);
    }

    mkBuilder.setExecutor(executor);
    mk = mkBuilder.open();

    // ensure a clusterId is initialized 
    // and expose it as 'oak.clusterid' repository descriptor
    GenericDescriptors clusterIdDesc = new GenericDescriptors();
    clusterIdDesc.put(ClusterRepositoryInfo.OAK_CLUSTERID_REPOSITORY_DESCRIPTOR_KEY,
            new SimpleValueFactory().createValue(ClusterRepositoryInfo.getOrCreateId(mk.getNodeStore())), true,
            false);
    whiteboard.register(Descriptors.class, clusterIdDesc, Collections.emptyMap());

    // If a shared data store register the repo id in the data store
    if (SharedDataStoreUtils.isShared(blobStore)) {
        try {
            String repoId = ClusterRepositoryInfo.getOrCreateId(mk.getNodeStore());
            ((SharedDataStore) blobStore).addMetadataRecord(new ByteArrayInputStream(new byte[0]),
                    SharedDataStoreUtils.SharedStoreRecordType.REPOSITORY.getNameFromId(repoId));
        } catch (Exception e) {
            throw new IOException("Could not register a unique repositoryId", e);
        }
    }

    registerJMXBeans(mk.getNodeStore(), mkBuilder);
    registerLastRevRecoveryJob(mk.getNodeStore());
    registerJournalGC(mk.getNodeStore());

    NodeStore store;
    documentNodeStore = mk.getNodeStore();
    store = documentNodeStore;
    observerTracker = new ObserverTracker(documentNodeStore);

    observerTracker.start(context.getBundleContext());

    DocumentStore ds = mk.getDocumentStore();

    // OAK-2682: time difference detection applied at startup with a default
    // max time diff of 2000 millis (2sec)
    final long maxDiff = Long.parseLong(System.getProperty("oak.documentMK.maxServerTimeDiffMillis", "2000"));
    try {
        if (maxDiff >= 0) {
            final long timeDiff = ds.determineServerTimeDifferenceMillis();
            log.info("registerNodeStore: server time difference: {}ms (max allowed: {}ms)", timeDiff, maxDiff);
            if (Math.abs(timeDiff) > maxDiff) {
                throw new AssertionError("Server clock seems off (" + timeDiff
                        + "ms) by more than configured amount (" + maxDiff + "ms)");
            }
        }
    } catch (RuntimeException e) { // no checked exception
        // in case of a RuntimeException, just log but continue
        log.warn("registerNodeStore: got RuntimeException while trying to determine time difference to server: "
                + e, e);
    }

    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(Constants.SERVICE_PID, DocumentNodeStore.class.getName());
    props.put(DESCRIPTION, getMetadata(ds));
    // OAK-2844: in order to allow DocumentDiscoveryLiteService to directly
    // require a service DocumentNodeStore (instead of having to do an 'instanceof')
    // the registration is now done for both NodeStore and DocumentNodeStore here.
    nodeStoreReg = context.getBundleContext().registerService(new String[] { NodeStore.class.getName(),
            DocumentNodeStore.class.getName(), Clusterable.class.getName() }, store, props);
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java

License:Apache License

@Override
public void setReadWriteMode(String readWriteMode) {
    if (readWriteMode == null || readWriteMode.equals(lastReadWriteMode)) {
        return;// w  w w. j  a v a2 s.c  o  m
    }
    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);
    }
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.replica.NodeCollectionProvider.java

License:Apache License

private MongoClient prepareClientForHostname(String hostname) throws UnknownHostException {
    ServerAddress address;//from   w w  w . j  a  v a  2  s . co  m
    if (hostname.contains(":")) {
        String[] hostSplit = hostname.split(":");
        if (hostSplit.length != 2) {
            throw new IllegalArgumentException("Not a valid hostname: " + hostname);
        }
        address = new ServerAddress(hostSplit[0], Integer.parseInt(hostSplit[1]));
    } else {
        address = new ServerAddress(hostname);
    }

    MongoClientURI originalUri = new MongoClientURI(originalMongoUri);
    List<MongoCredential> credentialList = new ArrayList<MongoCredential>(1);
    if (originalUri.getCredentials() != null) {
        credentialList.add(originalUri.getCredentials());
    }
    return new MongoClient(address, credentialList, originalUri.getOptions());
}

From source file:org.craftercms.commons.mongo.MongoScriptRunner.java

License:Open Source License

private List<String> getCommands(final Path scriptPath) throws MongoDataException {
    List<String> commandList = new ArrayList<>();
    if (SystemUtils.IS_OS_WINDOWS) {
        commandList.add("CMD");
        commandList.add("/C");
    }/*from  ww w  . j  a v  a2  s  . c om*/
    if (StringUtils.isBlank(mongoClientBin)) {
        throw new MongoDataException("Unable to run scripts, mongo client bin path is not set ");
    }
    String pwd = null;
    String authSource = null;
    String user = null;
    MongoClientURI uri = new MongoClientURI(connectionStr);
    if (uri.getCredentials() != null) {
        authSource = uri.getCredentials().getSource();
        user = uri.getCredentials().getUserName();
        if (uri.getCredentials().getPassword() != null) {
            pwd = new String(uri.getCredentials().getPassword());
        }
    }
    String replicaSetName = "";
    if (uri.getHosts().size() > 1) {
        replicaSetName = uri.getOptions().getRequiredReplicaSetName() + "/";
    }
    final String host = StringUtils.trim(replicaSetName + StringUtils.join(uri.getHosts(), ","));
    commandList.add(mongoClientBin);
    commandList.add("--host");
    commandList.add(host);
    commandList.add(uri.getDatabase());
    if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(pwd) && StringUtils.isNotBlank(authSource)) {
        commandList.add("-u");
        commandList.add(user);
        commandList.add("-p");
        commandList.add(pwd);
        commandList.add("--authenticationDatabase");
        commandList.add(authSource);
    }
    commandList.add(scriptPath.toAbsolutePath().toString());
    return commandList;
}

From source file:org.graylog.plugins.metrics.mongodb.providers.MongoDBReporterProvider.java

License:Open Source License

@Override
public MongoDBReporter get() {
    final MongoClientURI mongoClientURI = configuration.getUri();
    final MongoCredential credentials = mongoClientURI.getCredentials();
    return MongoDBReporter.forRegistry(metricRegistry).serverAddresses(extractServerAddresses(mongoClientURI))
            .mongoCredentials(//from w  ww . ja va 2  s.  c o  m
                    credentials == null ? new MongoCredential[0] : new MongoCredential[] { credentials })
            .mongoClientOptions(mongoClientURI.getOptions()).withDatabaseName(mongoClientURI.getDatabase())
            .additionalFields(configuration.getAdditionalFields())
            .convertDurationsTo(configuration.getUnitDurations()).convertRatesTo(configuration.getUnitRates())
            .filter(new RegexMetricFilter(configuration.getIncludeMetrics())).build();
}