Search in sources :

Example 51 with InternalDistributedMember

use of org.apache.geode.distributed.internal.membership.InternalDistributedMember in project geode by apache.

the class GemFireDeadlockDetectorDUnitTest method testDistributedDeadlockWithFunction.

// @Category(FlakyTest.class) // GEODE-516 & GEODE-576: async actions, thread sleeps, time
// sensitive
@Test
public void testDistributedDeadlockWithFunction() throws Throwable {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    getSystem();
    InternalDistributedMember member1 = createCache(vm0);
    final InternalDistributedMember member2 = createCache(vm1);
    getBlackboard().initBlackboard();
    // Have two threads lock locks on different members in different orders.
    String gateOnMember1 = "gateOnMember1";
    String gateOnMember2 = "gateOnMember2";
    // This thread locks the lock member1 first, then member2.
    AsyncInvocation async1 = lockTheLocks(vm0, member2, gateOnMember1, gateOnMember2);
    // This thread locks the lock member2 first, then member1.
    AsyncInvocation async2 = lockTheLocks(vm1, member1, gateOnMember2, gateOnMember1);
    try {
        final LinkedList<Dependency>[] deadlockHolder = new LinkedList[1];
        Awaitility.await("waiting for deadlock").atMost(20, TimeUnit.SECONDS).until(() -> {
            GemFireDeadlockDetector detect = new GemFireDeadlockDetector();
            LinkedList<Dependency> deadlock = detect.find().findCycle();
            if (deadlock != null) {
                deadlockHolder[0] = deadlock;
            }
            return deadlock != null;
        });
        LinkedList<Dependency> deadlock = deadlockHolder[0];
        LogWriterUtils.getLogWriter().info("Deadlock=" + DeadlockDetector.prettyFormat(deadlock));
        assertEquals(8, deadlock.size());
        stopStuckThreads();
    } finally {
        try {
            waitForAsyncInvocation(async1, 45, TimeUnit.SECONDS);
        } finally {
            waitForAsyncInvocation(async2, 45, TimeUnit.SECONDS);
        }
    }
}
Also used : InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) VM(org.apache.geode.test.dunit.VM) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) LinkedList(java.util.LinkedList) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 52 with InternalDistributedMember

use of org.apache.geode.distributed.internal.membership.InternalDistributedMember in project geode by apache.

the class DistributionAdvisorDUnitTest method postSetUp.

