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 checksum calculation bug when the payload is a CompositeByteBuf with readerIndex > 0 by lhotari · Pull Request #4196 · apache/bookkeeper

Conversation

@lhotari

@lhotari lhotari commented Jan 31, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Motivation

There's a checksum calculation bug when the payload is a CompositeByteBuf with readerIndex > 0.
This bug is demonstrated in the included repro test case in commit 9fb47f5.

The problematic solution to unwrap CompositeByteBuf was introduced in PRs #2701.
PR #2701 was added to resolve a problem described in this Apache Pulsar PR: apache/pulsar#10330 . "... BookKeeper's checksum logic, the nioBuffer() method will be called and the CompositeByteBuf's internal buffers will be concatenated into a single buffer in heap memory..."

Changes

Revert the unsafe ways to "unwrap" Netty ByteBuf instances and access CompositeByteBuf directly without taking the readerIndex state into account. This approach was introduced in PRs #2701.

Netty doesn't have a public API for visiting the sub buffers, but there is a way to do this using the "ByteBuf.getBytes" method. That is the approach used in this PR.

The reason to access the underlying buffers is to ensure that no extra copies will have to be made when calculating the checksum. This PR doesn't cause performance regressions, but improves performance since there were code paths in DigestManager where the previous buggy solution for checksum calculation optimization wasn't used.

Sorry, something went wrong.

Uh oh!

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

StevenLuMT reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
lhotari added 6 commits January 31, 2024 02:10
@lhotari
Add a test that reproduces a bug in checksum calculation
9fb47f5
@lhotari
Revert "Fixed unnecessary copy to heap (apache#2701)" changes to Byte…
0707710
…BufList

This partially reverts commit 3c9c710.
@lhotari
Remove CompositeBuffer unwrapping in DigestManager
fe9301d
@lhotari
Rename update -> internalUpdate so that unwrapping logic could be add…
532de4f
…ed to update
@lhotari
Remove unnecessary unwrapping logic in Java9IntHash
9a39d6b
@lhotari
Add safe way to handle CompositeByteBuf
986942d
@lhotari

lhotari commented Jan 31, 2024

Copy link
Copy Markdown
Member Author

@hangc0276 @poorbarcode @BewareMyPower Please review

BewareMyPower and hangc0276 reacted with eyes emoji
All reactions
  • 👀 2 reactions

Sorry, something went wrong.

Uh oh!

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

@lhotari lhotari mentioned this pull request Jan 31, 2024
fix calculate checkSum when using Java9IntHash #4140
Merged
lhotari added 2 commits January 31, 2024 06:13
@lhotari
Add license header
0669a66
@lhotari
Fix checkstyle
4036a3a
BewareMyPower
BewareMyPower reviewed Jan 31, 2024
View reviewed changes
Comment thread bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/checksum/DigestManager.java Resolved

Uh oh!

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

@lhotari
Refactor ByteBuf visitor solution
7a8c200
@lhotari
lhotari requested a review from BewareMyPower January 31, 2024 15:04
@lhotari

lhotari commented Jan 31, 2024

Copy link
Copy Markdown
Member Author

@merlimat @horizonzy @wenbingshen @eolivelli @dlg99 @nicoloboschi Please review

All reactions

Sorry, something went wrong.

Uh oh!

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

lhotari added 3 commits January 31, 2024 17:07
@lhotari
Fix checkstyle
b5b42ee
@lhotari
Reformat
81e9d2d
@lhotari
Refactor recursive visiting
6c7e6e0
@lhotari lhotari mentioned this pull request Jan 31, 2024
Add ByteBufVisitor for traversing possible array / memory address buffer components netty/netty#13820
Closed
@lhotari
Revisit equals, hashCode and toString
c19ae5d
@lhotari
lhotari force-pushed the lh-fix-compositebuffer-checksum branch from cfecfe4 to c19ae5d Compare January 31, 2024 17:29
BewareMyPower
BewareMyPower reviewed Feb 1, 2024
View reviewed changes

@BewareMyPower BewareMyPower left a comment •
edited
Loading

Uh oh!

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

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.

How about this implementation?

    final int update(int digest, ByteBuf buffer, int offset, int len) {
        if (buffer instanceof CompositeByteBuf) {
            int readerIndex = buffer.readerIndex();
            CompositeByteBuf compositeByteBuf = (CompositeByteBuf) buffer;
            for (int i = 0; i < compositeByteBuf.numComponents(); i++) {
                ByteBuf component = compositeByteBuf.component(i);
                int readableBytes = component.writerIndex() - readerIndex;
                if (readableBytes > 0) {
                    digest = internalUpdate(digest, component, readerIndex, readableBytes);
                    readerIndex = 0;
                } else {
                    readerIndex -= component.writerIndex();
                }
            }
            return digest;
        } else {
            return internalUpdate(digest, buffer, offset, len);
        }
    }

Sorry, something went wrong.

Uh oh!

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

All reactions
lhotari added 5 commits February 1, 2024 08:25
@lhotari
Refactor test case
8928f98
@lhotari
Add support for UnpooledHeapByteBuf.getBytes which passes an array
19e33ca
@lhotari
Add support for visiting buffers backed by byte[] arrays
86d002d
- getBytes calls setBytes with a byte[] argument for
  heap ByteBufs
@lhotari
Move ByteBufVisitor to org.apache.bookkeeper.util package
e793872
@lhotari
Update javadoc
6508bf5
@lhotari

lhotari commented Feb 1, 2024

Copy link
Copy Markdown
Member Author

How about this implementation?

@BewareMyPower The readerIndex state that is lost is in the wrappers. In many cases, there could be multiple layers of wrappers and the main issue is that there's no way to determine whether it's ok or not to call "unwrap". In a lot of cases, it's not ok to call "unwrap".

It's possible to use the test case in this PR and do attempts in the context of the test case to see what really happens with different approaches. The test case doesn't have all of the possible problem cases. I think that I'll add a scenario for "slice" too.

I'm fairly confident that the approach in the PR is one of rare safe ways to do "internal iteration" for ByteBuf as the Netty maintainers call it. I got some high level feedback from Netty maintainers about the approach: netty/netty#13820 . This is a gap in Netty 4. In Netty 5, there's a nice API to traverse all components.

All reactions

Sorry, something went wrong.

Uh oh!

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

@lhotari

lhotari commented Feb 1, 2024

Copy link
Copy Markdown
Member Author

@BewareMyPower I have refactored the solution to use a stateless visitor callback implementation where the state is passed around in a method call parameter. The benefit of this is that it reduces unnecessary object allocations. I'm not concerned about that specificly, but object allocations by callbacks was brought up in the discussion with the Netty maintainer. That's why I decided to handle that aspect too.

All reactions

Sorry, something went wrong.

Uh oh!

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

@lhotari

lhotari commented Feb 1, 2024

Copy link
Copy Markdown
Member Author

There's currently a failing test with heap buffers (byte[] backed buffer) and this PR is still WIP.

All reactions

Sorry, something went wrong.

Uh oh!

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

@eolivelli
eolivelli requested a review from merlimat February 1, 2024 10:30
@lhotari
Filter empty buffers and arrays in ByteBufVisitor
c215db8
eolivelli
eolivelli approved these changes Feb 5, 2024
View reviewed changes

@eolivelli eolivelli 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

great work

Sorry, something went wrong.

Uh oh!

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

All reactions
hangc0276
hangc0276 reviewed Feb 5, 2024
View reviewed changes
Comment thread bookkeeper-server/src/main/java/org/apache/bookkeeper/util/ByteBufVisitor.java
// skip visiting empty buffers
return;
}
InternalContext<T> internalContext = new InternalContext<>();

@hangc0276 hangc0276 Feb 5, 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.

visitBuffers is a high-frequency call. Can we reuse the InternalContext to reduce the object allocation?

Sorry, something went wrong.

Uh oh!

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

All reactions

@lhotari lhotari Feb 5, 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 reason to have that InternalContext is to enable sharing of all other instances. There will be only a single instance for each root level buffer so object allocation isn't a problem.

Sorry, something went wrong.

Uh oh!

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

All reactions

@lhotari lhotari Feb 5, 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.

