Skip to content
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

KAFKA-18538: Add Streams membership manager #18551

Merged
merged 11 commits into from
Feb 4, 2025

Conversation

cadonna
Copy link
Member

@cadonna cadonna commented Jan 15, 2025

The Streams membership manager is used client-side in the
background thread of the async consumer. For each member
/consumer, it is responsible for:

  • keeping the member state,
  • keeping assignments for the member,
  • reconciling the assignments of the member -- for example
    when tasks need to be revoked before other tasks are assigned
  • requesting invocations of assignment and revocation callbacks
    by the stream thread.

The Streams membership manager is called by the background thread of
the async consumer, directly in its event loop and from the
Streams group heartbeat request manager. The Streams membership
manager uses the Streams rebalance events processor to request
assignment/revocation callback in the stream thread.

More detailed description of your change,
if necessary. The PR title and PR message become
the squashed commit message, so use a separate
comment to ping reviewers.

Summary of testing strategy (including rationale)
for the feature or bug fix. Unit and/or integration
tests are expected for any behaviour change and
system tests should be considered for larger changes.

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

@cadonna cadonna added streams consumer KIP-1071 PRs related to KIP-1071 labels Jan 15, 2025
@cadonna cadonna requested review from bbejeck, lucasbru and mjsax January 15, 2025 13:51
@github-actions github-actions bot added triage PRs from the community clients labels Jan 15, 2025
@cadonna cadonna removed the triage PRs from the community label Jan 15, 2025
@cadonna cadonna force-pushed the streams_membership_manager branch from c58c298 to 67dd512 Compare January 15, 2025 21:33
Copy link
Member

@lucasbru lucasbru left a comment

Choose a reason for hiding this comment

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

I made a first pass on the membership manager, but this is one massive PR. I am not done yet. But a more general point: In the original PR, we discussed changing what we have to change in the membership manager and then trying to share some code with the AbstractMembershipManager. Is that something we have abandoned / is it too hard to do? This class is a complex beast, I was hoping we can share the state machine, and just plug a different reconciliation logic.

If we are not going to share the code, I think we have to go through the last months of changes on AbstractMembershipManager and see which of the changes apply to our membership manager as well. It looks like a number of changes were made in the state machine since we have forked the membership manager - we need to check whether they need to be adopted by our membership manager.

private static final int PARTITION_0 = 0;
private static final int PARTITION_1 = 1;

private Time time = new MockTime(0);
Copy link
Member

Choose a reason for hiding this comment

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

final

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

private static final int PARTITION_1 = 1;

private Time time = new MockTime(0);
private Metrics metrics = new Metrics(time);
Copy link
Member

Choose a reason for hiding this comment

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

final

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

rejoinedWhileReconciliationInProgress = false;
}

public void onTasksRevokedCallbackCompleted(final StreamsOnTasksRevokedCallbackCompletedEvent event) {
Copy link
Member

Choose a reason for hiding this comment

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

This is not unit tested

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

}

public boolean isLeavingGroup() {
MemberState state = state();
Copy link
Member

Choose a reason for hiding this comment

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

Not sure why we'd use the getter here. Maybe a result of copying?

Copy link
Member Author

Choose a reason for hiding this comment

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

Me neither, I copied this over. I will change it.

}

public boolean shouldHeartbeatNow() {
MemberState state = state();
Copy link
Member

Choose a reason for hiding this comment

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

don't use the getter

}

public void onHeartbeatRequestGenerated() {
MemberState state = state();
Copy link
Member

Choose a reason for hiding this comment

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

don't use the getter

@cadonna cadonna force-pushed the streams_membership_manager branch 2 times, most recently from 8403d02 to 6c65e3f Compare January 22, 2025 08:36
@cadonna cadonna force-pushed the streams_membership_manager branch 2 times, most recently from b0eaf13 to 5e6812f Compare January 29, 2025 16:51
@cadonna
Copy link
Member Author

cadonna commented Jan 29, 2025

@lucasbru I included your feedback. I added javadocs mainly on public methods. Please re-review.

Copy link
Member

@bbejeck bbejeck left a comment

Choose a reason for hiding this comment

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

Thanks for PR @cadonna - I've made a pass over the StreamsMembershipManager I'll do the test in another pass

}

