Search in sources :

Example 1 with ActiveProperties

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

Example 2 with ActiveProperties

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

Example 3 with ActiveProperties

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

Example 4 with ActiveProperties

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

Example 5 with ActiveProperties

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

Aggregations

ActiveProperties (org.apache.asterix.common.config.ActiveProperties)10 ConcurrentFramePool (org.apache.asterix.common.memory.ConcurrentFramePool)10 ByteBuffer (java.nio.ByteBuffer)4 Random (java.util.Random)3 ArrayDeque (java.util.ArrayDeque)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2 FrameAction (org.apache.asterix.common.memory.FrameAction)2