Search in sources :

Example 16 with TransactionDataNotColocatedException

use of org.apache.geode.cache.TransactionDataNotColocatedException in project geode by apache.

the class RemoteTransactionDUnitTest method doNonColocatedbulkOp.

private void doNonColocatedbulkOp(final OP op) {
    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);
    accessor.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Map custMap = new HashMap();
            for (int i = 0; i < 10; i++) {
                CustId cId = new CustId(i);
                Customer c = new Customer("name" + i, "addr" + i);
                custMap.put(cId, c);
            }
            GemFireCacheImpl cache = getGemfireCache();
            cache.getCacheTransactionManager().begin();
            Region r = cache.getRegion(CUSTOMER);
            try {
                switch(op) {
                    case PUTALL:
                        r.putAll(custMap);
                        break;
                    case GETALL:
                        r.put(new CustId(1), new Customer("cust1", "addr1"));
                        r.getAll(custMap.keySet());
                        break;
                    default:
                        break;
                }
                fail("expected exception not thrown");
            } catch (TransactionDataNotColocatedException e) {
            }
            cache.getCacheTransactionManager().rollback();
            return null;
        }
    });
}
Also used : HashMap(java.util.HashMap) CustId(org.apache.geode.internal.cache.execute.data.CustId) Customer(org.apache.geode.internal.cache.execute.data.Customer) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) Map(java.util.Map) HashMap(java.util.HashMap) 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)

Example 17 with TransactionDataNotColocatedException

use of org.apache.geode.cache.TransactionDataNotColocatedException 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 18 with TransactionDataNotColocatedException

use of org.apache.geode.cache.TransactionDataNotColocatedException in project geode by apache.

the class MemberFunctionExecutor method validateExecution.

@Override
public void validateExecution(final Function function, final Set dest) {
    final InternalCache cache = GemFireCacheImpl.getInstance();
    if (cache != null && cache.getTxManager().getTXState() != null) {
        if (dest.size() > 1) {
            throw new TransactionException(LocalizedStrings.PartitionedRegion_TX_FUNCTION_ON_MORE_THAN_ONE_NODE.toLocalizedString());
        } else {
            assert dest.size() == 1;
            DistributedMember funcTarget = (DistributedMember) dest.iterator().next();
            DistributedMember target = cache.getTxManager().getTXState().getTarget();
            if (target == null) {
                cache.getTxManager().getTXState().setTarget(funcTarget);
            } else if (!target.equals(funcTarget)) {
                throw new TransactionDataNotColocatedException(LocalizedStrings.PartitionedRegion_TX_FUNCTION_EXECUTION_NOT_COLOCATED.toLocalizedString());
            }
        }
    }
    if (function.optimizeForWrite() && cache != null && cache.getInternalResourceManager().getHeapMonitor().containsHeapCriticalMembers(dest) && !MemoryThresholds.isLowMemoryExceptionDisabled()) {
        Set<InternalDistributedMember> hcm = cache.getResourceAdvisor().adviseCritialMembers();
        Set<DistributedMember> sm = SetUtils.intersection(hcm, dest);
        throw new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(new Object[] { function.getId(), sm }), sm);
    }
}
Also used : TransactionException(org.apache.geode.cache.TransactionException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) LowMemoryException(org.apache.geode.cache.LowMemoryException)

Example 19 with TransactionDataNotColocatedException

use of org.apache.geode.cache.TransactionDataNotColocatedException in project geode by apache.

the class DistributedRegionFunctionExecutor method validateExecution.

/*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.geode.internal.cache.execute.AbstractExecution#validateExecution(org.apache.geode.
   * cache.execute.Function, java.util.Set)
   */
