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/02/03 23:19:35 UTC

[GitHub] [kafka] rondagostino opened a new pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

rondagostino opened a new pull request #10045:
URL: https://github.com/apache/kafka/pull/10045


   `KafkaApis` is configured differently when it is used in a broker with a Raft-based controller quorum vs. a ZooKeeper quorum.  For example, when using Raft, `ForwardingManager` is required rather than optional, and there is no `AdminManager`, `KafkaController`, or `KafkaZkClient`.  This PR introduces `MetadataSupport` to abstract the two possibilities: `ZkSupport` and `RaftSupport`.  This provides a fluent way to decide what to do based on the type of support that `KafkaApis` has been configured with.  Certain types of requests are not supported when using raft (`AlterIsrRequest`, `UpdateMetadataRequest`, etc.).
   
   Existing tests are sufficient to detect bugs and regressions.
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


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



[GitHub] [kafka] hachikuji commented on a change in pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#discussion_r571174163



##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -3511,4 +3501,14 @@ object KafkaApis {
     FetchResponse.sizeOf(versionId, unconvertedResponse.responseData.entrySet
       .iterator.asScala.filter(element => quota.isThrottled(element.getKey)).asJava)
   }
+
+  // visible for testing
+  private[server] def shouldNeverReceive(request: RequestChannel.Request): Exception = {

Review comment:
       Why move these and not `unsupported`/`notYetSupported`?




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



[GitHub] [kafka] ijuma commented on a change in pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
ijuma commented on a change in pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#discussion_r569830165



##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,69 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZk(createException: () => Exception): ZkSupport = {
+    this match {
+      case zkSupport@ZkSupport(_, _, _, _) => zkSupport
+      case RaftSupport(_) => throw createException()
+    }
+  }
+
+  /**
+   * Return this instance downcast for use with Raft
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with Raft
+   * @throws Exception if this instance is not for Raft
+   */
+  def requireRaft(createException: () => Exception): RaftSupport = {
+    this match {
+      case raftSupport@RaftSupport(_) => raftSupport
+      case ZkSupport(_, _, _, _) => throw createException()

Review comment:
       It's a bit odd to have methods where we do pattern matching and have some overrides. It's probably simpler to just have different implementations instead of pattern matching.




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



[GitHub] [kafka] jsancio commented on a change in pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
jsancio commented on a change in pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#discussion_r570507065



##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZk(createException: => Exception): ZkSupport

Review comment:
       This comment also applies to `requireRaft`.
   
   How about `requireZkOrThrow`? There is already precedence of this naming convention in Kafka and Java. 




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



[GitHub] [kafka] rondagostino commented on a change in pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
rondagostino commented on a change in pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#discussion_r569938271



##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -149,12 +153,23 @@ class KafkaApis(val requestChannel: RequestChannel,
       }
     }
 
