Search in sources :

Example 41 with DistributedMember

use of org.apache.geode.distributed.DistributedMember 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 42 with DistributedMember

use of org.apache.geode.distributed.DistributedMember 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 43 with DistributedMember

use of org.apache.geode.distributed.DistributedMember in project geode by apache.

the class MyTransactionFunction method verifyExecutionOnPrimary.

private void verifyExecutionOnPrimary(RegionFunctionContext ctx) {
    PartitionedRegion pr = (PartitionedRegion) ctx.getDataSet();
    ArrayList args = (ArrayList) ctx.getArguments();
    CustId custId = (CustId) args.get(1);
    int bucketId = PartitionedRegionHelper.getHashKey(pr, null, custId, null, null);
    DistributedMember primary = pr.getRegionAdvisor().getPrimaryMemberForBucket(bucketId);
    DistributedMember me = pr.getCache().getDistributedSystem().getDistributedMember();
    Assert.assertTrue(me.equals(primary), "Function should have been executed on primary:" + primary + " but was executed on member:" + me);
}
Also used : CustId(org.apache.geode.internal.cache.execute.data.CustId) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) ArrayList(java.util.ArrayList) DistributedMember(org.apache.geode.distributed.DistributedMember)

Example 44 with DistributedMember

use of org.apache.geode.distributed.DistributedMember in project geode by apache.

the class MemberFunctionExecutionDUnitTest method excuteOnMembers_InlineFunction.

/*
   * Execute Function
   */
public static void excuteOnMembers_InlineFunction(Integer noOfMembers) {
    assertNotNull(ds);
    Execution memberExcution = null;
    if (noOfMembers.intValue() == 1) {
        // Local VM
        DistributedMember localmember = ds.getDistributedMember();
        memberExcution = FunctionService.onMember(localmember);
    } else if (noOfMembers.intValue() == 5) {
        memberExcution = FunctionService.onMembers();
    } else {
        Set memberSet = new HashSet(ds.getDistributionManager().getNormalDistributionManagerIds());
        InternalDistributedMember localVM = ds.getDistributionManager().getDistributionManagerId();
        memberSet.remove(localVM);
        memberExcution = FunctionService.onMembers(memberSet);
    }
    Execution executor = memberExcution.setArguments("Key");
    try {
        ResultCollector rc = executor.execute(new FunctionAdapter() {

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

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

            @Override
            public boolean hasResult() {
                return true;
            }
        });
        List li = (ArrayList) rc.getResult();
        LogWriterUtils.getLogWriter().info("MemberFunctionExecutionDUnitTest#excuteOnMembers: Result : " + li);
        assertEquals(li.size(), noOfMembers.intValue());
        for (Object obj : li) {
            assertEquals(obj, "Success");
        }
    } catch (Exception e) {
        LogWriterUtils.getLogWriter().info("Exception Occurred : " + e.getMessage());
        e.printStackTrace();
        Assert.fail("Test failed", e);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) FunctionContext(org.apache.geode.cache.execute.FunctionContext) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) Execution(org.apache.geode.cache.execute.Execution) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashSet(java.util.HashSet)

Example 45 with DistributedMember

use of org.apache.geode.distributed.DistributedMember in project geode by apache.

the class MemberFunctionExecutionDUnitTest method testOnMembersWithoutCache.

@Test
public void testOnMembersWithoutCache() throws Exception {
    DistributedMember member1Id = (DistributedMember) member1.invoke(new SerializableCallable() {

        @Override
        public Object call() {
            disconnectFromDS();
            return getSystem().getDistributedMember();
        }
    });
    member2.invoke(new SerializableRunnable() {

        @Override
        public void run() {
            getSystem();
            ResultCollector<?, ?> rc = FunctionService.onMember(member1Id).execute(new FunctionAdapter() {

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

                @Override
                public void execute(FunctionContext context) {
                    // This will throw an exception because the cache is not yet created.
                    CacheFactory.getAnyInstance();
                }
            });
            try {
                rc.getResult(30, TimeUnit.SECONDS);
                fail("Should have seen an exception");
            } catch (Exception e) {
                if (!(e.getCause() instanceof FunctionInvocationTargetException)) {
                    Assert.fail("failed", e);
                }
            }
        }
    });
}
Also used : SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) ResultCollector(org.apache.geode.cache.execute.ResultCollector) FunctionContext(org.apache.geode.cache.execute.FunctionContext) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

DistributedMember (org.apache.geode.distributed.DistributedMember)360 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)120 Test (org.junit.Test)109 HashSet (java.util.HashSet)83 InternalCache (org.apache.geode.internal.cache.InternalCache)83 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)82 ArrayList (java.util.ArrayList)64 VM (org.apache.geode.test.dunit.VM)60 CliCommand (org.springframework.shell.core.annotation.CliCommand)59 CliMetaData (org.apache.geode.management.cli.CliMetaData)57 Result (org.apache.geode.management.cli.Result)56 Set (java.util.Set)49 ResourceOperation (org.apache.geode.management.internal.security.ResourceOperation)49 List (java.util.List)48 Cache (org.apache.geode.cache.Cache)47 Region (org.apache.geode.cache.Region)42 HashMap (java.util.HashMap)39 CliFunctionResult (org.apache.geode.management.internal.cli.functions.CliFunctionResult)39 TabularResultData (org.apache.geode.management.internal.cli.result.TabularResultData)39 FunctionException (org.apache.geode.cache.execute.FunctionException)37