GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: reference counting (retain/release) in PerChannelBookieClient by lhotari · Pull Request #4293 · apache/bookkeeper

Conversation

@lhotari

@lhotari lhotari commented Apr 16, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Motivation

This addresses the remaining gaps of #4289 in handling ByteBuf retain/release.
This PR will also address the concern about NioBuffer lifecycle brought up in the review of the original PR review: #791 (comment) .

This PR fixes several problems:

  • ByteString buffer lifecycle in client, follows ByteBufList lifecycle
  • ByteBufList lifecycle, moved to write promise
  • Calling of write promises in AuthHandler which buffers messages while authentication is in progress. It was ignoring the promises.

Changes

  • add 2 callback parameters to writeAndFlush: cleanupActionFailedBeforeWrite and cleanupActionAfterWrite
    • use these callback actions for proper cleanup
  • extract a utility class ByteStringUtil for wrapping ByteBufList or ByteBuf as concatenated zero copy ByteString
  • properly handle releasing of ByteBufList in the write promise
  • properly handle calling promises that are buffered while authentication is in progress

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Technoboy- and hangc0276 reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
@lhotari lhotari mentioned this pull request Apr 16, 2024
[fix] Fix ByteBuf release/retain in PerChannelBookClient #4289
Merged
@lhotari
lhotari force-pushed the lh-fix-reference-counts-in-PerChannelBookieClient branch from 37fe66d to 98bc499 Compare April 16, 2024 10:55
@lhotari lhotari changed the title fix: reference counts in PerChannelBookieClient fix: reference counting (retain/release) in PerChannelBookieClient Apr 16, 2024
@lhotari
lhotari force-pushed the lh-fix-reference-counts-in-PerChannelBookieClient branch 10 times, most recently from 1a7542d to 90f9325 Compare April 16, 2024 12:05
@lhotari lhotari mentioned this pull request Apr 16, 2024
Bookkeeper client allocates a lot of short-lived objects #791
Closed
@lhotari
lhotari force-pushed the lh-fix-reference-counts-in-PerChannelBookieClient branch from 90f9325 to 77b09c2 Compare April 22, 2024 14:41
@lhotari

lhotari commented Apr 22, 2024

Copy link
Copy Markdown
Member Author

@hangc0276 @poorbarcode Please review. This addresses the remaining gaps of #4289.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lhotari

lhotari commented Apr 26, 2024

Copy link
Copy Markdown
Member Author

@hangc0276 @poorbarcode @codelipenghui @BewareMyPower @merlimat Please review. This addresses the remaining gaps of #4289.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lhotari lhotari mentioned this pull request Apr 26, 2024
[Bug] parseMessageMetadata error when broker entry metadata enable with high loading apache/pulsar#22601
Closed
2 of 3 tasks
nicoloboschi
nicoloboschi approved these changes Apr 30, 2024
View reviewed changes
hezhangjian
hezhangjian approved these changes Apr 30, 2024
View reviewed changes
dlg99
dlg99 approved these changes Apr 30, 2024
View reviewed changes

@dlg99 dlg99 left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM, just a small comment

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/BookieClientImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@hezhangjian hezhangjian added release/4.16.6 release/4.17.1 labels May 16, 2024
@lhotari lhotari mentioned this pull request May 23, 2024
[fix][broker] Fix multiple transfer corruption issues when TLS is enabled apache/pulsar#22760
Closed
1 of 4 tasks
lhotari added 6 commits May 23, 2024 14:20
@lhotari
fix: reference counts in PerChannelBookieClient
b62ba86
@lhotari
Extract helper class
230271c
@lhotari
Add solution to ByteBufList.Encoder to release after the write completes
59dd9cd
@lhotari
Remove unnecessary clone of ByteBufList
d05e976
@lhotari
Fix bug in ByteStringUtil
fd5431a
@lhotari
Add ByteStringUtil optimization
6dfe88d
@lhotari
Add handling for channel promises to AuthHandler
74f9782
@lhotari
lhotari force-pushed the lh-fix-reference-counts-in-PerChannelBookieClient branch from 77b09c2 to 74f9782 Compare May 23, 2024 19:12
@lhotari

lhotari commented May 23, 2024

Copy link
Copy Markdown
Member Author

I finally found the root cause in Bookkeeper client. The client had several buffer leaks since the ByteBufList lifecycle was incorrectly handled and write promises were dropped and ignored when authentication was in progress.
@merlimat @hangc0276 @poorbarcode @eolivelli @nicoloboschi @dlg99 Please review!
I hope we could release 4.16.6 after this PR is processed, merged and cherry-picked to branch-4.16 .

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lhotari
Fix ByteBufListTest
b6127bf
merlimat
merlimat approved these changes May 23, 2024
View reviewed changes
@hezhangjian

hezhangjian commented May 23, 2024

Copy link
Copy Markdown
Member

Can we close #3902 after this pr is merged?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

