Search in sources :

Example 1 with TransactionWriteStore

use of io.seata.server.storage.file.TransactionWriteStore 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 2 with TransactionWriteStore

use of io.seata.server.storage.file.TransactionWriteStore in project XHuiCloud by sindaZeng.

the class FileSessionManager method restore.

private void restore(List<TransactionWriteStore> stores, Map<Long, BranchSession> unhandledBranchSessions) {
    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;
                    }
                    GlobalSession foundGlobalSession = sessionMap.get(globalSession.getXid());
                    if (foundGlobalSession == null) {
                        sessionMap.put(globalSession.getXid(), globalSession);
                    } else {
                        foundGlobalSession.setStatus(globalSession.getStatus());
                    }
                    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 (sessionMap.remove(globalSession.getXid()) == null) {
                        if (LOGGER.isInfoEnabled()) {
                            LOGGER.info("GlobalSession To Be Removed Does Not Exists [" + 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;
                    }
                    GlobalSession foundGlobalSession = sessionMap.get(branchSession.getXid());
                    if (foundGlobalSession == null) {
                        unhandledBranchSessions.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();
                    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) 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 3 with TransactionWriteStore

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

Example 4 with TransactionWriteStore

use of io.seata.server.storage.file.TransactionWriteStore in project seata by seata.

the class WriteStoreTest method readAll.

private static Map<SessionStorable, LogOperation> readAll(TransactionStoreManager transactionStoreManager) {
    Map<SessionStorable, LogOperation> resultMap = new HashMap<>(65535 * 5 * 9);
    while (((ReloadableStore) transactionStoreManager).hasRemaining(true)) {
        List<TransactionWriteStore> transactionWriteStores = ((ReloadableStore) transactionStoreManager).readWriteStore(2000, true);
        if (transactionWriteStores != null) {
            for (TransactionWriteStore transactionWriteStore : transactionWriteStores) {
                printLog(transactionWriteStore);
                resultMap.put(transactionWriteStore.getSessionRequest(), transactionWriteStore.getOperate());
            }
        }
    }
    while (((ReloadableStore) transactionStoreManager).hasRemaining(false)) {
        List<TransactionWriteStore> transactionWriteStores = ((ReloadableStore) transactionStoreManager).readWriteStore(2000, false);
        if (transactionWriteStores != null) {
            for (TransactionWriteStore transactionWriteStore : transactionWriteStores) {
                printLog(transactionWriteStore);
                resultMap.put(transactionWriteStore.getSessionRequest(), transactionWriteStore.getOperate());
            }
        }
    }
    return resultMap;
}
Also used : LogOperation(io.seata.server.store.TransactionStoreManager.LogOperation) HashMap(java.util.HashMap) TransactionWriteStore(io.seata.server.storage.file.TransactionWriteStore) SessionStorable(io.seata.server.store.SessionStorable) ReloadableStore(io.seata.server.storage.file.ReloadableStore)

Example 5 with TransactionWriteStore

use of io.seata.server.storage.file.TransactionWriteStore in project seata by seata.

the class FileTransactionStoreManager method findTimeoutAndSave.

private boolean findTimeoutAndSave() throws IOException {
    List<GlobalSession> globalSessionsOverMaxTimeout = sessionManager.findGlobalSessions(new SessionCondition(MAX_TRX_TIMEOUT_MILLS));
    if (CollectionUtils.isEmpty(globalSessionsOverMaxTimeout)) {
        return true;
    }
    for (GlobalSession globalSession : globalSessionsOverMaxTimeout) {
        TransactionWriteStore globalWriteStore = new TransactionWriteStore(globalSession, LogOperation.GLOBAL_ADD);
        byte[] data = globalWriteStore.encode();
        if (!writeDataFrame(data)) {
            return false;
        }
        List<BranchSession> branchSessIonsOverMaXTimeout = globalSession.getSortedBranches();
        if (branchSessIonsOverMaXTimeout != null) {
            for (BranchSession branchSession : branchSessIonsOverMaXTimeout) {
                try {
                    MDC.put(MDC_KEY_BRANCH_ID, String.valueOf(branchSession.getBranchId()));
                    TransactionWriteStore branchWriteStore = new TransactionWriteStore(branchSession, LogOperation.BRANCH_ADD);
                    data = branchWriteStore.encode();
                    if (!writeDataFrame(data)) {
                        return false;
                    }
                } finally {
                    MDC.remove(MDC_KEY_BRANCH_ID);
                }
            }
        }
    }
    if (flushWriteBuffer(writeBuffer)) {
        currFileChannel.force(false);
        return true;
    }
    return false;
}
Also used : TransactionWriteStore(io.seata.server.storage.file.TransactionWriteStore) GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) SessionCondition(io.seata.server.session.SessionCondition)

Aggregations

TransactionWriteStore (io.seata.server.storage.file.TransactionWriteStore)10 StoreException (io.seata.common.exception.StoreException)4 BranchSession (io.seata.server.session.BranchSession)4 GlobalSession (io.seata.server.session.GlobalSession)4 IOException (java.io.IOException)4 FileTransactionStoreManager (io.seata.server.storage.file.store.FileTransactionStoreManager)3 SessionStorable (io.seata.server.store.SessionStorable)3 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)2 SessionCondition (io.seata.server.session.SessionCondition)2 AbstractTransactionStoreManager (io.seata.server.store.AbstractTransactionStoreManager)2 TransactionStoreManager (io.seata.server.store.TransactionStoreManager)2 RandomAccessFile (java.io.RandomAccessFile)2 ByteBuffer (java.nio.ByteBuffer)2 FileChannel (java.nio.channels.FileChannel)2 ArrayList (java.util.ArrayList)2 ReloadableStore (io.seata.server.storage.file.ReloadableStore)1 LogOperation (io.seata.server.store.TransactionStoreManager.LogOperation)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1