-
Notifications
You must be signed in to change notification settings - Fork 893
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
Single buffer for small add requests #3783
Merged
Merged
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
11d5854
Single buffer for small add requests
merlimat 4fd46b0
Fixed checkstyle
merlimat 7901773
Fixed treating of ComposityByteBuf
merlimat 572310c
Merge remote-tracking branch 'apache/master' into single-buffer-add
merlimat 2b6632f
Merge remote-tracking branch 'apache/master' into single-buffer-add
merlimat 5b50e06
Fixed merge issues
merlimat 62c0bdf
Merge remote-tracking branch 'apache/master' into single-buffer-add
merlimat cc3c343
Fixed merge issues
merlimat 8791faf
WIP
merlimat ca4a555
Fixed test and removed dead code
merlimat e584323
Removed unused import
merlimat 28d3581
Fixed BookieJournalTest
merlimat 3390bc5
removed unused import
merlimat 822eb3e
fix the checkstyle
hangc0276 eeedef6
fix failed test
hangc0276 76c4768
fix failed test
hangc0276 441f522
Merge remote-tracking branch 'apache/master' into single-buffer-add
merlimat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,10 +22,13 @@ | |
import io.netty.buffer.CompositeByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.util.ReferenceCountUtil; | ||
import io.netty.util.ReferenceCounted; | ||
import java.security.GeneralSecurityException; | ||
import java.security.NoSuchAlgorithmException; | ||
import org.apache.bookkeeper.client.BKException.BKDigestMatchException; | ||
import org.apache.bookkeeper.client.LedgerHandle; | ||
import org.apache.bookkeeper.proto.BookieProtoEncoding; | ||
import org.apache.bookkeeper.proto.BookieProtocol; | ||
import org.apache.bookkeeper.proto.DataFormats.LedgerMetadataFormat.DigestType; | ||
import org.apache.bookkeeper.util.ByteBufList; | ||
import org.slf4j.Logger; | ||
|
@@ -96,14 +99,72 @@ public static byte[] generateMasterKey(byte[] password) throws NoSuchAlgorithmEx | |
* @param data | ||
* @return | ||
*/ | ||
public ByteBufList computeDigestAndPackageForSending(long entryId, long lastAddConfirmed, long length, | ||
ByteBuf data) { | ||
ByteBuf headersBuffer; | ||
public ReferenceCounted computeDigestAndPackageForSending(long entryId, long lastAddConfirmed, long length, | ||
ByteBuf data, byte[] masterKey, int flags) { | ||
if (this.useV2Protocol) { | ||
headersBuffer = allocator.buffer(METADATA_LENGTH + macCodeLength); | ||
return computeDigestAndPackageForSendingV2(entryId, lastAddConfirmed, length, data, masterKey, flags); | ||
} else { | ||
return computeDigestAndPackageForSendingV3(entryId, lastAddConfirmed, length, data); | ||
} | ||
} | ||
|
||
private ReferenceCounted computeDigestAndPackageForSendingV2(long entryId, long lastAddConfirmed, long length, | ||
ByteBuf data, byte[] masterKey, int flags) { | ||
boolean isSmallEntry = data.readableBytes() < BookieProtoEncoding.SMALL_ENTRY_SIZE_THRESHOLD; | ||
|
||
int headersSize = 4 // Request header | ||
+ BookieProtocol.MASTER_KEY_LENGTH // for the master key | ||
+ METADATA_LENGTH // | ||
+ macCodeLength; | ||
int payloadSize = data.readableBytes(); | ||
int bufferSize = 4 + headersSize + (isSmallEntry ? payloadSize : 0); | ||
|
||
ByteBuf buf = allocator.buffer(bufferSize, bufferSize); | ||
buf.writeInt(headersSize + payloadSize); | ||
buf.writeInt( | ||
BookieProtocol.PacketHeader.toInt( | ||
BookieProtocol.CURRENT_PROTOCOL_VERSION, BookieProtocol.ADDENTRY, (short) flags)); | ||
buf.writeBytes(masterKey, 0, BookieProtocol.MASTER_KEY_LENGTH); | ||
|
||
// The checksum is computed on the next part of the buffer only | ||
buf.readerIndex(buf.writerIndex()); | ||
buf.writeLong(ledgerId); | ||
buf.writeLong(entryId); | ||
buf.writeLong(lastAddConfirmed); | ||
buf.writeLong(length); | ||
|
||
// Compute checksum over the headers | ||
update(buf); | ||
|
||
// don't unwrap slices | ||
final ByteBuf unwrapped = data.unwrap() != null && data.unwrap() instanceof CompositeByteBuf | ||
? data.unwrap() : data; | ||
ReferenceCountUtil.retain(unwrapped); | ||
ReferenceCountUtil.safeRelease(data); | ||
|
||
if (unwrapped instanceof CompositeByteBuf) { | ||
((CompositeByteBuf) unwrapped).forEach(this::update); | ||
} else { | ||
headersBuffer = Unpooled.buffer(METADATA_LENGTH + macCodeLength); | ||
update(unwrapped); | ||
} | ||
|
||
populateValueAndReset(buf); | ||
|
||
// Reset the reader index to the beginning | ||
buf.readerIndex(0); | ||
|
||
if (isSmallEntry) { | ||
buf.writeBytes(data); | ||
data.release(); | ||
return buf; | ||
} else { | ||
return ByteBufList.get(buf, data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ByteBufList.get(buf, unwrap)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
} | ||
|
||
private ByteBufList computeDigestAndPackageForSendingV3(long entryId, long lastAddConfirmed, long length, | ||
ByteBuf data) { | ||
ByteBuf headersBuffer = Unpooled.buffer(METADATA_LENGTH + macCodeLength); | ||
headersBuffer.writeLong(ledgerId); | ||
headersBuffer.writeLong(entryId); | ||
headersBuffer.writeLong(lastAddConfirmed); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
buf.writeBytes(unwrap) ?