-    forwardingManager match {
-      case Some(mgr) if !request.isForwarded && !controller.isActive =>
-        mgr.forwardRequest(request, responseCallback)
+    metadataSupport match {
+      // ZooKeeper
+      case zkSupport@ZkSupport(_, _, _, forwardingManager) =>
+        forwardingManager match {
+          case Some(mgr) if !request.isForwarded && !zkSupport.controller.isActive =>
+            mgr.forwardRequest(request, responseCallback)
 
-      case _ =>
-        handler(request)
+          case _ =>
+            handler(request)
+        }
+      // Raft
+      case RaftSupport(fwdMgr) =>
+        if (!request.isForwarded) {
+          fwdMgr.forwardRequest(request, responseCallback)
+        } else {
+          handler(request) // will reject
+        }

Review comment:
       I added a maybeForward() method to the trait




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



[GitHub] [kafka] hachikuji commented on a change in pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#discussion_r570727037



##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZkOrThrow(createException: => Exception): ZkSupport
+
+  /**
+   * Return this instance downcast for use with Raft
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with Raft
+   * @throws Exception if this instance is not for Raft
+   */
+  def requireRaftOrThrow(createException: => Exception): RaftSupport
+
+  /**
+   * Confirm that this instance is consistent with the given config
+   *
+   * @param config the config to check for consistency with this instance
+   * @throws IllegalStateException if there is an inconsistency (Raft for a ZooKeeper config or vice-versa)
+   */
+  def confirmConsistentWith(config: KafkaConfig): Unit
+
+  def maybeForward(request: RequestChannel.Request,
+                   handler: RequestChannel.Request => Unit,
+                   responseCallback: Option[AbstractResponse] => Unit): Unit
+}
+
+case class ZkSupport(adminManager: ZkAdminManager,
+                     controller: KafkaController,
+                     zkClient: KafkaZkClient,
+                     forwardingManager: Option[ForwardingManager]) extends MetadataSupport {
+  val adminZkClient = new AdminZkClient(zkClient)
+
+  override def requireZkOrThrow(createException: => Exception): ZkSupport = this
+  override def requireRaftOrThrow(createException: => Exception): RaftSupport = throw createException
+
+  override def confirmConsistentWith(config: KafkaConfig): Unit = {
+    if (!config.requiresZookeeper) {
+      throw new IllegalStateException("Config specifies Raft but metadata support instance is for ZooKeeper")
+    }
+  }
+
+  override def maybeForward(request: RequestChannel.Request,
+                            handler: RequestChannel.Request => Unit,
+                            responseCallback: Option[AbstractResponse] => Unit): Unit = {
+    if (forwardingManager.isDefined && !request.isForwarded && !controller.isActive) {

Review comment:
       nit: usually when you see `isDefined` followed by `get`, there is likely an opportunity for a `match` or `foreach`. 
   ```scala
   forwardingManager match {
     case Some(mgr) if !request.isForwarded && !controllers.isActive =>
       forwardingManager.get.forwardRequest(request, responseCallback)
     case _ => 
       handler(request)
   }
   ```

##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZkOrThrow(createException: => Exception): ZkSupport
+
+  /**
+   * Return this instance downcast for use with Raft
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with Raft
+   * @throws Exception if this instance is not for Raft
+   */
+  def requireRaftOrThrow(createException: => Exception): RaftSupport
+
+  /**
+   * Confirm that this instance is consistent with the given config
+   *
+   * @param config the config to check for consistency with this instance
+   * @throws IllegalStateException if there is an inconsistency (Raft for a ZooKeeper config or vice-versa)
+   */
+  def confirmConsistentWith(config: KafkaConfig): Unit

Review comment:
       nit: confirm -> ensure?

##########
File path: core/src/test/scala/unit/kafka/server/KafkaApisTest.scala
##########
@@ -3197,4 +3200,192 @@ class KafkaApisTest {
 
   }
 
+  @Test
+  def testRaftHandleLeaderAndIsrRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleLeaderAndIsrRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+  }
+
+  @Test
+  def testRaftHandleStopReplicaRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleStopReplicaRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+    //    handleUpdateMetadataRequest

Review comment:
       Did you mean to include these?

##########
File path: core/src/test/scala/unit/kafka/utils/TestUtils.scala
##########
@@ -273,7 +273,8 @@ object TestUtils extends Logging {
                          logDirCount: Int = 1,
                          enableToken: Boolean = false,
                          numPartitions: Int = 1,
-                         defaultReplicationFactor: Short = 1): Properties = {
+                         defaultReplicationFactor: Short = 1,
+                         processRoles: String = ""): Properties = {

Review comment:
       Adding function args here is a no no. Too many downstream dependencies. Since we have only one usage at the moment, it is better to modify the `Properties` that is returned from `createBrokerConfig`. We can think of something nicer when we have more uses.

##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft

Review comment:
       Maybe worth adding a note that this state is temporary. We only allow forwarding manager at the moment with zk broker to enable integration testing. We can remove this as soon as the controller is checked in and we can convert the integration tests.

##########
File path: core/src/test/scala/unit/kafka/server/KafkaApisTest.scala
##########
@@ -3197,4 +3200,192 @@ class KafkaApisTest {
 
   }
 
+  @Test
+  def testRaftHandleLeaderAndIsrRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleLeaderAndIsrRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+  }
+
+  @Test
+  def testRaftHandleStopReplicaRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleStopReplicaRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+    //    handleUpdateMetadataRequest
+    //    handleControlledShutdownRequest
+    //    handleAlterIsrRequest
+    //    handleEnvelope
+  }
+
+  @Test
+  def testRaftHandleUpdateMetadataRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleUpdateMetadataRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+    //    handleControlledShutdownRequest
+    //    handleAlterIsrRequest
+    //    handleEnvelope
+  }
+
+  @Test
+  def testRaftHandleControlledShutdownRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleControlledShutdownRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+    //    handleAlterIsrRequest
+    //    handleEnvelope
+  }
+
+  @Test
+  def testRaftHandleAlterIsrRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleAlterIsrRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+  }
+
+  @Test
+  def testRaftHandleEnvelope(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleEnvelope(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+  }
+
+  @Test
+  def testRaftHandleCreateTopicsRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleCreateTopicsRequest(request))
+    assertEquals(kafkaApis.shouldAlwaysBeForwarded(request).getMessage, e.getMessage)
+  }
+
+  @Test
+  def testRaftHandleCreatePartitionsRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleCreatePartitionsRequest(request))
+    assertEquals(kafkaApis.shouldAlwaysBeForwarded(request).getMessage, e.getMessage)
+  }
+
+  @Test
+  def testRaftHandleDeleteTopicsRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])

