You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

스냅샷은 액터의 복구속도를 높일수가 있습니다.

SaveSnapShot을 호출하여 내부상태의 스냅샷을 저장할수가 있습니다.

 public class MySnapShotActor : UntypedPersistentActor
{
  private ILoggingAdapter log = Context.GetLogger();
  public override string PersistenceId => "my-stable-persistence-id";
  private const int SnapShotInterval = 5;
  private object state = new object();

  //public override Recovery Recovery => new Recovery(fromSnapshot: new SnapshotSelectionCriteria(maxSequenceNr: 10, maxTimeStamp: DateTime.UtcNow));
  public override Recovery Recovery => new Recovery(fromSnapshot: SnapshotSelectionCriteria.None);

  protected override void OnRecover(object message)
  {
      // handle recovery here
      if (message is SnapshotOffer offeredSnapshot)
      {
          log.Debug("이전 스냅샷 복구");
          state = offeredSnapshot.Snapshot;
      }
      else if (message is RecoveryCompleted)
      {
          log.Debug("스냅샷복구완료:");
      }
      else
      {
          // event
          log.Debug("스냅샷e:" + message);
      }
  }

  protected override void OnCommand(object message)
  {
      if (message is SaveSnapshotSuccess s)
      {
          log.Debug("스냅샷 성공");
      }
      else if (message is SaveSnapshotFailure f)
      {
          log.Debug("스냅샷 실패");
      }
      else if (message is string cmd)
      {
          log.Debug("cmd:" + cmd);
          Persist($"evt-{cmd}", e =>
          {
              UpdateState(e);
              if (LastSequenceNr % SnapShotInterval == 0 && LastSequenceNr != 0)
              {
                  log.Debug("스냅샷 시도");
                  SaveSnapshot(state);
              }
          });
      }
  }

  private void UpdateState(string evt)
  {
      state = evt;
  }
}





  • No labels