diff --git a/README.md b/README.md index 22d6d4d8..0e2310a2 100644 --- a/README.md +++ b/README.md @@ -263,6 +263,9 @@ To run several test cases: ```bash $ # a testcase for module "mnemonic-core" that requires 'pmalloc' memory service to pass $ mvn -Dtest=DurablePersonNGTest test -pl mnemonic-core -DskipTests=false + + $ # a testcase for module "mnemonic-core" that requires 'vmem' memory service to pass + $ mvn -Dtest=VolatilePersonNGTest test -pl mnemonic-core -DskipTests=false $ # a testcase for module "mnemonic-core" that requires 'pmalloc' memory service to pass $ mvn -Dtest=NonVolatileMemAllocatorNGTest test -pl mnemonic-core -DskipTests=false diff --git a/build-tools/runall.sh b/build-tools/runall.sh index 0d3a17c6..993d91ee 100755 --- a/build-tools/runall.sh +++ b/build-tools/runall.sh @@ -51,6 +51,15 @@ exit 1 fi echo [SUCCESS] Test case DurablePersonNGTest for \"mnemonic-core\" is completed! +echo [INFO] Running VolatilePersonNGTest test case for \"mnemonic-core\"... +mvn -Dtest=VolatilePersonNGTest test -pl mnemonic-core -DskipTests=false > testlog/VolatilePersonNGTest.log +if [ $? -gt 0 ] +then +echo [ERROR] This test case requires \"vmem\" memory service to pass, please check if \"vmem\" has been configured correctly! If \"vmem\" is installed, please refer to testlog/VolatilePersonNGTest.log for detailed information. +exit 1 +fi +echo [SUCCESS] Test case VolatilePersonNGTest for \"mnemonic-core\" is completed! + echo [INFO] Running NonVolatileMemAllocatorNGTest test case for \"mnemonic-core\"... mvn -Dtest=NonVolatileMemAllocatorNGTest test -pl mnemonic-core -DskipTests=false > testlog/NonVolatileMemAllocatorNGTest.log if [ $? -gt 0 ] diff --git a/mnemonic-core/src/main/java/org/apache/mnemonic/VolatileMemAllocator.java b/mnemonic-core/src/main/java/org/apache/mnemonic/VolatileMemAllocator.java index 453129b5..a816a1ed 100644 --- a/mnemonic-core/src/main/java/org/apache/mnemonic/VolatileMemAllocator.java +++ b/mnemonic-core/src/main/java/org/apache/mnemonic/VolatileMemAllocator.java @@ -29,7 +29,8 @@ * * */ -public class VolatileMemAllocator extends RestorableAllocator<VolatileMemAllocator> { +public class VolatileMemAllocator extends RestorableAllocator<VolatileMemAllocator> + implements AddressTranslator { private boolean m_activegc = true; private long m_gctimeout = 100; diff --git a/mnemonic-core/src/test/java/org/apache/mnemonic/VolatilePersonNGTest.java b/mnemonic-core/src/test/java/org/apache/mnemonic/VolatilePersonNGTest.java new file mode 100644 index 00000000..e1724a21 --- /dev/null +++ b/mnemonic-core/src/test/java/org/apache/mnemonic/VolatilePersonNGTest.java @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.mnemonic; + +/** + * + * + */ + +import java.nio.ByteBuffer; +import java.util.Random; +import java.util.UUID; + +import org.testng.annotations.Test; + +public class VolatilePersonNGTest { + private long cKEYCAPACITY; + + @Test(expectedExceptions = { OutOfHybridMemory.class }) + public void testGenPeople() throws OutOfHybridMemory, RetrieveDurableEntityError { + Random rand = Utils.createRandom(); + VolatileMemAllocator act = new VolatileMemAllocator(Utils.getVolatileMemoryAllocatorService("vmem"), + 1024 * 1024 * 8, "./pobj_person.dat", true); + + cKEYCAPACITY = act.handlerCapacity(); + act.setBufferReclaimer(new Reclaim<ByteBuffer>() { + @Override + public boolean reclaim(ByteBuffer mres, Long sz) { + System.out.println(String.format("Reclaim Memory Buffer: %X Size: %s", System.identityHashCode(mres), + null == sz ? "NULL" : sz.toString())); + return false; + } + }); + act.setChunkReclaimer(new Reclaim<Long>() { + @Override + public boolean reclaim(Long mres, Long sz) { + System.out.println(String.format("Reclaim Memory Chunk: %X Size: %s", System.identityHashCode(mres), + null == sz ? "NULL" : sz.toString())); + return false; + } + }); + + for (long i = 0; i < cKEYCAPACITY; ++i) { + act.setHandler(i, 0L); + } + + Person<Integer> mother; + Person<Integer> person; + + long keyidx = 0; + long val; + + try { + while (true) { + // if (keyidx >= KEYCAPACITY) break; + + keyidx %= cKEYCAPACITY; + + System.out.printf("************ Generating People on Key %d ***********\n", keyidx); + + val = act.getHandler(keyidx); + if (0L != val) { + PersonFactory.restore(act, val, true); + } + + person = PersonFactory.create(act); + person.setAge((short) rand.nextInt(50)); + person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true); + person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true); + person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true); + person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true); + + act.setHandler(keyidx, person.getHandler()); + + for (int deep = 0; deep < rand.nextInt(100); ++deep) { + + mother = PersonFactory.create(act); + mother.setAge((short) (50 + rand.nextInt(50))); + mother.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true); + + person.setMother(mother, true); + + person = mother; + + } + ++keyidx; + } + } finally { + act.close(); + } + } + + @Test(dependsOnMethods = { "testGenPeople" }) + public void testCheckPeople() throws RetrieveDurableEntityError { + VolatileMemAllocator act = new VolatileMemAllocator(Utils.getVolatileMemoryAllocatorService("pmalloc"), + 1024 * 1024 * 8, "./pobj_person.dat", true); + act.setBufferReclaimer(new Reclaim<ByteBuffer>() { + @Override + public boolean reclaim(ByteBuffer mres, Long sz) { + System.out.println(String.format("Reclaim Memory Buffer: %X Size: %s", System.identityHashCode(mres), + null == sz ? "NULL" : sz.toString())); + return false; + } + }); + act.setChunkReclaimer(new Reclaim<Long>() { + @Override + public boolean reclaim(Long mres, Long sz) { + System.out.println(String.format("Reclaim Memory Chunk: %X Size: %s", System.identityHashCode(mres), + null == sz ? "NULL" : sz.toString())); + return false; + } + }); + + long val; + for (long i = 0; i < cKEYCAPACITY; ++i) { + System.out.printf("----------Key %d--------------\n", i); + val = act.getHandler(i); + if (0L == val) { + break; + } + Person<Integer> person = PersonFactory.restore(act, val, true); + while (null != person) { + person.testOutput(); + person = person.getMother(); + } + } + + act.close(); + } +}