Example usage for com.mongodb MongoClient getReplicaSetStatus

List of usage examples for com.mongodb MongoClient getReplicaSetStatus

Introduction

In this page you can find the example usage for com.mongodb MongoClient getReplicaSetStatus.

Prototype

@SuppressWarnings("deprecation")
@Deprecated
@Nullable
public ReplicaSetStatus getReplicaSetStatus() 

Source Link

Document

Get the status of the replica set cluster.

Usage

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;//from w  w w  . java  2s .  co  m
    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.
 * //  w w w. j  a v  a  2s. c o m
 * @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;
}

From source file:io.debezium.connector.mongodb.ReplicaSetDiscovery.java

License:Apache License

/**
 * Connect to the shard cluster or replica set defined by the seed addresses, and obtain the specifications for each of the
 * replica sets.// w w  w. ja v  a  2 s.c o m
 * 
 * @return the information about the replica sets; never null but possibly empty
 */
public ReplicaSets getReplicaSets() {
    MongoClient client = context.clientFor(seedAddresses);
    Set<ReplicaSet> replicaSetSpecs = new HashSet<>();

    // First see if the addresses are for a config server replica set ...
    String shardsCollection = "shards";
    try {
        MongoUtil.onCollectionDocuments(client, CONFIG_DATABASE_NAME, shardsCollection, doc -> {
            logger.info("Checking shard details from configuration replica set {}", seedAddresses);
            String shardName = doc.getString("_id");
            String hostStr = doc.getString("host");
            String replicaSetName = MongoUtil.replicaSetUsedIn(hostStr);
            replicaSetSpecs.add(new ReplicaSet(hostStr, replicaSetName, shardName));
        });
    } catch (MongoException e) {
        logger.error("Error while reading the '{}' collection in the '{}' database: {}", shardsCollection,
                CONFIG_DATABASE_NAME, e.getMessage(), e);
    }
    if (replicaSetSpecs.isEmpty()) {
        // The addresses may be a replica set ...
        ReplicaSetStatus rsStatus = client.getReplicaSetStatus();
        logger.info("Checking current members of replica set at {}", seedAddresses);
        if (rsStatus != null) {
            // This is a replica set ...
            String addressStr = Strings.join(",", client.getServerAddressList());
            String replicaSetName = rsStatus.getName();
            replicaSetSpecs.add(new ReplicaSet(addressStr, replicaSetName, null));
        } else {
            logger.debug("Found standalone MongoDB replica set at {}", seedAddresses);
            // We aren't connecting to it as a replica set (likely not using auto-discovery of members),
            // but we can't monitor standalone servers unless they really are replica sets. We already know
            // that we're not connected to a config server replica set, so any replica set name from the seed addresses
            // is almost certainly our replica set name ...
            String replicaSetName = MongoUtil.replicaSetUsedIn(seedAddresses);
            if (replicaSetName != null) {
                for (String address : seedAddresses.split(",")) {
                    replicaSetSpecs.add(new ReplicaSet(address, replicaSetName, null));
                }
            }
        }
    }
    if (replicaSetSpecs.isEmpty()) {
        // Without a replica set name, we can't do anything ...
        logger.error(
                "Found no replica sets at {}, so there is nothing to monitor and no connector tasks will be started. Check seed addresses in connector configuration.",
                seedAddresses);
    }
    return new ReplicaSets(replicaSetSpecs);
}