@Override
public void validateExecution(Function function, Set targetMembers) {
    InternalCache cache = region.getGemFireCache();
    if (cache != null && cache.getTxManager().getTXState() != null) {
        if (targetMembers.size() > 1) {
            throw new TransactionException(LocalizedStrings.PartitionedRegion_TX_FUNCTION_ON_MORE_THAN_ONE_NODE.toLocalizedString());
        } else {
            assert targetMembers.size() == 1;
            DistributedMember funcTarget = (DistributedMember) targetMembers.iterator().next();
            DistributedMember target = cache.getTxManager().getTXState().getTarget();
            if (target == null) {
                cache.getTxManager().getTXState().setTarget(funcTarget);
            } else if (!target.equals(funcTarget)) {
                throw new TransactionDataNotColocatedException(LocalizedStrings.PartitionedRegion_TX_FUNCTION_EXECUTION_NOT_COLOCATED_0_1.toLocalizedString(target, funcTarget));
            }
        }
    }
    if (!MemoryThresholds.isLowMemoryExceptionDisabled() && function.optimizeForWrite()) {
        try {
            region.checkIfAboveThreshold(null);
        } catch (LowMemoryException ignore) {
            Set<DistributedMember> htrm = region.getMemoryThresholdReachedMembers();
            throw new LowMemoryException(LocalizedStrings.ResourceManager_LOW_MEMORY_FOR_0_FUNCEXEC_MEMBERS_1.toLocalizedString(function.getId(), htrm), htrm);
        }
    }
}
Also used : TransactionException(org.apache.geode.cache.TransactionException) Set(java.util.Set) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) DistributedMember(org.apache.geode.distributed.DistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) LowMemoryException(org.apache.geode.cache.LowMemoryException)

Example 20 with TransactionDataNotColocatedException

use of org.apache.geode.cache.TransactionDataNotColocatedException in project geode by apache.

the class PartitionedRegion method getDataRegionForRead.

@Override
public LocalRegion getDataRegionForRead(final KeyInfo keyInfo) {
    final Object entryKey = keyInfo.getKey();
    BucketRegion br;
    try {
        PartitionedRegionDataStore ds = getDataStore();
        if (ds == null) {
            throw new TransactionException(LocalizedStrings.PartitionedRegion_TX_ON_DATASTORE.toLocalizedString());
        }
        // TODO provide appropriate Operation and arg
        int bucketId = keyInfo.getBucketId();
        if (bucketId == KeyInfo.UNKNOWN_BUCKET) {
            bucketId = PartitionedRegionHelper.getHashKey(this, null, entryKey, keyInfo.getValue(), keyInfo.getCallbackArg());
            keyInfo.setBucketId(bucketId);
        }
        br = ds.getInitializedBucketWithKnownPrimaryForId(null, bucketId);
        if (keyInfo.isCheckPrimary()) {
            try {
                br.checkForPrimary();
            } catch (PrimaryBucketException pbe) {
                throw new TransactionDataRebalancedException(LocalizedStrings.PartitionedRegion_TRANSACTIONAL_DATA_MOVED_DUE_TO_REBALANCING.toLocalizedString(), pbe);
            }
        }
    } catch (RegionDestroyedException ignore) {
        // TODO: why is this purposely not wrapping the original cause?
        throw new TransactionDataNotColocatedException(LocalizedStrings.PartitionedRegion_KEY_0_NOT_COLOCATED_WITH_TRANSACTION.toLocalizedString(entryKey));
    } catch (ForceReattemptException ignore) {
        br = null;
    }
    return br;
}
Also used : TransactionException(org.apache.geode.cache.TransactionException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) TransactionDataNotColocatedException(org.apache.geode.cache.TransactionDataNotColocatedException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException)

Aggregations

TransactionDataNotColocatedException (org.apache.geode.cache.TransactionDataNotColocatedException)27 TransactionDataRebalancedException (org.apache.geode.cache.TransactionDataRebalancedException)13 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)11 CacheException (org.apache.geode.cache.CacheException)10 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)10 TransactionException (org.apache.geode.cache.TransactionException)10 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)9 ArrayList (java.util.ArrayList)8 CustId (org.apache.geode.internal.cache.execute.data.CustId)7 CommitConflictException (org.apache.geode.cache.CommitConflictException)6 Region (org.apache.geode.cache.Region)6 TransactionDataNodeHasDepartedException (org.apache.geode.cache.TransactionDataNodeHasDepartedException)6 RemoteOperationException (org.apache.geode.internal.cache.RemoteOperationException)6 Customer (org.apache.geode.internal.cache.execute.data.Customer)6 Collection (java.util.Collection)5 CancelException (org.apache.geode.CancelException)5 UnsupportedOperationInTransactionException (org.apache.geode.cache.UnsupportedOperationInTransactionException)5 NamingException (javax.naming.NamingException)4 RollbackException (javax.transaction.RollbackException)4 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)4