如何在主副本关闭之前将数据写入可靠集合

How to write data to reliable collections just before the primary replica closes

我有一个场景,当副本即将失去其主要状态时,我想将内存中的数据转储到可靠的字典中。

正确的方法是通过观察取消令牌在 RunAsync 方法上执行此操作吗?例如:

    protected override async Task RunAsync(CancellationToken cancellationToken)
    {
        while (true)
        {
            // exit only after data is dumped to reliable dictionary
            if (cancellationToken.IsCancellationRequested)
            {
                await DumpDataToDictionaryAsync(data);
                cancellationToken.ThrowIfCancellationRequested();
            }
        ...

如果应该保留信息,您应该在信息发生变化时这样做。如果服务崩溃,您将无法保存任何内容。即使在从主副本切换到辅助副本的情况下,也无法保存可靠的集合。 documentation 关于生命周期状态:

In Service Fabric, when a Primary is demoted, one of the first things that happens is that write access to the underlying state is revoked.

因为无法判断副本何时会失去其主要状态,所以无法将您的数据保存在可靠的集合中。