Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mongodb][hotfix] Fix drop or rename record cause documentKey being empty #2210

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.mongodb.client.ChangeStreamIterable;
import com.mongodb.client.MongoChangeStreamCursor;
import com.mongodb.client.MongoClient;
import com.mongodb.client.model.changestream.OperationType;
import com.mongodb.kafka.connect.source.heartbeat.HeartbeatManager;
import com.ververica.cdc.connectors.base.source.meta.split.SourceSplitBase;
import com.ververica.cdc.connectors.base.source.meta.split.StreamSplit;
Expand Down Expand Up @@ -57,6 +58,7 @@
import static com.ververica.cdc.connectors.mongodb.internal.MongoDBEnvelope.NAMESPACE_COLLECTION_FIELD;
import static com.ververica.cdc.connectors.mongodb.internal.MongoDBEnvelope.NAMESPACE_DATABASE_FIELD;
import static com.ververica.cdc.connectors.mongodb.internal.MongoDBEnvelope.NAMESPACE_FIELD;
import static com.ververica.cdc.connectors.mongodb.internal.MongoDBEnvelope.OPERATION_TYPE_FIELD;
import static com.ververica.cdc.connectors.mongodb.internal.MongoDBEnvelope.SNAPSHOT_KEY_FIELD;
import static com.ververica.cdc.connectors.mongodb.internal.MongoDBEnvelope.SOURCE_FIELD;
import static com.ververica.cdc.connectors.mongodb.internal.MongoDBEnvelope.TIMESTAMP_KEY_FIELD;
Expand Down Expand Up @@ -134,25 +136,38 @@ public void execute(Context context) throws Exception {
nextUpdate = time.milliseconds() + sourceConfig.getPollAwaitTimeMillis();
} else {
BsonDocument changeStreamDocument = next.get();
MongoNamespace namespace = getMongoNamespace(changeStreamDocument);

BsonDocument resumeToken = changeStreamDocument.getDocument(ID_FIELD);
BsonDocument valueDocument =
normalizeChangeStreamDocument(changeStreamDocument);

LOG.trace("Adding {} to {}", valueDocument, namespace.getFullName());

changeRecord =
createSourceRecord(
createPartitionMap(
sourceConfig.getScheme(),
sourceConfig.getHosts(),
namespace.getDatabaseName(),
namespace.getCollectionName()),
createSourceOffsetMap(resumeToken, false),
namespace.getFullName(),
changeStreamDocument.getDocument(ID_FIELD),
valueDocument);
OperationType operationType = getOperationType(changeStreamDocument);

switch (operationType) {
case INSERT:
case UPDATE:
case REPLACE:
case DELETE:
MongoNamespace namespace = getMongoNamespace(changeStreamDocument);

BsonDocument resumeToken = changeStreamDocument.getDocument(ID_FIELD);
BsonDocument valueDocument =
normalizeChangeStreamDocument(changeStreamDocument);

LOG.trace("Adding {} to {}", valueDocument, namespace.getFullName());

changeRecord =
createSourceRecord(
createPartitionMap(
sourceConfig.getScheme(),
sourceConfig.getHosts(),
namespace.getDatabaseName(),
namespace.getCollectionName()),
createSourceOffsetMap(resumeToken, false),
namespace.getFullName(),
changeStreamDocument.getDocument(ID_FIELD),
valueDocument);
break;
default:
// Ignore drop、drop_database、rename and other record to prevent
// documentKey from being empty.
LOG.info("Ignored {} record: {}", operationType, changeStreamDocument);
}
}

if (changeRecord != null) {
Expand Down Expand Up @@ -347,6 +362,11 @@ private MongoNamespace getMongoNamespace(BsonDocument changeStreamDocument) {
ns.getString(NAMESPACE_COLLECTION_FIELD).getValue());
}

private OperationType getOperationType(BsonDocument changeStreamDocument) {
return OperationType.fromString(
changeStreamDocument.getString(OPERATION_TYPE_FIELD).getValue());
}

private boolean isBoundedRead() {
return !NO_STOPPING_OFFSET.equals(streamSplit.getEndingOffset());
}
Expand Down
Loading