Search in sources :

Example 71 with DistributedMember

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

the class PartitionListenerDUnitTest method createPR.

protected DistributedMember createPR(VM vm, final String regionName, final boolean isAccessor) throws Throwable {
    SerializableCallable createPrRegion = new SerializableCallable("createRegion") {

        public Object call() {
            Cache cache = getCache();
            AttributesFactory attr = new AttributesFactory();
            PartitionAttributesFactory paf = new PartitionAttributesFactory();
            paf.setRedundantCopies(1);
            if (isAccessor) {
                paf.setLocalMaxMemory(0);
            }
            paf.addPartitionListener(new TestPartitionListener());
            PartitionAttributes prAttr = paf.create();
            attr.setPartitionAttributes(prAttr);
            cache.createRegion(regionName, attr.create());
            return cache.getDistributedSystem().getDistributedMember();
        }
    };
    return (DistributedMember) vm.invoke(createPrRegion);
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) PartitionAttributes(org.apache.geode.cache.PartitionAttributes) DistributedMember(org.apache.geode.distributed.DistributedMember) Cache(org.apache.geode.cache.Cache)

Example 72 with DistributedMember

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

the class RemoteTransactionDUnitTest method testTxFunctionWithOtherOps.

@Test
public void testTxFunctionWithOtherOps() {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM datastore1 = host.getVM(1);
    VM datastore2 = host.getVM(2);
    initAccessorAndDataStore(accessor, datastore1, datastore2, 0);
    SerializableCallable registerFunction = new SerializableCallable() {

        public Object call() throws Exception {
            FunctionService.registerFunction(new TXFunction());
            return null;
        }
    };
    accessor.invoke(registerFunction);
    datastore1.invoke(registerFunction);
    datastore2.invoke(registerFunction);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region custRegion = getGemfireCache().getRegion(CUSTOMER);
            TXManagerImpl mgr = getGemfireCache().getTXMgr();
            mgr.begin();
            try {
                FunctionService.onRegion(custRegion).execute(TXFunction.id).getResult();
                fail("Expected exception not thrown");
            } catch (TransactionException expected) {
            }
            Set filter = new HashSet();
            filter.add(expectedCustId);
            FunctionService.onRegion(custRegion).withFilter(filter).execute(TXFunction.id).getResult();
            assertEquals(expectedCustomer, custRegion.get(expectedCustId));
            TXStateProxy tx = mgr.internalSuspend();
            assertNull(custRegion.get(expectedCustId));
            mgr.internalResume(tx);
            return null;
        }
    });
    final Integer txOnDatastore1 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
    final Integer txOnDatastore2 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
    assertEquals(1, txOnDatastore1 + txOnDatastore2);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region custRegion = getGemfireCache().getRegion(CUSTOMER);
            CacheTransactionManager mgr = getGemfireCache().getTXMgr();
            mgr.commit();
            assertEquals(expectedCustomer, custRegion.get(expectedCustId));
            custRegion.destroy(expectedCustId);
            return null;
        }
    });
    // test onMembers
    SerializableCallable getMember = new SerializableCallable() {

        public Object call() throws Exception {
            return getGemfireCache().getMyId();
        }
    };
    final InternalDistributedMember ds1 = (InternalDistributedMember) datastore1.invoke(getMember);
    final InternalDistributedMember ds2 = (InternalDistributedMember) datastore2.invoke(getMember);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            // get owner for expectedKey
            DistributedMember owner = pr.getOwnerForKey(pr.getKeyInfo(expectedCustId));
            // get key on datastore1
            CustId keyOnOwner = null;
            keyOnOwner = getKeyOnMember(owner, pr);
            TXManagerImpl mgr = getGemfireCache().getTXMgr();
            mgr.begin();
            // bootstrap tx on owner
            pr.get(keyOnOwner);
            Set<DistributedMember> members = new HashSet<DistributedMember>();
            members.add(ds1);
            members.add(ds2);
            try {
                FunctionService.onMembers(members).execute(TXFunction.id).getResult();
                fail("expected exception not thrown");
            } catch (TransactionException expected) {
            }
            FunctionService.onMember(owner).execute(TXFunction.id).getResult();
            assertEquals(expectedCustomer, pr.get(expectedCustId));
            TXStateProxy tx = mgr.internalSuspend();
            assertNull(pr.get(expectedCustId));
            mgr.internalResume(tx);
            return null;
        }
    });
    final Integer txOnDatastore1_1 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
    final Integer txOnDatastore2_1 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
    assertEquals(1, txOnDatastore1_1 + txOnDatastore2_1);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region custRegion = getGemfireCache().getRegion(CUSTOMER);
            CacheTransactionManager mgr = getGemfireCache().getTXMgr();
            mgr.commit();
            assertEquals(expectedCustomer, custRegion.get(expectedCustId));
            custRegion.destroy(expectedCustId);
            return null;
        }
    });
    // test function execution on data store
    final DistributedMember owner = (DistributedMember) accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            return pr.getOwnerForKey(pr.getKeyInfo(expectedCustId));
        }
    });
    SerializableCallable testFnOnDs = new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTXMgr();
            PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            CustId keyOnDs = getKeyOnMember(pr.getMyId(), pr);
            mgr.begin();
            pr.get(keyOnDs);
            Set filter = new HashSet();
            filter.add(keyOnDs);
            FunctionService.onRegion(pr).withFilter(filter).execute(TXFunction.id).getResult();
            assertEquals(expectedCustomer, pr.get(expectedCustId));
            TXStateProxy tx = mgr.internalSuspend();
            assertNull(pr.get(expectedCustId));
            mgr.internalResume(tx);
            return null;
        }
    };
    SerializableCallable closeTx = new SerializableCallable() {

        public Object call() throws Exception {
            Region custRegion = getGemfireCache().getRegion(CUSTOMER);
            CacheTransactionManager mgr = getGemfireCache().getTXMgr();
            mgr.commit();
            assertEquals(expectedCustomer, custRegion.get(expectedCustId));
            custRegion.destroy(expectedCustId);
            return null;
        }
    };
    if (owner.equals(ds1)) {
        datastore1.invoke(testFnOnDs);
        final Integer txOnDatastore1_2 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
        final Integer txOnDatastore2_2 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
        // ds1 has a local transaction, not
        assertEquals(0, txOnDatastore1_2 + txOnDatastore2_2);
        // remote
        datastore1.invoke(closeTx);
    } else {
        datastore2.invoke(testFnOnDs);
        final Integer txOnDatastore1_2 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
        final Integer txOnDatastore2_2 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
        // ds1 has a local transaction, not
        assertEquals(0, txOnDatastore1_2 + txOnDatastore2_2);
        // remote
        datastore2.invoke(closeTx);
    }
    // test that function is rejected if function target is not same as txState target
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getTXMgr();
            PartitionedRegion pr = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            CustId keyOnDs1 = getKeyOnMember(ds1, pr);
            CustId keyOnDs2 = getKeyOnMember(ds2, pr);
            mgr.begin();
            // bootstrap txState
            pr.get(keyOnDs1);
            Set filter = new HashSet();
            filter.add(keyOnDs2);
            try {
                FunctionService.onRegion(pr).withFilter(filter).execute(TXFunction.id).getResult();
                fail("expected Exception not thrown");
            } catch (TransactionDataRebalancedException expected) {
            }
            try {
                FunctionService.onMember(ds2).execute(TXFunction.id).getResult();
                fail("expected exception not thrown");
            } catch (TransactionDataNotColocatedException expected) {
            }
            mgr.commit();
            return null;
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Host(org.apache.geode.test.dunit.Host) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionException(org.apache.geode.cache.TransactionException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) CustId(org.apache.geode.internal.cache.execute.data.CustId) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) TXExpiryJUnitTest(org.apache.geode.TXExpiryJUnitTest) Test(org.junit.Test)

