Transactions
事务语义允许事件流应用将消费,处理,生产消息整个过程定义为一个原子操作。 在 Pulsar 中,生产者或消费者能够处理跨多个主题和分区的消息。并确保这些消息作为一个单元被处理。
以下概念可以帮助你了解 Pulsar 事务。
事务协调者和事务日志
事务调度器负责维护在事务中互相关联的主题和订阅信息。 事务提交后,事务调度器与主题所在的broker交互完成事务。
The transaction coordinator maintains the entire life cycle of transactions, and prevents a transaction from incorrect status.
The transaction coordinator handles transaction timeout, and ensures that the transaction is aborted after a transaction timeout.
All the transaction metadata is persisted in the transaction log. The transaction log is backed by a Pulsar topic. After the transaction coordinator crashes, it can restore the transaction metadata from the transaction log.
事务 ID
The transaction ID (TxnID) identifies a unique transaction in Pulsar. The transaction ID is 128-bit. The highest 16 bits are reserved for the ID of the transaction coordinator, and the remaining bits are used for monotonically increasing numbers in each transaction coordinator. It is easy to locate the transaction crash with the TxnID.
事务缓存
Messages produced within a transaction are stored in the transaction buffer. The messages in transaction butter are not materialized (visible) to consumers until the transactions are committed. The messages in the transaction buffer are discarded when the transactions are aborted.
待确认状态
Message acknowledges within a transaction are maintained by the pending acknowledge state before the transaction completes. If a message is in the pending acknowledge state, the message cannot be acknowledged by other transactions until the message is removed from the pending acknowledge state.
The pending acknowledge state is persisted to the pending acknowledge log. The pending acknowledge log is backed by a Pulsar Topic. A new broker can restore the state from the pending acknowledge log to ensure the acknowledgement is not lost.