Search in sources :

Example 1 with GlobalStatus

use of io.seata.core.model.GlobalStatus in project seata by seata.

the class RedisSessionManagerTest method testReadSessionWithConditionStatus.

@Test
public void testReadSessionWithConditionStatus() throws TransactionException {
    GlobalSession session = GlobalSession.createGlobalSession("test", "test", "test123", 100);
    String xid = XID.generateXID(session.getTransactionId());
    session.setXid(xid);
    session.setTransactionId(session.getTransactionId());
    session.setBeginTime(System.currentTimeMillis());
    session.setApplicationData("abc=878s");
    session.setStatus(GlobalStatus.Begin);
    sessionManager.addGlobalSession(session);
    BranchSession branchSession = new BranchSession();
    branchSession.setBranchId(UUIDGenerator.generateUUID());
    branchSession.setXid(xid);
    branchSession.setTransactionId(session.getTransactionId());
    branchSession.setBranchId(1L);
    branchSession.setResourceGroupId("my_test_tx_group");
    branchSession.setResourceId("tb_1");
    branchSession.setLockKey("t_1");
    branchSession.setBranchType(BranchType.AT);
    branchSession.setApplicationData("{\"data\":\"test\"}");
    branchSession.setClientId("storage-server:192.168.158.80:11934");
    sessionManager.addBranchSession(session, branchSession);
    SessionCondition condition = new SessionCondition();
    condition.setStatus(GlobalStatus.Begin);
    sessionManager.findGlobalSessions(condition);
    condition.setStatus(null);
    GlobalStatus[] statuses = { GlobalStatus.Begin };
    condition.setStatuses(statuses);
    sessionManager.findGlobalSessions(condition);
    sessionManager.removeBranchSession(session, branchSession);
    sessionManager.removeGlobalSession(session);
}
Also used : GlobalStatus(io.seata.core.model.GlobalStatus) GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) SessionCondition(io.seata.server.session.SessionCondition) Test(org.junit.jupiter.api.Test)

Example 2 with GlobalStatus

use of io.seata.core.model.GlobalStatus in project seata by seata.

the class SessionHelper method endRollbacked.

/**
 * End rollbacked.
 *
 * @param globalSession the global session
 * @throws TransactionException the transaction exception
 */
public static void endRollbacked(GlobalSession globalSession) throws TransactionException {
    GlobalStatus currentStatus = globalSession.getStatus();
    if (isTimeoutGlobalStatus(currentStatus)) {
        globalSession.changeStatus(GlobalStatus.TimeoutRollbacked);
    } else {
        globalSession.changeStatus(GlobalStatus.Rollbacked);
    }
    globalSession.end();
}
Also used : GlobalStatus(io.seata.core.model.GlobalStatus)

Example 3 with GlobalStatus

use of io.seata.core.model.GlobalStatus in project seata by seata.

the class GlobalReportRequestCodec method encode.

@Override
public <T> void encode(T t, ByteBuf out) {
    super.encode(t, out);
    GlobalReportRequest reportRequest = (GlobalReportRequest) t;
    GlobalStatus globalStatus = reportRequest.getGlobalStatus();
    if (globalStatus != null) {
        out.writeByte((byte) globalStatus.getCode());
    } else {
        out.writeByte((byte) -1);
    }
}
Also used : GlobalReportRequest(io.seata.core.protocol.transaction.GlobalReportRequest) GlobalStatus(io.seata.core.model.GlobalStatus)

Example 4 with GlobalStatus

use of io.seata.core.model.GlobalStatus 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 5 with GlobalStatus

use of io.seata.core.model.GlobalStatus in project seata by seata.

the class DefaultCoreForEventBusTest method test.

@Test
public void test() throws IOException, TransactionException, InterruptedException {
    class GlobalTransactionEventSubscriber {

        private final Map<GlobalStatus, AtomicInteger> eventCounters;

        public Map<GlobalStatus, AtomicInteger> getEventCounters() {
            return eventCounters;
        }

        public GlobalTransactionEventSubscriber() {
            this.eventCounters = new ConcurrentHashMap<>();
        }

        @Subscribe
        public void processGlobalTransactionEvent(GlobalTransactionEvent event) {
            AtomicInteger counter = eventCounters.computeIfAbsent(event.getStatus(), status -> new AtomicInteger(0));
            counter.addAndGet(1);
        }
    }
    SessionHolder.init(null);
    RemotingServer remotingServer = new DefaultCoordinatorTest.MockServerMessageSender();
    DefaultCoordinator coordinator = new DefaultCoordinator(remotingServer);
    coordinator.init();
    try {
        DefaultCore core = new DefaultCore(remotingServer);
        GlobalTransactionEventSubscriber subscriber = new GlobalTransactionEventSubscriber();
        // avoid transactional interference from other unit tests
        Thread.sleep(1500);
        EventBusManager.get().register(subscriber);
        // start a transaction
        String xid = core.begin("test_app_id", "default_group", "test_tran_name", 30000);
        Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Begin).get());
        // commit this transaction
        core.commit(xid);
        // we need sleep for a short while because default canBeCommittedAsync() is true
        Thread.sleep(1000);
        // check
        Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.AsyncCommitting).get());
        Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Committed).get());
        // start another new transaction
        xid = core.begin("test_app_id", "default_group", "test_tran_name2", 30000);
        Assertions.assertEquals(2, subscriber.getEventCounters().get(GlobalStatus.Begin).get());
        core.rollback(xid);
        // check
        Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Rollbacking).get());
        Assertions.assertEquals(1, subscriber.getEventCounters().get(GlobalStatus.Rollbacked).get());
        // start more one new transaction for test timeout and let this transaction immediately timeout
        xid = core.begin("test_app_id", "default_group", "test_tran_name3", 0);
        // sleep for check ->  DefaultCoordinator.timeoutCheck
        Thread.sleep(1000);
        // at lease retry once because DefaultCoordinator.timeoutCheck is 1 second
        Assertions.assertTrue(subscriber.getEventCounters().get(GlobalStatus.TimeoutRollbacking).get() >= 1);
    } finally {
        coordinator.destroy();
        SessionHolder.destroy();
    }
}
Also used : GlobalStatus(io.seata.core.model.GlobalStatus) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultCore(io.seata.server.coordinator.DefaultCore) DefaultCoordinator(io.seata.server.coordinator.DefaultCoordinator) RemotingServer(io.seata.core.rpc.RemotingServer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) GlobalTransactionEvent(io.seata.core.event.GlobalTransactionEvent) Test(org.junit.jupiter.api.Test) DefaultCoordinatorTest(io.seata.server.coordinator.DefaultCoordinatorTest)

Aggregations

GlobalStatus (io.seata.core.model.GlobalStatus)20 TransactionException (io.seata.core.exception.TransactionException)4 ArrayList (java.util.ArrayList)4 BranchSession (io.seata.server.session.BranchSession)3 GlobalSession (io.seata.server.session.GlobalSession)3 SessionCondition (io.seata.server.session.SessionCondition)3 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)2 BranchStatus (io.seata.core.model.BranchStatus)2 SessionManager (io.seata.server.session.SessionManager)2 FileTransactionStoreManager (io.seata.server.storage.file.store.FileTransactionStoreManager)2 TransactionStoreManager (io.seata.server.store.TransactionStoreManager)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 Test (org.junit.jupiter.api.Test)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 DefaultConnection (io.mycat.datasource.jdbc.datasource.DefaultConnection)1 GlobalTransactionEvent (io.seata.core.event.GlobalTransactionEvent)1 AbstractGlobalEndResponse (io.seata.core.protocol.transaction.AbstractGlobalEndResponse)1