Search in sources :

Example 1 with PollForDecisionTaskResponse

use of com.uber.cadence.PollForDecisionTaskResponse in project cadence-client by uber-java.

the class ReplayDecisionTaskHandler method handleDecisionTaskImpl.

private Result handleDecisionTaskImpl(DecisionTaskWithHistoryIterator decisionTaskIterator) throws Throwable {
    HistoryHelper historyHelper = new HistoryHelper(decisionTaskIterator);
    ReplayDecider decider = createDecider(historyHelper);
    PollForDecisionTaskResponse decisionTask = historyHelper.getDecisionTask();
    if (decisionTask.isSetQuery()) {
        RespondQueryTaskCompletedRequest queryCompletedRequest = new RespondQueryTaskCompletedRequest();
        queryCompletedRequest.setTaskToken(decisionTask.getTaskToken());
        try {
            byte[] queryResult = decider.query(decisionTask.getQuery());
            queryCompletedRequest.setQueryResult(queryResult);
            queryCompletedRequest.setCompletedType(QueryTaskCompletedType.COMPLETED);
        } catch (Throwable e) {
            // TODO: Appropriate exception serialization.
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            queryCompletedRequest.setErrorMessage(sw.toString());
            queryCompletedRequest.setCompletedType(QueryTaskCompletedType.FAILED);
        }
        return new DecisionTaskHandler.Result(null, null, queryCompletedRequest, null);
    } else {
        decider.decide();
        DecisionsHelper decisionsHelper = decider.getDecisionsHelper();
        List<Decision> decisions = decisionsHelper.getDecisions();
        byte[] context = decisionsHelper.getWorkflowContextDataToReturn();
        if (log.isTraceEnabled()) {
            WorkflowExecution execution = decisionTask.getWorkflowExecution();
            log.trace("WorkflowTask startedEventId=" + decisionTask.getStartedEventId() + ", WorkflowID=" + execution.getWorkflowId() + ", RunID=" + execution.getRunId() + " completed with " + WorkflowExecutionUtils.prettyPrintDecisions(decisions));
        } else if (log.isDebugEnabled()) {
            WorkflowExecution execution = decisionTask.getWorkflowExecution();
            log.debug("WorkflowTask startedEventId=" + decisionTask.getStartedEventId() + ", WorkflowID=" + execution.getWorkflowId() + ", RunID=" + execution.getRunId() + " completed with " + decisions.size() + " new decisions");
        }
        RespondDecisionTaskCompletedRequest completedRequest = new RespondDecisionTaskCompletedRequest();
        completedRequest.setTaskToken(decisionTask.getTaskToken());
        completedRequest.setDecisions(decisions);
        completedRequest.setExecutionContext(context);
        return new DecisionTaskHandler.Result(completedRequest, null, null, null);
    }
}
Also used : PollForDecisionTaskResponse(com.uber.cadence.PollForDecisionTaskResponse) Decision(com.uber.cadence.Decision) StringWriter(java.io.StringWriter) RespondQueryTaskCompletedRequest(com.uber.cadence.RespondQueryTaskCompletedRequest) WorkflowExecution(com.uber.cadence.WorkflowExecution) RespondDecisionTaskCompletedRequest(com.uber.cadence.RespondDecisionTaskCompletedRequest) PrintWriter(java.io.PrintWriter)

Example 2 with PollForDecisionTaskResponse

use of com.uber.cadence.PollForDecisionTaskResponse in project cadence-client by uber-java.

the class ReplayDecisionTaskHandler method createDecider.

private ReplayDecider createDecider(HistoryHelper historyHelper) throws Exception {
    PollForDecisionTaskResponse decisionTask = historyHelper.getDecisionTask();
    WorkflowType workflowType = decisionTask.getWorkflowType();
    DecisionsHelper decisionsHelper = new DecisionsHelper(decisionTask);
    ReplayWorkflow workflow = workflowFactory.getWorkflow(workflowType);
    return new ReplayDecider(domain, workflow, historyHelper, decisionsHelper);
}
Also used : WorkflowType(com.uber.cadence.WorkflowType) PollForDecisionTaskResponse(com.uber.cadence.PollForDecisionTaskResponse)

Example 3 with PollForDecisionTaskResponse

use of com.uber.cadence.PollForDecisionTaskResponse in project cadence-client by uber-java.

the class TestWorkflowMutableStateImpl method query.

@Override
public QueryWorkflowResponse query(QueryWorkflowRequest queryRequest) throws TException {
    QueryId queryId = new QueryId(executionId);
    PollForDecisionTaskResponse task = new PollForDecisionTaskResponse().setTaskToken(queryId.toBytes()).setWorkflowExecution(executionId.getExecution()).setWorkflowType(startRequest.getWorkflowType()).setQuery(queryRequest.getQuery());
    TaskListId taskListId = new TaskListId(queryRequest.getDomain(), startRequest.getTaskList().getName());
    store.sendQueryTask(executionId, taskListId, task);
    CompletableFuture<QueryWorkflowResponse> result = new CompletableFuture<>();
    queries.put(queryId.getQueryId(), result);
    try {
        return result.get();
    } catch (InterruptedException e) {
        return new QueryWorkflowResponse();
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof TException) {
            throw (TException) cause;
        }
        throw new InternalServiceError(Throwables.getStackTraceAsString(cause));
    }
}
Also used : TException(org.apache.thrift.TException) CompletableFuture(java.util.concurrent.CompletableFuture) QueryWorkflowResponse(com.uber.cadence.QueryWorkflowResponse) TaskListId(com.uber.cadence.internal.testservice.TestWorkflowStore.TaskListId) PollForDecisionTaskResponse(com.uber.cadence.PollForDecisionTaskResponse) InternalServiceError(com.uber.cadence.InternalServiceError) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with PollForDecisionTaskResponse

