HBASE-14860 Improve BoundedByteBufferPool; make lockless

This commit is contained in:
stack 2015-11-24 17:06:46 -08:00
parent 1178c4b89b
commit 5898b95329
2 changed files with 27 additions and 5 deletions

View File

@ -76,19 +76,23 @@ public class BoundedByteBufferPool {
// queued buffers in any transition.
private final AtomicLong stateRef = new AtomicLong();
private static int toCountOfBuffers(long state) {
@VisibleForTesting
static int toCountOfBuffers(long state) {
return (int)state;
}
private static int toTotalCapacity(long state) {
@VisibleForTesting
static int toTotalCapacity(long state) {
return (int)(state >>> 32);
}
private static long toState(int countOfBuffers, int totalCapacity) {
return ((long)totalCapacity << 32) | totalCapacity;
@VisibleForTesting
static long toState(int countOfBuffers, int totalCapacity) {
return ((long)totalCapacity << 32) | countOfBuffers;
}
private static long subtractOneBufferFromState(long state, int capacity) {
@VisibleForTesting
static long subtractOneBufferFromState(long state, int capacity) {
return state - ((long)capacity << 32) - 1;
}

View File

@ -17,6 +17,10 @@
*/
package org.apache.hadoop.hbase.io;
import static org.apache.hadoop.hbase.io.BoundedByteBufferPool.subtractOneBufferFromState;
import static org.apache.hadoop.hbase.io.BoundedByteBufferPool.toCountOfBuffers;
import static org.apache.hadoop.hbase.io.BoundedByteBufferPool.toState;
import static org.apache.hadoop.hbase.io.BoundedByteBufferPool.toTotalCapacity;
import static org.junit.Assert.assertEquals;
import java.nio.ByteBuffer;
@ -146,4 +150,18 @@ public class TestBoundedByteBufferPool {
// runningAverage in pool
assertEquals(initialByteBufferSize, this.reservoir.getRunningAverage());
}
@Test
public void testStateConversionMethods() {
int countOfBuffers = 123;
int totalCapacity = 456;
long state = toState(countOfBuffers, totalCapacity);
assertEquals(countOfBuffers, toCountOfBuffers(state));
assertEquals(totalCapacity, toTotalCapacity(state));
long state2 = subtractOneBufferFromState(state, 7);
assertEquals(countOfBuffers - 1, toCountOfBuffers(state2));
assertEquals(totalCapacity - 7, toTotalCapacity(state2));
}
}