Skip to content

Move tables n:m support#693

Merged
morgo merged 14 commits into
block:mainfrom
morgo:move-nm-support-v2
Apr 14, 2026
Merged

Move tables n:m support#693
morgo merged 14 commits into
block:mainfrom
morgo:move-nm-support-v2

Conversation

@morgo
Copy link
Copy Markdown
Collaborator

@morgo morgo commented Apr 13, 2026

A Pull Request should be associated with an Issue.

We wish to have discussions in Issues. A single issue may be targeted by multiple PRs.
If you're offering a new feature or fixing anything, we'd like to know beforehand in Issues,
and potentially we'll be able to point development in a particular direction.
Further notes in https://github.com/block/spirit/blob/main/.github/CONTRIBUTING.md

Fixes #690

morgo added 10 commits April 11, 2026 08:08
Replace individual SourceDB/SourceConfig/SourceDSN/TargetDSN fields
with Sources []SourceResource slice in the Resources struct. This
prepares the check package for N:M move operations where multiple
source databases need to be validated.

All checks updated to iterate over Sources:
- configuration: validates MySQL config on all sources
- privileges: checks privileges on all sources via checkSourcePrivileges helper
- resume_state: uses Sources[0] for checkpoint validation
- target_state/table_compatibility: unchanged (use SourceTables only)

The runner wraps its single source into []SourceResource{...} for
backward compatibility. No behavioral change for 1:1 moves.
Refactor CutOver to accept []CutOverSource instead of a single
db/feed pair. This prepares the cutover for N:M move operations
where multiple source databases need to be locked, flushed, and
renamed atomically.

Key changes:
- New CutOverSource struct with DB, ReplClient, Tables
- NewCutOver accepts []CutOverSource
- algorithmCutover locks all sources, flushes all feeds, checks
  all changes flushed, then renames per source
- On partial rename failure, completed renames are rolled back
  by reversing the RENAME TABLE statements

The runner wraps its single source into []CutOverSource{...} for
backward compatibility. No behavioral change for 1:1 moves.
Support multiple source databases in move operations, enabling N:M
resharding where data from N source shards is redistributed across
M target shards.

Depends on:
- check-resources-refactor (Sources []SourceResource)
- cutover-multi-source ([]CutOverSource with rollback)

Key changes:

Runner struct:
- New sourceInfo struct holds per-source db, config, dsn, replClient, tables
- Runner.sources []sourceInfo replaces singular source/sourceConfig/replClient
- Helper methods: flushAllReplClients, setWatermarkOptimizationAll,
  getDeltaLenAll, stopPeriodicFlushAll

Move struct:
- New SourceDSNs []string field for programmatic N:M configuration
- SourceDSN remains as CLI convenience for 1:1 case

Checkpoint:
- New binlog_positions TEXT column (JSON) replaces binlog_name/binlog_pos
- Per-source binlog positions serialized as JSON keyed by DB name
- Stored on sources[0] by convention

Foundation:
- TableInfo.DB() accessor for per-chunk source routing
- Buffered copier uses chunk.Table.DB() instead of single c.db

Tests:
- New TestNtoMShardedMove: 2 sources x 2 targets, 100 rows redistributed
- Existing tests updated for new struct layout
# Conflicts:
#	pkg/move/cutover.go
#	pkg/move/runner.go
# Conflicts:
#	pkg/move/check/configuration.go
#	pkg/move/check/resume_state.go
#	pkg/move/runner.go
@morgo morgo marked this pull request as ready for review April 14, 2026 13:14
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the move/reshard runner to support N:M moves (multiple source shards to multiple target shards), addressing Issue #690.

Changes:

  • Add multi-source support in move.Runner by managing per-source DB/config/repl-client state and subscribing chunkers per source.
  • Update checkpointing to persist per-source binlog positions (as JSON) and restore them on resume.
  • Adjust the buffered copier to read chunk data from the correct per-chunk source DB, and add E2E tests for N:M sharded moves and determinism.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
pkg/testutils/testing.go Ensures unique test DB names within a single test via an atomic counter.
pkg/table/tableinfo.go Exposes TableInfo.DB() so downstream components can read from the correct source DB.
pkg/move/runner.go Core N:M runner changes: multiple sources, multiple repl clients, per-source checkpoint binlog positions, multi-source locks/flush/status.
pkg/move/move.go Adds Move.SourceDSNs to represent multiple sources for N:M moves.
pkg/copier/buffered.go Reads chunk rows using the chunk’s source table DB connection (required for N:M).
pkg/move/move_test.go Updates resume E2E test wiring for the new multi-source runner structure.
pkg/move/move_sharded_test.go Adds E2E tests for N:M sharded moves and checkpoint determinism.
pkg/move/check/target_state.go Minor formatting cleanup.
pkg/move/check/resume_state.go Minor error message tweak/formatting cleanup.
pkg/move/check/privileges_test.go Adds coverage that privileges checks iterate across all sources.
pkg/move/check/configuration_test.go Adds coverage that configuration checks handle multiple sources.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/move/runner.go
Comment thread pkg/move/runner.go
Comment thread pkg/move/runner.go
Comment thread pkg/move/runner.go
Comment thread pkg/move/runner.go Outdated
morgo and others added 2 commits April 14, 2026 08:04
Address PR review feedback:
- Add sourceKey() helper (addr/dbname) for stable source identification
- Sort sources by sourceKey instead of raw DSN to survive credential rotations
- Use sourceKey as checkpoint map key to avoid collisions when sources share DBName
- Avoid mutating caller's Move.SourceDSNs slice by sorting parsed sources instead
- Update determinism test to match new sort behavior
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@morgo morgo merged commit d7c75c1 into block:main Apr 14, 2026
12 of 13 checks passed
@morgo morgo deleted the move-nm-support-v2 branch April 14, 2026 19:56
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.

Support N:M move (reshard) operations

3 participants