chia7712 commented on code in PR #15836: URL: https://github.com/apache/kafka/pull/15836#discussion_r1590078150
########## core/src/main/scala/kafka/server/FetchSession.scala: ########## @@ -690,6 +702,10 @@ class FetchSessionCache(private val maxEntries: Int, * 2. B is considered "stale" because it has been inactive for a long time, or * 3. A contains more partitions than B, and B is not recently created. * + * Prior to KAFKA-9401, the session cache was not sharded and we looked at all Review Comment: This docs is great. Could you please update this also? https://github.com/apache/kafka/blob/9b8aac22ec7ce927a2ceb2bfe7afd57419ee946c/core/src/main/scala/kafka/server/KafkaConfig.scala#L182 ########## core/src/main/scala/kafka/server/FetchSession.scala: ########## @@ -787,9 +803,37 @@ class FetchSessionCache(private val maxEntries: Int, } } } +object FetchSessionCache { + private[server] val metricsGroup = new KafkaMetricsGroup(classOf[FetchSessionCache]) + private val counter = new AtomicLong(0) +} + +class FetchSessionCache(private val cacheShards: Seq[FetchSessionCacheShard]) { + // Set up metrics. + FetchSessionCache.metricsGroup.newGauge(FetchSession.NUM_INCREMENTAL_FETCH_SESSIONS, () => cacheShards.map(_.size).sum) + FetchSessionCache.metricsGroup.newGauge(FetchSession.NUM_INCREMENTAL_FETCH_PARTITIONS_CACHED, () => cacheShards.map(_.totalPartitions).sum) + + def getCacheShard(sessionId: Int): FetchSessionCacheShard = { + val shard = sessionId / cacheShards.head.sessionIdRange Review Comment: It assumes the `cacheShards` is sorted by the `shardNum`, right? If so, could you please add comments for it? ########## core/src/main/scala/kafka/server/FetchSession.scala: ########## @@ -787,9 +803,37 @@ class FetchSessionCache(private val maxEntries: Int, } } } +object FetchSessionCache { + private[server] val metricsGroup = new KafkaMetricsGroup(classOf[FetchSessionCache]) + private val counter = new AtomicLong(0) +} + +class FetchSessionCache(private val cacheShards: Seq[FetchSessionCacheShard]) { + // Set up metrics. + FetchSessionCache.metricsGroup.newGauge(FetchSession.NUM_INCREMENTAL_FETCH_SESSIONS, () => cacheShards.map(_.size).sum) + FetchSessionCache.metricsGroup.newGauge(FetchSession.NUM_INCREMENTAL_FETCH_PARTITIONS_CACHED, () => cacheShards.map(_.totalPartitions).sum) + + def getCacheShard(sessionId: Int): FetchSessionCacheShard = { + val shard = sessionId / cacheShards.head.sessionIdRange + cacheShards(shard) + } + + // Returns the shard in round-robin + def getNextCacheShard: FetchSessionCacheShard = { + val shardNum = (FetchSessionCache.counter.getAndIncrement() % size).toInt Review Comment: As `int` is enough to this case, maybe we can use `AtomicInteger`? -- 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. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org