hachikuji commented on a change in pull request #9839:
URL: https://github.com/apache/kafka/pull/9839#discussion_r553640245



##########
File path: core/src/main/scala/kafka/raft/RaftManager.scala
##########
@@ -0,0 +1,279 @@
+/*
+ * 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 kafka.raft
+
+import java.io.File
+import java.nio.file.Files
+import java.util.Random
+import java.util.concurrent.CompletableFuture
+
+import kafka.log.{Log, LogConfig, LogManager}
+import kafka.raft.KafkaRaftManager.RaftIoThread
+import kafka.server.{BrokerTopicStats, KafkaConfig, KafkaServer, 
LogDirFailureChannel}
+import kafka.utils.timer.SystemTimer
+import kafka.utils.{KafkaScheduler, Logging, ShutdownableThread}
+import org.apache.kafka.clients.{ApiVersions, ClientDnsLookup, 
ManualMetadataUpdater, NetworkClient}
+import org.apache.kafka.common.TopicPartition
+import org.apache.kafka.common.metrics.Metrics
+import org.apache.kafka.common.network.{ChannelBuilders, NetworkReceive, 
Selectable, Selector}
+import org.apache.kafka.common.protocol.ApiMessage
+import org.apache.kafka.common.requests.RequestHeader
+import org.apache.kafka.common.security.JaasContext
+import org.apache.kafka.common.utils.{LogContext, Time}
+import org.apache.kafka.raft.{FileBasedStateStore, KafkaRaftClient, 
QuorumState, RaftClient, RaftConfig, RaftRequest, RecordSerde}
+
+import scala.jdk.CollectionConverters._
+
+object KafkaRaftManager {
+  class RaftIoThread(
+    client: KafkaRaftClient[_]
+  ) extends ShutdownableThread(
+    name = "raft-io-thread",
+    isInterruptible = false
+  ) {
+    override def doWork(): Unit = {
+      client.poll()
+    }
+
+    override def initiateShutdown(): Boolean = {
+      if (super.initiateShutdown()) {
+        client.shutdown(5000).whenComplete { (_, exception) =>
+          if (exception != null) {
+            error("Graceful shutdown of RaftClient failed", exception)
+          } else {
+            info("Completed graceful shutdown of RaftClient")
+          }
+        }
+        true
+      } else {
+        false
+      }
+    }
+
+    override def isRunning: Boolean = {
+      client.isRunning
+    }
+  }
+
+  private def createLogDirectory(logDir: File, logDirName: String): File = {
+    val logDirPath = logDir.getAbsolutePath
+    val dir = new File(logDirPath, logDirName)
+    Files.createDirectories(dir.toPath)
+    dir
+  }
+}
+
+trait RaftManager[T] {
+  def handleRequest(
+    header: RequestHeader,
+    request: ApiMessage,
+    createdTimeMs: Long
+  ): CompletableFuture[ApiMessage]
+
+  def register(
+    listener: RaftClient.Listener[T]
+  ): Unit
+
+  def scheduleAppend(
+    epoch: Int,
+    records: Seq[T]
+  ): Option[Long]
+}
+
+class KafkaRaftManager[T](
+  nodeId: Int,
+  baseLogDir: String,
+  recordSerde: RecordSerde[T],
+  topicPartition: TopicPartition,
+  config: KafkaConfig,
+  time: Time,
+  metrics: Metrics
+) extends RaftManager[T] with Logging {
+
+  private val raftConfig = new RaftConfig(config.originals)
+  private val logContext = new LogContext(s"[RaftManager $nodeId] ")
+  this.logIdent = logContext.logPrefix()
+
+  private val scheduler = new KafkaScheduler(threads = 1)
+  scheduler.startup()
+
+  private val dataDir = createDataDir()
+  private val metadataLog = buildMetadataLog()
+  private val netChannel = buildNetworkChannel()
+  private val raftClient = buildRaftClient()
+  private val raftIoThread = new RaftIoThread(raftClient)
+
+  def startup(): Unit = {
+    netChannel.start()
+    raftClient.initialize()
+    raftIoThread.start()
+  }
+
+  def shutdown(): Unit = {
+    raftIoThread.shutdown()
+    scheduler.shutdown()
+    netChannel.close()
+    metadataLog.close()
+  }
+
+  override def register(
+    listener: RaftClient.Listener[T]
+  ): Unit = {
+    raftClient.register(listener)
+  }
+
+  override def scheduleAppend(
+    epoch: Int,
+    records: Seq[T]
+  ): Option[Long] = {
+    val offset: java.lang.Long = raftClient.scheduleAppend(epoch, 
records.asJava)

Review comment:
       I started to implement this, but found it was taking me a bit afield. If 
you don't mind, let's address this in 
https://issues.apache.org/jira/browse/KAFKA-12158.




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