Search in sources :

Example 1 with GetWorkflowExecutionHistoryRequest

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

the class WorkflowExecutionUtils method getHistoryPage.

public static GetWorkflowExecutionHistoryResponse getHistoryPage(byte[] nextPageToken, IWorkflowService service, String domain, WorkflowExecution workflowExecution) {
    GetWorkflowExecutionHistoryRequest getHistoryRequest = new GetWorkflowExecutionHistoryRequest();
    getHistoryRequest.setDomain(domain);
    getHistoryRequest.setExecution(workflowExecution);
    getHistoryRequest.setNextPageToken(nextPageToken);
    GetWorkflowExecutionHistoryResponse history;
    try {
        history = service.GetWorkflowExecutionHistory(getHistoryRequest);
    } catch (TException e) {
        throw new Error(e);
    }
    if (history == null) {
        throw new IllegalArgumentException("unknown workflow execution: " + workflowExecution);
    }
    return history;
}
Also used : TException(org.apache.thrift.TException) GetWorkflowExecutionHistoryRequest(com.uber.cadence.GetWorkflowExecutionHistoryRequest) BadRequestError(com.uber.cadence.BadRequestError) EntityNotExistsError(com.uber.cadence.EntityNotExistsError) GetWorkflowExecutionHistoryResponse(com.uber.cadence.GetWorkflowExecutionHistoryResponse)

Example 2 with GetWorkflowExecutionHistoryRequest

use of com.uber.cadence.GetWorkflowExecutionHistoryRequest 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 3 with GetWorkflowExecutionHistoryRequest

use of com.uber.cadence.GetWorkflowExecutionHistoryRequest 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 GetWorkflowExecutionHistoryRequest

use of com.uber.cadence.GetWorkflowExecutionHistoryRequest 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 GetWorkflowExecutionHistoryRequest

use of com.uber.cadence.GetWorkflowExecutionHistoryRequest 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

GetWorkflowExecutionHistoryRequest (com.uber.cadence.GetWorkflowExecutionHistoryRequest)5 History (com.uber.cadence.History)4 HistoryEvent (com.uber.cadence.HistoryEvent)4 EntityNotExistsError (com.uber.cadence.EntityNotExistsError)3 GetWorkflowExecutionHistoryResponse (com.uber.cadence.GetWorkflowExecutionHistoryResponse)3 WorkflowExecution (com.uber.cadence.WorkflowExecution)3 TimeoutException (java.util.concurrent.TimeoutException)2 TException (org.apache.thrift.TException)2 BadRequestError (com.uber.cadence.BadRequestError)1 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 CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Test (org.junit.Test)1