Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] [client] Fix memory leak when publishing encountered a corner case error #23738

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

poorbarcode
Copy link
Contributor

@poorbarcode poorbarcode commented Dec 16, 2024

Motivation

Issue 1: memory leak if get errors when publishing
Conditions:

  • Send queue is full
  • Or reaches the limitation named max message size
  • Or publishes after closing the producer
  • Or encounters an error when calling ProducerInterceptor. eligible
  • see testSendQueueIsFull, testSendMessageSizeExceeded, testSendAfterClosedProducer, and testInterceptorError

Modifications

  • Fix issues

Documentation

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

Matching PR in forked repository

PR in forked repository: x

@poorbarcode poorbarcode added type/bug The PR fixed a bug or issue reported a bug release/3.0.9 release/3.3.4 release/4.0.2 labels Dec 16, 2024
@poorbarcode poorbarcode added this to the 4.1.0 milestone Dec 16, 2024
@poorbarcode poorbarcode self-assigned this Dec 16, 2024
@github-actions github-actions bot added the doc-not-needed Your PR changes do not impact docs label Dec 16, 2024
@poorbarcode
Copy link
Contributor Author

/pulsarbot rerun-failure-checks

@poorbarcode poorbarcode changed the title [fix] [client] Fix memory leak and publish stuck when publishing [fix] [client] Fix memory leak when publishing encountered a corner case error Dec 17, 2024
Comment on lines +509 to +516
// To guarantee compatibility with customized "SendCallback", see detail the doc of this method.
if ((callback instanceof ProducerImpl.DefaultSendMessageCallback
|| callback.getClass().getName().endsWith("NonPersistentReplicator$ProducerSendCallback")
|| callback.getClass().getName().endsWith("ProducerSendCallback$ProducerSendCallback"))
&& msg.getDataBuffer().capacity() > 0) {
checkArgument(msg.getDataBuffer().refCnt() >= 2,
"Message's data's refCnt is less than 2, see #23738");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Such type assertions really should be avoided. I mean, the checkArgument(message instanceof MessageImpl) assertion before is also redundant.

The reference count validation should not be performed in sendAsync, instead, it should be validated before the caller calls sendAsync (e.g. in GeoPersistentReplicator#replicateEntries).

MsgPayloadTouchableMessageBuilder<String> msgBuilder = newMessage(producer);
producer.close();
msgBuilder.value("msg-1").sendAsync().exceptionally(ex -> {
log.warn("expected error", ex);
Copy link
Contributor

Choose a reason for hiding this comment

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

If it's an expected error, don't print warn logs. You'd better use assertThrows or a trivial try-catch:

try {
    msgBuilder.value("msg-1").send();
    fail();
} catch (PulsarClientException ignored) {
    // or you can add the type assertion here
}

Comment on lines +92 to +96
try{
sendFutureList.get(sendFutureList.size() - 1).join();
} catch (Exception ex) {
log.warn("", ex);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Which case is expected? Should it succeed or fail?

indexCalledSend++;
}
} catch (Exception ex) {
log.warn("", ex);
Copy link
Contributor

Choose a reason for hiding this comment

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

I see many places calling log.warn("", ex). If the exception is expected, please do type assertions (like assertTrue(e instanceof xxx) or assertTrue(e.getMessage().contains("xxx")) rather than printing the whole exception stack

Comment on lines +303 to +305
msgBuilder.value("msg-1").sendAsync().get(3, TimeUnit.SECONDS);
} catch (Exception ex) {
log.warn("Intercept error", ex);
Copy link
Contributor

Choose a reason for hiding this comment

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

Yet another unnecessary exception stack logs, which is noisy. And you should call fail() in the try block.

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 86.95652% with 3 lines in your changes missing coverage. Please review.

Project coverage is 74.42%. Comparing base (bbc6224) to head (f69c975).
Report is 795 commits behind head on master.

Files with missing lines Patch % Lines
...va/org/apache/pulsar/client/impl/ProducerImpl.java 81.25% 1 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             master   #23738      +/-   ##
============================================
+ Coverage     73.57%   74.42%   +0.85%     
- Complexity    32624    35114    +2490     
============================================
  Files          1877     1945      +68     
  Lines        139502   147540    +8038     
  Branches      15299    16290     +991     
============================================
+ Hits         102638   109812    +7174     
- Misses        28908    29261     +353     
- Partials       7956     8467     +511     
Flag Coverage Δ
inttests 27.43% <34.78%> (+2.85%) ⬆️
systests 24.35% <26.08%> (+0.02%) ⬆️
unittests 73.82% <86.95%> (+0.97%) ⬆️

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

Files with missing lines Coverage Δ
.../pulsar/client/impl/BatchMessageContainerImpl.java 85.39% <100.00%> (+4.49%) ⬆️
...g/apache/pulsar/client/impl/ConnectionHandler.java 86.17% <ø> (-0.60%) ⬇️
...pache/pulsar/client/impl/ProducerInterceptors.java 72.50% <100.00%> (ø)
...he/pulsar/client/impl/TypedMessageBuilderImpl.java 83.96% <100.00%> (+0.12%) ⬆️
...va/org/apache/pulsar/client/impl/ProducerImpl.java 83.67% <81.25%> (+0.07%) ⬆️

... and 664 files with indirect coverage changes

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 release/3.0.9 release/3.3.4 release/4.0.2 type/bug The PR fixed a bug or issue reported a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants