Search in sources :

Example 11 with GlobalSession

use of io.seata.server.session.GlobalSession in project seata by seata.

the class DefaultCore method rollback.

@Override
public GlobalStatus rollback(String xid) throws TransactionException {
    GlobalSession globalSession = SessionHolder.findGlobalSession(xid);
    if (globalSession == null) {
        return GlobalStatus.Finished;
    }
    globalSession.addSessionLifecycleListener(SessionHolder.getRootSessionManager());
    // just lock changeStatus
    boolean shouldRollBack = SessionHolder.lockAndExecute(globalSession, () -> {
        // Highlight: Firstly, close the session, then no more branch can be registered.
        globalSession.close();
        if (globalSession.getStatus() == GlobalStatus.Begin) {
            globalSession.changeStatus(GlobalStatus.Rollbacking);
            return true;
        }
        return false;
    });
    if (!shouldRollBack) {
        return globalSession.getStatus();
    }
    doGlobalRollback(globalSession, false);
    return globalSession.getStatus();
}
Also used : GlobalSession(io.seata.server.session.GlobalSession)

Example 12 with GlobalSession

use of io.seata.server.session.GlobalSession in project seata by seata.

the class DefaultCore method begin.

@Override
public String begin(String applicationId, String transactionServiceGroup, String name, int timeout) throws TransactionException {
    GlobalSession session = GlobalSession.createGlobalSession(applicationId, transactionServiceGroup, name, timeout);
    MDC.put(RootContext.MDC_KEY_XID, session.getXid());
    session.addSessionLifecycleListener(SessionHolder.getRootSessionManager());
    session.begin();
    // transaction start event
    eventBus.post(new GlobalTransactionEvent(session.getTransactionId(), GlobalTransactionEvent.ROLE_TC, session.getTransactionName(), applicationId, transactionServiceGroup, session.getBeginTime(), null, session.getStatus()));
    return session.getXid();
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) GlobalTransactionEvent(io.seata.core.event.GlobalTransactionEvent)

Example 13 with GlobalSession

use of io.seata.server.session.GlobalSession 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);
        }
    }
}
Also used : TransactionWriteStore(io.seata.server.storage.file.TransactionWriteStore) GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) SessionStorable(io.seata.server.store.SessionStorable) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) AbstractTransactionStoreManager(io.seata.server.store.AbstractTransactionStoreManager) TransactionStoreManager(io.seata.server.store.TransactionStoreManager) FileTransactionStoreManager(io.seata.server.storage.file.store.FileTransactionStoreManager)

Example 14 with GlobalSession

use of io.seata.server.session.GlobalSession 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));
}
Also used : LogOperation(io.seata.server.store.TransactionStoreManager.LogOperation) GlobalStatus(io.seata.core.model.GlobalStatus) SessionManager(io.seata.server.session.SessionManager) BranchSession(io.seata.server.session.BranchSession) SessionCondition(io.seata.server.session.SessionCondition) TransactionException(io.seata.core.exception.TransactionException) IOException(java.io.IOException) TransactionStoreManager(io.seata.server.store.TransactionStoreManager) FileTransactionStoreManager(io.seata.server.storage.file.store.FileTransactionStoreManager) TransactionException(io.seata.core.exception.TransactionException) GlobalSession(io.seata.server.session.GlobalSession) SessionStorable(io.seata.server.store.SessionStorable) Collection(java.util.Collection) BranchStatus(io.seata.core.model.BranchStatus) ArrayList(java.util.ArrayList) List(java.util.List) FileTransactionStoreManager(io.seata.server.storage.file.store.FileTransactionStoreManager)

Example 15 with GlobalSession

use of io.seata.server.session.GlobalSession in project seata by seata.

the class DefaultCoordinatorTest method branchRollback.

@Disabled
@ParameterizedTest
@MethodSource("xidAndBranchIdProviderForRollback")
public void branchRollback(String xid, Long branchId) {
    BranchStatus result = null;
    GlobalSession globalSession = SessionHolder.findGlobalSession(xid);
    try {
        result = core.branchRollback(globalSession, globalSession.getBranch(branchId));
    } catch (TransactionException e) {
        Assertions.fail(e.getMessage());
    }
    Assertions.assertEquals(result, BranchStatus.PhaseTwo_Rollbacked);
}
Also used : TransactionException(io.seata.core.exception.TransactionException) GlobalSession(io.seata.server.session.GlobalSession) BranchStatus(io.seata.core.model.BranchStatus) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

GlobalSession (io.seata.server.session.GlobalSession)73 BranchSession (io.seata.server.session.BranchSession)33 Test (org.junit.jupiter.api.Test)32 Connection (java.sql.Connection)10 TransactionException (io.seata.core.exception.TransactionException)9 ArrayList (java.util.ArrayList)8 SessionCondition (io.seata.server.session.SessionCondition)7 GlobalTransactionEvent (io.seata.core.event.GlobalTransactionEvent)6 BranchStatus (io.seata.core.model.BranchStatus)6 ResultSet (java.sql.ResultSet)6 GlobalTransactionDO (io.seata.core.store.GlobalTransactionDO)5 LockManager (io.seata.server.lock.LockManager)5 FileLockManagerForTest (io.seata.server.lock.file.FileLockManagerForTest)5 FileTransactionStoreManager (io.seata.server.storage.file.store.FileTransactionStoreManager)5 List (java.util.List)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 BranchTransactionException (io.seata.core.exception.BranchTransactionException)4 TransactionWriteStore (io.seata.server.storage.file.TransactionWriteStore)4 GlobalStatus (io.seata.core.model.GlobalStatus)3 SessionManager (io.seata.server.session.SessionManager)3