You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by "dao-jun (via GitHub)" <gi...@apache.org> on 2024/04/02 05:41:28 UTC

[PR] [improve] Prevent memory copy when read entry [bookkeeper]

dao-jun opened a new pull request, #4259:
URL: https://github.com/apache/bookkeeper/pull/4259

   ### Motivation
   
   Prevent memory copy when read entry.
   
   ### Changes
   
   (Describe: what changes you have made)
   
   Master Issue: #<master-issue-number>
   
   > ---
   > In order to uphold a high standard for quality for code contributions, Apache BookKeeper runs various precommit
   > checks for pull requests. A pull request can only be merged when it passes precommit checks.
   >
   > ---
   > Be sure to do all of the following to help us incorporate your contribution
   > quickly and easily:
   >
   > If this PR is a BookKeeper Proposal (BP):
   >
   > - [ ] Make sure the PR title is formatted like:
   >     `<BP-#>: Description of bookkeeper proposal`
   >     `e.g. BP-1: 64 bits ledger is support`
   > - [ ] Attach the master issue link in the description of this PR.
   > - [ ] Attach the google doc link if the BP is written in Google Doc.
   >
   > Otherwise:
   > 
   > - [ ] Make sure the PR title is formatted like:
   >     `<Issue #>: Description of pull request`
   >     `e.g. Issue 123: Description ...`
   > - [ ] Make sure tests pass via `mvn clean apache-rat:check install spotbugs:check`.
   > - [ ] Replace `<Issue #>` in the title with the actual Issue number.
   > 
   > ---
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [improve] Prevent memory copy and heap memory allocation when read entry [bookkeeper]

