You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/01/07 06:13:45 UTC
[GitHub] [kafka] chia7712 commented on a change in pull request #9839: MINOR: Factor `RaftManager` out of `TestRaftServer`
chia7712 commented on a change in pull request #9839:
URL: https://github.com/apache/kafka/pull/9839#discussion_r553116066
##########
File path: core/src/main/scala/kafka/raft/KafkaNetworkChannel.scala
##########
@@ -87,6 +87,7 @@ private[raft] class RaftSendThread(
def sendRequest(request: RequestAndCompletionHandler): Unit = {
queue.add(request)
+ wakeup()
Review comment:
Is this a bug fix?
##########
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 = {
Review comment:
Should we shutdown ```raftClient``` in this method?
##########
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:
(this is unrelated to this PR.)
How about using ```Optional``` to replace the ```null```?
##########
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
Review comment:
Should it consider the state of "this thread also"? for example:
```scala
super.isRunning && client.isRunning
```
##########
File path: core/src/main/scala/kafka/tools/TestRaftServer.scala
##########
@@ -131,162 +102,29 @@ class TestRaftServer(
SocketServer.DataPlaneThreadPrefix
)
- socketServer.startProcessingRequests(Map.empty)
- ioThread = new RaftIoThread(raftClient)
- ioThread.start()
workloadGenerator.start()
+ raftManager.startup()
+ socketServer.startProcessingRequests(Map.empty)
}
def shutdown(): Unit = {
Review comment:
Is it need to shutdown ```raftManager```?
----------------------------------------------------------------
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:
users@infra.apache.org