dielhennr commented on a change in pull request #9101:
URL: https://github.com/apache/kafka/pull/9101#discussion_r465351991



##########
File path: 
clients/src/main/java/org/apache/kafka/clients/consumer/internals/DynamicConsumerConfig.java
##########
@@ -0,0 +1,168 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.Map;
+
+import org.apache.kafka.clients.ClientResponse;
+import org.apache.kafka.clients.DynamicClientConfigUpdater;
+import org.apache.kafka.clients.GroupRebalanceConfig;
+import org.apache.kafka.common.Node;
+import org.apache.kafka.common.requests.DescribeConfigsRequest;
+import org.apache.kafka.common.requests.DescribeConfigsResponse;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Time;
+import org.slf4j.Logger;
+
+/**
+ * Handles the request and response of a dynamic client configuration update 
for the consumer
+ */
+public class DynamicConsumerConfig extends DynamicClientConfigUpdater {
+    /* Client to use */
+    private ConsumerNetworkClient client;
+
+    /* Configs to update */
+    private GroupRebalanceConfig rebalanceConfig;
+
+    /* Object to synchronize on when response is recieved */
+    Object lock;
+
+    /* Logger to use */
+    private Logger log;
+
+    /* The resource name to use when constructing a DescribeConfigsRequest */
+    private final String clientId;
+
+    /* Dynamic Configs recieved from the previous DescribeConfigsResponse */
+    private Map<String, String> previousDynamicConfigs;
+
+    /* Indicates if we have recieved the initial dynamic configurations */
+    private boolean initialConfigsFetched;
+
+    public DynamicConsumerConfig(ConsumerNetworkClient client, Object lock, 
GroupRebalanceConfig config, Time time, LogContext logContext) {
+        super(time);
+        this.rebalanceConfig = config;
+        this.log = logContext.logger(DynamicConsumerConfig.class);
+        this.client = client;
+        this.lock = lock;
+        this.clientId = rebalanceConfig.clientId;
+        this.previousDynamicConfigs = new HashMap<>();
+        this.initialConfigsFetched = false;
+    }
+    
+    /**
+     * Send a {@link DescribeConfigsRequest} to a node specifically for 
dynamic client configurations
+     *
+     * @return {@link RequestFuture} 
+     */ 
+    public RequestFuture<ClientResponse> maybeFetchInitialConfigs() {
+        if (!initialConfigsFetched) {
+            Node node = null;
+            while (node == null) {
+                node = client.leastLoadedNode();
+            }
+            log.info("Trying to fetch initial dynamic configs before join 
group request");
+            RequestFuture<ClientResponse> configsFuture = client.send(node, 
newRequestBuilder(this.clientId));
+            return configsFuture;
+        }
+        return null;
+    }
+
+    /**
+     * Block for a {@link DescribeConfigsResponse} and process it. Used to 
fetch the initial dynamic configurations synchronously before sending the 
initial
+     * {@link org.apache.kafka.common.requests.JoinGroupRequest}. Since this 
join RPC sends the group member's session timeout
+     * to the group coordinator, we should check if a dynamic configuration 
for session timeout is set before joining.
+     * If we do not do this initial fetch synchronously, then we could 
possibly trigger an unnecessary group rebalance operation by 
+     * sending a second join request after the dynamic configs are recieved 
asynchronously.
+     *
+     * @param responseFuture - future to block on
+     * @return true if responseFuture was blocked on and a response was 
recieved
+     */
+    public boolean maybeWaitForInitialConfigs(RequestFuture<ClientResponse> 
responseFuture) {
+        if (responseFuture != null) {
+            client.poll(responseFuture);
+            if (responseFuture.isDone()) {
+                DescribeConfigsResponse configsResponse = 
(DescribeConfigsResponse) responseFuture.value().responseBody();
+                handleSuccessfulResponse(configsResponse);
+                this.initialConfigsFetched = true;
+                return true;
+            }
+        }
+        return false;

Review comment:
       I used this to test that the RPC was being sent periodically but this 
can be done in a different way... I will make the return type void.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to