|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.kafka.clients.consumer.internals; |
| 18 | + |
| 19 | +import org.apache.kafka.clients.ClientResponse; |
| 20 | +import org.apache.kafka.common.errors.RetriableException; |
| 21 | +import org.apache.kafka.common.message.StreamsGroupTopologyDescriptionUpdateRequestData; |
| 22 | +import org.apache.kafka.common.protocol.Errors; |
| 23 | +import org.apache.kafka.common.requests.StreamsGroupTopologyDescriptionUpdateRequest; |
| 24 | +import org.apache.kafka.common.requests.StreamsGroupTopologyDescriptionUpdateResponse; |
| 25 | +import org.apache.kafka.common.utils.Time; |
| 26 | +import org.apache.kafka.common.utils.internals.LogContext; |
| 27 | + |
| 28 | +import org.slf4j.Logger; |
| 29 | + |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +public class StreamsGroupTopologyDescriptionRequestManager implements RequestManager { |
| 34 | + |
| 35 | + private final Logger logger; |
| 36 | + private final Time time; |
| 37 | + private final StreamsMembershipManager membershipManager; |
| 38 | + private final StreamsRebalanceData streamsRebalanceData; |
| 39 | + private final CoordinatorRequestManager coordinatorRequestManager; |
| 40 | + private final RequestState pushRequestState; |
| 41 | + |
| 42 | + private long nextPushTimeMs = 0L; |
| 43 | + |
| 44 | + public StreamsGroupTopologyDescriptionRequestManager(final LogContext logContext, |
| 45 | + final Time time, |
| 46 | + final long retryBackoffMs, |
| 47 | + final long retryBackoffMaxMs, |
| 48 | + final StreamsMembershipManager membershipManager, |
| 49 | + final StreamsRebalanceData streamsRebalanceData, |
| 50 | + final CoordinatorRequestManager coordinatorRequestManager) { |
| 51 | + this.logger = logContext.logger(getClass()); |
| 52 | + this.time = Objects.requireNonNull(time); |
| 53 | + this.membershipManager = Objects.requireNonNull(membershipManager); |
| 54 | + this.streamsRebalanceData = Objects.requireNonNull(streamsRebalanceData); |
| 55 | + this.coordinatorRequestManager = Objects.requireNonNull(coordinatorRequestManager); |
| 56 | + this.pushRequestState = new RequestState( |
| 57 | + logContext, |
| 58 | + StreamsGroupTopologyDescriptionRequestManager.class.getSimpleName(), |
| 59 | + retryBackoffMs, |
| 60 | + retryBackoffMaxMs); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public NetworkClientDelegate.PollResult poll(final long currentTimeMs) { |
| 65 | + if (!shouldSendTopologyDescriptionUpdate(currentTimeMs)) { |
| 66 | + return NetworkClientDelegate.PollResult.EMPTY; |
| 67 | + } |
| 68 | + |
| 69 | + final StreamsGroupTopologyDescriptionUpdateRequestData data = new StreamsGroupTopologyDescriptionUpdateRequestData() |
| 70 | + .setGroupId(membershipManager.groupId()) |
| 71 | + .setMemberId(membershipManager.memberId()) |
| 72 | + .setTopologyEpoch(streamsRebalanceData.topologyEpoch()) |
| 73 | + .setTopologyDescription(streamsRebalanceData.wireTopologyDescription()); |
| 74 | + |
| 75 | + final NetworkClientDelegate.UnsentRequest unsent = new NetworkClientDelegate.UnsentRequest( |
| 76 | + new StreamsGroupTopologyDescriptionUpdateRequest.Builder(data), |
| 77 | + coordinatorRequestManager.coordinator() |
| 78 | + ); |
| 79 | + unsent.whenComplete((response, exception) -> onResponse(response, exception)); |
| 80 | + |
| 81 | + pushRequestState.onSendAttempt(currentTimeMs); |
| 82 | + return new NetworkClientDelegate.PollResult(Collections.singletonList(unsent)); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public long maximumTimeToWait(final long currentTimeMs) { |
| 87 | + if (!streamsRebalanceData.topologyPushRequired()) { |
| 88 | + return Long.MAX_VALUE; |
| 89 | + } |
| 90 | + final long backoffRemainingMs = pushRequestState.remainingBackoffMs(currentTimeMs); |
| 91 | + final long throttleRemainingMs = Math.max(0L, nextPushTimeMs - currentTimeMs); |
| 92 | + final long waitMs = Math.max(backoffRemainingMs, throttleRemainingMs); |
| 93 | + if (waitMs > 0L) { |
| 94 | + return waitMs; |
| 95 | + } |
| 96 | + return shouldSendTopologyDescriptionUpdate(currentTimeMs) ? 0L : Long.MAX_VALUE; |
| 97 | + } |
| 98 | + |
| 99 | + private boolean shouldSendTopologyDescriptionUpdate(final long currentTimeMs) { |
| 100 | + if (!pushRequestState.canSendRequest(currentTimeMs) || currentTimeMs < nextPushTimeMs) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + if (!streamsRebalanceData.topologyPushRequired() || streamsRebalanceData.wireTopologyDescription() == null) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + final String memberId = membershipManager.memberId(); |
| 107 | + if (memberId == null || memberId.isEmpty()) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + return coordinatorRequestManager.coordinator().isPresent(); |
| 111 | + } |
| 112 | + |
| 113 | + private void onResponse(final ClientResponse response, final Throwable exception) { |
| 114 | + final long responseTimeMs = time.milliseconds(); |
| 115 | + |
| 116 | + if (exception != null) { |
| 117 | + if (exception instanceof RetriableException) { |
| 118 | + pushRequestState.onFailedAttempt(responseTimeMs); |
| 119 | + coordinatorRequestManager.handleCoordinatorDisconnect(exception, responseTimeMs); |
| 120 | + logger.warn("Topology description push failed with retriable exception; will retry on next poll", exception); |
| 121 | + } else { |
| 122 | + // Non-retriable exceptions should clear the flag and give up. |
| 123 | + pushRequestState.onSuccessfulAttempt(responseTimeMs); |
| 124 | + streamsRebalanceData.setTopologyPushRequired(false); |
| 125 | + logger.warn("Topology description push failed with non-retriable exception.", exception); |
| 126 | + } |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + final StreamsGroupTopologyDescriptionUpdateResponse body = |
| 131 | + (StreamsGroupTopologyDescriptionUpdateResponse) response.responseBody(); |
| 132 | + final Errors error = Errors.forCode(body.data().errorCode()); |
| 133 | + final String errorMessage = body.data().errorMessage(); |
| 134 | + |
| 135 | + if (body.data().throttleTimeMs() > 0) { |
| 136 | + nextPushTimeMs = responseTimeMs + body.data().throttleTimeMs(); |
| 137 | + } |
| 138 | + |
| 139 | + switch (error) { |
| 140 | + case NONE: |
| 141 | + pushRequestState.onSuccessfulAttempt(responseTimeMs); |
| 142 | + streamsRebalanceData.setTopologyPushRequired(false); |
| 143 | + break; |
| 144 | + |
| 145 | + case NOT_COORDINATOR: |
| 146 | + case COORDINATOR_NOT_AVAILABLE: |
| 147 | + pushRequestState.onFailedAttempt(responseTimeMs); |
| 148 | + logger.info("Coordinator error {} pushing topology description. Will rediscover and retry: {}", error, errorMessage); |
| 149 | + coordinatorRequestManager.markCoordinatorUnknown(errorMessage, responseTimeMs); |
| 150 | + break; |
| 151 | + |
| 152 | + case COORDINATOR_LOAD_IN_PROGRESS: |
| 153 | + pushRequestState.onFailedAttempt(responseTimeMs); |
| 154 | + logger.info("Coordinator is loading; will retry on next poll: {}", errorMessage); |
| 155 | + break; |
| 156 | + |
| 157 | + case UNKNOWN_MEMBER_ID: |
| 158 | + // Member was dropped — clear the flag and let the heartbeat path drive the rejoin. |
| 159 | + // onSuccessfulAttempt resets request state without backoff since no retry follows. |
| 160 | + pushRequestState.onSuccessfulAttempt(responseTimeMs); |
| 161 | + logger.info("Topology description push rejected with UNKNOWN_MEMBER_ID; heartbeat will trigger rejoin: {}", errorMessage); |
| 162 | + streamsRebalanceData.setTopologyPushRequired(false); |
| 163 | + break; |
| 164 | + |
| 165 | + case STREAMS_TOPOLOGY_DESCRIPTION_UPDATE_FAILED: |
| 166 | + case INVALID_REQUEST: |
| 167 | + case UNSUPPORTED_VERSION: |
| 168 | + case GROUP_ID_NOT_FOUND: |
| 169 | + case GROUP_AUTHORIZATION_FAILED: |
| 170 | + default: |
| 171 | + // Use onSuccessfulAttempt because no retry follows, |
| 172 | + // a future push triggered by a later heartbeat should not inherit backoff from this failure. |
| 173 | + // the broker will re-signal via heartbeat if a push is needed again. |
| 174 | + pushRequestState.onSuccessfulAttempt(responseTimeMs); |
| 175 | + logger.warn("Topology description push failed with {}: {}", error, errorMessage); |
| 176 | + streamsRebalanceData.setTopologyPushRequired(false); |
| 177 | + break; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments