use of org.apache.asterix.common.config.ActiveProperties in project asterixdb by apache.
the class ConcurrentFramePoolUnitTest method testConcurrentAcquireReleaseMemoryManager.
@org.junit.Test
public void testConcurrentAcquireReleaseMemoryManager() {
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);
FixedSizeGoodAllocator[] runners = new FixedSizeGoodAllocator[NUM_THREADS];
Thread[] threads = new Thread[NUM_THREADS];
Arrays.parallelSetAll(runners, (int i) -> new FixedSizeGoodAllocator(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 (FixedSizeGoodAllocator allocator : runners) {
i += allocator.getAllocated();
}
Assert.assertEquals(NUM_FRAMES, i);
} catch (Throwable th) {
th.printStackTrace();
Assert.fail(th.getMessage());
}
Assert.assertNull(cause);
}
use of org.apache.asterix.common.config.ActiveProperties in project asterixdb by apache.
the class ConcurrentFramePoolUnitTest method testFixedSizeSubscribtion.
@org.junit.Test
public void testFixedSizeSubscribtion() {
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);
int i = 0;
ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_FRAME_SIZE);
LinkedBlockingDeque<ByteBuffer> buffers = new LinkedBlockingDeque<>();
FrameAction frameAction = new FrameAction();
frameAction.setFrame(buffer);
while (!fmm.subscribe(frameAction)) {
buffers.put(frameAction.retrieve());
i++;
}
// One subscriber.
// Check that all frames have been consumed
Assert.assertEquals(i, NUM_FRAMES);
// Release a frame (That will be handed out to the subscriber)
fmm.release(buffers.take());
// Check that all frames have been consumed (since the released frame have been handed to the consumer)
Assert.assertEquals(0, fmm.remaining());
} catch (Throwable th) {
th.printStackTrace();
Assert.fail(th.getMessage());
} finally {
Assert.assertNull(cause);
}
}
use of org.apache.asterix.common.config.ActiveProperties in project asterixdb by apache.
the class ConcurrentFramePoolUnitTest method testVarSizeMemoryManager.
@org.junit.Test
public void testVarSizeMemoryManager() {
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();
int i = 0;
int req;
while (true) {
req = random.nextInt(MAX_SIZE) + 1;
if (req == 1) {
if (fmm.get() != null) {
i += 1;
} else {
break;
}
} else if (fmm.get(req * DEFAULT_FRAME_SIZE) != null) {
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());
}
Assert.assertNull(cause);
}
use of org.apache.asterix.common.config.ActiveProperties in project asterixdb by apache.
the class ConcurrentFramePoolUnitTest method testConcurrentMemoryManager.
@org.junit.Test
public void testConcurrentMemoryManager() {
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);
FixedSizeAllocator[] runners = new FixedSizeAllocator[NUM_THREADS];
Thread[] threads = new Thread[NUM_THREADS];
Arrays.parallelSetAll(runners, (int i) -> new FixedSizeAllocator(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 (FixedSizeAllocator allocator : runners) {
i += allocator.getAllocated();
}
Assert.assertEquals(NUM_FRAMES, i);
} catch (Throwable th) {
th.printStackTrace();
Assert.fail(th.getMessage());
}
Assert.assertNull(cause);
}
use of org.apache.asterix.common.config.ActiveProperties in project asterixdb by apache.
the class ConcurrentFramePoolUnitTest method testgetWhileSubscribersExist.
@org.junit.Test
public void testgetWhileSubscribersExist() {
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);
int i = 0;
ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_FRAME_SIZE);
LinkedBlockingDeque<ByteBuffer> buffers = new LinkedBlockingDeque<>();
FrameAction frameAction = new FrameAction();
frameAction.setFrame(buffer);
while (!fmm.subscribe(frameAction)) {
buffers.put(frameAction.retrieve());
i++;
}
// One subscriber.
// Check that all frames have been consumed
Assert.assertEquals(i, NUM_FRAMES);
// Release a frame (That will be handed out to the subscriber)
fmm.release(buffers.take());
// Check that all frames have been consumed (since the released frame have been handed to the consumer)
Assert.assertEquals(fmm.remaining(), 0);
buffers.put(frameAction.retrieve());
// Create another subscriber that takes frames of double the size
ByteBuffer bufferTimes2 = ByteBuffer.allocate(DEFAULT_FRAME_SIZE * 2);
LinkedBlockingDeque<ByteBuffer> buffersTimes2 = new LinkedBlockingDeque<>();
FrameAction frameActionTimes2 = new FrameAction();
frameActionTimes2.setFrame(bufferTimes2);
Assert.assertEquals(true, fmm.subscribe(frameActionTimes2));
// release a small one
fmm.release(buffers.take());
Assert.assertEquals(fmm.remaining(), 1);
// Check that a small get fails
Assert.assertEquals(null, fmm.get());
// release another small one
fmm.release(buffers.take());
// Check that no small frames exists in the pool since subscriber request was satisfied
Assert.assertEquals(fmm.remaining(), 0);
buffersTimes2.add(frameActionTimes2.retrieve());
fmm.release(buffers);
fmm.release(bufferTimes2);
Assert.assertEquals(fmm.remaining(), NUM_FRAMES);
} catch (Throwable th) {
th.printStackTrace();
Assert.fail(th.getMessage());
} finally {
Assert.assertNull(cause);
}
}
Aggregations