Search in sources :

Example 1 with History

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

the class WorkflowTestingTest method testTimerCancellation.

@Test
public void testTimerCancellation() throws TException {
    Worker worker = testEnvironment.newWorker(TASK_LIST);
    worker.registerWorkflowImplementationTypes(TestTimerCancellationWorkflow.class);
    worker.registerActivitiesImplementations(new ActivityImpl());
    worker.start();
    WorkflowClient client = testEnvironment.newWorkflowClient();
    TestWorkflow workflow = client.newWorkflowStub(TestWorkflow.class);
    WorkflowExecution execution = WorkflowClient.start(workflow::workflow1, "input1");
    WorkflowStub untyped = client.newUntypedWorkflowStub(execution, Optional.empty());
    testEnvironment.sleep(Duration.ofHours(1));
    untyped.cancel();
    try {
        untyped.getResult(String.class);
        fail("unreacheable");
    } catch (CancellationException e) {
    }
    History history = testEnvironment.getWorkflowService().GetWorkflowExecutionHistory(new GetWorkflowExecutionHistoryRequest().setExecution(execution).setDomain(client.getDomain())).getHistory();
    List<HistoryEvent> historyEvents = history.getEvents();
    assertTrue(WorkflowExecutionUtils.prettyPrintHistory(history, false), WorkflowExecutionUtils.containsEvent(historyEvents, EventType.TimerCanceled));
}
Also used : WorkflowStub(com.uber.cadence.client.WorkflowStub) CancellationException(java.util.concurrent.CancellationException) GetWorkflowExecutionHistoryRequest(com.uber.cadence.GetWorkflowExecutionHistoryRequest) Worker(com.uber.cadence.worker.Worker) WorkflowClient(com.uber.cadence.client.WorkflowClient) WorkflowExecution(com.uber.cadence.WorkflowExecution) History(com.uber.cadence.History) HistoryEvent(com.uber.cadence.HistoryEvent) Test(org.junit.Test)

Example 2 with History

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

the class TestWorkflowStoreImpl method getWorkflowExecutionHistory.

