use of org.apache.asterix.common.memory.ConcurrentFramePool in project asterixdb by apache.
the class ConcurrentFramePoolUnitTest method testAcquireReleaseVarSizeMemoryManager.
@org.junit.Test
public void testAcquireReleaseVarSizeMemoryManager() {
try {
ActiveProperties afp = Mockito.mock(ActiveProperties.class);
Mockito.when(afp.getMemoryComponentGlobalBudget()).thenReturn(FEED_MEM_BUDGET);
ConcurrentFramePool fmm = new ConcurrentFramePool("TestNode", afp.getMemoryComponentGlobalBudget(), DEFAULT_FRAME_SIZE);
Random random = new Random();
ArrayDeque<ByteBuffer> stack = new ArrayDeque<>();
int i = 0;
int req;
while (true) {
// release
if (random.nextDouble() < RELEASE_PROBABILITY) {
if (!stack.isEmpty()) {
ByteBuffer buffer = stack.pop();
i -= (buffer.capacity() / DEFAULT_FRAME_SIZE);
fmm.release(buffer);
}
} else {
// acquire
req = random.nextInt(MAX_SIZE) + 1;
if (req == 1) {
ByteBuffer buffer = fmm.get();
if (buffer != null) {
stack.push(buffer);
i += 1;
} else {
break;
}
} else {
ByteBuffer buffer = fmm.get(req * DEFAULT_FRAME_SIZE);
if (buffer != null) {
stack.push(buffer);
i += req;
} else {
break;
}
}
}
}
Assert.assertEquals(i <= NUM_FRAMES, true);
Assert.assertEquals(i + req > NUM_FRAMES, true);
Assert.assertEquals(i + fmm.remaining(), NUM_FRAMES);
} catch (Throwable th) {
th.printStackTrace();
Assert.fail(th.getMessage());
} finally {
Assert.assertNull(cause);
}
}
use of org.apache.asterix.common.memory.ConcurrentFramePool in project asterixdb by apache.
the class ConcurrentFramePoolUnitTest method testAcquireReleaseMemoryManager.
@org.junit.Test
public void testAcquireReleaseMemoryManager() throws HyracksDataException {
ActiveProperties afp = Mockito.mock(ActiveProperties.class);
Mockito.when(afp.getMemoryComponentGlobalBudget()).thenReturn(FEED_MEM_BUDGET);
ConcurrentFramePool fmm = new ConcurrentFramePool("TestNode", afp.getMemoryComponentGlobalBudget(), DEFAULT_FRAME_SIZE);
Random random = new Random();
ArrayDeque<ByteBuffer> stack = new ArrayDeque<>();
while (true) {
if (random.nextDouble() < RELEASE_PROBABILITY) {
if (!stack.isEmpty()) {
fmm.release(stack.pop());
}
} else {
ByteBuffer buffer = fmm.get();
if (buffer == null) {
break;
} else {
stack.push(buffer);
}
}
}
Assert.assertEquals(stack.size(), NUM_FRAMES);
Assert.assertEquals(fmm.remaining(), 0);
for (ByteBuffer buffer : stack) {
fmm.release(buffer);
}
stack.clear();
Assert.assertEquals(fmm.remaining(), NUM_FRAMES);
Assert.assertNull(cause);
}
use of org.apache.asterix.common.memory.ConcurrentFramePool in project asterixdb by apache.
the class ConcurrentFramePoolUnitTest method testConcurrentAcquireReleaseVarSizeMemoryManager.
@org.junit.Test
public void testConcurrentAcquireReleaseVarSizeMemoryManager() {
try {
ActiveProperties afp = Mockito.mock(ActiveProperties.class);
Mockito.when(afp.getMemoryComponentGlobalBudget()).thenReturn(FEED_MEM_BUDGET);
ConcurrentFramePool fmm = new ConcurrentFramePool("TestNode", afp.getMemoryComponentGlobalBudget(), DEFAULT_FRAME_SIZE);
VarSizeGoodAllocator[] runners = new VarSizeGoodAllocator[NUM_THREADS];
Thread[] threads = new Thread[NUM_THREADS];
Arrays.parallelSetAll(runners, (int i) -> new VarSizeGoodAllocator(fmm));
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(runners[i]);
}
for (int i = 0; i < threads.length; i++) {
threads[i].start();
}
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
int i = 0;
for (VarSizeGoodAllocator allocator : runners) {
if (allocator.cause() != null) {
allocator.cause().printStackTrace();
Assert.fail(allocator.cause().getMessage());
}
i += allocator.getAllocated();
}
Assert.assertEquals(NUM_FRAMES, i + fmm.remaining());
} catch (Throwable th) {
th.printStackTrace();
Assert.fail(th.getMessage());
} finally {
Assert.assertNull(cause);
}
}
use of org.apache.asterix.common.memory.ConcurrentFramePool in project asterixdb by apache.
the class ConcurrentFramePoolUnitTest method testLargerThanBudgetSubscribe.
@org.junit.Test
public void testLargerThanBudgetSubscribe() {
HyracksDataException hde = null;
try {
ConcurrentFramePool fmm = new ConcurrentFramePool("TestNode", DEFAULT_FRAME_SIZE * 16, DEFAULT_FRAME_SIZE);
ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_FRAME_SIZE * 32);
FrameAction frameAction = new FrameAction();
frameAction.setFrame(buffer);
fmm.subscribe(frameAction);
} catch (HyracksDataException e) {
hde = e;
} catch (Throwable th) {
th.printStackTrace();
Assert.fail(th.getMessage());
}
Assert.assertNotNull(hde);
Assert.assertNull(cause);
}
Aggregations