Example usage for com.mongodb ReadPreference primaryPreferred

List of usage examples for com.mongodb ReadPreference primaryPreferred

Introduction

In this page you can find the example usage for com.mongodb ReadPreference primaryPreferred.

Prototype

public static ReadPreference primaryPreferred() 

Source Link

Document

Gets a read preference that forces reads to the primary if available, otherwise to a secondary.

Usage

From source file:fr.gouv.vitam.mdbes.MainMapReduce.java

License:Open Source License

/**
 * @param args/*from   w w w. j a  v a 2s.  c  o  m*/
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    if (args.length < 4) {
        System.err.println(
                "need: logfile host database esbase unicast [map] [reduce] [output] [options-commaEqualities separated list]");
        return;
    }
    final String log4j = args[0];
    PropertyConfigurator.configure(log4j);
    VitamLoggerFactory.setDefaultFactory(new LogbackLoggerFactory(VitamLogLevel.DEBUG));
    LOGGER = VitamLoggerFactory.getInstance(MainMapReduce.class);
    final String networkHost = "192.168.56.102";
    GlobalDatas.localNetworkAddress = networkHost;
    // connect to the local database server
    if (args.length > 1) {
        host = args[1];
    }
    if (args.length > 2) {
        database = args[2];
    }
    if (args.length > 3) {
        esbase = args[3];
    }
    if (args.length > 4) {
        unicast = args[4];
    }
    if (args.length > 5) {
        map = args[5];
    }
    if (args.length > 6) {
        reduce = args[6];
    }
    if (args.length > 7) {
        output = args[7];
    }
    if (args.length > 8) {
        options = args[8];
    }
    // connect to the local database server
    MongoDbAccess dbvitam = null;
    try {
        final MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(MAXTHREAD)
                .build();
        mongoClient = new MongoClient(host, options);
        mongoClient.setReadPreference(ReadPreference.primaryPreferred());
        dbvitam = new MongoDbAccess(mongoClient, database, esbase, unicast, false);
        runOnce(dbvitam);
    } catch (Exception e) {
        LOGGER.error(e);
    } finally {
        // release resources
        dbvitam.close();
        mongoClient.close();
    }
}

From source file:fr.gouv.vitam.mdbes.MainPrepareIngestFile.java

License:Open Source License

/**
 * Will save to a File and to ElasticSearch, then to MongoDB
 *
 * @param args/*from  w  w  w .  j  av a2 s  .c o  m*/
 *            logfile eraze/noeraze host database escluster unicast start nbload file fileout limitdepth nbThread mongoimport
 *            stopindex/xx
 *
 *            <ol>
 *            <li>logfile = Log4J configuration log file</li>
 *            <li>noeraze/index = index will (re)create index, else nothing is done</li>
 *            <li>host = MongoDB host</li>
 *            <li>database = MongoDB database name as VitamLinks</li>
 *            <li>escluster = ElasticSearch cluster name</li>
 *            <li>unicast = ElasticSearch unicast servers list (as in "mdb001, mdb002, mdb003")</li>
 *            <li>start = start index in the bench (will be for instance between 1-1000 start from 100)</li>
 *            <li>nbload = number of iteration from start</li>
 *            <li>file = ingest file</li>
 *            <li>fileout = output saved</li>
 *            <li>limitdepth = from which level the output is saved to the file and not to MongoDB</li>
 *            <li>nbThread = number of thread (default 1)</li>
 *            <li>mongoimport = optional command for import</li>
 *            <li>stopindex/xx = shall we stop index during import in MongoDB</li>
 *            </ol>
 */
