/** * Get the address of the primary node, if there is one. * * @return the address of the replica set's primary node, or {@code null} if there is currently no primary */ public ServerAddress address() { return execute("get replica set primary", primary -> { ReplicaSetStatus rsStatus = primary.getReplicaSetStatus(); if (rsStatus != null) { return rsStatus.getMaster(); } else { return null; } }); }
ReplicaSetStatus rsStatus = client.getReplicaSetStatus(); logger.info("Checking current members of replica set at {}", seedAddresses); if (rsStatus != null) {
/** * Obtain a client that talks only to the primary node of the replica set. * * @param replicaSet the replica set information; may not be null * @return the client, or {@code null} if no primary could be found for the replica set */ protected MongoClient clientForPrimary(ReplicaSet replicaSet) { MongoClient replicaSetClient = clientForReplicaSet(replicaSet); ReplicaSetStatus rsStatus = replicaSetClient.getReplicaSetStatus(); if (rsStatus == null) { if ( !this.useHostsAsSeeds ) { // No replica set status is available, but it may still be a replica set ... return replicaSetClient; } // This is not a replica set, so there will be no oplog to read ... throw new ConnectException("The MongoDB server(s) at '" + replicaSet + "' is not a valid replica set and cannot be used"); } // It is a replica set ... ServerAddress primaryAddress = rsStatus.getMaster(); if (primaryAddress != null) { return pool.clientFor(primaryAddress); } return null; } }
/** * Returns the default write concern depending on MongoDB deployment. * <ul> * <li>{@link WriteConcern#MAJORITY}: for a MongoDB replica set</li> * <li>{@link WriteConcern#ACKNOWLEDGED}: for single MongoDB instance</li> * </ul> * * @param client the connection to MongoDB. * @return the default write concern to use for Oak. */ public static WriteConcern getDefaultWriteConcern(@NotNull MongoClient client) { WriteConcern w; if (client.getReplicaSetStatus() != null) { w = WriteConcern.MAJORITY; } else { w = WriteConcern.ACKNOWLEDGED; } return w; }
/** * Returns the default write concern depending on MongoDB deployment. * <ul> * <li>{@link WriteConcern#MAJORITY}: for a MongoDB replica set</li> * <li>{@link WriteConcern#ACKNOWLEDGED}: for single MongoDB instance</li> * </ul> * * @param client the connection to MongoDB. * @return the default write concern to use for Oak. */ public static WriteConcern getDefaultWriteConcern(@NotNull MongoClient client) { WriteConcern w; if (client.getReplicaSetStatus() != null) { w = WriteConcern.MAJORITY; } else { w = WriteConcern.ACKNOWLEDGED; } return w; }
private boolean secondariesWithinAcceptableLag() { return client.getReplicaSetStatus() == null || status.getReplicaSetLagEstimate() < acceptableLagMillis; }
private boolean secondariesWithinAcceptableLag() { return client.getReplicaSetStatus() == null || status.getReplicaSetLagEstimate() < acceptableLagMillis; }
/** * Returns {@code true} if the given read concern is sufficient for Oak. On * a replica set Oak expects majority or linear. For a single MongoDB node * deployment local is sufficient. * * @param client the client. * @param rc the read concern. * @return whether the read concern is sufficient. */ public static boolean isSufficientReadConcern(@NotNull MongoClient client, @NotNull ReadConcern rc) { ReadConcernLevel r = readConcernLevel(checkNotNull(rc)); if (client.getReplicaSetStatus() == null) { return true; } else { return REPLICA_RC.contains(r); } }
/** * Returns {@code true} if the given read concern is sufficient for Oak. On * a replica set Oak expects majority or linear. For a single MongoDB node * deployment local is sufficient. * * @param client the client. * @param rc the read concern. * @return whether the read concern is sufficient. */ public static boolean isSufficientReadConcern(@NotNull MongoClient client, @NotNull ReadConcern rc) { ReadConcernLevel r = readConcernLevel(checkNotNull(rc)); if (client.getReplicaSetStatus() == null) { return true; } else { return REPLICA_RC.contains(r); } }
/** * Returns the default read concern depending on MongoDB deployment. * <ul> * <li>{@link ReadConcern#MAJORITY}: for a MongoDB replica set with w=majority</li> * <li>{@link ReadConcern#LOCAL}: for other cases</li> * </ul> * * @param db the connection to MongoDB. * @return the default write concern to use for Oak. */ public static ReadConcern getDefaultReadConcern(@NotNull MongoClient client, @NotNull MongoDatabase db) { ReadConcern r; if (checkNotNull(client).getReplicaSetStatus() != null && isMajorityWriteConcern(db)) { r = ReadConcern.MAJORITY; } else { r = ReadConcern.LOCAL; } return r; }
/** * Returns the default read concern depending on MongoDB deployment. * <ul> * <li>{@link ReadConcern#MAJORITY}: for a MongoDB replica set with w=majority</li> * <li>{@link ReadConcern#LOCAL}: for other cases</li> * </ul> * * @param db the connection to MongoDB. * @return the default write concern to use for Oak. */ public static ReadConcern getDefaultReadConcern(@NotNull MongoClient client, @NotNull MongoDatabase db) { ReadConcern r; if (checkNotNull(client).getReplicaSetStatus() != null && isMajorityWriteConcern(db)) { r = ReadConcern.MAJORITY; } else { r = ReadConcern.LOCAL; } return r; }
"Unknown write concern: " + wc); if (client.getReplicaSetStatus() != null) { return w >= 2; } else {
"Unknown write concern: " + wc); if (client.getReplicaSetStatus() != null) { return w >= 2; } else {
private MongoClient mockMongoClient(boolean replicaSet) { ReplicaSetStatus status; if (replicaSet) { status = mock(ReplicaSetStatus.class); } else { status = null; } MongoClient client = mock(MongoClient.class); when(client.getReplicaSetStatus()).thenReturn(status); return client; } }
private void snapshot() { MongoClient client = dataSource.getMongoInstance(); Map<String, Object> status = new TreeMap<String, Object>(); // update driver status.put(MetricConstants.MONGO_DRIVER_VERSION, client.getVersion()); // update status ReplicaSetStatus rss = client.getReplicaSetStatus(); String master = NOT_FOUND; if (rss != null) { status.put(MetricConstants.REPL_STATUS, rss.toString()); ServerAddress masterServer = rss.getMaster(); if (masterServer != null) { master = masterServer.getHost(); } } else { status.put(MetricConstants.REPL_STATUS, "no repl set found!"); } // update mongo cluster master status.put(MetricConstants.REPL_MASTER, master); // list mongo databases Map<String, Object> databaseSizeMap = listDatabases(client); String databases = StringUtils.join(databaseSizeMap.keySet(), ','); status.put(MetricConstants.REPL_DATABASES, databases); status.put(MetricConstants.MONGO_DB_SIZE, databaseSizeMap); mongoStatus = status; }
replicaSetStatus = freshClient.getReplicaSetStatus(); if ( replicaSetStatus != null ) { rss = replicaSetStatus.getMaster() != null ? ReplSetStatus.ReadWrite : ReplSetStatus.ReadOnly;
ServerAddress address = null; for (int i = 0; i < 5; i++) { address = c.getReplicaSetStatus().getMaster(); if (address == null) { LOG.info("Primary unavailable. Waiting one second...");