use of com.uber.cadence.PollForDecisionTaskResponse in project cadence-client by uber-java.

the class TestWorkflowStoreImpl method save.

@Override
public long save(RequestContext ctx) throws InternalServiceError, EntityNotExistsError {
    long result;
    lock.lock();
    boolean historiesEmpty = histories.isEmpty();
    try {
        ExecutionId executionId = ctx.getExecutionId();
        HistoryStore history = histories.get(executionId);
        List<HistoryEvent> events = ctx.getEvents();
        if (history == null) {
            if (events.isEmpty() || events.get(0).getEventType() != EventType.WorkflowExecutionStarted) {
                throw new IllegalStateException("No history found for " + executionId);
            }
            history = new HistoryStore(executionId, lock);
            histories.put(executionId, history);
        }
        history.checkNextEventId(ctx.getInitialEventId());
        history.addAllLocked(events, ctx.currentTimeInNanoseconds());
        result = history.getNextEventIdLocked();
        timerService.updateLocks(ctx.getTimerLocks());
        ctx.fireCallbacks(history.getEventsLocked().size());
    } finally {
        if (historiesEmpty && !histories.isEmpty()) {
            // Initially locked in the constructor
            timerService.unlockTimeSkipping();
        }
        lock.unlock();
    }
    // Push tasks to the queues out of locks
    DecisionTask decisionTask = ctx.getDecisionTask();
    if (decisionTask != null) {
        BlockingQueue<PollForDecisionTaskResponse> decisionsQueue = getDecisionTaskListQueue(decisionTask.getTaskListId());
        decisionsQueue.add(decisionTask.getTask());
    }
    List<ActivityTask> activityTasks = ctx.getActivityTasks();
    if (activityTasks != null) {
        for (ActivityTask activityTask : activityTasks) {
            BlockingQueue<PollForActivityTaskResponse> activitiesQueue = getActivityTaskListQueue(activityTask.getTaskListId());
            activitiesQueue.add(activityTask.getTask());
        }
    }
    List<Timer> timers = ctx.getTimers();
    if (timers != null) {
        for (Timer t : timers) {
            timerService.schedule(Duration.ofSeconds(t.getDelaySeconds()), t.getCallback());
        }
    }
    return result;
}
Also used : PollForDecisionTaskResponse(com.uber.cadence.PollForDecisionTaskResponse) HistoryEvent(com.uber.cadence.HistoryEvent) Timer(com.uber.cadence.internal.testservice.RequestContext.Timer) PollForActivityTaskResponse(com.uber.cadence.PollForActivityTaskResponse)

Example 5 with PollForDecisionTaskResponse

use of com.uber.cadence.PollForDecisionTaskResponse in project cadence-client by uber-java.

the class TestWorkflowService method PollForDecisionTask.

@Override
public PollForDecisionTaskResponse PollForDecisionTask(PollForDecisionTaskRequest pollRequest) throws BadRequestError, InternalServiceError, ServiceBusyError, TException {
    PollForDecisionTaskResponse task;
    try {
        task = store.pollForDecisionTask(pollRequest);
    } catch (InterruptedException e) {
        return new PollForDecisionTaskResponse();
    }
    ExecutionId executionId = new ExecutionId(pollRequest.getDomain(), task.getWorkflowExecution());
    TestWorkflowMutableState mutableState = getMutableState(executionId);
    try {
        mutableState.startDecisionTask(task, pollRequest);
        return task;
    } catch (EntityNotExistsError e) {
        if (log.isDebugEnabled()) {
            log.debug("Skipping outdated decision task for " + executionId, e);
        }
    // skip the task
    }
    return task;
}
Also used : PollForDecisionTaskResponse(com.uber.cadence.PollForDecisionTaskResponse) EntityNotExistsError(com.uber.cadence.EntityNotExistsError)

Aggregations

PollForDecisionTaskResponse (com.uber.cadence.PollForDecisionTaskResponse)6 HistoryEvent (com.uber.cadence.HistoryEvent)2 TaskListId (com.uber.cadence.internal.testservice.TestWorkflowStore.TaskListId)2 Decision (com.uber.cadence.Decision)1 DecisionTaskScheduledEventAttributes (com.uber.cadence.DecisionTaskScheduledEventAttributes)1 EntityNotExistsError (com.uber.cadence.EntityNotExistsError)1 InternalServiceError (com.uber.cadence.InternalServiceError)1 PollForActivityTaskResponse (com.uber.cadence.PollForActivityTaskResponse)1 QueryWorkflowResponse (com.uber.cadence.QueryWorkflowResponse)1 RespondDecisionTaskCompletedRequest (com.uber.cadence.RespondDecisionTaskCompletedRequest)1 RespondQueryTaskCompletedRequest (com.uber.cadence.RespondQueryTaskCompletedRequest)1 WorkflowExecution (com.uber.cadence.WorkflowExecution)1 WorkflowType (com.uber.cadence.WorkflowType)1 Timer (com.uber.cadence.internal.testservice.RequestContext.Timer)1 DecisionTask (com.uber.cadence.internal.testservice.TestWorkflowStore.DecisionTask)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TException (org.apache.thrift.TException)1