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);
}
}
}
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;
}
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);
}
}
}
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;
}
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;
}
Aggregations