jsancio commented on code in PR #14559:
URL: https://github.com/apache/kafka/pull/14559#discussion_r1361238299


##########
raft/src/main/java/org/apache/kafka/raft/KafkaNetworkChannel.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.raft;
+
+import org.apache.kafka.clients.ClientResponse;
+import org.apache.kafka.clients.KafkaClient;
+import org.apache.kafka.common.Node;
+import org.apache.kafka.common.message.BeginQuorumEpochRequestData;
+import org.apache.kafka.common.message.EndQuorumEpochRequestData;
+import org.apache.kafka.common.message.FetchRequestData;
+import org.apache.kafka.common.message.FetchSnapshotRequestData;
+import org.apache.kafka.common.message.VoteRequestData;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.ApiMessage;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.AbstractRequest;
+import org.apache.kafka.common.requests.BeginQuorumEpochRequest;
+import org.apache.kafka.common.requests.EndQuorumEpochRequest;
+import org.apache.kafka.common.requests.FetchRequest;
+import org.apache.kafka.common.requests.FetchSnapshotRequest;
+import org.apache.kafka.common.requests.VoteRequest;
+import org.apache.kafka.common.utils.Time;
+import org.apache.kafka.server.util.InterBrokerSendThread;
+import org.apache.kafka.server.util.RequestAndCompletionHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class KafkaNetworkChannel implements NetworkChannel {
+
+    static class SendThread extends InterBrokerSendThread {
+
+        private Queue<RequestAndCompletionHandler> queue = new 
ConcurrentLinkedQueue<>();
+
+        public SendThread(String name, KafkaClient networkClient, int 
requestTimeoutMs, Time time, boolean isInterruptible) {
+            super(name, networkClient, requestTimeoutMs, time, 
isInterruptible);
+        }
+
+        @Override
+        public Collection<RequestAndCompletionHandler> generateRequests() {
+            List<RequestAndCompletionHandler> list =  new ArrayList<>();
+            while (true) {
+                RequestAndCompletionHandler request = queue.poll();
+                if (request == null) {
+                    return list;
+                } else {
+                    list.add(request);
+                }
+            }
+        }
+
+        public void sendRequest(RequestAndCompletionHandler request) {
+            queue.add(request);
+            wakeup();
+        }
+    }
+
+    private static final Logger log = 
LoggerFactory.getLogger(KafkaNetworkChannel.class);
+
+    private final SendThread requestThread;
+
+    private final AtomicInteger correlationIdCounter = new AtomicInteger(0);
+    private final Map<Integer, Node> endpoints = new HashMap<>();
+
+    public KafkaNetworkChannel(Time time, KafkaClient client, int 
requestTimeoutMs, String threadNamePrefix) {
+        this.requestThread = new SendThread(
+            threadNamePrefix + "-outbound-request-thread",
+            client,
+            requestTimeoutMs,
+            time,
+            false
+        );
+    }
+
+    @Override
+    public int newCorrelationId() {
+        return correlationIdCounter.getAndIncrement();
+    }
+
+    @Override
+    public void send(RaftRequest.Outbound request) {
+        Node node = endpoints.get(request.destinationId());
+        if (node != null) {
+            requestThread.sendRequest(new RequestAndCompletionHandler(
+                request.createdTimeMs,
+                node,
+                buildRequest(request.data),
+                response -> sendDnComplete(request, response)
+            ));
+        } else
+            sendCompleteFuture(request, errorResponse(request.data, 
Errors.BROKER_NOT_AVAILABLE));
+    }
+
+    private void sendCompleteFuture(RaftRequest.Outbound request, ApiMessage 
message) {
+        RaftResponse.Inbound response = new RaftResponse.Inbound(
+                request.correlationId,
+                message,
+                request.destinationId()
+        );
+        request.completion.complete(response);
+    }
+
+    private void sendDnComplete(RaftRequest.Outbound request, ClientResponse 
clientResponse) {

Review Comment:
   Did you mean `sendOnComplete`?



##########
raft/src/main/java/org/apache/kafka/raft/KafkaNetworkChannel.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.raft;

Review Comment:
   No need to do it in this PR but maybe I should move a lot of classes in this 
package to `org.apache.kafka.raft.internals`. once `RaftManager` has been moved 
to the `raft` module.



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