Repository navigation
[improve][client] PIP-224 Part 1: Add TopicMessageId for seek and acknowledge - #19158
BewareMyPower merged 3 commits into
Conversation
Master Issue: apache#18616 ### Motivation Introduce `TopicMessageId` to support getting the owner topic of a `MessageId`. When a `MessageId` is retrieved from a received message, the owner topic will be correctly set by the client library. When it's returned by `Producer#send`, this PR provides a `TopicMessageId#create` method to configure the owner topic. `acknowledge` APIs are affected only for the error cases: when a `MessageId` other than a `TopicMessageId` is accepted on a multi-topics consumer, `PulsarClientException.NotAllowedException` will be thrown. The semantic of the `seek(MessageId)` API is changed. Now if a `TopicMessageId` is accepted on a multi-topics consumer, the seek behavior will happen on the internal consumer of the owner topic. ### Modifications - Add the `TopicMessageId` interface. - In `MultiTopicsConsumerImpl#doAcknowledge`, complete the future with `NotAllowedException` if the argument is not a `TopicMessageId`. - In `MultiTopicsConsumerImpl#seekAsync`, when the argument is a `TopicMessageId`, find the internal consumer according to the owner topic and pass the argument to it if it exists. - In `ConsumerImpl#seekAsync`, get the inner message ID of the `TopicMessageId` so that now a single-topic consumer can also accept a `TopicMessageId` to seek. Besides the main modifications above, this patch does some refactorings to avoid direct access to `TopicMessageIdImpl`: - Deprecated `getTopicName` method by trimming the partition suffix of the owner topic in `getOriginTopicNameStr`. - Deprecated `getTopicPartitionName` by `getOwnerTopic`. - `getInnerMessageId` cannot be deprecated because we still need to convert `TopicMessageId` to `MessageIdImpl` in many cases (because we cannot get the fields like ledger id). Instead of deprecating it, use `MessageIdImpl.convertToMessageIdImpl` to replace it. - In `convertToMessageIdImpl`, for a customized `TopicMessageId` implementation, use serialization and deserialization to get the `MessageIdImpl` object. ### Verifications Add the following tests to `MultiTopicsConsumerTest`: - `testAcknowledgeWrongMessageId`: verify the correct exceptions are thrown in `acknowledge` APIs - `testSeekCustomTopicMessageId`: verify the new seek semantics for a `TopicMessageId`, including the existing `TopicMessageIdImpl` and the customized implementation by `TopicMessageId#create` ### TODO - Add a standard SerDes class for `TopicMessageId` - Apply `TopicMessageId` into `getLastMessageId` related APIs. - Deprecate the `getInnerMessageId` after PIP-229 is approved.
30cbbb9 to
4042a95
Compare
| */ | ||
| String getOwnerTopic(); | ||
|
|
||
| static TopicMessageId create(String topic, MessageId messageId) { |
There was a problem hiding this comment.
We will create an anonymous implementation here. Is it better to have an internal static class? e.g., TopicMessageIdBase or TopicMessageIdWrapper, and then the TopicMessageIdImpl can extend the base class.
We still need the messageId reference. Otherwise, it's not easy to get the actual message ID in the heap dump.
There was a problem hiding this comment.
I changed it to an inner class.
There was a problem hiding this comment.
Looks like you haven't pushed the commit?
There was a problem hiding this comment.
Oh, sorry, I pushed it now.
| * @throws PulsarClientException.AlreadyClosedException} | ||
| * if the consumer was already closed | ||
| * @throws PulsarClientException.NotAllowedException | ||
| * if `messageId` is not a {@link TopicMessageId} when multiple topics are subscribed |
There was a problem hiding this comment.
And is it also for the partitioned topic? Users need to use
MessageId.fromByteArrayWithTopic();or
TopicMessageId.create(topic, MessageId.fromByteArray());Does the TopicMessageId interface is intentionally exposed to users? Which one should be suggested in the future?
There was a problem hiding this comment.
Does the TopicMessageId interface is intentionally exposed to users?
Yes. The new getLastTopicMessageId APIs will return TopicMessageId instances, they will be introduced in another PR, see the original PIP issue.. And users can avoid touching the pulsar-client module (internal implementations) to get the topic name of a message ID.
if (msgId instanceof TopicMessageId) {
multiTopicsConsumer.seek(msgId);
} else {
multiTopicsConsumer.seek(TopicMessageId.create(topic, msgId));
}Which one should be suggested in the future?
fromByteArrayWithTopic should be deprecated. The MessageId that is not a TopicMessageId could be retrieved from other ways, not only the deserialization. But fromByteArrayWithTopic assumes it (e.g. a MessageIdImpl) was retrieved from deserialization.
There was a problem hiding this comment.
Does the TopicMessageId interface is intentionally exposed to users?
Yes. The new getLastTopicMessageId APIs will return TopicMessageId instances, they will be introduced in another PR, see the original PIP issue.. And users can avoid touching the pulsar-client module (internal implementations) to get the topic name of a message ID.
if (msgId instanceof TopicMessageId) {
multiTopicsConsumer.seek(msgId);
} else {
multiTopicsConsumer.seek(TopicMessageId.create(topic, msgId));
}Which one should be suggested in the future?
fromByteArrayWithTopic should be deprecated. The MessageId that is not a TopicMessageId could be retrieved from other ways, not only the deserialization. But fromByteArrayWithTopic assumes it (e.g. a MessageIdImpl) was retrieved from deserialization.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #19158 +/- ##
============================================
- Coverage 64.33% 62.78% -1.56%
+ Complexity 26041 25551 -490
============================================
Files 1818 1819 +1
Lines 133110 133134 +24
Branches 14644 14647 +3
============================================
- Hits 85641 83582 -2059
- Misses 39640 41841 +2201
+ Partials 7829 7711 -118
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Master Issue: #18616
Motivation
Introduce
TopicMessageIdto support getting the owner topic of aMessageId. When aMessageIdis retrieved from a received message, the owner topic will be correctly set by the client library. When it's returned byProducer#send, this PR provides aTopicMessageId#createmethod to configure the owner topic.acknowledgeAPIs are affected only for the error cases: when aMessageIdother than aTopicMessageIdis accepted on a multi-topics consumer,PulsarClientException.NotAllowedExceptionwill be thrown.The semantic of the
seek(MessageId)API is changed. Now if aTopicMessageIdis accepted on a multi-topics consumer, the seek behavior will happen on the internal consumer of the owner topic.Modifications
TopicMessageIdinterface.MultiTopicsConsumerImpl#doAcknowledge, complete the future withNotAllowedExceptionif the argument is not aTopicMessageId.MultiTopicsConsumerImpl#seekAsync, when the argument is aTopicMessageId, find the internal consumer according to the owner topic and pass the argument to it if it exists.ConsumerImpl#seekAsync, get the inner message ID of theTopicMessageIdso that now a single-topic consumer can also accept aTopicMessageIdto seek.Besides the main modifications above, this patch does some refactorings to avoid direct access to
TopicMessageIdImpl:getTopicNamemethod by trimming the partition suffix of the owner topic ingetOriginTopicNameStr.getTopicPartitionNamebygetOwnerTopic.getInnerMessageIdcannot be deprecated because we still need to convertTopicMessageIdtoMessageIdImplin many cases (because we cannot get the fields like ledger id). Instead of deprecating it, useMessageIdImpl.convertToMessageIdImplto replace it.convertToMessageIdImpl, for a customizedTopicMessageIdimplementation, use serialization and deserialization to get theMessageIdImplobject.Verifications
Add the following tests to
MultiTopicsConsumerTest:testAcknowledgeWrongMessageId: verify the correct exceptions are thrown inacknowledgeAPIstestSeekCustomTopicMessageId: verify the new seek semantics for aTopicMessageId, including the existingTopicMessageIdImpland the customized implementation byTopicMessageId#createTODO
TopicMessageIdTopicMessageIdintogetLastMessageIdrelated APIs.getInnerMessageIdafter PIP-229 is approved.Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes
Documentation
docdoc-requireddoc-not-neededdoc-completeMatching PR in forked repository
PR in forked repository: BewareMyPower#17