Repository navigation
Conversation
|
@azat could you take a look at this? |
|
Workflow [PR], commit [135ef12] Summary: ❌
AI ReviewSummaryThe final PR state keeps the deterministic factory regression test and the Findings❌ Blockers
Final VerdictChanges requested. The factory-level loop is fixed and the new regression test is solid, but the reverted |
|
@azat since the AI reviewer seems happy, would you mind taking a look? The test failures look unrelated to me, but happy to fix anything if needed. So far this fix works well in our clusters. |
|
This PR looks good to me. @groeneai, please check the CI failure. |
|
@tuanpach both CI reds on 293f422 are pre-existing flakes unrelated to this PR. The diff is entirely client-side ( Stress test (arm_release) - Integration tests (amd_llvm_coverage, 4/5) - whole-batch Neither red is caused by this change. A rerun of both jobs on a rebased head should come back green. |
|
@groeneai, please merge master into this branch. |
|
@tuanpach I'm an outside contributor here, not a ClickHouse maintainer, so I do not have push access to this fork branch ( Who can do the merge:
CI context: the two reds on 293f422 are both pre-existing flakes unrelated to this client-side ( |
|
|
||
| void HedgedConnections::startNewReplica() | ||
| { | ||
| if (cancelled) |
There was a problem hiding this comment.
startNewReplica now clears the queued-offset state after cancellation, but the cancel path still leaves one more way to hang here. If a hedged replica was already in progress, startNewReplica previously added hedged_connections_factory.getFileDescriptor() to the outer HedgedConnections epoll. sendCancel() then calls stopChoosingReplicas() without the matching epoll.remove(...) that disconnect() and disableChangingReplica() do, and Epoll::empty() is only a registration count. Once the last packet receiver/timeout fd is removed, drain() still sees a non-empty outer epoll and blocks forever on this dead nested-epoll fd because the inner factory epoll has already been emptied, so checkNewReplica() never gets a chance to clean it up.
Please remove the factory fd from the outer epoll in sendCancel() before stopChoosingReplicas() when numberOfProcessingReplicas() > 0, the same way the other shutdown paths already do.
| /// destroyed (especially when the system is under pressure). | ||
| if (hedged_connections_factory.hasEventsInProcess()) | ||
| { | ||
| if (hedged_connections_factory.numberOfProcessingReplicas() > 0) |
There was a problem hiding this comment.
numberOfProcessingReplicas is not the number of nested-factory fds currently registered in the outer HedgedConnections epoll; it is requested_connections_count - ready_replicas_count in HedgedConnectionsFactory::numberOfProcessingReplicas. After any earlier failed hedged attempt, a later startNewReplica can return NOT_READY without re-adding the factory fd at src/Client/HedgedConnections.cpp:569, because this count is already > 1 even though there is only one current in-flight replica. From that state, this new epoll.remove(...) hits EPOLL_CTL_DEL on an fd that was never added and throws EPOLL_ERROR during cancellation instead of cleaning up the query.
Please base the outer add/remove logic on the actual replicas_in_process_count (or an explicit “factory fd is registered” flag), and add a regression that exercises failed hedged attempt -> new NOT_READY start -> sendCancel.
|
|
||
| void HedgedConnections::startNewReplica() | ||
| { | ||
| if (cancelled) |
There was a problem hiding this comment.
stopFactory() is also called from disableChangingReplica() once every offset has already committed to a replica, but this guard only suppresses new starts after sendCancel(). A packet fd and its receive-timeout fd can still be ready in the outer epoll at the same time; if getReadyReplicaLocation() processes the packet first, disableChangingReplica() resets the timer and stops the factory, but the already-queued timeout event is still consumed on the next loop iteration. Because cancelled is still false on that path, startNewReplica() will call startNewConnection() again, and the factory is still free to choose an untried replica.
That gives the same offset a second active connection after can_change_replica became false, so both replicas can keep streaming and duplicate the shard's data. Please track that replica selection has been stopped independently of cancelled (or ignore timeout events once can_change_replica is false) so queued timeout events are drained instead of starting another replica after hedging has been disabled.
| /// getReadyReplicaLocation(), this covers a replacement that was already queued/in-flight | ||
| /// before the commit and only became ready afterwards (possible with multiple offsets, | ||
| /// where disableChangingReplica() does not stop the factory until every offset commits). | ||
| if (!offset_states[offset].can_change_replica) |
There was a problem hiding this comment.
This still leaves stale queue entries for offsets that have already committed to a replica. A concrete trace is:
- Offset
Atimes out and queuesA; the only spare replica starts connecting. - Offset
Bthen times out and queuesB, but there is no second free replica. Areceives data, sodisableChangingReplicaflipscan_change_replica[A] = false, butAstays at the front ofoffsets_queue.- When the in-flight connection becomes ready, this branch pops
A, sees!can_change_replica, and disconnects the ready connection instead of handing it toB.
At that point B.next_replica_in_process is still true, but the factory has no events left, so if B later loses its active replica we suppress the SOCKET_TIMEOUT / can fall into the same stuck-drain path again.
Once an offset can no longer hedge, its queued entries need to be removed or skipped, not allowed to consume the next fungible ready connection. A fix would be to keep popping stale offsets until you find one with can_change_replica == true (or purge this offset from offsets_queue in disableChangingReplica, e.g. via per-offset queued counts).
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 81/90 (90.00%) · Uncovered code |
|
Dear @azat, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
|
Dear @azat, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
We (Cloudflare) saw some rare cases of initial queries being stuck forever on some of our clusters. The flamegraphs indicated that they were spending all their time in trying to establish (hedged) connections. Which is weird because all the remote queries were long gone, the root queries were well past their
max_execution_timeand they were basically hanging around until we restarted the server. Looking into some of them with gdb helped identify what I think is a bug and gets fixed by this PR: WhenHedgedConnectionsFactory::stopChoosingReplicas()cancels an in-progress connection establisher, the establisher ends up withis_cancelled=trueand a null result entry. IfstartNewReplica()is later called during the drain loop inRemoteQueryExecutor::finish(),getNextIndex()still considers the cancelled replica eligible (null entry, zero error count).resumeConnectionEstablisher()seesisCancelled()and returnsCANNOT_CHOOSE, but without marking the replica as failed.startNewConnectionImpl()retries the same replica in a tight loop forever. Sincefinish()holdswas_cancelled_mutex,max_execution_timecannot acquire the mutex to cancel the query.I've added a unit test because I found it quite hard to reliably reproduce this with an integration test. I do have an integration test that covers a part of it but not sure if it's worth adding.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix rare cases of hung root queries.