Search in sources :

Example 51 with SerializableCallable

use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.

the class SizingFlagDUnitTest method getEvictions.

private long getEvictions(VM vm0) {
    return (Long) vm0.invoke(new SerializableCallable() {

        public Object call() {
            Cache cache = getCache();
            LocalRegion region = (LocalRegion) cache.getRegion("region");
            return getEvictions(region);
        }
    });
}
Also used : SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Cache(org.apache.geode.cache.Cache)

Example 52 with SerializableCallable

use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.

the class SizingFlagDUnitTest method prHostsBucketForKey.

private boolean prHostsBucketForKey(VM vm, final Object key) {
    Boolean result = (Boolean) vm.invoke(new SerializableCallable("prHostsBucketForKey") {

        public Object call() {
            Cache cache = getCache();
            DistributedMember myId = cache.getDistributedSystem().getDistributedMember();
            Region region = cache.getRegion("region");
            DistributedMember hostMember = PartitionRegionHelper.getPrimaryMemberForKey(region, key);
            if (hostMember == null) {
                throw new IllegalStateException("bucket for key " + key + " is not hosted!");
            }
            boolean res = Boolean.valueOf(myId.equals(hostMember));
            // cache.getLogger().info("DEBUG prHostsBucketForKey=" + res);
            return res;
        }
    });
    return result.booleanValue();
}
Also used : SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) DistributedMember(org.apache.geode.distributed.DistributedMember) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache)

Example 53 with SerializableCallable

use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.

the class SizingFlagDUnitTest method getObjectSizerInvocations.

