Pulsar 分区主题、非持久性主题和系统主题

目录

1、Partitioned topics 分区主题

(1)Routing modes 路由模式

(2)Ordering guarantee 排序保证

(3)Hashing scheme(Hash 策略)​

2、Non-persistent topics 非持久性主题

3、System topic 系统主题


1、Partitioned topics 分区主题

        普通主题仅由单个代理(broker)提供服务,这限制了主题的最大吞吐量。分区主题是一种特殊类型的主题,由多个 broker 处理,因此可以有更高的吞吐量。

        一个分区主题实际上由 N 个内部主题实现(其中N是分区数)。当消息发布到分区主题时,每条消息都会路由到其中一个代理上,Pulsar 会自动处理跨代理的分区分布。

        下图说明了这一点:

        Topic1 主题有五个分区( P0 到 P4 ),分为三个代理(brokers)。因为分区比 brokers 多,所以其中两个 brokers 需要处理两个分区,而第三个 brokers 只需要处理一个分区(Pulsar 会自动处理分区的分布)。

        此主题的消息将广播给两个消费者。路由模式决定了消息应该发布到哪一个分区,而订阅类型决定了消息应该发布给哪一个消费者

        大多数情况下,可以分别决定路由策略和订阅类型。通常,分区或路由策略根据吞吐量来决定,而订阅类型根据应用程序的应用场景指定。

        对于订阅类型来说,分区主题和普通主题没有什么区别,因为分区主题仅决定生产者如何发布消息、消费者如何处理和确认消息。

        分区主题需要通过 admin API 显式创建。创建主题时可以指定分区数。

(1)Routing modes 路由模式

        消息发布到分区主题时,必须指定路由模式。路由模式决定了消息应该发布到那个分区(即哪个内部主题)。

        有三种消息路由模式可用:

ModeDescription

RoundRobinPartition

循环分区(默认)

If no key is provided, the producer will publish messages across all partitions in round-robin fashion to achieve maximum throughput. Please note that round-robin is not done per individual message but rather it's set to the same boundary of batching delay, to ensure batching is effective. While if a key is specified on the message, the partitioned producer will hash the key and assign message to a particular partition. This is the default mode.

// 如果没有提供Key,生产者将以循环方式跨所有分区发布消息,以实现最大吞吐量。请注意,循环方式不是针对单个消息进行的,而是设置和批量处理相同的边界,确保该批量处理有效。如果在消息上指定了Key,则分区生产者会对该Key进行Hash计算后,并将消息分配给特定分区。

SinglePartition

单独分区

If no key is provided, the producer will randomly pick one single partition and publish all the messages into that partition. While if a key is specified on the message, the partitioned producer will hash the key and assign message to a particular partition.

// 如果没有提供 Key,生产者将随机选择一个分区,并将所有消息发布到该分区中。如果在消息上指定了Key,则分区生产者会对该Key进行Hash计算后,并将消息分配给特定分区。

CustomPartition

自定义分区

Use custom message router implementation that will be called to determine the partition for a particular message. User can create a custom routing mode by using the Java client and implementing the MessageRouter interface.

// 使用自定义的消息路由策略将特定的消息发布到分区。用户可以使用Java客户端并实现MessageRouter接口来创建自定义路由模式。

(2)Ordering guarantee 排序保证

        消息的顺序与消息路由模式和 Message Key 有关。通常,用户会希望按 Key 分区来保证排序。

        如果有一条消息附加了 Key,那么在使用单独分区(SinglePartition)或循环分区(RoundRobinPartition)模式时,消息将根据 ProducerBuilder 中 HashingScheme 指定的哈希策略路由到相应的分区。// 根据 key 排序,或者根据生产者排序

Ordering guaranteeDescriptionRouting Mode and Key
Per-key-partition

All the messages with the same key will be in order and be placed in same partition.

// 所有具有相同 key 的消息将按顺序排列并放置在同一分区中。

Use either SinglePartition or RoundRobinPartition mode, and Key is provided by each message.

// 提供每条消息的key

Per-producer

All the messages from the same producer will be in order.

// 来自同一生产者的所有消息将按顺序排列。

Use SinglePartition mode, and no Key is provided for each message.

// 使用单独分区,不提供消息的 key

(3)Hashing scheme(Hash 策略)

        HashingScheme 是一个枚举类,表示为一条特定的消息选择分区时可用的标准哈希函数集。

        有两种类型的标准哈希函数可用:JavaStringHash 和 Murmur3_32Hash。producer 使用的默认哈希函数是 JavaStringHash。请注意,当生产者来自多种不同的语言客户端时,JavaStringHash 将不再有用,在这种情况下,建议使用 Murmur3_32Hash。

2、Non-persistent topics 非持久性主题

        默认情况下,Pulsar 将所有未确认的消息持久存储在多个 BookKeeper(存储节点)上。因此,持久主题上的消息数据可以在代理(broker)重新启动和消费者故障切换后继续存在。

        然而,Pulsar 也支持非持久性主题。这些主题上的消息不会持久化到磁盘,而只存在于内存中。当使用非持久性主题时,停止 Pulsar 代理或断开主题订阅者的连接,意味着该(非持久性)主题上所有传输中的消息都会丢失,意味着这些消息丢失客户端也可能会看到。

        非持久性主题的名称格式如下所示(请注意名称中的非持久性):

non-persistent://tenant/namespace/topic

        更多非持久性主题的详细描述,请单击这里

        非持久性主题消去除了消息持久化的步骤,所以在一些情况下会比持久化消息更加快一点,但是这样做,会毁损失一部分 pulsar 的核心优势。

3、System topic 系统主题

        系统主题是 Pulsar 内部使用的预定义主题。它可以是持久性主题,也可以是非持久性主题。

        系统主题用于实现已经确定的功能并用来消除对第三方组件的依赖,比如,事务、心跳检测、主题策略和资源分组服务。系统主题可以使这些功能的实现变得更简单、可靠和灵活。以心跳检测(heartbeat detections)为例,您可以充分的利用系统主题来进行心跳检测,让内部的 producer/reader 可以去指定的心跳命名空间下 produce/consume 消息,通过这种方式可以检测当前的服务是否仍处于活动状态。

        不同的命名空间,有不同的系统主题。下表概述了每个命名空间中的可用系统主题:

NamespaceTopicNameDomainCountUsage
pulsar/systemtransaction_coordinator_assign_${id}PersistentDefault 16Transaction coordinator
pulsar/system_transaction_log${tc_id}PersistentDefault 16Transaction log
pulsar/systemresource-usageNon-persistentDefault 4Resource group service
host/portheartbeatPersistent1

Heartbeat detection

心跳检测

User-defined-ns__change_eventsPersistentDefault 4Topic events
User-defined-ns__transaction_buffer_snapshotPersistentOne per namespaceTransaction buffer snapshots
User-defined-ns${topicName}__transaction_pending_ackPersistentOne per every topic subscription acknowledged with transactionsAcknowledgements with transactions

注意事项

  1. 系统主题无法被用户创建
  2. 默认情况下,系统主题是被禁用的,如果你想要启用系统主题,可以在 conf / broker.conf 或者 conf / standalone.conf 文件中进行如下配置:
systemTopicEnabled=true
topicLevelPoliciesEnabled=true

点击回到首页

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值