Example 73 with DistributedMember

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

the class RemoteTransactionDUnitTest method doTestTxFunction.

private void doTestTxFunction(final Executions e) {
    Host host = Host.getHost(0);
    VM accessor = host.getVM(0);
    VM datastore1 = host.getVM(1);
    VM datastore2 = host.getVM(2);
    initAccessorAndDataStore(accessor, datastore1, datastore2, 0);
    SerializableCallable registerFunction = new SerializableCallable() {

        public Object call() throws Exception {
            FunctionService.registerFunction(new TXFunction());
            return null;
        }
    };
    accessor.invoke(registerFunction);
    datastore1.invoke(registerFunction);
    datastore2.invoke(registerFunction);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            PartitionedRegion custRegion = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            TXManagerImpl mgr = getGemfireCache().getTXMgr();
            Set regions = new HashSet();
            regions.add(custRegion);
            regions.add(getGemfireCache().getRegion(ORDER));
            mgr.begin();
            try {
                switch(e) {
                    case OnRegion:
                        FunctionService.onRegion(custRegion).execute(TXFunction.id).getResult();
                        break;
                    case OnMember:
                        FunctionService.onMembers().execute(TXFunction.id).getResult();
                        break;
                }
                fail("Expected exception not thrown");
            } catch (TransactionException expected) {
            }
            try {
                InternalFunctionService.onRegions(regions).execute(TXFunction.id).getResult();
                fail("Expected exception not thrown");
            } catch (TransactionException expected) {
            }
            Set filter = new HashSet();
            filter.add(expectedCustId);
            switch(e) {
                case OnRegion:
                    FunctionService.onRegion(custRegion).withFilter(filter).execute(TXFunction.id).getResult();
                    break;
                case OnMember:
                    DistributedMember owner = custRegion.getOwnerForKey(custRegion.getKeyInfo(expectedCustId));
                    FunctionService.onMember(owner).execute(TXFunction.id).getResult();
                    break;
            }
            TXStateProxy tx = mgr.internalSuspend();
            GemFireCacheImpl.getInstance().getLogger().warning("TX SUSPENDO:" + tx);
            assertNull(custRegion.get(expectedCustId));
            mgr.internalResume(tx);
            return null;
        }
    });
    final Integer txOnDatastore1 = (Integer) datastore1.invoke(getNumberOfTXInProgress);
    final Integer txOnDatastore2 = (Integer) datastore2.invoke(getNumberOfTXInProgress);
    assertEquals(1, txOnDatastore1 + txOnDatastore2);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getTXMgr();
            mgr.commit();
            return null;
        }
    });
    datastore1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region custRegion = getGemfireCache().getRegion(CUSTOMER);
            assertEquals(expectedCustomer, custRegion.get(expectedCustId));
            return null;
        }
    });
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            TXManagerImpl mgr = getGemfireCache().getTXMgr();
            mgr.begin();
            PartitionedRegion custRegion = (PartitionedRegion) getGemfireCache().getRegion(CUSTOMER);
            Set filter = new HashSet();
            filter.add(expectedCustId);
            switch(e) {
                case OnRegion:
                    FunctionService.onRegion(custRegion).withFilter(filter).execute(TXFunction.id).getResult();
                    break;
                case OnMember:
                    DistributedMember owner = custRegion.getOwnerForKey(custRegion.getKeyInfo(expectedCustId));
                    FunctionService.onMember(owner).execute(TXFunction.id).getResult();
                    break;
            }
            TXStateProxy tx = mgr.internalSuspend();
            custRegion.put(expectedCustId, new Customer("Cust6", "updated6"));
            mgr.internalResume(tx);
            try {
                mgr.commit();
                fail("expected commit conflict not thrown");
            } catch (CommitConflictException expected) {
            }
            return null;
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Customer(org.apache.geode.internal.cache.execute.data.Customer) Host(org.apache.geode.test.dunit.Host) NamingException(javax.naming.NamingException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) RollbackException(javax.transaction.RollbackException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) CommitConflictException(org.apache.geode.cache.CommitConflictException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) CommitConflictException(org.apache.geode.cache.CommitConflictException) TransactionException(org.apache.geode.cache.TransactionException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet)

