🎨 Improving structure / format of the code. Switch Case 语句格式 调整

This commit is contained in:
lbw committed 2022-07-03 08:37:43 +08:00
1 parent 2dfebcb79e
commit ca20a8a732
17 files changed
+657 -656

No files matched your search

@@ -212,39 +212,39 @@ public class DefaultCore implements Core {
branchSession);
switch (branchStatus) {
case PhaseTwo_Committed:
globalSession.removeBranch(branchSession);
return CONTINUE;
case PhaseTwo_CommitFailed_Unretryable:
if (globalSession.canBeCommittedAsync()) {
LOGGER.error(
"Committing branch transaction[{}], status: PhaseTwo_CommitFailed_Unretryable, please check the business log.",
branchSession.getBranchId());
case PhaseTwo_Committed:
globalSession.removeBranch(branchSession);
return CONTINUE;
}
else {
SessionHelper.endCommitFailed(globalSession);
LOGGER.error(
"Committing global transaction[{}] finally failed, caused by branch transaction[{}] commit failed.",
globalSession.getXid(), branchSession.getBranchId());
return false;
}
default:
if (!retrying) {
globalSession.queueToRetryCommit();
return false;
}
if (globalSession.canBeCommittedAsync()) {
LOGGER.error("Committing branch transaction[{}], status:{} and will retry later",
branchSession.getBranchId(), branchStatus);
return CONTINUE;
}
else {
LOGGER.error(
"Committing global transaction[{}] failed, caused by branch transaction[{}] commit failed, will retry later.",
globalSession.getXid(), branchSession.getBranchId());
return false;
}
case PhaseTwo_CommitFailed_Unretryable:
if (globalSession.canBeCommittedAsync()) {
LOGGER.error(
"Committing branch transaction[{}], status: PhaseTwo_CommitFailed_Unretryable, please check the business log.",
branchSession.getBranchId());
return CONTINUE;
}
else {
SessionHelper.endCommitFailed(globalSession);
LOGGER.error(
"Committing global transaction[{}] finally failed, caused by branch transaction[{}] commit failed.",
globalSession.getXid(), branchSession.getBranchId());
return false;
}
default:
if (!retrying) {
globalSession.queueToRetryCommit();
return false;
}
if (globalSession.canBeCommittedAsync()) {
LOGGER.error("Committing branch transaction[{}], status:{} and will retry later",
branchSession.getBranchId(), branchStatus);
return CONTINUE;
}
else {
LOGGER.error(
"Committing global transaction[{}] failed, caused by branch transaction[{}] commit failed, will retry later.",
globalSession.getXid(), branchSession.getBranchId());
return false;
}
}
}
catch (Exception ex) {
@@ -331,23 +331,23 @@ public class DefaultCore implements Core {
try {
BranchStatus branchStatus = branchRollback(globalSession, branchSession);
switch (branchStatus) {
case PhaseTwo_Rollbacked:
globalSession.removeBranch(branchSession);
LOGGER.info("Rollback branch transaction successfully, xid = {} branchId = {}",
globalSession.getXid(), branchSession.getBranchId());
return CONTINUE;
case PhaseTwo_RollbackFailed_Unretryable:
SessionHelper.endRollbackFailed(globalSession);
LOGGER.info("Rollback branch transaction fail and stop retry, xid = {} branchId = {}",
globalSession.getXid(), branchSession.getBranchId());
return false;
default:
LOGGER.info("Rollback branch transaction fail and will retry, xid = {} branchId = {}",
globalSession.getXid(), branchSession.getBranchId());
if (!retrying) {
globalSession.queueToRetryRollback();
}
return false;
case PhaseTwo_Rollbacked:
globalSession.removeBranch(branchSession);
LOGGER.info("Rollback branch transaction successfully, xid = {} branchId = {}",
globalSession.getXid(), branchSession.getBranchId());
return CONTINUE;
case PhaseTwo_RollbackFailed_Unretryable:
SessionHelper.endRollbackFailed(globalSession);
LOGGER.info("Rollback branch transaction fail and stop retry, xid = {} branchId = {}",
globalSession.getXid(), branchSession.getBranchId());
return false;
default:
LOGGER.info("Rollback branch transaction fail and will retry, xid = {} branchId = {}",
globalSession.getXid(), branchSession.getBranchId());
if (!retrying) {
globalSession.queueToRetryRollback();
}
return false;
}
}
catch (Exception ex) {
@@ -159,45 +159,45 @@ public class SessionHolder {
GlobalSession globalSession = iterator.next();
GlobalStatus globalStatus = globalSession.getStatus();
switch (globalStatus) {
case UnKnown:
case Committed:
case CommitFailed:
case Rollbacked:
case RollbackFailed:
case TimeoutRollbacked:
case TimeoutRollbackFailed:
case Finished:
removeGlobalSessions.add(globalSession);
break;
case AsyncCommitting:
if (storeMode == StoreMode.FILE) {
queueToAsyncCommitting(globalSession);
}
break;
default: {
if (storeMode == StoreMode.FILE) {
lockBranchSessions(globalSession.getSortedBranches());
switch (globalStatus) {
case Committing:
case CommitRetrying:
queueToRetryCommit(globalSession);
break;
case Rollbacking:
case RollbackRetrying:
case TimeoutRollbacking:
case TimeoutRollbackRetrying:
queueToRetryRollback(globalSession);
break;
case Begin:
globalSession.setActive(true);
break;
default:
throw new ShouldNeverHappenException("NOT properly handled " + globalStatus);
case UnKnown:
case Committed:
case CommitFailed:
case Rollbacked:
case RollbackFailed:
case TimeoutRollbacked:
case TimeoutRollbackFailed:
case Finished:
removeGlobalSessions.add(globalSession);
break;
case AsyncCommitting:
if (storeMode == StoreMode.FILE) {
queueToAsyncCommitting(globalSession);
}
break;
default: {
if (storeMode == StoreMode.FILE) {
lockBranchSessions(globalSession.getSortedBranches());
switch (globalStatus) {
case Committing:
case CommitRetrying:
queueToRetryCommit(globalSession);
break;
case Rollbacking:
case RollbackRetrying:
case TimeoutRollbacking:
case TimeoutRollbackRetrying:
queueToRetryRollback(globalSession);
break;
case Begin:
globalSession.setActive(true);
break;
default:
throw new ShouldNeverHappenException("NOT properly handled " + globalStatus);
}
}
break;
}
break;
}
}
}
for (GlobalSession globalSession : removeGlobalSessions) {
@@ -109,18 +109,18 @@ public class TransactionWriteStore implements SessionStorable {
private SessionStorable getSessionInstanceByOperation(LogOperation logOperation) {
SessionStorable sessionStorable = null;
switch (logOperation) {
case GLOBAL_ADD:
case GLOBAL_UPDATE:
case GLOBAL_REMOVE:
sessionStorable = new GlobalSession();
break;
case BRANCH_ADD:
case BRANCH_UPDATE:
case BRANCH_REMOVE:
sessionStorable = new BranchSession();
break;
default:
throw new ShouldNeverHappenException("incorrect logOperation");
case GLOBAL_ADD:
case GLOBAL_UPDATE:
case GLOBAL_REMOVE:
sessionStorable = new GlobalSession();
break;
case BRANCH_ADD:
case BRANCH_UPDATE:
case BRANCH_REMOVE:
sessionStorable = new BranchSession();
break;
default:
throw new ShouldNeverHappenException("incorrect logOperation");
}
return sessionStorable;
}
@@ -180,17 +180,17 @@ public class FileSessionManager extends AbstractSessionManager implements Reload
private boolean checkSessionStatus(GlobalSession globalSession) {
GlobalStatus globalStatus = globalSession.getStatus();
switch (globalStatus) {
case UnKnown:
case Committed:
case CommitFailed:
case Rollbacked:
case RollbackFailed:
case TimeoutRollbacked:
case TimeoutRollbackFailed:
case Finished:
return false;
default:
return true;
case UnKnown:
case Committed:
case CommitFailed:
case Rollbacked:
case RollbackFailed:
case TimeoutRollbacked:
case TimeoutRollbackFailed:
case Finished:
return false;
default:
return true;
}
}
@@ -212,119 +212,119 @@ public class FileSessionManager extends AbstractSessionManager implements Reload
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);
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;
}
else {
removedGlobalBuffer.add(globalSession.getXid());
unhandledBranchBuffer.remove(globalSession.getXid());
if (removedGlobalBuffer.contains(globalSession.getXid())) {
break;
}
}
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 + "]");
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 {
found.remove(theBranch);
if (this.checkSessionStatus(globalSession)) {
foundGlobalSession.setStatus(globalSession.getStatus());
}
else {
sessionMap.remove(globalSession.getXid());
removedGlobalBuffer.add(globalSession.getXid());
unhandledBranchBuffer.remove(globalSession.getXid());
}
}
break;
}
break;
}
default:
throw new ShouldNeverHappenException("Unknown Operation: " + logOperation);
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);
}
}
@@ -106,46 +106,47 @@ public class SagaCore extends AbstractCore {
globalSession.getXid(), -1, getSagaResourceId(globalSession), globalSession.getStatus().name()));
switch (branchStatus) {
case PhaseTwo_Committed:
removeAllBranches(globalSession);
LOGGER.info("Successfully committed SAGA global[" + globalSession.getXid() + "]");
break;
case PhaseTwo_Rollbacked:
LOGGER.info("Successfully rollbacked SAGA global[" + globalSession.getXid() + "]");
removeAllBranches(globalSession);
SessionHelper.endRollbacked(globalSession);
return false;
case PhaseTwo_RollbackFailed_Retryable:
LOGGER.error("By [{}], failed to rollback SAGA global [{}], will retry later.", branchStatus,
globalSession.getXid());
SessionHolder.getRetryCommittingSessionManager().removeGlobalSession(globalSession);
globalSession.queueToRetryRollback();
return false;
case PhaseOne_Failed:
LOGGER.error("By [{}], finish SAGA global [{}]", branchStatus, globalSession.getXid());
removeAllBranches(globalSession);
globalSession.changeStatus(GlobalStatus.Finished);
globalSession.end();
return false;
case PhaseTwo_CommitFailed_Unretryable:
if (globalSession.canBeCommittedAsync()) {
LOGGER.error("By [{}], failed to commit SAGA global [{}]", branchStatus, globalSession.getXid());
case PhaseTwo_Committed:
removeAllBranches(globalSession);
LOGGER.info("Successfully committed SAGA global[" + globalSession.getXid() + "]");
break;
}
else {
SessionHelper.endCommitFailed(globalSession);
LOGGER.error("Finally, failed to commit SAGA global[{}]", globalSession.getXid());
case PhaseTwo_Rollbacked:
LOGGER.info("Successfully rollbacked SAGA global[" + globalSession.getXid() + "]");
removeAllBranches(globalSession);
SessionHelper.endRollbacked(globalSession);
return false;
}
default:
if (!retrying) {
globalSession.queueToRetryCommit();
case PhaseTwo_RollbackFailed_Retryable:
LOGGER.error("By [{}], failed to rollback SAGA global [{}], will retry later.", branchStatus,
globalSession.getXid());
SessionHolder.getRetryCommittingSessionManager().removeGlobalSession(globalSession);
globalSession.queueToRetryRollback();
return false;
}
else {
LOGGER.error("Failed to commit SAGA global[{}], will retry later.", globalSession.getXid());
case PhaseOne_Failed:
LOGGER.error("By [{}], finish SAGA global [{}]", branchStatus, globalSession.getXid());
removeAllBranches(globalSession);
globalSession.changeStatus(GlobalStatus.Finished);
globalSession.end();
return false;
}
case PhaseTwo_CommitFailed_Unretryable:
if (globalSession.canBeCommittedAsync()) {
LOGGER.error("By [{}], failed to commit SAGA global [{}]", branchStatus,
globalSession.getXid());
break;
}
else {
SessionHelper.endCommitFailed(globalSession);
LOGGER.error("Finally, failed to commit SAGA global[{}]", globalSession.getXid());
return false;
}
default:
if (!retrying) {
globalSession.queueToRetryCommit();
return false;
}
else {
LOGGER.error("Failed to commit SAGA global[{}], will retry later.", globalSession.getXid());
return false;
}
}
}
catch (Exception ex) {
@@ -166,26 +167,26 @@ public class SagaCore extends AbstractCore {
globalSession.getXid(), -1, getSagaResourceId(globalSession), globalSession.getStatus().name()));
switch (branchStatus) {
case PhaseTwo_Rollbacked:
removeAllBranches(globalSession);
LOGGER.info("Successfully rollbacked SAGA global[{}]", globalSession.getXid());
break;
case PhaseTwo_RollbackFailed_Unretryable:
SessionHelper.endRollbackFailed(globalSession);
LOGGER.error("Failed to rollback SAGA global[{}]", globalSession.getXid());
return false;
case PhaseTwo_CommitFailed_Retryable:
SessionHolder.getRetryRollbackingSessionManager().removeGlobalSession(globalSession);
globalSession.queueToRetryCommit();
LOGGER.warn("Retry by custom recover strategy [Forward] on timeout, SAGA global[{}]",
globalSession.getXid());
return false;
default:
LOGGER.error("Failed to rollback SAGA global[{}]", globalSession.getXid());
if (!retrying) {
globalSession.queueToRetryRollback();
}
return false;
case PhaseTwo_Rollbacked:
removeAllBranches(globalSession);
LOGGER.info("Successfully rollbacked SAGA global[{}]", globalSession.getXid());
break;
case PhaseTwo_RollbackFailed_Unretryable:
SessionHelper.endRollbackFailed(globalSession);
LOGGER.error("Failed to rollback SAGA global[{}]", globalSession.getXid());
return false;
case PhaseTwo_CommitFailed_Retryable:
SessionHolder.getRetryRollbackingSessionManager().removeGlobalSession(globalSession);
globalSession.queueToRetryCommit();
LOGGER.warn("Retry by custom recover strategy [Forward] on timeout, SAGA global[{}]",
globalSession.getXid());
return false;
default:
LOGGER.error("Failed to rollback SAGA global[{}]", globalSession.getXid());
if (!retrying) {
globalSession.queueToRetryRollback();
}
return false;
}
}
catch (Exception ex) {