@ThreadSafe public class Replicator extends Object
The logic used in this component to perform an initial sync is similar to that of the official initial sync functionality used by secondary MongoDB nodes, although our logic can be simpler since we are not storing state nor building indexes:
It is important to understand that step 2 is not performing a consistent snapshot. That means that once we start copying a collection, clients can make changes and we may or may not see those changes in our copy. However, this is not a problem because the MongoDB replication process -- and our logic -- relies upon the fact that every change recorded in the MongoDB oplog is idempotent. So, as long as we read the oplog from the same point in time (or earlier) than we started our copy operation, and apply all of the changes in the same order, then the state of all documents described by this connector will be the same.
source information, then
when the replicator starts it will simply start reading the primary's oplog starting at the same point it last left off.
This replicator does each of its tasks using a connection to the primary. If the replicator is not able to establish a
connection to the primary (e.g., there is no primary, or the replicator cannot communicate with the primary), the replicator
will continue to try to establish a connection, using an exponential back-off strategy to prevent saturating the system.
After a configurable number of failed attempts, the replicator
will fail by throwing a ConnectException.
| Modifier and Type | Field and Description |
|---|---|
private Clock |
clock |
private Predicate<CollectionId> |
collectionFilter |
private ReplicationContext |
context |
private ExecutorService |
copyThreads |
private org.slf4j.Logger |
logger |
private ConnectionContext.MongoPrimary |
primaryClient |
private RecordMakers |
recordMakers |
private ReplicaSet |
replicaSet |
private String |
rsName |
private AtomicBoolean |
running |
private SourceInfo |
source |
| Constructor and Description |
|---|
Replicator(ReplicationContext context,
ReplicaSet replicaSet,
BlockingConsumer<org.apache.kafka.connect.source.SourceRecord> recorder) |
| Modifier and Type | Method and Description |
|---|---|
protected long |
copyCollection(CollectionId collectionId,
long timestamp)
Copy the collection, sending to the recorder a record for each document.
|
protected long |
copyCollection(com.mongodb.MongoClient primary,
CollectionId collectionId,
long timestamp)
Copy the collection, sending to the recorder a record for each document.
|
protected boolean |
establishConnectionToPrimary()
Establish a connection to the primary.
|
protected boolean |
handleOplogEvent(com.mongodb.ServerAddress primaryAddress,
org.bson.Document event)
Handle a single oplog event.
|
protected boolean |
isInitialSyncExpected()
Determine if an initial sync should be performed.
|
protected boolean |
performInitialSync()
Perform the initial sync of the collections in the replica set.
|
protected void |
readOplog()
Repeatedly obtain a connection to the replica set's current primary and use that primary to read the oplog.
|
protected void |
readOplog(com.mongodb.MongoClient primary)
Use the given primary to read the oplog.
|
protected void |
recordCurrentOplogPosition()
Obtain the current position of the oplog, and record it in the source.
|
void |
run()
Perform the replication logic.
|
void |
stop()
Stop the replication from running.
|
private final org.slf4j.Logger logger
private final ReplicationContext context
private final ExecutorService copyThreads
private final ReplicaSet replicaSet
private final String rsName
private final AtomicBoolean running
private final SourceInfo source
private final RecordMakers recordMakers
private final Predicate<CollectionId> collectionFilter
private final Clock clock
private ConnectionContext.MongoPrimary primaryClient
public Replicator(ReplicationContext context, ReplicaSet replicaSet, BlockingConsumer<org.apache.kafka.connect.source.SourceRecord> recorder)
context - the replication context; may not be nullreplicaSet - the replica set to be replicated; may not be nullrecorder - the recorder for source record produced by this replicator; may not be nullpublic void stop()
This method does nothing if the snapshot is not running
public void run()
protected boolean establishConnectionToPrimary()
true if a connection was established, or false otherwiseprotected void recordCurrentOplogPosition()
protected boolean isInitialSyncExpected()
source has no previously
recorded offsets for this replica set, or if a snapshot should
always be performed.true if the initial sync should be performed, or false otherwiseprotected boolean performInitialSync()
true if the initial sync was completed, or false if it was stopped for any reasonprotected long copyCollection(CollectionId collectionId, long timestamp) throws InterruptedException
collectionId - the identifier of the collection to be copied; may not be nulltimestamp - the timestamp in milliseconds at which the copy operation was startedInterruptedException - if the thread was interrupted while the copy operation was runningprotected long copyCollection(com.mongodb.MongoClient primary,
CollectionId collectionId,
long timestamp)
throws InterruptedException
primary - the connection to the replica set's primary node; may not be nullcollectionId - the identifier of the collection to be copied; may not be nulltimestamp - the timestamp in milliseconds at which the copy operation was startedInterruptedException - if the thread was interrupted while the copy operation was runningprotected void readOplog()
protected void readOplog(com.mongodb.MongoClient primary)
primary - the connection to the replica set's primary node; may not be nullprotected boolean handleOplogEvent(com.mongodb.ServerAddress primaryAddress,
org.bson.Document event)
primaryAddress - the address of the primary server from which the event was obtained; may not be nullevent - the oplog event; may not be nulltrue if additional events should be processed, or false if the caller should stop
processing eventsCopyright © 2016 JBoss by Red Hat. All rights reserved.