Skip to content

Conversation

@seizethedave
Copy link
Contributor

@seizethedave seizethedave commented Dec 10, 2025

What this PR does

This is a followup from a comment to #12222 from @dimitarvdimitrov:

which also makes me think that these selects should be somehow abstracted away - there are already 4 slightly different versions of that select in this function. idk if you have time to think about this or i should. either way - probably something for a follow-up PR

So this PR makes run() somewhat less complex by reducing the tangled control flow that was previously going on, moving delicately coded resource freeing to separate methods with defer blocks, and so on.

Checklist

  • {n/a} Tests updated.
  • {n/a} Documentation added.
  • {n/a} CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]. If changelog entry is not needed, please add the changelog-not-needed label to the PR.
  • {n/a} about-versioning.md updated with experimental features.

Note

Refactors ConcurrentFetchers.run by extracting fetch handling into handleWant, performAttempt, and isDone, simplifying control flow and channel/defers; minor casHWM param rename.

  • Ingest/Kafka fetcher (pkg/storage/ingest/fetcher.go):
    • Refactor ConcurrentFetchers.run:
      • Delegates per-want processing to new handleWant().
      • Introduces performAttempt() to encapsulate a single fetch attempt (including HWM updates, merge, backoff reset, and result dispatch).
      • Adds isDone() helper for shutdown checks.
      • Uses defer in handleWant to close(w.result) and finish spans; consolidates select/send logic for result delivery and waiting.
    • Minor:
      • Rename function parameter in casHWM to highWatermark for clarity.

Written by Cursor Bugbot for commit 127f700. This will update automatically on new commits. Configure here.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the ConcurrentFetchers.run() method in the Kafka fetch logic by extracting complex inline logic into separate helper methods for improved code organization and readability.

Key Changes:

  • Extracted fetch want handling into handleWant() method
  • Extracted individual fetch attempt logic into performAttempt() method
  • Added isDone() helper method for cleaner shutdown checking
  • Fixed typo in casHWM function parameter name (highWwatermark → highWatermark)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

… 2 users, load averages: 3.31 3.61 3.22

USER       TTY      FROM    LOGIN@  IDLE WHAT
davidgrant console  -      Fri10   5days -
davidgrant s000     -      Fri10       - tmux when retrying.
@seizethedave seizethedave requested a review from Copilot December 11, 2025 00:34
@seizethedave seizethedave changed the title Davidgrant/ingest refact Refactor ConcurrentFetchers.run Dec 11, 2025
@seizethedave seizethedave added the changelog-not-needed PRs that don't need a CHANGELOG.md entry label Dec 11, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if !continueAttempts {
break
}
}
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop may exit without sending any buffered records to the result channel. If the loop condition becomes false (e.g., errBackoff.Ongoing() returns false, isDone() returns true, or w.endOffset <= w.startOffset), any records in bufferedResult will be discarded instead of being sent to w.result. In the original code, bufferedResult was always sent to the channel before checking these conditions again. Consider sending any remaining bufferedResult before the handleWant function exits.

Suggested change
}
}
// Send any remaining bufferedResult before exiting.
if len(bufferedResult.Records) > 0 {
w.result <- bufferedResult
}

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, interesting, I'll think about that.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@seizethedave seizethedave marked this pull request as ready for review December 11, 2025 01:08
@seizethedave seizethedave requested a review from a team as a code owner December 11, 2025 01:08
@seizethedave seizethedave changed the title Refactor ConcurrentFetchers.run ingest storage: Refactor ConcurrentFetchers.run Dec 11, 2025
func (r *ConcurrentFetchers) run(ctx context.Context, wants chan fetchWant, logger log.Logger, highWatermark *atomic.Int64) {
defer r.wg.Done()

errBackoff := backoff.New(ctx, r.fetchBackoffConfig)
Copy link
Contributor Author

@seizethedave seizethedave Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should every want get a fresh backoff?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would different wants get different behaviour from the kafka brokers? i don't think so. if they start getting errors, they slow down faster if they use a single backoff

Copy link
Contributor

@dimitarvdimitrov dimitarvdimitrov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM, this looks valid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-not-needed PRs that don't need a CHANGELOG.md entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants