Skip to content

Commit

Permalink
bugfix: clean root context when state machine inst record failed (apa…
Browse files Browse the repository at this point in the history
  • Loading branch information
anselleeyy committed Dec 8, 2020
1 parent c2c3488 commit 465f28b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import io.seata.common.Constants;
import io.seata.common.exception.FrameworkErrorCode;
import io.seata.common.exception.StoreException;
import io.seata.common.util.CollectionUtils;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
Expand Down Expand Up @@ -97,18 +98,31 @@ public void recordStateMachineStarted(StateMachineInstance machineInstance, Proc
beginTransaction(machineInstance, context);
}

try {
if (StringUtils.isEmpty(machineInstance.getId()) && seqGenerator != null) {
machineInstance.setId(seqGenerator.generate(DomainConstants.SEQ_ENTITY_STATE_MACHINE_INST));
}

if (StringUtils.isEmpty(machineInstance.getId()) && seqGenerator != null) {
machineInstance.setId(seqGenerator.generate(DomainConstants.SEQ_ENTITY_STATE_MACHINE_INST));
}

// bind SAGA branch type
RootContext.bindBranchType(BranchType.SAGA);
// bind SAGA branch type
RootContext.bindBranchType(BranchType.SAGA);

// save to db
machineInstance.setSerializedStartParams(paramsSerializer.serialize(machineInstance.getStartParams()));
executeUpdate(stateLogStoreSqls.getRecordStateMachineStartedSql(dbType),
// save to db
machineInstance.setSerializedStartParams(paramsSerializer.serialize(machineInstance.getStartParams()));
int effect = executeUpdate(stateLogStoreSqls.getRecordStateMachineStartedSql(dbType),
STATE_MACHINE_INSTANCE_TO_STATEMENT_FOR_INSERT, machineInstance);
if (effect < 1) {
throw new StoreException("StateMachineInstance record start error, Xid: " + machineInstance.getId(),
FrameworkErrorCode.OperationDenied);
}
} catch (StoreException e) {
LOGGER.error("Record statemachine start error: {}, StateMachine: {}, XID: {}, Reason: {}",
e.getErrcode(), machineInstance.getStateMachine().getName(), machineInstance.getId(), e.getMessage(), e);
// clear
RootContext.unbind();
RootContext.unbindBranchType();
sagaTransactionalTemplate.cleanUp();
throw e;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package io.seata.saga.engine.db;

import io.seata.common.exception.FrameworkErrorCode;
import io.seata.common.exception.StoreException;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.core.model.GlobalStatus;
import io.seata.saga.engine.AsyncCallback;
Expand Down Expand Up @@ -726,6 +728,18 @@ public void testStateMachineTransTimeoutAsync() throws Exception {
((DefaultStateMachineConfig)stateMachineEngine.getStateMachineConfig()).setTransOperationTimeout(60000 * 30);
}

@Test
public void testStateMachineRecordFailed() {

String businessKey = "bizKey";

Assertions.assertDoesNotThrow(() -> stateMachineEngine.startWithBusinessKey("simpleTestStateMachine", null, businessKey, new HashMap<>()));

// use same biz key to mock exception
Assertions.assertThrows(StoreException.class, () -> stateMachineEngine.startWithBusinessKey("simpleTestStateMachine", null, businessKey, new HashMap<>()));
Assertions.assertNull(RootContext.getXID());
}

private void doTestStateMachineTransTimeout(Map<String, Object> paramMap) throws Exception {

long start = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.seata.saga.engine.mock;

import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.core.model.GlobalStatus;
import io.seata.saga.engine.sequence.SpringJvmUUIDSeqGenerator;
Expand Down Expand Up @@ -52,6 +53,7 @@ public void begin() throws TransactionException {
public void begin(int timeout) throws TransactionException {
status = GlobalStatus.Begin;
xid = uuidSeqGenerator.generate(null).toString();
RootContext.bind(xid);
}

@Override
Expand Down

0 comments on commit 465f28b

Please sign in to comment.