Search in sources :

Example 11 with BranchSession

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

the class DataBaseSessionManagerTest method test_removeBranchSession.

@Test
public void test_removeBranchSession() throws Exception {
    GlobalSession globalSession = GlobalSession.createGlobalSession("test", "test", "test123", 100);
    String xid = XID.generateXID(globalSession.getTransactionId());
    globalSession.setXid(xid);
    globalSession.setTransactionId(146757978);
    globalSession.setBeginTime(System.currentTimeMillis());
    globalSession.setApplicationData("abc=878s");
    globalSession.setStatus(GlobalStatus.Begin);
    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.setApplicationData("{\"data\":\"test\"}");
    branchSession.setStatus(BranchStatus.PhaseOne_Done);
    sessionManager.addBranchSession(globalSession, branchSession);
    sessionManager.removeBranchSession(globalSession, branchSession);
    String sql = "select * from branch_table where xid= '" + xid + "'";
    String delSql = "delete from branch_table where xid= '" + xid + "'" + ";" + "delete from global_table where xid= '" + xid + "'";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        ResultSet rs = conn.createStatement().executeQuery(sql);
        if (rs.next()) {
            Assertions.assertTrue(false);
        } else {
            Assertions.assertTrue(true);
        }
        conn.createStatement().execute(delSql);
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.jupiter.api.Test)

Example 12 with BranchSession

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

the class DataBaseSessionManagerTest method test_updateBranchSessionStatus.

@Test
public void test_updateBranchSessionStatus() throws Exception {
    GlobalSession globalSession = GlobalSession.createGlobalSession("test", "test", "test123", 100);
    String xid = XID.generateXID(globalSession.getTransactionId());
    globalSession.setXid(xid);
    globalSession.setTransactionId(146757978);
    globalSession.setBeginTime(System.currentTimeMillis());
    globalSession.setApplicationData("abc=878s");
    globalSession.setStatus(GlobalStatus.Begin);
    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.setApplicationData("{\"data\":\"test\"}");
    branchSession.setStatus(BranchStatus.PhaseOne_Done);
    sessionManager.addBranchSession(globalSession, branchSession);
    branchSession.setStatus(BranchStatus.PhaseOne_Timeout);
    sessionManager.updateBranchSessionStatus(branchSession, BranchStatus.PhaseOne_Timeout);
    String sql = "select * from branch_table where xid= '" + xid + "'";
    String delSql = "delete from branch_table where xid= '" + xid + "'" + ";" + "delete from global_table where xid= '" + xid + "'";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        ResultSet rs = conn.createStatement().executeQuery(sql);
        if (rs.next()) {
            Assertions.assertTrue(true);
            Assertions.assertEquals(rs.getInt("status"), BranchStatus.PhaseOne_Timeout.getCode());
        } else {
            Assertions.assertTrue(false);
        }
        conn.createStatement().execute(delSql);
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.jupiter.api.Test)

Example 13 with BranchSession

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

the class SessionConverterTest method testConvertBranchTransactionDONotNull.

@Test
public void testConvertBranchTransactionDONotNull() {
    BranchSession branchSession = new BranchSession();
    branchSession.setXid("192.168.158.80:8091:39372760251957248");
    branchSession.setResourceId("jdbc:mysql://116.62.62.62/seata-storage");
    branchSession.setTransactionId(39372760251957248L);
    branchSession.setBranchId(39372760251957111L);
    branchSession.setBranchType(BranchType.XA);
    branchSession.setResourceGroupId("abc");
    branchSession.setClientId("storage-server:192.168.158.80:11934");
    branchSession.setStatus(BranchStatus.PhaseOne_Failed);
    branchSession.setApplicationData("abc=123");
    BranchTransactionDO branchTransactionDO = SessionConverter.convertBranchTransactionDO(branchSession);
    Assertions.assertEquals(branchSession.getXid(), branchTransactionDO.getXid());
    Assertions.assertEquals(branchSession.getResourceId(), branchTransactionDO.getResourceId());
    Assertions.assertEquals(branchSession.getTransactionId(), branchTransactionDO.getTransactionId());
    Assertions.assertEquals(branchSession.getBranchId(), branchTransactionDO.getBranchId());
    Assertions.assertEquals(branchSession.getBranchType().name(), branchTransactionDO.getBranchType());
    Assertions.assertEquals(branchSession.getResourceGroupId(), branchTransactionDO.getResourceGroupId());
    Assertions.assertEquals(branchSession.getClientId(), branchTransactionDO.getClientId());
    Assertions.assertEquals(branchSession.getStatus().getCode(), branchTransactionDO.getStatus());
    Assertions.assertEquals(branchSession.getApplicationData(), branchTransactionDO.getApplicationData());
}
Also used : BranchSession(io.seata.server.session.BranchSession) BranchTransactionDO(io.seata.core.store.BranchTransactionDO) Test(org.junit.jupiter.api.Test)

Example 14 with BranchSession

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

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

the class DefaultCoreTest method doGlobalCommitCommitTest.

/**
 * Do global commit test.
 *
 * @param xid the xid
 * @throws Exception the exception
 */
@ParameterizedTest
@MethodSource("xidProvider")
public void doGlobalCommitCommitTest(String xid) throws Exception {
    globalSession = SessionHolder.findGlobalSession(xid);
    BranchSession branchSession = SessionHelper.newBranchByGlobal(globalSession, BranchType.XA, resourceId, applicationData, "t1:1", clientId);
    globalSession.addBranch(branchSession);
    globalSession.changeBranchStatus(branchSession, BranchStatus.PhaseOne_Done);
    core.mockCore(BranchType.XA, new MockCore(BranchStatus.PhaseTwo_Committed, BranchStatus.PhaseOne_Done));
    core.doGlobalCommit(globalSession, false);
    Assertions.assertEquals(globalSession.getStatus(), GlobalStatus.Committed);
}
Also used : BranchSession(io.seata.server.session.BranchSession) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

BranchSession (io.seata.server.session.BranchSession)60 GlobalSession (io.seata.server.session.GlobalSession)33 Test (org.junit.jupiter.api.Test)29 Connection (java.sql.Connection)9 SessionCondition (io.seata.server.session.SessionCondition)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 MethodSource (org.junit.jupiter.params.provider.MethodSource)7 BranchTransactionDO (io.seata.core.store.BranchTransactionDO)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 ResultSet (java.sql.ResultSet)5 ArrayList (java.util.ArrayList)5 BranchTransactionException (io.seata.core.exception.BranchTransactionException)4 TransactionException (io.seata.core.exception.TransactionException)4 BranchStatus (io.seata.core.model.BranchStatus)4 TransactionWriteStore (io.seata.server.storage.file.TransactionWriteStore)4 GlobalStatus (io.seata.core.model.GlobalStatus)3 SessionManager (io.seata.server.session.SessionManager)3 TransactionStoreManager (io.seata.server.store.TransactionStoreManager)3