-
Notifications
You must be signed in to change notification settings - Fork 34
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-7293 Fallback to old schema in case of race condition between ROW and FIELD #175
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,8 +5,6 @@ | |||||
*/ | ||||||
package io.debezium.connector.vitess.connection; | ||||||
|
||||||
import static io.debezium.connector.vitess.connection.ReplicationMessage.Column; | ||||||
|
||||||
import java.sql.Types; | ||||||
import java.time.Instant; | ||||||
import java.util.ArrayList; | ||||||
|
@@ -47,8 +45,13 @@ public class VStreamOutputMessageDecoder implements MessageDecoder { | |||||
|
||||||
private final VitessDatabaseSchema schema; | ||||||
|
||||||
private final VitessDatabaseSchema schemaBackup; | ||||||
|
||||||
public VStreamOutputMessageDecoder(VitessDatabaseSchema schema) { | ||||||
this.schema = schema; | ||||||
// Schema can be null. See: VitessConnector.validateConnection | ||||||
if (schema != null) this.schemaBackup = schema.clone(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be no cloning, both |
||||||
else this.schemaBackup = null; | ||||||
} | ||||||
|
||||||
@Override | ||||||
|
@@ -293,16 +296,32 @@ private Optional<Table> resolveRelation(String shard, String schemaName, String | |||||
return Optional.ofNullable(schema.tableFor(VitessDatabaseSchema.buildTableId(shard, schemaName, tableName))); | ||||||
} | ||||||
|
||||||
private Optional<Table> resolveRelationFromBackup(TableId tableId) { | ||||||
return Optional.ofNullable(schemaBackup.tableFor(tableId)); | ||||||
} | ||||||
|
||||||
/** Resolve the vEvent data to a list of replication message columns (with values). */ | ||||||
private List<Column> resolveColumns(Row row, Table table) { | ||||||
return resolveColumns(row, table, false); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Resolve the vEvent data to a list of replication message columns (with values). | ||||||
* NOTE: Sometimes due to race condition in Vitess ROW and TYPE messages can be mixed up, so there's a fallback | ||||||
* the old schema version in case row length mismatches between Row and Table in schema cache. | ||||||
* <a href="https://vitess.io/docs/18.0/reference/vreplication/internal/tracker/#caveat">...</a> | ||||||
*/ | ||||||
private List<Column> resolveColumns(Row row, Table table, Boolean fromBackup) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
int numberOfColumns = row.getLengthsCount(); | ||||||
List<io.debezium.relational.Column> tableColumns = table.columns(); | ||||||
if (tableColumns.size() != numberOfColumns) { | ||||||
throw new IllegalStateException( | ||||||
TableId tableId = table.id(); | ||||||
Optional<Table> backup = fromBackup ? Optional.empty() : resolveRelationFromBackup(tableId); | ||||||
return backup.map(backupTable -> resolveColumns(row, backupTable, true)).orElseThrow(() -> new IllegalStateException( | ||||||
String.format( | ||||||
"The number of columns in the ROW event %s is different from the in-memory table schema %s.", | ||||||
row, | ||||||
table)); | ||||||
table))); | ||||||
} | ||||||
|
||||||
ByteString rawValues = row.getValues(); | ||||||
|
@@ -371,8 +390,12 @@ else if ((field.getFlags() & UNIQUE_KEY_FLAG) != 0) { | |||||
Table table = resolveTable(shard, schemaName, tableName, columns); | ||||||
LOGGER.debug("Number of columns in the resolved table: {}", table.columns().size()); | ||||||
|
||||||
if (schema.tableIds().contains(table.id())) { | ||||||
Table tableToBackup = schema.tableFor(table.id()); | ||||||
LOGGER.info("Old schema for table: {} found in cache, backing up", table.id()); | ||||||
schemaBackup.applySchemaChangesForTable(tableToBackup); | ||||||
} | ||||||
schema.applySchemaChangesForTable(table); | ||||||
return; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.