Repository navigation
VReplication: reload the schema at a DDL only once MySQL has committed it - #21887
Conversation
A vstreamer reloads the schema engine as soon as it reads a schema-changing DDL from the binlog, at the DDL's position. But MySQL sends a transaction's binlog events to the binlog dump threads once the binlog is synced, before the storage engine commit, and with semi-sync AFTER_SYNC the commit then waits for a replica's acknowledgement. A reload in that window doesn't see the DDL: for an Online DDL cutover, the renamed-over table still has its old create_time. ReloadAtEx still records the DDL's position as reloaded, so the other vstreams skip their reloads, and the schema tracker saves the pre-DDL schema as the DDL's version, which every later replay across the DDL then decodes its rows with. The vstreamer now calls a new Engine.ReloadAtDDL, which first waits until MySQL has committed the DDL's own GTID (WAIT_FOR_EXECUTED_GTID_SET: @@gtid_executed includes a transaction only once it has committed). It waits only for that GTID, because the rest of a stream's position can hold GTIDs this server never executes, and only when the engine hasn't already reloaded at the position. The wait runs on its own connection without holding the engine's mutex, is bounded by --schema-change-reload-timeout, and closes the connection when its context ends, since the wait query doesn't watch it. A wait over a second is logged. File position and MariaDB GTIDs aren't waited on: those waits follow a replica's SQL thread, which doesn't advance on a primary. The vstreamer tests' createSchemaVersionTable helper now creates _vt.schema_version as the sidecar database does, with an AUTO_INCREMENT id, which the schema tracker's insert needs after TestVersion drops the table. Fixes #21883 Signed-off-by: Matt Lord <mattalord@gmail.com>
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
There was a problem hiding this comment.
🟡 Changes recommended
Concurrent vstreams waiting on the same DDL can each open an unpooled MySQL connection, potentially exhausting max_connections and disrupting streams and other database access.
1 open issue
What changed in this PR
This PR addresses #21883 by delaying DDL-triggered schema reloads until MySQL has committed the DDL, preventing the schema tracker from saving pre-DDL definitions.
Changes:
- Add a bounded commit wait for the DDL’s own MySQL GTID before reloading.
- Pass the current transaction’s GTID from the vstreamer to the schema engine.
- Add regression coverage and align the schema-version test table with the sidecar definition.
| File | Description |
|---|---|
go/vt/vttablet/tabletserver/vstreamer/vstreamer.go |
Track the current GTID and use the DDL-aware reload. |
go/vt/vttablet/tabletserver/vstreamer/vstreamer_test.go |
Test schema visibility during delayed commits and correct the test table definition. |
go/vt/vttablet/tabletserver/schema/engine.go |
Wait for DDL commit with timeout, cancellation, and slow-wait logging. |
go/vt/vttablet/tabletserver/schema/engine_test.go |
Cover GTID waits, cached reloads, timeouts, and non-MySQL fallback. |
Review effort: Balanced
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #21887 +/- ##
===========================================
+ Coverage 69.67% 80.73% +11.06%
===========================================
Files 1614 29 -1585
Lines 216793 5310 -211483
===========================================
- Hits 151044 4287 -146757
+ Misses 65749 1023 -64726
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…ach it Each vstream that reached a DDL before any reload at it opened its own unpooled MySQL connection to wait for the DDL to commit. While the commit is held for a semi-sync acknowledgement, that is one connection per vstream for up to --schema-change-reload-timeout, and then again on every retry, at the moment the primary is already short of connections. The waits for a DDL are now coalesced by its GTID with singleflight: the vstreams that reach it share one wait on one connection, and each still stops waiting when its own context ends. The shared wait runs on a context that no single caller can cancel, so it can outlive its callers: it ends when MySQL commits the DDL, when the reload timeout passes, or if its connection fails. A caller that joins late shares the first wait's deadline, and its error no longer claims it waited for the whole timeout. Signed-off-by: Matt Lord <mattalord@gmail.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Correctness depends on MySQL binlog/commit visibility affecting persistent schema history, while the real-MySQL semi-sync regression and connection timeout behavior require runtime validation beyond static review.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
TestOpenAndReload started the second caller of ReloadAtDDL in a goroutine and didn't wait for it to join the shared wait for the DDL. The DDL commits only after the third caller's 200ms deadline, so a second caller that wasn't scheduled until the shared wait had ended would have started a wait of its own and failed the test, though the wait is shared correctly. Pass the second caller a context that records when its Done channel is first asked for, which waitForDDLCommit does only once it is waiting, and wait for that before cancelling the vstream that started the wait. Signed-off-by: Matt Lord <mattalord@gmail.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed to evaluate external-MySQL streams ignoring the configured timeout and long DDL waits blocking heartbeats, potentially terminating client streams.
No open issues 🎉
1 resolved since last review
Review effort: Max
Signed-off-by: Matt Lord <mattalord@gmail.com>
maxenglander
left a comment
There was a problem hiding this comment.
Thanks Matt, this looks good to me. I traced the wait for tablet vstreams, external MySQL sources and the binlog server connector. In each case it waits on the same MySQL that the binlog comes from, or it's skipped. One small non-blocking note inline.
| // waits, since the totals of the SchemaReload timings count only reloads. | ||
| start := time.Now() | ||
| defer se.env.Stats().WaitTimings.Record("DDLCommitWait", start) | ||
| ctx, cancel := context.WithTimeout(ctx, se.reloadTimeout) |
There was a problem hiding this comment.
For external MySQL sources, se.reloadTimeout seems to always be the 30s default here. externalConnector.Get builds its schema engine from tabletenv.NewDefaultConfig(), and vreplication.NewEngine only passes it config.ExternalConnections. So --schema-change-reload-timeout doesn't apply to these streams, even though its help text now says it bounds this wait. That's already true of their reload on main, so the wait just inherits it. I think it would be nice to pass the configured timeout into the external connector, which would fix both. Fine with me in this PR or a follow-up, also open to neither if you think it's a non-issue.
There was a problem hiding this comment.
Thanks for the review, Max, and for tracing all three paths! You're right: externalConnector.Get builds its engines' env from tabletenv.NewDefaultConfig(), so streams from an external source always get the 30s default, for the reload and now for the wait too. It isn't only this flag, though. Apart from the connection parameters, every tabletenv setting those engines read is the default there, such as --queryserver-config-schema-reload-time and --vreplication-copy-phase-max-innodb-history-list-length. Some, like --track-schema-versions, don't suit an external MySQL, so I think the fix needs to decide which of the tablet's settings an external source should follow. I'd rather do that in a follow-up than plumb this one setting through in a PR that we're backporting, so I'll open an issue for it. In the meantime, I've noted the 30s bound for external sources in the PR description.
Promptless documentation updates
|
The cherry-pick conflicted in three places, and the bot also added the whole v25.0.0 release summary. - vstreamer.go: on release-24.0, eventGTID is already a replication.GTID, assigned directly in parseEvent at the GTID event and cleared at format description, rotate, stop and previous-GTIDs events. main formats it into a string through setEventGTID, which #21887 extended to record a separate gtid field. Kept release-24.0's eventGTID, dropped the gtid field and setEventGTID, and pass vs.eventGTID to ReloadAtDDL. It carries the same DDL GTID. - engine_test.go: import block. Kept release-24.0's strings import alongside #21887's sync and sync/atomic. - engine.go: added the log/slog import, which main already had and the new slow-wait log needs. - changelog/25.0/25.0.0/summary.md: removed. The file does not exist on release-24.0. Signed-off-by: Matt Lord <mattalord@gmail.com>
In vstreamer.go, release-23.0 keeps eventGTID as a replication.GTID, set and cleared at the same binlog events as main's new gtid field, so the gtid field and the setEventGTID change are dropped and the DDL handler passes vs.eventGTID to ReloadAtDDL. The reload at a DDL is best effort on release-23.0 (main's error check and rebuildPlans come from #19204), so the error is still not returned; it is logged unless the stream was cancelled. The call uses the stream's context rather than context.Background(), so that a stream cancel does not wait out the DDL commit wait. In engine.go, log.Warningf replaces the slog-style log.Warn, which release-23.0 does not have. In engine_test.go, the imports keep release-23.0's strings and add sync and sync/atomic. In vstreamer_test.go, only TestSchemaReloadWaitsForDDLCommit and the four helpers it uses are kept (createSchemaVersionTable as #21887 changed it, isolateSchemaVersionTable, insertSchemaVersionRow and makeHistorianMinimalTable). The historian startup tests and helpers from #19697, which is not on release-23.0, are dropped. The replication and schema imports are added, and one comment is reworded, since release-23.0's tracker streams from the current position. The 25.0.0 changelog summary is dropped, since release-23.0 does not have it. Signed-off-by: Matt Lord <mattalord@gmail.com>


