Skip to content

Commit

Permalink
Fix TimerOrleansTest_Migration rounding error
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenBond committed Apr 28, 2024
1 parent 3246cec commit fb033b5
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 425 deletions.
2 changes: 1 addition & 1 deletion src/Orleans.Core.Abstractions/Core/Grain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected Grain(IGrainContext grainContext, IGrainRuntime? grainRuntime = null)
/// <param name="period">Period of subsequent timer ticks.</param>
/// <returns>Handle for this Timer.</returns>
/// <seealso cref="IDisposable"/>
protected IDisposable RegisterTimer(Func<object, Task> asyncCallback, object state, TimeSpan dueTime, TimeSpan period)
protected IGrainTimer RegisterTimer(Func<object?, Task> asyncCallback, object? state, TimeSpan dueTime, TimeSpan period)
{
if (asyncCallback == null)
throw new ArgumentNullException(nameof(asyncCallback));
Expand Down
41 changes: 18 additions & 23 deletions src/Orleans.Core.Abstractions/Runtime/IGrainTimer.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
using System;
using System.Threading.Tasks;
using System.Threading;

namespace Orleans.Runtime
{
/// <summary>
/// Represents a grain timer and its functionality.
/// </summary>
internal interface IGrainTimer : IDisposable
{
/// <summary>
/// Starts the timer.
/// </summary>
void Start();

/// <summary>
/// Stops the timer.
/// </summary>
void Stop();
namespace Orleans.Runtime;

/// <summary>
/// Gets the currently executing grain timer task.
/// </summary>
/// <returns>The currently executing grain timer task.</returns>
Task GetCurrentlyExecutingTickTask();
}
/// <summary>
/// Represents a timer belonging to a grain.
/// </summary>
public interface IGrainTimer : IDisposable, IAsyncDisposable
{
/// <summary>Changes the start time and the interval between method invocations for a timer, using <see cref="TimeSpan"/> values to measure time intervals.</summary>
/// <param name="dueTime">
/// A <see cref="TimeSpan"/> representing the amount of time to delay before invoking the callback method specified when the <see cref="IGrainTimer"/> was constructed.
/// Specify <see cref="Timeout.InfiniteTimeSpan"/> to prevent the timer from restarting. Specify <see cref="TimeSpan.Zero"/> to restart the timer immediately.
/// </param>
/// <param name="period">
/// The time interval between invocations of the callback method specified when the timer was constructed.
/// Specify <see cref="Timeout.InfiniteTimeSpan"/> to disable periodic signaling.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">The <paramref name="dueTime"/> or <paramref name="period"/> parameter, in milliseconds, is less than -1 or greater than 4294967294.</exception>
void Change(TimeSpan dueTime, TimeSpan period);
}
48 changes: 24 additions & 24 deletions src/Orleans.Core.Abstractions/Timers/ITimerRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
#nullable enable
using System;
using System.Threading.Tasks;
using Orleans.Runtime;

namespace Orleans.Timers
namespace Orleans.Timers;

/// <summary>
/// Functionality for managing grain timers.
/// </summary>
public interface ITimerRegistry
{
/// <summary>
/// Functionality for managing grain timers.
/// Creates a grain timer.
/// </summary>
public interface ITimerRegistry
{
/// <summary>
/// Creates a grain timer.
/// </summary>
/// <param name="grainContext">The grain which the timer is associated with.</param>
/// <param name="asyncCallback">The timer callback, which will fire whenever the timer becomes due.</param>
/// <param name="state">The state object passed to the callback.</param>
/// <param name="dueTime">
/// The amount of time to delay before the <paramref name="asyncCallback"/> is invoked.
/// Specify <see cref="System.Threading.Timeout.InfiniteTimeSpan"/> to prevent the timer from starting.
/// Specify <see cref="TimeSpan.Zero"/> to invoke the callback promptly.
/// </param>
/// <param name="period">
/// The time interval between invocations of <paramref name="asyncCallback"/>.
/// Specify <see cref="System.Threading.Timeout.InfiniteTimeSpan"/> to disable periodic signaling.
/// </param>
/// <returns>
/// An <see cref="IDisposable"/> object which will cancel the timer upon disposal.
/// </returns>
IDisposable RegisterTimer(IGrainContext grainContext, Func<object, Task> asyncCallback, object state, TimeSpan dueTime, TimeSpan period);
}
/// <param name="grainContext">The grain which the timer is associated with.</param>
/// <param name="callback">The timer callback, which will fire whenever the timer becomes due.</param>
/// <param name="state">The state object passed to the callback.</param>
/// <param name="dueTime">
/// The amount of time to delay before the <paramref name="callback"/> is invoked.
/// Specify <see cref="System.Threading.Timeout.InfiniteTimeSpan"/> to prevent the timer from starting.
/// Specify <see cref="TimeSpan.Zero"/> to invoke the callback promptly.
/// </param>
/// <param name="period">
/// The time interval between invocations of <paramref name="callback"/>.
/// Specify <see cref="System.Threading.Timeout.InfiniteTimeSpan"/> to disable periodic signaling.
/// </param>
/// <returns>
/// An <see cref="IGrainTimer"/> instance which represents the timer.
/// </returns>
IGrainTimer RegisterTimer(IGrainContext grainContext, Func<object?, Task> callback, object? state, TimeSpan dueTime, TimeSpan period);
}
5 changes: 5 additions & 0 deletions src/Orleans.Core/Runtime/IRuntimeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace Orleans.Runtime
/// </summary>
internal interface IRuntimeClient
{
/// <summary>
/// Gets the time provider used by the system.
/// </summary>
TimeProvider TimeProvider { get; }

/// <summary>
/// Grain Factory to get and cast grain references.
/// </summary>
Expand Down
216 changes: 0 additions & 216 deletions src/Orleans.Core/Timers/AsyncTaskSafeTimer.cs

This file was deleted.

10 changes: 6 additions & 4 deletions src/Orleans.Runtime/Catalog/ActivationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ private void StopAllTimers()

foreach (var timer in Timers)
{
timer.Stop();
timer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
}
}
}
Expand All @@ -595,9 +595,11 @@ private Task WaitForAllTimersToFinish(CancellationToken cancellationToken)
var timerCopy = Timers.ToList(); // need to copy since OnTimerDisposed will change the timers set.
foreach (var timer in timerCopy)
{
// first call dispose, then wait to finish.
Utils.SafeExecute(timer.Dispose, _shared.Logger, "timer.Dispose has thrown");
tasks.Add(timer.GetCurrentlyExecutingTickTask());
var task = timer.DisposeAsync();
if (!task.IsCompletedSuccessfully)
{
tasks.Add(task.AsTask());
}
}

return Task.WhenAll(tasks).WithCancellation(cancellationToken);
Expand Down

0 comments on commit fb033b5

Please sign in to comment.