Search in sources :

Example 16 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)

Example 17 with TXManagerImpl

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

the class TXJUnitTest method testSuspendResume.

@Test
public void testSuspendResume() {
    TXManagerImpl txMgrImpl = (TXManagerImpl) this.txMgr;
    assertTrue(!this.txMgr.exists());
    assertEquals(null, txMgrImpl.internalSuspend());
    TXStateProxy txProxy = null;
    txMgrImpl.internalResume(txProxy);
    assertTrue(!this.txMgr.exists());
    this.txMgr.begin();
    TransactionId origId = this.txMgr.getTransactionId();
    assertTrue(this.txMgr.exists());
    {
        TXStateProxy tx = txMgrImpl.internalSuspend();
        assertTrue(!this.txMgr.exists());
        this.txMgr.begin();
        try {
            txMgrImpl.internalResume(tx);
            fail("expected IllegalStateException");
        } catch (IllegalStateException expected) {
        }
        this.txMgr.rollback();
        assertTrue(!this.txMgr.exists());
        txMgrImpl.internalResume(tx);
    }
    assertTrue(this.txMgr.exists());
    assertEquals(origId, this.txMgr.getTransactionId());
    this.txMgr.rollback();
}
Also used : TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) TransactionId(org.apache.geode.cache.TransactionId) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 18 with TXManagerImpl

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

the class MyTransactionFunction method verifyTxStateAndConflicts.

private void verifyTxStateAndConflicts(RegionFunctionContext ctx) {
    Region custPR = ctx.getDataSet();
    Region orderPR = custPR.getCache().getRegion(PRTransactionDUnitTest.OrderPartitionedRegionName);
    ArrayList args = (ArrayList) ctx.getArguments();
    CustId custId = (CustId) args.get(1);
    CacheTransactionManager mgr = custPR.getCache().getCacheTransactionManager();
    OrderId vOrderId = new OrderId(3000, custId);
    Order vOrder = new Order("vOrder");
    TXManagerImpl mImp = (TXManagerImpl) mgr;
    mImp.begin();
    orderPR.put(vOrderId, vOrder);
    TXStateProxy txState = mImp.internalSuspend();
    Iterator it = txState.getRegions().iterator();
    Assert.assertTrue(txState.getRegions().size() == 1, "Expected 1 region; " + "found:" + txState.getRegions().size());
    LocalRegion lr = (LocalRegion) it.next();
    Assert.assertTrue(lr instanceof BucketRegion);
    TXRegionState txRegion = txState.readRegion(lr);
    TXEntryState txEntry = txRegion.readEntry(txRegion.getEntryKeys().iterator().next());
    mImp.internalResume(txState);
    orderPR.put(vOrderId, new Order("foo"));
    txState = mImp.internalSuspend();
    // since both puts were on same key, verify that
    // TxRegionState and TXEntryState are same
    LocalRegion lr1 = (LocalRegion) txState.getRegions().iterator().next();
    Assert.assertTrue(lr == lr1);
    TXRegionState txRegion1 = txState.readRegion(lr);
    TXEntryState txEntry1 = txRegion1.readEntry(txRegion.getEntryKeys().iterator().next());
    Assert.assertTrue(txEntry == txEntry1);
    // to check for conflicts, start a new transaction, operate on same key,
    // commit the second and expect the first to fail
    mImp.begin();
    orderPR.put(vOrderId, new Order("foobar"));
    mImp.commit();
    // now begin the first
    mImp.internalResume(txState);
    boolean caughtException = false;
    try {
        mImp.commit();
    } catch (CommitConflictException e) {
        caughtException = true;
    }
    if (!caughtException) {
        throw new TestException("An expected exception was not thrown");
    }
}
Also used : Order(org.apache.geode.internal.cache.execute.data.Order) TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) TestException(util.TestException) TXRegionState(org.apache.geode.internal.cache.TXRegionState) ArrayList(java.util.ArrayList) TXEntryState(org.apache.geode.internal.cache.TXEntryState) LocalRegion(org.apache.geode.internal.cache.LocalRegion) OrderId(org.apache.geode.internal.cache.execute.data.OrderId) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) CommitConflictException(org.apache.geode.cache.CommitConflictException) BucketRegion(org.apache.geode.internal.cache.BucketRegion) CustId(org.apache.geode.internal.cache.execute.data.CustId) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) Iterator(java.util.Iterator) LocalRegion(org.apache.geode.internal.cache.LocalRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion)

Example 19 with TXManagerImpl

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

the class MyTransactionFunction method verifyRepeatableRead.

private void verifyRepeatableRead(RegionFunctionContext ctx) {
    Region custPR = ctx.getDataSet();
    Region orderPR = custPR.getCache().getRegion(PRColocationDUnitTest.OrderPartitionedRegionName);
    ArrayList args = (ArrayList) ctx.getArguments();
    CustId custId = (CustId) args.get(1);
    Customer cust = (Customer) args.get(2);
    Assert.assertTrue(custPR.get(custId) == null);
    CacheTransactionManager mgr = custPR.getCache().getCacheTransactionManager();
    TXManagerImpl mImp = (TXManagerImpl) mgr;
    mImp.begin();
    custPR.put(custId, cust);
    Assert.assertTrue(cust.equals(custPR.get(custId)));
    TXStateProxy txState = mImp.internalSuspend();
    Assert.assertTrue(custPR.get(custId) == null);
    mImp.internalResume(txState);
    mImp.commit();
    // change value
    mImp.begin();
    Customer oldCust = (Customer) custPR.get(custId);
    Assert.assertTrue(oldCust.equals(cust));
    txState = mImp.internalSuspend();
    Customer newCust = new Customer("fooNew", "barNew");
    custPR.put(custId, newCust);
    mImp.internalResume(txState);
    Assert.assertTrue(oldCust.equals(custPR.get(custId)));
    mImp.commit();
}
Also used : TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) CustId(org.apache.geode.internal.cache.execute.data.CustId) Customer(org.apache.geode.internal.cache.execute.data.Customer) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) ArrayList(java.util.ArrayList) LocalRegion(org.apache.geode.internal.cache.LocalRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager)

Example 20 with TXManagerImpl

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

the class RemoteOperationMessageTest method noNewTxProcessingAfterTXManagerImplClosed.

@Test
public void noNewTxProcessingAfterTXManagerImplClosed() throws RemoteOperationException {
    txMgr = new TXManagerImpl(null, cache);
    when(msg.checkCacheClosing(dm)).thenReturn(false);
    when(msg.checkDSClosing(dm)).thenReturn(false);
    when(msg.getCache(dm)).thenReturn(cache);
    when(msg.getRegionByPath(cache)).thenReturn(r);
    when(msg.getTXManager(cache)).thenReturn(txMgr);
    when(msg.canParticipateInTransaction()).thenReturn(true);
    when(msg.canStartRemoteTransaction()).thenReturn(true);
    msg.process(dm);
    txMgr.close();
    msg.process(dm);
    verify(msg, times(1)).operateOnRegion(dm, r, startTime);
}
Also used : TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

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