Class SourceInfo
- java.lang.Object
-
- io.debezium.connector.AbstractSourceInfo
-
- io.debezium.connector.common.BaseSourceInfo
-
- io.debezium.connector.mongodb.SourceInfo
-
@NotThreadSafe public final class SourceInfo extends BaseSourceInfo
Information about the source of information, which includes the partitions and offsets within those partitions. The MongoDB connector considers each MongoDB database a separate "partitions" (in Kafka Connect terminology), and each partition has an "offset" (if Kafka Connect terminology) that defines the position within that partition/database where the connector finds a particular record. As the connector uses the Kafka Connect framework to process each record in a partition, Kafka Connect keeps track of the most recent offset for that partition.The
source partitioninformation identifies the particular MongoDB replica set and the connector's logical name of the MongoDB server. A JSON-like representation of the source partition for a database named "customers" hosted in a MongoDB replica set named "myMongoServer" is as follows:{ "server_id" : "myMongoServer", "replicaSetName" : "rs0" }The
source offsetinformation describes the position within a particular partition of each record. Since each event in MongoDB's oplog is identified by aBSONTimestampthat tracks the time and the order of the event for that particular time (e.g., multiple events that occur at the same time will have unique orders), the offset includes the BSONTimetamp representation. (The event'shfield is the unique ID for the operation, so this is also included in the offset.) And, if an initial sync is in progress, the offset will include theinitsyncfield.Here's a JSON-like representation of an example timestamp:
{ "sec" = 1422998530, "ord" = 0, "h" = 398278732173914406, "initsync" = true }- Author:
- Randall Hauch
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classSourceInfo.Position
-
Field Summary
Fields Modifier and Type Field Description static StringCOLLECTIONprivate CollectionIdcollectionIdId of collection the current event applies to.private static SourceInfo.PositionINITIAL_POSITIONstatic StringINITIAL_SYNCprivate static org.bson.BsonTimestampINITIAL_TIMESTAMPprivate Set<String>initialSyncReplicaSetsstatic StringNAMESPACEstatic StringOPERATION_IDstatic StringORDERprivate SourceInfo.Positionpositionprivate ConcurrentMap<String,SourceInfo.Position>positionsByReplicaSetNamestatic StringREPLICA_SET_NAMEprivate StringreplicaSetNamestatic intSCHEMA_VERSIONstatic StringSERVER_ID_KEYstatic StringSESSION_TXN_IDprivate ConcurrentMap<String,Map<String,String>>sourcePartitionsByReplicaSetNamestatic StringTIMESTAMPstatic StringTX_ORD-
Fields inherited from class io.debezium.connector.common.BaseSourceInfo
snapshotRecord
-
Fields inherited from class io.debezium.connector.AbstractSourceInfo
COLLECTION_NAME_KEY, DATABASE_NAME_KEY, DEBEZIUM_CONNECTOR_KEY, DEBEZIUM_VERSION_KEY, SCHEMA_NAME_KEY, SEQUENCE_KEY, SERVER_NAME_KEY, SNAPSHOT_KEY, TABLE_NAME_KEY, TIMESTAMP_KEY
-
-
Constructor Summary
Constructors Constructor Description SourceInfo(MongoDbConnectorConfig connectorConfig)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static booleanbooleanOffsetValue(Map<String,?> values, String key)voidcollectionEvent(String replicaSetName, CollectionId collectionId)(package private) CollectionIdcollectionId()protected Stringdatabase()protected static org.bson.BsonTimestampextractEventTimestamp(org.bson.Document oplogEvent)Utility to extract thetimestampvalue from the event.protected static StringextractSessionTxnId(org.bson.Document oplogEvent)Utility to extract theunique transaction idvalue from the event.booleanhasOffset(String replicaSetName)Determine whether we have previously recorded a MongoDB timestamp for the replica set.private static intintOffsetValue(Map<String,?> values, String key)booleanisInitialSyncOngoing(String replicaSetName)Determine if the initial sync for the given replica set is still ongoing.booleanisSnapshotRunning()Returns whether any replica sets are still running a snapshot.Map<String,?>lastOffset(String replicaSetName)Get the Kafka Connect detail about the source "offset" for the named database, which describes the given position in the database where we have last read.org.bson.BsonTimestamplastOffsetTimestamp(String replicaSetName)Get the MongoDB timestamp of the last offset position for the replica set.OptionalLonglastOffsetTxOrder(String replicaSetName)Get the MongoDB transaction order of the last offset position for the replica set.private static longlongOffsetValue(Map<String,?> values, String key)private voidonEvent(String replicaSetName, CollectionId collectionId, SourceInfo.Position position)voidopLogEvent(String replicaSetName, org.bson.Document oplogEvent)voidopLogEvent(String replicaSetName, org.bson.Document oplogEvent, org.bson.Document masterEvent, long orderInTx)Map<String,String>partition(String replicaSetName)Get the Kafka Connect detail about the source "partition" for the given database in the replica set.(package private) SourceInfo.Positionposition()(package private) StringreplicaSetName()static StringreplicaSetNameForPartition(Map<String,?> partition)Get the replica set name for the given partition.StringserverId()booleansetOffsetFor(String replicaSetName, Map<String,?> sourceOffset)Set the source offset, as read from Kafka Connect, for the given replica set.booleansetOffsetFor(Map<String,String> partition, Map<String,?> sourceOffset)Set the source offset, as read from Kafka Connect, for the given replica set.protected SnapshotRecordsnapshot()voidstartInitialSync(String replicaSetName)Record that an initial sync has started for the given replica set.voidstopInitialSync(String replicaSetName)Record that an initial sync has stopped for the given replica set.private static StringstringOffsetValue(Map<String,?> values, String key)protected Instanttimestamp()protected OptionalLongtransactionPosition()-
Methods inherited from class io.debezium.connector.common.BaseSourceInfo
isSnapshot, setSnapshot
-
Methods inherited from class io.debezium.connector.AbstractSourceInfo
schema, sequence, serverName, struct, structMaker
-
-
-
-
Field Detail
-
SCHEMA_VERSION
public static final int SCHEMA_VERSION
- See Also:
- Constant Field Values
-
SERVER_ID_KEY
public static final String SERVER_ID_KEY
- See Also:
- Constant Field Values
-
REPLICA_SET_NAME
public static final String REPLICA_SET_NAME
- See Also:
- Constant Field Values
-
NAMESPACE
public static final String NAMESPACE
- See Also:
- Constant Field Values
-
TIMESTAMP
public static final String TIMESTAMP
- See Also:
- Constant Field Values
-
ORDER
public static final String ORDER
- See Also:
- Constant Field Values
-
OPERATION_ID
public static final String OPERATION_ID
- See Also:
- Constant Field Values
-
TX_ORD
public static final String TX_ORD
- See Also:
- Constant Field Values
-
SESSION_TXN_ID
public static final String SESSION_TXN_ID
- See Also:
- Constant Field Values
-
INITIAL_SYNC
public static final String INITIAL_SYNC
- See Also:
- Constant Field Values
-
COLLECTION
public static final String COLLECTION
- See Also:
- Constant Field Values
-
INITIAL_TIMESTAMP
private static final org.bson.BsonTimestamp INITIAL_TIMESTAMP
-
INITIAL_POSITION
private static final SourceInfo.Position INITIAL_POSITION
-
sourcePartitionsByReplicaSetName
private final ConcurrentMap<String,Map<String,String>> sourcePartitionsByReplicaSetName
-
positionsByReplicaSetName
private final ConcurrentMap<String,SourceInfo.Position> positionsByReplicaSetName
-
replicaSetName
private String replicaSetName
-
collectionId
private CollectionId collectionId
Id of collection the current event applies to. May benullafter noop events, after which the recorded offset may be retrieved but not the source struct.
-
position
private SourceInfo.Position position
-
-
Constructor Detail
-
SourceInfo
public SourceInfo(MongoDbConnectorConfig connectorConfig)
-
-
Method Detail
-
replicaSetNameForPartition
public static String replicaSetNameForPartition(Map<String,?> partition)
Get the replica set name for the given partition.- Parameters:
partition- the partition map- Returns:
- the replica set name (when the partition is valid), or
nullif the partition is null or has no replica set name entry
-
collectionId
CollectionId collectionId()
-
position
SourceInfo.Position position()
-
serverId
public String serverId()
- Returns:
- server id
-
partition
public Map<String,String> partition(String replicaSetName)
Get the Kafka Connect detail about the source "partition" for the given database in the replica set. If the database is not known, this method records the new partition.- Parameters:
replicaSetName- the name of the replica set name for which the partition is to be obtained; may not be null- Returns:
- the source partition information; never null
-
lastOffsetTimestamp
public org.bson.BsonTimestamp lastOffsetTimestamp(String replicaSetName)
Get the MongoDB timestamp of the last offset position for the replica set.- Parameters:
replicaSetName- the name of the replica set name for which the new offset is to be obtained; may not be null- Returns:
- the timestamp of the last offset, or the beginning of time if there is none
-
lastOffsetTxOrder
public OptionalLong lastOffsetTxOrder(String replicaSetName)
Get the MongoDB transaction order of the last offset position for the replica set.- Parameters:
replicaSetName- the name of the replica set name for which the new offset is to be obtained; may not be null- Returns:
- the tx order of the transaction in progress or 0 in case of non-transactional event
-
lastOffset
public Map<String,?> lastOffset(String replicaSetName)
Get the Kafka Connect detail about the source "offset" for the named database, which describes the given position in the database where we have last read. If the database has not yet been seen, this records the starting position for that database. However, if there is a position for the database, the offset representation is returned.- Parameters:
replicaSetName- the name of the replica set name for which the new offset is to be obtained; may not be null- Returns:
- a copy of the current offset for the database; never null
-
collectionEvent
public void collectionEvent(String replicaSetName, CollectionId collectionId)
Get aStructrepresentation of the sourcepartitionandoffsetinformation where we have last read. The Struct complies with theAbstractSourceInfo.schema()for the MongoDB connector.- Parameters:
replicaSetName- the name of the replica set name for which the new offset is to be obtained; may not be nullcollectionId- the event's collection identifier; may not be null- See Also:
AbstractSourceInfo.schema()
-
opLogEvent
public void opLogEvent(String replicaSetName, org.bson.Document oplogEvent, org.bson.Document masterEvent, long orderInTx)
Get aStructrepresentation of the sourcepartitionandoffsetinformation. The Struct complies with theAbstractSourceInfo.schema()for the MongoDB connector.- Parameters:
replicaSetName- the name of the replica set name for which the new offset is to be obtained; may not be nulloplogEvent- the replica set oplog event that was last read; may be null if the position is the start of the oplogmasterEvent- the replica set oplog event that contains event metadata; same as oplogEvent for non-transactional changesorderInTx- order in transaction batch, 0 for non-transactional events- See Also:
AbstractSourceInfo.schema()
-
opLogEvent
public void opLogEvent(String replicaSetName, org.bson.Document oplogEvent)
Get aStructrepresentation of the sourcepartitionandoffsetinformation. The Struct complies with theAbstractSourceInfo.schema()for the MongoDB connector.- Parameters:
replicaSetName- the name of the replica set name for which the new offset is to be obtained; may not be nulloplogEvent- the replica set oplog event that was last read; may be null if the position is the start of the oplog- See Also:
AbstractSourceInfo.schema()
-
extractEventTimestamp
protected static org.bson.BsonTimestamp extractEventTimestamp(org.bson.Document oplogEvent)
Utility to extract thetimestampvalue from the event.- Parameters:
oplogEvent- the event- Returns:
- the timestamp, or null if the event is null or there is no
tsfield
-
extractSessionTxnId
protected static String extractSessionTxnId(org.bson.Document oplogEvent)
Utility to extract theunique transaction idvalue from the event.- Parameters:
oplogEvent- the event- Returns:
- the session transaction id or null
-
onEvent
private void onEvent(String replicaSetName, CollectionId collectionId, SourceInfo.Position position)
-
hasOffset
public boolean hasOffset(String replicaSetName)
Determine whether we have previously recorded a MongoDB timestamp for the replica set.- Parameters:
replicaSetName- the name of the replica set name; may not be null- Returns:
trueif an offset has been recorded for the replica set, orfalseif the replica set has not yet been seen
-
setOffsetFor
public boolean setOffsetFor(String replicaSetName, Map<String,?> sourceOffset)
Set the source offset, as read from Kafka Connect, for the given replica set. This method does nothing if the supplied map is null.- Parameters:
replicaSetName- the name of the replica set name for which the new offset is to be obtained; may not be nullsourceOffset- the previously-recorded Kafka Connect source offset; may be null- Returns:
trueif the offset was recorded, orfalseif the source offset is null- Throws:
org.apache.kafka.connect.errors.ConnectException- if any offset parameter values are missing, invalid, or of the wrong type
-
setOffsetFor
public boolean setOffsetFor(Map<String,String> partition, Map<String,?> sourceOffset)
Set the source offset, as read from Kafka Connect, for the given replica set. This method does nothing if the supplied map is null.- Parameters:
partition- the partition information; may not be nullsourceOffset- the previously-recorded Kafka Connect source offset; may be null- Returns:
trueif the offset was recorded, orfalseif the source offset is null- Throws:
org.apache.kafka.connect.errors.ConnectException- if any offset parameter values are missing, invalid, or of the wrong type
-
startInitialSync
public void startInitialSync(String replicaSetName)
Record that an initial sync has started for the given replica set.- Parameters:
replicaSetName- the name of the replica set; never null
-
stopInitialSync
public void stopInitialSync(String replicaSetName)
Record that an initial sync has stopped for the given replica set.- Parameters:
replicaSetName- the name of the replica set; never null
-
isInitialSyncOngoing
public boolean isInitialSyncOngoing(String replicaSetName)
Determine if the initial sync for the given replica set is still ongoing.- Parameters:
replicaSetName- the name of the replica set; never null- Returns:
trueif the initial sync for this replica is still ongoing or was not completed before restarting, orfalseif there is currently no initial sync operation for this replica set
-
isSnapshotRunning
public boolean isSnapshotRunning()
Returns whether any replica sets are still running a snapshot.
-
timestamp
protected Instant timestamp()
- Specified by:
timestampin classAbstractSourceInfo
-
snapshot
protected SnapshotRecord snapshot()
- Overrides:
snapshotin classBaseSourceInfo
-
database
protected String database()
- Specified by:
databasein classAbstractSourceInfo
-
replicaSetName
String replicaSetName()
-
transactionPosition
protected OptionalLong transactionPosition()
-
-