Search in sources :

Example 1 with FileTransactionStoreManager

use of io.seata.server.storage.file.store.FileTransactionStoreManager in project seata by seata.

the class FileTransactionStoreManagerTest method testFindTimeoutAndSave.

@Test
public void testFindTimeoutAndSave() throws Exception {
    File seataFile = Files.newTemporaryFile();
    Method findTimeoutAndSaveMethod = FileTransactionStoreManager.class.getDeclaredMethod("findTimeoutAndSave");
    findTimeoutAndSaveMethod.setAccessible(true);
    FileSessionManager sessionManager = null;
    FileTransactionStoreManager fileTransactionStoreManager = null;
    try {
        List<GlobalSession> timeoutSessions = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            GlobalSession globalSession = new GlobalSession("", "", "", 60000);
            BranchSession branchSessionA = Mockito.mock(BranchSession.class);
            Mockito.when(branchSessionA.encode()).thenReturn(createBigBranchSessionData(globalSession, (byte) 'A'));
            Mockito.when(branchSessionA.getApplicationData()).thenReturn(new String(createBigApplicationData((byte) 'A')));
            globalSession.addBranch(branchSessionA);
            BranchSession branchSessionB = Mockito.mock(BranchSession.class);
            Mockito.when(branchSessionB.encode()).thenReturn(createBigBranchSessionData(globalSession, (byte) 'B'));
            Mockito.when(branchSessionB.getApplicationData()).thenReturn(new String(createBigApplicationData((byte) 'B')));
            globalSession.addBranch(branchSessionB);
            timeoutSessions.add(globalSession);
        }
        SessionManager sessionManagerMock = Mockito.mock(SessionManager.class);
        Mockito.when(sessionManagerMock.findGlobalSessions(Mockito.any())).thenReturn(timeoutSessions);
        fileTransactionStoreManager = new FileTransactionStoreManager(seataFile.getAbsolutePath(), sessionManagerMock);
        Assertions.assertTrue((boolean) findTimeoutAndSaveMethod.invoke(fileTransactionStoreManager));
        sessionManager = new FileSessionManager(seataFile.getName(), seataFile.getParent());
        sessionManager.reload();
        Collection<GlobalSession> globalSessions = sessionManager.allSessions();
        Assertions.assertNotNull(globalSessions);
        globalSessions.forEach(g -> {
            Assertions.assertNotNull(g);
            List<BranchSession> branches = g.getBranchSessions();
            Assertions.assertEquals(2, branches.size());
            Assertions.assertEquals(new String(createBigApplicationData((byte) 'A')), branches.get(0).getApplicationData());
            Assertions.assertEquals(new String(createBigApplicationData((byte) 'B')), branches.get(1).getApplicationData());
        });
    } finally {
        findTimeoutAndSaveMethod.setAccessible(false);
        if (fileTransactionStoreManager != null) {
            fileTransactionStoreManager.shutdown();
        }
        if (sessionManager != null) {
            sessionManager.destroy();
        }
        Assertions.assertTrue(seataFile.delete());
    }
}
Also used : FileSessionManager(io.seata.server.storage.file.session.FileSessionManager) SessionManager(io.seata.server.session.SessionManager) BranchSession(io.seata.server.session.BranchSession) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) FileSessionManager(io.seata.server.storage.file.session.FileSessionManager) GlobalSession(io.seata.server.session.GlobalSession) File(java.io.File) FileTransactionStoreManager(io.seata.server.storage.file.store.FileTransactionStoreManager) Test(org.junit.jupiter.api.Test)

Example 2 with FileTransactionStoreManager

use of io.seata.server.storage.file.store.FileTransactionStoreManager 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 FileTransactionStoreManager

use of io.seata.server.storage.file.store.FileTransactionStoreManager 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 FileTransactionStoreManager

use of io.seata.server.storage.file.store.FileTransactionStoreManager in project seata by seata.

the class FileTransactionStoreManagerTest method testBigDataWrite.

@Test
public void testBigDataWrite() throws Exception {
    File seataFile = Files.newTemporaryFile();
    FileTransactionStoreManager fileTransactionStoreManager = null;
    try {
        fileTransactionStoreManager = new FileTransactionStoreManager(seataFile.getAbsolutePath(), null);
        BranchSession branchSessionA = Mockito.mock(BranchSession.class);
        GlobalSession global = new GlobalSession();
        Mockito.when(branchSessionA.encode()).thenReturn(createBigBranchSessionData(global, (byte) 'A'));
        Mockito.when(branchSessionA.getApplicationData()).thenReturn(new String(createBigApplicationData((byte) 'A')));
        BranchSession branchSessionB = Mockito.mock(BranchSession.class);
        Mockito.when(branchSessionB.encode()).thenReturn(createBigBranchSessionData(global, (byte) 'B'));
        Mockito.when(branchSessionB.getApplicationData()).thenReturn(new String(createBigApplicationData((byte) 'B')));
        Assertions.assertTrue(fileTransactionStoreManager.writeSession(TransactionStoreManager.LogOperation.BRANCH_ADD, branchSessionA));
        Assertions.assertTrue(fileTransactionStoreManager.writeSession(TransactionStoreManager.LogOperation.BRANCH_ADD, branchSessionB));
        List<TransactionWriteStore> list = fileTransactionStoreManager.readWriteStore(2000, false);
        Assertions.assertNotNull(list);
        Assertions.assertEquals(2, list.size());
        BranchSession loadedBranchSessionA = (BranchSession) list.get(0).getSessionRequest();
        Assertions.assertEquals(branchSessionA.getApplicationData(), loadedBranchSessionA.getApplicationData());
        BranchSession loadedBranchSessionB = (BranchSession) list.get(1).getSessionRequest();
        Assertions.assertEquals(branchSessionB.getApplicationData(), loadedBranchSessionB.getApplicationData());
    } finally {
        if (fileTransactionStoreManager != null) {
            fileTransactionStoreManager.shutdown();
        }
        Assertions.assertTrue(seataFile.delete());
    }
}
Also used : TransactionWriteStore(io.seata.server.storage.file.TransactionWriteStore) GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) File(java.io.File) FileTransactionStoreManager(io.seata.server.storage.file.store.FileTransactionStoreManager) Test(org.junit.jupiter.api.Test)

Aggregations

BranchSession (io.seata.server.session.BranchSession)4 GlobalSession (io.seata.server.session.GlobalSession)4 FileTransactionStoreManager (io.seata.server.storage.file.store.FileTransactionStoreManager)4 SessionManager (io.seata.server.session.SessionManager)3 ArrayList (java.util.ArrayList)3 TransactionException (io.seata.core.exception.TransactionException)2 BranchStatus (io.seata.core.model.BranchStatus)2 GlobalStatus (io.seata.core.model.GlobalStatus)2 SessionCondition (io.seata.server.session.SessionCondition)2 TransactionStoreManager (io.seata.server.store.TransactionStoreManager)2 File (java.io.File)2 Collection (java.util.Collection)2 List (java.util.List)2 Test (org.junit.jupiter.api.Test)2 TransactionWriteStore (io.seata.server.storage.file.TransactionWriteStore)1 FileSessionManager (io.seata.server.storage.file.session.FileSessionManager)1 SessionStorable (io.seata.server.store.SessionStorable)1 LogOperation (io.seata.server.store.TransactionStoreManager.LogOperation)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1