Posted by "dao-jun (via GitHub)" <gi...@apache.org>.
dao-jun commented on code in PR #4259:
URL: https://github.com/apache/bookkeeper/pull/4259#discussion_r1573265982


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/ReadEntryProcessorV3.java:
##########
@@ -170,25 +169,21 @@ protected ReadResponse readEntry(ReadResponse.Builder readResponseBuilder,
                                      boolean readLACPiggyBack,
                                      Stopwatch startTimeSw)
         throws IOException, BookieException {
-        ByteBuf entryBody = requestProcessor.getBookie().readEntry(ledgerId, entryId);
+        entryBody = requestProcessor.getBookie().readEntry(ledgerId, entryId);
         if (null != fenceResult) {
-            handleReadResultForFenceRead(entryBody, readResponseBuilder, entryId, startTimeSw);
+            handleReadResultForFenceRead(readResponseBuilder, entryId, startTimeSw);
             return null;
         } else {
-            try {
-                readResponseBuilder.setBody(ByteString.copyFrom(entryBody.nioBuffer()));
-                if (readLACPiggyBack) {
-                    readResponseBuilder.setEntryId(entryId);
-                } else {
-                    long knownLAC = requestProcessor.getBookie().readLastAddConfirmed(ledgerId);
-                    readResponseBuilder.setMaxLAC(knownLAC);
-                }
-                registerSuccessfulEvent(readStats, startTimeSw);
-                readResponseBuilder.setStatus(StatusCode.EOK);
-                return readResponseBuilder.build();
-            } finally {
-                ReferenceCountUtil.release(entryBody);
+            readResponseBuilder.setBody(UnsafeByteOperations.unsafeWrap(entryBody.nioBuffer()));

Review Comment:
   @lhotari Thanks for reminding, fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [improve] Prevent memory copy and heap memory allocation when read entry [bookkeeper]

Posted by "dao-jun (via GitHub)" <gi...@apache.org>.
dao-jun commented on code in PR #4259:
URL: https://github.com/apache/bookkeeper/pull/4259#discussion_r1573307954


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/ReadEntryProcessorV3.java:
##########
@@ -350,6 +344,43 @@ protected void registerEvent(boolean failed, OpStatsLogger statsLogger, Stopwatc
         }
     }
 
+    ByteString parseToByteString(ByteBuf entryBody) {
+        ByteString aggregated = null;
+        ByteString piece;

Review Comment:
   makes sense to me, fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [improve] Prevent memory copy when read entry [bookkeeper]

Posted by "shoothzj (via GitHub)" <gi...@apache.org>.
shoothzj commented on PR #4259:
URL: https://github.com/apache/bookkeeper/pull/4259#issuecomment-2047078648

   I am not familiar with netty reference count. I have two questions.
   1. why we need `onSendResponseFinished`? Can we just change to `UnsafeByteOperations.unsafeWrap(entryBody.nioBuffer())`? It may leak memory?
   2. I think extract a class field is a little hard to maintenance, can we still pass it by method?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [improve] Prevent memory copy and heap memory allocation when read entry [bookkeeper]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #4259:
URL: https://github.com/apache/bookkeeper/pull/4259#discussion_r1572219468


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/ReadEntryProcessorV3.java:
##########
@@ -170,25 +169,21 @@ protected ReadResponse readEntry(ReadResponse.Builder readResponseBuilder,
                                      boolean readLACPiggyBack,
                                      Stopwatch startTimeSw)
         throws IOException, BookieException {
-        ByteBuf entryBody = requestProcessor.getBookie().readEntry(ledgerId, entryId);
+        entryBody = requestProcessor.getBookie().readEntry(ledgerId, entryId);
         if (null != fenceResult) {
-            handleReadResultForFenceRead(entryBody, readResponseBuilder, entryId, startTimeSw);
+            handleReadResultForFenceRead(readResponseBuilder, entryId, startTimeSw);
             return null;
         } else {
-            try {
-                readResponseBuilder.setBody(ByteString.copyFrom(entryBody.nioBuffer()));
-                if (readLACPiggyBack) {
-                    readResponseBuilder.setEntryId(entryId);
-                } else {
-                    long knownLAC = requestProcessor.getBookie().readLastAddConfirmed(ledgerId);
-                    readResponseBuilder.setMaxLAC(knownLAC);
-                }
-                registerSuccessfulEvent(readStats, startTimeSw);
-                readResponseBuilder.setStatus(StatusCode.EOK);
-                return readResponseBuilder.build();
-            } finally {
-                ReferenceCountUtil.release(entryBody);
+            readResponseBuilder.setBody(UnsafeByteOperations.unsafeWrap(entryBody.nioBuffer()));

Review Comment:
   Calling `.nioBuffer()` might result in memory copies when the buffer is a composite buffer.
   Something in this direction would result in zero copies:
   https://github.com/apache/bookkeeper/pull/4293/files#diff-7c7c625da2136a47ce06a1753046baa3e3aeea006ef537b3fb6efd34d25000ddR851-R872
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [improve] Prevent memory copy when read entry [bookkeeper]

Posted by "dao-jun (via GitHub)" <gi...@apache.org>.
dao-jun commented on PR #4259:
URL: https://github.com/apache/bookkeeper/pull/4259#issuecomment-2039175191

   @lhotari @eolivelli @hangc0276 @zymap Could you please take a look?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [improve] Prevent memory copy when read entry [bookkeeper]

Posted by "dao-jun (via GitHub)" <gi...@apache.org>.
dao-jun commented on PR #4259:
URL: https://github.com/apache/bookkeeper/pull/4259#issuecomment-2047118889

   > I am not familiar with netty reference count. I have two questions.
   > 
   > 1. why we need `onSendResponseFinished`? Can we just change to `UnsafeByteOperations.unsafeWrap(entryBody.nioBuffer())`? It may leak memory?
   > 2. I think extract a class field is a little hard to maintenance, can we still pass it by method?
   
   @shoothzj 
   1. `UnsafeByteOperations` is protobuf‘s API, it will not release the memory, so we have to release it manually.
   2. Pass `body` by method will lead to more changes, this is the most simple way.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [improve] Prevent memory copy and heap memory allocation when read entry [bookkeeper]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #4259:
URL: https://github.com/apache/bookkeeper/pull/4259#discussion_r1573270871


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/ReadEntryProcessorV3.java:
##########
@@ -350,6 +344,43 @@ protected void registerEvent(boolean failed, OpStatsLogger statsLogger, Stopwatc
         }
     }
 
+    ByteString parseToByteString(ByteBuf entryBody) {
+        ByteString aggregated = null;
+        ByteString piece;

Review Comment:
   Having this variable introduced at this level isn't needed.



##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/ReadEntryProcessorV3.java:
##########
@@ -350,6 +344,43 @@ protected void registerEvent(boolean failed, OpStatsLogger statsLogger, Stopwatc
         }
     }
 
+    ByteString parseToByteString(ByteBuf entryBody) {

Review Comment:
   The `parse` naming isn't optimal. A better name is `unsafeWrapAsByteString` etc. It's also worth adding a comment that the lifecycle of the ByteString continues to be managed with the ByteBuf lifecycle (retain/release). 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org