lucliu1108 commented on code in PR #22640:
URL: https://github.com/apache/kafka/pull/22640#discussion_r3462915813


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/StreamsGroupTopologyDescriptionRequestManager.java:
##########
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.clients.consumer.internals;
+
+import org.apache.kafka.clients.ClientResponse;
+import 
org.apache.kafka.common.message.StreamsGroupTopologyDescriptionUpdateRequestData;
+import org.apache.kafka.common.protocol.Errors;
+import 
org.apache.kafka.common.requests.StreamsGroupTopologyDescriptionUpdateRequest;
+import 
org.apache.kafka.common.requests.StreamsGroupTopologyDescriptionUpdateResponse;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.common.utils.internals.LogContext;
+
+import org.slf4j.Logger;
+
+import java.util.Collections;
+import java.util.Objects;
+
+public class StreamsGroupTopologyDescriptionRequestManager implements 
RequestManager {
+
+    private final Logger logger;
+    private final Time time;
+    private final String groupId;
+    private final StreamsRebalanceData streamsRebalanceData;
+    private final CoordinatorRequestManager coordinatorRequestManager;
+    private final RequestState pushRequestState;
+
+    private long nextPushTimeMs = 0L;
+
+    public StreamsGroupTopologyDescriptionRequestManager(final LogContext 
logContext,
+                                                         final Time time,
+                                                         final long 
retryBackoffMs,
+                                                         final long 
retryBackoffMaxMs,
+                                                         final String groupId,
+                                                         final 
StreamsRebalanceData streamsRebalanceData,
+                                                         final 
CoordinatorRequestManager coordinatorRequestManager) {
+        this.logger = logContext.logger(getClass());
+        this.time = Objects.requireNonNull(time);
+        this.groupId = Objects.requireNonNull(groupId);
+        this.streamsRebalanceData = 
Objects.requireNonNull(streamsRebalanceData);
+        this.coordinatorRequestManager = 
Objects.requireNonNull(coordinatorRequestManager);
+        this.pushRequestState = new RequestState(
+            logContext,
+            
StreamsGroupTopologyDescriptionRequestManager.class.getSimpleName(),
+            retryBackoffMs,
+            retryBackoffMaxMs);
+    }
+
+    @Override
+    public NetworkClientDelegate.PollResult poll(final long currentTimeMs) {
+        if (!shouldSendTopologyDescriptionUpdate(currentTimeMs)) {
+            return NetworkClientDelegate.PollResult.EMPTY;
+        }
+
+        final StreamsGroupTopologyDescriptionUpdateRequestData data = new 
StreamsGroupTopologyDescriptionUpdateRequestData()
+            .setGroupId(groupId)
+            .setMemberId(streamsRebalanceData.memberId())
+            .setTopologyEpoch(streamsRebalanceData.topologyEpoch())
+            
.setTopologyDescription(streamsRebalanceData.wireTopologyDescription());
+
+        final NetworkClientDelegate.UnsentRequest unsent = new 
NetworkClientDelegate.UnsentRequest(
+            new StreamsGroupTopologyDescriptionUpdateRequest.Builder(data),
+            coordinatorRequestManager.coordinator()
+        );
+        unsent.whenComplete((response, exception) -> onResponse(response, 
exception));
+
+        pushRequestState.onSendAttempt(currentTimeMs);
+        return new 
NetworkClientDelegate.PollResult(Collections.singletonList(unsent));
+    }
+
+    @Override
+    public long maximumTimeToWait(final long currentTimeMs) {
+        if (!streamsRebalanceData.topologyPushRequired()) {
+            return Long.MAX_VALUE;
+        }
+        final long backoffRemainingMs = 
pushRequestState.remainingBackoffMs(currentTimeMs);
+        final long throttleRemainingMs = Math.max(0L, nextPushTimeMs - 
currentTimeMs);
+        final long waitMs = Math.max(backoffRemainingMs, throttleRemainingMs);
+        if (waitMs > 0L) {
+            return waitMs;
+        }
+        return shouldSendTopologyDescriptionUpdate(currentTimeMs) ? 0L : 
Long.MAX_VALUE;
+    }
+
+    private boolean shouldSendTopologyDescriptionUpdate(final long 
currentTimeMs) {
+        if (!pushRequestState.canSendRequest(currentTimeMs) || currentTimeMs < 
nextPushTimeMs) {
+            return false;
+        }
+        if (!streamsRebalanceData.topologyPushRequired() || 
streamsRebalanceData.wireTopologyDescription() == null) {
+            return false;
+        }
+        final String memberId = streamsRebalanceData.memberId();
+        if (memberId == null || memberId.isEmpty()) {
+            return false;
+        }
+        return coordinatorRequestManager.coordinator().isPresent();
+    }
+
+    private void onResponse(final ClientResponse response, final Throwable 
exception) {
+        final long responseTimeMs = time.milliseconds();
+
+        if (exception != null) {
+            pushRequestState.onFailedAttempt(responseTimeMs);
+            logger.warn("Topology description push failed with exception; will 
retry on next poll", exception);
+            return;
+        }

Review Comment:
   Adopt the suggestion. If it is a retriable exception, retry; else don't, 
clears the state and flip back the flag.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to