Search in sources :

Example 11 with MongoInterruptedException

use of com.mongodb.MongoInterruptedException in project debezium by debezium.

the class ReplicaSetDiscovery method getReplicaSets.

/**
 * Connect to the shard cluster or replica set defined by the seed addresses, and obtain the specifications for each of the
 * replica sets.
 *
 * @return the information about the replica sets; never null but possibly empty
 */
public ReplicaSets getReplicaSets() {
    MongoClient client = context.getConnectionContext().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 (MongoInterruptedException e) {
        LOGGER.error("Interrupted while reading the '{}' collection in the '{}' database: {}", shardsCollection, CONFIG_DATABASE_NAME, e.getMessage(), e);
        Thread.currentThread().interrupt();
    } 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 ...
        final ClusterDescription clusterDescription = client.getClusterDescription();
        LOGGER.info("Checking current members of replica set at {}", seedAddresses);
        if (clusterDescription != null) {
            // This is a replica set ...
            final List<ServerDescription> serverDescriptions = clusterDescription.getServerDescriptions();
            if (serverDescriptions == null || serverDescriptions.size() == 0) {
                LOGGER.warn("Server descriptions not available, got '{}'", serverDescriptions);
            } else {
                String addressStr = serverDescriptions.stream().map(x -> x.getAddress().toString()).collect(Collectors.joining(","));
                String replicaSetName = serverDescriptions.get(0).getSetName();
                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 : MongoUtil.ADDRESS_DELIMITER_PATTERN.split(seedAddresses)) {
                    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);
}
Also used : HashSet(java.util.HashSet) List(java.util.List) MongoInterruptedException(com.mongodb.MongoInterruptedException) ServerDescription(com.mongodb.connection.ServerDescription) Logger(org.slf4j.Logger) MongoClient(com.mongodb.client.MongoClient) MongoException(com.mongodb.MongoException) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) ClusterDescription(com.mongodb.connection.ClusterDescription) Collectors(java.util.stream.Collectors) ThreadSafe(io.debezium.annotation.ThreadSafe) MongoClient(com.mongodb.client.MongoClient) MongoException(com.mongodb.MongoException) MongoInterruptedException(com.mongodb.MongoInterruptedException) ServerDescription(com.mongodb.connection.ServerDescription) ClusterDescription(com.mongodb.connection.ClusterDescription) HashSet(java.util.HashSet)

Aggregations

MongoInterruptedException (com.mongodb.MongoInterruptedException)11 MongoTimeoutException (com.mongodb.MongoTimeoutException)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 ClusterDescription (com.mongodb.connection.ClusterDescription)3 CompositeServerSelector (com.mongodb.selector.CompositeServerSelector)2 ServerSelector (com.mongodb.selector.ServerSelector)2 MongoClientException (com.mongodb.MongoClientException)1 MongoConfigurationException (com.mongodb.MongoConfigurationException)1 MongoException (com.mongodb.MongoException)1 MongoIncompatibleDriverException (com.mongodb.MongoIncompatibleDriverException)1 MongoInternalException (com.mongodb.MongoInternalException)1 MongoSecurityException (com.mongodb.MongoSecurityException)1 MongoSocketClosedException (com.mongodb.MongoSocketClosedException)1 MongoSocketException (com.mongodb.MongoSocketException)1 MongoClient (com.mongodb.client.MongoClient)1 ServerDescription (com.mongodb.connection.ServerDescription)1 ConnectionMessageReceivedEvent (com.mongodb.event.ConnectionMessageReceivedEvent)1 LatencyMinimizingServerSelector (com.mongodb.internal.selector.LatencyMinimizingServerSelector)1 ServerAddressSelector (com.mongodb.internal.selector.ServerAddressSelector)1 ServerAddressSelector (com.mongodb.selector.ServerAddressSelector)1