Example 74 with DistributedMember

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

the class PRClientServerRegionFunctionExecutionNoSingleHopDUnitTest method serverAllKeyExecution.

public static void serverAllKeyExecution(Boolean isByName) {
    Region region = cache.getRegion(PartitionedRegionName);
    assertNotNull(region);
    final HashSet testKeysSet = new HashSet();
    for (int i = (totalNumBuckets.intValue() / 2); i > 0; i--) {
        testKeysSet.add("execKey-" + i);
    }
    DistributedSystem.setThreadsSocketPolicy(false);
    Function function = new TestFunction(true, TEST_FUNCTION2);
    FunctionService.registerFunction(function);
    Execution dataSet = FunctionService.onRegion(region);
    try {
        int j = 0;
        HashSet origVals = new HashSet();
        for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
            Integer val = new Integer(j++);
            origVals.add(val);
            region.put(i.next(), val);
        }
        ResultCollector rc1 = executeOnAll(dataSet, Boolean.TRUE, function, isByName);
        List resultList = (List) ((List) rc1.getResult());
        LogWriterUtils.getLogWriter().info("Result size : " + resultList.size());
        LogWriterUtils.getLogWriter().info("Result are SSSS : " + resultList);
        assertEquals(3, resultList.size());
        Iterator resultIterator = resultList.iterator();
        Map.Entry entry = null;
        DistributedMember key = null;
        List resultListForMember = new ArrayList();
        // }
        for (Object result : resultList) {
            assertEquals(Boolean.TRUE, result);
        }
        List l2 = null;
        ResultCollector rc2 = executeOnAll(dataSet, testKeysSet, function, isByName);
        l2 = ((List) rc2.getResult());
        assertEquals(3, l2.size());
        HashSet foundVals = new HashSet();
        for (Iterator i = l2.iterator(); i.hasNext(); ) {
            ArrayList subL = (ArrayList) (i.next());
            assertTrue(subL.size() > 0);
            for (Iterator subI = subL.iterator(); subI.hasNext(); ) {
                assertTrue(foundVals.add(subI.next()));
            }
        }
        assertEquals(origVals, foundVals);
    } catch (Exception e) {
        Assert.fail("Test failed after the put operation", e);
    }
}
Also used : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) ArrayList(java.util.ArrayList) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) ServerException(java.rmi.ServerException) FunctionException(org.apache.geode.cache.execute.FunctionException) SocketException(java.net.SocketException) CacheClosedException(org.apache.geode.cache.CacheClosedException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) EOFException(java.io.EOFException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) Execution(org.apache.geode.cache.execute.Execution) Iterator(java.util.Iterator) DistributedMember(org.apache.geode.distributed.DistributedMember) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) ArrayList(java.util.ArrayList) List(java.util.List) ResultCollector(org.apache.geode.cache.execute.ResultCollector) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 75 with DistributedMember

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

