qdrant icon indicating copy to clipboard operation
qdrant copied to clipboard

Retire copy-on-write flush dependencies when a segment changes copy-on-write role

Open abhinav-phi opened this issue 2 months ago • 4 comments

Summary

Copy-on-write flush dependencies are keyed by segment id and are only ever retired by operation version. But a segment id changes its copy-on-write role in place while the optimizer proxies it and unwraps it again, so an edge recorded when an id was a copy-on-write destination outlives the role change and keeps constraining the flush order long after its premise is gone. Two ids that each played destination-then-source in opposite order end up recorded as depending on each other, and the flush ordering — a topological sort — has no valid order for that.

This is the root cause behind #10789 and #10662, and it is what #10859 works around rather than fixes. That PR makes the topology acyclic by refusing the cycle-closing edge and protecting the affected source copies; this issue is about removing the need for those compensations.

The mechanism

SegmentHolder::flush_dependency is a TopoSort<SegmentId, SeqNumberType>. An edge means "element depends on depends_on", i.e. "depends_on must be flushed before element".

Its only writer is apply_points_with_conditional_move, which records add_dependency(source_id, cow_destination_id, op_num) for each copy-on-write move, with the standing comment that the destination must be persisted before the source drops its pre-image. Edges are retired only by version — flush_all calls retain(|_, _, version| *version > cleanup_up_to). Nothing retires an edge because a segment's role changed.

A segment id does change role in place, because SegmentHolder::replace removes and re-adds a segment under the same id and reclassifies it by is_appendable():

  • optimize_segment does replace(idx, proxy). ProxySegment::is_appendable() is hard-coded false (lib/shard/src/proxy_segment/segment_entry.rs), so an appendable id moves out of appendable_segments and becomes a copy-on-write source.
  • unproxy_segments does replace(proxy_id, wrapped) in its second phase, moving that same id back into appendable_segments if the wrapped segment was appendable, so it can be a copy-on-write destination again.

So one id is a destination in one epoch and a source in the next, while the edge recorded in the first epoch survives. A restart is only one way a proxy gets unwrapped: optimize_segment calls unwrap_proxy on all error kinds, and the snapshot paths unproxy too. That is why both harness and harness_no_restarts report it, and why it is the optimizer running concurrently with updates that matters rather than restarts specifically.

Why the topology goes contradictory

Once a stale edge survives a role flip, it can point the wrong way for the new epoch. Concretely, with the surviving edge 0 depends on 1 and a later move from 1 into 0, the move wants 1 depends on 0 — the exact opposite — and can only have one or the other. #10859 resolves that by refusing the new edge, which keeps the graph a DAG but means the refused move has no ordering guarantee. The surviving edge then orders the flush the wrong way round for that move, which is why the copy has to be preserved in its source segment instead.

Consequences beyond the panic

The panic at lib/shard/src/segment_holder/flush.rs is only the loudest symptom. Three things share the root cause:

  • Debug/CI: the debug_assert! in sort_segment_ids_by_flush_dependency fires on the leftovers, which is the flake in #10789 and #10662.
  • Release: the same cycle is not asserted on; into_unordered_vec returns the leftovers and the caller appends them in arbitrary order, so the flush order for those segments is undecided.
  • Single-segment flush: flush_segment_with_cow_destinations discovers a segment's copy-on-write destinations through cow_flush_destinations, which reads the same dependencies_of. A stale or missing edge means that path cannot order the destinations it is supposed to flush first.

Suggested direction

Retire or re-validate dependencies when a segment's role changes, rather than only when its version is flushed. The premise an edge encodes — "this id is an appendable destination that must be durable before I drop my pre-image" — is exactly what a role change invalidates, so the natural place is the role transition itself, which funnels through SegmentHolder::replace.

The open question is the policy at that point, and that is a maintainer decision rather than something this issue should prescribe: drop the edges naming the flipped id, drop only those where it acted as a destination, re-record them, or version-bump them so they expire on the next flush. Dropping is the only option that removes the contradiction outright, but it hands back a durability guarantee that a later refused move still has to be protected from. Whatever the policy, cow_flush_destinations and sort_segment_ids_by_flush_dependency need to agree on it, since they are the two consumers.

Evidence

Reproduced on unmodified dev. The two-node cycles match both reports verbatim, and longer cycles occur too, so this is not limited to a two-segment shape:

signal value
panic site lib/shard/src/segment_holder/flush.rs:231
thread tokio-rt-worker
two-node topologies [0, 1], [1, 0]
other observed remainders [0, 1, 10], [1, 0, 10], and lists spanning 7-8 segments
soak runs that panicked 2 of 6 completed runs
cycle-panic lines in those runs 2526

The soak was driven through the model_testing binary rather than the test, because model_testing::tests is gated on Linux, with the configuration of the harness cell (one shard, 2000 operations, restart probability 0.002, optimizer enabled) and a fresh seed per iteration:

cargo run --bin model_testing --features service_debug -- --shard-count 1 --op-num 2000 --restart-probability 0.002 --disable-snapshots --max-segment-size-kb 10 --indexing-threshold-kb 5 --id-pool 500 --uuid-id-fraction 0.5 --swarm-interval 2500 --seed <N>

#10859 also carries a deterministic regression test that reproduces the cycle through SegmentHolder::replace alone, which is the entry point any fix would need to go through.

