This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Added byteBuffer based APIs to support zerocopy calls for distributed… #4
Open
arvindkandhare
wants to merge
2
commits into
twitter-archive:master
Choose a base branch
from
arvindkandhare:zerocopy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,16 +18,16 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import java.nio.ByteBuffer; | ||
import java.security.GeneralSecurityException; | ||
|
||
import org.apache.bookkeeper.client.BKException.BKDigestMatchException; | ||
import org.apache.bookkeeper.client.BookKeeper.DigestType; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.jboss.netty.buffer.ChannelBuffer; | ||
import org.jboss.netty.buffer.ChannelBufferInputStream; | ||
import org.jboss.netty.buffer.ChannelBuffers; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.security.GeneralSecurityException; | ||
|
||
/** | ||
* This class takes an entry, attaches a digest to it and packages it with relevant | ||
|
@@ -45,11 +45,12 @@ abstract class DigestManager { | |
|
||
abstract int getMacCodeLength(); | ||
|
||
void update(byte[] data) { | ||
update(data, 0, data.length); | ||
} | ||
/*void update(ByteBuffer data) { | ||
update(data, 0, data.limit()); | ||
}*/ | ||
|
||
abstract void update(byte[] data, int offset, int length); | ||
// abstract void update(byte[] data, int offset, int length); | ||
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. Same here. |
||
abstract void update(ByteBuffer data); | ||
abstract byte[] getValueAndReset(); | ||
|
||
final int macCodeLength; | ||
|
@@ -80,7 +81,8 @@ static DigestManager instantiate(long ledgerId, byte[] passwd, DigestType digest | |
* @return | ||
*/ | ||
|
||
public ChannelBuffer computeDigestAndPackageForSending(long entryId, long lastAddConfirmed, long length, byte[] data, int doffset, int dlength) { | ||
public ChannelBuffer computeDigestAndPackageForSending(long entryId, long lastAddConfirmed, long length, | ||
ByteBuffer data) { | ||
|
||
byte[] bufferArray = new byte[METADATA_LENGTH + macCodeLength]; | ||
ByteBuffer buffer = ByteBuffer.wrap(bufferArray); | ||
|
@@ -90,16 +92,16 @@ public ChannelBuffer computeDigestAndPackageForSending(long entryId, long lastAd | |
buffer.putLong(length); | ||
buffer.flip(); | ||
|
||
update(buffer.array(), 0, METADATA_LENGTH); | ||
update(data, doffset, dlength); | ||
update(buffer); | ||
update(data); | ||
byte[] digest = getValueAndReset(); | ||
|
||
buffer.limit(buffer.capacity()); | ||
buffer.position(METADATA_LENGTH); | ||
buffer.put(digest); | ||
buffer.flip(); | ||
|
||
return ChannelBuffers.wrappedBuffer(ChannelBuffers.wrappedBuffer(buffer), ChannelBuffers.wrappedBuffer(data, doffset, dlength)); | ||
return ChannelBuffers.wrappedBuffer(ChannelBuffers.wrappedBuffer(buffer), ChannelBuffers.wrappedBuffer(data)); | ||
} | ||
|
||
private void verifyDigest(ChannelBuffer dataReceived) throws BKDigestMatchException { | ||
|
@@ -123,10 +125,14 @@ private void verifyDigest(long entryId, ChannelBuffer dataReceived, boolean skip | |
this.getClass().getName(), dataReceived.readableBytes()); | ||
throw new BKDigestMatchException(); | ||
} | ||
update(dataReceivedBuffer.array(), dataReceivedBuffer.position(), METADATA_LENGTH); | ||
|
||
dataReceivedBuffer.limit(dataReceivedBuffer.position() + METADATA_LENGTH); | ||
update(dataReceivedBuffer); | ||
|
||
int offset = METADATA_LENGTH + macCodeLength; | ||
update(dataReceivedBuffer.array(), dataReceivedBuffer.position() + offset, dataReceived.readableBytes() - offset); | ||
dataReceivedBuffer.position(dataReceivedBuffer.position() + offset); | ||
dataReceivedBuffer.limit( dataReceived.readableBytes() - offset); | ||
update(dataReceivedBuffer); | ||
digest = getValueAndReset(); | ||
|
||
for (int i = 0; i < digest.length; i++) { | ||
|
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 |
---|---|---|
|
@@ -20,23 +20,12 @@ | |
*/ | ||
package org.apache.bookkeeper.client; | ||
|
||
import java.security.GeneralSecurityException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Enumeration; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Queue; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.concurrent.RejectedExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Objects; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import com.google.common.util.concurrent.RateLimiter; | ||
import org.apache.bookkeeper.client.AsyncCallback.AddCallback; | ||
import org.apache.bookkeeper.client.AsyncCallback.CloseCallback; | ||
import org.apache.bookkeeper.client.AsyncCallback.ReadCallback; | ||
|
@@ -58,10 +47,14 @@ | |
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import com.google.common.util.concurrent.RateLimiter; | ||
import java.nio.ByteBuffer; | ||
import java.security.GeneralSecurityException; | ||
import java.util.*; | ||
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. import all the dependencies, rather than importing * |
||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.concurrent.RejectedExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* Ledger handle contains ledger metadata and is used to access the read and | ||
|
@@ -598,7 +591,26 @@ public void asyncAddEntry(final byte[] data, final AddCallback cb, | |
public void asyncAddEntry(final byte[] data, final int offset, final int length, | ||
final AddCallback cb, final Object ctx) { | ||
PendingAddOp op = new PendingAddOp(LedgerHandle.this, cb, ctx); | ||
doAsyncAddEntry(op, data, offset, length, cb, ctx); | ||
doAsyncAddEntry(op, ByteBuffer.wrap(data, offset, length), cb, ctx); | ||
} | ||
|
||
|
||
/** | ||
* Add entry asynchronously to an open ledger, using a bytebuffer. | ||
* | ||
* @param data | ||
* ByteBuffer to be written | ||
* @param cb | ||
* object implementing callbackinterface | ||
* @param ctx | ||
* some control object | ||
* @throws ArrayIndexOutOfBoundsException if offset or length is negative or | ||
* offset and length sum to a value higher than the length of data. | ||
*/ | ||
public void asyncAddEntry(final ByteBuffer data, | ||
final AddCallback cb, final Object ctx) { | ||
PendingAddOp op = new PendingAddOp(LedgerHandle.this, cb, ctx); | ||
doAsyncAddEntry(op, data, cb, ctx); | ||
} | ||
|
||
/** | ||
|
@@ -613,17 +625,11 @@ public void asyncAddEntry(final byte[] data, final int offset, final int length, | |
void asyncRecoveryAddEntry(final byte[] data, final int offset, final int length, | ||
final AddCallback cb, final Object ctx) { | ||
PendingAddOp op = new PendingAddOp(LedgerHandle.this, cb, ctx).enableRecoveryAdd(); | ||
doAsyncAddEntry(op, data, offset, length, cb, ctx); | ||
doAsyncAddEntry(op, ByteBuffer.wrap(data, offset, length), cb, ctx); | ||
} | ||
|
||
private void doAsyncAddEntry(final PendingAddOp op, final byte[] data, final int offset, final int length, | ||
private void doAsyncAddEntry(final PendingAddOp op, final ByteBuffer data, | ||
final AddCallback cb, final Object ctx) { | ||
if (offset < 0 || length < 0 | ||
|| (offset + length) > data.length) { | ||
throw new ArrayIndexOutOfBoundsException( | ||
"Invalid values for offset("+offset | ||
+") or length("+length+")"); | ||
} | ||
throttler.acquire(); | ||
|
||
final long entryId; | ||
|
@@ -670,8 +676,8 @@ public String toString() { | |
@Override | ||
public void safeRun() { | ||
ChannelBuffer toSend = macManager.computeDigestAndPackageForSending( | ||
entryId, getLastAddConfirmed(), currentLength, data, offset, length); | ||
op.initiate(toSend, length); | ||
entryId, getLastAddConfirmed(), currentLength, data ); | ||
op.initiate(toSend, data.limit()); | ||
} | ||
@Override | ||
public String toString() { | ||
|
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
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.
can you just remove these lines rather than just commented out them?