Description
A vstreamer reloads the schema engine as soon as it reads a schema-changing DDL from the binlog, at the DDL's position (
ReloadAt). But MySQL sends a transaction's binlog events to the binlog dump threads once the binlog is synced, before the storage engine commit, and with semi-syncAFTER_SYNCthe commit then waits for a replica's acknowledgement. A reload in that window doesn't see the DDL:create_time, so the reload finds nothing to reload.ReloadAtExstill records the DDL's position as reloaded, so every other vstream'sReloadAtfor that position skips its reload.This PR adds
Engine.ReloadAtDDL, which the vstreamer now calls for a DDL in place ofReloadAt. It waits until MySQL has committed the DDL, then reloads at its position:Conn.WaitUntilPosition(WAIT_FOR_EXECUTED_GTID_SET).@@gtid_executedincludes a transaction only once it has committed and is visible. The rest of the stream's position can contain GTIDs this server never executes, such as errant GTIDs in the position a stream started from, so waiting for the whole position could block forever.binlog_order_commits=ON, MySQL's default.singleflight, keyed by the DDL's GTID) runs in a goroutine that the DDL's first waiter starts. It can outlive its callers: it ends when the DDL commits, when the reload timeout passes, or if its connection fails.--schema-change-reload-timeout(30s by default), the same bound as the reload itself. If the DDL hasn't committed by then, the reload fails and so does the vstream, which its client retries. Today it would have silently loaded the wrong schema.DDLCommitWaittype of theWaitstimings, next to the tablet's other waits. It isn't underSchemaReload, whose totals count only reloads.Backport: the bug goes back to the original historian, and it saves wrong schema versions silently. I think this is worth backporting to release-23.0 and release-24.0. On release-23.0 the vstreamer calls
ReloadAt(context.Background(), ...)and ignores its error, so the backport there needs a small adaptation.Motivation
How do you use Vitess? (pick one)
Did you run this change against a real Vitess deployment? Not against a deployment. #21883 has the reproduction.
Related Issue(s)
Checklist
Deployment Notes
A vstream that reaches a schema-changing DDL now waits until MySQL has committed the DDL before it goes on. This affects VReplication, the VStream API and the schema tracker.
--schema-change-reload-timeout(30s by default) now bounds that wait as well as the reload, so a reload after a DDL can take up to twice that. If the DDL hasn't committed by then, the vstream fails with an error instead of continuing with a schema that doesn't include the DDL.externalConnectionsin--tablet-config) always use the 30s default, for the wait as for the reload, because their engines use the default tablet settings.Waitstimings have a newDDLCommitWaittype that times each of those waits (vttablet_waits_bucket{type="DDLCommitWait"}and its_sumand_countin Prometheus).AI Disclosure
This PR was written with Claude Code.