Search in sources :

Example 11 with TXStateProxy

use of org.apache.geode.internal.cache.TXStateProxy in project geode by apache.

the class PeerTypeRegistration method updateRegion.

private void updateRegion(Object k, Object v) {
    Region<Object, Object> r = getIdToType();
    InternalCache cache = (InternalCache) r.getRegionService();
    checkDistributedTypeRegistryState();
    TXManagerImpl txManager = (TXManagerImpl) cache.getCacheTransactionManager();
    TXStateProxy currentState = suspendTX();
    boolean state = useUDPMessagingIfNecessary();
    try {
        // The loop might not be necessary because we're
        // updating a replicated region inside a dlock,
        // but just in case we'll make sure to retry the transaction.
        int failureCount = 0;
        while (true) {
            txManager.begin();
            try {
                r.put(k, v);
                txManager.commit();
                return;
            } catch (TransactionException e) {
                // even put can now throw a TransactionException, rollback if required
                if (txManager.exists()) {
                    txManager.rollback();
                }
                // Let's just make sure things don't get out of hand.
                if (++failureCount > MAX_TRANSACTION_FAILURES) {
                    throw e;
                }
            }
        }
    } finally {
        releaseUDPMessaging(state);
        resumeTX(currentState);
    }
}
Also used : TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) TransactionException(org.apache.geode.cache.TransactionException) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) InternalCache(org.apache.geode.internal.cache.InternalCache)

Example 12 with TXStateProxy

use of org.apache.geode.internal.cache.TXStateProxy in project geode by apache.

the class PeerTypeRegistration method allocateTypeId.

private int allocateTypeId(PdxType newType) {
    TXStateProxy currentState = suspendTX();
    Region<Object, Object> r = getIdToType();
    int id = newType.hashCode() & PLACE_HOLDER_FOR_TYPE_ID;
    int newTypeId = id | this.dsId;
    try {
        int maxTry = maxTypeId;
        while (r.get(newTypeId) != null) {
            maxTry--;
            if (maxTry == 0) {
                throw new InternalGemFireError("Used up all of the PDX type ids for this distributed system. The maximum number of PDX types is " + maxTypeId);
            }
            // Find the next available type id.
            id++;
            if (id > this.maxTypeId) {
                id = 1;
            }
            newTypeId = id | this.dsId;
        }
        return newTypeId;
    } finally {
        resumeTX(currentState);
    }
}
Also used : TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) InternalGemFireError(org.apache.geode.InternalGemFireError)

Example 13 with TXStateProxy

use of org.apache.geode.internal.cache.TXStateProxy in project geode by apache.

the class PeerTypeRegistration method getEnumById.

public EnumInfo getEnumById(int id) {
    verifyConfiguration();
    EnumId enumId = new EnumId(id);
    TXStateProxy currentState = suspendTX();
    try {
        return (EnumInfo) getIdToType().get(enumId);
    } finally {
        resumeTX(currentState);
    }
}
Also used : TXStateProxy(org.apache.geode.internal.cache.TXStateProxy)

Example 14 with TXStateProxy

use of org.apache.geode.internal.cache.TXStateProxy in project geode by apache.

the class PeerTypeRegistration method getExistingIdForEnum.

/** Should be called holding the dlock */
private EnumId getExistingIdForEnum(EnumInfo ei) {
    TXStateProxy currentState = suspendTX();
    int totalEnumIdInDS = 0;
    try {
        EnumId result = null;
        for (Map.Entry<Object, Object> entry : getIdToType().entrySet()) {
            Object v = entry.getValue();
            Object k = entry.getKey();
            if (k instanceof EnumId) {
                EnumId id = (EnumId) k;
                EnumInfo info = (EnumInfo) v;
                enumToId.put(info, id);
                int tmpDsId = PLACE_HOLDER_FOR_DS_ID & id.intValue();
                if (tmpDsId == this.dsId) {
                    totalEnumIdInDS++;
                }
                if (ei.equals(info)) {
                    result = id;
                }
            } else {
                typeToId.put((PdxType) v, (Integer) k);
            }
        }
        if (totalEnumIdInDS == this.maxTypeId) {
            throw new InternalGemFireError("Used up all of the PDX enum ids for this distributed system. The maximum number of PDX types is " + this.maxTypeId);
        }
        return result;
    } finally {
        resumeTX(currentState);
    }
}
Also used : TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) HashMap(java.util.HashMap) CopyOnWriteHashMap(org.apache.geode.internal.util.concurrent.CopyOnWriteHashMap) Map(java.util.Map) InternalGemFireError(org.apache.geode.InternalGemFireError)

Example 15 with TXStateProxy

use of org.apache.geode.internal.cache.TXStateProxy in project geode by apache.

the class TXJUnitTest method testJTASynchronization.

@Test
public void testJTASynchronization() throws CacheException, javax.transaction.NotSupportedException, javax.transaction.RollbackException, javax.transaction.SystemException, javax.transaction.HeuristicMixedException, javax.transaction.HeuristicRollbackException {
    javax.transaction.TransactionManager jtaTxMgr = this.cache.getJTATransactionManager();
    TransactionListener tl = new TransactionListener() {

        @Override
        public void afterCommit(TransactionEvent event) {
            ++listenerAfterCommit;
            te = event;
        }

        @Override
        public void afterFailedCommit(TransactionEvent event) {
            ++listenerAfterFailedCommit;
            te = event;
        }

        @Override
        public void afterRollback(TransactionEvent event) {
            ++listenerAfterRollback;
            te = event;
        }

        @Override
        public void close() {
            ++listenerClose;
        }
    };
    this.txMgr.addListener(tl);
    Synchronization gfTXSync;
    // Test successful JTA commit
    jtaTxMgr.begin();
    this.txMgr.begin();
    {
        TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
        gfTXSync = gfTxMgrImpl.getTXState();
    }
    jtaTxMgr.getTransaction().registerSynchronization(gfTXSync);
    assertEquals(0, this.listenerAfterCommit);
    this.cache.getLogger().info("SWAP:doingCreate");
    this.region.create("syncKey1", "syncVal1");
    jtaTxMgr.commit();
    assertEquals(1, this.listenerAfterCommit);
    assertEquals("syncVal1", this.region.getEntry("syncKey1").getValue());
    try {
        this.txMgr.commit();
        fail("JTA Cache Manager should have called commit!");
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable expected) {
    }
    // Test JTA rollback
    jtaTxMgr.begin();
    this.txMgr.begin();
    {
        TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
        gfTXSync = gfTxMgrImpl.getTXState();
    }
    jtaTxMgr.getTransaction().registerSynchronization(gfTXSync);
    assertEquals(0, this.listenerAfterRollback);
    this.region.put("syncKey2", "syncVal2");
    jtaTxMgr.rollback();
    assertEquals(1, this.listenerAfterRollback);
    assertTrue(!this.region.containsKey("syncKey2"));
    // Test failed JTA commit with suspend
    jtaTxMgr.begin();
    this.txMgr.begin();
    {
        TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
        gfTXSync = gfTxMgrImpl.getTXState();
        jtaTxMgr.getTransaction().registerSynchronization(gfTXSync);
        assertEquals(0, this.listenerAfterFailedCommit);
        this.region.put("syncKey3", "syncVal3");
        assertEquals("syncVal3", this.region.getEntry("syncKey3").getValue());
        TXStateProxy gfTx = gfTxMgrImpl.internalSuspend();
        javax.transaction.Transaction jtaTx = jtaTxMgr.suspend();
        assertNull(jtaTxMgr.getTransaction());
        this.region.put("syncKey3", "syncVal4");
        assertEquals("syncVal4", this.region.getEntry("syncKey3").getValue());
        gfTxMgrImpl.internalResume(gfTx);
        try {
            jtaTxMgr.resume(jtaTx);
        } catch (Exception failure) {
            fail("JTA resume failed");
        }
        assertNotNull(jtaTxMgr.getTransaction());
    }
    assertEquals("syncVal3", this.region.getEntry("syncKey3").getValue());
    try {
        jtaTxMgr.commit();
        fail("Expected JTA manager conflict exception!");
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable expected) {
    }
    assertEquals(1, this.listenerAfterFailedCommit);
    assertEquals("syncVal4", this.region.getEntry("syncKey3").getValue());
    // Test failed JTA commit with a new thread
    jtaTxMgr.begin();
    this.txMgr.begin();
    {
        TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
        gfTXSync = gfTxMgrImpl.getTXState();
        jtaTxMgr.getTransaction().registerSynchronization(gfTXSync);
        assertEquals(1, this.listenerAfterFailedCommit);
        this.region.put("syncKey4", "syncVal3");
        assertEquals("syncVal3", this.region.getEntry("syncKey4").getValue());
        // Create a new thread and have it update the same key, causing
        // a conflict
        final int[] signal = { 0 };
        Thread t = new Thread("non-TX conflict generator") {

            @Override
            public void run() {
                try {
                    region.put("syncKey4", "syncVal4");
                    while (true) synchronized (signal) {
                        signal[0] = 1;
                        signal.notify();
                        signal.wait();
                        if (signal[0] == 0) {
                            break;
                        }
                    }
                } catch (Exception error) {
                    fail("Non-tx thread failure due to: " + error);
                }
            }
        };
        t.start();
        try {
            while (true) synchronized (signal) {
                if (signal[0] == 1) {
                    signal[0] = 0;
                    signal.notify();
                    break;
                } else {
                    signal.wait();
                }
            }
        } catch (InterruptedException dangit) {
            fail("Tx thread waiting for non-tx thread failed due to : " + dangit);
        }
        assertEquals("syncVal3", this.region.getEntry("syncKey4").getValue());
    }
    try {
        jtaTxMgr.commit();
        fail("Expected JTA manager conflict exception!");
    } catch (javax.transaction.HeuristicRollbackException expected) {
    } catch (javax.transaction.RollbackException alsoExpected) {
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable yuk) {
        fail("Did not expect this throwable from JTA commit: " + yuk);
    }
    assertEquals(2, this.listenerAfterFailedCommit);
    assertEquals("syncVal4", this.region.getEntry("syncKey4").getValue());
    this.txMgr.removeListener(tl);
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) Synchronization(javax.transaction.Synchronization) TimeoutException(org.apache.geode.cache.TimeoutException) EntryExistsException(org.apache.geode.cache.EntryExistsException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) CacheWriterException(org.apache.geode.cache.CacheWriterException) TransactionException(org.apache.geode.cache.TransactionException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) FailedSynchronizationException(org.apache.geode.cache.FailedSynchronizationException) NoSuchElementException(java.util.NoSuchElementException) CacheException(org.apache.geode.cache.CacheException) CommitConflictException(org.apache.geode.cache.CommitConflictException) QueryException(org.apache.geode.cache.query.QueryException) TransactionEvent(org.apache.geode.cache.TransactionEvent) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

TXStateProxy (org.apache.geode.internal.cache.TXStateProxy)37 TXManagerImpl (org.apache.geode.internal.cache.TXManagerImpl)20 Test (org.junit.Test)9 CommitConflictException (org.apache.geode.cache.CommitConflictException)8 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)8 IOException (java.io.IOException)6 TransactionException (org.apache.geode.cache.TransactionException)6 InternalCache (org.apache.geode.internal.cache.InternalCache)6 LocalRegion (org.apache.geode.internal.cache.LocalRegion)6 InternalGemFireError (org.apache.geode.InternalGemFireError)5 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)5 EntryExistsException (org.apache.geode.cache.EntryExistsException)5 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)5 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)5 CacheWriterException (org.apache.geode.cache.CacheWriterException)4 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)4 Region (org.apache.geode.cache.Region)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ResourceException (javax.resource.ResourceException)3