Repository navigation
GH-6043 An ejected node that comes back only reclaims the agents nobody else took over - #6044
Conversation
…nts nobody else took over
|
@Hulkstance How did this happen in real life? What went wrong in production that you saw this? |
jeremydmiller
left a comment
There was a problem hiding this comment.
Thanks, this is the right fix: re-registration now claims only the rows nobody owns, and the sweep stops the stale local copy without touching the peer's row. Merging as is. The bulk TryClaimAssignmentsAsync and the remaining last-writer-wins path through StartAgentAsync/AddAssignmentAsync can be follow-ups.
We run a few nodes where an agent running twice at the same time would be really bad, so before trusting the failover I wanted to know what happens if a node gets ejected but isn't actually dead, like a long GC pause or a paused container. Most of that code already claims rows only if nobody has them since #4408, but re-registration still used AssignAgentsAsync, so I went down that rabbit hole. We had a workaround on our side for it, which we can now remove. Thanks for merging it so fast! |
|
So, not production usage. Only doing purposeful chaos monkey testing? |
no, no prod yet. I'm building a trading bot platform meant to scale out to a million users, and I've been running experiments with several nodes using Claude. So basically these findings are from Claude |
Fixes #6043.
When a node is frozen or cut off for longer than StaleNodeTimeout, the other nodes eject it and the leader hands its agents to the survivors. If the node was actually still alive, its next heartbeat finds its row gone and re-registers it, which is the GH-3604 path. The trouble is what it does with its agents after that. It restores their assignment rows with AssignAgentsAsync, and that's a last-writer-wins write (a delete and insert on SQL Server, an upsert on the other stores). So the rows move back to the node that came back, even for agents another node is already running.
Here's how it plays out. Node A runs agent X. A freezes past the stale timeout, B ejects A, and the leader starts X on B, so the row for X now says B. Then A wakes up, still running its old copy of X, and its heartbeat rewrites the row to say A. From that point both copies are live, and B's reconciliation sweep sees X running on B while the row belongs to A, so it stops B's copy. That's backwards. B's copy is the one the leader placed and it kept running the whole time A was frozen, while A's copy is working from state that's a minute or more old. And the heartbeat has quietly undone the leader's decision.
Now the re-registration claims each agent through TryClaimAssignmentAsync. It's really GH-4407's item 2, "restoring a claim can overwrite a newer one", applied to the one restore path #4408 left on last-writer-wins. An agent nobody holds gets its row back like before. An agent a peer already took over keeps the peer's row, and the sweep on A then sees another node owning it and stops A's copy, the same way it handles any other orphan. The warning it logs now says how many it restored and how many it left to the nodes that took them over.
Both copies still run until a sweep acts, the same as on main. On a big node that window can actually be longer than before, because now it's A's sweep alone that stops every copy A lost, and it's capped at MaxLocalAgentReconciliationsPerTick, where on main the stops were spread across the peers' sweeps. I didn't stop them right in the re-registration because that runs in the heartbeat loop, and the heartbeat got its own timer in GH-3604 (D1) precisely so slow agent work can't hold it past StaleNodeTimeout. Stopping a few thousand agents in there could get the node ejected all over again.
One case this doesn't cover: if A had a start in flight when it froze, or picks up a stale AssignAgent after it wakes, StartAgentAsync still writes the row through AddAssignmentAsync, which is last-writer-wins, so A can still take the row that way. I left that alone to keep this one small.
About cost, it's now a loop of single claims where it used to be one AssignAgentsAsync call. That's two statements per agent on Postgres and SQLite (and MySQL and Oracle), one batch per agent on SQL Server, and a load plus a create on RavenDB and Cosmos DB, all inside the heartbeat loop. Before, SQL Server and Postgres did the whole set in one round trip. It only runs when the node's row is missing, but on a node with thousands of agents that's thousands of round trips at exactly the moment the node is already behind. That's the question I asked in #6043: if you'd rather have a bulk TryClaimAssignmentsAsync across the stores, I'm happy to add it to this PR, and since 7.0 is still at rc this seems like the time to change the interface.
The 6.0 branch has the same code (HeartBeat.cs:136 on 6.0), so it could go there too if you want a backport.
On tests, node_reregisters_after_ejection gets a new one where the heartbeat finds its row gone and a peer already owns the agent. It checks that the row stays with the peer, and then that after LocalAgentReconciliationThreshold health checks the local copy is stopped without touching the peer's row. It fails on main because the heartbeat calls AssignAgentsAsync, and passes with the change. The existing re-registration test there now expects the claim instead of AssignAgentsAsync, and the re-registration round trip in NodePersistenceCompliance restores through TryClaimAssignmentAsync to match what the controller does. I also had to tell local_agent_reconciliation_sweep that its node row is there. Its substitute returned false from MarkHealthCheckAsync by default, so every tick in that class went through re-registration, and once that path started claiming, never_acts_on_the_synthetic_self_row_injected_for_a_lagging_snapshot picked up a claim that wasn't the sweep's. And I added a few sentences to the hosts-that-pause section of the leadership docs.
I ran CoreTests Runtime.Agents on net10.0 (651 passed, 6 skipped), node_persistence on SQLite, Postgres and SQL Server (18 of 18 on each), and the Postgres leader election suite (25 of 25).