|
| 1 | +# PIP-386: Add resetIncludeHead in CommandSubscribe for startMessageIdInclusive implementation |
| 2 | + |
| 3 | +# Motivation |
| 4 | + |
| 5 | +This pip is intended to fix issue https://github.com/apache/pulsar/issues/23239. |
| 6 | + |
| 7 | +In the previous implementation of the method startMessageIdInclusive (https://github.com/apache/pulsar/pull/4331), |
| 8 | +we added startMessageIdInclusive() to support include current position of reset on ReaderBuilder. |
| 9 | + |
| 10 | +However, the condition `if (((BatchMessageIdImpl) msgId).getBatchIndex() >= 0)` in PersistentTopic#getNonDurableSubscription was directly removed. |
| 11 | +When we use the NonDurableSubscription, this caused the entryId to decrease by 1 for non-batch messages, |
| 12 | +resulting in wrong msgBackLog after topic unload for non-durable subscription. |
| 13 | + |
| 14 | +# Goals |
| 15 | + |
| 16 | +Add resetIncludeHead in CommandSubscribe to implement startMessageIdInclusive, and fix the NonDurable Subscription msgBackLog incorrect after topic unload |
| 17 | + |
| 18 | +# High Level Design |
| 19 | + |
| 20 | +# Detailed Design |
| 21 | + |
| 22 | +## Design & Implementation Details |
| 23 | + |
| 24 | +- CommandSubscribe add the field **resetIncludeHead**, when use the ConsumerBuilder#startMessageIdInclusive or ReaderBuilder#startMessageIdInclusive this param is true, otherwise it is false. |
| 25 | +- PersistTopic#getNonDurableSubscription add the judge condition `(msgId.getBatchIndex() >= 0 || resetIncludeHead)`, entryId - 1 will execute **when msg is batch or the resetIncludeHead is true.** |
| 26 | + |
| 27 | + |
| 28 | +```java |
| 29 | + if (ledgerId >= 0 && entryId >= 0 |
| 30 | + && msgId instanceof BatchMessageIdImpl |
| 31 | + && (msgId.getBatchIndex() >= 0 || resetIncludeHead)) { |
| 32 | + // When the start message is relative to a batch, we need to take one step back on the previous |
| 33 | + // message, |
| 34 | + // because the "batch" might not have been consumed in its entirety. |
| 35 | + // The client will then be able to discard the first messages if needed. |
| 36 | + entryId = msgId.getEntryId() - 1; |
| 37 | + } |
| 38 | +``` |
| 39 | + |
| 40 | + |
| 41 | +### Binary protocol |
| 42 | + |
| 43 | +Add `reset_include_head` field to the `CommandSubscribe`. |
| 44 | + |
| 45 | +```protobuf |
| 46 | +PulsarApi.proto |
| 47 | +
|
| 48 | +message CommandSubscribe { |
| 49 | + enum SubType { |
| 50 | + Exclusive = 0; |
| 51 | + Shared = 1; |
| 52 | + Failover = 2; |
| 53 | + Key_Shared = 3; |
| 54 | + } |
| 55 | + required string topic = 1; |
| 56 | + required string subscription = 2; |
| 57 | + required SubType subType = 3; |
| 58 | +
|
| 59 | + required uint64 consumer_id = 4; |
| 60 | + required uint64 request_id = 5; |
| 61 | + optional string consumer_name = 6; |
| 62 | + optional int32 priority_level = 7; |
| 63 | +
|
| 64 | + // Signal wether the subscription should be backed by a |
| 65 | + // durable cursor or not |
| 66 | + optional bool durable = 8 [default = true]; |
| 67 | +
|
| 68 | + // If specified, the subscription will position the cursor |
| 69 | + // markd-delete position on the particular message id and |
| 70 | + // will send messages from that point |
| 71 | + optional MessageIdData start_message_id = 9; |
| 72 | +
|
| 73 | + /// Add optional metadata key=value to this consumer |
| 74 | + repeated KeyValue metadata = 10; |
| 75 | +
|
| 76 | + optional bool read_compacted = 11; |
| 77 | +
|
| 78 | + optional Schema schema = 12; |
| 79 | + enum InitialPosition { |
| 80 | + Latest = 0; |
| 81 | + Earliest = 1; |
| 82 | + } |
| 83 | + // Signal whether the subscription will initialize on latest |
| 84 | + // or not -- earliest |
| 85 | + optional InitialPosition initialPosition = 13 [default = Latest]; |
| 86 | +
|
| 87 | + // Mark the subscription as "replicated". Pulsar will make sure |
| 88 | + // to periodically sync the state of replicated subscriptions |
| 89 | + // across different clusters (when using geo-replication). |
| 90 | + optional bool replicate_subscription_state = 14; |
| 91 | +
|
| 92 | + // If true, the subscribe operation will cause a topic to be |
| 93 | + // created if it does not exist already (and if topic auto-creation |
| 94 | + // is allowed by broker. |
| 95 | + // If false, the subscribe operation will fail if the topic |
| 96 | + // does not exist. |
| 97 | + optional bool force_topic_creation = 15 [default = true]; |
| 98 | +
|
| 99 | + // If specified, the subscription will reset cursor's position back |
| 100 | + // to specified seconds and will send messages from that point |
| 101 | + optional uint64 start_message_rollback_duration_sec = 16 [default = 0]; |
| 102 | +
|
| 103 | + optional KeySharedMeta keySharedMeta = 17; |
| 104 | +
|
| 105 | + repeated KeyValue subscription_properties = 18; |
| 106 | +
|
| 107 | + // The consumer epoch, when exclusive and failover consumer redeliver unack message will increase the epoch |
| 108 | + optional uint64 consumer_epoch = 19; |
| 109 | +
|
| 110 | + optional bool reset_include_head = 20 [default = false]; |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | + |
| 115 | +# Links |
| 116 | + |
| 117 | +* Mailing List discussion thread: |
| 118 | +* Mailing List voting thread: |
0 commit comments