Files
seaweedfs/weed/mq/sub_coordinator
Chris LuandClaude Opus 5.5 25b9278d60 mq: conditional topic.conf saves, bounded topic.conf I/O, and mq.balance moves that save their assignment (#13231)
* mq broker: give up topic.conf filer calls with the caller's context

The topic.conf read and save helpers took no context, so a filer that took
the call and never answered kept the balancer inside topicConfLock for good,
and every ConfigureTopic, lookup repair and EndPartitionGeneration queued
behind it on a plain mutex. reportGenerationEnded's per-attempt deadline
could not bound any of it. The helpers now take a context, the RPC paths pass
their request context, and topicConfLock is a one-slot semaphore whose wait
ends with the caller's context. Calls made while holding it are also bounded
by topicConfTimeout, so a caller without a deadline cannot hold it forever.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: save topic.conf only if it is still what was read

Every topic.conf update is a read-modify-write on the balancer, but the save
was unconditional, so a balancer whose lease ran out between its read and its
save wrote its stale assignments over a successor's newer epoch. topicConfLock
only orders writers within one broker. A failed read in ConfigureTopic could
also recreate an existing topic from scratch.

The read now returns the topic.conf entry, and the save is an ObjectTransaction
requiring that entry unchanged, or the file still absent for a new topic. A
refused save is redone from a fresh read a few times, so a report for an epoch
a successor already ended finds it moved on, and then gives up with
Unavailable for the caller to retry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: save mq.balance moves to topic.conf before assigning them

A balance move only told the target to load the partition and the source to
drain it. It never touched topic.conf, so lookups kept sending publishers to
the source, and since partitions are fenced by epoch the target was asked to
lead at epoch 0 and refused before anything moved.

The balancer now runs the move itself: it saves the target as leader at the
next epoch through the conditional topic.conf save, tells the source about the
new epoch so it retires its copy, flushing what it holds under its own fence
and sealing it, and then assigns the target, which takes the fence at the new
epoch. A follower that becomes the leader swaps places with the old leader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: keep a retiring leader's fence renewed until it seals

A leader told of a newer epoch, as a balance move's source is, retires its
copy by shutting it down and waiting for its last flush before sealing the
fence, but it did not renew the fence meanwhile. A flush outlasting
fenceSealWait left an aged unsealed fence the new leader could take, and the
pending flush then failed its fence check and dropped acknowledged messages.
The fence is now renewed for as long as the flush takes. Loaders retiring the
same copy at once share one renewal, which the first to return stops before
any seal, so no renewal lands on a sealed fence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: save a configure's assignment repair with its update

ConfigureTopic saved the repair of assignments on dead brokers on its own,
then saved the requested schema or retention change against the entry it had
read before that repair. The second save was always refused, so the update
went round a full reread, and a resize first drained brokers for nothing. The
repair is now applied in memory and saved together with the requested change
in one conditional save. A resize replaces the assignments anyway, so it no
longer saves a repair first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: report a balance move the target refused

assignTopicPartitionsToBrokers logged each broker that refused its assignment
and returned nil, so mq.balance reported success for a move whose target never
took the partition, after topic.conf had already moved and the source had
retired its copy. It now returns the refusals, and BalanceTopics passes them
to its caller. The saved move stands: a retry assigns the target again, and
the target also loads the partition on first use. ConfigureTopic keeps
logging a refused assignment of a topic it has saved, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: give up a topic.conf cache miss with the caller's context

getTopicConfFromCache waited for topicConfLock and read the filer under
context.Background(), so a lookup whose caller had already given up stayed
queued behind whatever held the lock and then did up to topicConfTimeout of
filer work for nobody. It now takes the caller's context, which LookupTopicBrokers
and the retention purge pass along and the partition loaders pass as
Background; cache hits are unchanged. A waiter whose context has already ended
no longer takes a free lock either, since a select with both cases ready picks
one at random.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mq broker: require exactly one moved partition in the balance tests

The move tests took the last partition led by the target as the moved
one, so a move that handed both partitions to the target would pass.
They now fail unless exactly one partition moved.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 13:03:20 -08:00
..