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


##########
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:
   Yes, I debated it. It is nice to have the driver own shutdown since it is in 
control of the poll loop. Given that, it seemed safer for it to also close the 
client once it knows that shutdown is complete.
   
   In a future patch, my thought was to have the driver own creation of the 
client as well. We can replace `RaftManager` with a builder which constructs 
the client in the context of the driver. That would establish clear ownership 
and resolve the issue.



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