forked from microsoft/durabletask-java
-
Notifications
You must be signed in to change notification settings - Fork 9
[WIP] Named timers in Durable Task #43
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
Draft
salaboy
wants to merge
2
commits into
dapr:main
Choose a base branch
from
salaboy:1532-named-timers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -492,7 +492,7 @@ public <V> Task<V> waitForExternalEvent(String name, Duration timeout, Class<V> | |
// If a non-infinite timeout is specified, schedule an internal durable timer. | ||
// If the timer expires and the external event task hasn't yet completed, we'll cancel the task. | ||
if (hasTimeout) { | ||
this.createTimer(timeout).future.thenRun(() -> { | ||
this.createTimer(name, timeout).future.thenRun(() -> { | ||
if (!eventTask.isDone()) { | ||
// Book-keeping - remove the task record for the canceled task | ||
eventQueue.removeIf(t -> t.task == eventTask); | ||
|
@@ -632,7 +632,26 @@ public Task<Void> createTimer(Duration duration) { | |
Helpers.throwIfArgumentNull(duration, "duration"); | ||
|
||
Instant finalFireAt = this.currentInstant.plus(duration); | ||
return createTimer(finalFireAt); | ||
return createTimer("", finalFireAt); | ||
} | ||
|
||
public Task<Void> createTimer(String name, Duration duration) { | ||
Helpers.throwIfOrchestratorComplete(this.isComplete); | ||
Helpers.throwIfArgumentNull(duration, "duration"); | ||
Helpers.throwIfArgumentNull(name, "name"); | ||
|
||
Instant finalFireAt = this.currentInstant.plus(duration); | ||
return createTimer(name, finalFireAt); | ||
} | ||
|
||
@Override | ||
public Task<Void> createTimer(String name, ZonedDateTime zonedDateTime) { | ||
Helpers.throwIfOrchestratorComplete(this.isComplete); | ||
Helpers.throwIfArgumentNull(zonedDateTime, "zonedDateTime"); | ||
Helpers.throwIfArgumentNull(name, "name"); | ||
|
||
Instant finalFireAt = zonedDateTime.toInstant(); | ||
return createTimer(name, finalFireAt); | ||
} | ||
|
||
@Override | ||
|
@@ -641,18 +660,19 @@ public Task<Void> createTimer(ZonedDateTime zonedDateTime) { | |
Helpers.throwIfArgumentNull(zonedDateTime, "zonedDateTime"); | ||
|
||
Instant finalFireAt = zonedDateTime.toInstant(); | ||
return createTimer(finalFireAt); | ||
return createTimer("", finalFireAt); | ||
} | ||
|
||
private Task<Void> createTimer(Instant finalFireAt) { | ||
return new TimerTask(finalFireAt); | ||
private Task<Void> createTimer(String name, Instant finalFireAt) { | ||
return new TimerTask(name, finalFireAt); | ||
} | ||
|
||
private CompletableTask<Void> createInstantTimer(int id, Instant fireAt) { | ||
private CompletableTask<Void> createInstantTimer(String name, int id, Instant fireAt) { | ||
Timestamp ts = DataConverter.getTimestampFromInstant(fireAt); | ||
this.pendingActions.put(id, OrchestratorAction.newBuilder() | ||
.setId(id) | ||
.setCreateTimer(CreateTimerAction.newBuilder().setFireAt(ts)) | ||
.setCreateTimer(CreateTimerAction.newBuilder() | ||
.setName(name + "-" + id).setFireAt(ts)) | ||
.build()); | ||
|
||
if (!this.isReplaying) { | ||
|
@@ -1022,10 +1042,10 @@ private class TimerTask extends CompletableTask<Void> { | |
private Instant finalFireAt; | ||
CompletableTask<Void> task; | ||
|
||
public TimerTask(Instant finalFireAt) { | ||
public TimerTask(String name, Instant finalFireAt) { | ||
super(); | ||
CompletableTask<Void> firstTimer = createTimerTask(finalFireAt); | ||
CompletableFuture<Void> timerChain = createTimerChain(finalFireAt, firstTimer.future); | ||
CompletableTask<Void> firstTimer = createTimerTask(name, finalFireAt); | ||
CompletableFuture<Void> timerChain = createTimerChain(name, finalFireAt, firstTimer.future); | ||
this.task = new CompletableTask<>(timerChain); | ||
this.finalFireAt = finalFireAt; | ||
} | ||
|
@@ -1035,26 +1055,26 @@ public TimerTask(Instant finalFireAt) { | |
// currentFuture completes, we check if we have not yet reached finalFireAt. If that is the case, we create a new sub-timer | ||
// task and make a recursive call on that new sub-timer task so that once it completes, another sub-timer task is created | ||
// if necessary. Otherwise, we return and no more sub-timers are created. | ||
private CompletableFuture<Void> createTimerChain(Instant finalFireAt, CompletableFuture<Void> currentFuture) { | ||
private CompletableFuture<Void> createTimerChain(String name, Instant finalFireAt, CompletableFuture<Void> currentFuture) { | ||
return currentFuture.thenRun(() -> { | ||
Instant currentInstsanceMinusNanos = currentInstant.minusNanos(currentInstant.getNano()); | ||
Instant finalFireAtMinusNanos = finalFireAt.minusNanos(finalFireAt.getNano()); | ||
if (currentInstsanceMinusNanos.compareTo(finalFireAtMinusNanos) >= 0) { | ||
return; | ||
} | ||
Task<Void> nextTimer = createTimerTask(finalFireAt); | ||
createTimerChain(finalFireAt, nextTimer.future); | ||
Task<Void> nextTimer = createTimerTask(name + "-next", finalFireAt); | ||
createTimerChain(name, finalFireAt, nextTimer.future); | ||
}); | ||
} | ||
|
||
private CompletableTask<Void> createTimerTask(Instant finalFireAt) { | ||
private CompletableTask<Void> createTimerTask(String name, Instant finalFireAt) { | ||
CompletableTask<Void> nextTimer; | ||
Duration remainingTime = Duration.between(currentInstant, finalFireAt); | ||
if (remainingTime.compareTo(maximumTimerInterval) > 0) { | ||
Instant nextFireAt = currentInstant.plus(maximumTimerInterval); | ||
nextTimer = createInstantTimer(sequenceNumber++, nextFireAt); | ||
nextTimer = createInstantTimer(name, sequenceNumber++, nextFireAt); | ||
} else { | ||
nextTimer = createInstantTimer(sequenceNumber++, finalFireAt); | ||
nextTimer = createInstantTimer(name, sequenceNumber++, finalFireAt); | ||
} | ||
nextTimer.setParentTask(this); | ||
return nextTimer; | ||
|
@@ -1185,7 +1205,7 @@ public void tryRetry(TaskFailedException ex) { | |
Duration delay = this.getNextDelay(); | ||
if (!delay.isZero() && !delay.isNegative()) { | ||
// Use a durable timer to create the delay between retries | ||
this.context.createTimer(delay).await(); | ||
this.context.createTimer(getName() + "-retry",delay).await(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in this case I think this is good, its a made up name, but that it shows that it comes from the activity but is a retry |
||
} | ||
|
||
this.totalRetryTime = Duration.between(this.startTime, this.context.getCurrentInstant()); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you should add the id here, the name is the name, and whatever is provided should be respected