use of io.seata.server.session.BranchSession 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.session.BranchSession 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.session.BranchSession in project seata by seata.
the class FileLockManager method releaseGlobalSessionLock.
@Override
public boolean releaseGlobalSessionLock(GlobalSession globalSession) throws TransactionException {
ArrayList<BranchSession> branchSessions = globalSession.getBranchSessions();
boolean releaseLockResult = true;
for (BranchSession branchSession : branchSessions) {
try {
MDC.put(MDC_KEY_BRANCH_ID, String.valueOf(branchSession.getBranchId()));
if (!this.releaseLock(branchSession)) {
releaseLockResult = false;
}
} finally {
MDC.remove(MDC_KEY_BRANCH_ID);
}
}
return releaseLockResult;
}
use of io.seata.server.session.BranchSession in project seata by seata.
the class FileSessionManager method restore.
private void restore(List<TransactionWriteStore> stores, Set<String> removedGlobalBuffer, Map<String, Map<Long, BranchSession>> unhandledBranchBuffer) {
for (TransactionWriteStore store : stores) {
TransactionStoreManager.LogOperation logOperation = store.getOperate();
SessionStorable sessionStorable = store.getSessionRequest();
switch(logOperation) {
case GLOBAL_ADD:
case GLOBAL_UPDATE:
{
GlobalSession globalSession = (GlobalSession) sessionStorable;
if (globalSession.getTransactionId() == 0) {
LOGGER.error("Restore globalSession from file failed, the transactionId is zero , xid:" + globalSession.getXid());
break;
}
if (removedGlobalBuffer.contains(globalSession.getXid())) {
break;
}
GlobalSession foundGlobalSession = sessionMap.get(globalSession.getXid());
if (foundGlobalSession == null) {
if (this.checkSessionStatus(globalSession)) {
sessionMap.put(globalSession.getXid(), globalSession);
} else {
removedGlobalBuffer.add(globalSession.getXid());
unhandledBranchBuffer.remove(globalSession.getXid());
}
} else {
if (this.checkSessionStatus(globalSession)) {
foundGlobalSession.setStatus(globalSession.getStatus());
} else {
sessionMap.remove(globalSession.getXid());
removedGlobalBuffer.add(globalSession.getXid());
unhandledBranchBuffer.remove(globalSession.getXid());
}
}
break;
}
case GLOBAL_REMOVE:
{
GlobalSession globalSession = (GlobalSession) sessionStorable;
if (globalSession.getTransactionId() == 0) {
LOGGER.error("Restore globalSession from file failed, the transactionId is zero , xid:" + globalSession.getXid());
break;
}
if (removedGlobalBuffer.contains(globalSession.getXid())) {
break;
}
if (sessionMap.remove(globalSession.getXid()) == null) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("GlobalSession To Be Removed Does Not Exists [" + globalSession.getXid() + "]");
}
}
removedGlobalBuffer.add(globalSession.getXid());
unhandledBranchBuffer.remove(globalSession.getXid());
break;
}
case BRANCH_ADD:
case BRANCH_UPDATE:
{
BranchSession branchSession = (BranchSession) sessionStorable;
if (branchSession.getTransactionId() == 0) {
LOGGER.error("Restore branchSession from file failed, the transactionId is zero , xid:" + branchSession.getXid());
break;
}
if (removedGlobalBuffer.contains(branchSession.getXid())) {
break;
}
GlobalSession foundGlobalSession = sessionMap.get(branchSession.getXid());
if (foundGlobalSession == null) {
unhandledBranchBuffer.computeIfAbsent(branchSession.getXid(), key -> new HashMap<>()).put(branchSession.getBranchId(), branchSession);
} else {
BranchSession existingBranch = foundGlobalSession.getBranch(branchSession.getBranchId());
if (existingBranch == null) {
foundGlobalSession.add(branchSession);
} else {
existingBranch.setStatus(branchSession.getStatus());
}
}
break;
}
case BRANCH_REMOVE:
{
BranchSession branchSession = (BranchSession) sessionStorable;
String xid = branchSession.getXid();
if (removedGlobalBuffer.contains(xid)) {
break;
}
long bid = branchSession.getBranchId();
if (branchSession.getTransactionId() == 0) {
LOGGER.error("Restore branchSession from file failed, the transactionId is zero , xid:" + branchSession.getXid());
break;
}
GlobalSession found = sessionMap.get(xid);
if (found == null) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("GlobalSession To Be Updated (Remove Branch) Does Not Exists [" + bid + "/" + xid + "]");
}
} else {
BranchSession theBranch = found.getBranch(bid);
if (theBranch == null) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("BranchSession To Be Updated Does Not Exists [" + bid + "/" + xid + "]");
}
} else {
found.remove(theBranch);
}
}
break;
}
default:
throw new ShouldNeverHappenException("Unknown Operation: " + logOperation);
}
}
}
use of io.seata.server.session.BranchSession in project seata by seata.
the class WriteStoreTest method main.
/**
* The entry point of application.
*
* @param args the input arguments
* @throws InterruptedException the interrupted exception
* @throws IOException the io exception
*/
public static void main(String[] args) throws InterruptedException, IOException {
TransactionStoreManager transactionStoreManager = new FileTransactionStoreManager("~/Documents/test/data", new SessionManager() {
@Override
public void destroy() {
}
@Override
public void addGlobalSession(GlobalSession session) throws TransactionException {
}
@Override
public GlobalSession findGlobalSession(String xid) {
return null;
}
@Override
public GlobalSession findGlobalSession(String xid, boolean withBranchSessions) {
return null;
}
@Override
public void updateGlobalSessionStatus(GlobalSession session, GlobalStatus status) throws TransactionException {
}
@Override
public void removeGlobalSession(GlobalSession session) throws TransactionException {
}
@Override
public void addBranchSession(GlobalSession globalSession, BranchSession session) throws TransactionException {
}
@Override
public void updateBranchSessionStatus(BranchSession session, BranchStatus status) throws TransactionException {
}
@Override
public void removeBranchSession(GlobalSession globalSession, BranchSession session) throws TransactionException {
}
@Override
public Collection<GlobalSession> allSessions() {
return null;
}
@Override
public List<GlobalSession> findGlobalSessions(SessionCondition condition) {
List<GlobalSession> globalSessions = new ArrayList<>();
int begin = 10000;
int num = 1000;
for (int i = begin; i < begin + num; i++) {
BranchSession branchSession1 = new BranchSession();
branchSession1.setTransactionId(i);
branchSession1.setBranchId(begin + num + (i - begin) * 2);
branchSession1.setResourceId("mockDbkeY1");
BranchSession branchSession2 = new BranchSession();
branchSession2.setTransactionId(i);
branchSession2.setBranchId(begin + num + (i - begin) * 2 + 1);
branchSession2.setResourceId("mockDbkeY2");
GlobalSession globalSession = new GlobalSession(appname, vgroup, instname, 60000);
try {
globalSession.add(branchSession1);
globalSession.add(branchSession2);
globalSessions.add(globalSession);
} catch (Exception exx) {
}
}
return globalSessions;
}
@Override
public <T> T lockAndExecute(GlobalSession globalSession, GlobalSession.LockCallable<T> lockCallable) throws TransactionException {
return null;
}
@Override
public void onBegin(GlobalSession globalSession) throws TransactionException {
}
@Override
public void onStatusChange(GlobalSession globalSession, GlobalStatus status) throws TransactionException {
}
@Override
public void onBranchStatusChange(GlobalSession globalSession, BranchSession branchSession, BranchStatus status) throws TransactionException {
}
@Override
public void onAddBranch(GlobalSession globalSession, BranchSession branchSession) throws TransactionException {
}
@Override
public void onRemoveBranch(GlobalSession globalSession, BranchSession branchSession) throws TransactionException {
}
@Override
public void onClose(GlobalSession globalSession) throws TransactionException {
}
@Override
public void onEnd(GlobalSession globalSession) throws TransactionException {
}
});
long beginWriteMills = System.currentTimeMillis();
write(transactionStoreManager);
long endWriteMills = System.currentTimeMillis();
Thread.sleep(10 * 1000);
long beginReadMills = System.currentTimeMillis();
Map<SessionStorable, LogOperation> resultMap = readAll(transactionStoreManager);
long endReadMills = System.currentTimeMillis();
if ((resultMap.size() % (65535)) % 3000 == 0) {
System.out.print("check success");
} else {
System.out.print("check failed");
}
System.out.print("write cost:" + (endWriteMills - beginWriteMills) + ",read cost:" + (endReadMills - beginReadMills));
}
Aggregations