Search in sources :

Example 6 with CommitConflictException

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

the class DistTXState method precommit.

/*
   * (non-Javadoc)
   * 
   * @see org.apache.geode.internal.cache.TXStateInterface#commit()
   * 
   * Take Locks Does conflict check on primary ([DISTTX] TODO on primary only) Invoke TxWriter
   */
@Override
public void precommit() throws CommitConflictException, UnsupportedOperationInTransactionException {
    if (logger.isDebugEnabled()) {
        logger.debug("DistTXState.precommit transaction {} is closed {} ", getTransactionId(), this.closed, new Throwable());
    }
    if (this.closed) {
        return;
    }
    synchronized (this.completionGuard) {
        this.completionStarted = true;
    }
    if (onBehalfOfRemoteStub && !proxy.isCommitOnBehalfOfRemoteStub()) {
        throw new UnsupportedOperationInTransactionException(LocalizedStrings.TXState_CANNOT_COMMIT_REMOTED_TRANSACTION.toLocalizedString());
    }
    cleanupNonDirtyRegions();
    /*
     * Lock buckets so they can't be rebalanced then perform the conflict check to fix #43489
     */
    try {
        lockBucketRegions();
    } catch (PrimaryBucketException pbe) {
        // not sure what to do here yet
        RuntimeException re = new TransactionDataRebalancedException(LocalizedStrings.PartitionedRegion_TRANSACTIONAL_DATA_MOVED_DUE_TO_REBALANCING.toLocalizedString());
        re.initCause(pbe);
        throw re;
    }
    if (this.locks == null) {
        reserveAndCheck();
    }
    // For internal testing
    if (this.internalAfterConflictCheck != null) {
        this.internalAfterConflictCheck.run();
    }
    updateRegionVersions();
    generateTailKeysForParallelDispatcherEvents();
    /*
     * If there is a TransactionWriter plugged in, we need to to give it an opportunity to abort the
     * transaction.
     */
    TransactionWriter writer = this.proxy.getTxMgr().getWriter();
    if (!firedWriter && writer != null) {
        try {
            firedWriter = true;
            writer.beforeCommit(getEvent());
        } catch (TransactionWriterException twe) {
            cleanup();
            throw new CommitConflictException(twe);
        } catch (VirtualMachineError err) {
            // cleanup(); this allocates objects so I don't think we can do it -
            // that leaves the TX open, but we are poison pilling so we should be
            // ok??
            SystemFailure.initiateFailure(err);
            // now, so don't let this thread continue.
            throw err;
        } catch (Throwable t) {
            // rollback the transaction!
            cleanup();
            // Whenever you catch Error or Throwable, you must also
            // catch VirtualMachineError (see above). However, there is
            // _still_ a possibility that you are dealing with a cascading
            // error condition, so you also need to check to see if the JVM
            // is still usable:
            SystemFailure.checkFailure();
            throw new CommitConflictException(t);
        }
    }
}
Also used : CommitConflictException(org.apache.geode.cache.CommitConflictException) TransactionWriter(org.apache.geode.cache.TransactionWriter) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) TransactionDataRebalancedException(org.apache.geode.cache.TransactionDataRebalancedException)

Example 7 with CommitConflictException

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

the class ClientTXStateStub method obtainLocalLocks.

/**
   * Lock the keys in a local transaction manager
   * 
   * @throws CommitConflictException if the key is already locked by some other transaction
   */
private void obtainLocalLocks() {
    lockReq = new TXLockRequest();
    InternalCache cache = GemFireCacheImpl.getExisting("");
    for (TransactionalOperation txOp : this.recordedOperations) {
        if (ServerRegionOperation.lockKeyForTx(txOp.getOperation())) {
            TXRegionLockRequest rlr = lockReq.getRegionLockRequest(txOp.getRegionName());
            if (rlr == null) {
                rlr = new TXRegionLockRequestImpl(cache.getRegionByPath(txOp.getRegionName()));
                lockReq.addLocalRequest(rlr);
            }
            if (txOp.getOperation() == ServerRegionOperation.PUT_ALL || txOp.getOperation() == ServerRegionOperation.REMOVE_ALL) {
                rlr.addEntryKeys(txOp.getKeys());
            } else {
                rlr.addEntryKey(txOp.getKey());
            }
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("TX: client localLockRequest: {}", lockReq);
    }
    try {
        lockReq.obtain();
    } catch (CommitConflictException e) {
        // cleanup tx artifacts on server
        rollback();
        throw e;
    }
    if (internalAfterLocalLocks != null) {
        internalAfterLocalLocks.run();
    }
}
Also used : CommitConflictException(org.apache.geode.cache.CommitConflictException) TXRegionLockRequestImpl(org.apache.geode.internal.cache.TXRegionLockRequestImpl) TXLockRequest(org.apache.geode.internal.cache.TXLockRequest) InternalCache(org.apache.geode.internal.cache.InternalCache) TXRegionLockRequest(org.apache.geode.internal.cache.locks.TXRegionLockRequest)

Example 8 with CommitConflictException

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

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

use of org.apache.geode.cache.CommitConflictException 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

CommitConflictException (org.apache.geode.cache.CommitConflictException)28 Test (org.junit.Test)17 TransactionWriterException (org.apache.geode.cache.TransactionWriterException)12 Region (org.apache.geode.cache.Region)11 UnsupportedOperationInTransactionException (org.apache.geode.cache.UnsupportedOperationInTransactionException)10 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)10 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)8 TransactionDataRebalancedException (org.apache.geode.cache.TransactionDataRebalancedException)8 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)7 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)7 TransactionException (org.apache.geode.cache.TransactionException)7 Host (org.apache.geode.test.dunit.Host)7 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)7 VM (org.apache.geode.test.dunit.VM)7 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)7 NamingException (javax.naming.NamingException)6 RollbackException (javax.transaction.RollbackException)6 CacheWriterException (org.apache.geode.cache.CacheWriterException)6 TransactionDataNotColocatedException (org.apache.geode.cache.TransactionDataNotColocatedException)6 TransactionWriter (org.apache.geode.cache.TransactionWriter)6