Akka.Net在应用程序重新启动后的内存中持久性调用'Recover'

kxe2p93d  于 2022-11-05  发布在  .NET
关注(0)|答案(1)|浏览(177)

我试着测试演员的毅力,但行为很奇怪。
我的测试演员:

public class PredictionManager : ReceivePersistentActor
{
    public override string PersistenceId => _persistanceId;

    public PredictionManager(string persistenceId)
    {
        _persistanceId = persistenceId;
        Command<AddPredictionRequest>(OnPrediction);
        Recover<SnapshotOffer>(x => OnRecover((PredictionManagerState)x.Snapshot), x => x.Snapshot is PredictionManagerState);
    }
    private void OnPrediction(AddPredictionRequest request)
    {
       /* some code */
        _state.Add(request);
        SaveSnapshot(_state);
    }
    private void OnRecover(PredictionManagerState state)
    {
        foreach(var request in state.RequestMap)
        {
            OnPrediction(request.Value);
        }
    }
}

我的状态保存所有消息,并在管理器actor接收到一些消息后删除它们。当我尝试调试我的测试时,首先调用了Recover函数,之后调用了OnPrediction。我的问题是-这怎么可能?如果数据存储在内存中,为什么它会有SnapshotOffer?我也尝试过从Guid.NewGuid()生成新的percistenceId,但它不起作用。

public void AddPrediction_PassToChild_CreateNewManager_PassToChild()
        {
            var sender = CreateTestProbe(Sys);
            var persistanceId = "AddPrediction_PassToChild_CreateNewManager_PassToChild";
            var props = Props.Create(() => new PredictionManager(Mock.Of<IEventBus>(), persistanceId));
            var predictionManager = ActorOf(props);
            var message = new PredictionManager.AddPredictionRequest(Props.Create(() => new ChildTestActor(sender.Ref)), 
                new StartPrediction<IPredictionParameter>("a", 1, "a", new Param() ));

            //Act
            predictionManager.Tell(message, sender);
            sender.ExpectMsg<string>(x => x == "ok", TimeSpan.FromSeconds(15));

            Sys.Stop(predictionManager);
            predictionManager = Sys.ActorOf(props);

            sender.ExpectMsg<string>(x => x == "ok", TimeSpan.FromSeconds(15));
            Sys.Stop(predictionManager);
        }
r7s23pms

r7s23pms1#

我发现快照的默认存储是LocalStorage而不是MemoryStorage。所以它将快照存储在文件中,这就是为什么在应用程序重启后它有SnapshotOffer。但我仍然不明白为什么Guid.NewGuid()作为persistanceId不工作。

相关问题