@Override
public GetWorkflowExecutionHistoryResponse getWorkflowExecutionHistory(ExecutionId executionId, GetWorkflowExecutionHistoryRequest getRequest) throws EntityNotExistsError {
    HistoryStore history;
    // Used to eliminate the race condition on waitForNewEvents
    long expectedNextEventId;
    lock.lock();
    try {
        history = getHistoryStore(executionId);
        if (!getRequest.isWaitForNewEvent() && getRequest.getHistoryEventFilterType() != HistoryEventFilterType.CLOSE_EVENT) {
            List<HistoryEvent> events = history.getEventsLocked();
            // Copy the list as it is mutable. Individual events assumed immutable.
            ArrayList<HistoryEvent> eventsCopy = new ArrayList<>(events);
            return new GetWorkflowExecutionHistoryResponse().setHistory(new History().setEvents(eventsCopy));
        }
        expectedNextEventId = history.getNextEventIdLocked();
    } finally {
        lock.unlock();
    }
    List<HistoryEvent> events = history.waitForNewEvents(expectedNextEventId, getRequest.getHistoryEventFilterType());
    GetWorkflowExecutionHistoryResponse result = new GetWorkflowExecutionHistoryResponse();
    if (events != null) {
        result.setHistory(new History().setEvents(events));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) GetWorkflowExecutionHistoryResponse(com.uber.cadence.GetWorkflowExecutionHistoryResponse) HistoryEvent(com.uber.cadence.HistoryEvent) History(com.uber.cadence.History)

Example 3 with History

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

the class StateMachines method startDecisionTask.

private static void startDecisionTask(RequestContext ctx, DecisionTaskData data, PollForDecisionTaskRequest request, long notUsed) {
    DecisionTaskStartedEventAttributes a = new DecisionTaskStartedEventAttributes().setIdentity(request.getIdentity()).setScheduledEventId(data.scheduledEventId);
    HistoryEvent event = new HistoryEvent().setEventType(EventType.DecisionTaskStarted).setDecisionTaskStartedEventAttributes(a);
    long startedEventId = ctx.addEvent(event);
    ctx.onCommit((historySize) -> {
        data.decisionTask.setStartedEventId(startedEventId);
        DecisionTaskToken taskToken = new DecisionTaskToken(ctx.getExecutionId(), historySize);
        data.decisionTask.setTaskToken(taskToken.toBytes());
        GetWorkflowExecutionHistoryRequest getRequest = new GetWorkflowExecutionHistoryRequest().setDomain(request.getDomain()).setExecution(ctx.getExecution());
        List<HistoryEvent> events;
        try {
            events = data.store.getWorkflowExecutionHistory(ctx.getExecutionId(), getRequest).getHistory().getEvents();
        } catch (EntityNotExistsError entityNotExistsError) {
            throw new InternalServiceError(entityNotExistsError.toString());
        }
        data.decisionTask.setHistory(new History().setEvents(events));
        data.previousStartedEventId = startedEventId;
        data.attempt++;
    });
}
Also used : DecisionTaskStartedEventAttributes(com.uber.cadence.DecisionTaskStartedEventAttributes) GetWorkflowExecutionHistoryRequest(com.uber.cadence.GetWorkflowExecutionHistoryRequest) InternalServiceError(com.uber.cadence.InternalServiceError) EntityNotExistsError(com.uber.cadence.EntityNotExistsError) HistoryEvent(com.uber.cadence.HistoryEvent) History(com.uber.cadence.History)

Example 4 with History

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

the class WorkflowExecutionUtils method getInstanceCloseEventAsync.

private static CompletableFuture<HistoryEvent> getInstanceCloseEventAsync(IWorkflowService service, String domain, final WorkflowExecution workflowExecution, byte[] pageToken, long timeout, TimeUnit unit) {
    // TODO: Interrupt service long poll call on timeout and on interrupt
    long start = System.currentTimeMillis();
    CompletableFuture<HistoryEvent> result = new CompletableFuture<>();
    GetWorkflowExecutionHistoryRequest request = new GetWorkflowExecutionHistoryRequest();
    request.setDomain(domain);
    request.setExecution(workflowExecution);
    request.setHistoryEventFilterType(HistoryEventFilterType.CLOSE_EVENT);
    request.setNextPageToken(pageToken);
    CompletableFuture<GetWorkflowExecutionHistoryResponse> response = getWorkflowExecutionHistoryAsync(service, request);
    return response.thenComposeAsync((r) -> {
        if (timeout != 0 && System.currentTimeMillis() - start > unit.toMillis(timeout)) {
            throw CheckedExceptionWrapper.wrap(new TimeoutException("WorkflowId=" + workflowExecution.getWorkflowId() + ", runId=" + workflowExecution.getRunId() + ", timeout=" + timeout + ", unit=" + unit));
        }
        History history = r.getHistory();
        if (history == null || history.getEvents().size() == 0) {
            // Empty poll returned
            return getInstanceCloseEventAsync(service, domain, workflowExecution, pageToken, timeout, unit);
        }
        HistoryEvent event = history.getEvents().get(0);
        if (!isWorkflowExecutionCompletedEvent(event)) {
            throw new RuntimeException("Last history event is not completion event: " + event);
        }
        // Workflow called continueAsNew. Start polling the new generation with new runId.
        if (event.getEventType() == EventType.WorkflowExecutionContinuedAsNew) {
            WorkflowExecution nextWorkflowExecution = new WorkflowExecution().setWorkflowId(workflowExecution.getWorkflowId()).setRunId(event.getWorkflowExecutionContinuedAsNewEventAttributes().getNewExecutionRunId());
            return getInstanceCloseEventAsync(service, domain, nextWorkflowExecution, r.getNextPageToken(), timeout, unit);
        }
        return CompletableFuture.completedFuture(event);
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) GetWorkflowExecutionHistoryRequest(com.uber.cadence.GetWorkflowExecutionHistoryRequest) WorkflowExecution(com.uber.cadence.WorkflowExecution) GetWorkflowExecutionHistoryResponse(com.uber.cadence.GetWorkflowExecutionHistoryResponse) HistoryEvent(com.uber.cadence.HistoryEvent) History(com.uber.cadence.History) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with History

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

the class WorkflowExecutionUtils method getInstanceCloseEvent.

/**
 * Returns an instance closing event, potentially waiting for workflow to complete.
 */
public static HistoryEvent getInstanceCloseEvent(IWorkflowService service, String domain, WorkflowExecution workflowExecution, long timeout, TimeUnit unit) throws TimeoutException, EntityNotExistsError {
    byte[] pageToken = null;
    GetWorkflowExecutionHistoryResponse response;
    // TODO: Interrupt service long poll call on timeout and on interrupt
    long start = System.currentTimeMillis();
    HistoryEvent event;
    do {
        GetWorkflowExecutionHistoryRequest r = new GetWorkflowExecutionHistoryRequest();
        r.setDomain(domain);
        r.setExecution(workflowExecution);
        r.setHistoryEventFilterType(HistoryEventFilterType.CLOSE_EVENT);
        r.setNextPageToken(pageToken);
        try {
            response = Retryer.retryWithResult(retryParameters, () -> service.GetWorkflowExecutionHistory(r));
        } catch (EntityNotExistsError e) {
            throw e;
        } catch (TException e) {
            throw CheckedExceptionWrapper.wrap(e);
        }
        if (timeout != 0 && System.currentTimeMillis() - start > unit.toMillis(timeout)) {
            throw new TimeoutException("WorkflowId=" + workflowExecution.getWorkflowId() + ", runId=" + workflowExecution.getRunId() + ", timeout=" + timeout + ", unit=" + unit);
        }
        pageToken = response.getNextPageToken();
        History history = response.getHistory();
        if (history != null && history.getEvents().size() > 0) {
            event = history.getEvents().get(0);
            if (!isWorkflowExecutionCompletedEvent(event)) {
                throw new RuntimeException("Last history event is not completion event: " + event);
            }
            // Workflow called continueAsNew. Start polling the new generation with new runId.
            if (event.getEventType() == EventType.WorkflowExecutionContinuedAsNew) {
                pageToken = null;
                workflowExecution = new WorkflowExecution().setWorkflowId(workflowExecution.getWorkflowId()).setRunId(event.getWorkflowExecutionContinuedAsNewEventAttributes().getNewExecutionRunId());
                continue;
            }
            break;
        }
    } while (true);
    return event;
}
Also used : TException(org.apache.thrift.TException) GetWorkflowExecutionHistoryRequest(com.uber.cadence.GetWorkflowExecutionHistoryRequest) WorkflowExecution(com.uber.cadence.WorkflowExecution) GetWorkflowExecutionHistoryResponse(com.uber.cadence.GetWorkflowExecutionHistoryResponse) EntityNotExistsError(com.uber.cadence.EntityNotExistsError) HistoryEvent(com.uber.cadence.HistoryEvent) History(com.uber.cadence.History) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

History (com.uber.cadence.History)5 HistoryEvent (com.uber.cadence.HistoryEvent)5 GetWorkflowExecutionHistoryRequest (com.uber.cadence.GetWorkflowExecutionHistoryRequest)4 GetWorkflowExecutionHistoryResponse (com.uber.cadence.GetWorkflowExecutionHistoryResponse)3 WorkflowExecution (com.uber.cadence.WorkflowExecution)3 EntityNotExistsError (com.uber.cadence.EntityNotExistsError)2 TimeoutException (java.util.concurrent.TimeoutException)2 DecisionTaskStartedEventAttributes (com.uber.cadence.DecisionTaskStartedEventAttributes)1 InternalServiceError (com.uber.cadence.InternalServiceError)1 WorkflowClient (com.uber.cadence.client.WorkflowClient)1 WorkflowStub (com.uber.cadence.client.WorkflowStub)1 Worker (com.uber.cadence.worker.Worker)1 ArrayList (java.util.ArrayList)1 CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TException (org.apache.thrift.TException)1 Test (org.junit.Test)1