Review comment:
       nit: there's a lot of redundancy in these test cases. Could we define helpers? For example:
   ```scala
   def assertUnsupportedWithRaft(handler: RequestChannel.Request => Unit)
   
   assertUnsupportedWithRaft(kafkaApis.handleDeleteTopicsRequest)
   ```

##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -3446,17 +3465,41 @@ class KafkaApis(val requestChannel: RequestChannel,
     request.temporaryMemoryBytes = conversionStats.temporaryMemoryBytes
   }
 
-  private def isBrokerEpochStale(brokerEpochInRequest: Long): Boolean = {
+  private def isBrokerEpochStale(zkSupport: ZkSupport, brokerEpochInRequest: Long): Boolean = {
     // Broker epoch in LeaderAndIsr/UpdateMetadata/StopReplica request is unknown
     // if the controller hasn't been upgraded to use KIP-380
     if (brokerEpochInRequest == AbstractControlRequest.UNKNOWN_BROKER_EPOCH) false
     else {
       // brokerEpochInRequest > controller.brokerEpoch is possible in rare scenarios where the controller gets notified
       // about the new broker epoch and sends a control request with this epoch before the broker learns about it
-      brokerEpochInRequest < controller.brokerEpoch
+      brokerEpochInRequest < zkSupport.controller.brokerEpoch
     }
   }
 
+  // visible for testing
+  private[server] def shouldNeverReceive(request: RequestChannel.Request): Exception = {
+    new UnsupportedVersionException(s"Should never receive when using a Raft-based metadata quorum: $request")

Review comment:
       Perhaps no need to print the whole request. Maybe the ApiKey is enough?

##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZkOrThrow(createException: => Exception): ZkSupport

Review comment:
       It's a little odd to see this in the trait. Would it be reasonable to turn them into private defs in `KafkaApis`?

##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZkOrThrow(createException: => Exception): ZkSupport

Review comment:
       It's a little odd to see this in the trait. Would it be reasonable to turn them into private defs in `KafkaApis`? I'm ok with it if you think it is better. We'll only ever have the two implementations.




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



[GitHub] [kafka] hachikuji commented on a change in pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#discussion_r570727037



##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZkOrThrow(createException: => Exception): ZkSupport
+
+  /**
+   * Return this instance downcast for use with Raft
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with Raft
+   * @throws Exception if this instance is not for Raft
+   */
+  def requireRaftOrThrow(createException: => Exception): RaftSupport
+
+  /**
+   * Confirm that this instance is consistent with the given config
+   *
+   * @param config the config to check for consistency with this instance
+   * @throws IllegalStateException if there is an inconsistency (Raft for a ZooKeeper config or vice-versa)
+   */
+  def confirmConsistentWith(config: KafkaConfig): Unit
+
+  def maybeForward(request: RequestChannel.Request,
+                   handler: RequestChannel.Request => Unit,
+                   responseCallback: Option[AbstractResponse] => Unit): Unit
+}
+
+case class ZkSupport(adminManager: ZkAdminManager,
+                     controller: KafkaController,
+                     zkClient: KafkaZkClient,
+                     forwardingManager: Option[ForwardingManager]) extends MetadataSupport {
+  val adminZkClient = new AdminZkClient(zkClient)
+
+  override def requireZkOrThrow(createException: => Exception): ZkSupport = this
+  override def requireRaftOrThrow(createException: => Exception): RaftSupport = throw createException
+
+  override def confirmConsistentWith(config: KafkaConfig): Unit = {
+    if (!config.requiresZookeeper) {
+      throw new IllegalStateException("Config specifies Raft but metadata support instance is for ZooKeeper")
+    }
+  }
+
+  override def maybeForward(request: RequestChannel.Request,
+                            handler: RequestChannel.Request => Unit,
+                            responseCallback: Option[AbstractResponse] => Unit): Unit = {
+    if (forwardingManager.isDefined && !request.isForwarded && !controller.isActive) {

Review comment:
       nit: usually when you see `isDefined` followed by `get`, there is likely an opportunity for a `match` or `foreach`. 
   ```scala
   forwardingManager match {
     case Some(mgr) if !request.isForwarded && !controllers.isActive =>
       forwardingManager.get.forwardRequest(request, responseCallback)
     case _ => 
       handler(request)
   }
   ```

##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZkOrThrow(createException: => Exception): ZkSupport
+
+  /**
+   * Return this instance downcast for use with Raft
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with Raft
+   * @throws Exception if this instance is not for Raft
+   */
+  def requireRaftOrThrow(createException: => Exception): RaftSupport
+
+  /**
+   * Confirm that this instance is consistent with the given config
+   *
+   * @param config the config to check for consistency with this instance
+   * @throws IllegalStateException if there is an inconsistency (Raft for a ZooKeeper config or vice-versa)
+   */
+  def confirmConsistentWith(config: KafkaConfig): Unit

Review comment:
       nit: confirm -> ensure?

##########
File path: core/src/test/scala/unit/kafka/server/KafkaApisTest.scala
##########
@@ -3197,4 +3200,192 @@ class KafkaApisTest {
 
   }
 
+  @Test
+  def testRaftHandleLeaderAndIsrRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleLeaderAndIsrRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+  }
+
+  @Test
+  def testRaftHandleStopReplicaRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleStopReplicaRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+    //    handleUpdateMetadataRequest

Review comment:
       Did you mean to include these?

##########
File path: core/src/test/scala/unit/kafka/utils/TestUtils.scala
##########
@@ -273,7 +273,8 @@ object TestUtils extends Logging {
                          logDirCount: Int = 1,
                          enableToken: Boolean = false,
                          numPartitions: Int = 1,
-                         defaultReplicationFactor: Short = 1): Properties = {
+                         defaultReplicationFactor: Short = 1,
+                         processRoles: String = ""): Properties = {

Review comment:
       Adding function args here is a no no. Too many downstream dependencies. Since we have only one usage at the moment, it is better to modify the `Properties` that is returned from `createBrokerConfig`. We can think of something nicer when we have more uses.

##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft

Review comment:
       Maybe worth adding a note that this state is temporary. We only allow forwarding manager at the moment with zk broker to enable integration testing. We can remove this as soon as the controller is checked in and we can convert the integration tests.

##########
File path: core/src/test/scala/unit/kafka/server/KafkaApisTest.scala
##########
@@ -3197,4 +3200,192 @@ class KafkaApisTest {
 
   }
 
+  @Test
+  def testRaftHandleLeaderAndIsrRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleLeaderAndIsrRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+  }
+
+  @Test
+  def testRaftHandleStopReplicaRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleStopReplicaRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+    //    handleUpdateMetadataRequest
+    //    handleControlledShutdownRequest
+    //    handleAlterIsrRequest
+    //    handleEnvelope
+  }
+
+  @Test
+  def testRaftHandleUpdateMetadataRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleUpdateMetadataRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+    //    handleControlledShutdownRequest
+    //    handleAlterIsrRequest
+    //    handleEnvelope
+  }
+
+  @Test
+  def testRaftHandleControlledShutdownRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleControlledShutdownRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+    //    handleAlterIsrRequest
+    //    handleEnvelope
+  }
+
+  @Test
+  def testRaftHandleAlterIsrRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleAlterIsrRequest(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+  }
+
+  @Test
+  def testRaftHandleEnvelope(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleEnvelope(request))
+    assertEquals(kafkaApis.shouldNeverReceive(request).getMessage, e.getMessage)
+  }
+
+  @Test
+  def testRaftHandleCreateTopicsRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleCreateTopicsRequest(request))
+    assertEquals(kafkaApis.shouldAlwaysBeForwarded(request).getMessage, e.getMessage)
+  }
+
+  @Test
+  def testRaftHandleCreatePartitionsRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])
+    val kafkaApis = createKafkaApis(raftSupport = true)
+    val e = assertThrows(classOf[UnsupportedVersionException],
+      () => kafkaApis.handleCreatePartitionsRequest(request))
+    assertEquals(kafkaApis.shouldAlwaysBeForwarded(request).getMessage, e.getMessage)
+  }
+
+  @Test
+  def testRaftHandleDeleteTopicsRequest(): Unit = {
+    val request: RequestChannel.Request = EasyMock.createNiceMock(classOf[RequestChannel.Request])

Review comment:
       nit: there's a lot of redundancy in these test cases. Could we define helpers? For example:
   ```scala
   def assertUnsupportedWithRaft(handler: RequestChannel.Request => Unit)
   
   assertUnsupportedWithRaft(kafkaApis.handleDeleteTopicsRequest)
   ```

##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -3446,17 +3465,41 @@ class KafkaApis(val requestChannel: RequestChannel,
     request.temporaryMemoryBytes = conversionStats.temporaryMemoryBytes
   }
 
