Errors this answers
for action repairReplicationFailed to reconnect to primary: deadline exceededis primary, but its type is REPLICA
Symptom
PlannedReparentShard (PRS) should take a second or two. On Vitess v14 it keeps taking much longer, about 16 to 18 seconds with a Consul topology, with the odd run finishing in 2 or 3 seconds for no obvious reason. The PRS succeeds every time, so nothing alerts; it's just slow.
The old primary's vttablet log shows a lock attempt during the PRS, and with Consul a lock timeout about 15 seconds later:
locks.go:375] Locking shard commerce/80-c0 for action repairReplication to zone1-0000000101 as parent)
replmanager.go:127] Failed to reconnect to primary: deadline exceeded: vitess/global/keyspaces/commerce/shards/80-c0/Lock, will keep retrying.
Why it happens
A self-inflicted deadlock on the old primary:
- PRS, running in vtctld, takes the shard lock, demotes the old primary, and calls
SetReplicationSourceon it so it replicates from the new primary. - On the old primary,
setReplicationSourceLockedchanges the tablet type toREPLICA. The type change makes the replication manager run an immediate check, and because MySQL replication isn't set up yet, the check decides replication is broken and callsrepairReplication. repairReplicationasks for the shard lock so it can point replication at the primary. PRS holds that lock, and PRS is waiting for this sameSetReplicationSourcecall to return. Nothing moves until the lock attempt gives up.- Then the call returns and PRS finishes. The repair never did anything useful: had it got the lock, it would have called
setReplicationSourceLocked, the function that was already running.
The fast runs are the same bug failing in a different place. If the topology still lists the old primary as PRIMARY when the check runs, repairReplication stops before asking for the lock, with an error ending in is primary, but its type is REPLICA. No lock wait, so PRS takes 2 or 3 seconds, but the repair still failed.
How long the stall lasts depends on how long the topology server's lock attempt waits. We ran 10 PRSs in a row in a lab, each one rotating the primary:
| Release | Topology | PRS time (median) | Old primary asks for the shard lock |
|---|---|---|---|
| v14.0.5 | etcd | 6.4 s | Every PRS |
| v14.0.5 | Consul | 16.7 s | Every PRS |
| v24.0.2 | Consul | 1.1 s | Never |
In the etcd runs, vtctld's log shows PRS taking the shard lock, the old primary asking for the same lock a second later, and PRS releasing it about five seconds after that. In our testing, v12 didn't have the problem.
Check
Time a PRS on a shard nothing else is using:
time vtctldclient PlannedReparentShard commerce/80-c0 --new-primary zone1-0000000101
Then look in the old primary's vttablet log for a repairReplication lock attempt during that PRS:
grep -E 'for action repairReplication|Failed to reconnect to primary|is primary, but its type is REPLICA' vttablet.INFO
A Locking shard ... for action repairReplication line inside the PRS window, with PRS taking seconds longer than it should, is this bug.
Fix
Upgrade. v24 doesn't do this: in the lab the old primary never asked for the shard lock, and PRS took about a second. We haven't pinned down which release in between fixed it.
Or patch v14. The fix is to skip the repair when it was triggered from inside setReplicationSourceLocked, since that code is already doing what the repair would do. That's what this patch to a v14.0.5 fork does: it moves the "is setReplicationSourceLocked already running?" guard far enough down the call stack to cover the replication manager's immediate check. With it, the old primary no longer asks for the lock during PRS, and the stall goes away.
Raising lock or RPC timeouts doesn't help: it only makes the stall longer.
Why it matters
- PRS holds the shard lock the whole time, so anything else that needs it, such as another reparent, waits too. A tool that reparents shards one after another, such as a rolling restart or a fleet-wide upgrade, takes several times longer than it should.
- The fast runs hide it. Timing a few PRSs and seeing 2 seconds proves nothing; that's the same bug failing earlier.
Gotchas
- Reproducing it takes many runs on real clusters, because whether a given PRS takes the slow or the fast path depends on how quickly the topology record catches up. In the lab, with nothing else touching the topology, it took the slow path every time.
- The log line comes from the old primary, not from vtctld or the new primary. vtctld's log only shows PRS holding the lock for longer than it should.
Related
Running this fleet-wide, by hand?
HASHJOIN builds the control plane that runs upgrades, failovers and repairs like this one across every host, with a lock per unit, a drain on Ctrl-C and a log you can read at 3 a.m.
New fixes and field notes by email, about once a month.