This PR removes some allocations that were happening in the old solution so there won't be additional allocations caused by this PR. One example of removed allocations is the reduced use of slice. When you call slice on a Netty ByteBuf, it will create an instance. The solution will be passing a byte[] or a direct buffer to the checksum calculations and the existing solutions to extract to use slice to get the buffer content won't be used any more.

For example this has been creating a lot of objects when CRC32 is used:

bookkeeper/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/checksum/DirectMemoryCRC32Digest.java

Lines 55 to 58 in 2209734

// Fallback to data copy if buffer is not contiguous
byte[] b = new byte[length];
buf.getBytes(index, b, 0, length);
crcValue = (int) updateBytes.invoke(null, crcValue, b, 0, b.length);

Similarly Java9IntHash was using slice here:

bookkeeper/circe-checksum/src/main/java/com/scurrilous/circe/checksum/Java9IntHash.java

Line 123 in 2209734

buffer.slice(loopOffset, length).readBytes(b, 0, length);

JniIntHash here:

bookkeeper/circe-checksum/src/main/java/com/scurrilous/circe/checksum/JniIntHash.java

Line 51 in 2209734

return hash.resume(current, buffer.slice(offset, len).nioBuffer());

All of this is now optimized and won't end up using this code since ByteBufVisitor will be extracting (or copying) byte[] or direct buffers (memory address) without any object allocations. Previously there has been object allocations.

Sorry, something went wrong.

Uh oh!

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

All reactions

@hangc0276 hangc0276 Feb 7, 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.

