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

DBZ-7903 Improve blocking snapshot reliability in case of restart #161

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
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
35 changes: 10 additions & 25 deletions src/main/java/io/debezium/connector/db2/Db2OffsetContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

import org.apache.kafka.connect.data.Schema;

import io.debezium.connector.AbstractSourceInfo;
import io.debezium.connector.SnapshotRecord;
import io.debezium.connector.SnapshotType;
import io.debezium.pipeline.CommonOffsetContext;
import io.debezium.pipeline.source.snapshot.incremental.IncrementalSnapshotContext;
import io.debezium.pipeline.source.snapshot.incremental.SignalBasedIncrementalSnapshotContext;
Expand All @@ -27,7 +29,6 @@ public class Db2OffsetContext extends CommonOffsetContext<SourceInfo> {
private static final String EVENT_SERIAL_NO_KEY = "event_serial_no";

private final Schema sourceInfoSchema;
private boolean snapshotCompleted;

private final TransactionContext transactionContext;
private final IncrementalSnapshotContext<TableId> incrementalSnapshotContext;
Expand All @@ -37,35 +38,35 @@ public class Db2OffsetContext extends CommonOffsetContext<SourceInfo> {
*/
private long eventSerialNo;

public Db2OffsetContext(Db2ConnectorConfig connectorConfig, TxLogPosition position, boolean snapshot, boolean snapshotCompleted, long eventSerialNo,
public Db2OffsetContext(Db2ConnectorConfig connectorConfig, TxLogPosition position, SnapshotType snapshot, boolean snapshotCompleted, long eventSerialNo,
TransactionContext transactionContext, IncrementalSnapshotContext<TableId> incrementalSnapshotContext) {
super(new SourceInfo(connectorConfig));
super(new SourceInfo(connectorConfig), snapshotCompleted);

sourceInfo.setCommitLsn(position.getCommitLsn());
sourceInfo.setChangeLsn(position.getInTxLsn());
sourceInfoSchema = sourceInfo.schema();

this.snapshotCompleted = snapshotCompleted;
if (this.snapshotCompleted) {
postSnapshotCompletion();
}
else {
sourceInfo.setSnapshot(snapshot ? SnapshotRecord.TRUE : SnapshotRecord.FALSE);
setSnapshot(snapshot);
sourceInfo.setSnapshot(snapshot != null ? SnapshotRecord.TRUE : SnapshotRecord.FALSE);
}
this.eventSerialNo = eventSerialNo;
this.transactionContext = transactionContext;
this.incrementalSnapshotContext = incrementalSnapshotContext;
}

public Db2OffsetContext(Db2ConnectorConfig connectorConfig, TxLogPosition position, boolean snapshot, boolean snapshotCompleted) {
public Db2OffsetContext(Db2ConnectorConfig connectorConfig, TxLogPosition position, SnapshotType snapshot, boolean snapshotCompleted) {
this(connectorConfig, position, snapshot, snapshotCompleted, 1, new TransactionContext(), new SignalBasedIncrementalSnapshotContext<>(false));
}

@Override
public Map<String, ?> getOffset() {
if (sourceInfo.isSnapshot()) {
if (getSnapshot().isPresent()) {
return Collect.hashMapOf(
SourceInfo.SNAPSHOT_KEY, true,
AbstractSourceInfo.SNAPSHOT_KEY, getSnapshot().get().toString(),
SNAPSHOT_COMPLETED_KEY, snapshotCompleted,
SourceInfo.COMMIT_LSN_KEY, sourceInfo.getCommitLsn().toString());
}
Expand Down Expand Up @@ -102,26 +103,10 @@ public void setChangePosition(TxLogPosition position, int eventCount) {
sourceInfo.setChangeLsn(position.getInTxLsn());
}

@Override
public boolean isSnapshotRunning() {
return sourceInfo.isSnapshot() && !snapshotCompleted;
}

public boolean isSnapshotCompleted() {
return snapshotCompleted;
}

@Override
public void preSnapshotStart() {
sourceInfo.setSnapshot(SnapshotRecord.TRUE);
snapshotCompleted = false;
}

@Override
public void preSnapshotCompletion() {
snapshotCompleted = true;
}

public static class Loader implements OffsetContext.Loader<Db2OffsetContext> {

private final Db2ConnectorConfig connectorConfig;
Expand All @@ -134,7 +119,7 @@ public Loader(Db2ConnectorConfig connectorConfig) {
public Db2OffsetContext load(Map<String, ?> offset) {
final Lsn changeLsn = Lsn.valueOf((String) offset.get(SourceInfo.CHANGE_LSN_KEY));
final Lsn commitLsn = Lsn.valueOf((String) offset.get(SourceInfo.COMMIT_LSN_KEY));
boolean snapshot = Boolean.TRUE.equals(offset.get(SourceInfo.SNAPSHOT_KEY));
final SnapshotType snapshot = loadSnapshot((Map<String, Object>) offset);
boolean snapshotCompleted = Boolean.TRUE.equals(offset.get(SNAPSHOT_COMPLETED_KEY));

// only introduced in 0.10.Beta1, so it might be not present when upgrading from earlier versions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ protected void determineSnapshotOffset(RelationalSnapshotContext<Db2Partition, D
ctx.offset = new Db2OffsetContext(
connectorConfig,
TxLogPosition.valueOf(jdbcConnection.getMaxLsn()),
false,
null,
false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void init(Db2OffsetContext offsetContext) {

this.effectiveOffsetContext = offsetContext != null
? offsetContext
: new Db2OffsetContext(connectorConfig, TxLogPosition.NULL, false, false);
: new Db2OffsetContext(connectorConfig, TxLogPosition.NULL, null, false);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/io/debezium/connector/db2/Db2ConnectorIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import io.debezium.config.CommonConnectorConfig;
import io.debezium.config.Configuration;
import io.debezium.connector.SnapshotType;
import io.debezium.connector.db2.Db2ConnectorConfig.SnapshotMode;
import io.debezium.connector.db2.util.TestHelper;
import io.debezium.converters.CloudEventsConverterTest;
Expand Down Expand Up @@ -407,7 +408,7 @@ public void verifyOffsets() throws Exception {
records = records.subList(1, records.size());
for (Iterator<SourceRecord> it = records.iterator(); it.hasNext();) {
SourceRecord record = it.next();
assertThat(record.sourceOffset().get("snapshot")).as("Snapshot phase").isEqualTo(true);
assertThat(record.sourceOffset().get("snapshot")).as("Snapshot phase").isEqualTo(SnapshotType.INITIAL.toString());
if (it.hasNext()) {
assertThat(record.sourceOffset().get("snapshot_completed")).as("Snapshot in progress").isEqualTo(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.junit.Test;

import io.debezium.config.Configuration;
import io.debezium.connector.SnapshotType;
import io.debezium.connector.db2.Db2ConnectorConfig.SnapshotMode;
import io.debezium.connector.db2.util.TestHelper;
import io.debezium.doc.FixFor;
Expand Down Expand Up @@ -98,7 +99,7 @@ public void snapshotSchemaChanges() throws Exception {
schemaRecords.forEach(record -> {
assertThat(record.topic()).isEqualTo("testdb");
assertThat(((Struct) record.key()).getString("databaseName")).isEqualTo("TESTDB");
assertThat(record.sourceOffset().get("snapshot")).isEqualTo(true);
assertThat(record.sourceOffset().get("snapshot")).isEqualTo(SnapshotType.INITIAL.toString());
});
assertThat(((Struct) schemaRecords.get(0).value()).getStruct("source").getString("snapshot")).isEqualTo("true");
assertThat(((Struct) schemaRecords.get(1).value()).getStruct("source").getString("snapshot")).isEqualTo("true");
Expand Down
Loading