Kafka 消费者模块(四):rebalance的确认最终分配结果

        onJoinComplete 完成rebalance最后一步,确认最终分配结果。

protected void onJoinComplete(int generation,
                              String memberId,
                              String assignmentStrategy,
                              ByteBuffer assignmentBuffer) {
    log.debug("Executing onJoinComplete with generation {} and memberId {}", generation, memberId);

    // only the leader is responsible for monitoring for metadata changes (i.e. partition changes)
    if (!isLeader)
        assignmentSnapshot = null;
    // 获取最终确定的分区分配策略对应的分区分配器
    ConsumerPartitionAssignor assignor = lookupAssignor(assignmentStrategy);
    if (assignor == null)
        throw new IllegalStateException("Coordinator selected invalid assignment protocol: " + assignmentStrategy);

    Set<TopicPartition> ownedPartitions = new HashSet<>(subscriptions.assignedPartitions());
    // 反序列化获取分区分配信息
    Assignment assignment = ConsumerProtocol.deserializeAssignment(assignmentBuffer);
    // 拿到分配的分区
    Set<TopicPartition> assignedPartitions = new HashSet<>(assignment.partitions());
    // 拿到的分区和我们的需要订阅的不一样,就重新rebalance
    if (!subscriptions.checkAssignmentMatchedSubscription(assignedPartitions)) {
        log.warn("We received an assignment {} that doesn't match our current subscription {}; it is likely " +
            "that the subscription has changed since we joined the group. Will try re-join the group with current subscription",
            assignment.partitions(), subscriptions.prettyString());

        requestRejoin();

        return;
    }

    final AtomicReference<Exception> firstException = new AtomicReference<>(null);
    Set<TopicPartition> addedPartitions = new HashSet<>(assignedPartitions);
    addedPartitions.removeAll(ownedPartitions);

    if (protocol == RebalanceProtocol.COOPERATIVE) {
        Set<TopicPartition> revokedPartitions = new HashSet<>(ownedPartitions);
        revokedPartitions.removeAll(assignedPartitions);

        log.info("Updating assignment with\n" +
                "now assigned partitions: {}\n" +
                "compare with previously owned partitions: {}\n" +
                "newly added partitions: {}\n" +
                "revoked partitions: {}\n",
            Utils.join(assignedPartitions, ", "),
            Utils.join(ownedPartitions, ", "),
            Utils.join(addedPartitions, ", "),
            Utils.join(revokedPartitions, ", ")
        );

        if (!revokedPartitions.isEmpty()) {
            // revoke partitions that were previously owned but no longer assigned;
            // note that we should only change the assignment (or update the assignor's state)
            // AFTER we've triggered  the revoke callback
            firstException.compareAndSet(null, invokePartitionsRevoked(revokedPartitions));

            // if revoked any partitions, need to re-join the group afterwards
            log.debug("Need to revoke partitions {} and re-join the group", revokedPartitions);
            requestRejoin();
        }
    }

    // The leader may have assigned partitions which match our subscription pattern, but which
    // were not explicitly requested, so we update the joined subscription here.
    // 以正则的方式订阅topic
    maybeUpdateJoinedSubscription(assignedPartitions);

    // give the assignor a chance to update internal state based on the received assignment
    // 更新本地缓存的集群元数据信息
    ConsumerGroupMetadata metadata = new ConsumerGroupMetadata(rebalanceConfig.groupId, generation, memberId, rebalanceConfig.groupInstanceId);
    assignor.onAssignment(assignment, metadata);

    // reschedule the auto commit starting from now
    // 重置下次自动提交 offset 的截止时间
    if (autoCommitEnabled)
        this.nextAutoCommitTimer.updateAndReset(autoCommitIntervalMs);
    // 更新assignments字段
    subscriptions.assignFromSubscribed(assignedPartitions);

    // add partitions that were not previously owned but are now assigned
    firstException.compareAndSet(null, invokePartitionsAssigned(addedPartitions));

    if (firstException.get() != null)
        throw new KafkaException("User rebalance callback throws an error", firstException.get());
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值