the class PRClientServerRegionFunctionExecutionNoSingleHopDUnitTest method FunctionExecution_Inline_Bug40714.

public static void FunctionExecution_Inline_Bug40714() {
    Region region = cache.getRegion(PartitionedRegionName);
    assertNotNull(region);
    final HashSet testKeysSet = new HashSet();
    for (int i = (totalNumBuckets.intValue() * 10); i > 0; i--) {
        testKeysSet.add("execKey-" + i);
    }
    int j = 0;
    for (Iterator i = testKeysSet.iterator(); i.hasNext(); ) {
        Integer val = new Integer(j++);
        region.put(i.next(), val);
    }
    HashMap resultMap = (HashMap) FunctionService.onRegion(region).setArguments(Boolean.TRUE).execute(new FunctionAdapter() {

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

        public String getId() {
            return "Function";
        }

        public boolean hasResult() {
            return true;
        }
    }).getResult();
    assertEquals(3, resultMap.size());
    Iterator mapIterator = resultMap.entrySet().iterator();
    Map.Entry entry = null;
    DistributedMember key = null;
    ArrayList resultListForMember = null;
    while (mapIterator.hasNext()) {
        entry = (Map.Entry) mapIterator.next();
        key = (DistributedMember) entry.getKey();
        resultListForMember = (ArrayList) entry.getValue();
        for (Object result : resultListForMember) {
            assertEquals(Boolean.TRUE, result);
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FunctionContext(org.apache.geode.cache.execute.FunctionContext) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) Iterator(java.util.Iterator) DistributedMember(org.apache.geode.distributed.DistributedMember) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) FunctionAdapter(org.apache.geode.cache.execute.FunctionAdapter) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

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