Pulsar源码解析-分区Producer创建的底层实现

上一篇介绍过
Pulsar源码解析-非分区Topic的Producer底层实现原理

差别不大,主要在客户端,回到创建Producer时的代码

    private <T> CompletableFuture<Producer<T>> createProducerAsync(String topic,
                                                                   ProducerConfigurationData conf,
                                                                   Schema<T> schema,
                                                                   ProducerInterceptors interceptors) {
        CompletableFuture<Producer<T>> producerCreatedFuture = new CompletableFuture<>();

        getPartitionedTopicMetadata(topic).thenAccept(metadata -> {

            ProducerBase<T> producer;
            if (metadata.partitions > 0) {
            	// 分区Topic创建
                producer = newPartitionedProducerImpl(topic, conf, schema, interceptors, producerCreatedFuture,
                        metadata);
            } else {
                producer = newProducerImpl(topic, -1, conf, schema, interceptors, producerCreatedFuture);
            }

            producers.add(producer);
        }).exceptionally(ex -> {
        });

        return producerCreatedFuture;
    }

具体在newPartitionedProducerImpl

public class PartitionedProducerImpl<T> extends ProducerBase<T> {

    public PartitionedProducerImpl(PulsarClientImpl client, String topic, ProducerConfigurationData conf, int numPartitions,
            CompletableFuture<Producer<T>> producerCreatedFuture, Schema<T> schema, ProducerInterceptors interceptors) {
        super(client, topic, conf, producerCreatedFuture, schema, interceptors);
        // 收集每个生产者的集合
        this.producers = Lists.newArrayListWithCapacity(numPartitions);
        this.topicMetadata = new TopicMetadataImpl(numPartitions);
        this.routerPolicy = getMessageRouter();
        stats = client.getConfiguration().getStatsIntervalSeconds() > 0 ? new ProducerStatsRecorderImpl() : null;
		
        int maxPendingMessages = Math.min(conf.getMaxPendingMessages(),
                conf.getMaxPendingMessagesAcrossPartitions() / numPartitions);
        conf.setMaxPendingMessages(maxPendingMessages);
		// 关键
        start();
		// 分区更新后自动发现
        if (conf.isAutoUpdatePartitions()) {
            topicsPartitionChangedListener = new TopicsPartitionChangedListener();
            partitionsAutoUpdateTimeout = client.timer()
                .newTimeout(partitionsAutoUpdateTimerTask, conf.getAutoUpdatePartitionsIntervalSeconds(), TimeUnit.SECONDS);
        }
    }
}

关键在start();

private void start() {
        AtomicReference<Throwable> createFail = new AtomicReference<Throwable>();
        AtomicInteger completed = new AtomicInteger();
        // 根据分区数创建对应数量Producer
        for (int partitionIndex = 0; partitionIndex < topicMetadata.numPartitions(); partitionIndex++) {
            String partitionName = TopicName.get(topic).getPartition(partitionIndex).toString();
            ProducerImpl<T> producer = client.newProducerImpl(partitionName, partitionIndex,
                    conf, schema, interceptors, new CompletableFuture<>());
            producers.add(producer);
            producer.producerCreatedFuture().handle((prod, createException) -> {
            	// 有任何一个失败都是失败
                if (createException != null) {
                    setState(State.Failed);
                    createFail.compareAndSet(null, createException);
                }
                // 最后一个分区创建完成
                if (completed.incrementAndGet() == topicMetadata.numPartitions()) {
                    if (createFail.get() == null) {
                    	// 标记分区Producer Ready
                        setState(State.Ready);
                        producerCreatedFuture().complete(PartitionedProducerImpl.this);
                    } else {
                        closeAsync().handle((ok, closeException) -> {
                            producerCreatedFuture().completeExceptionally(createFail.get());
                            client.cleanupProducer(this);
                            return null;
                        });
                    }
                }

                return null;
            });
        }

    }

看完上面这个相信大家都明白了,非分区topic的生产者只创建一个,分区Topic的生产者有多少分区数,创建多少生产者。
创建的区别就这么多,别的还有不同例如:发送时要选择一个分区发,有对应的路由策略。如果Close,则要遍历producer列表关闭每一个。

总结:Producer创建的核心还是在上一篇,不同点就是分区topic的Producer会创建对应分区数了的Producer。

Pulsar源码解析-非分区Topic的Producer底层实现原理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值