gaurav-narula commented on code in PR #15836: URL: https://github.com/apache/kafka/pull/15836#discussion_r1590274476
########## 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: I used `AtomicLong` to practically rule out an overflow but found `Utils.toPositive` which is used by `RoundRobinPartitioner` :) Updated to use an `AtomicInteger` and also added some test to ensure round-robin allocations. -- 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