I see. This PR won't introduce more object allocation. I'm not blocking this PR, and it will be perfect if we can reduce the InternalContext object allocation. For each entry, it will create two InternalContext objects.

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/util/ByteBufVisitor.java
} else if (callback.acceptsMemoryAddress(callbackContext) && visitBuffer.isDirect()
&& visitBuffer.alloc().isDirectBufferPooled()) {
// read-only buffers need to be copied before they can be directly accessed
ByteBuf copyBuffer = visitBuffer.copy(visitIndex, visitLength);

@hangc0276 hangc0276 Feb 5, 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 will copy the ByteBuf here, will this path will high-frequency called?

Sorry, something went wrong.

Uh oh!

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

All reactions

@lhotari lhotari Feb 5, 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.

When it gets here, the source buffer (visitBuffer) will need to be copied in any case.
Since this checks that the source buffer is a direct buffer and that the callback accepts a memory address buffer (direct buffer), it's better to make a direct copy than to use the thread local copy buffer (byte[]) for copying.

The last resort is copying the source buffer using the byte[] copying.

Netty won't allow visiting the backing direct buffer or backing array when the buffer is a read-only buffer. In all other cases, the ByteBufVisitor is able to visit all source buffers without copies. This is the most efficient solution that there is available.

Sorry, something went wrong.

Uh oh!

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

hangc0276 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@hangc0276

hangc0276 commented Feb 5, 2024

Copy link
Copy Markdown
Contributor

Do we need a CPU and object alloc profile to see if this change introduces any performance issues?

All reactions

Sorry, something went wrong.

Uh oh!

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

@lhotari

lhotari commented Feb 5, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

Do we need a CPU and object alloc profile to see if this change introduces any performance issues?

We can do that, however it is very likely that this improves performance instead of causing a regression.

Another important detail is that there's a clear bug in correctness and we need to get rid of the current unwrapping solution introduced in #2701. I don't think that it's acceptable leaving it as it is.

All reactions

Sorry, something went wrong.

Uh oh!

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

@lhotari

lhotari commented Feb 5, 2024

Copy link
Copy Markdown
Member Author

This issue may also be related ? apache/pulsar#21892 I got some exceptions and tracked it for quite some time @lhotari

14:29:19.961 [BookKeeperClientWorker-OrderedExecutor-2-0] ERROR org.apache.bookkeeper.common.util.SingleThreadExecutor - Error while running task: readerIndex: 31215, writerIndex: 21324 (expected: 0 <= readerIndex <= writerIndex <= capacity(65536))
java.lang.IndexOutOfBoundsException: readerIndex: 31215, writerIndex: 21324 (expected: 0 <= readerIndex <= writerIndex <= capacity(65536))
	at io.netty.buffer.AbstractByteBuf.checkIndexBounds(AbstractByteBuf.java:112) ~[io.netty-netty-buffer-4.1.104.Final.jar:4.1.104.Final]
	at io.netty.buffer.AbstractByteBuf.setIndex(AbstractByteBuf.java:144) ~[io.netty-netty-buffer-4.1.104.Final.jar:4.1.104.Final]
	at io.netty.buffer.DuplicatedByteBuf.<init>(DuplicatedByteBuf.java:56) ~[io.netty-netty-buffer-4.1.104.Final.jar:4.1.104.Final]
	at io.netty.buffer.DuplicatedByteBuf.<init>(DuplicatedByteBuf.java:42) ~[io.netty-netty-buffer-4.1.104.Final.jar:4.1.104.Final]
	at io.netty.buffer.UnpooledDuplicatedByteBuf.<init>(UnpooledDuplicatedByteBuf.java:24) ~[io.netty-netty-buffer-4.1.104.Final.jar:4.1.104.Final]
	at io.netty.buffer.AbstractPooledDerivedByteBuf$PooledNonRetainedDuplicateByteBuf.<init>(AbstractPooledDerivedByteBuf.java:164) ~[io.netty-netty-buffer-4.1.104.Final.jar:4.1.104.Final]
	at io.netty.buffer.AbstractPooledDerivedByteBuf.duplicate0(AbstractPooledDerivedByteBuf.java:157) ~[io.netty-netty-buffer-4.1.104.Final.jar:4.1.104.Final]
	at io.netty.buffer.PooledSlicedByteBuf.duplicate(PooledSlicedByteBuf.java:118) ~[io.netty-netty-buffer-4.1.104.Final.jar:4.1.104.Final]
	at io.netty.buffer.CompositeByteBuf$Component.duplicate(CompositeByteBuf.java:1947) ~[io.netty-netty-buffer-4.1.104.Final.jar:4.1.104.Final]
	at io.netty.buffer.CompositeByteBuf.component(CompositeByteBuf.java:1556) ~[io.netty-netty-buffer-4.1.104.Final.jar:4.1.104.Final]
	at org.apache.bookkeeper.proto.checksum.DigestManager.computeDigestAndPackageForSendingV2(DigestManager.java:149) ~[bookkeeper-server-4.17.0-SNAPSHOT.jar:4.17.0-SNAPSHOT]
	at org.apache.bookkeeper.proto.checksum.DigestManager.computeDigestAndPackageForSending(DigestManager.java:106) ~[bookkeeper-server-4.17.0-SNAPSHOT.jar:4.17.0-SNAPSHOT]
	at org.apache.bookkeeper.client.PendingAddOp.initiate(PendingAddOp.java:246) ~[bookkeeper-server-4.17.0-SNAPSHOT.jar:4.17.0-SNAPSHOT]
	at org.apache.bookkeeper.client.LedgerHandle.doAsyncAddEntry(LedgerHandle.java:1363) ~[bookkeeper-server-4.17.0-SNAPSHOT.jar:4.17.0-SNAPSHOT]
	at org.apache.bookkeeper.client.LedgerHandle.asyncAddEntry(LedgerHandle.java:1061) ~[bookkeeper-server-4.17.0-SNAPSHOT.jar:4.17.0-SNAPSHOT]
	at org.apache.bookkeeper.mledger.impl.OpAddEntry.initiate(OpAddEntry.java:144) ~[org.apache.pulsar-managed-ledger-3.3.0-SNAPSHOT.jar:3.3.0-SNAPSHOT]
	at org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.internalAsyncAddEntry(ManagedLedgerImpl.java:862) ~[org.apache.pulsar-managed-ledger-3.3.0-SNAPSHOT.jar:3.3.0-SNAPSHOT]
	at org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.lambda$asyncAddEntry$3(ManagedLedgerImpl.java:794) ~[org.apache.pulsar-managed-ledger-3.3.0-SNAPSHOT.jar:3.3.0-SNAPSHOT]
	at org.apache.bookkeeper.common.util.SingleThreadExecutor.safeRunTask(SingleThreadExecutor.java:137) ~[org.apache.bookkeeper-bookkeeper-common-4.16.3.jar:4.16.3]
	at org.apache.bookkeeper.common.util.SingleThreadExecutor.run(SingleThreadExecutor.java:107) ~[org.apache.bookkeeper-bookkeeper-common-4.16.3.jar:4.16.3]
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[io.netty-netty-common-4.1.104.Final.jar:4.1.104.Final]
	at java.lang.Thread.run(Thread.java:833) ~[?:?]

@graysonzeng This looks like a multi-threading / thread-safety issue. Possibly multiple threads are mutating the same Netty ByteBuf instance. ByteBuf isn't thread safe. It might not be related to the problem that this PR is fixing.

All reactions

Sorry, something went wrong.

Uh oh!

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

BewareMyPower
BewareMyPower approved these changes Feb 6, 2024
View reviewed changes
hangc0276
hangc0276 approved these changes Feb 7, 2024
View reviewed changes
merlimat
merlimat approved these changes Feb 7, 2024
View reviewed changes
@merlimat merlimat added this to the 4.17.0 milestone Feb 7, 2024
@merlimat
merlimat merged commit 9c373f7 into apache:master Feb 7, 2024
lhotari added a commit to lhotari/bookkeeper that referenced this pull request Feb 8, 2024
@lhotari
Fix checksum calculation bug when the payload is a CompositeByteBuf w…
304aa1a
…ith readerIndex > 0 (apache#4196)

* Add a test that reproduces a bug in checksum calculation

* Revert "Fixed unnecessary copy to heap (apache#2701)" changes to ByteBufList

This partially reverts commit 3c9c710.

* Remove CompositeBuffer unwrapping in DigestManager

* Rename update -> internalUpdate so that unwrapping logic could be added to update

* Remove unnecessary unwrapping logic in Java9IntHash

* Add safe way to handle CompositeByteBuf

* Add license header

* Fix checkstyle

* Refactor ByteBuf visitor solution

* Fix checkstyle

* Reformat

* Refactor recursive visiting

* Revisit equals, hashCode and toString

* Refactor test case

* Add support for UnpooledHeapByteBuf.getBytes which passes an array

* Add support for visiting buffers backed by byte[] arrays

- getBytes calls setBytes with a byte[] argument for
  heap ByteBufs

* Move ByteBufVisitor to org.apache.bookkeeper.util package

* Update javadoc

* Refactor to use stateless visitor so that instance can be shared

* Improve test so that a single scenario can be used for debugging

* Fix bug in Java9IntHash calculation that assumed crc32c_update(x) == ~crc32c_update(~x)

- Java9IntHash uses private methods from java.util.zip.CRC32C class,
  updateBytes and updateDirectByteBuffer.
  When inspecting the use and interface contract, it doesn't match
  how it is used in Java9IntHash. This PR addresses that by introducing
  a separate initial value for initializing the accumulated value
  so that the initial value could match the logic in
  java.util.zip.CRC32C.reset method. There's also a separate
  method for finalizing the accumulated value into a final
  checksum value. This is to match the java.util.zip.CRC32C.getValue
  method's logic (uses bitwise complement operator ~).

- With a quick glance, it might appear that the previous logic is similar.
  However it isn't since I have a failing test which gets fixed with this
  change. I haven't yet added the Java9IntHash level unit test case to prove how
  it differs. It must be related to integer value overflow. For the CRC32C function,
  I believe it means that it cannot be assumed in all cases that
  func(x) == ~func(~x). That's the assumption that the previous code was making.
  It probably applies for many inputs, but not all. It would break in overflow
  cases.

* Fix checkstyle

* Fix checkstyle

* Fix missing depth increment that prevents StackOverflowException

* Properly handle the depth increase and decrease

* Remove unnecessary condition

* Use more efficient way to read bytes to the target array

* Don't use ByteBufVisitor if it's not necessary

* Revert "Fix bug in Java9IntHash calculation that assumed crc32c_update(x) == ~crc32c_update(~x)"

This reverts commit 272e962.

* Fix issue in resume byte[] version that was added

- input and output should be complemented. explanation has been added to the
  resume ByteBuf method

* Polish ByteBufVisitor

- reuse GetBytesCallbackByteBuf instance for handling the root ByteBuf instance

* Use extracted method

* Fix bug with array handling

* Polish ByteBufVisitor

* Optimize the buffer copying in the case where array or memory address cannot be accessed

- read-only buffers will need to be copied before reading
  - use ByteBuf.copy for direct buffers with pooled allocator when the algorithm can accept
    a memory address buffer
- use the 64kB threadlocal byte[] buffer for copying all other inputs

* Check if memory address is accepted

* Improve comments about complement (current = ~current) in resume

* Print thread dump when build is cancelled

* Filter empty buffers and arrays in ByteBufVisitor

(cherry picked from commit 9c373f7)
@lhotari lhotari mentioned this pull request Feb 8, 2024
[branch-4.16] Fix checksum calculation bug when the payload is a CompositeByteBuf with readerIndex > 0 (#4196) #4205
Merged
merlimat pushed a commit that referenced this pull request Feb 9, 2024
@lhotari
Fix checksum calculation bug when the payload is a CompositeByteBuf w…
1e89151
…ith readerIndex > 0 (#4196) (#4205)

* Add a test that reproduces a bug in checksum calculation

* Revert "Fixed unnecessary copy to heap (#2701)" changes to ByteBufList

This partially reverts commit 3c9c710.

* Remove CompositeBuffer unwrapping in DigestManager

* Rename update -> internalUpdate so that unwrapping logic could be added to update

* Remove unnecessary unwrapping logic in Java9IntHash

* Add safe way to handle CompositeByteBuf

* Add license header

* Fix checkstyle

* Refactor ByteBuf visitor solution

* Fix checkstyle

* Reformat

* Refactor recursive visiting

* Revisit equals, hashCode and toString

* Refactor test case

* Add support for UnpooledHeapByteBuf.getBytes which passes an array

* Add support for visiting buffers backed by byte[] arrays

- getBytes calls setBytes with a byte[] argument for
  heap ByteBufs

* Move ByteBufVisitor to org.apache.bookkeeper.util package

* Update javadoc

* Refactor to use stateless visitor so that instance can be shared

* Improve test so that a single scenario can be used for debugging

* Fix bug in Java9IntHash calculation that assumed crc32c_update(x) == ~crc32c_update(~x)

- Java9IntHash uses private methods from java.util.zip.CRC32C class,
  updateBytes and updateDirectByteBuffer.
  When inspecting the use and interface contract, it doesn't match
  how it is used in Java9IntHash. This PR addresses that by introducing
  a separate initial value for initializing the accumulated value
  so that the initial value could match the logic in
  java.util.zip.CRC32C.reset method. There's also a separate
  method for finalizing the accumulated value into a final
  checksum value. This is to match the java.util.zip.CRC32C.getValue
  method's logic (uses bitwise complement operator ~).

- With a quick glance, it might appear that the previous logic is similar.
  However it isn't since I have a failing test which gets fixed with this
  change. I haven't yet added the Java9IntHash level unit test case to prove how
  it differs. It must be related to integer value overflow. For the CRC32C function,
  I believe it means that it cannot be assumed in all cases that
  func(x) == ~func(~x). That's the assumption that the previous code was making.
  It probably applies for many inputs, but not all. It would break in overflow
  cases.

* Fix checkstyle

* Fix checkstyle

* Fix missing depth increment that prevents StackOverflowException

* Properly handle the depth increase and decrease

* Remove unnecessary condition

* Use more efficient way to read bytes to the target array

* Don't use ByteBufVisitor if it's not necessary

* Revert "Fix bug in Java9IntHash calculation that assumed crc32c_update(x) == ~crc32c_update(~x)"

This reverts commit 272e962.

* Fix issue in resume byte[] version that was added

- input and output should be complemented. explanation has been added to the
  resume ByteBuf method

* Polish ByteBufVisitor

- reuse GetBytesCallbackByteBuf instance for handling the root ByteBuf instance

* Use extracted method

* Fix bug with array handling

* Polish ByteBufVisitor

* Optimize the buffer copying in the case where array or memory address cannot be accessed

- read-only buffers will need to be copied before reading
  - use ByteBuf.copy for direct buffers with pooled allocator when the algorithm can accept
    a memory address buffer
- use the 64kB threadlocal byte[] buffer for copying all other inputs

* Check if memory address is accepted

* Improve comments about complement (current = ~current) in resume

* Print thread dump when build is cancelled

* Filter empty buffers and arrays in ByteBufVisitor

(cherry picked from commit 9c373f7)
This was referenced Feb 23, 2024
[Bug] [Broker] Entry digest does not match apache/pulsar#22103
Open
[improve][misc][WIP] Detect "double release" and "use after release" bugs with recycled objects apache/pulsar#22110
Closed
@lhotari lhotari mentioned this pull request Mar 29, 2024
[Bug] Bookkeeper returns mac mismatch while reading apache/pulsar#21421
Closed
1 of 2 tasks
@lhotari lhotari mentioned this pull request Apr 11, 2024
[improve][misc] Upgrade to Bookkeeper 4.16.5 apache/pulsar#22484
Merged
1 of 4 tasks
@StevenLuMT

StevenLuMT commented Apr 16, 2024

Copy link
Copy Markdown
Member

Good work

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
This was referenced May 17, 2024
ByteBuf returned by BookKeeper triggers CRC Checksum calculation when calling "readBytes" #4372
Open
Remove unused code from ByteBufVisitor #4383
Merged
hezhangjian pushed a commit that referenced this pull request May 24, 2024
@lhotari
Remove unused code from ByteBufVisitor (#4383)
84fd255
### Motivation

ByteBufVisitor added in #4196 contains some code that isn't used and covered by unit tests. 
It's better to remove such code since it makes it harder to reason about the solution. The ByteBufVisitor solution will unwrap all direct buffers that can be unwrapped without the code that is to be removed. I believe that I forgot this code from some earlier phase of the solution where it was necessary to include this.
I ran some local tests and didn't see that the code was used for the original purpose that it was added for (supporting read-only buffers).

### Changes

Remove the unused code.
hezhangjian pushed a commit that referenced this pull request May 25, 2024
@lhotari @hezhangjian
Remove unused code from ByteBufVisitor (#4383)
cc76792
### Motivation

ByteBufVisitor added in #4196 contains some code that isn't used and covered by unit tests.
It's better to remove such code since it makes it harder to reason about the solution. The ByteBufVisitor solution will unwrap all direct buffers that can be unwrapped without the code that is to be removed. I believe that I forgot this code from some earlier phase of the solution where it was necessary to include this.
I ran some local tests and didn't see that the code was used for the original purpose that it was added for (supporting read-only buffers).

### Changes

Remove the unused code.

(cherry picked from commit 84fd255)
hezhangjian pushed a commit that referenced this pull request May 25, 2024
@lhotari @hezhangjian
Remove unused code from ByteBufVisitor (#4383)
6ea6de7
### Motivation

ByteBufVisitor added in #4196 contains some code that isn't used and covered by unit tests.
It's better to remove such code since it makes it harder to reason about the solution. The ByteBufVisitor solution will unwrap all direct buffers that can be unwrapped without the code that is to be removed. I believe that I forgot this code from some earlier phase of the solution where it was necessary to include this.
I ran some local tests and didn't see that the code was used for the original purpose that it was added for (supporting read-only buffers).

### Changes

Remove the unused code.

(cherry picked from commit 84fd255)
@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 checksum calculation bug when the payload is a CompositeByteBuf w…
42b493a
…ith readerIndex > 0 (apache#4196)

* Add a test that reproduces a bug in checksum calculation

* Revert "Fixed unnecessary copy to heap (apache#2701)" changes to ByteBufList

This partially reverts commit 3c9c710.

* Remove CompositeBuffer unwrapping in DigestManager

* Rename update -> internalUpdate so that unwrapping logic could be added to update

* Remove unnecessary unwrapping logic in Java9IntHash

* Add safe way to handle CompositeByteBuf

* Add license header

* Fix checkstyle

* Refactor ByteBuf visitor solution

* Fix checkstyle

* Reformat

* Refactor recursive visiting

* Revisit equals, hashCode and toString

* Refactor test case

* Add support for UnpooledHeapByteBuf.getBytes which passes an array

* Add support for visiting buffers backed by byte[] arrays

- getBytes calls setBytes with a byte[] argument for
  heap ByteBufs

* Move ByteBufVisitor to org.apache.bookkeeper.util package

* Update javadoc

* Refactor to use stateless visitor so that instance can be shared

* Improve test so that a single scenario can be used for debugging

* Fix bug in Java9IntHash calculation that assumed crc32c_update(x) == ~crc32c_update(~x)

- Java9IntHash uses private methods from java.util.zip.CRC32C class,
  updateBytes and updateDirectByteBuffer.
  When inspecting the use and interface contract, it doesn't match
  how it is used in Java9IntHash. This PR addresses that by introducing
  a separate initial value for initializing the accumulated value
  so that the initial value could match the logic in
  java.util.zip.CRC32C.reset method. There's also a separate
  method for finalizing the accumulated value into a final
  checksum value. This is to match the java.util.zip.CRC32C.getValue
  method's logic (uses bitwise complement operator ~).

- With a quick glance, it might appear that the previous logic is similar.
  However it isn't since I have a failing test which gets fixed with this
  change. I haven't yet added the Java9IntHash level unit test case to prove how
  it differs. It must be related to integer value overflow. For the CRC32C function,
  I believe it means that it cannot be assumed in all cases that
  func(x) == ~func(~x). That's the assumption that the previous code was making.
  It probably applies for many inputs, but not all. It would break in overflow
  cases.

* Fix checkstyle

* Fix checkstyle

* Fix missing depth increment that prevents StackOverflowException

* Properly handle the depth increase and decrease

* Remove unnecessary condition

* Use more efficient way to read bytes to the target array

* Don't use ByteBufVisitor if it's not necessary

* Revert "Fix bug in Java9IntHash calculation that assumed crc32c_update(x) == ~crc32c_update(~x)"

This reverts commit 272e962.

* Fix issue in resume byte[] version that was added

- input and output should be complemented. explanation has been added to the
  resume ByteBuf method

* Polish ByteBufVisitor

- reuse GetBytesCallbackByteBuf instance for handling the root ByteBuf instance

* Use extracted method

* Fix bug with array handling

* Polish ByteBufVisitor

* Optimize the buffer copying in the case where array or memory address cannot be accessed

- read-only buffers will need to be copied before reading
  - use ByteBuf.copy for direct buffers with pooled allocator when the algorithm can accept
    a memory address buffer
- use the 64kB threadlocal byte[] buffer for copying all other inputs

* Check if memory address is accepted

* Improve comments about complement (current = ~current) in resume

* Print thread dump when build is cancelled

* Filter empty buffers and arrays in ByteBufVisitor
Ghatage pushed a commit to sijie/bookkeeper that referenced this pull request Jul 12, 2024
@lhotari Anup Ghatage
Remove unused code from ByteBufVisitor (apache#4383)
a9524d1
### Motivation

ByteBufVisitor added in apache#4196 contains some code that isn't used and covered by unit tests. 
It's better to remove such code since it makes it harder to reason about the solution. The ByteBufVisitor solution will unwrap all direct buffers that can be unwrapped without the code that is to be removed. I believe that I forgot this code from some earlier phase of the solution where it was necessary to include this.
I ran some local tests and didn't see that the code was used for the original purpose that it was added for (supporting read-only buffers).

### Changes

Remove the unused code.
@lhotari lhotari mentioned this pull request Oct 30, 2024
[improve][client][PIP-389] Add a producer config to improve compression performance apache/pulsar#23525
Merged
1 of 15 tasks
@lhotari lhotari mentioned this pull request Sep 4, 2026
[improve][broker] Copy small entries into a flat buffer when prepending broker entry metadata apache/pulsar#26464
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@merlimat merlimat merlimat approved these changes
@hangc0276 hangc0276 hangc0276 approved these changes
@eolivelli eolivelli eolivelli approved these changes
@dlg99 dlg99 Awaiting requested review from dlg99
+1 more reviewer
@BewareMyPower BewareMyPower BewareMyPower 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

None yet

Projects

None yet

Milestone

4.17.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 @eolivelli @graysonzeng @dlg99 @hangc0276 @StevenLuMT @merlimat @BewareMyPower
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.