public static void main(final String[] args) throws Exception {
    if (args.length < 6) {
        System.err.println(
                "need: logfile noeraze/index host database escluster unicast start nbload file fileout limitdepth mongoimport stopindex/xx nbThread");
        // System.err.println("before was need: logfile nbload files eraze/noeraze start host escluster unicast fileout limitdepth mongoimport 0/1 (1=stop index)");
        return;
    }
    final String log4j = args[0];
    PropertyConfigurator.configure(log4j);
    VitamLoggerFactory.setDefaultFactory(new LogbackLoggerFactory(VitamLogLevel.WARN));
    LOGGER = VitamLoggerFactory.getInstance(MainPrepareIngestFile.class);
    boolean reindex = false;
    if (args.length > 1) {
        reindex = args[1].equals("index");
    }
    if (simulate) {
        reindex = false;
    }
    final String networkHost = "192.168.56.102";
    GlobalDatas.localNetworkAddress = networkHost;
    // connect to the local database server
    if (args.length > 2) {
        host = args[2];
    }
    if (args.length > 3) {
        database = args[3];
    }
    if (args.length > 4) {
        esbase = args[4];
    }
    if (args.length > 5) {
        unicast = args[5];
    }
    int realnb = -1;
    if (args.length > 6) {
        startFrom = Integer.parseInt(args[6]);
    }
    if (args.length > 7) {
        realnb = Integer.parseInt(args[7]);
    }
    if (args.length > 8) {
        ingest = FileUtil.readFile(args[8]);
    }
    if (args.length > 9) {
        fileout = args[9];
    }
    if (args.length > 10) {
        final int stoplevel = Integer.parseInt(args[10]);
        minleveltofile = stoplevel;
    }
    if (args.length > 11) {
        nbThread = Integer.parseInt(args[11]);
    }
    if (args.length > 12) {
        commandMongo = args[12];
    }
    boolean stopindex = false;
    if (args.length > 13) {
        stopindex = args[13].equalsIgnoreCase("stopindex");
    }
    LOGGER.debug("Start with " + reindex + ":" + host + ":" + database + ":" + esbase + ":" + unicast);
    if (args.length > 6) {
        LOGGER.debug("and " + startFrom + ":" + realnb + ":" + ingest + ":" + fileout + ":" + minleveltofile
                + ":" + nbThread + ":" + commandMongo + ":" + stopindex);
    }
    MongoDbAccess dbvitam = null;
    try {
        MAXTHREAD += nbThread;
        final MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(MAXTHREAD)
                .build();
        mongoClient = new MongoClient(host, options);
        mongoClient.setReadPreference(ReadPreference.primaryPreferred());
        dbvitam = new MongoDbAccess(mongoClient, database, esbase, unicast, reindex);
        // get a list of the collections in this database and print them out
        LOGGER.debug(dbvitam.toString());
        if (realnb < 0) {
            return;
        }
        // drop all the data in it
        final ParserIngest parser = new ParserIngest(true);
        parser.parse(ingest);
        model = parser.getModel();
        if (reindex) {
            LOGGER.debug("ensureIndex");
            dbvitam.ensureIndex();
            if (model != null) {
                LOGGER.debug("updateEsIndex");
                dbvitam.updateEsIndex(model);
            }
            LOGGER.debug("end Index");
        }
        LOGGER.warn(dbvitam.toString());

        final int stepnb = realnb;
        nb = stepnb;
        loadt = new AtomicLong(0);
        cptMaip.set(0);
        runOnce(dbvitam);
    } catch (Exception e) {
        LOGGER.error(e);
    } finally {
        // release resources
        final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
        final ToClean toclean = new ToClean(dbvitam);
        scheduler.schedule(toclean, 1, TimeUnit.MILLISECONDS);
        final ToShutdown toShutdown = new ToShutdown();
        scheduler.schedule(toShutdown, 5000, TimeUnit.MILLISECONDS);
        scheduler.awaitTermination(7000, TimeUnit.MILLISECONDS);
        System.exit(0);
    }

}

From source file:fr.gouv.vitam.mdbes.MainQuery.java

License:Open Source License