private int getObjectSizerInvocations(VM vm0) {
    return (Integer) vm0.invoke(new SerializableCallable() {

        public Object call() {
            Cache cache = getCache();
            LocalRegion region = (LocalRegion) cache.getRegion("region");
            return getObjectSizerInvocations(region);
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Cache(org.apache.geode.cache.Cache)

Example 54 with SerializableCallable

use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.

the class FunctionServiceStatsDUnitTest method testP2PMembersFunctionExecutionStats.

/**
   * Test the execution of function on all memebers haveResults = true
   * 
   * member1 calls for the function executions sp the results received on memeber 1 should be equal
   * to the no of function execution calls. Function Execution should happen on all other members
   * too. so the no of function execution calls and no of function executions completed should be
   * equal tio the no of functions from member 1
   */
@Test
public void testP2PMembersFunctionExecutionStats() throws Exception {
    Host host = Host.getHost(0);
    VM member1 = host.getVM(0);
    VM member2 = host.getVM(1);
    VM member3 = host.getVM(2);
    VM member4 = host.getVM(3);
    SerializableCallable connectToDistributedSystem = new SerializableCallable("connectToDistributedSystem") {

        public Object call() throws Exception {
            Properties props = new Properties();
            try {
                ds = getSystem(props);
                assertNotNull(ds);
            } catch (Exception e) {
                Assert.fail("Failed while creating the Distribued System", e);
            }
            return Boolean.TRUE;
        }
    };
    member1.invoke(connectToDistributedSystem);
    member2.invoke(connectToDistributedSystem);
    member3.invoke(connectToDistributedSystem);
    member4.invoke(connectToDistributedSystem);
    member1.invoke(initializeStats);
    member2.invoke(initializeStats);
    member3.invoke(initializeStats);
    member4.invoke(initializeStats);
    final int noOfMembers = 1;
    final Function inlineFunction = new FunctionAdapter() {

        public void execute(FunctionContext context) {
            if (context.getArguments() instanceof String) {
                context.getResultSender().lastResult("Success");
            } else {
                context.getResultSender().lastResult("Failure");
            }
        }

        public String getId() {
            return getClass().getName();
        }

        public boolean hasResult() {
            return true;
        }
    };
    member1.invoke(new SerializableCallable("excuteOnMembers_InlineFunction") {

        public Object call() throws Exception {
            assertNotNull(ds);
            Execution memberExecution = null;
            DistributedMember localmember = ds.getDistributedMember();
            memberExecution = FunctionService.onMember(localmember);
            memberExecution.setArguments("Key");
            try {
                ResultCollector rc = memberExecution.execute(inlineFunction);
                int size = ((List) rc.getResult()).size();
                resultReceived_Aggregate += size;
                noOfExecutionCalls_Aggregate++;
                noOfExecutionsCompleted_Aggregate++;
                resultReceived_Inline += size;
                noOfExecutionCalls_Inline++;
                noOfExecutionsCompleted_Inline++;
            } catch (Exception e) {
                LogWriterUtils.getLogWriter().info("Exception Occurred : " + e.getMessage());
                e.printStackTrace();
                Assert.fail("Test failed", e);
            }
            return Boolean.TRUE;
        }
    });
    member1.invoke(new SerializableCallable("checkFunctionExecutionStatsForMember1") {

        public Object call() throws Exception {
            FunctionServiceStats functionServiceStats = ds.getFunctionServiceStats();
            waitNoFunctionsRunning(functionServiceStats);
            assertEquals(noOfExecutionCalls_Aggregate, functionServiceStats.getFunctionExecutionCalls());
            assertEquals(noOfExecutionsCompleted_Aggregate, functionServiceStats.getFunctionExecutionsCompleted());
            assertEquals(resultReceived_Aggregate, functionServiceStats.getResultsReceived());
            FunctionStats functionStats = FunctionStats.getFunctionStats(inlineFunction.getId(), ds);
            assertEquals(noOfExecutionCalls_Inline, functionStats.getFunctionExecutionCalls());
            assertEquals(noOfExecutionsCompleted_Inline, functionStats.getFunctionExecutionsCompleted());
            assertEquals(resultReceived_Inline, functionStats.getResultsReceived());
            return Boolean.TRUE;
        }
    });
    SerializableCallable checkFunctionExecutionStatsForOtherMember = new SerializableCallable("checkFunctionExecutionStatsForOtherMember") {

        public Object call() throws Exception {
            FunctionServiceStats functionServiceStats = ds.getFunctionServiceStats();
            waitNoFunctionsRunning(functionServiceStats);
            // One function Execution took place on there members
            // noOfExecutionCalls_Aggregate++;
            // noOfExecutionsCompleted_Aggregate++;
            assertEquals(noOfExecutionCalls_Aggregate, functionServiceStats.getFunctionExecutionCalls());
            assertEquals(noOfExecutionsCompleted_Aggregate, functionServiceStats.getFunctionExecutionsCompleted());
            FunctionStats functionStats = FunctionStats.getFunctionStats(inlineFunction.getId(), ds);
            // noOfExecutionCalls_Inline++;
            // noOfExecutionsCompleted_Inline++;
            assertEquals(noOfExecutionCalls_Inline, functionStats.getFunctionExecutionCalls());
            assertEquals(noOfExecutionsCompleted_Inline, functionStats.getFunctionExecutionsCompleted());
            return Boolean.TRUE;
        }
    };
    member2.invoke(checkFunctionExecutionStatsForOtherMember);
    member3.invoke(checkFunctionExecutionStatsForOtherMember);
    member4.invoke(checkFunctionExecutionStatsForOtherMember);
    SerializableCallable closeDistributedSystem = new SerializableCallable("closeDistributedSystem") {

        public Object call() throws Exception {
            if (getCache() != null && !getCache().isClosed()) {
                getCache().close();
                getCache().getDistributedSystem().disconnect();
            }
            return Boolean.TRUE;
        }
    };
    member1.invoke(closeDistributedSystem);
    member2.invoke(closeDistributedSystem);
    member3.invoke(closeDistributedSystem);
    member4.invoke(closeDistributedSystem);
}
Also used : Host(org.apache.geode.test.dunit.Host) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) FunctionException(org.apache.geode.cache.execute.FunctionException) IOException(java.io.IOException) FunctionContext(org.apache.geode.cache.execute.FunctionContext) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) Execution(org.apache.geode.cache.execute.Execution) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) DistributedMember(org.apache.geode.distributed.DistributedMember) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) ResultCollector(org.apache.geode.cache.execute.ResultCollector) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 55 with SerializableCallable

use of org.apache.geode.test.dunit.SerializableCallable in project geode by apache.

the class FunctionServiceStatsDUnitTest method testP2PDistributedRegionFunctionExecutionStats.

/**
   * Test the function execution statistics in case of the distributed Region P2P DataStore0 is with
   * Empty datapolicy
   */
