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