-  private def isBrokerEpochStale(brokerEpochInRequest: Long): Boolean = {
+  private def isBrokerEpochStale(zkSupport: ZkSupport, brokerEpochInRequest: Long): Boolean = {
     // Broker epoch in LeaderAndIsr/UpdateMetadata/StopReplica request is unknown
     // if the controller hasn't been upgraded to use KIP-380
     if (brokerEpochInRequest == AbstractControlRequest.UNKNOWN_BROKER_EPOCH) false
     else {
       // brokerEpochInRequest > controller.brokerEpoch is possible in rare scenarios where the controller gets notified
       // about the new broker epoch and sends a control request with this epoch before the broker learns about it
-      brokerEpochInRequest < controller.brokerEpoch
+      brokerEpochInRequest < zkSupport.controller.brokerEpoch
     }
   }
 
+  // visible for testing
+  private[server] def shouldNeverReceive(request: RequestChannel.Request): Exception = {
+    new UnsupportedVersionException(s"Should never receive when using a Raft-based metadata quorum: $request")

Review comment:
       Perhaps no need to print the whole request. Maybe the ApiKey is enough?

##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZkOrThrow(createException: => Exception): ZkSupport

Review comment:
       It's a little odd to see this in the trait. Would it be reasonable to turn them into private defs in `KafkaApis`?




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



[GitHub] [kafka] hachikuji merged pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
hachikuji merged pull request #10045:
URL: https://github.com/apache/kafka/pull/10045


   


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



[GitHub] [kafka] hachikuji commented on a change in pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
hachikuji commented on a change in pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#discussion_r570730096



##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZkOrThrow(createException: => Exception): ZkSupport

Review comment:
       It's a little odd to see this in the trait. Would it be reasonable to turn them into private defs in `KafkaApis`? I'm ok with it if you think it is better. We'll only ever have the two implementations.




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



[GitHub] [kafka] rondagostino commented on pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
rondagostino commented on pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#issuecomment-773019866


   This is still in draft because it depends on https://github.com/apache/kafka/pull/10005


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



[GitHub] [kafka] rondagostino commented on a change in pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
rondagostino commented on a change in pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#discussion_r571174825



##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -3511,4 +3501,14 @@ object KafkaApis {
     FetchResponse.sizeOf(versionId, unconvertedResponse.responseData.entrySet
       .iterator.asScala.filter(element => quota.isThrottled(element.getKey)).asJava)
   }
+
+  // visible for testing
+  private[server] def shouldNeverReceive(request: RequestChannel.Request): Exception = {

Review comment:
       Ah, I hadn't tested for those, and I moved based on rewriting tests.  Will move.




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



[GitHub] [kafka] ijuma commented on a change in pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
ijuma commented on a change in pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#discussion_r569830423



##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,69 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZk(createException: () => Exception): ZkSupport = {
+    this match {
+      case zkSupport@ZkSupport(_, _, _, _) => zkSupport
+      case RaftSupport(_) => throw createException()
+    }
+  }
+
+  /**
+   * Return this instance downcast for use with Raft
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with Raft
+   * @throws Exception if this instance is not for Raft
+   */
+  def requireRaft(createException: () => Exception): RaftSupport = {
+    this match {
+      case raftSupport@RaftSupport(_) => raftSupport
+      case ZkSupport(_, _, _, _) => throw createException()
+    }
+  }
+}
+
+case class ZkSupport(adminManager: ZkAdminManager,
+                     controller: KafkaController,
+                     zkClient: KafkaZkClient,
+                     forwardingManager: Option[ForwardingManager]) extends MetadataSupport {
+  private val _adminZkClient = new AdminZkClient(zkClient)

Review comment:
       There's no benefit in having a val and def for something. var and def makes sense, but that's not what we have here. Just make the `val` public. Right?




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



[GitHub] [kafka] jsancio commented on a change in pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
jsancio commented on a change in pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#discussion_r569838605



##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -117,9 +113,17 @@ class KafkaApis(val requestChannel: RequestChannel,
                 val brokerFeatures: BrokerFeatures,
                 val finalizedFeatureCache: FinalizedFeatureCache) extends ApiRequestHandler with Logging {
 
+  metadataSupport match {
+    case ZkSupport(_, _, _, _) if !config.requiresZookeeper =>
+      throw new IllegalStateException("Config specifies Raft but metadata support instance is for ZooKeeper")
+    case RaftSupport(_) if config.requiresZookeeper =>
+      throw new IllegalStateException("Config specifies ZooKeeper but metadata support instance is for Raft")
+    case _ => // consistent configs and metadata support instance
+  }

Review comment:
       We can probably move this to `MetadataSupport::validateRequiresZooKeeper(value: Boolean): Unit`

##########
File path: core/src/main/scala/kafka/server/KafkaApis.scala
##########
@@ -149,12 +153,23 @@ class KafkaApis(val requestChannel: RequestChannel,
       }
     }
 
