Skip to content

perf(messagequeue): lease-aware partition acquisition - #529

Merged
behinddwalls merged 3 commits into
mainfrom
preetam/mq-lease-aware-acquire
Aug 6, 2026
Merged

perf(messagequeue): lease-aware partition acquisition#529
behinddwalls merged 3 commits into
mainfrom
preetam/mq-lease-aware-acquire

Conversation

@behinddwalls

@behinddwalls behinddwalls commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

Why?

Every discovery tick, DiscoverAndAcquirePartitions attempts TryAcquireLease on every discovered partition — including partitions this subscriber already owns and partitions validly leased by peers. Each attempt is an INSERT … ON DUPLICATE KEY UPDATE (a write that takes a row lock even when the steal condition fails) plus an ownership SELECT. At steady state that is ~2 × P×(N−1)/N queries per subscriber per tick of contended writes that can never win, concentrated on the same lease rows from every node in the group.

What?

partitionLeaseStore gains GetAllLeases — a single PK-prefix read of every lease row for (consumer_group, topic). DiscoverAndAcquirePartitions uses it to classify discovered partitions: self-owned rows count toward the cap but are not re-probed (renewal is the lease tick's job), rows validly held by another subscriber are skipped without any write, and only unleased or stale (stealable) partitions are attempted. The classification is advisory — TryAcquireLease remains the atomic arbiter, so races between the read and the write resolve exactly as before. The read also replaces the previous pre-loop GetLeasedPartitions query. A lease_aware_skipped counter records how many probes each tick avoids.

Test Plan

  • ✅ Full Docker integration suite — rebalance, crash-recovery (stale-lease steal), and orphan-sweep tests exercise the new classification against real MySQL.

Issues

## Summary

### Why?

Partition discovery, lease acquisition, and worker reconciliation all run on the message-poll ticker (`PollIntervalMs`, 100ms default). Discovery drives topic-wide work every tick — a `DISTINCT partition_key` scan, an active-subscriber read, self-lease reads, and lease-acquisition probes — whose query volume multiplies with subscribers × topics at 10x/sec, even though its outcome only changes when membership or the partition set changes. Message polling needs 100ms latency; discovery does not.

### What?

New `PartitionDiscoveryIntervalMs` subscription config (default 1s) drives the supervisor's discovery ticker; `PollIntervalMs` continues to drive per-partition message polling unchanged. This cuts discovery-driven query volume ~10x at the default settings. The accepted trade-off is that a brand-new partition's first message now waits up to the discovery interval (1s) before a worker picks it up; messages on already-owned partitions are unaffected. Integration test configs pin discovery to 100ms so lease-handoff and rebalance convergence assertions stay fast.

## Test Plan

- ✅ Full Docker integration suite (`bazel test //test/integration/extension/messagequeue/...`) — discovery-latency-sensitive tests (empty-topic wake-up, rebalance convergence, crash recovery) pass with the new cadence.
## Summary

### Why?

Every discovery tick, `DiscoverAndAcquirePartitions` attempts `TryAcquireLease` on every discovered partition — including partitions this subscriber already owns and partitions validly leased by peers. Each attempt is an `INSERT … ON DUPLICATE KEY UPDATE` (a write that takes a row lock even when the steal condition fails) plus an ownership SELECT. At steady state that is ~2 × P×(N−1)/N queries per subscriber per tick of contended writes that can never win, concentrated on the same lease rows from every node in the group.

### What?

`partitionLeaseStore` gains `GetAllLeases` — a single PK-prefix read of every lease row for `(consumer_group, topic)`. `DiscoverAndAcquirePartitions` uses it to classify discovered partitions: self-owned rows count toward the cap but are not re-probed (renewal is the lease tick's job), rows validly held by another subscriber are skipped without any write, and only unleased or stale (stealable) partitions are attempted. The classification is advisory — `TryAcquireLease` remains the atomic arbiter, so races between the read and the write resolve exactly as before. The read also replaces the previous pre-loop `GetLeasedPartitions` query. A `lease_aware_skipped` counter records how many probes each tick avoids.

## Test Plan

- ✅ Full Docker integration suite — rebalance, crash-recovery (stale-lease steal), and orphan-sweep tests exercise the new classification against real MySQL.
@behinddwalls
behinddwalls force-pushed the preetam/mq-discovery-interval branch from 7240e79 to 66537f6 Compare August 6, 2026 16:58
@behinddwalls
behinddwalls force-pushed the preetam/mq-lease-aware-acquire branch from 474e9c9 to d27bb4d Compare August 6, 2026 16:58
@behinddwalls
behinddwalls marked this pull request as ready for review August 6, 2026 16:59
@behinddwalls
behinddwalls requested review from a team and sbalabanov as code owners August 6, 2026 16:59
@behinddwalls
behinddwalls changed the base branch from preetam/mq-discovery-interval to main August 6, 2026 20:36
@behinddwalls
behinddwalls merged commit 1a89958 into main Aug 6, 2026
3 checks passed
@behinddwalls
behinddwalls deleted the preetam/mq-lease-aware-acquire branch August 7, 2026 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant