奥尔良提醒执行是交错的吗?

Is Orleans reminder execution interleaved?

如果在同一个grain activation上有两个不同的reminders在同一个点触发,考虑到grain execution context是单线程的,两个reminders会同时执行和交错吗?

另外,提醒执行是否受默认30s超时限制?

使用常规 grain 方法调用来调用提醒:IRemindable 接口是常规 grain 接口。 IRemindable.ReceiveReminder(...) 没有被标记为 [AlwaysInterleave],所以只有当你的 grain class 被标记为 [Reentrant].

时才会交错

简而言之:不,默认情况下不交错提醒调用。

提醒不会覆盖 SiloMessagingOptions.ResponseTimeout 值,因此默认执行时间为 30 秒。

如果您的提醒可能需要很长时间才能执行,您可以遵循以下模式:开始 long-running 在后台任务中工作并确保每当相关提醒触发时,它仍然是 运行(未完成或有故障)。

下面是使用该模式的示例:

public class MyGrain : Grain, IMyGrain
{
    private readonly CancellationTokenSource _deactivating = new CancellationTokenSource();
    private Task _processQueueTask;
    private IGrainReminder _reminder = null;

    public Task ReceiveReminder(string reminderName, TickStatus status)
    {
        // Ensure that the reminder task is running.
        if (_processQueueTask is null || _processQueueTask.IsCompleted)
        {
            if (_processQueueTask?.Exception is Exception exception)
            {
                // Log that an error occurred.
            }

            _processQueueTask = DoLongRunningWork();
            _processQueueTask.Ignore();
        }

        return Task.CompletedTask;
    }

    public override async Task OnActivateAsync()
    {
        if (_reminder != null)
        {
            return;
        }

        _reminder = await RegisterOrUpdateReminder(
            "long-running-work",
            TimeSpan.FromMinutes(1),
            TimeSpan.FromMinutes(1)
        );
    }

    public override async Task OnDeactivateAsync()
    {
        _deactivating.Cancel(throwOnFirstException: false);

        Task processQueueTask = _processQueueTask;
        if (processQueueTask != null)
        {
            // Optionally add some max deactivation timeout here to stop waiting after (eg) 45 seconds

            await processQueueTask;
        }
    }

    public async Task StopAsync()
    {
        if (_reminder == null)
        {
            return;
        }
        await UnregisterReminder(_reminder);
        _reminder = null;
    }

    private async Task DoLongRunningWork()
    {
        // Log that we are starting the long-running work
        while (!_deactivating.IsCancellationRequested)
        {
            try
            {
                // Do long-running work
            }
            catch (Exception exception)
            {
                // Log exception. Potentially wait before retrying loop, since it seems like GetMessageAsync may have failed for us to end up here.
            }
        }
    }
}