use of com.uber.cadence.HistoryEvent in project cadence-client by uber-java.
the class ReplayDecider method decideImpl.
private void decideImpl(Functions.Proc query) throws Throwable {
try {
long lastNonReplayedEventId = historyHelper.getLastNonReplayEventId();
// Buffer events until the next DecisionTaskStarted and then process them
// setting current time to the time of DecisionTaskStarted event
HistoryHelper.EventsIterator eventsIterator = historyHelper.getEvents();
// Looks ahead to the DecisionTaskStarted event to get current time before calling eventLoop.
do {
List<HistoryEvent> decisionCompletionToStartEvents = new ArrayList<>();
while (eventsIterator.hasNext()) {
HistoryEvent event = eventsIterator.next();
EventType eventType = event.getEventType();
if (eventType == EventType.DecisionTaskCompleted) {
decisionsHelper.setWorkflowContextData(event.getDecisionTaskCompletedEventAttributes().getExecutionContext());
} else if (eventType == EventType.DecisionTaskStarted || !eventsIterator.hasNext()) {
// Above check for the end of history is to support queries that get histories
// without DecisionTaskStarted being the last event.
decisionsHelper.handleDecisionTaskStartedEvent();
if (!eventsIterator.isNextDecisionFailed()) {
// Cadence timestamp is in nanoseconds
long replayCurrentTimeMilliseconds = event.getTimestamp() / MILLION;
context.setReplayCurrentTimeMilliseconds(replayCurrentTimeMilliseconds);
break;
}
} else if (eventType != EventType.DecisionTaskScheduled && eventType != EventType.DecisionTaskTimedOut && eventType != EventType.DecisionTaskFailed) {
decisionCompletionToStartEvents.add(event);
}
}
for (HistoryEvent event : decisionCompletionToStartEvents) {
if (event.getEventId() >= lastNonReplayedEventId) {
context.setReplaying(false);
}
EventType eventType = event.getEventType();
processEvent(event, eventType);
}
eventLoop();
mayBeCompleteWorkflow();
} while (eventsIterator.hasNext());
} finally {
if (query != null) {
query.apply();
}
workflow.close();
}
}
use of com.uber.cadence.HistoryEvent 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);
});
}
use of com.uber.cadence.HistoryEvent 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;
}
use of com.uber.cadence.HistoryEvent in project cadence-client by uber-java.
the class WorkflowExecutionUtils method prettyPrintHistory.
public static String prettyPrintHistory(Iterator<HistoryEvent> events, boolean showWorkflowTasks) {
StringBuilder result = new StringBuilder();
result.append("{");
boolean first = true;
long firstTimestamp = 0;
while (events.hasNext()) {
HistoryEvent event = events.next();
if (!showWorkflowTasks && event.getEventType().toString().startsWith("WorkflowTask")) {
continue;
}
if (first) {
first = false;
firstTimestamp = event.getTimestamp();
} else {
result.append(",");
}
result.append("\n ");
result.append(prettyPrintHistoryEvent(event, firstTimestamp));
}
result.append("\n}");
return result.toString();
}
Aggregations