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


##########
raft/src/main/java/org/apache/kafka/raft/KafkaRaftClientDriver.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.common.protocol.ApiMessage;
+import org.apache.kafka.common.requests.RequestHeader;
+import org.apache.kafka.common.utils.LogContext;
+import org.apache.kafka.server.fault.FaultHandler;
+import org.apache.kafka.server.util.ShutdownableThread;
+import org.slf4j.Logger;
+
+import java.util.concurrent.CompletableFuture;
+
+
+/**
+ * A single-threaded driver for {@link KafkaRaftClient}.
+ *
+ * @param <T> See {@link KafkaRaftClient<T>}
+ */
+public class KafkaRaftClientDriver<T> extends ShutdownableThread {
+    private final Logger log;
+    private final KafkaRaftClient<T> client;
+    private final FaultHandler fatalFaultHandler;
+
+    public KafkaRaftClientDriver(
+        KafkaRaftClient<T> client,
+        String threadNamePrefix,
+        FaultHandler fatalFaultHandler,
+        LogContext logContext
+    ) {
+        super(threadNamePrefix + "-io-thread", false);
+        this.client = client;
+        this.fatalFaultHandler = fatalFaultHandler;
+        this.log = logContext.logger(KafkaRaftClientDriver.class);
+    }
+
+    @Override
+    public void doWork() {
+        try {
+            client.poll();
+        } catch (Throwable t) {
+            throw fatalFaultHandler.handleFault("Unexpected error in raft IO 
thread", t);
+        }
+    }
+
+    @Override
+    public boolean initiateShutdown() {
+        if (super.initiateShutdown()) {
+            client.shutdown(5000).whenComplete((na, exception) -> {
+                if (exception != null) {
+                    log.error("Graceful shutdown of RaftClient failed", 
exception);
+                } else {
+                    log.info("Completed graceful shutdown of RaftClient");
+                }
+            });
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * Shutdown the thread. In addition to stopping any utilized threads, this 
will
+     * close the {@link KafkaRaftClient} instance.
+     */
+    @Override
+    public void shutdown() throws InterruptedException {
+        try {
+            super.shutdown();
+        } finally {
+            client.close();

Review Comment:
   I added some comments in the latest commit. Let me know if they address the 
comment.



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