GroupMetadataManager是GroupCoordinator中管理ConsumerGroup元数据以及对应offset信息的组件。GroupMetadataManager底层使用offsetsTopic,以消息的形式储存Consumer Group的GroupMetadata信息以及消费者每个分区的offset
class GroupMetadataManager(val brokerId: Int,
val config: OffsetConfig,
// replicaManager对象,管理__consumer_offsets
replicaManager: ReplicaManager,
zkUtils: ZkUtils,
time: Time) extends Logging with KafkaMetricsGroup {
/* offsets cache */
// 记录了每个ConsumerGroup消费的分区的offset位置
private val offsetsCache = new Pool[GroupTopicPartition, OffsetAndMetadata]
/* group metadata cache */
// 记录每个Ggroup在服务端对应的GroupMetadata对象
private val groupsCache = new Pool[String, GroupMetadata]
/* partitions of consumer groups that are being loaded, its lock should be always called BEFORE offsetExpireLock and the group lock if needed */
// 记录了正在加载的offsets Topic分区的ID
private val loadingPartitions: mutable.Set[Int] = mutable.Set()
/* partitions of consumer groups that are assigned, using the same loading partition lock */
// 记录了已经加载的offsets Topic分区的ID
private val ownedPartitions: mutable.Set[Int] = mutable.Set()
/* lock for expiring stale offsets, it should be always called BEFORE the group lock if needed */
private val offsetExpireLock = new ReentrantReadWriteLock()
/* shutting down flag */
private val shuttingDown = new AtomicBoolean(false)
/* number of partitions for the consumer metadata topic */
// 记录offsets topic的分区数量,这个字段会调用getOffsetsTopicPartitionCount()进行初始化
private val groupMetadataTopicPartitionCount = getOffsetsTopicPartitionCount
/* Single-thread scheduler to handling offset/group metadata cache loading and unloading */
// 执行delete-expired-consumer-offsets
private val scheduler = new KafkaScheduler(threads = 1, threadNamePrefix = "group-metadata-manager-")
}
private def getOffsetsTopicPartitionCount = {
val topic = TopicConstants.GROUP_METADATA_TOPIC_NAME
// 从ZK中获取offset这个topic的分区信息和副本信息
val topicData = zkUtils.getPartitionAssignmentForTopics(Seq(topic))
if (topicData(topic).nonEmpty)
topicData(topic).size // 返回分区数量
else
config.offsetsTopicNumPartitions //返回配置数量
}