Skip to content

fix(sink-worker): rebalance - #4515

Merged
tothandras merged 1 commit into
mainfrom
fix/rebalance
Jun 12, 2026
Merged

tothandras merged 1 commit into
mainfrom
fix/rebalance

Conversation

@tothandras

@tothandras tothandras commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • Bug Fixes
    • Optimized Kafka consumer subscription handling during periodic updates to prevent unnecessary rebalances, improving stability and performance when topic subscriptions remain unchanged.

@tothandras
tothandras requested a review from a team as a code owner June 12, 2026 10:28
@tothandras tothandras added the release-note/bug-fix Release note: Bug Fixes label Jun 12, 2026
@coderabbitai

coderabbitai Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: da839d0d-e54f-415a-8867-0f0fab5fff55

📥 Commits

Reviewing files that changed from the base of the PR and between c345f9c and ff54338.

📒 Files selected for processing (1)
  • openmeter/sink/sink.go

📝 Walkthrough

Walkthrough

The PR optimizes Kafka consumer subscription handling by avoiding unnecessary rebalances. When the sink periodically refreshes namespaces, it now compares the sorted topic set against the current subscription and skips re-subscribing if nothing has changed.

Changes

Subscription Rebalance Optimization

Layer / File(s) Summary
Conditional subscription update with early return
openmeter/sink/sink.go
Adds slices import and updates updateTopicSubscription to sort matching topic names, compare them against current subscription, and return early without calling SubscribeTopics if the topic set is unchanged.

🎯 3 (Moderate) | ⏱️ ~15 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title is vague and doesn't clearly convey the main change. 'rebalance' is too generic—it doesn't explain what the fix actually does or why it matters. Consider a more descriptive title like 'fix(sink-worker): skip rebalance when topic subscription unchanged' to clarify the optimization.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/rebalance

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps

greptile-apps Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes perpetual consumer-group rebalancing in the sink-worker by skipping SubscribeTopics when the current Kafka subscription already matches the desired topic list. Previously, calling SubscribeTopics on every periodic NamespaceRefetch cycle triggered a consumer-group rejoin in librdkafka even when no topics had changed, causing REBALANCE_IN_PROGRESS errors that blocked offset commits and stalled consumption.

  • Adds a Subscription() call before SubscribeTopics and sorts both slices for a stable comparison using slices.Equal / slices.Sorted.
  • Returns early (no-op) when the sorted topic list is identical to the current subscription, preventing unnecessary rebalances across all sink-worker replicas.

Confidence Score: 5/5

Safe to merge — the change is a targeted guard that short-circuits only when the topic set is provably identical, leaving all other paths untouched.

The fix is small and self-contained: it adds a Subscription() call, sorts both slices for a stable comparison, and returns early on a match. The slices.Values / slices.Sorted pair (Go 1.23+) is compatible with the project's Go 1.25 toolchain. No pre-existing code paths are altered, and the sorted topics slice passed to SubscribeTopics on an actual change is semantically equivalent to the unsorted version Kafka accepted before.

No files require special attention.

Important Files Changed

Filename Overview
openmeter/sink/sink.go Adds an early-exit guard in updateTopicSubscription that compares sorted current and desired topic lists and skips SubscribeTopics when unchanged; logic and imports are correct.

Sequence Diagram

sequenceDiagram
    participant T as Ticker (NamespaceRefetch)
    participant S as updateTopicSubscription
    participant K as Kafka Broker (GetMetadata)
    participant C as Consumer (librdkafka)

    T->>S: periodic tick
    S->>K: GetMetadata()
    K-->>S: all topics
    S->>S: filter by namespaceTopicRegexp → topics[]
    S->>C: Subscription() → current[]
    S->>S: "sort(topics) == sort(current)?"
    alt unchanged
        S-->>T: return nil (no rebalance)
    else changed
        S->>C: SubscribeTopics(topics, rebalance)
        C-->>S: ok
        Note over C: Consumer group rejoin triggered
        S-->>T: return nil
    end
Loading

Reviews (1): Last reviewed commit: "fix(sink-worker): rebalance" | Re-trigger Greptile

@tothandras
tothandras enabled auto-merge (squash) June 12, 2026 10:33
@tothandras
tothandras merged commit fccacdb into main Jun 12, 2026
24 of 27 checks passed
@tothandras
tothandras deleted the fix/rebalance branch June 12, 2026 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants