Search in sources :

Example 1 with SessionCondition

use of io.seata.server.session.SessionCondition 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 SessionCondition

use of io.seata.server.session.SessionCondition 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 3 with SessionCondition

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

the class WriteStoreMultithreadTest method main.

public static void main(String[] args) throws Exception {
    TransactionStoreManager transactionStoreManager = new FileTransactionStoreManager("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();
    for (int i = 0; i < threadNum; i++) {
        final int threadNo = i;
        Thread thread = new Thread(() -> {
            write(transactionStoreManager, threadNo);
        });
        thread.start();
    }
    countDownLatch.await();
    long endWriteMills = System.currentTimeMillis();
    System.out.println("thread nums:" + threadNum + ", per_thread_trx_num:" + per_thread_trx_num + " ,cost" + (endWriteMills - beginWriteMills));
}
Also used : 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) 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) 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 4 with SessionCondition

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

the class DataBaseSessionManagerTest method test_findGlobalSessions.

@Test
public void test_findGlobalSessions() throws TransactionException, SQLException {
    String xid = null;
    {
        GlobalSession globalSession = GlobalSession.createGlobalSession("test", "test", "test123", 100);
        xid = XID.generateXID(globalSession.getTransactionId());
        globalSession.setXid(xid);
        globalSession.setTransactionId(146757978);
        globalSession.setBeginTime(System.currentTimeMillis());
        globalSession.setApplicationData("abc=878s");
        globalSession.setStatus(GlobalStatus.Begin);
        sessionManager.addGlobalSession(globalSession);
        BranchSession branchSession = new BranchSession();
        branchSession.setBranchId(UUIDGenerator.generateUUID());
        branchSession.setXid(xid);
        branchSession.setTransactionId(globalSession.getTransactionId());
        branchSession.setBranchId(1L);
        branchSession.setResourceGroupId("my_test_tx_group");
        branchSession.setResourceId("tb_1");
        branchSession.setLockKey("t_1");
        branchSession.setBranchType(BranchType.AT);
        branchSession.setClientId("abc-123");
        branchSession.setApplicationData("{\"data\":\"test\"}");
        branchSession.setStatus(BranchStatus.PhaseOne_Done);
        sessionManager.addBranchSession(globalSession, branchSession);
    }
    String xid2 = null;
    {
        GlobalSession globalSession = GlobalSession.createGlobalSession("test", "test", "test123", 100);
        xid2 = XID.generateXID(globalSession.getTransactionId());
        globalSession.setXid(xid);
        globalSession.setTransactionId(146757978);
        globalSession.setBeginTime(System.currentTimeMillis());
        globalSession.setApplicationData("abc=878s");
        globalSession.setStatus(GlobalStatus.CommitRetrying);
        sessionManager.addGlobalSession(globalSession);
        BranchSession branchSession = new BranchSession();
        branchSession.setBranchId(UUIDGenerator.generateUUID());
        branchSession.setXid(xid2);
        branchSession.setTransactionId(globalSession.getTransactionId());
        branchSession.setBranchId(1L);
        branchSession.setResourceGroupId("my_test_tx_group");
        branchSession.setResourceId("tb_1");
        branchSession.setLockKey("t_1");
        branchSession.setBranchType(BranchType.AT);
        branchSession.setClientId("abc-123");
        branchSession.setApplicationData("{\"data\":\"test\"}");
        branchSession.setStatus(BranchStatus.PhaseOne_Done);
        sessionManager.addBranchSession(globalSession, branchSession);
    }
    Collection<GlobalSession> rets = sessionManager.findGlobalSessions(new SessionCondition(GlobalStatus.Begin));
    Assertions.assertNotNull(rets);
    Assertions.assertEquals(1, rets.size());
    GlobalSession globalSession_db = (GlobalSession) new ArrayList(rets).get(0);
    Assertions.assertNotNull(globalSession_db.getReverseSortedBranches());
    Assertions.assertEquals(1, globalSession_db.getReverseSortedBranches().size());
    Assertions.assertNotNull(globalSession_db.getBranch(1L));
    String delSql = "delete from branch_table where xid= '" + xid + "'" + ";" + "delete from global_table where xid= '" + xid + "'";
    String delSql2 = "delete from branch_table where xid= '" + xid2 + "'" + ";" + "delete from global_table where xid= '" + xid2 + "'";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        conn.createStatement().execute(delSql);
        conn.createStatement().execute(delSql2);
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) SessionCondition(io.seata.server.session.SessionCondition) Test(org.junit.jupiter.api.Test)

Example 5 with SessionCondition

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

the class RedisSessionManagerTest method testReadSessionWithCondition.

@Test
public void testReadSessionWithCondition() 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.setXid(xid);
    List<GlobalSession> globalSessions = sessionManager.findGlobalSessions(condition);
    Assertions.assertEquals(session.getXid(), globalSessions.get(0).getXid());
    Assertions.assertEquals(session.getTransactionId(), globalSessions.get(0).getTransactionId());
    Assertions.assertEquals(branchSession.getXid(), globalSessions.get(0).getBranchSessions().get(0).getXid());
    Assertions.assertEquals(branchSession.getBranchId(), globalSessions.get(0).getBranchSessions().get(0).getBranchId());
    Assertions.assertEquals(branchSession.getClientId(), globalSessions.get(0).getBranchSessions().get(0).getClientId());
    condition.setXid(null);
    condition.setTransactionId(session.getTransactionId());
    globalSessions = sessionManager.findGlobalSessions(condition);
    Assertions.assertEquals(session.getXid(), globalSessions.get(0).getXid());
    Assertions.assertEquals(session.getTransactionId(), globalSessions.get(0).getTransactionId());
    Assertions.assertEquals(branchSession.getXid(), globalSessions.get(0).getBranchSessions().get(0).getXid());
    Assertions.assertEquals(branchSession.getBranchId(), globalSessions.get(0).getBranchSessions().get(0).getBranchId());
    Assertions.assertEquals(branchSession.getClientId(), globalSessions.get(0).getBranchSessions().get(0).getClientId());
    condition.setTransactionId(null);
    globalSessions = sessionManager.findGlobalSessions(condition);
    Assertions.assertNull(globalSessions);
    sessionManager.removeBranchSession(session, branchSession);
    sessionManager.removeGlobalSession(session);
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) SessionCondition(io.seata.server.session.SessionCondition) Test(org.junit.jupiter.api.Test)

Aggregations

BranchSession (io.seata.server.session.BranchSession)7 GlobalSession (io.seata.server.session.GlobalSession)7 SessionCondition (io.seata.server.session.SessionCondition)7 GlobalStatus (io.seata.core.model.GlobalStatus)3 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 TransactionException (io.seata.core.exception.TransactionException)2 BranchStatus (io.seata.core.model.BranchStatus)2 SessionManager (io.seata.server.session.SessionManager)2 TransactionWriteStore (io.seata.server.storage.file.TransactionWriteStore)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 SessionStorable (io.seata.server.store.SessionStorable)1 LogOperation (io.seata.server.store.TransactionStoreManager.LogOperation)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1