Skip to content

[core][flink] Add startup schema evolution for MySQL CDC - #11490

Open
lilei1128 wants to merge 3 commits into
apache:masterfrom
lilei1128:cdc-schmea-evolution
Open

lilei1128 wants to merge 3 commits into
apache:masterfrom
lilei1128:cdc-schmea-evolution

Conversation

@lilei1128

@lilei1128 lilei1128 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

MySQL CDC jobs fail at startup when the source schema drifted from the
Paimon table (e.g. a shadow table was renamed into place but the schema
change never reached Paimon). Recovery currently requires manual ALTERs.

Add an opt-in --add_missing_columns_on_startup option to
mysql_sync_table / mysql_sync_database. During job submission the action
reads the current MySQL schema through JDBC, applies the configured type
mappings, then for each Paimon table:
- appends missing source columns as nullable fields (never outside
primary/partition/bucket keys), so historical rows read as NULL;
- widens compatible existing columns (VARCHAR/INT/PI precision, decimal
with preserved integer range, honoring decimal-no-change) while
keeping the target's existing nullability and any already-wider type;
- rejects incompatible changes before touching the table and commits all
additions/updates of a table as a single schema version.

To make the batch safe under concurrent schema edits, introduce the
SchemaChange.checkSchemaVersion change in paimon-api/paimon-core: the
schema build fails with ConcurrentModificationException if the table moved
past the version used for validation. On conflict the action reloads the
table, recomputes and retries (up to two retries). This is guarded so the
existing single-table atomicity and the --ignore_incompatible behavior
of database sync are preserved. Docs updated in mysql-cdc.mdx, including
the note that REST Catalog services must support checkSchemaVersion and
that cross-table updates are sequential (not atomic across tables).

See the error message: " java.lang.IllegalArgumentException:Incompatible schema found."

Tests

  • paimon-api: SchemaChangeTest#testCheckSchemaVersionJsonRoundTrip,
    #testCheckSchemaVersionEquality
  • paimon-core: SchemaManagerTest#testChangesWithMatchingSchemaVersion,
    #testStaleSchemaVersionPreventsPartialCommit (verifies no partial commit)
  • paimon-flink-cdc: MySqlStartupSchemaEvolutionTest (add nullable columns,
    widening, decimal integer-range/decimal-no-change, key exclusion,
    concurrent add/drop/rename reload and retry, last-retry conflict keeps
    original exception as suppressed, per-table atomicity)
  • ITCases: MySqlSyncDatabaseActionITCase#testAddMissingColumnsOnStartup
    (DIVIDED/COMBINED), #testWidenColumnsOnStartup,
    #testStartupRepairKeepsWiderTargetType,
    #testStartupRepairIgnoresIncompatibleTable;
    MySqlSyncTableActionITCase#testAddMissingColumnsOnStartupAfterRename,
    #testRestoreSavepointAfterAddingMissingColumnsOnStartup,
    #testWidenColumnsOnStartupAfterRename,
    #testIncompatibleStartupTypeChangeDoesNotAddMissingColumns

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The startup repair addresses a real recovery case after a MySQL table replacement. I ran 101 schema and Flink CDC tests locally on this commit. The Flink 1 CI job also passed both MySQL sync integration suites. Flink 1 CDC CI

validateChanges(table, identifier, changes);
try {
catalog.alterTable(identifier, changes, false);
} catch (Catalog.ColumnAlreadyExistException | ConcurrentModificationException e) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Reload the schema after a REST version conflict

If an operator runs a concurrent ALTER TABLE after the CDC action loads the target, SchemaManagerUtils raises ConcurrentModificationException for the stale version. The repository's REST test server maps this exception to HTTP 500, which the REST client handles outside this catch. The job submission therefore fails before it can reload and retry, even with a server that accepts the new action. Please define a version-conflict response that survives the REST round trip and add an integration test that advances the schema between the GET and ALTER.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. Fixed by adding a 409 + SCHEMA response for version conflicts and an integration test to verify schema reload and retry.

@JsonSubTypes({
@JsonSubTypes.Type(
value = SchemaChange.CheckSchemaVersion.class,
name = SchemaChange.Actions.CHECK_SCHEMA_VERSION_ACTION),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Add the version check to the REST ALTER schema

Startup evolution sends this action as the first entry in AlterTableRequest.changes. I checked docs/static/rest-catalog-open-api.yaml and found that both the schema-change union and its discriminator exclude checkSchemaVersion. A client or gateway that validates against the published contract will reject the entire batch. Please add the action and its required int64 schemaId to the OpenAPI definition and verify a whole ALTER request against it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by adding checkSchemaVersion to the OpenAPI spec.

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified the schema-version conflict retry, MySQL 5.7 savepoint restore, and database sync in both modes. All 112 focused tests passed on the integration with master (2be2aed46d).

private final long schemaId;

@JsonCreator
private CheckSchemaVersion(@JsonProperty(FIELD_SCHEMA_ID) long schemaId) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Require an explicit integer schema version before applying ALTER

The published schema requires an integer schemaId. I sent an ALTER containing checkSchemaVersion followed by addColumn to the REST test service with a target at schema version 0. A missing schemaId, null, 0.5, and "0" each decoded as 0, returned HTTP 200, and committed the added column. An explicit 1 returned 409 SCHEMA and left the table at version 0. Please validate the presence and integer type at deserialization, and cover the bad-request response and unchanged schema in the HTTP test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants