List of usage examples for com.mongodb ReplicaSetStatus getMaster
@SuppressWarnings("deprecation") @Nullable public ServerAddress getMaster()
From source file:com.ebay.cloud.cms.sysmgmt.monitor.metrics.MongoMetric.java
License:Apache License
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;/* ww w.j av a2s .c om*/ 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; }
From source file:io.debezium.connector.mongodb.ConnectionContext.java
License:Apache License
/** * Obtain a client that talks only to the primary node of the replica set. * /* www.jav a 2 s . c om*/ * @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; }