public void registerStateListener(MemberStateListener listener) {
stateUpdatesListeners.add(Objects.requireNonNull(listener, "State updates listener cannot be null"));
Copy link
Member

Choose a reason for hiding this comment

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

Should stateUpdatesListeners be a Set or should we check for possibly adding a duplicate MemberStateListener?

Copy link
Member Author

@cadonna cadonna Jan 30, 2025

Choose a reason for hiding this comment

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

Not allowing duplicate listener makes sense to me. However, I would prefer to check for reference equality instead of using a Set since the latter requires equals() and hashCode() being implemented which is not needed for listener IMO.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a check for duplicates.

stateUpdatesListeners.add(Objects.requireNonNull(listener, "State updates listener cannot be null"));
}

void notifyEpochChange(Optional<Integer> epoch) {
Copy link
Member

Choose a reason for hiding this comment

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

Why the package-private modifier here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

* the interval.
*/
public boolean shouldHeartbeatNow() {
return state == MemberState.ACKNOWLEDGING || state == MemberState.LEAVING || state == MemberState.JOINING;
Copy link
Member

Choose a reason for hiding this comment

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

why heartbeat on leaving? more for my own knowledge

Copy link
Member Author

@cadonna cadonna Jan 30, 2025

Choose a reason for hiding this comment

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

If a member leaves the group, it send a heartbeat with the leave epoch to the group coordinator. The leave epoch is -1 for non-static members and -2 for static members. So the group coordinator knows that the member can be safely removed from the group.
A leave group heartbeat request does not need to wait for the expiration of the heartbeat interval, but should be sent immediately.

*/
public void onFenced() {
if (state == MemberState.PREPARE_LEAVING) {
log.info("Member {} with epoch {} got fenced but it is already preparing to leave " +
Copy link
Member

Choose a reason for hiding this comment

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

the other log statements in onFenced are at the debug level, is there reason this one is info?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question! I actually do not know. I copied this over from the AbstractMembershipManager without noticing this. I am going to change the level to debug.

@cadonna cadonna force-pushed the streams_membership_manager branch from 5e6812f to 8dab352 Compare January 30, 2025 07:37
Copy link
Member

@lucasbru lucasbru left a comment

Choose a reason for hiding this comment

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

Getting there! I made a deeper pass on the main class. I did not look into the unit tests too closely, since Bill intended to do this.

* If the member is already part of the group, this will only ensure that the updated subscription
* is included in the next heartbeat request.
* <p/>
* Note that list of topics of the subscription is taken from the shared subscription state.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Note that list of topics of the subscription is taken from the shared subscription state.
* Note that the list of topics in the subscription is taken from the shared subscription state.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

* <ul>
* <li>Keeping member state</li>
* <li>Keeping assignment for the member</li>
* <li>Reconciling assignment, for example if tasks need to be revoked before other tasks can be assigned</li>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* <li>Reconciling assignment, for example if tasks need to be revoked before other tasks can be assigned</li>
* <li>Reconciling assignment, for example, if tasks need to be revoked before other tasks can be assigned</li>

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

private static final String MEMBER_ID = "test-member-1";
private static final int MEMBER_EPOCH = 1;

private static final String SUB_TOPOLOGY_ID_0 = "subtopology-0";
Copy link
Member

Choose a reason for hiding this comment

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

If the capitalization is subtopologyId, then this should be SUBTOPOLOGY_ID.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, right!

*/
private void transitionTo(MemberState nextState) {
if (!state.equals(nextState) && !nextState.getPreviousValidStates().contains(state)) {
throw new IllegalStateException(String.format("Invalid state transition from %s to %s",
Copy link
Member

Choose a reason for hiding this comment

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

Should we test detection of invalid state transitions?

Copy link
Member Author

Choose a reason for hiding this comment

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

I do not understand why this method is not part of the MemberState class. Then it would also be easy to test the transitions.

Copy link
Member

Choose a reason for hiding this comment

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

We are free to change this, if we want

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed!

Let me try that in a separate PR where I can also update the other membership managers.

* Notify when the poll timer expired.
*/
public void onPollTimerExpired() {
transitionToSendingLeaveGroup(true);
Copy link
Member

Choose a reason for hiding this comment

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

Should we test what happens in LEAVING and UNSUBSCRIBED state?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean FATAL and UNSUBSCRIBED?

Copy link
Member

Choose a reason for hiding this comment

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

Yes

"current assignment {}. Nothing to reconcile.",
targetAssignment, currentAssignment);
// Make sure we transition the member back to STABLE if it was RECONCILING (ex.
// member was RECONCILING unresolved assignments that were just removed by the
Copy link
Member

Choose a reason for hiding this comment

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

What does it mean for an assignment to be unresolved if we do not use UUIDs?

copy & paste error?

You may want to replace this comment by a test.

Copy link
Member Author

Choose a reason for hiding this comment

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

I replaced the comment with a test.
It is not clear to me how a member can be in JOINING with a reconciled assignment. When the member starts up and joins it does not a reconciled assignment with the assignment that comes in with onHeartbeatSuccess(). If the member rejoins after being stale because it exceeded the poll time out the current task assignment is cleared. Thus, the assignment cannot be reconciled with the assignment that comes in with onHeartbeatSuccess().

public class StreamsMembershipManager implements RequestManager {

/**
* A data structure to represent the current task assignment, and current target task assignment of a member in a
Copy link
Member

Choose a reason for hiding this comment

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

You renamed current target task assignment to just target task assignment, so then you should probably update this comment as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

@Override
public NetworkClientDelegate.PollResult poll(long currentTimeMs) {
if (state == MemberState.RECONCILING) {
maybeReconcile();
Copy link
Member

Choose a reason for hiding this comment

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

I moved the reconciliation to poll in the consumer membership manager back then, because we may receive new metadata for resolving topic UUIDs. Does it still make sense to reconcile in poll?

Copy link
Member Author

@cadonna cadonna Jan 31, 2025

Choose a reason for hiding this comment

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

I would like to postpone this broader discussion after merging this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Okay

@cadonna cadonna force-pushed the streams_membership_manager branch from 295686b to 5b2a196 Compare January 31, 2025 17:05
Copy link
Member

@lucasbru lucasbru left a comment

Choose a reason for hiding this comment

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

Thanks for the updates @cadonna. Just nits. Any fixes to port from the last changes to consumer/abstract membership manager?

import java.util.stream.Stream;

/**
* Tracks state the state of a single member in relationship to a group:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Tracks state the state of a single member in relationship to a group:
* Tracks the state of a single member in relationship to a group:

private final SubscriptionState subscriptionState;

/**
* Current state of this member as part of the consumer group, as defined in {@link MemberState}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Current state of this member as part of the consumer group, as defined in {@link MemberState}
* Current state of this member as part of the consumer group, as defined in {@link MemberState}.

*/
private void transitionTo(MemberState nextState) {
if (!state.equals(nextState) && !nextState.getPreviousValidStates().contains(state)) {
throw new IllegalStateException(String.format("Invalid state transition from %s to %s",
Copy link
Member

Choose a reason for hiding this comment

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

We are free to change this, if we want

* Notify when the poll timer expired.
*/
public void onPollTimerExpired() {
transitionToSendingLeaveGroup(true);
Copy link
Member

Choose a reason for hiding this comment

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

Yes

@Override
public NetworkClientDelegate.PollResult poll(long currentTimeMs) {
if (state == MemberState.RECONCILING) {
maybeReconcile();
Copy link
Member

Choose a reason for hiding this comment

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

Okay

@cadonna cadonna force-pushed the streams_membership_manager branch from 5b2a196 to 3fde353 Compare February 3, 2025 21:01
@cadonna
Copy link
Member Author

cadonna commented Feb 3, 2025

Thanks for the updates @cadonna. Just nits. Any fixes to port from the last changes to consumer/abstract membership manager?

Thanks a lot for this comment, @lucasbru ! I indeed found two fixes that I ported over to the streams membership manager. Do you want to re-review?

Copy link
Member

@bbejeck bbejeck left a comment

Choose a reason for hiding this comment

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

Thanks for PR @cadonna - I made a pass over the test - there are a couple of minor spots not covered, but not enough to block this PR, if you agree with my comments you could do it in a follow-up PR

* active call to {@link org.apache.kafka.clients.consumer.KafkaConsumer#poll(java.time.Duration)}"
*/
public void onConsumerPoll() {
if (subscriptionUpdated.compareAndSet(true, false) && state == MemberState.UNSUBSCRIBED) {
Copy link
Member

Choose a reason for hiding this comment

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

In the test this part always evaluates the true case, never false, probably would be good to add a case for the false case

Copy link
Member Author

Choose a reason for hiding this comment

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

I added some tests.

if (shouldAbort) {
String reason = rejoinedWhileReconciliationInProgress ?
"the member has re-joined the group" :
Copy link
Member

Choose a reason for hiding this comment

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

This branch is never tested

Copy link
Member Author

Choose a reason for hiding this comment

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

I added some tests. Admittedly, the number and complexity of the tests tell me that this class needs some refactoring in the future.

@cadonna cadonna force-pushed the streams_membership_manager branch from 3fde353 to 6288bd2 Compare February 4, 2025 07:24
@lucasbru
Copy link
Member

lucasbru commented Feb 4, 2025

Thanks a lot for this comment, @lucasbru ! I indeed found two fixes that I ported over to the streams membership manager. Do you want to re-review?

I went through the latest commits. I think there were several unrelated changes squashed, at least I could not see the coherence in the commits. But looks good to go from my side.

The Streams membership manager is used client-side in the
background thread of the async consumer. For each member
/consumer, it is responsible for:
* keeping the member state,
* keeping assignments for the member,
* reconciling the assignments of the member -- for example
when tasks need to be revoked before other tasks are assigned
* requesting invocations of assignment and revocation callbacks
by the stream thread.

The Streams membership manager is called by the background thread of
the async consumer, directly in its event loop and from the
 Streams group heartbeat request manager. The Streams membership
manager uses the Streams rebalance events processor to request
assignment/revocation callback in the stream thread.
@cadonna cadonna force-pushed the streams_membership_manager branch from 6288bd2 to 82c9dcf Compare February 4, 2025 11:18
@cadonna cadonna merged commit b998189 into apache:trunk Feb 4, 2025
9 checks passed
pdruley pushed a commit to pdruley/kafka that referenced this pull request Feb 12, 2025
The Streams membership manager is used client-side in the
background thread of the async consumer. For each member
/consumer, it is responsible for:
* keeping the member state,
* keeping assignments for the member,
* reconciling the assignments of the member -- for example
when tasks need to be revoked before other tasks are assigned
* requesting invocations of assignment and revocation callbacks
by the stream thread.

The Streams membership manager is called by the background thread of
the async consumer, directly in its event loop and from the
 Streams group heartbeat request manager. The Streams membership
manager uses the Streams rebalance events processor to request
assignment/revocation callback in the stream thread.

Reviewers: Lucas Brutschy <[email protected]>, Bill Bejeck <[email protected]>
manoj-mathivanan pushed a commit to manoj-mathivanan/kafka that referenced this pull request Feb 19, 2025
The Streams membership manager is used client-side in the
background thread of the async consumer. For each member
/consumer, it is responsible for:
* keeping the member state,
* keeping assignments for the member,
* reconciling the assignments of the member -- for example
when tasks need to be revoked before other tasks are assigned
* requesting invocations of assignment and revocation callbacks
by the stream thread.

The Streams membership manager is called by the background thread of
the async consumer, directly in its event loop and from the
 Streams group heartbeat request manager. The Streams membership
manager uses the Streams rebalance events processor to request
assignment/revocation callback in the stream thread.

Reviewers: Lucas Brutschy <[email protected]>, Bill Bejeck <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants