hachikuji commented on code in PR #12862:
URL: https://github.com/apache/kafka/pull/12862#discussion_r1038418696


##########
clients/src/main/java/org/apache/kafka/clients/consumer/internals/NetworkClientDelegate.java:
##########
@@ -0,0 +1,260 @@
+/*
+ * 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.ClientRequest;
+import org.apache.kafka.clients.ClientResponse;
+import org.apache.kafka.clients.KafkaClient;
+import org.apache.kafka.clients.RequestCompletionHandler;
+import org.apache.kafka.common.Node;
+import org.apache.kafka.common.errors.DisconnectException;
+import org.apache.kafka.common.errors.TimeoutException;
+import org.apache.kafka.common.errors.WakeupException;
+import org.apache.kafka.common.requests.AbstractRequest;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.common.utils.Timer;
+import org.slf4j.Logger;
+
+import java.io.IOException;
+import java.util.ArrayDeque;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Queue;
+
+/**
+ * A wrapper around the {@link org.apache.kafka.clients.NetworkClient} to 
handle poll and send operations.
+ */
+public class NetworkClientDelegate implements AutoCloseable {
+    private final KafkaClient client;
+    private final Time time;
+    private final Logger log;
+    private boolean wakeup = false;
+    private final Queue<UnsentRequest> unsentRequests;
+
+    public NetworkClientDelegate(
+            final Time time,
+            final LogContext logContext,
+            final KafkaClient client) {
+        this.time = time;
+        this.client = client;
+        this.log = logContext.logger(getClass());
+        this.unsentRequests = new ArrayDeque<>();
+    }
+
+    public List<ClientResponse> poll(Timer timer, boolean disableWakeup) {
+        client.wakeup();
+        if (!disableWakeup) {
+            // trigger wakeups after checking for disconnects so that the 
callbacks will be ready
+            // to be fired on the next call to poll()
+            maybeTriggerWakeup();
+        }
+
+        trySend();
+        return this.client.poll(timer.timeoutMs(), time.milliseconds());
+    }
+
+    private void trySend() {
+        while (unsentRequests.size() > 0) {
+            UnsentRequest unsent = unsentRequests.poll();
+            if (unsent.timer.isExpired()) {
+                // TODO: expired request should be marked
+                unsent.callback.ifPresent(c -> c.onFailure(new 
TimeoutException(
+                        "Failed to send request after " + 
unsent.timer.timeoutMs() + " " + "ms.")));
+                continue;
+            }
+
+            if (!doSend(unsent)) {
+                log.debug("No broker available to send the request: {}", 
unsent);
+                unsent.callback.ifPresent(v -> v.onFailure(
+                        new IllegalThreadStateException("No node available in 
the kafka cluster to send the request")));

Review Comment:
   Use retriable exception. Maybe `NETWORK_EXCEPTION`? Or a custom exception, 
`NoNodeAvailableException` or sth like that.
   
   An alternative is to add to the end of the queue? We don't need to back off. 
Or perhaps we can use a separate queue for requests that have no target node.



-- 
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: jira-unsubscr...@kafka.apache.org

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

Reply via email to