@Test
public void testP2PDistributedRegionFunctionExecutionStats() {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    final VM datastore0 = host.getVM(0);
    final VM datastore1 = host.getVM(1);
    final VM datastore2 = host.getVM(2);
    final VM datastore3 = host.getVM(3);
    datastore0.invoke(initializeStats);
    datastore1.invoke(initializeStats);
    datastore2.invoke(initializeStats);
    datastore3.invoke(initializeStats);
    SerializableCallable createAndPopulateRegionWithEmpty = new SerializableCallable("Create PR with Function Factory") {

        public Object call() throws Exception {
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.DISTRIBUTED_ACK);
            factory.setDataPolicy(DataPolicy.EMPTY);
            Region region = getCache().createRegion(rName, factory.create());
            LogWriterUtils.getLogWriter().info("Region Created :" + region);
            assertNotNull(region);
            FunctionService.registerFunction(new TestFunction(true, TestFunction.TEST_FUNCTION2));
            for (int i = 1; i <= 200; i++) {
                region.put("execKey-" + i, new Integer(i));
            }
            return Boolean.TRUE;
        }
    };
    datastore0.invoke(createAndPopulateRegionWithEmpty);
    SerializableCallable createAndPopulateRegionWithReplicate = new SerializableCallable("Create PR with Function Factory") {

        public Object call() throws Exception {
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.DISTRIBUTED_ACK);
            factory.setDataPolicy(DataPolicy.REPLICATE);
            Region region = getCache().createRegion(rName, factory.create());
            LogWriterUtils.getLogWriter().info("Region Created :" + region);
            assertNotNull(region);
            FunctionService.registerFunction(new TestFunction(true, TestFunction.TEST_FUNCTION2));
            for (int i = 1; i <= 200; i++) {
                region.put("execKey-" + i, new Integer(i));
            }
            return Boolean.TRUE;
        }
    };
    datastore1.invoke(createAndPopulateRegionWithReplicate);
    datastore2.invoke(createAndPopulateRegionWithReplicate);
    datastore3.invoke(createAndPopulateRegionWithReplicate);
    SerializableCallable executeFunction = new SerializableCallable("ExecuteFunction from Normal Region") {

        public Object call() throws Exception {
            Region region = getCache().getRegion(rName);
            try {
                List list = (List) FunctionService.onRegion(region).setArguments(Boolean.TRUE).execute(TestFunction.TEST_FUNCTION2).getResult();
                // this is the Distributed Region with Empty Data policy.
                // therefore no function execution takes place here. it only receives the results.
                resultReceived_Aggregate += list.size();
                assertEquals(resultReceived_Aggregate, ((InternalDistributedSystem) getCache().getDistributedSystem()).getFunctionServiceStats().getResultsReceived());
                resultReceived_TESTFUNCTION2 += list.size();
                assertEquals(resultReceived_TESTFUNCTION2, ((InternalDistributedSystem) getCache().getDistributedSystem()).getFunctionServiceStats().getResultsReceived());
                return Boolean.TRUE;
            } catch (FunctionException e) {
                e.printStackTrace();
                Assert.fail("test failed due to", e);
                return Boolean.FALSE;
            } catch (Exception e) {
                e.printStackTrace();
                Assert.fail("test failed due to", e);
                return Boolean.FALSE;
            }
        }
    };
    datastore0.invoke(executeFunction);
    // there is a replicated region on 3 nodes so we cannot predict on which
    // node the function execution will take place
    // so i have avoided that check.
    SerializableCallable closeDistributedSystem = new SerializableCallable("closeDistributedSystem") {

        public Object call() throws Exception {
            if (getCache() != null && !getCache().isClosed()) {
                getCache().close();
                getCache().getDistributedSystem().disconnect();
            }
            return Boolean.TRUE;
        }
    };
    datastore0.invoke(closeDistributedSystem);
    datastore1.invoke(closeDistributedSystem);
    datastore2.invoke(closeDistributedSystem);
    datastore3.invoke(closeDistributedSystem);
}
Also used : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) FunctionException(org.apache.geode.cache.execute.FunctionException) Host(org.apache.geode.test.dunit.Host) FunctionException(org.apache.geode.cache.execute.FunctionException) IOException(java.io.IOException) AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) ArrayList(java.util.ArrayList) List(java.util.List) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)502 VM (org.apache.geode.test.dunit.VM)326 Test (org.junit.Test)314 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)311 Host (org.apache.geode.test.dunit.Host)306 Region (org.apache.geode.cache.Region)224 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)157 IgnoredException (org.apache.geode.test.dunit.IgnoredException)155 AttributesFactory (org.apache.geode.cache.AttributesFactory)139 Cache (org.apache.geode.cache.Cache)109 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)95 FunctionException (org.apache.geode.cache.execute.FunctionException)88 ArrayList (java.util.ArrayList)83 HashSet (java.util.HashSet)83 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)77 Execution (org.apache.geode.cache.execute.Execution)74 CommitConflictException (org.apache.geode.cache.CommitConflictException)70 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)66 Function (org.apache.geode.cache.execute.Function)63 IOException (java.io.IOException)62