private void changeSourceToLastSnapshotRecord(SourceRecord currentRecord) { final Struct envelope = (Struct)currentRecord.value(); final Struct source = (Struct)envelope.get("source"); if (source.getBoolean(SourceInfo.LAST_SNAPSHOT_RECORD_KEY) != null) { source.put(SourceInfo.LAST_SNAPSHOT_RECORD_KEY, true); } }
protected void verifyFromInitialSync(SourceRecord record, AtomicBoolean foundLast) { if (record.sourceOffset().containsKey(SourceInfo.INITIAL_SYNC)) { assertThat(record.sourceOffset().containsKey(SourceInfo.INITIAL_SYNC)).isTrue(); Struct value = (Struct) record.value(); assertThat(value.getStruct(Envelope.FieldName.SOURCE).getBoolean(SourceInfo.INITIAL_SYNC)).isTrue(); } else { // Only the last record in the initial sync should be marked as not being part of the initial sync ... assertThat(foundLast.getAndSet(true)).isFalse(); } }
protected void verifyNotFromInitialSync(SourceRecord record) { assertThat(record.sourceOffset().containsKey(SourceInfo.INITIAL_SYNC)).isFalse(); Struct value = (Struct) record.value(); assertThat(value.getStruct(Envelope.FieldName.SOURCE).getBoolean(SourceInfo.INITIAL_SYNC)).isNull(); }
protected void assertRecordOffsetAndSnapshotSource(SourceRecord record, boolean shouldBeSnapshot, boolean shouldBeLastSnapshotRecord) { Map<String, ?> offset = record.sourceOffset(); assertNotNull(offset.get(SourceInfo.TXID_KEY)); assertNotNull(offset.get(SourceInfo.TIMESTAMP_KEY)); assertNotNull(offset.get(SourceInfo.LSN_KEY)); Object snapshot = offset.get(SourceInfo.SNAPSHOT_KEY); Object lastSnapshotRecord = offset.get(SourceInfo.LAST_SNAPSHOT_RECORD_KEY); if (shouldBeSnapshot) { assertTrue("Snapshot marker expected but not found", (Boolean) snapshot); assertEquals("Last snapshot record marker mismatch", shouldBeLastSnapshotRecord, lastSnapshotRecord); } else { assertNull("Snapshot marker not expected, but found", snapshot); assertNull("Last snapshot marker not expected, but found", lastSnapshotRecord); } final Struct envelope = (Struct)record.value(); if (envelope != null) { final Struct source = (Struct)envelope.get("source"); final Boolean sourceSnapshot = source.getBoolean(SourceInfo.SNAPSHOT_KEY); final Boolean sourceLastSnapshotRecord = source.getBoolean(SourceInfo.LAST_SNAPSHOT_RECORD_KEY); if (shouldBeSnapshot) { assertTrue("Snapshot marker expected in source but not found", sourceSnapshot); assertEquals("Last snapshot record marker in source mismatch", shouldBeLastSnapshotRecord, sourceLastSnapshotRecord); } else { assertNull("Source snapshot marker not expected, but found", sourceSnapshot); assertNull("Source last snapshot marker not expected, but found", sourceLastSnapshotRecord); } } }
assertThat(struct.getString(SourceInfo.REPLICA_SET_NAME)).isEqualTo(REPLICA_SET_NAME); assertThat(struct.getString(SourceInfo.SERVER_NAME)).isEqualTo("serverX"); assertThat(struct.getBoolean(SourceInfo.INITIAL_SYNC)).isNull();
@Test public void shouldReturnRecordedOffsetForUsedReplicaName() { Document event = new Document().append("ts", new BsonTimestamp(100, 2)) .append("h", Long.valueOf(1987654321)) .append("ns", "dbA.collectA"); assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(false); source.offsetStructForEvent(REPLICA_SET_NAME, event); assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(true); Map<String, ?> offset = source.lastOffset(REPLICA_SET_NAME); assertThat(offset.get(SourceInfo.TIMESTAMP)).isEqualTo(100); assertThat(offset.get(SourceInfo.ORDER)).isEqualTo(2); assertThat(offset.get(SourceInfo.OPERATION_ID)).isEqualTo(1987654321L); BsonTimestamp ts = source.lastOffsetTimestamp(REPLICA_SET_NAME); assertThat(ts.getTime()).isEqualTo(100); assertThat(ts.getInc()).isEqualTo(2); Struct struct = source.lastOffsetStruct(REPLICA_SET_NAME,new CollectionId(REPLICA_SET_NAME,"dbA","collectA")); assertThat(struct.getInt32(SourceInfo.TIMESTAMP)).isEqualTo(100); assertThat(struct.getInt32(SourceInfo.ORDER)).isEqualTo(2); assertThat(struct.getInt64(SourceInfo.OPERATION_ID)).isEqualTo(1987654321L); assertThat(struct.getString(SourceInfo.NAMESPACE)).isEqualTo("dbA.collectA"); assertThat(struct.getString(SourceInfo.REPLICA_SET_NAME)).isEqualTo(REPLICA_SET_NAME); assertThat(struct.getString(SourceInfo.SERVER_NAME)).isEqualTo("serverX"); assertThat(struct.getBoolean(SourceInfo.INITIAL_SYNC)).isNull(); }
@Test public void shouldReturnRecordedOffsetForUsedReplicaNameDuringInitialSync() { source.startInitialSync(REPLICA_SET_NAME); Document event = new Document().append("ts", new BsonTimestamp(100, 2)) .append("h", Long.valueOf(1987654321)) .append("ns", "dbA.collectA"); assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(false); source.offsetStructForEvent(REPLICA_SET_NAME, event); assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(true); Map<String, ?> offset = source.lastOffset(REPLICA_SET_NAME); assertThat(offset.get(SourceInfo.TIMESTAMP)).isEqualTo(100); assertThat(offset.get(SourceInfo.ORDER)).isEqualTo(2); assertThat(offset.get(SourceInfo.OPERATION_ID)).isEqualTo(1987654321L); BsonTimestamp ts = source.lastOffsetTimestamp(REPLICA_SET_NAME); assertThat(ts.getTime()).isEqualTo(100); assertThat(ts.getInc()).isEqualTo(2); Struct struct = source.lastOffsetStruct(REPLICA_SET_NAME,new CollectionId(REPLICA_SET_NAME,"dbA","collectA")); assertThat(struct.getInt32(SourceInfo.TIMESTAMP)).isEqualTo(100); assertThat(struct.getInt32(SourceInfo.ORDER)).isEqualTo(2); assertThat(struct.getInt64(SourceInfo.OPERATION_ID)).isEqualTo(1987654321L); assertThat(struct.getString(SourceInfo.NAMESPACE)).isEqualTo("dbA.collectA"); assertThat(struct.getString(SourceInfo.REPLICA_SET_NAME)).isEqualTo(REPLICA_SET_NAME); assertThat(struct.getString(SourceInfo.SERVER_NAME)).isEqualTo("serverX"); assertThat(struct.getBoolean(SourceInfo.INITIAL_SYNC)).isEqualTo(true); }
@Test public void shouldReturnOffsetForUnusedReplicaName() { assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(false); Map<String, ?> offset = source.lastOffset(REPLICA_SET_NAME); assertThat(offset.get(SourceInfo.TIMESTAMP)).isEqualTo(0); assertThat(offset.get(SourceInfo.ORDER)).isEqualTo(0); assertThat(offset.get(SourceInfo.OPERATION_ID)).isNull(); BsonTimestamp ts = source.lastOffsetTimestamp(REPLICA_SET_NAME); assertThat(ts.getTime()).isEqualTo(0); assertThat(ts.getInc()).isEqualTo(0); Struct struct = source.lastOffsetStruct(REPLICA_SET_NAME,new CollectionId(REPLICA_SET_NAME,"dbA","collectA")); assertThat(struct.getInt32(SourceInfo.TIMESTAMP)).isEqualTo(0); assertThat(struct.getInt32(SourceInfo.ORDER)).isEqualTo(0); assertThat(struct.getInt64(SourceInfo.OPERATION_ID)).isNull(); assertThat(struct.getString(SourceInfo.NAMESPACE)).isEqualTo("dbA.collectA"); assertThat(struct.getString(SourceInfo.REPLICA_SET_NAME)).isEqualTo(REPLICA_SET_NAME); assertThat(struct.getString(SourceInfo.SERVER_NAME)).isEqualTo("serverX"); assertThat(struct.getBoolean(SourceInfo.INITIAL_SYNC)).isNull(); assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(false); }
@Test public void shouldReturnOffsetForUnusedReplicaNameDuringInitialSync() { source.startInitialSync(REPLICA_SET_NAME); assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(false); Map<String, ?> offset = source.lastOffset(REPLICA_SET_NAME); assertThat(offset.get(SourceInfo.TIMESTAMP)).isEqualTo(0); assertThat(offset.get(SourceInfo.ORDER)).isEqualTo(0); assertThat(offset.get(SourceInfo.OPERATION_ID)).isNull(); BsonTimestamp ts = source.lastOffsetTimestamp(REPLICA_SET_NAME); assertThat(ts.getTime()).isEqualTo(0); assertThat(ts.getInc()).isEqualTo(0); Struct struct = source.lastOffsetStruct(REPLICA_SET_NAME,new CollectionId(REPLICA_SET_NAME,"dbA","collectA")); assertThat(struct.getInt32(SourceInfo.TIMESTAMP)).isEqualTo(0); assertThat(struct.getInt32(SourceInfo.ORDER)).isEqualTo(0); assertThat(struct.getInt64(SourceInfo.OPERATION_ID)).isNull(); assertThat(struct.getString(SourceInfo.NAMESPACE)).isEqualTo("dbA.collectA"); assertThat(struct.getString(SourceInfo.REPLICA_SET_NAME)).isEqualTo(REPLICA_SET_NAME); assertThat(struct.getString(SourceInfo.SERVER_NAME)).isEqualTo("serverX"); assertThat(struct.getBoolean(SourceInfo.INITIAL_SYNC)).isEqualTo(true); assertThat(source.hasOffset(REPLICA_SET_NAME)).isEqualTo(false); }
assertThat(secondToLast.sourceOffset().containsKey(SourceInfo.SNAPSHOT_KEY)).isTrue(); assertThat(((Struct) secondToLast.value()).getStruct(Envelope.FieldName.SOURCE).getBoolean(SourceInfo.SNAPSHOT_KEY)).isTrue(); assertThat(((Struct) last.value()).getStruct(Envelope.FieldName.SOURCE).getBoolean(SourceInfo.SNAPSHOT_KEY)).isTrue();
Boolean c1 = after.getBoolean("c1"); assertThat(c1).isEqualTo(Boolean.TRUE);
private void changeSourceToLastSnapshotRecord(SourceRecord currentRecord) { final Struct envelope = (Struct)currentRecord.value(); final Struct source = (Struct)envelope.get("source"); if (source.getBoolean(SourceInfo.LAST_SNAPSHOT_RECORD_KEY) != null) { source.put(SourceInfo.LAST_SNAPSHOT_RECORD_KEY, true); } }
protected void assertRecordOffsetAndSnapshotSource(SourceRecord record, boolean shouldBeSnapshot, boolean shouldBeLastSnapshotRecord) { Map<String, ?> offset = record.sourceOffset(); assertNotNull(offset.get(SourceInfo.TXID_KEY)); assertNotNull(offset.get(SourceInfo.TIMESTAMP_KEY)); assertNotNull(offset.get(SourceInfo.LSN_KEY)); Object snapshot = offset.get(SourceInfo.SNAPSHOT_KEY); Object lastSnapshotRecord = offset.get(SourceInfo.LAST_SNAPSHOT_RECORD_KEY); if (shouldBeSnapshot) { assertTrue("Snapshot marker expected but not found", (Boolean) snapshot); assertEquals("Last snapshot record marker mismatch", shouldBeLastSnapshotRecord, lastSnapshotRecord); } else { assertNull("Snapshot marker not expected, but found", snapshot); assertNull("Last snapshot marker not expected, but found", lastSnapshotRecord); } final Struct envelope = (Struct)record.value(); if (envelope != null) { final Struct source = (Struct)envelope.get("source"); final Boolean sourceSnapshot = source.getBoolean(SourceInfo.SNAPSHOT_KEY); final Boolean sourceLastSnapshotRecord = source.getBoolean(SourceInfo.LAST_SNAPSHOT_RECORD_KEY); if (shouldBeSnapshot) { assertTrue("Snapshot marker expected in source but not found", sourceSnapshot); assertEquals("Last snapshot record marker in source mismatch", shouldBeLastSnapshotRecord, sourceLastSnapshotRecord); } else { assertNull("Source snapshot marker not expected, but found", sourceSnapshot); assertNull("Source last snapshot marker not expected, but found", sourceLastSnapshotRecord); } } }
assertThat(secondToLast.sourceOffset().containsKey(SourceInfo.SNAPSHOT_KEY)).isTrue(); assertThat(((Struct) secondToLast.value()).getStruct(Envelope.FieldName.SOURCE).getBoolean(SourceInfo.SNAPSHOT_KEY)).isTrue(); assertThat(((Struct) last.value()).getStruct(Envelope.FieldName.SOURCE).getBoolean(SourceInfo.SNAPSHOT_KEY)).isTrue();
Boolean c1 = after.getBoolean("c1"); assertThat(c1).isEqualTo(Boolean.TRUE);