Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose wf schedule/execution time #333

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,10 @@ Optional<byte[]> mutableSideEffect(

/** @return replay safe UUID */
UUID randomUUID();

/** @return schedule time of the workflow execution. */
long getScheduleTimeMillis();

/** @return start time of the workflow execution. */
long getExecutionTimeMillis();
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ public UUID randomUUID() {
return workflowClient.randomUUID();
}

@Override
public long getScheduleTimeMillis() {
return workflowClient.getScheduleTimeMillis();
}

@Override
public long getExecutionTimeMillis() {
return workflowClient.getExecutionTimeMillis();
}

@Override
public Random newRandom() {
return workflowClient.newRandom();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

package com.uber.cadence.internal.replay;

import com.uber.cadence.*;
import com.uber.cadence.ChildPolicy;
import com.uber.cadence.PollForDecisionTaskResponse;
import com.uber.cadence.WorkflowExecution;
import com.uber.cadence.WorkflowExecutionStartedEventAttributes;
import com.uber.cadence.WorkflowType;
import java.util.concurrent.TimeUnit;

final class WorkflowContext {

Expand Down Expand Up @@ -131,4 +136,14 @@ void setCurrentRunId(String currentRunId) {
String getCurrentRunId() {
return currentRunId;
}

long getScheduleTimeMillis() {
return TimeUnit.NANOSECONDS.toMillis(
decisionTask.getHistory().getEvents().get(0).getTimestamp());
}

long getExecutionTimeMillis() {
return TimeUnit.NANOSECONDS.toMillis(
decisionTask.getHistory().getEvents().get(1).getTimestamp());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is always true. For example signalWithStart will have signal as a second event.
Also when cron workflow is waiting for exectution all received signals will be put before DecisionTaskScheduled.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,12 @@ void handleExternalWorkflowExecutionSignaled(HistoryEvent event) {
}
}
}

long getScheduleTimeMillis() {
return workflowContext.getScheduleTimeMillis();
}

long getExecutionTimeMillis() {
return workflowContext.getExecutionTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -648,5 +648,15 @@ public boolean getEnableLoggingInReplay() {
public UUID randomUUID() {
return UUID.randomUUID();
}

@Override
public long getScheduleTimeMillis() {
throw new UnsupportedOperationException("not implemented");
}

@Override
public long getExecutionTimeMillis() {
throw new UnsupportedOperationException("not implemented");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ public Duration getExecutionStartToCloseTimeout() {
public ChildPolicy getChildPolicy() {
return context.getChildPolicy();
}

@Override
public long getScheduleTimeMillis() {
return context.getScheduleTimeMillis();
}

@Override
public long getExecutionTimeMillis() {
return context.getExecutionTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,13 @@ private static void addStartChildTask(
.execute(
() -> {
try {
int backoffIntervalSecs =
TestWorkflowMutableStateImpl.getBackoffIntervalSeconds(
data.service::currentTimeMillis, startChild.getCronSchedule());

data.service.startWorkflowExecutionImpl(
startChild,
0,
backoffIntervalSecs,
Optional.of(ctx.getWorkflowMutableState()),
OptionalLong.of(data.initiatedEventId),
Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,21 +888,8 @@ private void startNewCronRun(
WorkflowData data,
byte[] lastCompletionResult)
throws InternalServiceError, BadRequestError {
CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
CronParser parser = new CronParser(cronDefinition);
Cron cron = parser.parse(data.cronSchedule);

Instant i = Instant.ofEpochMilli(store.currentTimeMillis());
ZonedDateTime now = ZonedDateTime.ofInstant(i, ZoneOffset.UTC);

ExecutionTime executionTime = ExecutionTime.forCron(cron);
Optional<Duration> backoff = executionTime.timeToNextExecution(now);
int backoffIntervalSeconds = (int) backoff.get().getSeconds();

if (backoffIntervalSeconds == 0) {
backoff = executionTime.timeToNextExecution(now.plusSeconds(1));
backoffIntervalSeconds = (int) backoff.get().getSeconds() + 1;
}
int backoffIntervalSeconds =
getBackoffIntervalSeconds(store::currentTimeMillis, data.cronSchedule);

ContinueAsNewWorkflowExecutionDecisionAttributes continueAsNewAttr =
new ContinueAsNewWorkflowExecutionDecisionAttributes()
Expand Down Expand Up @@ -932,6 +919,29 @@ private void startNewCronRun(
continuedAsNewEventAttributes.setNewExecutionRunId(runId);
}

static int getBackoffIntervalSeconds(LongSupplier currTimeMillis, String cronSchedule) {
if (Strings.isNullOrEmpty(cronSchedule)) {
return 0;
}

CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
CronParser parser = new CronParser(cronDefinition);
Cron cron = parser.parse(cronSchedule);

Instant i = Instant.ofEpochMilli(currTimeMillis.getAsLong());
ZonedDateTime now = ZonedDateTime.ofInstant(i, ZoneOffset.UTC);

ExecutionTime executionTime = ExecutionTime.forCron(cron);
Optional<Duration> backoff = executionTime.timeToNextExecution(now);
int backoffIntervalSeconds = (int) backoff.get().getSeconds();

if (backoffIntervalSeconds == 0) {
backoff = executionTime.timeToNextExecution(now.plusSeconds(1));
backoffIntervalSeconds = (int) backoff.get().getSeconds() + 1;
}
return backoffIntervalSeconds;
}

private void processCancelWorkflowExecution(
RequestContext ctx, CancelWorkflowExecutionDecisionAttributes d, long decisionTaskCompletedId)
throws InternalServiceError, BadRequestError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,16 @@ public void DeprecateDomain(DeprecateDomainRequest deprecateRequest)
@Override
public StartWorkflowExecutionResponse StartWorkflowExecution(
StartWorkflowExecutionRequest startRequest) throws TException {
int backoffIntervalSecs =
TestWorkflowMutableStateImpl.getBackoffIntervalSeconds(
store::currentTimeMillis, startRequest.getCronSchedule());

return startWorkflowExecutionImpl(
startRequest, 0, Optional.empty(), OptionalLong.empty(), Optional.empty());
startRequest,
backoffIntervalSecs,
Optional.empty(),
OptionalLong.empty(),
Optional.empty());
}

StartWorkflowExecutionResponse startWorkflowExecutionImpl(
Expand Down Expand Up @@ -501,10 +509,7 @@ public void SignalWorkflowExecution(SignalWorkflowExecutionRequest signalRequest

@Override
public StartWorkflowExecutionResponse SignalWithStartWorkflowExecution(
SignalWithStartWorkflowExecutionRequest r)
throws BadRequestError, InternalServiceError, EntityNotExistsError, ServiceBusyError,
DomainNotActiveError, LimitExceededError, WorkflowExecutionAlreadyStartedError,
TException {
SignalWithStartWorkflowExecutionRequest r) throws TException {
ExecutionId executionId = new ExecutionId(r.getDomain(), r.getWorkflowId(), null);
TestWorkflowMutableState mutableState = getMutableState(executionId, false);
SignalWorkflowExecutionRequest signalRequest =
Expand Down Expand Up @@ -535,8 +540,16 @@ public StartWorkflowExecutionResponse SignalWithStartWorkflowExecution(
.setCronSchedule(r.getCronSchedule())
.setRequestId(r.getRequestId())
.setIdentity(r.getIdentity());

int backoffIntervalSecs =
TestWorkflowMutableStateImpl.getBackoffIntervalSeconds(
store::currentTimeMillis, startRequest.getCronSchedule());
return startWorkflowExecutionImpl(
startRequest, 0, Optional.empty(), OptionalLong.empty(), Optional.of(signalRequest));
startRequest,
backoffIntervalSecs,
Optional.empty(),
OptionalLong.empty(),
Optional.of(signalRequest));
}

// TODO: https://github.com/uber/cadence-java-client/issues/359
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/uber/cadence/workflow/WorkflowInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ public interface WorkflowInfo {
Duration getExecutionStartToCloseTimeout();

ChildPolicy getChildPolicy();

long getScheduleTimeMillis();

long getExecutionTimeMillis();
}
12 changes: 8 additions & 4 deletions src/test/java/com/uber/cadence/workflow/WorkflowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3249,20 +3249,24 @@ public String execute(String testName) {

lastCompletionResult = Workflow.getLastCompletionResult(String.class);

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss.SSS");

Date scheduleTime = new Date(Workflow.getWorkflowInfo().getScheduleTimeMillis());
log.info("TestWorkflowWithCronScheduleImpl scheduled at " + sdf.format(scheduleTime));

Date now = new Date(Workflow.currentTimeMillis());
log.info("TestWorkflowWithCronScheduleImpl run at " + sdf.format(now));

AtomicInteger count = retryCount.get(testName);
if (count == null) {
count = new AtomicInteger();
retryCount.put(testName, count);
}
int c = count.incrementAndGet();

if (c == 3) {
throw new RuntimeException("simulated error");
}

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss.SSS");
Date now = new Date(Workflow.currentTimeMillis());
log.debug("TestWorkflowWithCronScheduleImpl run at " + sdf.format(now));
return "run " + c;
}
}
Expand Down