You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by jo...@apache.org on 2023/11/03 17:41:13 UTC
(kafka) 01/01: Simplify callback argument
This is an automated email from the ASF dual-hosted git repository.
jolshan pushed a commit to branch kafka-15653
in repository https://gitbox.apache.org/repos/asf/kafka.git
commit 9cca64580dfad593d83a1a3c02a24d2463cafd06
Author: Justine <jo...@confluent.io>
AuthorDate: Fri Nov 3 10:40:47 2023 -0700
Simplify callback argument
---
.../scala/kafka/server/KafkaRequestHandler.scala | 24 +++++++++------------
.../main/scala/kafka/server/ReplicaManager.scala | 7 +++---
.../kafka/server/KafkaRequestHandlerTest.scala | 25 +++++++++++-----------
3 files changed, 25 insertions(+), 31 deletions(-)
diff --git a/core/src/main/scala/kafka/server/KafkaRequestHandler.scala b/core/src/main/scala/kafka/server/KafkaRequestHandler.scala
index 170eb12a60f..3853a72d053 100755
--- a/core/src/main/scala/kafka/server/KafkaRequestHandler.scala
+++ b/core/src/main/scala/kafka/server/KafkaRequestHandler.scala
@@ -50,36 +50,32 @@ object KafkaRequestHandler {
}
/**
- * A callback method that expected to be executed once in an arbitrary request handler thread after an asynchronous action completes.
- * The RequestLocal passed in must belong to the request handler thread that is executing the callback.
- */
- class AsynchronousCompletionCallback[T](val fun: (RequestLocal, T) => Unit)
-
- /**
- * Wrap callback to schedule it on an arbitrary request thread.
+ * Execute or create a callback to be asynchronously scheduled on an arbitrary request thread
* NOTE: this function must be originally called from a request thread.
- * @param asyncCompletionCallback a callback function to execute as the result of an asynchronous action completing
- * @param requestLocal The RequestLocal for the current request handler thread in case we need to call
- * the callback function without queueing the callback request
+ * @param asyncCompletionCallback A callback method that is expected to be executed once in an arbitrary request
+ * handler thread after an asynchronous action completes. The RequestLocal passed in
+ * must belong to the request handler thread that is executing the callback.
+ * @param requestLocal The RequestLocal for the current request handler thread in case we need to execute the callback
+ * function immediately without queueing the callback request
* @return Wrapped callback that schedules `asyncCompletionCallback` on an arbitrary request thread
*/
- def wrap[T](asyncCompletionCallback: AsynchronousCompletionCallback[T], requestLocal: RequestLocal): T => Unit = {
+ def executeOrRegisterAsyncCallback[T](asyncCompletionCallback: (RequestLocal, T) => Unit, requestLocal: RequestLocal): T => Unit = {
val requestChannel = threadRequestChannel.get()
val currentRequest = threadCurrentRequest.get()
if (requestChannel == null || currentRequest == null) {
if (!bypassThreadCheck)
throw new IllegalStateException("Attempted to reschedule to request handler thread from non-request handler thread.")
- T => asyncCompletionCallback.fun(requestLocal, T)
+ T => asyncCompletionCallback(requestLocal, T)
} else {
T => {
if (threadCurrentRequest.get() == currentRequest) {
// If the callback is actually executed on the same request thread, we can directly execute
// it without re-scheduling it.
- asyncCompletionCallback.fun(requestLocal, T)
+ asyncCompletionCallback(requestLocal, T)
} else {
// The requestChannel and request are captured in this lambda, so when it's executed on the callback thread
// we can re-schedule the original callback on a request thread and update the metrics accordingly.
- requestChannel.sendCallbackRequest(RequestChannel.CallbackRequest(newRequestLocal => asyncCompletionCallback.fun(newRequestLocal, T), currentRequest))
+ requestChannel.sendCallbackRequest(RequestChannel.CallbackRequest(newRequestLocal => asyncCompletionCallback(newRequestLocal, T), currentRequest))
}
}
}
diff --git a/core/src/main/scala/kafka/server/ReplicaManager.scala b/core/src/main/scala/kafka/server/ReplicaManager.scala
index 8089d14f579..44ef1a8b6f6 100644
--- a/core/src/main/scala/kafka/server/ReplicaManager.scala
+++ b/core/src/main/scala/kafka/server/ReplicaManager.scala
@@ -23,7 +23,6 @@ import kafka.controller.{KafkaController, StateChangeLogger}
import kafka.log.remote.RemoteLogManager
import kafka.log.{LogManager, UnifiedLog}
import kafka.server.HostedPartition.Online
-import kafka.server.KafkaRequestHandler.AsynchronousCompletionCallback
import kafka.server.QuotaFactory.QuotaManagers
import kafka.server.ReplicaManager.{AtMinIsrPartitionCountMetricName, FailedIsrUpdatesPerSecMetricName, IsrExpandsPerSecMetricName, IsrShrinksPerSecMetricName, LeaderCountMetricName, OfflineReplicaCountMetricName, PartitionCountMetricName, PartitionsWithLateTransactionsCountMetricName, ProducerIdCountMetricName, ReassigningPartitionsMetricName, UnderMinIsrPartitionCountMetricName, UnderReplicatedPartitionsMetricName}
import kafka.server.ReplicaManager.createLogReadResult
@@ -759,8 +758,8 @@ class ReplicaManager(val config: KafkaConfig,
producerId = batchInfo.producerId,
producerEpoch = batchInfo.producerEpoch,
topicPartitions = notYetVerifiedEntriesPerPartition.keySet.toSeq,
- callback = KafkaRequestHandler.wrap(
- new AsynchronousCompletionCallback[Map[TopicPartition, Errors]](appendEntries(
+ callback = KafkaRequestHandler.executeOrRegisterAsyncCallback(
+ appendEntries(
entriesPerPartition,
internalTopicsAllowed,
origin,
@@ -771,7 +770,7 @@ class ReplicaManager(val config: KafkaConfig,
timeout,
responseCallback,
delayedProduceLock
- )),
+ ),
requestLocal)
))
}
diff --git a/core/src/test/scala/kafka/server/KafkaRequestHandlerTest.scala b/core/src/test/scala/kafka/server/KafkaRequestHandlerTest.scala
index e2588069bbf..d897a70f3bd 100644
--- a/core/src/test/scala/kafka/server/KafkaRequestHandlerTest.scala
+++ b/core/src/test/scala/kafka/server/KafkaRequestHandlerTest.scala
@@ -19,7 +19,6 @@ package kafka.server
import com.yammer.metrics.core.Meter
import kafka.network.RequestChannel
-import kafka.server.KafkaRequestHandler.AsynchronousCompletionCallback
import org.apache.kafka.common.memory.MemoryPool
import org.apache.kafka.common.network.{ClientInformation, ListenerName}
import org.apache.kafka.common.protocol.ApiKeys
@@ -58,11 +57,11 @@ class KafkaRequestHandlerTest {
when(apiHandler.handle(ArgumentMatchers.eq(request), any())).thenAnswer { _ =>
time.sleep(2)
// Prepare the callback.
- val callback = KafkaRequestHandler.wrap(
- new AsynchronousCompletionCallback[Int]((reqLocal: RequestLocal, ms: Int) => {
+ val callback = KafkaRequestHandler.executeOrRegisterAsyncCallback(
+ (reqLocal: RequestLocal, ms: Int) => {
time.sleep(ms)
handler.stop()
- }),
+ },
RequestLocal.NoCaching)
// Execute the callback asynchronously.
CompletableFuture.runAsync(() => callback(1))
@@ -97,10 +96,10 @@ class KafkaRequestHandlerTest {
when(apiHandler.handle(ArgumentMatchers.eq(request), any())).thenAnswer { _ =>
handledCount = handledCount + 1
// Prepare the callback.
- val callback = KafkaRequestHandler.wrap(
- new AsynchronousCompletionCallback[Int]((reqLocal: RequestLocal, ms: Int) => {
+ val callback = KafkaRequestHandler.executeOrRegisterAsyncCallback(
+ (reqLocal: RequestLocal, ms: Int) => {
handler.stop()
- }),
+ },
RequestLocal.NoCaching)
// Execute the callback asynchronously.
CompletableFuture.runAsync(() => callback(1))
@@ -133,12 +132,12 @@ class KafkaRequestHandlerTest {
when(apiHandler.handle(ArgumentMatchers.eq(request), any())).thenAnswer { _ =>
// Prepare the callback.
- val callback = KafkaRequestHandler.wrap(
- new AsynchronousCompletionCallback[Int]((reqLocal: RequestLocal, ms: Int) => {
+ val callback = KafkaRequestHandler.executeOrRegisterAsyncCallback(
+ (reqLocal: RequestLocal, ms: Int) => {
reqLocal.bufferSupplier.close()
handledCount = handledCount + 1
handler.stop()
- }),
+ },
originalRequestLocal)
// Execute the callback asynchronously.
CompletableFuture.runAsync(() => callback(1))
@@ -168,12 +167,12 @@ class KafkaRequestHandlerTest {
when(apiHandler.handle(ArgumentMatchers.eq(request), any())).thenAnswer { _ =>
// Prepare the callback.
- val callback = KafkaRequestHandler.wrap(
- new AsynchronousCompletionCallback[Int]((reqLocal: RequestLocal, ms: Int) => {
+ val callback = KafkaRequestHandler.executeOrRegisterAsyncCallback(
+ (reqLocal: RequestLocal, ms: Int) => {
reqLocal.bufferSupplier.close()
handledCount = handledCount + 1
handler.stop()
- }),
+ },
originalRequestLocal)
// Execute the callback before the request returns.
callback(1)