KB · Vitess · Fix

PlannedReparentShard takes 15+ seconds: "Locking shard ... for action repairReplication"

Vitess · Fix · Verified on Vitess v14 and v24 · Updated 2026-12-04

Errors this answers

  • for action repairReplication
  • Failed to reconnect to primary: deadline exceeded
  • is 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:

  1. PRS, running in vtctld, takes the shard lock, demotes the old primary, and calls SetReplicationSource on it so it replicates from the new primary.
  2. On the old primary, setReplicationSourceLocked changes the tablet type to REPLICA. 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 calls repairReplication.
  3. repairReplication asks for the shard lock so it can point replication at the primary. PRS holds that lock, and PRS is waiting for this same SetReplicationSource call to return. Nothing moves until the lock attempt gives up.
  4. 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:

ReleaseTopologyPRS time (median)Old primary asks for the shard lock
v14.0.5etcd6.4 sEvery PRS
v14.0.5Consul16.7 sEvery PRS
v24.0.2Consul1.1 sNever

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

Gotchas

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.