-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add "waiting" transition and timeout to partition startup * address PR feedback
- Loading branch information
1 parent
f24f6de
commit fc777fe
Showing
3 changed files
with
90 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace DurableTask.Netherite | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
/// <summary> | ||
/// A utility class for terminating the partition if some task takes too long. | ||
/// Implemented as a disposable, with <see cref="IAsyncDisposable.DisposeAsync"> used to cancel the timeout. | ||
/// </summary> | ||
class PartitionTimeout : IAsyncDisposable | ||
{ | ||
readonly CancellationTokenSource tokenSource; | ||
readonly Task timeoutTask; | ||
|
||
public PartitionTimeout(IPartitionErrorHandler errorHandler, string task, TimeSpan timeout) | ||
{ | ||
this.tokenSource = new CancellationTokenSource(); | ||
this.timeoutTask = Task.Run(async () => | ||
{ | ||
try | ||
{ | ||
await Task.Delay(timeout, this.tokenSource.Token).ConfigureAwait(false); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
// we did not time out | ||
return; | ||
} | ||
|
||
errorHandler.HandleError( | ||
$"{nameof(PartitionTimeout)}", | ||
$"{task} timed out after {timeout}", | ||
e: null, | ||
terminatePartition: true, | ||
reportAsWarning: false); | ||
}); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
// cancel the timeout task (if it has not already completed) | ||
this.tokenSource.Cancel(); | ||
|
||
// wait for the timeouttask to complete here, so we can be sure that the | ||
// decision about the timeout firing or not firing has been made | ||
// before we leave this method | ||
await this.timeoutTask.ConfigureAwait(false); | ||
|
||
// we can dispose the token source now since the timeoutTask is completed | ||
this.tokenSource.Dispose(); | ||
} | ||
} | ||
} |