Example usage for com.mongodb ServerAddress equals

List of usage examples for com.mongodb ServerAddress equals

Introduction

In this page you can find the example usage for com.mongodb ServerAddress equals.

Prototype

@Override
    public boolean equals(final Object o) 

Source Link

Usage

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

License:Apache License

/**
 * Handle a single oplog event./* w  ww .  j  av  a 2 s.c  om*/
 * 
 * @param primaryAddress the address of the primary server from which the event was obtained; may not be null
 * @param event the oplog event; may not be null
 * @return {@code true} if additional events should be processed, or {@code false} if the caller should stop
 *         processing events
 */
protected boolean handleOplogEvent(ServerAddress primaryAddress, Document event) {
    logger.debug("Found event: {}", event);
    String ns = event.getString("ns");
    Document object = event.get("o", Document.class);
    if (object == null) {
        logger.warn("Missing 'o' field in event, so skipping {}", event.toJson());
        return true;
    }
    if (ns == null || ns.isEmpty()) {
        // These are replica set events ...
        String msg = object.getString("msg");
        if ("new primary".equals(msg)) {
            AtomicReference<ServerAddress> address = new AtomicReference<>();
            try {
                primaryClient.executeBlocking("conn", mongoClient -> {
                    ServerAddress currentPrimary = mongoClient.getAddress();
                    address.set(currentPrimary);
                });
            } catch (InterruptedException e) {
                logger.error("Get current primary executeBlocking", e);
            }
            ServerAddress serverAddress = address.get();
            if (serverAddress != null && !serverAddress.equals(primaryAddress)) {
                logger.info(
                        "Found new primary event in oplog, so stopping use of {} to continue with new primary",
                        primaryAddress);
                // There is a new primary, so stop using this server and instead use the new primary ...
                return false;
            } else {
                logger.info("Found new primary event in oplog, current {} is new primary. "
                        + "Continue to process oplog event.", primaryAddress);
            }
        }
        // Otherwise, ignore this event ...
        logger.debug("Skipping event with no namespace: {}", event.toJson());
        return true;
    }
    int delimIndex = ns.indexOf('.');
    if (delimIndex > 0) {
        assert (delimIndex + 1) < ns.length();
        String dbName = ns.substring(0, delimIndex);
        String collectionName = ns.substring(delimIndex + 1);
        if ("$cmd".equals(collectionName)) {
            // This is a command on a database ...
            // TODO: Probably want to handle some of these when we track creation/removal of collections
            logger.debug("Skipping database command event: {}", event.toJson());
            return true;
        }
        // Otherwise, it is an event on a document in a collection ...
        CollectionId collectionId = new CollectionId(rsName, dbName, collectionName);
        if (collectionFilter.test(collectionId)) {
            RecordsForCollection factory = recordMakers.forCollection(collectionId);
            try {
                factory.recordEvent(event, clock.currentTimeInMillis());
            } catch (InterruptedException e) {
                Thread.interrupted();
                return false;
            }
        }
    }
    return true;
}