protected void prepare() { EmbeddedTransactionManager dtm = (EmbeddedTransactionManager) tm(0); dtm.getTransaction().runPrepare(); }
protected Future<Void> beginAndPrepareTx(final Object k, final int cacheIndex) { return fork(() -> { try { tm(cacheIndex).begin(); cache(cacheIndex).put(k,"v"); final EmbeddedTransaction transaction = (EmbeddedTransaction) tm(cacheIndex).getTransaction(); transaction.runPrepare(); } catch (Throwable e) { log.errorf(e, "Error preparing transaction for key %s on cache %s", k, cache(cacheIndex)); } }); }
public void testLocksOnPutData() throws Exception { LockTestData tl = lockTestData; Cache<String, String> cache = tl.cache; TransactionManager tm = tl.tm; tm.begin(); cache.putAll(Collections.singletonMap("k", "v")); assertEquals("v", cache.get("k")); final EmbeddedTransaction tx = ((EmbeddedTransactionManager) tm).getTransaction(); assertTrue(tx.runPrepare()); assertLocked("k"); tx.runCommit(false); assertNoLocks(); tm.begin(); assertEquals("v", cache.get("k")); assertNoLocks(); tm.commit(); assertNoLocks(); }
public void testConcurrentWriters() throws Exception { LockTestData tl = lockTestData; Cache<String, String> cache = tl.cache; EmbeddedTransactionManager tm = tl.tm; tm.begin(); cache.put("k", "v"); final EmbeddedTransaction transaction = tm.getTransaction(); assertTrue(transaction.runPrepare()); tm.suspend(); tm.begin(); cache.put("k", "v"); assert !tm.getTransaction().runPrepare(); tm.rollback(); tm.resume(transaction); transaction.runCommit(false); assertNoLocks(); }
public void testNumberOfLocksHeld() throws Exception { EmbeddedTransactionManager tm = (EmbeddedTransactionManager) tm(); tm.begin(); cache.put("key", "value"); tm.getTransaction().runPrepare(); assertAttributeValue("NumberOfLocksHeld", 1); tm.getTransaction().runCommit(false); assertAttributeValue("NumberOfLocksHeld", 0); }
public void testTxAndLockOnSameNode() throws Exception { final Object k = getKeyForCache(0); tm(0).begin(); cache(0).put(k, "v"); EmbeddedTransaction dtm = (EmbeddedTransaction) tm(0).getTransaction(); dtm.runPrepare(); assert lockManager(0).isLocked(k); assert !lockManager(1).isLocked(k); assert !lockManager(2).isLocked(k); dtm.runCommit(false); assertNotLocked(k); assertValue(k, false); }
public void testTxAndLockOnSameNode() throws Exception { Object k0 = new MagicKey("k0", cache(0)); tm(0).begin(); cache(0).put(k0, "v"); EmbeddedTransaction dtm = (EmbeddedTransaction) tm(0).getTransaction(); dtm.runPrepare(); assert lockManager(0).isLocked(k0); assert !lockManager(1).isLocked(k0); assert !lockManager(2).isLocked(k0); dtm.runCommit(false); assertNotLocked(k0); assertValue(k0, false); }
public void testLocksOnRemoveNonexistent() throws Exception { LockTestData tl = lockTestData; Cache<String, String> cache = tl.cache; EmbeddedTransactionManager tm = tl.tm; assert !cache.containsKey("k") : "Should not exist"; tm.begin(); cache.remove("k"); tm.getTransaction().runPrepare(); assertLocked("k"); tm.getTransaction().runCommit(false); assert !cache.containsKey("k") : "Should not exist"; assertNoLocks(); }
protected void testTxAndLockOnDifferentNodes(Operation operation, boolean addFirst, boolean removed) throws Exception { final Object k = getKeyForCache(1); if (addFirst) cache(0).put(k, "v_initial"); assertNotLocked(k); tm(0).begin(); operation.perform(k, 0); EmbeddedTransaction dtm = (EmbeddedTransaction) tm(0).getTransaction(); dtm.runPrepare(); assert !lockManager(0).isLocked(k); assert lockManager(1).isLocked(k); assert !lockManager(2).isLocked(k); dtm.runCommit(false); assertNotLocked(k); assertValue(k, removed); }
protected void testTxAndLockOnDifferentNodes(Operation operation, boolean addFirst, boolean removed) throws Exception { MagicKey k = new MagicKey("k", cache(0)); if (addFirst) cache(0).put(k, "v_initial"); assertNotLocked(k); tm(0).begin(); operation.perform(k, 0); EmbeddedTransaction dtm = (EmbeddedTransaction) tm(0).getTransaction(); dtm.runPrepare(); assert lockManager(0).isLocked(k); assert !lockManager(1).isLocked(k); assert !lockManager(2).isLocked(k); dtm.runCommit(false); assertNotLocked(k); assertValue(k, removed); }
public void testNumberOfLocksAvailable() throws Exception { EmbeddedTransactionManager tm = (EmbeddedTransactionManager) tm(); int initialAvailable = getAttrValue("NumberOfLocksAvailable"); tm.begin(); cache.put("key", "value"); tm.getTransaction().runPrepare(); assertAttributeValue("NumberOfLocksHeld", 1); assertAttributeValue("NumberOfLocksAvailable", initialAvailable - 1); tm.getTransaction().runCommit(true); assertAttributeValue("NumberOfLocksAvailable", initialAvailable); assertAttributeValue("NumberOfLocksHeld", 0); }
@Test(groups = "functional", expectedExceptions = { CacheException.class }) public void testLockAcquisitionTimeout() throws Exception { AdvancedCache<Object, Object> cache1 = advancedCache(0, "syncReplCache"); AdvancedCache<Object, Object> cache2 = advancedCache(1, "syncReplCache"); cache1.getCacheConfiguration().locking().lockAcquisitionTimeout(10); cache2.getCacheConfiguration().locking().lockAcquisitionTimeout(10); TestingUtil.blockUntilViewsReceived(10000, cache1, cache2); // get a lock on cache 2 and hold on to it. EmbeddedTransactionManager tm = (EmbeddedTransactionManager) TestingUtil.getTransactionManager(cache2); tm.begin(); cache2.put("block", "block"); assertTrue(tm.getTransaction().runPrepare()); tm.suspend(); cache1.put("block", "v"); }
cache(1).put(getKeyForCache(0),"v"); final EmbeddedTransaction t = (EmbeddedTransaction) tm(1).getTransaction(); t.runPrepare(); tm(1).suspend();
public void testInitiatorNodeCrashesBeforeCommit() throws Exception { Object k = getKeyForCache(2); tm(1).begin(); cache(1).put(k,"v"); final EmbeddedTransaction transaction = (EmbeddedTransaction) tm(1).getTransaction(); transaction.runPrepare(); tm(1).suspend(); assertEventuallyNotLocked(cache(0), k); assertEventuallyNotLocked(cache(1), k); assertLocked(cache(2), k); checkTxCount(0, 0, 1); checkTxCount(1, 1, 0); checkTxCount(2, 0, 1); killMember(1); assertNotLocked(k); eventually(() -> checkTxCount(0, 0, 0) && checkTxCount(1, 0, 0)); } }
public void testLocksOnPutKeyVal() throws Exception { Cache<String, String> cache = lockTestData.cache; EmbeddedTransactionManager tm = lockTestData.tm; tm.begin(); cache.put("k", "v"); assertTrue(tm.getTransaction().runPrepare()); assertLocked("k"); tm.getTransaction().runCommit(false); assertNoLocks(); tm.begin(); assertEquals("v", cache.get("k")); assertNotLocked("k"); tm.commit(); assertNoLocks(); tm.begin(); cache.remove("k"); assertTrue(tm.getTransaction().runPrepare()); assertLocked("k"); tm.getTransaction().runCommit(false); assertNoLocks(); }
private void testLockMigrationAfterPrepare(Object key) throws Exception { final EmbeddedTransactionManager tm = embeddedTm(ORIGINATOR_INDEX); tm.begin(); originatorCache.put(key, "value"); EmbeddedTransaction tx = tm.getTransaction(); boolean prepareSuccess = tx.runPrepare(); assert prepareSuccess; tm.suspend(); log.trace("Lock transfer happens here"); killCache(); checkNewTransactionFails(key); log.trace("About to commit existing transaction."); tm.resume(tx); tx.runCommit(false); // read the data from the container, just to make sure all replicas are correctly set checkValue(key, "value"); }
public void testMultipleLocksInSameTx() throws Exception { final Object k1 = new MagicKey("k1", cache(0)); final Object k2 = new MagicKey("k2", cache(0)); tm(0).begin(); cache(0).put(k1, "v"); cache(0).put(k2, "v"); EmbeddedTransaction dtm = (EmbeddedTransaction) tm(0).getTransaction(); dtm.runPrepare(); assert lockManager(0).isLocked(k1); assert lockManager(0).isLocked(k2); assert !lockManager(1).isLocked(k1); assert !lockManager(1).isLocked(k2); assert !lockManager(1).isLocked(k2); assert !lockManager(2).isLocked(k2); dtm.runCommit(false); assertNotLocked(k1); assertNotLocked(k2); assertValue(k1, false); assertValue(k2, false); }
public void testLocksOnRemoveData() throws Exception { LockTestData tl = lockTestData; Cache<String, String> cache = tl.cache; EmbeddedTransactionManager tm = tl.tm; // init some data cache.put("k", "v"); cache.put("k2", "v2"); assertEquals("v", cache.get("k")); assertEquals("v2", cache.get("k2")); // remove tm.begin(); cache.remove("k"); cache.remove("k2"); assertTrue(tm.getTransaction().runPrepare()); assertLocked("k"); assertLocked("k2"); tm.getTransaction().runCommit(false); assert cache.isEmpty(); assertNoLocks(); }
public void testSplitBeforeRollback() throws Exception { //the transaction is successfully prepare and then the split happens before the commit phase starts. waitForClusterToForm(OPTIMISTIC_TX_CACHE_NAME); final KeyInfo keyInfo = createKeys(OPTIMISTIC_TX_CACHE_NAME); final Cache<Object, String> originator = cache(0, OPTIMISTIC_TX_CACHE_NAME); final EmbeddedTransactionManager transactionManager = (EmbeddedTransactionManager) originator.getAdvancedCache().getTransactionManager(); transactionManager.begin(); final EmbeddedTransaction transaction = transactionManager.getTransaction(); keyInfo.putFinalValue(originator); AssertJUnit.assertTrue(transaction.runPrepare()); transactionManager.suspend(); SplitMode.BOTH_DEGRADED.split(this); transactionManager.resume(transaction); transaction.runCommit(true); assertEventuallyNotLocked(cache(1, OPTIMISTIC_TX_CACHE_NAME), keyInfo.getKey1()); assertLocked(cache(2, OPTIMISTIC_TX_CACHE_NAME), keyInfo.getKey2()); mergeCluster(OPTIMISTIC_TX_CACHE_NAME); finalAsserts(OPTIMISTIC_TX_CACHE_NAME, keyInfo, INITIAL_VALUE); }
public void testSplitBeforeCommit() throws Exception { //the transaction is successfully prepare and then the split happens before the commit phase starts. waitForClusterToForm(OPTIMISTIC_TX_CACHE_NAME); final KeyInfo keyInfo = createKeys(OPTIMISTIC_TX_CACHE_NAME); final Cache<Object, String> originator = cache(0, OPTIMISTIC_TX_CACHE_NAME); final EmbeddedTransactionManager transactionManager = (EmbeddedTransactionManager) originator.getAdvancedCache().getTransactionManager(); transactionManager.begin(); final EmbeddedTransaction transaction = transactionManager.getTransaction(); keyInfo.putFinalValue(originator); AssertJUnit.assertTrue(transaction.runPrepare()); transactionManager.suspend(); SplitMode.BOTH_DEGRADED.split(this); transactionManager.resume(transaction); transaction.runCommit(false); assertLocked(cache(1, OPTIMISTIC_TX_CACHE_NAME), keyInfo.getKey1()); assertLocked(cache(2, OPTIMISTIC_TX_CACHE_NAME), keyInfo.getKey2()); mergeCluster(OPTIMISTIC_TX_CACHE_NAME); finalAsserts(OPTIMISTIC_TX_CACHE_NAME, keyInfo, FINAL_VALUE); }