Search in sources :

Example 6 with TransactionWriteStore

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

the class FileTransactionStoreManager method parseDataFile.

private List<TransactionWriteStore> parseDataFile(File file, int readSize, long currentOffset, boolean isHistory) {
    List<TransactionWriteStore> transactionWriteStores = new ArrayList<>(readSize);
    RandomAccessFile raf = null;
    FileChannel fileChannel = null;
    try {
        raf = new RandomAccessFile(file, "r");
        raf.seek(currentOffset);
        fileChannel = raf.getChannel();
        fileChannel.position(currentOffset);
        long size = raf.length();
        ByteBuffer buffSize = ByteBuffer.allocate(MARK_SIZE);
        while (fileChannel.position() < size) {
            try {
                buffSize.clear();
                int avilReadSize = fileChannel.read(buffSize);
                if (avilReadSize != MARK_SIZE) {
                    break;
                }
                buffSize.flip();
                int bodySize = buffSize.getInt();
                byte[] byBody = new byte[bodySize];
                ByteBuffer buffBody = ByteBuffer.wrap(byBody);
                avilReadSize = fileChannel.read(buffBody);
                if (avilReadSize != bodySize) {
                    break;
                }
                TransactionWriteStore writeStore = new TransactionWriteStore();
                writeStore.decode(byBody);
                transactionWriteStores.add(writeStore);
                if (transactionWriteStores.size() == readSize) {
                    break;
                }
            } catch (Exception ex) {
                LOGGER.error("decode data file error:{}", ex.getMessage(), ex);
                break;
            }
        }
        return transactionWriteStores;
    } catch (IOException exx) {
        LOGGER.error("parse data file error:{},file:{}", exx.getMessage(), file.getName(), exx);
        return null;
    } finally {
        try {
            if (fileChannel != null) {
                if (isHistory) {
                    recoverHisOffset = fileChannel.position();
                } else {
                    recoverCurrOffset = fileChannel.position();
                }
            }
            closeFile(raf);
        } catch (IOException exx) {
            LOGGER.error("file close error{}", exx.getMessage(), exx);
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) TransactionWriteStore(io.seata.server.storage.file.TransactionWriteStore) FileChannel(java.nio.channels.FileChannel) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) StoreException(io.seata.common.exception.StoreException) IOException(java.io.IOException)

Example 7 with TransactionWriteStore

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

the class FileTransactionStoreManager method writeSession.

@Override
public boolean writeSession(LogOperation logOperation, SessionStorable session) {
    writeSessionLock.lock();
    long curFileTrxNum;
    try {
        if (!writeDataFile(new TransactionWriteStore(session, logOperation).encode())) {
            return false;
        }
        lastModifiedTime = System.currentTimeMillis();
        curFileTrxNum = FILE_TRX_NUM.incrementAndGet();
        if (curFileTrxNum % PER_FILE_BLOCK_SIZE == 0 && (System.currentTimeMillis() - trxStartTimeMills) > MAX_TRX_TIMEOUT_MILLS) {
            return saveHistory();
        }
    } catch (Exception exx) {
        LOGGER.error("writeSession error, {}", exx.getMessage(), exx);
        return false;
    } finally {
        writeSessionLock.unlock();
    }
    flushDisk(curFileTrxNum, currFileChannel);
    return true;
}
Also used : TransactionWriteStore(io.seata.server.storage.file.TransactionWriteStore) StoreException(io.seata.common.exception.StoreException) IOException(java.io.IOException)

Example 8 with TransactionWriteStore

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

the class FileTransactionStoreManager method parseDataFile.

private List<TransactionWriteStore> parseDataFile(File file, int readSize, long currentOffset, boolean isHistory) {
    List<TransactionWriteStore> transactionWriteStores = new ArrayList<>(readSize);
    RandomAccessFile raf = null;
    FileChannel fileChannel = null;
    try {
        raf = new RandomAccessFile(file, "r");
        raf.seek(currentOffset);
        fileChannel = raf.getChannel();
        fileChannel.position(currentOffset);
        long size = raf.length();
        ByteBuffer buffSize = ByteBuffer.allocate(MARK_SIZE);
        while (fileChannel.position() < size) {
            try {
                buffSize.clear();
                int avilReadSize = fileChannel.read(buffSize);
                if (avilReadSize != MARK_SIZE) {
                    break;
                }
                buffSize.flip();
                int bodySize = buffSize.getInt();
                byte[] byBody = new byte[bodySize];
                ByteBuffer buffBody = ByteBuffer.wrap(byBody);
                avilReadSize = fileChannel.read(buffBody);
                if (avilReadSize != bodySize) {
                    break;
                }
                TransactionWriteStore writeStore = new TransactionWriteStore();
                writeStore.decode(byBody);
                transactionWriteStores.add(writeStore);
                if (transactionWriteStores.size() == readSize) {
                    break;
                }
            } catch (Exception ex) {
                LOGGER.error("decode data file error:{}", ex.getMessage(), ex);
                break;
            }
        }
        return transactionWriteStores;
    } catch (IOException exx) {
        LOGGER.error("parse data file error:{},file:{}", exx.getMessage(), file.getName(), exx);
        return null;
    } finally {
        try {
            if (fileChannel != null) {
                if (isHistory) {
                    recoverHisOffset = fileChannel.position();
                } else {
                    recoverCurrOffset = fileChannel.position();
                }
            }
            closeFile(raf);
        } catch (IOException exx) {
            LOGGER.error("file close error{}", exx.getMessage(), exx);
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) TransactionWriteStore(io.seata.server.storage.file.TransactionWriteStore) FileChannel(java.nio.channels.FileChannel) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) StoreException(io.seata.common.exception.StoreException) IOException(java.io.IOException)

Example 9 with TransactionWriteStore

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

the class FileTransactionStoreManager method writeSession.

@Override
public boolean writeSession(LogOperation logOperation, SessionStorable session) {
    writeSessionLock.lock();
    long curFileTrxNum;
    try {
        if (!writeDataFile(new TransactionWriteStore(session, logOperation).encode())) {
            return false;
        }
        lastModifiedTime = System.currentTimeMillis();
        curFileTrxNum = FILE_TRX_NUM.incrementAndGet();
        if (curFileTrxNum % PER_FILE_BLOCK_SIZE == 0 && (System.currentTimeMillis() - trxStartTimeMills) > MAX_TRX_TIMEOUT_MILLS) {
            return saveHistory();
        }
    } catch (Exception exx) {
        LOGGER.error("writeSession error, {}", exx.getMessage(), exx);
        return false;
    } finally {
        writeSessionLock.unlock();
    }
    flushDisk(curFileTrxNum, currFileChannel);
    return true;
}
Also used : TransactionWriteStore(io.seata.server.storage.file.TransactionWriteStore) StoreException(io.seata.common.exception.StoreException) IOException(java.io.IOException)

Example 10 with TransactionWriteStore

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

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) {
                TransactionWriteStore branchWriteStore = new TransactionWriteStore(branchSession, LogOperation.BRANCH_ADD);
                data = branchWriteStore.encode();
                if (!writeDataFrame(data)) {
                    return false;
                }
            }
        }
    }
    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