-    forwardingManager match {
-      case Some(mgr) if !request.isForwarded && !controller.isActive =>
-        mgr.forwardRequest(request, responseCallback)
+    metadataSupport match {
+      // ZooKeeper
+      case zkSupport@ZkSupport(_, _, _, forwardingManager) =>
+        forwardingManager match {
+          case Some(mgr) if !request.isForwarded && !zkSupport.controller.isActive =>
+            mgr.forwardRequest(request, responseCallback)
 
-      case _ =>
-        handler(request)
+          case _ =>
+            handler(request)
+        }
+      // Raft
+      case RaftSupport(fwdMgr) =>
+        if (!request.isForwarded) {
+          fwdMgr.forwardRequest(request, responseCallback)
+        } else {
+          handler(request) // will reject
+        }

Review comment:
       One way to move this complexity to the implementations of `MetadataSupport` is by adding the following method:
   
   ```scala
   def forwardRequest(request: RequestChannel.Request, callback: Option[AbstractResponse] => Unit): Boolean
   ```
   
   With this function, here you can:
   
   ```scala
   if (!metadataSupport.forwardRequest(request, responseCallback)) {
     handler(request);
   }
   ```

##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,69 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZk(createException: () => Exception): ZkSupport = {

Review comment:
       If you want to delay the execution of some expression, you can do this with `def requireZkOrThrow(thunk: => Exception): ZkSupport`. At the call site you can just do `metadataSupport.requireZkOrThrow(new Exception())`. This will allow to change some of the functions you defined from:
   ```scala
   def createException(...): () => Exception
   ```
   to just
   ```scala
   def createException(...): Exception
   ```




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



[GitHub] [kafka] jsancio commented on a change in pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
jsancio commented on a change in pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#discussion_r570507065



##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZk(createException: => Exception): ZkSupport

Review comment:
       This comment also applies to `requireRaft`.
   
   How about `requireZkOrThrow`? There is already precedence of this naming convention in Kafka and Java. 




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



[GitHub] [kafka] rondagostino commented on a change in pull request #10045: MINOR: Allow KafkaApis to be configured for Raft controller quorums

Posted by GitBox <gi...@apache.org>.
rondagostino commented on a change in pull request #10045:
URL: https://github.com/apache/kafka/pull/10045#discussion_r571124965



##########
File path: core/src/main/scala/kafka/server/MetadataSupport.scala
##########
@@ -0,0 +1,109 @@
+/**
+ * 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.server
+
+import kafka.controller.KafkaController
+import kafka.network.RequestChannel
+import kafka.zk.{AdminZkClient, KafkaZkClient}
+import org.apache.kafka.common.requests.AbstractResponse
+
+sealed trait MetadataSupport {
+  /**
+   * Provide a uniform way of getting to the ForwardingManager, which is a shared concept
+   * despite being optional when using ZooKeeper and required when using Raft
+   */
+  val forwardingManager: Option[ForwardingManager]
+
+  /**
+   * Return this instance downcast for use with ZooKeeper
+   *
+   * @param createException function to create an exception to throw
+   * @return this instance downcast for use with ZooKeeper
+   * @throws Exception if this instance is not for ZooKeeper
+   */
+  def requireZkOrThrow(createException: => Exception): ZkSupport

Review comment:
       Yeah, I prefer it here only because this has worked out so well that I would not be surprised if we tried to use the same approach somewhere else.




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