Search in sources :

Example 6 with ActiveProperties

use of org.apache.asterix.common.config.ActiveProperties in project asterixdb by apache.

the class ConcurrentFramePoolUnitTest method testMemoryManager.

@org.junit.Test
public void testMemoryManager() {
    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;
    while (fmm.get() != null) {
        i++;
    }
    Assert.assertEquals(i, NUM_FRAMES);
    Assert.assertNull(cause);
}
Also used : ConcurrentFramePool(org.apache.asterix.common.memory.ConcurrentFramePool) ActiveProperties(org.apache.asterix.common.config.ActiveProperties)

Example 7 with ActiveProperties

use of org.apache.asterix.common.config.ActiveProperties in project asterixdb by apache.

the class ConcurrentFramePoolUnitTest method testConcurrentVarSizeMemoryManager.

@org.junit.Test
public void testConcurrentVarSizeMemoryManager() {
    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);
        VarSizeAllocator[] runners = new VarSizeAllocator[NUM_THREADS];
        Thread[] threads = new Thread[NUM_THREADS];
        Arrays.parallelSetAll(runners, (int i) -> new VarSizeAllocator(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 allocated = 0;
        for (int i = 0; i < threads.length; i++) {
            if (runners[i].cause() != null) {
                runners[i].cause().printStackTrace();
                Assert.fail(runners[i].cause().getMessage());
            }
            allocated += runners[i].getAllocated();
        }
        Assert.assertEquals(allocated <= NUM_FRAMES, true);
        for (int i = 0; i < threads.length; i++) {
            Assert.assertEquals(allocated + runners[i].getLastReq() > NUM_FRAMES, true);
        }
        Assert.assertEquals(allocated + 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) ActiveProperties(org.apache.asterix.common.config.ActiveProperties)

Example 8 with ActiveProperties

use of org.apache.asterix.common.config.ActiveProperties 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 9 with ActiveProperties

use of org.apache.asterix.common.config.ActiveProperties 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 10 with ActiveProperties

use of org.apache.asterix.common.config.ActiveProperties 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)

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