/**
 * @param args/*from  w ww .j  a  v  a 2 s  .  c  om*/
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    if (args.length < 6) {
        System.err.println("need: logfile host database esbase unicast type filequery");
        return;
    }
    final String log4j = args[0];
    PropertyConfigurator.configure(log4j);
    VitamLoggerFactory.setDefaultFactory(new LogbackLoggerFactory(VitamLogLevel.DEBUG));
    LOGGER = VitamLoggerFactory.getInstance(MainQuery.class);
    final String networkHost = "192.168.56.102";
    GlobalDatas.localNetworkAddress = networkHost;
    // connect to the local database server
    if (args.length > 1) {
        host = args[1];
    }
    if (args.length > 2) {
        database = args[2];
    }
    if (args.length > 3) {
        esbase = args[3];
    }
    if (args.length > 4) {
        unicast = args[4];
    }
    if (args.length > 5) {
        typeBase = args[5];
    }
    if (args.length > 6) {
        request = FileUtil.readFile(args[6]);
    }
    // connect to the local database server
    MongoDbAccess dbvitam = null;
    try {
        final MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(MAXTHREAD)
                .build();
        mongoClient = new MongoClient(host, options);
        mongoClient.setReadPreference(ReadPreference.primaryPreferred());
        dbvitam = new MongoDbAccess(mongoClient, database, esbase, unicast, false);
        runOnce(dbvitam);
    } catch (Exception e) {
        LOGGER.error(e);
    } finally {
        // release resources
        dbvitam.close();
        mongoClient.close();
    }
}

From source file:fr.gouv.vitam.mdbes.MainQueryBench.java

License:Open Source License

/**
 * @param args/* w ww  .j a  va2  s. c  om*/
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    if (args.length < 7) {
        System.err.println("need: logfile host database esbase unicast nbload filequeryDepth filequeryTree");
        return;
    }
    final String log4j = args[0];
    PropertyConfigurator.configure(log4j);
    VitamLoggerFactory.setDefaultFactory(new LogbackLoggerFactory(VitamLogLevel.WARN));
    Properties systemProperties = System.getProperties();
    systemProperties.put("net.spy.log.LoggerImpl", "net.spy.memcached.compat.log.SLF4JLogger");
    System.setProperties(systemProperties);
    LOGGER = VitamLoggerFactory.getInstance(MainQueryBench.class);
    final String networkHost = "192.168.56.102";
    GlobalDatas.localNetworkAddress = networkHost;
    // connect to the local database server
    if (args.length > 1) {
        host = args[1];
    }
    if (args.length > 2) {
        database = args[2];
    }
    if (args.length > 3) {
        esbase = args[3];
    }
    if (args.length > 4) {
        unicast = args[4];
    }
    nbload = 1;
    if (args.length > 5) {
        nbload = Integer.parseInt(args[5]);
    }
    if (args.length > 6) {
        fileDepth = FileUtil.readFile(args[6]);
    }
    if (args.length > 7) {
        fileTree = FileUtil.readFile(args[7]);
    }
    // connect to the local database server
    MongoDbAccess dbvitam = null;
    try {
        final MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(MAXTHREAD)
                .build();
        mongoClient = new MongoClient(host, options);
        mongoClient.setReadPreference(ReadPreference.primaryPreferred());
        dbvitam = new MongoDbAccess(mongoClient, database, esbase, unicast, false);
        List<URI> hosts = new ArrayList<URI>();
        hosts.add(new URI("http://192.168.56.110:8091/pools"));
        LOGGER.warn(" USEMEMCACHED: " + GlobalDatas.USEMEMCACHED + " USELRUCACHE: " + GlobalDatas.USELRUCACHE
                + " USEREDIS: " + GlobalDatas.USEREDIS + " USECACHE: " + GlobalDatas.SAVERESULT);
        LOGGER.warn("Cache starting with: " + dbvitam.getCacheSize());
        int nbt = 1;
        int nbloop = 2;
        if (GlobalDatas.SAVERESULT) {
            nbloop = 5;
        }
        for (int i = 0; i < nbloop; i++) {
            for (nbt = 1; nbt <= MAXTHREAD; nbt *= 2) {
                //for (nbt = 1; nbt <= maxThread; nbt += 2) {
                // for (; nbt <= maxThread; nbt += 100) {
                nbThread = nbt;
                MainQueryBench.tree.set(0);
                MainQueryBench.depthmax.set(0);
                MainQueryBench.cachetree.set(0);
                MainQueryBench.cachedepthmax.set(0);
                MainQueryBench.cachetreecount.set(0);
                MainQueryBench.cachedepthmaxcount.set(0);
                MainQueryBench.querydepthmaxcount.set(0);
                MainQueryBench.querytreecount.set(0);
                cptMaip.set(0);
                runOnce(dbvitam);
            }
        }
    } catch (Exception e) {
        LOGGER.error(e);
    } finally {
        // release resources
        dbvitam.close();
        dbvitam.closeFinal();
        mongoClient.close();
    }
}

