Skip to content

[improve][client] PIP-224 Part 1: Add TopicMessageId for seek and acknowledge - #19158

Merged
BewareMyPower merged 3 commits into
apache:masterfrom
BewareMyPower:bewaremypower/pip-224-topic-msg-id
Jan 31, 2023
Merged

BewareMyPower merged 3 commits into
apache:masterfrom
BewareMyPower:bewaremypower/pip-224-topic-msg-id

Conversation

@BewareMyPower

Copy link
Copy Markdown
Contributor

Master Issue: #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.

Does this pull request potentially affect one of the following parts:

If the box was checked, please highlight the changes

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

Matching PR in forked repository

PR in forked repository: BewareMyPower#17

@BewareMyPower BewareMyPower self-assigned this Jan 9, 2023
@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Jan 9, 2023
@BewareMyPower BewareMyPower added type/feature The PR added a new feature or issue requested a new feature and removed doc-not-needed Your PR changes do not impact docs labels Jan 9, 2023
@BewareMyPower BewareMyPower added this to the 2.12.0 milestone Jan 9, 2023
@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Jan 9, 2023
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.
@BewareMyPower
BewareMyPower force-pushed the bewaremypower/pip-224-topic-msg-id branch from 30cbbb9 to 4042a95 Compare January 16, 2023 03:06
@BewareMyPower

Copy link
Copy Markdown
Contributor Author

*/
String getOwnerTopic();

static TopicMessageId create(String topic, MessageId messageId) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to an inner class.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you haven't pushed the commit?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, got it.

@Technoboy- Technoboy- left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Technoboy- Technoboy- closed this Jan 31, 2023
@Technoboy- Technoboy- reopened this Jan 31, 2023
@codecov-commenter

codecov-commenter commented Jan 31, 2023 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 52.83019% with 50 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.78%. Comparing base (c17f355) to head (51a375d).
⚠️ Report is 2910 commits behind head on master.

Files with missing lines Patch % Lines
...he/pulsar/client/impl/MultiTopicsConsumerImpl.java 41.81% 28 Missing and 4 partials ⚠️
...a/org/apache/pulsar/client/api/TopicMessageId.java 53.84% 5 Missing and 1 partial ⚠️
...ent/impl/UnAckedTopicMessageRedeliveryTracker.java 0.00% 4 Missing ⚠️
...a/org/apache/pulsar/client/impl/MessageIdImpl.java 66.66% 2 Missing and 1 partial ⚠️
...pulsar/client/impl/UnAckedTopicMessageTracker.java 0.00% 2 Missing ⚠️
.../apache/pulsar/client/impl/BatchMessageIdImpl.java 50.00% 0 Missing and 1 partial ⚠️
...va/org/apache/pulsar/client/impl/ConsumerImpl.java 92.85% 0 Missing and 1 partial ⚠️
...rg/apache/pulsar/client/impl/TopicMessageImpl.java 50.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             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     
Flag Coverage Δ
inttests 25.04% <21.69%> (+0.16%) ⬆️
systests 25.59% <12.26%> (+<0.01%) ⬆️
unittests 59.93% <44.33%> (-1.60%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
.../apache/pulsar/client/api/MessageAcknowledger.java 0.00% <ø> (ø)
...apache/pulsar/client/impl/NegativeAcksTracker.java 98.11% <100.00%> (-0.07%) ⬇️
.../apache/pulsar/client/impl/TopicMessageIdImpl.java 85.71% <100.00%> (-14.29%) ⬇️
.../apache/pulsar/functions/utils/FunctionCommon.java 60.50% <100.00%> (+0.50%) ⬆️
...a/org/apache/pulsar/websocket/ConsumerHandler.java 45.56% <100.00%> (ø)
.../apache/pulsar/client/impl/BatchMessageIdImpl.java 95.00% <50.00%> (ø)
...va/org/apache/pulsar/client/impl/ConsumerImpl.java 65.65% <92.85%> (-0.41%) ⬇️
...rg/apache/pulsar/client/impl/TopicMessageImpl.java 58.13% <50.00%> (ø)
...pulsar/client/impl/UnAckedTopicMessageTracker.java 43.75% <0.00%> (ø)
...a/org/apache/pulsar/client/impl/MessageIdImpl.java 88.29% <66.66%> (+2.12%) ⬆️
... and 3 more

... and 184 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc-not-needed Your PR changes do not impact docs ready-to-test type/feature The PR added a new feature or issue requested a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants