-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(kafka-reader): Separates kafka constructs for better separation of concerns & reuse. #14982
Conversation
e279e79
to
236fd84
Compare
236fd84
to
5813b62
Compare
continue | ||
} | ||
|
||
if err := c.Commit(ctx, currOffset); err == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we log the error when it's non-nil?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks for doing this! It is definitely an improvement. I've left a few comments to address but I'd be happy to approve once they're looked at
lastCommittedOffset = int64(KafkaStartOffset) | ||
} | ||
|
||
if lastCommittedOffset > 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just spotted this now, but this should be >=
as 0 is a valid offset
} | ||
lastProducedOffset = lastProducedOffset - 1 // Kafka returns the next empty offset so we must subtract 1 to get the oldest written offset. | ||
|
||
level.Debug(logger).Log("msg", "fetched latest offset information", "partition_start_offset", partitionStartOffset, "last_produced_offset", lastProducedOffset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add lastCommittedOffset to this log line? We can then understand all the decisions made beyond this point as we frequently need to know what happened here.
// Ensure there are some records to consume. For example, if the partition has been inactive for a long | ||
// time and all its records have been deleted, the partition start offset may be > 0 but there are no | ||
// records to actually consume. | ||
if partitionStartOffset > lastProducedOffset { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this should also be >=
. What happens if a partition is new and has had a single record written to it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing this causes TestPartitionReader_ProcessCommits
to fail. I'd like to leave this debugging for a future PR and let this PR simply refactor the existing structure, but not the logic.
return | ||
} | ||
// Commit has internal timeouts, so this call shouldn't block for too long | ||
_ = c.Commit(context.Background(), offset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be useful to add an Info log here saying what the final committed offset was.
I know we have a debug line in Commit() but it is useful to know the final commit at shutdown, even if debug logging is not enabled.
"go.uber.org/atomic" | ||
) | ||
|
||
type refactoredPartitionCommitter struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a big fan of "refactored..." as a name!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it will be removed when the old code is deleted? @owen-d
level.Debug(r.logger).Log( | ||
"msg", "malformed response, setting to start offset", | ||
) | ||
return int64(KafkaStartOffset), nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like the other failures cases in this method, can we also return error here and let the caller decide on the offset?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with you, but my goal here is to refactor the existing implementation, not change it's logic. This is a good followup PR though.
Name: "loki_ingest_storage_reader_phase", | ||
Help: "The current phase of the consumer.", | ||
}, []string{"phase"}), | ||
receiveDelay: promauto.With(r).NewHistogramVec(prometheus.HistogramOpts{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not used in the service, should we remove it?
7e0604d
to
bf8dbb8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any issues with the refactor, and I'm happy to delay any logic / logging modifications to a future PR, so approving to unblock!
Refactor Kafka Reader and Partition Committer
This PR refactors the Kafka ingestion pipeline components to achieve better separation of concerns and cleaner interfaces. The changes split the existing
Reader
into three distinct components:ReaderIfc
that handles pure Kafka interactionspartitionCommitter
that manages async offset commits (already existed, but I've refactored it to use the interface)ReaderService
that coordinates the overall lifecycleKey Changes
Note: As of now, this just adds 3 new files with refactored versions -- if we're happy I'll replace the existing ones, but the current structure mirrors how I developed against the prior versions.
Disclaimer: I haven't hooked this up to testware yet; if we're happy with this direction, I'll do that next.edit: done.New Reader Interface
Created a focused interface for Kafka operations:
Improved Committer Design
Service Lifecycle
Benefits