From source file:fr.gouv.vitam.mdbes.MainSimpleRequest.java

License:Open Source License

/**
 * @param args// www . j a v a  2 s  .  c  o m
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    if (args.length < 6) {
        System.err.println("need: logfile host database esbase unicast type query");
        return;
    }
    final String log4j = args[0];
    PropertyConfigurator.configure(log4j);
    VitamLoggerFactory.setDefaultFactory(new LogbackLoggerFactory(VitamLogLevel.DEBUG));
    LOGGER = VitamLoggerFactory.getInstance(MainSimpleRequest.class);
    final String networkHost = "192.168.56.102";
    GlobalDatas.localNetworkAddress = networkHost;
    // connect to the local database server
    if (args.length > 1) {
        host = args[1];
    }
    if (args.length > 2) {
        database = args[2];
    }
    if (args.length > 3) {
        esbase = args[3];
    }
    if (args.length > 4) {
        unicast = args[4];
    }
    if (args.length > 5) {
        request = args[5];
    }
    if (args.length > 6) {
        ids = args[6];
    }
    // connect to the local database server
    MongoDbAccess dbvitam = null;
    try {
        final MongoClientOptions options = new MongoClientOptions.Builder().connectionsPerHost(MAXTHREAD)
                .build();
        mongoClient = new MongoClient(host, options);
        mongoClient.setReadPreference(ReadPreference.primaryPreferred());
        dbvitam = new MongoDbAccess(mongoClient, database, esbase, unicast, false);
        runOnce(dbvitam);
    } catch (Exception e) {
        LOGGER.error(e);
    } finally {
        // release resources
        dbvitam.close();
        mongoClient.close();
    }
}

From source file:fr.gouv.vitam.mdbes.QueryBenchJavaSampler.java

License:Open Source License

@Override
public void setupTest(JavaSamplerContext arg0) {
    // called when the test is startup but only for global argument, not specific ones
    for (ARG arg : ARG.values()) {
        if (arg0.containsParameter(arg.name())) {
            switch (arg) {
            case maxes:
                maxES = arg0.getIntParameter(arg.name());
                GlobalDatas.limitES = maxES;
                break;
            case modelfile:
                modelfile = arg0.getParameter(arg.name()).trim();
                try {
                    modelQuery = FileUtil.readFile(modelfile);
                } catch (IOException e) {
                    e.printStackTrace();
                    modelQuery = null;//from   www.java 2  s  .c o m
                }
                break;
            case start:
                start = arg0.getIntParameter(arg.name());
                current = start;
                break;
            case stop:
                stop = arg0.getIntParameter(arg.name());
                break;
            case serie:
                serie = arg0.getParameter(arg.name()).trim();
            default:
                break;
            }
        }
    }
    if (initialized) {
        queryBench = new QueryBench(false);
        try {
            queryBench.prepareParse(modelQuery);
        } catch (InvalidParseOperationException e) {
            e.printStackTrace();
        }
        model = queryBench.getModel();
        return;
    }
    // called when the test is startup but only for global argument, not specific ones
    for (ARG arg : ARG.values()) {
        if (arg0.containsParameter(arg.name())) {
            switch (arg) {
            case esclustername:
                esclustername = arg0.getParameter(arg.name()).trim();
                break;
            case unicast:
                unicast = arg0.getParameter(arg.name()).trim();
                ;
                break;
            case maxconn:
                maxconn = arg0.getIntParameter(arg.name());
                break;
            case mongobase:
                mongobase = arg0.getParameter(arg.name()).trim();
                break;
            case mongohost:
                mongohost = arg0.getParameter(arg.name()).trim();
                break;
            default:
                break;
            }
        }
    }
    lock.lock();
    try {
        if (modelQuery != null) {
            try {
                if (mongoClient == null) {
                    options = new MongoClientOptions.Builder().connectionsPerHost(maxconn).build();
                    mongoClient = new MongoClient(mongohost, options);
                    mongoClient.setReadPreference(ReadPreference.primaryPreferred());
                }
                dbvitam = new MongoDbAccess(mongoClient, mongobase, esclustername, unicast, false);
                queryBench = new QueryBench(false);
                queryBench.prepareParse(modelQuery);
                model = queryBench.getModel();
                initialized = true;
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (InvalidParseOperationException e) {
                e.printStackTrace();
            } catch (InvalidUuidOperationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (!initialized) {
                    if (dbvitam != null) {
                        dbvitam.close();
                        dbvitam = null;
                    }
                    if (mongoClient != null) {
                        mongoClient.close();
                        mongoClient = null;
                    }
                }
            }
        }
    } finally {
        lock.unlock();
    }
}

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

License:Apache License

<T extends Document> ReadPreference getMongoReadPreference(@Nonnull Collection<T> collection,
        @Nullable String parentId, @Nullable String documentId, @Nonnull DocumentReadPreference preference) {
    switch (preference) {
    case PRIMARY:
        return ReadPreference.primary();
    case PREFER_PRIMARY:
        return ReadPreference.primaryPreferred();
    case PREFER_SECONDARY:
        return getConfiguredReadPreference(collection);
    case PREFER_SECONDARY_IF_OLD_ENOUGH:
        if (collection != Collection.NODES) {
            return ReadPreference.primary();
        }//from w  w  w.  j a va2  s  .c om

        boolean secondarySafe;
        if (fallbackSecondaryStrategy) {
            // This is not quite accurate, because ancestors
            // are updated in a background thread (_lastRev). We
            // will need to revise this for low maxReplicationLagMillis
            // values
            long replicationSafeLimit = getTime() - maxReplicationLagMillis;

            if (parentId == null) {
                secondarySafe = false;
            } else {
                //If parent has been modified loooong time back then there children
                //would also have not be modified. In that case we can read from secondary
                NodeDocument cachedDoc = nodesCache.getIfPresent(parentId);
                secondarySafe = cachedDoc != null && !cachedDoc.hasBeenModifiedSince(replicationSafeLimit);
            }
        } else {
            secondarySafe = true;
            secondarySafe &= collection == Collection.NODES;
            secondarySafe &= documentId == null || !localChanges.mayContain(documentId);
            secondarySafe &= parentId == null || !localChanges.mayContainChildrenOf(parentId);
            secondarySafe &= mostRecentAccessedRevisions == null
                    || replicaInfo.isMoreRecentThan(mostRecentAccessedRevisions);
        }

        ReadPreference readPreference;
        if (secondarySafe) {
            readPreference = getConfiguredReadPreference(collection);
        } else {
            readPreference = ReadPreference.primary();
        }

        return readPreference;
    default:
        throw new IllegalArgumentException("Unsupported usage " + preference);
    }
}

From source file:org.atlasapi.application.ApplicationPersistenceModule.java

@Bean
public LegacyApplicationStore applicationStore() {
    DatabasedMongo mongo = persistence.databasedWriteMongo();
    IdGenerator idGenerator = new MongoSequentialIdGenerator(mongo, "application");
    MongoApplicationStore legacyStore = new MongoApplicationStore(mongo, idGenerator,
            ReadPreference.primaryPreferred());
    LegacyAdaptingApplicationStore store = new LegacyAdaptingApplicationStore(legacyStore, mongo, idGenerator,
            idCodec);/*from w  ww  .  ja  v  a2s  .com*/
    return new CacheBackedApplicationStore(store, cacheMinutes);
}

From source file:org.codinjutsu.tools.mongo.view.ServerConfigurationPanel.java

License:Apache License

public ServerConfigurationPanel(Project project, MongoManager mongoManager) {
    this.project = project;
    setLayout(new BorderLayout());
    add(rootPanel, BorderLayout.CENTER);
    this.mongoManager = mongoManager;

    labelField.setName("labelField");
    feedbackLabel.setName("feedbackLabel");

    sslConnectionField.setName("sslConnectionField");
    readPreferenceComboBox.setName("readPreferenceComboBox");
    authenticationPanel.setBorder(IdeBorderFactory.createTitledBorder("Authentication settings", true));
    serverUrlsField.setName("serverUrlsField");
    usernameField.setName("usernameField");
    passwordField.setName("passwordField");
    mongoCRAuthRadioButton.setName("mongoCRAuthField");
    scramSHA1AuthRadioButton.setName("scramSHA1AuthField");
    defaultAuthMethodRadioButton.setName("defaultAuthMethod");

    userDatabaseField.setName("userDatabaseField");
    userDatabaseField.setToolTipText(// w w w  . ja va2  s .com
            "If your access is restricted to a specific database (e.g.: MongoLab), you can set it right here");

    autoConnectCheckBox.setName("autoConnectField");

    mongoShellOptionsPanel.setBorder(IdeBorderFactory.createTitledBorder("Mongo shell options", true));
    shellArgumentsLineField.setDialogCaption("Mongo arguments");

    testConnectionButton.setName("testConnection");

    connectionOptionPanel.setBorder(IdeBorderFactory.createTitledBorder("Connection Settings", true));
    readPreferenceComboBox.setModel(new DefaultComboBoxModel<>(new ReadPreference[] { ReadPreference.primary(),
            ReadPreference.primaryPreferred(), ReadPreference.secondary(), ReadPreference.secondaryPreferred(),
            ReadPreference.nearest() }));

    readPreferenceComboBox.setRenderer(new ColoredListCellRenderer() {
        @Override
        protected void customizeCellRenderer(JList list, Object value, int index, boolean selected,
                boolean hasFocus) {
            ReadPreference readPreference = (ReadPreference) value;
            append(readPreference.getName());
        }
    });

    readPreferenceComboBox.setSelectedItem(ReadPreference.primary());

    authMethodGroup = new ButtonGroup();
    authMethodGroup.add(mongoCRAuthRadioButton);
    authMethodGroup.add(scramSHA1AuthRadioButton);
    authMethodGroup.add(defaultAuthMethodRadioButton);

    defaultAuthMethodRadioButton.setSelected(true);
    defaultAuthMethodRadioButton.setToolTipText("Let the driver resolves the auth. mechanism");
    shellWorkingDirField.setText(null);
    initListeners();
}

From source file:org.eclipse.ditto.services.utils.persistence.mongo.MongoHealthChecker.java

License:Open Source License

private MongoHealthChecker() {

    final DefaultMongoDbConfig mongoDbConfig = DefaultMongoDbConfig
            .of(DefaultScopedConfig.dittoScoped(getContext().getSystem().settings().config()));
    mongoClient = MongoClientWrapper.getBuilder(mongoDbConfig).connectionPoolMaxSize(HEALTH_CHECK_MAX_POOL_SIZE)
            .build();/*from w ww.ja v a 2s  .c  o  m*/

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