HBASE-24501 Backport to branch-1 HBASE-15785 Unnecessary lock in ByteBufferArray (#1842)
Signed-off-by: Guanghao Zhang <zghao@apache.org>
This commit is contained in:
parent
26828ac615
commit
4118383fb1
|
@ -19,8 +19,6 @@
|
||||||
package org.apache.hadoop.hbase.util;
|
package org.apache.hadoop.hbase.util;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.concurrent.locks.Lock;
|
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
@ -38,7 +36,6 @@ public final class ByteBufferArray {
|
||||||
|
|
||||||
static final int DEFAULT_BUFFER_SIZE = 4 * 1024 * 1024;
|
static final int DEFAULT_BUFFER_SIZE = 4 * 1024 * 1024;
|
||||||
private ByteBuffer buffers[];
|
private ByteBuffer buffers[];
|
||||||
private Lock locks[];
|
|
||||||
private int bufferSize;
|
private int bufferSize;
|
||||||
private int bufferCount;
|
private int bufferCount;
|
||||||
|
|
||||||
|
@ -58,9 +55,7 @@ public final class ByteBufferArray {
|
||||||
+ ", sizePerBuffer=" + StringUtils.byteDesc(bufferSize) + ", count="
|
+ ", sizePerBuffer=" + StringUtils.byteDesc(bufferSize) + ", count="
|
||||||
+ bufferCount + ", direct=" + directByteBuffer);
|
+ bufferCount + ", direct=" + directByteBuffer);
|
||||||
buffers = new ByteBuffer[bufferCount + 1];
|
buffers = new ByteBuffer[bufferCount + 1];
|
||||||
locks = new Lock[bufferCount + 1];
|
|
||||||
for (int i = 0; i <= bufferCount; i++) {
|
for (int i = 0; i <= bufferCount; i++) {
|
||||||
locks[i] = new ReentrantLock();
|
|
||||||
if (i < bufferCount) {
|
if (i < bufferCount) {
|
||||||
buffers[i] = directByteBuffer ? ByteBuffer.allocateDirect(bufferSize)
|
buffers[i] = directByteBuffer ? ByteBuffer.allocateDirect(bufferSize)
|
||||||
: ByteBuffer.allocate(bufferSize);
|
: ByteBuffer.allocate(bufferSize);
|
||||||
|
@ -102,8 +97,8 @@ public final class ByteBufferArray {
|
||||||
|
|
||||||
private final static Visitor GET_MULTIPLE_VISTOR = new Visitor() {
|
private final static Visitor GET_MULTIPLE_VISTOR = new Visitor() {
|
||||||
@Override
|
@Override
|
||||||
public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) {
|
public void visit(ByteBuffer bb, int pos, byte[] array, int arrayIdx, int len) {
|
||||||
bb.get(array, arrayIdx, len);
|
ByteBufferUtils.copyFromBufferToArray(array, bb, pos, arrayIdx, len);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -131,8 +126,8 @@ public final class ByteBufferArray {
|
||||||
|
|
||||||
private final static Visitor PUT_MULTIPLE_VISITOR = new Visitor() {
|
private final static Visitor PUT_MULTIPLE_VISITOR = new Visitor() {
|
||||||
@Override
|
@Override
|
||||||
public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) {
|
public void visit(ByteBuffer bb, int pos, byte[] array, int arrayIdx, int len) {
|
||||||
bb.put(array, arrayIdx, len);
|
ByteBufferUtils.copyFromArrayToBuffer(bb, pos, array, arrayIdx, len);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -142,11 +137,12 @@ public final class ByteBufferArray {
|
||||||
* bytes from the buffer to the destination array, else if it is a write
|
* bytes from the buffer to the destination array, else if it is a write
|
||||||
* action, we will transfer the bytes from the source array to the buffer
|
* action, we will transfer the bytes from the source array to the buffer
|
||||||
* @param bb byte buffer
|
* @param bb byte buffer
|
||||||
|
* @param pos Start position in ByteBuffer
|
||||||
* @param array a source or destination byte array
|
* @param array a source or destination byte array
|
||||||
* @param arrayOffset offset of the byte array
|
* @param arrayOffset offset of the byte array
|
||||||
* @param len read/write length
|
* @param len read/write length
|
||||||
*/
|
*/
|
||||||
void visit(ByteBuffer bb, byte[] array, int arrayOffset, int len);
|
void visit(ByteBuffer bb, int pos, byte[] array, int arrayOffset, int len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -169,7 +165,7 @@ public final class ByteBufferArray {
|
||||||
assert startBuffer >= 0 && startBuffer < bufferCount;
|
assert startBuffer >= 0 && startBuffer < bufferCount;
|
||||||
assert (endBuffer >= 0 && endBuffer < bufferCount)
|
assert (endBuffer >= 0 && endBuffer < bufferCount)
|
||||||
|| (endBuffer == bufferCount && endOffset == 0);
|
|| (endBuffer == bufferCount && endOffset == 0);
|
||||||
if (startBuffer >= locks.length || startBuffer < 0) {
|
if (startBuffer >= buffers.length || startBuffer < 0) {
|
||||||
String msg = "Failed multiple, start=" + start + ",startBuffer="
|
String msg = "Failed multiple, start=" + start + ",startBuffer="
|
||||||
+ startBuffer + ",bufferSize=" + bufferSize;
|
+ startBuffer + ",bufferSize=" + bufferSize;
|
||||||
LOG.error(msg);
|
LOG.error(msg);
|
||||||
|
@ -177,26 +173,21 @@ public final class ByteBufferArray {
|
||||||
}
|
}
|
||||||
int srcIndex = 0, cnt = -1;
|
int srcIndex = 0, cnt = -1;
|
||||||
for (int i = startBuffer; i <= endBuffer; ++i) {
|
for (int i = startBuffer; i <= endBuffer; ++i) {
|
||||||
Lock lock = locks[i];
|
ByteBuffer bb = buffers[i].duplicate();
|
||||||
lock.lock();
|
int pos = 0;
|
||||||
try {
|
if (i == startBuffer) {
|
||||||
ByteBuffer bb = buffers[i];
|
cnt = bufferSize - startOffset;
|
||||||
if (i == startBuffer) {
|
if (cnt > len) {
|
||||||
cnt = bufferSize - startOffset;
|
cnt = len;
|
||||||
if (cnt > len) cnt = len;
|
|
||||||
bb.limit(startOffset + cnt).position(startOffset);
|
|
||||||
} else if (i == endBuffer) {
|
|
||||||
cnt = endOffset;
|
|
||||||
bb.limit(cnt).position(0);
|
|
||||||
} else {
|
|
||||||
cnt = bufferSize;
|
|
||||||
bb.limit(cnt).position(0);
|
|
||||||
}
|
}
|
||||||
visitor.visit(bb, array, srcIndex + arrayOffset, cnt);
|
pos = startOffset;
|
||||||
srcIndex += cnt;
|
} else if (i == endBuffer) {
|
||||||
} finally {
|
cnt = endOffset;
|
||||||
lock.unlock();
|
} else {
|
||||||
|
cnt = bufferSize;
|
||||||
}
|
}
|
||||||
|
visitor.visit(bb, pos, array, srcIndex + arrayOffset, cnt);
|
||||||
|
srcIndex += cnt;
|
||||||
}
|
}
|
||||||
assert srcIndex == len;
|
assert srcIndex == len;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue