Search in sources :

Example 6 with TXManagerImpl

use of org.apache.geode.internal.cache.TXManagerImpl 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)

Example 7 with TXManagerImpl

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

the class TXJUnitTest method testConflicts.

@Test
public void testConflicts() throws CacheException {
    final TXManagerImpl txMgrImpl = (TXManagerImpl) this.txMgr;
    TXStateProxy tx;
    // try a put with no conflict to show that commit works
    txMgrImpl.begin();
    this.region.put("key1", "value1");
    txMgrImpl.commit();
    assertEquals("value1", this.region.get("key1"));
    this.region.localDestroy("key1");
    // now try a put with a conflict and make sure it is detected
    txMgrImpl.begin();
    this.region.put("key1", "value1");
    tx = txMgrImpl.internalSuspend();
    // do a non-tx put to force conflict
    this.region.put("key1", "value2");
    txMgrImpl.internalResume(tx);
    try {
        txMgrImpl.commit();
        fail("expected CommitConflictException");
    } catch (CommitConflictException ex) {
    }
    assertEquals("value2", this.region.get("key1"));
    this.region.localDestroy("key1");
    // slightly difference version where value already exists in cmt state
    this.region.put("key1", "value0");
    txMgrImpl.begin();
    this.region.put("key1", "value1");
    txMgrImpl.commit();
    assertEquals("value1", this.region.get("key1"));
    this.region.localDestroy("key1");
    // now the conflict
    this.region.put("key1", "value0");
    txMgrImpl.begin();
    this.region.put("key1", "value1");
    tx = txMgrImpl.internalSuspend();
    // do a non-tx put to force conflict
    this.region.put("key1", "value2");
    txMgrImpl.internalResume(tx);
    try {
        txMgrImpl.commit();
        fail("expected CommitConflictException");
    } catch (CommitConflictException ex) {
    }
    assertEquals("value2", this.region.get("key1"));
    this.region.localDestroy("key1");
    // now test create
    txMgrImpl.begin();
    this.region.create("key1", "value1");
    txMgrImpl.commit();
    assertEquals("value1", this.region.get("key1"));
    this.region.localDestroy("key1");
    // now try a create with a conflict and make sure it is detected
    txMgrImpl.begin();
    this.region.create("key1", "value1");
    tx = txMgrImpl.internalSuspend();
    // do a non-tx put to force conflict
    this.region.put("key1", "value2");
    txMgrImpl.internalResume(tx);
    try {
        txMgrImpl.commit();
        fail("expected CommitConflictException");
    } catch (CommitConflictException ex) {
    }
    assertEquals("value2", this.region.get("key1"));
    this.region.localDestroy("key1");
    // test localInvalidate
    this.region.put("key1", "value0");
    txMgrImpl.begin();
    this.region.localInvalidate("key1");
    txMgrImpl.commit();
    assertTrue(this.region.containsKey("key1") && !this.region.containsValueForKey("key1"));
    this.region.localDestroy("key1");
    // now the conflict
    this.region.put("key1", "value0");
    txMgrImpl.begin();
    this.region.localInvalidate("key1");
    tx = txMgrImpl.internalSuspend();
    // do a non-tx put to force conflict
    this.region.put("key1", "value2");
    txMgrImpl.internalResume(tx);
    try {
        txMgrImpl.commit();
        fail("expected CommitConflictException");
    } catch (CommitConflictException ex) {
    }
    assertEquals("value2", this.region.get("key1"));
    this.region.localDestroy("key1");
    // test invalidate
    this.region.put("key1", "value0");
    txMgrImpl.begin();
    this.region.invalidate("key1");
    txMgrImpl.commit();
    assertTrue(this.region.containsKey("key1") && !this.region.containsValueForKey("key1"));
    this.region.localDestroy("key1");
    // now the conflict
    this.region.put("key1", "value0");
    txMgrImpl.begin();
    this.region.invalidate("key1");
    tx = txMgrImpl.internalSuspend();
    // do a non-tx put to force conflict
    this.region.put("key1", "value2");
    txMgrImpl.internalResume(tx);
    try {
        txMgrImpl.commit();
        fail("expected CommitConflictException");
    } catch (CommitConflictException ex) {
    }
    assertEquals("value2", this.region.get("key1"));
    this.region.localDestroy("key1");
    // check C + DD is a NOOP that still gets conflict if non-tx entry created */
    this.txMgr.begin();
    this.region.create("newKey", "valueTX");
    tx = txMgrImpl.internalSuspend();
    this.region.create("newKey", "valueNONTX");
    txMgrImpl.internalResume(tx);
    this.region.destroy("newKey");
    assertTrue(!this.region.containsKey("key1"));
    try {
        txMgrImpl.commit();
        fail("expected CommitConflictException");
    } catch (CommitConflictException ex) {
    }
    assertEquals("valueNONTX", this.region.get("newKey"));
    this.region.localDestroy("newKey");
    // check C + LD is a NOOP that still gets conflict if non-tx entry created */
    this.txMgr.begin();
    this.region.create("newKey", "valueTX");
    tx = txMgrImpl.internalSuspend();
    this.region.create("newKey", "valueNONTX");
    txMgrImpl.internalResume(tx);
    this.region.localDestroy("newKey");
    assertTrue(!this.region.containsKey("key1"));
    try {
        txMgrImpl.commit();
        fail("expected CommitConflictException");
    } catch (CommitConflictException ex) {
    }
    assertEquals("valueNONTX", this.region.get("newKey"));
    this.region.localDestroy("newKey");
    // test localDestroy
    this.region.put("key1", "value0");
    txMgrImpl.begin();
    this.region.localDestroy("key1");
    txMgrImpl.commit();
    assertTrue(!this.region.containsKey("key1"));
    // now the conflict
    this.region.put("key1", "value0");
    txMgrImpl.begin();
    this.region.localDestroy("key1");
    tx = txMgrImpl.internalSuspend();
    // do a non-tx put to force conflict
    this.region.put("key1", "value2");
    txMgrImpl.internalResume(tx);
    try {
        txMgrImpl.commit();
        fail("expected CommitConflictException");
    } catch (CommitConflictException ex) {
    }
    assertEquals("value2", this.region.get("key1"));
    this.region.localDestroy("key1");
    // test destroy
    this.region.put("key1", "value0");
    txMgrImpl.begin();
    this.region.destroy("key1");
    txMgrImpl.commit();
    assertTrue(!this.region.containsKey("key1"));
    // now the conflict
    this.region.put("key1", "value0");
    txMgrImpl.begin();
    this.region.destroy("key1");
    tx = txMgrImpl.internalSuspend();
    // do a non-tx put to force conflict
    this.region.put("key1", "value2");
    txMgrImpl.internalResume(tx);
    try {
        txMgrImpl.commit();
        fail("expected CommitConflictException");
    } catch (CommitConflictException ex) {
    }
    assertEquals("value2", this.region.get("key1"));
    this.region.localDestroy("key1");
    checkUserAttributeConflict(txMgrImpl);
    // make sure non-tx local-invalidate followed by invalidate
    // does not cause conflict
    this.region.create("key1", "val1");
    this.region.localInvalidate("key1");
    txMgrImpl.begin();
    this.region.put("key1", "txVal1");
    tx = txMgrImpl.internalSuspend();
    this.region.invalidate("key1");
    txMgrImpl.internalResume(tx);
    txMgrImpl.commit();
    assertEquals("txVal1", this.region.getEntry("key1").getValue());
    this.region.destroy("key1");
    // now try a put and a region destroy.
    txMgrImpl.begin();
    this.region.create("key1", "value1");
    TXStateProxy tis = txMgrImpl.internalSuspend();
    // non-tx
    this.region.localDestroyRegion();
    txMgrImpl.internalResume(tis);
    try {
        txMgrImpl.commit();
        fail("expected CommitConflictException");
    } catch (TransactionException ex) {
    }
}
Also used : CommitConflictException(org.apache.geode.cache.CommitConflictException) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) TransactionException(org.apache.geode.cache.TransactionException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 8 with TXManagerImpl

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

the class TXJUnitTest method testLoader.

@Test
public void testLoader() throws CacheException {
    AttributesMutator<String, String> mutator = this.region.getAttributesMutator();
    mutator.setCacheLoader(new CacheLoader<String, String>() {

        int count = 0;

        @Override
        public String load(LoaderHelper helper) throws CacheLoaderException {
            count++;
            return "LV " + count;
        }

        @Override
        public void close() {
        }
    });
    LocalRegion reg1 = (LocalRegion) this.region;
    if (isPR())
        ((PartitionedRegion) reg1).setHaveCacheLoader();
    assertTrue(!reg1.containsKey("key1"));
    assertEquals("LV 1", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 1", reg1.getEntry("key1").getValue());
    reg1.localDestroy("key1");
    // TX load: only TX
    this.txMgr.begin();
    assertTrue(!reg1.containsKey("key1"));
    assertEquals("LV 2", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 2", reg1.getEntry("key1").getValue());
    this.txMgr.rollback();
    assertTrue(!reg1.containsKey("key1"));
    // assertIndexDetailsEquals("LV 2", reg1.getEntry("key1").getValue());
    // reg1.localDestroy("key1");
    // TX load: commit check
    this.txMgr.begin();
    assertTrue(!reg1.containsKey("key1"));
    assertEquals("LV 3", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 3", reg1.getEntry("key1").getValue());
    this.txMgr.commit();
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 3", reg1.getEntry("key1").getValue());
    reg1.localDestroy("key1");
    // TX load YES conflict: no-initial state, tx create, committed load
    {
        final TXManagerImpl txMgrImpl = (TXManagerImpl) this.txMgr;
        TXStateProxy tx;
        this.txMgr.begin();
        reg1.create("key1", "txValue");
        assertEquals("txValue", reg1.getEntry("key1").getValue());
        tx = txMgrImpl.internalSuspend();
        assertTrue(!reg1.containsKey("key1"));
        assertEquals("LV 4", reg1.get("key1"));
        assertTrue(reg1.containsKey("key1"));
        txMgrImpl.internalResume(tx);
        assertEquals("txValue", reg1.getEntry("key1").getValue());
        assertEquals("txValue", reg1.get("key1"));
        try {
            this.txMgr.commit();
            fail("Should have thrown a commit conflict");
        } catch (CommitConflictException cce) {
        // this is what we want
        }
        assertEquals("LV 4", reg1.getEntry("key1").getValue());
        assertEquals("LV 4", reg1.get("key1"));
        reg1.localDestroy("key1");
    }
    // TX load no conflict: load initial state, tx update
    assertEquals("LV 5", reg1.get("key1"));
    this.txMgr.begin();
    reg1.put("key1", "txValue");
    assertEquals("txValue", reg1.get("key1"));
    assertEquals("txValue", reg1.getEntry("key1").getValue());
    // no conflict! Make sure committed value overrode initial state
    this.txMgr.commit();
    assertEquals("txValue", reg1.getEntry("key1").getValue());
    assertEquals("txValue", reg1.get("key1"));
    reg1.localDestroy("key1");
    // TX load no conflict: load initial state, tx load
    assertEquals("LV 6", reg1.get("key1"));
    this.txMgr.begin();
    reg1.localInvalidate("key1");
    assertEquals("LV 7", reg1.get("key1"));
    assertEquals("LV 7", reg1.getEntry("key1").getValue());
    // no conflict! Make sure committed value overrode initial state
    this.txMgr.commit();
    assertEquals("LV 7", reg1.getEntry("key1").getValue());
    assertEquals("LV 7", reg1.get("key1"));
    reg1.localDestroy("key1");
    // TX load no conflict: no initial state, tx load, committed create
    {
        final TXManagerImpl txMgrImpl = (TXManagerImpl) this.txMgr;
        TXStateProxy tx;
        this.txMgr.begin();
        assertEquals("LV 8", reg1.get("key1"));
        assertEquals("LV 8", reg1.getEntry("key1").getValue());
        tx = txMgrImpl.internalSuspend();
        assertTrue(!reg1.containsKey("key1"));
        reg1.create("key1", "txValue");
        assertTrue(reg1.containsKey("key1"));
        assertEquals("txValue", reg1.get("key1"));
        txMgrImpl.internalResume(tx);
        assertEquals("LV 8", reg1.getEntry("key1").getValue());
        try {
            // should conflict
            this.txMgr.commit();
            fail("Should have thrown cce");
        } catch (CommitConflictException cce) {
        // this is what we want
        }
        assertEquals("txValue", reg1.getEntry("key1").getValue());
        assertEquals("txValue", reg1.get("key1"));
        reg1.localDestroy("key1");
    }
    // TX load conflict: no-inital state, tx load->update, committed update
    {
        final TXManagerImpl txMgrImpl = (TXManagerImpl) this.txMgr;
        TXStateProxy tx;
        this.txMgr.begin();
        reg1.create("key1", "txValue");
        tx = txMgrImpl.internalSuspend();
        assertTrue(!reg1.containsKey("key1"));
        // new transaction, load(create) + put
        this.txMgr.begin();
        assertEquals("LV 9", reg1.get("key1"));
        assertEquals("LV 9", reg1.getEntry("key1").getValue());
        reg1.put("key1", "txValue2");
        assertEquals("txValue2", reg1.get("key1"));
        assertEquals("txValue2", reg1.getEntry("key1").getValue());
        this.txMgr.commit();
        assertTrue(reg1.containsKey("key1"));
        assertEquals("txValue2", reg1.get("key1"));
        assertEquals("txValue2", reg1.getEntry("key1").getValue());
        txMgrImpl.internalResume(tx);
        assertEquals("txValue", reg1.getEntry("key1").getValue());
        assertEquals("txValue", reg1.get("key1"));
        try {
            this.txMgr.commit();
            fail("expected CommitConflictException!");
        } catch (CommitConflictException expected) {
        }
        assertTrue(reg1.containsKey("key1"));
        assertEquals("txValue2", reg1.get("key1"));
        assertEquals("txValue2", reg1.getEntry("key1").getValue());
        reg1.localDestroy("key1");
    }
    // TX load repeat: no-initial state, tx load->get
    this.txMgr.begin();
    assertTrue(!reg1.containsKey("key1"));
    // first invocation
    assertEquals("LV 10", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 10", reg1.getEntry("key1").getValue());
    // second invocation
    assertEquals("LV 10", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 10", reg1.getEntry("key1").getValue());
    this.txMgr.rollback();
    // TX load repeat: no-initial state, tx load->localDestory->load
    this.txMgr.begin();
    assertTrue(!reg1.containsKey("key1"));
    // first invocation
    assertEquals("LV 11", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 11", reg1.getEntry("key1").getValue());
    reg1.localDestroy("key1");
    // second invocation
    assertEquals("LV 12", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 12", reg1.getEntry("key1").getValue());
    this.txMgr.rollback();
    // TX load repeat: no-initial state: tx load->destroy->load
    this.txMgr.begin();
    assertTrue(!reg1.containsKey("key1"));
    // first invocation
    assertEquals("LV 13", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 13", reg1.getEntry("key1").getValue());
    reg1.destroy("key1");
    // second invocation
    assertEquals("LV 14", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 14", reg1.getEntry("key1").getValue());
    this.txMgr.rollback();
    // TX load repeat: no-initial state, tx load->localInvalidate->load
    this.txMgr.begin();
    assertTrue(!reg1.containsKey("key1"));
    // first invocation
    assertEquals("LV 15", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 15", reg1.getEntry("key1").getValue());
    reg1.localInvalidate("key1");
    // second invocation
    assertEquals("LV 16", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 16", reg1.getEntry("key1").getValue());
    this.txMgr.rollback();
    // TX load repeat: no-initial, tx load->invalidate->load
    this.txMgr.begin();
    assertTrue(!reg1.containsKey("key1"));
    // first invocation
    assertEquals("LV 17", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 17", reg1.getEntry("key1").getValue());
    reg1.invalidate("key1");
    // second invocation
    assertEquals("LV 18", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 18", reg1.getEntry("key1").getValue());
    this.txMgr.rollback();
    // TX load repeat: invalid entry initial state, tx load->get
    reg1.create("key1", null);
    this.txMgr.begin();
    assertTrue(reg1.containsKey("key1"));
    assertNull(reg1.getEntry("key1").getValue());
    // first invocation
    assertEquals("LV 19", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 19", reg1.getEntry("key1").getValue());
    // second invocation
    assertEquals("LV 19", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 19", reg1.getEntry("key1").getValue());
    this.txMgr.rollback();
    // TX load repeat: invalid entry initial state, tx load->localDestory->load
    this.txMgr.begin();
    assertTrue(reg1.containsKey("key1"));
    assertNull(reg1.getEntry("key1").getValue());
    // first invocation
    assertEquals("LV 20", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 20", reg1.getEntry("key1").getValue());
    reg1.localDestroy("key1");
    // second invocation
    assertEquals("LV 21", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 21", reg1.getEntry("key1").getValue());
    this.txMgr.rollback();
    // TX load repeat: invalid entry initial state: tx load->destroy->load
    this.txMgr.begin();
    assertTrue(reg1.containsKey("key1"));
    assertNull(reg1.getEntry("key1").getValue());
    // first invocation
    assertEquals("LV 22", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 22", reg1.getEntry("key1").getValue());
    reg1.destroy("key1");
    // second invocation
    assertEquals("LV 23", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 23", reg1.getEntry("key1").getValue());
    this.txMgr.rollback();
    // TX load repeat: invalid entry initial state, tx load->localInvalidate->load
    this.txMgr.begin();
    assertTrue(reg1.containsKey("key1"));
    assertNull(reg1.getEntry("key1").getValue());
    // first invocation
    assertEquals("LV 24", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 24", reg1.getEntry("key1").getValue());
    reg1.localInvalidate("key1");
    // second invocation
    assertEquals("LV 25", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 25", reg1.getEntry("key1").getValue());
    this.txMgr.rollback();
    // TX load repeat: invalid entry initial state, tx load->invalidate->load
    this.txMgr.begin();
    assertTrue(reg1.containsKey("key1"));
    assertNull(reg1.getEntry("key1").getValue());
    // first invocation
    assertEquals("LV 26", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 26", reg1.getEntry("key1").getValue());
    reg1.invalidate("key1");
    // second invocation
    assertEquals("LV 27", reg1.get("key1"));
    assertTrue(reg1.containsKey("key1"));
    assertEquals("LV 27", reg1.getEntry("key1").getValue());
    this.txMgr.rollback();
    // Make sure a load does not conflict with the region being destroyed
    this.txMgr.begin();
    assertEquals("LV 28", reg1.get("key2"));
    this.txMgr.commit();
    // non-tx region op
    reg1.localDestroyRegion();
// reg1 is now destroyed
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) CommitConflictException(org.apache.geode.cache.CommitConflictException) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 9 with TXManagerImpl

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

the class TXJUnitTest method testPutAllSupported.

/**
   * make sure that we throw an UnsupportedOperationInTransactionException
   * 
   * @throws Exception
   */
@Test
public void testPutAllSupported() throws Exception {
    TXManagerImpl ctm = this.cache.getTxManager();
    AttributesFactory af = new AttributesFactory();
    Region r = this.cache.createRegion("dRegion", af.create());
    PartitionAttributes pa = new PartitionAttributesFactory().setRedundantCopies(0).setTotalNumBuckets(1).create();
    af.setPartitionAttributes(pa);
    Region pr = this.cache.createRegion("prRegion", af.create());
    Map map = new HashMap();
    map.put("stuff", "junk");
    map.put("stuff2", "junk2");
    ctm.begin();
    pr.putAll(map);
    r.putAll(map);
    TXStateProxy tx = ctm.internalSuspend();
    assertTrue(!pr.containsKey("stuff"));
    assertTrue(!r.containsKey("stuff"));
    ctm.internalResume(tx);
    ctm.commit();
    assertTrue(pr.containsKey("stuff"));
    assertTrue(r.containsKey("stuff"));
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) HashMap(java.util.HashMap) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) PartitionAttributes(org.apache.geode.cache.PartitionAttributes) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 10 with TXManagerImpl

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

the class TXJUnitTest method testStats.

@Test
public void testStats() throws CacheException {
    final int SLEEP_MS = 250;
    // final int OP_TIME = 0; // ns // changed form 10 to 0 because on fater platforms
    // and low resolution clocks this test will fail.
    final CachePerfStats stats = this.cache.getCachePerfStats();
    class statsValidator {

        private long txSuccessLifeTime;

        private long txFailedLifeTime;

        private long txRollbackLifeTime;

        private int txCommits;

        private int txFailures;

        private int txRollbacks;

        private long txCommitTime;

        private long txFailureTime;

        private long txRollbackTime;

        private int txCommitChanges;

        private int txFailureChanges;

        private int txRollbackChanges;

        private CachePerfStats stats;

        private statsValidator(CachePerfStats stats) {
            this.stats = stats;
        }

        private void reset() {
            this.txSuccessLifeTime = this.stats.getTxSuccessLifeTime();
            this.txFailedLifeTime = this.stats.getTxFailedLifeTime();
            this.txRollbackLifeTime = this.stats.getTxRollbackLifeTime();
            this.txCommits = this.stats.getTxCommits();
            this.txFailures = this.stats.getTxFailures();
            this.txRollbacks = this.stats.getTxRollbacks();
            this.txCommitTime = this.stats.getTxCommitTime();
            this.txFailureTime = this.stats.getTxFailureTime();
            this.txRollbackTime = this.stats.getTxRollbackTime();
            this.txCommitChanges = this.stats.getTxCommitChanges();
            this.txFailureChanges = this.stats.getTxFailureChanges();
            this.txRollbackChanges = this.stats.getTxRollbackChanges();
        }

        private void setTxSuccessLifeTime(long txSuccessLifeTime) {
            this.txSuccessLifeTime = txSuccessLifeTime;
        }

        private void setTxFailedLifeTime(long txFailedLifeTime) {
            this.txFailedLifeTime = txFailedLifeTime;
        }

        private void setTxRollbackLifeTime(long txRollbackLifeTime) {
            this.txRollbackLifeTime = txRollbackLifeTime;
        }

        private void setTxCommits(int txCommits) {
            this.txCommits = txCommits;
        }

        private void setTxFailures(int txFailures) {
            this.txFailures = txFailures;
        }

        private void setTxRollbacks(int txRollbacks) {
            this.txRollbacks = txRollbacks;
        }

        private void setTxCommitTime(long txCommitTime) {
            this.txCommitTime = txCommitTime;
        }

        private void setTxFailureTime(long txFailureTime) {
            this.txFailureTime = txFailureTime;
        }

        private void setTxRollbackTime(long txRollbackTime) {
            this.txRollbackTime = txRollbackTime;
        }

        private void setTxCommitChanges(int txCommitChanges) {
            this.txCommitChanges = txCommitChanges;
        }

        private void setTxFailureChanges(int txFailureChanges) {
            this.txFailureChanges = txFailureChanges;
        }

        private void setTxRollbackChanges(int txRollbackChanges) {
            this.txRollbackChanges = txRollbackChanges;
        }

        private void assertValid() {
            assertEquals(this.txRollbacks, this.stats.getTxRollbacks());
            assertEquals(this.txRollbackChanges, this.stats.getTxRollbackChanges());
            if (Boolean.getBoolean(DistributionConfig.GEMFIRE_PREFIX + "cache.enable-time-statistics")) {
                assertTrue(this.txRollbackTime <= this.stats.getTxRollbackTime());
                // assertTrue(this.txRollbackLifeTime+((SLEEP_MS-10)*1000000) <=
                // this.stats.getTxRollbackLifeTime());
                assertTrue("RollbackLifeTime " + this.txRollbackLifeTime + " is not <= " + this.stats.getTxRollbackLifeTime(), this.txRollbackLifeTime <= this.stats.getTxRollbackLifeTime());
                assertTrue(this.txCommitTime <= this.stats.getTxCommitTime());
                assertTrue(this.txSuccessLifeTime <= this.stats.getTxSuccessLifeTime());
                assertTrue(this.txFailureTime <= this.stats.getTxFailureTime());
                assertTrue("FailedLifeTime " + this.txFailedLifeTime + " is not <= " + this.stats.getTxFailedLifeTime(), this.txFailedLifeTime <= this.stats.getTxFailedLifeTime());
            }
            assertEquals(this.txCommits, this.stats.getTxCommits());
            assertEquals(this.txCommitChanges, this.stats.getTxCommitChanges());
            assertEquals(this.txFailures, this.stats.getTxFailures());
            assertEquals(this.txFailureChanges, this.stats.getTxFailureChanges());
        }
    }
    statsValidator statsVal = new statsValidator(stats);
    // Zero and non-zero rollback stats test
    int i;
    long testRollbackLifeTime = 0, testTotalTx = 0;
    for (i = 0; i < 2; ++i) {
        statsVal.reset();
        statsVal.setTxRollbacks(stats.getTxRollbacks() + 1);
        statsVal.setTxRollbackLifeTime(stats.getTxRollbackLifeTime() + ((SLEEP_MS - 20) * 1000000));
        final long beforeBegin = NanoTimer.getTime();
        this.txMgr.begin();
        final long afterBegin = NanoTimer.getTime();
        pause(SLEEP_MS);
        if (i > 0) {
            statsVal.setTxRollbackChanges(stats.getTxRollbackChanges() + 2);
            this.region.put("stats1", "stats rollback1");
            this.region.put("stats2", "stats rollback2");
        }
        statsVal.setTxRollbackTime(stats.getTxRollbackTime());
        final long beforeRollback = NanoTimer.getTime();
        this.txMgr.rollback();
        final long afterRollback = NanoTimer.getTime();
        final long statsRollbackLifeTime = stats.getTxRollbackLifeTime();
        testRollbackLifeTime += beforeRollback - afterBegin;
        // bruce - time based stats are disabled by default
        String p = (String) cache.getDistributedSystem().getProperties().get(DistributionConfig.GEMFIRE_PREFIX + "enable-time-statistics");
        if (p != null && Boolean.getBoolean(p)) {
            assertTrue("Local RollbackLifeTime assertion:  " + testRollbackLifeTime + " is not <= " + statsRollbackLifeTime, testRollbackLifeTime <= statsRollbackLifeTime);
        }
        testTotalTx += afterRollback - beforeBegin;
        final long totalTXMinusRollback = testTotalTx - stats.getTxRollbackTime();
        if (Boolean.getBoolean(DistributionConfig.GEMFIRE_PREFIX + "cache.enable-time-statistics")) {
            assertTrue("Total Tx Minus Rollback assertion:  " + totalTXMinusRollback + " is not >= " + statsRollbackLifeTime, totalTXMinusRollback >= statsRollbackLifeTime);
        }
        statsVal.assertValid();
    }
    // Zero and non-zero commit stats test
    for (i = 0; i < 2; ++i) {
        statsVal.reset();
        statsVal.setTxCommits(stats.getTxCommits() + 1);
        statsVal.setTxSuccessLifeTime(stats.getTxSuccessLifeTime() + ((SLEEP_MS - 10) * 1000000));
        this.txMgr.begin();
        pause(SLEEP_MS);
        if (i > 0) {
            statsVal.setTxCommitChanges(stats.getTxCommitChanges() + 2);
            this.region.put("stats1", "commit1");
            this.region.put("stats2", "commit2");
        }
        try {
            statsVal.setTxCommitTime(stats.getTxCommitTime());
            this.txMgr.commit();
        } catch (CommitConflictException ex) {
            fail("unexpected " + ex);
        }
        statsVal.assertValid();
    }
    // Non-zero failed commit stats
    TXManagerImpl txMgrImpl = (TXManagerImpl) this.txMgr;
    statsVal.reset();
    statsVal.setTxFailures(stats.getTxFailures() + 1);
    statsVal.setTxFailureChanges(stats.getTxFailureChanges() + 2);
    statsVal.setTxFailedLifeTime(stats.getTxFailedLifeTime() + ((SLEEP_MS - 20) * 1000000));
    this.region.put("stats3", "stats fail3");
    this.txMgr.begin();
    this.region.put("stats1", "stats fail1");
    this.region.put("stats2", "stats fail2");
    try {
        this.region.create("stats3", "try stats3");
        fail("expected EntryExistsException");
    } catch (EntryExistsException ok) {
    }
    // begin other tx simulation
    TXStateProxy tx = txMgrImpl.internalSuspend();
    this.region.put("stats1", "stats success1");
    this.region.put("stats2", "stats success2");
    txMgrImpl.internalResume(tx);
    // end other tx simulation
    pause(SLEEP_MS);
    try {
        statsVal.setTxFailureTime(stats.getTxFailureTime());
        this.txMgr.commit();
        fail("expected CommitConflictException");
    } catch (CommitConflictException ex) {
    // expected failure
    }
    statsVal.assertValid();
}
Also used : CommitConflictException(org.apache.geode.cache.CommitConflictException) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) CachePerfStats(org.apache.geode.internal.cache.CachePerfStats) EntryExistsException(org.apache.geode.cache.EntryExistsException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

TXManagerImpl (org.apache.geode.internal.cache.TXManagerImpl)31 TXStateProxy (org.apache.geode.internal.cache.TXStateProxy)20 Test (org.junit.Test)13 LocalRegion (org.apache.geode.internal.cache.LocalRegion)10 CommitConflictException (org.apache.geode.cache.CommitConflictException)8 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)8 Region (org.apache.geode.cache.Region)7 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)6 InternalCache (org.apache.geode.internal.cache.InternalCache)6 IOException (java.io.IOException)5 CacheException (org.apache.geode.cache.CacheException)5 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)5 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)5 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)5 EntryExistsException (org.apache.geode.cache.EntryExistsException)4 TransactionException (org.apache.geode.cache.TransactionException)4 TXId (org.apache.geode.internal.cache.TXId)4 AttributesFactory (org.apache.geode.cache.AttributesFactory)3 CacheWriterException (org.apache.geode.cache.CacheWriterException)3 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)3