Search in sources :

Example 1 with AsyncCallback

use of io.seata.saga.engine.AsyncCallback in project seata by seata.

the class StateMachineDBTests method simpleChoiceTestStateMachineAsyncConcurrently.

@Test
@Disabled("https://github.com/seata/seata/issues/2414#issuecomment-639546811")
public void simpleChoiceTestStateMachineAsyncConcurrently() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(100);
    final List<Exception> exceptions = new ArrayList<>();
    final AsyncCallback asyncCallback = new AsyncCallback() {

        @Override
        public void onFinished(ProcessContext context, StateMachineInstance stateMachineInstance) {
            countDownLatch.countDown();
        }

        @Override
        public void onError(ProcessContext context, StateMachineInstance stateMachineInstance, Exception exp) {
            countDownLatch.countDown();
            exceptions.add(exp);
        }
    };
    long start = System.currentTimeMillis();
    for (int i = 0; i < 10; i++) {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int j = 0; j < 10; j++) {
                    Map<String, Object> paramMap = new HashMap<>();
                    paramMap.put("a", 1);
                    paramMap.put("barThrowException", "false");
                    String stateMachineName = "simpleCompensationStateMachine";
                    try {
                        stateMachineEngine.startAsync(stateMachineName, null, paramMap, asyncCallback);
                    } catch (Exception e) {
                        countDownLatch.countDown();
                        exceptions.add(e);
                    }
                }
            }
        });
        t.start();
    }
    countDownLatch.await(10000, TimeUnit.MILLISECONDS);
    if (exceptions.size() > 0) {
        Assertions.fail(exceptions.get(0));
    }
    long cost = System.currentTimeMillis() - start;
    System.out.println("====== cost :" + cost);
}
Also used : AsyncCallback(io.seata.saga.engine.AsyncCallback) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) TransactionException(io.seata.core.exception.TransactionException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) StoreException(io.seata.common.exception.StoreException) ProcessContext(io.seata.saga.proctrl.ProcessContext) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 2 with AsyncCallback

use of io.seata.saga.engine.AsyncCallback in project seata by seata.

the class EngineUtils method endStateMachine.

/**
 * end StateMachine
 *
 * @param context
 */
public static void endStateMachine(ProcessContext context) {
    if (context.hasVariable(DomainConstants.VAR_NAME_IS_LOOP_STATE)) {
        if (context.hasVariable(DomainConstants.LOOP_SEMAPHORE)) {
            Semaphore semaphore = (Semaphore) context.getVariable(DomainConstants.LOOP_SEMAPHORE);
            semaphore.release();
        }
        return;
    }
    StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
    stateMachineInstance.setGmtEnd(new Date());
    Exception exp = (Exception) context.getVariable(DomainConstants.VAR_NAME_CURRENT_EXCEPTION);
    if (exp != null) {
        stateMachineInstance.setException(exp);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Exception Occurred: " + exp);
        }
    }
    StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
    stateMachineConfig.getStatusDecisionStrategy().decideOnEndState(context, stateMachineInstance, exp);
    stateMachineInstance.getEndParams().putAll((Map<String, Object>) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONTEXT));
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    instruction.setEnd(true);
    stateMachineInstance.setRunning(false);
    stateMachineInstance.setGmtEnd(new Date());
    if (stateMachineInstance.getStateMachine().isPersist() && stateMachineConfig.getStateLogStore() != null) {
        stateMachineConfig.getStateLogStore().recordStateMachineFinished(stateMachineInstance, context);
    }
    AsyncCallback callback = (AsyncCallback) context.getVariable(DomainConstants.VAR_NAME_ASYNC_CALLBACK);
    if (callback != null) {
        if (exp != null) {
            callback.onError(context, stateMachineInstance, exp);
        } else {
            callback.onFinished(context, stateMachineInstance);
        }
    }
}
Also used : StateInstruction(io.seata.saga.engine.pcext.StateInstruction) AsyncCallback(io.seata.saga.engine.AsyncCallback) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) Semaphore(java.util.concurrent.Semaphore) Date(java.util.Date) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

Example 3 with AsyncCallback

use of io.seata.saga.engine.AsyncCallback in project seata by seata.

the class EngineUtils method failStateMachine.

/**
 * fail StateMachine
 *
 * @param context
 * @param exp
 */
public static void failStateMachine(ProcessContext context, Exception exp) {
    if (context.hasVariable(DomainConstants.VAR_NAME_IS_LOOP_STATE)) {
        return;
    }
    StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
    StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
    stateMachineConfig.getStatusDecisionStrategy().decideOnTaskStateFail(context, stateMachineInstance, exp);
    stateMachineInstance.getEndParams().putAll((Map<String, Object>) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONTEXT));
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    instruction.setEnd(true);
    stateMachineInstance.setRunning(false);
    stateMachineInstance.setGmtEnd(new Date());
    stateMachineInstance.setException(exp);
    if (stateMachineInstance.getStateMachine().isPersist() && stateMachineConfig.getStateLogStore() != null) {
        stateMachineConfig.getStateLogStore().recordStateMachineFinished(stateMachineInstance, context);
    }
    AsyncCallback callback = (AsyncCallback) context.getVariable(DomainConstants.VAR_NAME_ASYNC_CALLBACK);
    if (callback != null) {
        callback.onError(context, stateMachineInstance, exp);
    }
}
Also used : StateInstruction(io.seata.saga.engine.pcext.StateInstruction) AsyncCallback(io.seata.saga.engine.AsyncCallback) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) Date(java.util.Date) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

Aggregations

AsyncCallback (io.seata.saga.engine.AsyncCallback)3 StateMachineInstance (io.seata.saga.statelang.domain.StateMachineInstance)3 StateMachineConfig (io.seata.saga.engine.StateMachineConfig)2 StateInstruction (io.seata.saga.engine.pcext.StateInstruction)2 Date (java.util.Date)2 StoreException (io.seata.common.exception.StoreException)1 TransactionException (io.seata.core.exception.TransactionException)1 EngineExecutionException (io.seata.saga.engine.exception.EngineExecutionException)1 ProcessContext (io.seata.saga.proctrl.ProcessContext)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Semaphore (java.util.concurrent.Semaphore)1 Disabled (org.junit.jupiter.api.Disabled)1 Test (org.junit.jupiter.api.Test)1