use of io.seata.server.lock.file.FileLockManagerForTest in project seata by seata.
the class SessionStoreTest method testRestoredFromFile.
/**
* Test restored from file.
*
* @throws Exception the exception
*/
@Test
public void testRestoredFromFile() throws Exception {
try {
SessionHolder.init("file");
GlobalSession globalSession = new GlobalSession("demo-app", "my_test_tx_group", "test", 6000);
String xid = XID.generateXID(globalSession.getTransactionId());
globalSession.setXid(xid);
globalSession.addSessionLifecycleListener(SessionHolder.getRootSessionManager());
globalSession.begin();
BranchSession branchSession1 = SessionHelper.newBranchByGlobal(globalSession, BranchType.AT, RESOURCE_ID, "ta:1,2;tb:3", "xxx");
branchSession1.setXid(xid);
branchSession1.lock();
globalSession.addBranch(branchSession1);
LockManager lockManager = new FileLockManagerForTest();
String otherXID = XID.generateXID(0L);
Assertions.assertFalse(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1"));
Assertions.assertFalse(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:2"));
Assertions.assertFalse(lockManager.isLockable(otherXID, RESOURCE_ID, "tb:3"));
Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:4"));
Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "tb:5"));
lockManager.cleanAllLocks();
Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1"));
Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:2"));
Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "tb:3"));
// Re-init SessionHolder: restore sessions from file
SessionHolder.init("file");
long tid = globalSession.getTransactionId();
GlobalSession reloadSession = SessionHolder.findGlobalSession(globalSession.getXid());
Assertions.assertNotNull(reloadSession);
Assertions.assertFalse(globalSession == reloadSession);
Assertions.assertEquals(globalSession.getApplicationId(), reloadSession.getApplicationId());
Assertions.assertFalse(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1"));
Assertions.assertFalse(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:2"));
Assertions.assertFalse(lockManager.isLockable(otherXID, RESOURCE_ID, "tb:3"));
Assertions.assertTrue(lockManager.isLockable(xid, RESOURCE_ID, "tb:3"));
// clear
reloadSession.addSessionLifecycleListener(SessionHolder.getRootSessionManager());
reloadSession.end();
} finally {
SessionHolder.destroy();
}
}
use of io.seata.server.lock.file.FileLockManagerForTest in project seata by seata.
the class SessionStoreTest method testRestoredFromFileAsyncCommitting.
/**
* Test restored from file async committing.
*
* @throws Exception the exception
*/
@Test
public void testRestoredFromFileAsyncCommitting() throws Exception {
try {
SessionHolder.init("file");
GlobalSession globalSession = new GlobalSession("demo-app", "my_test_tx_group", "test", 6000);
String xid = XID.generateXID(globalSession.getTransactionId());
globalSession.setXid(xid);
globalSession.addSessionLifecycleListener(SessionHolder.getRootSessionManager());
globalSession.begin();
BranchSession branchSession1 = SessionHelper.newBranchByGlobal(globalSession, BranchType.AT, RESOURCE_ID, "ta:1", "xxx");
Assertions.assertTrue(branchSession1.lock());
globalSession.addBranch(branchSession1);
LockManager lockManager = new FileLockManagerForTest();
String otherXID = XID.generateXID(0L);
Assertions.assertFalse(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1"));
globalSession.changeStatus(GlobalStatus.AsyncCommitting);
lockManager.cleanAllLocks();
Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1"));
// Re-init SessionHolder: restore sessions from file
SessionHolder.init("file");
long tid = globalSession.getTransactionId();
GlobalSession reloadSession = SessionHolder.findGlobalSession(globalSession.getXid());
Assertions.assertEquals(reloadSession.getStatus(), GlobalStatus.AsyncCommitting);
GlobalSession sessionInAsyncCommittingQueue = SessionHolder.getAsyncCommittingSessionManager().findGlobalSession(globalSession.getXid());
Assertions.assertTrue(reloadSession == sessionInAsyncCommittingQueue);
// No locking for session in AsyncCommitting status
Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1"));
// clear
reloadSession.addSessionLifecycleListener(SessionHolder.getRootSessionManager());
reloadSession.end();
} finally {
SessionHolder.destroy();
}
}
use of io.seata.server.lock.file.FileLockManagerForTest in project seata by seata.
the class LockManagerTest method concurrentUseAbilityTest.
/**
* Make sure two concurrent branchSession register process with different row key list, at least one process could
* success and only one process could success.
*
* @param branchSession1 the branch session 1
* @param branchSession2 the branch session 2
* @throws Exception the exception
*/
@ParameterizedTest
@MethodSource("deadlockBranchSessionsProvider")
public void concurrentUseAbilityTest(BranchSession branchSession1, BranchSession branchSession2) throws Exception {
LockManager lockManager = new FileLockManagerForTest();
try {
final AtomicBoolean first = new AtomicBoolean();
final AtomicBoolean second = new AtomicBoolean();
CountDownLatch countDownLatch = new CountDownLatch(2);
new Thread(() -> {
try {
first.set(lockManager.acquireLock(branchSession1));
} catch (TransactionException e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
}).start();
new Thread(() -> {
try {
second.set(lockManager.acquireLock(branchSession2));
} catch (TransactionException e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
}).start();
// Assume execute more than 5 seconds means deadlock happened.
if (countDownLatch.await(5, TimeUnit.SECONDS)) {
Assertions.assertTrue(!first.get() || !second.get());
}
} finally {
lockManager.releaseLock(branchSession1);
lockManager.releaseLock(branchSession2);
}
}
use of io.seata.server.lock.file.FileLockManagerForTest in project seata by seata.
the class LockManagerTest method deadlockTest.
/**
* deadlock test.
*
* @param branchSession1 the branch session 1
* @param branchSession2 the branch session 2
* @throws Exception the exception
*/
@ParameterizedTest
@MethodSource("deadlockBranchSessionsProvider")
public void deadlockTest(BranchSession branchSession1, BranchSession branchSession2) throws Exception {
LockManager lockManager = new FileLockManagerForTest();
try {
CountDownLatch countDownLatch = new CountDownLatch(2);
new Thread(() -> {
try {
lockManager.acquireLock(branchSession1);
} catch (TransactionException e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
}).start();
new Thread(() -> {
try {
lockManager.acquireLock(branchSession2);
} catch (TransactionException e) {
e.printStackTrace();
} finally {
countDownLatch.countDown();
}
}).start();
// Assume execute more than 5 seconds means deadlock happened.
Assertions.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));
} finally {
lockManager.releaseLock(branchSession1);
lockManager.releaseLock(branchSession2);
}
}
use of io.seata.server.lock.file.FileLockManagerForTest in project seata by seata.
the class SessionStoreTest method testRestoredFromFileCommitRetry.
/**
* Test restored from file commit retry.
*
* @throws Exception the exception
*/
@Test
public void testRestoredFromFileCommitRetry() throws Exception {
try {
SessionHolder.init("file");
GlobalSession globalSession = new GlobalSession("demo-app", "my_test_tx_group", "test", 6000);
String xid = XID.generateXID(globalSession.getTransactionId());
globalSession.setXid(xid);
globalSession.addSessionLifecycleListener(SessionHolder.getRootSessionManager());
globalSession.begin();
BranchSession branchSession1 = SessionHelper.newBranchByGlobal(globalSession, BranchType.AT, RESOURCE_ID, "ta:1", "xxx");
branchSession1.lock();
globalSession.addBranch(branchSession1);
LockManager lockManager = new FileLockManagerForTest();
String otherXID = XID.generateXID(0L);
Assertions.assertFalse(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1"));
globalSession.changeStatus(GlobalStatus.Committing);
globalSession.changeBranchStatus(branchSession1, BranchStatus.PhaseTwo_CommitFailed_Retryable);
globalSession.changeStatus(GlobalStatus.CommitRetrying);
lockManager.cleanAllLocks();
Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1"));
// Re-init SessionHolder: restore sessions from file
SessionHolder.init("file");
long tid = globalSession.getTransactionId();
GlobalSession reloadSession = SessionHolder.findGlobalSession(globalSession.getXid());
Assertions.assertEquals(reloadSession.getStatus(), GlobalStatus.CommitRetrying);
GlobalSession sessionInRetryCommittingQueue = SessionHolder.getRetryCommittingSessionManager().findGlobalSession(globalSession.getXid());
Assertions.assertTrue(reloadSession == sessionInRetryCommittingQueue);
BranchSession reloadBranchSession = reloadSession.getBranch(branchSession1.getBranchId());
Assertions.assertEquals(reloadBranchSession.getStatus(), BranchStatus.PhaseTwo_CommitFailed_Retryable);
// Lock is held by session in CommitRetrying status
Assertions.assertFalse(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1"));
// clear
reloadSession.addSessionLifecycleListener(SessionHolder.getRootSessionManager());
reloadSession.end();
} finally {
SessionHolder.destroy();
}
}
Aggregations