Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

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 @@ -132,8 +132,15 @@ String getLSN() {
}

/*
* For SqlServer we compare strings for LSN. LSN format is like 00000021:0000010f:0001.
* String comparison is sufficient for Datastream's string representation.
* For SqlServer, LSN is the source of truth for ordering. Datastream's SQL Server change-table
* reader does not stamp each event with its own commit time (events in a read share one
* timestamp, which can be later than the commit and can move backwards between reads), so
* ordering by timestamp first can make a newer UPDATE/DELETE look older and get skipped.
* When both events are CDC events with different LSNs, the LSN decides. Datastream's CDC LSN
* (the change table __$seqval) is fixed-width hex, so string comparison follows LSN order.
* Backfill events use a different format ("0000002E:00001890:0002", or empty/zeros) that does not
* compare correctly against CDC LSNs as strings, so if either event is not a CDC event, or the
* LSNs are equal, fall back to timestamp, then LSN.
*/
@Override
public int compareTo(ChangeEventSequence o) {
Expand All @@ -143,6 +150,13 @@ public int compareTo(ChangeEventSequence o) {
}
SqlServerChangeEventSequence other = (SqlServerChangeEventSequence) o;

if (isCdcLsn(this.lsn) && isCdcLsn(other.getLSN())) {
int lsnComparisonResult = this.lsn.compareTo(other.getLSN());
if (lsnComparisonResult != 0) {
return lsnComparisonResult;
}
}

int timestampComparisonResult = this.timestamp.compareTo(other.getTimestamp());

if (timestampComparisonResult != 0) {
Expand All @@ -152,6 +166,11 @@ public int compareTo(ChangeEventSequence o) {
}
}

/** CDC events carry the change table __$seqval as plain hex; backfill LSNs are empty or contain ':'. */
private static boolean isCdcLsn(String lsn) {
return !lsn.isEmpty() && lsn.indexOf(':') < 0;
}

@Override
public String toString() {
return "SqlServerChangeEventSequence{" + "timestamp=" + timestamp + ", lsn=" + lsn + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ public void canOrderBasedOnLsn() {
assertTrue(newEvent.compareTo(oldEvent) > 0);
}

@Test
public void lsnWinsOverTimestampForCdcEvents() {
// Datastream stamped the INSERT later than its own UPDATE/DELETE (seen in load test).
SqlServerChangeEventSequence insert =
new SqlServerChangeEventSequence(1795685652L, "00000055000049E8000C");
SqlServerChangeEventSequence update =
new SqlServerChangeEventSequence(1795685523L, "000000620000C1C80002");
SqlServerChangeEventSequence delete =
new SqlServerChangeEventSequence(1795685528L, "000000650000DA200002");
assertTrue(insert.compareTo(update) < 0);
assertTrue(update.compareTo(delete) < 0);
assertTrue(delete.compareTo(insert) > 0);
}

@Test
public void backfillLsnFormatFallsBackToTimestamp() {
// Backfill events carry "XXXXXXXX:XXXXXXXX:XXXX"; as a string it sorts after a later CDC LSN.
SqlServerChangeEventSequence backfill =
new SqlServerChangeEventSequence(100L, "0000002E:00001890:0002");
SqlServerChangeEventSequence cdcUpdate =
new SqlServerChangeEventSequence(200L, "0000002E000019000002");
assertTrue(backfill.compareTo(cdcUpdate) < 0);
assertTrue(cdcUpdate.compareTo(backfill) > 0);
}

@Test
public void equalTimestampAndLsn() {
SqlServerChangeEventSequence event1 = new SqlServerChangeEventSequence(eventTimestamp, lsn1);
Expand Down
Loading