poorbarcode
poorbarcode approved these changes May 24, 2024
View reviewed changes
hangc0276
hangc0276 reviewed May 24, 2024
View reviewed changes
Comment thread bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/AuthHandler.java
ChannelPromise promise;
// check if the message has an associated promise as the next element in the queue
if (waitingForAuth.peek() instanceof ChannelPromise) {
promise = (ChannelPromise) waitingForAuth.poll();

@hangc0276 hangc0276 May 24, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

If found this issue before https://github.com/apache/bookkeeper/pull/3902/files. Your solution is better.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lhotari reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java
body = UnsafeByteOperations.unsafeWrap(toSend.toArray());
}
ByteString body = ByteStringUtil.byteBufListToByteString(toSend);
toSend.retain();

@hangc0276 hangc0276 May 24, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The toSend already convert to ByteString, why do we need to retain and release after flushed no matter the flush succeed or not?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@lhotari lhotari May 24, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The ByteString doesn't handle the lifecycle of the buffers referenced by the ByteString.
It's the toSend.retain() which handles the lifecycle.
The cleanupActionFailedBeforeWrite and cleanupActionAfterWrite parameters (Runnables) are used to ensure that the reference count is properly decreased later.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@hangc0276 hangc0276 May 26, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We already converted toSend to ByteString, the lifecycle of toSend can be tracked by the caller of the method.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java
}
}
ByteString body = ByteStringUtil.byteBufListToByteString(bufToSend);
bufToSend.retain();

@hangc0276 hangc0276 May 24, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Do not need retain the bufToSend?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@lhotari lhotari May 24, 2024

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

same answer as #4293 (comment)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
hangc0276
hangc0276 approved these changes May 24, 2024
View reviewed changes
@hangc0276

hangc0276 commented May 24, 2024

Copy link
Copy Markdown
Contributor

Nice catch!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

codelipenghui
codelipenghui approved these changes May 24, 2024
View reviewed changes
@hezhangjian
hezhangjian merged commit 0ef2f99 into apache:master May 24, 2024
@hezhangjian hezhangjian mentioned this pull request May 24, 2024
Fix promise lost in ClientSide AuthHandler #3902
Closed
@lhotari lhotari mentioned this pull request May 24, 2024
[improve][misc][WIP] Detect "double release" and "use after release" bugs with recycled objects apache/pulsar#22110
Closed
1 of 4 tasks
hezhangjian pushed a commit that referenced this pull request May 25, 2024
@lhotari @hezhangjian
fix: reference counting (retain/release) in PerChannelBookieClient (#…
110d653
…4293)

### Motivation

This addresses the remaining gaps of #4289 in handling ByteBuf retain/release.
This PR will also address the concern about NioBuffer lifecycle brought up in the review of the original PR review: #791 (comment) .

This PR fixes several problems:
* ByteString buffer lifecycle in client, follows ByteBufList lifecycle
* ByteBufList lifecycle, moved to write promise
* Calling of write promises in AuthHandler which buffers messages while authentication is in progress. It was ignoring the promises.

### Changes

- add 2 callback parameters to writeAndFlush: cleanupActionFailedBeforeWrite and cleanupActionAfterWrite
  - use these callback actions for proper cleanup
- extract a utility class ByteStringUtil for wrapping ByteBufList or ByteBuf as concatenated zero copy ByteString
- properly handle releasing of ByteBufList in the write promise
- properly handle calling promises that are buffered while authentication is in progress

(cherry picked from commit 0ef2f99)
@hezhangjian hezhangjian added the cherry-picked/branch-4.17 label May 25, 2024
hangc0276
hangc0276 reviewed May 26, 2024
View reviewed changes
Comment thread bookkeeper-server/src/main/java/org/apache/bookkeeper/util/ByteBufList.java
ChannelPromise compositePromise = ctx.newPromise();
compositePromise.addListener(future -> {
// release the ByteBufList after the write operation is completed
ReferenceCountUtil.safeRelease(b);

@hangc0276 hangc0276 May 26, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Use ReferenceCountUtil.relesse(b) to expose potential exceptions in release the ByteBufList?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
zhiheng123 pushed a commit to zhiheng123/bookkeeper that referenced this pull request May 26, 2024
@lhotari @zhiheng123
fix: reference counting (retain/release) in PerChannelBookieClient (a…
9183007
…pache#4293)

This addresses the remaining gaps of apache#4289 in handling ByteBuf retain/release.
This PR will also address the concern about NioBuffer lifecycle brought up in the review of the original PR review: apache#791 (comment) .

This PR fixes several problems:
* ByteString buffer lifecycle in client, follows ByteBufList lifecycle
* ByteBufList lifecycle, moved to write promise
* Calling of write promises in AuthHandler which buffers messages while authentication is in progress. It was ignoring the promises.

- add 2 callback parameters to writeAndFlush: cleanupActionFailedBeforeWrite and cleanupActionAfterWrite
  - use these callback actions for proper cleanup
- extract a utility class ByteStringUtil for wrapping ByteBufList or ByteBuf as concatenated zero copy ByteString
- properly handle releasing of ByteBufList in the write promise
- properly handle calling promises that are buffered while authentication is in progress
lhotari added a commit to lhotari/bookkeeper that referenced this pull request May 27, 2024
@lhotari
fix: reference counting (retain/release) in PerChannelBookieClient (a…
9000a3f
…pache#4293)

### Motivation

This addresses the remaining gaps of apache#4289 in handling ByteBuf retain/release.
This PR will also address the concern about NioBuffer lifecycle brought up in the review of the original PR review: apache#791 (comment) .

This PR fixes several problems:
* ByteString buffer lifecycle in client, follows ByteBufList lifecycle
* ByteBufList lifecycle, moved to write promise
* Calling of write promises in AuthHandler which buffers messages while authentication is in progress. It was ignoring the promises.

### Changes

- add 2 callback parameters to writeAndFlush: cleanupActionFailedBeforeWrite and cleanupActionAfterWrite
  - use these callback actions for proper cleanup
- extract a utility class ByteStringUtil for wrapping ByteBufList or ByteBuf as concatenated zero copy ByteString
- properly handle releasing of ByteBufList in the write promise
- properly handle calling promises that are buffered while authentication is in progress

(cherry picked from commit 0ef2f99)

# Conflicts:
#	bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java
@lhotari lhotari mentioned this pull request May 27, 2024
[branch-4.16] fix: reference counting (retain/release) in PerChannelBookieClient (#4293) #4396
Merged
hezhangjian pushed a commit that referenced this pull request May 27, 2024
@lhotari
fix: reference counting (retain/release) in PerChannelBookieClient (#…
29a74ac
…4293) (#4396)

### Motivation

This addresses the remaining gaps of #4289 in handling ByteBuf retain/release.
This PR will also address the concern about NioBuffer lifecycle brought up in the review of the original PR review: #791 (comment) .

This PR fixes several problems:
* ByteString buffer lifecycle in client, follows ByteBufList lifecycle
* ByteBufList lifecycle, moved to write promise
* Calling of write promises in AuthHandler which buffers messages while authentication is in progress. It was ignoring the promises.

### Changes

- add 2 callback parameters to writeAndFlush: cleanupActionFailedBeforeWrite and cleanupActionAfterWrite
  - use these callback actions for proper cleanup
- extract a utility class ByteStringUtil for wrapping ByteBufList or ByteBuf as concatenated zero copy ByteString
- properly handle releasing of ByteBufList in the write promise
- properly handle calling promises that are buffered while authentication is in progress

(cherry picked from commit 0ef2f99)

# Conflicts:
#	bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java
@hezhangjian hezhangjian added the cherry-picked/branch-4.16 label May 27, 2024
@lhotari lhotari mentioned this pull request Jun 14, 2024
[Bug] Ledger can not recover with Digest Mismatch Error apache/pulsar#22906
Closed
2 of 3 tasks
Ghatage pushed a commit to sijie/bookkeeper that referenced this pull request Jul 12, 2024
@lhotari Anup Ghatage
fix: reference counting (retain/release) in PerChannelBookieClient (a…
a28837b
…pache#4293)

### Motivation

This addresses the remaining gaps of apache#4289 in handling ByteBuf retain/release.
This PR will also address the concern about NioBuffer lifecycle brought up in the review of the original PR review: apache#791 (comment) .

This PR fixes several problems:
* ByteString buffer lifecycle in client, follows ByteBufList lifecycle
* ByteBufList lifecycle, moved to write promise
* Calling of write promises in AuthHandler which buffers messages while authentication is in progress. It was ignoring the promises.

### Changes

- add 2 callback parameters to writeAndFlush: cleanupActionFailedBeforeWrite and cleanupActionAfterWrite
  - use these callback actions for proper cleanup
- extract a utility class ByteStringUtil for wrapping ByteBufList or ByteBuf as concatenated zero copy ByteString
- properly handle releasing of ByteBufList in the write promise
- properly handle calling promises that are buffered while authentication is in progress
@lhotari lhotari mentioned this pull request Mar 6, 2026
[client] fix writeLac memory leak and thread safety issue #4713
Merged
@hangc0276 hangc0276 added this to the 4.18.0 milestone May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@hangc0276 hangc0276 hangc0276 approved these changes
@merlimat merlimat merlimat approved these changes
@dlg99 dlg99 dlg99 approved these changes
@hezhangjian hezhangjian hezhangjian approved these changes
@nicoloboschi nicoloboschi nicoloboschi approved these changes
+2 more reviewers
@codelipenghui codelipenghui codelipenghui approved these changes
@poorbarcode poorbarcode poorbarcode approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

cherry-picked/branch-4.16 cherry-picked/branch-4.17 release/4.16.6 release/4.17.1

Projects

None yet

Milestone

4.18.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@lhotari @hezhangjian @hangc0276 @merlimat @dlg99 @codelipenghui @nicoloboschi @poorbarcode
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.