【生产者篇】 send方法中更新元数据源码分析

本文深入分析Kafka生产者send方法中元数据更新的源码,详细解释waitOnMetadata和awaitUpdate两个关键步骤,揭示如何等待并获取最新Cluster信息。在send操作中,若消息未指定分区,将依赖Cluster信息计算分区号。通过do-while循环和版本号判断,确保数据更新成功。元数据更新由sender线程负责,当needUpdate标志为true时触发更新。此外,文章还梳理了Metadata、Cluster和Kafka节点的数据模型。
摘要由CSDN通过智能技术生成

在send方法中需要获取Cluster信息,然后计算分区的时候,如果消息没有指定分区,那么就会使用到Cluster信息用于计算分区号。所以在计算分区号之前,需要获取最新的Cluster数据

但是更新元数据的具体不在这里,准确的应该叫等待元数据更新。为什么是等待呢?看源码

send方法源码:

//....省略无关代码  
//等待元数据更新:[2.1]
clusterAndWaitTime = waitOnMetadata(record.topic(), record.partition(), clusterAndWaitTime.waitedOnMetadataMs);
//....
Cluster cluster = clusterAndWaitTime.cluster;
//....
// 计算分区号
int partition = partition(record, serializedKey, serializedValue, cluster);
//....省略无关代码  
2.1waitOnMetadata源码分析
/**
 * 等待集群的元数据,
 * Wait for cluster metadata including partitions for the given topic to be available.
 *
 * @param topic The topic we want metadata for
 * @param partition A specific partition expected to exist in metadata, or null if there's no preference
 * @param maxWaitMs The maximum time in ms for waiting on the metadata
 * @return The cluster containing topic metadata and the amount of time we waited in ms
 * @throws KafkaException for all Kafka-related exceptions, including the case where this method is called after
 *             producer close
 */
private ClusterAndWaitTime waitOnMetadata(String topic, Integer partition, long maxWaitMs)
    throws InterruptedException {
   
    // add topic to metadata topic list if it is not there already and reset expiry
    /**
     * 将topic的名字加入metadata进行维护
     * 
     * 在 metadata 中添加 topic 后,并重置过期时间,
     * 
     * 如果 metadata 中没有这个 topic 的 meta,也就是新加入的topic,打开更新开关needUpdate=true
     * 
     */
    metadata.add(topic);
    /**
     * metadata 在KafkaProducer 核心构造方法中实例化了
     * 获取Cluster 信息
     */
    Cluster cluster = metadata.fetch();
    /**
     * 获取topic 的分区数
     */
    Integer partitionsCount = cluster.partitionCountForTopic(topic);
    // Return cached metadata if we have it, and if the record's partition is either undefined
    // or within the known partition range
    /**
     * 如果medata中可已经维护的有该topic 的元数据,并且需要更新的分区号在 metadata维护的数据中,
     * 那么就直接返回该medata中的cluster 信息,
     */
    if (partitionsCount != null && (partition == null || partition < partitionsCount))
        return new ClusterAndWaitTime(cluster, 0);

    long begin = time.milliseconds();
    long remainingWaitMs = maxWaitMs;
    long elapsed;
    // Issue metadata requests until we have metadata for the topic or maxWaitTimeMs is exceeded.
    // In case we already have cached metadata for the topic, but the requested partition is greater
    // than expected, issue an update request only once. This is necessary in case the metadata
    // is stale and the number of partitions for this topic ha
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值