@Override
public final void postSetUp() throws Exception {
    // connect to distributed system in every VM
    Invoke.invokeInEveryVM(new SerializableRunnable("DistributionAdvisorDUnitTest: SetUp") {

        public void run() {
            getSystem();
        }
    });
    // reinitialize the advisor
    this.advisor = DistributionAdvisor.createDistributionAdvisor(new DistributionAdvisee() {

        public DistributionAdvisee getParentAdvisee() {
            return null;
        }

        public InternalDistributedSystem getSystem() {
            return DistributionAdvisorDUnitTest.this.getSystem();
        }

        public String getName() {
            return "DistributionAdvisorDUnitTest";
        }

        public String getFullPath() {
            return getName();
        }

        public DM getDistributionManager() {
            return getSystem().getDistributionManager();
        }

        public DistributionAdvisor getDistributionAdvisor() {
            return DistributionAdvisorDUnitTest.this.advisor;
        }

        public DistributionAdvisor.Profile getProfile() {
            return null;
        }

        public void fillInProfile(DistributionAdvisor.Profile profile) {
        }

        public int getSerialNumber() {
            return 0;
        }

        public CancelCriterion getCancelCriterion() {
            return DistributionAdvisorDUnitTest.this.getSystem().getCancelCriterion();
        }
    });
    Set ids = getSystem().getDistributionManager().getOtherNormalDistributionManagerIds();
    assertEquals(VM.getVMCount(), ids.size());
    List profileList = new ArrayList();
    int i = 0;
    for (Iterator itr = ids.iterator(); itr.hasNext(); i++) {
        InternalDistributedMember id = (InternalDistributedMember) itr.next();
        DistributionAdvisor.Profile profile = new DistributionAdvisor.Profile(id, 0);
        // add profile to advisor
        advisor.putProfile(profile);
        profileList.add(profile);
    }
    this.profiles = (DistributionAdvisor.Profile[]) profileList.toArray(new DistributionAdvisor.Profile[profileList.size()]);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 53 with InternalDistributedMember

use of org.apache.geode.distributed.internal.membership.InternalDistributedMember in project geode by apache.

the class CacheAdvisorDUnitTest method testNetWriteAdvice.

@Test
public void testNetWriteAdvice() throws Exception {
    final String rgnName = getUniqueName();
    Set expected = new HashSet();
    for (int i = 0; i < vms.length; i++) {
        VM vm = vms[i];
        InternalDistributedMember id = ids[i];
        if (i % 2 == 0) {
            expected.add(id);
        }
        final int index = i;
        vm.invoke(new CacheSerializableRunnable("CacheAdvisorDUnitTest.testNetWriteAdvice") {

            public void run2() throws CacheException {
                AttributesFactory fac = new AttributesFactory();
                if (index % 2 == 0) {
                    fac.setCacheWriter(new CacheWriterAdapter());
                }
                createRegion(rgnName, fac.create());
            }
        });
    }
    RegionAttributes attrs = new AttributesFactory().create();
    DistributedRegion rgn = (DistributedRegion) createRegion(rgnName, attrs);
    assertEquals(expected, rgn.getCacheDistributionAdvisor().adviseNetWrite());
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) HashSet(java.util.HashSet) Set(java.util.Set) AttributesFactory(org.apache.geode.cache.AttributesFactory) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) VM(org.apache.geode.test.dunit.VM) HashSet(java.util.HashSet) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 54 with InternalDistributedMember

use of org.apache.geode.distributed.internal.membership.InternalDistributedMember in project geode by apache.

the class DistributedCacheOperationTest method shouldBeMockable.

@Test
public void shouldBeMockable() throws Exception {
    DistributedCacheOperation mockDistributedCacheOperation = mock(DistributedCacheOperation.class);
    CacheOperationMessage mockCacheOperationMessage = mock(CacheOperationMessage.class);
    Map<InternalDistributedMember, PersistentMemberID> persistentIds = new HashMap<>();
    when(mockDistributedCacheOperation.supportsDirectAck()).thenReturn(false);
    mockDistributedCacheOperation.waitForAckIfNeeded(mockCacheOperationMessage, persistentIds);
    verify(mockDistributedCacheOperation, times(1)).waitForAckIfNeeded(mockCacheOperationMessage, persistentIds);
    assertThat(mockDistributedCacheOperation.supportsDirectAck()).isFalse();
}
Also used : CacheOperationMessage(org.apache.geode.internal.cache.DistributedCacheOperation.CacheOperationMessage) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) HashMap(java.util.HashMap) PersistentMemberID(org.apache.geode.internal.cache.persistence.PersistentMemberID) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 55 with InternalDistributedMember

use of org.apache.geode.distributed.internal.membership.InternalDistributedMember 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)

Aggregations

InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)516 Test (org.junit.Test)162 HashSet (java.util.HashSet)124 Set (java.util.Set)77 MembershipTest (org.apache.geode.test.junit.categories.MembershipTest)63 NetView (org.apache.geode.distributed.internal.membership.NetView)60 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)56 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)55 ArrayList (java.util.ArrayList)54 DistributedMember (org.apache.geode.distributed.DistributedMember)49 UnitTest (org.apache.geode.test.junit.categories.UnitTest)49 HashMap (java.util.HashMap)46 IOException (java.io.IOException)36 Iterator (java.util.Iterator)34 PartitionedRegionLoadModel (org.apache.geode.internal.cache.partitioned.rebalance.PartitionedRegionLoadModel)34 CompositeDirector (org.apache.geode.internal.cache.partitioned.rebalance.CompositeDirector)33 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)32 Map (java.util.Map)29 CancelException (org.apache.geode.CancelException)29 DM (org.apache.geode.distributed.internal.DM)29