vitessio / vitess

Vitess is a database clustering system for horizontal scaling of MySQL.
http://vitess.io
Apache License 2.0
18.55k stars 2.09k forks source link

RFC: Importing data into Vitess using VReplication for databases that have foreign key constraints #13136

Closed rohit-nayak-ps closed 6 months ago

rohit-nayak-ps commented 1 year ago

Feature Description

This document discusses the changes required in VReplication workflows related to the proposed foreign key support in Vitess for foreign keys including cascade constraints in unsharded and shard-scoped Vitess clusters.

TL;DR

For importing, we will need to run the vreplication MoveTables workflow using a modified version of the copy phase. We run a single copy phase cycle based on one consistent snapshot for all tables with foreign_key_checks=off, followed by the replicate phase starting from the snapshot gtid. Currently we don't honor the foreign_key_checks settings on connections during the replicate phase: that will be fixed.

After the cutover, vtgate will be emulating fk constraints. As a result, once data is imported, we can run vreplication workflows, including native onlineddl workflows, as they do today since cascade queries will make it to the binlog.

Proposed Design

Current MoveTables flow

VReplication workflows first run a copy phase to bulk copy the data from the source and then switch to the replicate phase where it streams binlogs. The copy phase copies one table at a time. Large tables typically take multiple cycles: by default each cycle is one hour long. In between cycles and tables we also stream from the binlog (the catchup phase) where updates/inserts/deletes for tables and table ranges already copied are applied. The catchup phase is needed so that we are tracking the source binlogs closely and are resilient to the normal binlog purging. More details here.

During the copy phase, including catchup phases, we set foreign_key_checks=off. If we allow cascade constraints, we will see failures during catchup because some tables are not yet present or are partially present.

Proposed flow

To get around this, we propose running a single copy phase which copies all tables using a single consistent snapshot. Essentially no catchup phase. Once the copy is complete, the database is consistent as of the snapshot's gtid. As long as the binlogs as of that gtid are still available, we can then stream from that gtid. We perform the binlog streaming with the foreign_key_checks set by the user. MySQL will then cascade the constraints. Note that both copy phase and replicate phase queries are run directly on vttablet (and not via vtgate).

Proposed Import flow

Post Cutover

Limitations

Notes

Use Case(s)

Databases with tables which use foreign keys with restrict and cascade modes

rohit-nayak-ps commented 6 months ago

Closed via #13137 and #14013