Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/main/scala/kafka/cluster/Partition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ object Partition {
metricsGroup.removeMetric("ReplicasCount", tags)
metricsGroup.removeMetric("LastStableOffsetLag", tags)
metricsGroup.removeMetric("AtMinIsr", tags)
metricsGroup.removeMetric("Sealed", tags)
}
}

Expand Down Expand Up @@ -228,6 +229,7 @@ class Partition(val topicPartition: TopicPartition,
metricsGroup.newGauge("AtMinIsr", () => if (isAtMinIsr) 1 else 0, tags)
metricsGroup.newGauge("ReplicasCount", () => if (isLeader) assignmentState.replicationFactor else 0, tags)
metricsGroup.newGauge("LastStableOffsetLag", () => log.map(_.lastStableOffsetLag).getOrElse(0), tags)
metricsGroup.newGauge("Sealed", () => if (isSealed) 1 else 0, tags)

def unifiedLog(): Optional[UnifiedLog] = log.toJava

Expand Down
7 changes: 6 additions & 1 deletion core/src/main/scala/kafka/server/ReplicaManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import kafka.cluster.Partition
import kafka.log.LogManager
import kafka.server.HostedPartition.Online
import kafka.server.QuotaFactory.QuotaManagers
import kafka.server.ReplicaManager.{AtMinIsrPartitionCountMetricName, FailedIsrUpdatesPerSecMetricName, IsrExpandsPerSecMetricName, IsrShrinksPerSecMetricName, LeaderCountMetricName, OfflineReplicaCountMetricName, PartitionCountMetricName, PartitionsWithLateTransactionsCountMetricName, ProducerIdCountMetricName, ReassigningPartitionsMetricName, UnderMinIsrPartitionCountMetricName, UnderReplicatedPartitionsMetricName, createLogReadResult, isListOffsetsTimestampUnsupported}
import kafka.server.ReplicaManager.{AtMinIsrPartitionCountMetricName, FailedIsrUpdatesPerSecMetricName, IsrExpandsPerSecMetricName, IsrShrinksPerSecMetricName, LeaderCountMetricName, OfflineReplicaCountMetricName, PartitionCountMetricName, PartitionsWithLateTransactionsCountMetricName, ProducerIdCountMetricName, ReassigningPartitionsMetricName, SealedPartitionsCountMetricName, UnderMinIsrPartitionCountMetricName, UnderReplicatedPartitionsMetricName, createLogReadResult, isListOffsetsTimestampUnsupported}
import kafka.server.metadata.{InklessMetadataView, KRaftMetadataCache}
import kafka.server.share.DelayedShareFetch
import kafka.utils._
Expand Down Expand Up @@ -151,6 +151,7 @@ object ReplicaManager {
private val UnderMinIsrPartitionCountMetricName = "UnderMinIsrPartitionCount"
private val AtMinIsrPartitionCountMetricName = "AtMinIsrPartitionCount"
private val ReassigningPartitionsMetricName = "ReassigningPartitions"
private val SealedPartitionsCountMetricName = "SealedPartitionsCount"
private val PartitionsWithLateTransactionsCountMetricName = "PartitionsWithLateTransactionsCount"
private val ProducerIdCountMetricName = "ProducerIdCount"
private val IsrExpandsPerSecMetricName = "IsrExpandsPerSec"
Expand All @@ -165,6 +166,7 @@ object ReplicaManager {
UnderMinIsrPartitionCountMetricName,
AtMinIsrPartitionCountMetricName,
ReassigningPartitionsMetricName,
SealedPartitionsCountMetricName,
PartitionsWithLateTransactionsCountMetricName,
ProducerIdCountMetricName
)
Expand Down Expand Up @@ -310,11 +312,14 @@ class ReplicaManager(val config: KafkaConfig,
metricsGroup.newGauge(UnderMinIsrPartitionCountMetricName, () => leaderPartitionsIterator.count(_.isUnderMinIsr))
metricsGroup.newGauge(AtMinIsrPartitionCountMetricName, () => leaderPartitionsIterator.count(_.isAtMinIsr))
metricsGroup.newGauge(ReassigningPartitionsMetricName, () => reassigningPartitionsCount)
metricsGroup.newGauge(SealedPartitionsCountMetricName, () => sealedPartitionsCount)
metricsGroup.newGauge(PartitionsWithLateTransactionsCountMetricName, () => lateTransactionsCount)
metricsGroup.newGauge(ProducerIdCountMetricName, () => producerIdCount)

private def reassigningPartitionsCount: Int = leaderPartitionsIterator.count(_.isReassigning)

private def sealedPartitionsCount: Int = leaderPartitionsIterator.count(_.isSealed)

private def lateTransactionsCount: Int = {
val currentTimeMs = time.milliseconds()
leaderPartitionsIterator.count(_.hasLateTransaction(currentTimeMs))
Expand Down
26 changes: 25 additions & 1 deletion core/src/test/scala/unit/kafka/cluster/PartitionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2840,7 +2840,8 @@ class PartitionTest extends AbstractPartitionTest {
"InSyncReplicasCount",
"ReplicasCount",
"LastStableOffsetLag",
"AtMinIsr")
"AtMinIsr",
"Sealed")

def getMetric(metric: String): Option[Metric] = {
KafkaYammerMetrics.defaultRegistry().allMetrics().asScala.find { case (metricName, _) =>
Expand Down Expand Up @@ -4086,6 +4087,29 @@ class PartitionTest extends AbstractPartitionTest {
assertTrue(partition.isSealed)
}

@Test
def testSealedPartitionMetric(): Unit = {
val leaderEpoch = 1
partition = setupPartitionWithMocks(leaderEpoch, isLeader = true)

def sealedGaugeValue(): Int = {
KafkaYammerMetrics.defaultRegistry().allMetrics().asScala
.find { case (metricName, _) =>
metricName.getName == "Sealed" && metricName.getType == "Partition" &&
metricName.getScope.contains(s"topic.${topicPartition.topic}") &&
metricName.getScope.contains(s"partition.${topicPartition.partition}")
}
.map(_._2.asInstanceOf[com.yammer.metrics.core.Gauge[Int]].value())
.getOrElse(fail("Sealed metric not found"))
}

assertEquals(0, sealedGaugeValue(), "Sealed gauge should be 0 before sealing")

partition.seal()

assertEquals(1, sealedGaugeValue(), "Sealed gauge should be 1 after sealing")
}

@Test
def testSealIsIdempotent(): Unit = {
val leaderEpoch = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6911,11 +6911,14 @@ class ReplicaManagerTest {
val sealFollower = rm.getPartitionOrException(new TopicPartition(topicToSeal, 1))
val otherLeader = rm.getPartitionOrException(new TopicPartition(otherTopic, 0))

assertEquals(0, yammerMetricValue("name=SealedPartitionsCount"), "SealedPartitionsCount should be 0 before sealing")

rm.sealTopicPartitions(topicToSeal)

assertTrue(sealLeader.isSealed, "Leader of target topic should be sealed")
assertFalse(sealFollower.isSealed, "Follower of target topic should not be sealed")
assertFalse(otherLeader.isSealed, "Leader of other topic should not be sealed")
assertEquals(1, yammerMetricValue("name=SealedPartitionsCount"), "SealedPartitionsCount should be 1 after sealing one leader")
} finally {
rm.shutdown(checkpointHW = false)
}
Expand Down
Loading