You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by ij...@apache.org on 2023/07/19 19:24:48 UTC

[kafka] branch trunk updated: KAFKA-15141: Initialize logger statically on hot codepaths (#13949)

This is an automated email from the ASF dual-hosted git repository.

ijuma pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 29f36d733b4 KAFKA-15141: Initialize logger statically on hot codepaths (#13949)
29f36d733b4 is described below

commit 29f36d733b4898257b148ddc4270c1b2a291cb7a
Author: gaurav-narula <97...@users.noreply.github.com>
AuthorDate: Wed Jul 19 20:24:40 2023 +0100

    KAFKA-15141: Initialize logger statically on hot codepaths (#13949)
    
    Log4j based loggers use `org.apache.logging.log4j.spi.AbstractLoggerAdapter::getContext`
    which invokes StackLocatorUtil to walk the stacktrace. This operation is quite CPU intensive
    and is performed each time during instantiation.
    
    To avoid walking the stack often, this change uses a static variable to initialize the logger
    for a few classes which seem to be instantiated frequently.
    
    Reviewers: Divij Vaidya <di...@amazon.com>, Ismael Juma <is...@juma.me.uk>
---
 .../main/scala/kafka/server/DelayedProduce.scala    |  9 +++++++--
 core/src/main/scala/kafka/server/FetchSession.scala | 21 +++++++++++++++++++++
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/core/src/main/scala/kafka/server/DelayedProduce.scala b/core/src/main/scala/kafka/server/DelayedProduce.scala
index 48a0ccbcff4..2f1261ada23 100644
--- a/core/src/main/scala/kafka/server/DelayedProduce.scala
+++ b/core/src/main/scala/kafka/server/DelayedProduce.scala
@@ -19,7 +19,7 @@ package kafka.server
 
 import java.util.concurrent.TimeUnit
 import java.util.concurrent.locks.Lock
-
+import com.typesafe.scalalogging.Logger
 import com.yammer.metrics.core.Meter
 import kafka.utils.Implicits._
 import kafka.utils.Pool
@@ -47,6 +47,10 @@ case class ProduceMetadata(produceRequiredAcks: Short,
   override def toString = s"[requiredAcks: $produceRequiredAcks, partitionStatus: $produceStatus]"
 }
 
+object DelayedProduce {
+  private final val logger = Logger(classOf[DelayedProduce])
+}
+
 /**
  * A delayed produce operation that can be created by the replica manager and watched
  * in the produce operation purgatory
@@ -58,6 +62,8 @@ class DelayedProduce(delayMs: Long,
                      lockOpt: Option[Lock] = None)
   extends DelayedOperation(delayMs, lockOpt) {
 
+  override lazy val logger = DelayedProduce.logger
+
   // first update the acks pending variable according to the error code
   produceMetadata.produceStatus.forKeyValue { (topicPartition, status) =>
     if (status.responseStatus.error == Errors.NONE) {
@@ -147,4 +153,3 @@ object DelayedProduceMetrics {
     partitionExpirationMeters.getAndMaybePut(partition).mark()
   }
 }
-
diff --git a/core/src/main/scala/kafka/server/FetchSession.scala b/core/src/main/scala/kafka/server/FetchSession.scala
index 8725a09f090..3f399311172 100644
--- a/core/src/main/scala/kafka/server/FetchSession.scala
+++ b/core/src/main/scala/kafka/server/FetchSession.scala
@@ -17,6 +17,7 @@
 
 package kafka.server
 
+import com.typesafe.scalalogging.Logger
 import kafka.utils.Logging
 import org.apache.kafka.common.{TopicIdPartition, TopicPartition, Uuid}
 import org.apache.kafka.common.message.FetchResponseData
@@ -356,12 +357,19 @@ class SessionErrorContext(val error: Errors,
   }
 }
 
+object SessionlessFetchContext {
+  private final val logger = Logger(classOf[SessionlessFetchContext])
+}
+
 /**
   * The fetch context for a sessionless fetch request.
   *
   * @param fetchData          The partition data from the fetch request.
   */
 class SessionlessFetchContext(val fetchData: util.Map[TopicIdPartition, FetchRequest.PartitionData]) extends FetchContext {
+
+  override lazy val logger = SessionlessFetchContext.logger
+
   override def getFetchOffset(part: TopicIdPartition): Option[Long] =
     Option(fetchData.get(part)).map(_.fetchOffset)
 
@@ -379,6 +387,10 @@ class SessionlessFetchContext(val fetchData: util.Map[TopicIdPartition, FetchReq
   }
 }
 
+object FullFetchContext {
+  private final val logger = Logger(classOf[FullFetchContext])
+}
+
 /**
   * The fetch context for a full fetch request.
   *
@@ -395,6 +407,9 @@ class FullFetchContext(private val time: Time,
                        private val fetchData: util.Map[TopicIdPartition, FetchRequest.PartitionData],
                        private val usesTopicIds: Boolean,
                        private val isFromFollower: Boolean) extends FetchContext {
+
+  override lazy val logger = FullFetchContext.logger
+
   override def getFetchOffset(part: TopicIdPartition): Option[Long] =
     Option(fetchData.get(part)).map(_.fetchOffset)
 
@@ -423,6 +438,10 @@ class FullFetchContext(private val time: Time,
   }
 }
 
+object IncrementalFetchContext {
+  private val logger = Logger(classOf[IncrementalFetchContext])
+}
+
 /**
   * The fetch context for an incremental fetch request.
   *
@@ -436,6 +455,8 @@ class IncrementalFetchContext(private val time: Time,
                               private val session: FetchSession,
                               private val topicNames: FetchSession.TOPIC_NAME_MAP) extends FetchContext {
 
+  override lazy val logger = IncrementalFetchContext.logger
+
   override def getFetchOffset(tp: TopicIdPartition): Option[Long] = session.getFetchOffset(tp)
 
   override def foreachPartition(fun: (TopicIdPartition, FetchRequest.PartitionData) => Unit): Unit = {