Search in sources :

Example 21 with ConcurrentFramePool

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);
    }
}
Also used : ConcurrentFramePool(org.apache.asterix.common.memory.ConcurrentFramePool) Random(java.util.Random) ActiveProperties(org.apache.asterix.common.config.ActiveProperties) ByteBuffer(java.nio.ByteBuffer) ArrayDeque(java.util.ArrayDeque)

Example 22 with ConcurrentFramePool

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);
}
Also used : ConcurrentFramePool(org.apache.asterix.common.memory.ConcurrentFramePool) Random(java.util.Random) ActiveProperties(org.apache.asterix.common.config.ActiveProperties) ByteBuffer(java.nio.ByteBuffer) ArrayDeque(java.util.ArrayDeque)

Example 23 with ConcurrentFramePool

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);
    }
}
Also used : ConcurrentFramePool(org.apache.asterix.common.memory.ConcurrentFramePool) ActiveProperties(org.apache.asterix.common.config.ActiveProperties)

Example 24 with ConcurrentFramePool

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);
}
Also used : ConcurrentFramePool(org.apache.asterix.common.memory.ConcurrentFramePool) FrameAction(org.apache.asterix.common.memory.FrameAction) ByteBuffer(java.nio.ByteBuffer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Aggregations

ConcurrentFramePool (org.apache.asterix.common.memory.ConcurrentFramePool)24 FeedRuntimeInputHandler (org.apache.asterix.external.feed.dataflow.FeedRuntimeInputHandler)12 FeedPolicyAccessor (org.apache.asterix.external.feed.policy.FeedPolicyAccessor)12 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)12 Test (org.junit.Test)12 ByteBuffer (java.nio.ByteBuffer)10 ActiveProperties (org.apache.asterix.common.config.ActiveProperties)10 TestControlledFrameWriter (org.apache.hyracks.api.test.TestControlledFrameWriter)8 Random (java.util.Random)7 VSizeFrame (org.apache.hyracks.api.comm.VSizeFrame)7 TestFrameWriter (org.apache.hyracks.api.test.TestFrameWriter)4 FrameAction (org.apache.asterix.common.memory.FrameAction)3 ArrayDeque (java.util.ArrayDeque)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)2