abhinav-phi avatar Sep 30 '26 17:09 abhinav-phi

Related work

While writing this up I found that #10665 ("Fix stale flush dependencies after segment replacement") is already open and targets the same mechanism: it adds TopoSort::remove_element and clears flush dependencies in SegmentHolder::replace, which is where both role flips go (replace(idx, proxy) in optimize.rs and replace(proxy_id, wrapped) in proxy.rs). #10665 fixes #10662, which carries this panic's signature.

So the cause and the fix are already in flight there, and this issue should be read as the remaining design question rather than a fresh report. Two things are not covered by that PR, as far as I can tell:

  • #10665's author narrowed the cleanup to replace() and explicitly deferred swap_new() and permanent-id cleanup, so the policy at the role transition is still open.
  • The refusal path is a structural problem, not only a lifecycle one. Two contradictory orderings cannot both be honoured, so whenever that happens the affected copy has to be preserved instead of deleted. #10859 takes that as a backstop; it is what covers the cases a role-transition policy does not reach.

Whoever picks this up, the useful contribution is probably agreeing on one policy and having both the lifecycle cleanup and the acyclicity guarantee agree with it — cow_flush_destinations and sort_segment_ids_by_flush_dependency are the two consumers that have to be consistent.

Keeping this open for the policy discussion rather than closing it as a duplicate, because #10665 deliberately left part of the scope out.

abhinav-phi avatar Sep 30 '26 18:09 abhinav-phi

I'd be interested in working on this.

From the description, it looks like the first step would be to build on the deterministic SegmentHolder::replace regression case from #10859 and trace the dependency lifecycle across the appendable → proxy/source → appendable role transition.

Before implementing an invalidation policy, I'd like to confirm the intended durability semantics: when an ID changes COW role in SegmentHolder::replace, should dependencies where that ID acted as a destination be retired, or would maintainers prefer the dependencies to be revalidated/reconstructed at the transition?

Happy to investigate and put together a fix with regression coverage once the intended invariant is confirmed.

likhitha281 avatar Oct 01 '26 04:10 likhitha281

Correcting course — CI caught a regression I introduced, and both retention commits are reverted.

rust-tests failed on ubuntu and windows, and not with the cycle panic. It was this:

thread 'model_testing::tests::harness' panicked at lib/collection/src/model_testing/apply/reads.rs:960:5:
assertion `left == right` failed: count(tag=="c") mismatch

apply_count_by_tag compares an exact filtered count against the model, so the shard was returning more points than the model had. The cause is the retention this thread asked for, and the fault in my reasoning for implementing it.

The existing point_is_deferred case keeps the source copy for a specific reason: the destination copy is invisible until indexing, so retaining the visible source leaves exactly one visible copy. That is the only thing making it correct. A refused dependency has the opposite shape — the destination copy is fully visible — so keeping a visible source pre-image leaves the point readable twice. The two cases sit on the same line of code, which is exactly why I conflated them. I checked the "delete path is separate, so retention is safe" question but never checked whether the retained copy is visible, which is the part that matters here.

So the branch is back to the single acyclicity commit. A refused edge now falls back to dev's existing behaviour of deleting the source copy.

To be clear about what that leaves: the refused move still has no ordering guarantee, so a crash between the two flushes can drop it and WAL replay cannot rebuild it once the source pre-image is gone. That exposure is pre-existing, not introduced here — on dev the same contradictory pair produced a cyclic topology whose leftovers were flushed unordered, which is no safer. The difference is that it is now deterministic and logged rather than nondeterministic and panicking. A data-loss window is still worse than a clean ordering, which is why I would rather not ship a fix that trades one for the other; see below.

This also changes my read on the shape of the real fix. Preventing the refusal is the only approach that closes the window without regressing reads, which is what #10665 does by retiring the stale dependency at the role transition so the edge can always be recorded. The protection-set approach was my attempt to make the refusal survivable, and it does not work for a visible destination. I have updated the PR description to say this plainly and have opened #10877 for the policy question.

I am not going to re-apply the retention. If you still consider it worth having, I would want the duplicate-read problem solved first, and I do not see how without changing what a refused move does rather than how it is recorded.

abhinav-phi avatar Oct 01 '26 18:10 abhinav-phi

@likhitha281 sorry for the slow reply. the answer is in the write up above but i never pointed it at your question directly.

retiring is the right shape, not revalidating or reconstructing. the invariant that decides it is that after the role flip the destination copy has to be the only visible one. i tried the other direction first, keeping a visible source copy so a refused edge stayed readable, and the model testing harness caught it: apply_count_by_tag saw more points than the model had, because a refused dependency already has a fully visible destination, so the extra source copy makes the point readable twice. both retention commits are reverted for that reason.

and the part i should have said plainly, that is already what your own #10665 does. it retires the stale dependency at the transition in SegmentHolder::replace so the edge can always be recorded. you are not duplicating anything here and you do not need an invariant confirmation from me to keep going. what is still genuinely open is only the crash window on a refused move, which is pre-existing on dev rather than something this introduces.

one more thing, #10665 looks quiet since sep 16 from where i sit, and this PR has the same problem. if a maintainer could look at the two together that would move all of this faster than anything more i can write. whatever qdrant prefers on the refused move case is the answer i would follow.

abhinav-phi avatar Oct 09 '26 07:10 abhinav-phi