protected async ValueTask Tell(IFullyEvent <PrimaryKey> @event) { if (Logger.IsEnabled(LogLevel.Trace)) { Logger.LogTrace(LogEventIds.FollowEventProcessing, "Start event handling, grain Id = {0} and state version = {1},event type = {2} ,event = {3}", GrainId.ToString(), Snapshot.Version, @event.GetType().FullName, Serializer.SerializeToString(@event)); } try { if (@event.Base.Version == Snapshot.Version + 1) { var onEventDeliveredTask = OnEventDelivered(@event); if (!onEventDeliveredTask.IsCompletedSuccessfully) { await onEventDeliveredTask; } Snapshot.FullUpdateVersion(@event.Base, GrainType);//更新处理完成的Version } else if (@event.Base.Version > Snapshot.Version) { var eventList = await EventStorage.GetList(GrainId, Snapshot.StartTimestamp, Snapshot.Version + 1, @event.Base.Version - 1); foreach (var evt in eventList) { var onEventDeliveredTask = OnEventDelivered(evt); if (!onEventDeliveredTask.IsCompletedSuccessfully) { await onEventDeliveredTask; } Snapshot.FullUpdateVersion(evt.Base, GrainType);//更新处理完成的Version } } if (@event.Base.Version == Snapshot.Version + 1) { var onEventDeliveredTask = OnEventDelivered(@event); if (!onEventDeliveredTask.IsCompletedSuccessfully) { await onEventDeliveredTask; } Snapshot.FullUpdateVersion(@event.Base, GrainType);//更新处理完成的Version } if (@event.Base.Version > Snapshot.Version) { throw new EventVersionNotMatchStateException(GrainId.ToString(), GrainType, @event.Base.Version, Snapshot.Version); } await SaveSnapshotAsync(); if (Logger.IsEnabled(LogLevel.Trace)) { Logger.LogTrace(LogEventIds.FollowEventProcessing, "Event Handling Completion, grain Id ={0} and state version = {1},event type = {2}", GrainId.ToString(), Snapshot.Version, @event.GetType().FullName); } } catch (Exception ex) { if (Logger.IsEnabled(LogLevel.Critical)) { Logger.LogCritical(LogEventIds.FollowEventProcessing, ex, "FollowGrain Event handling failed with Id = {0},event = {1}", GrainId.ToString(), Serializer.SerializeToString(@event)); } throw; } }
protected virtual ValueTask OnEventDelivered(IFullyEvent <PrimaryKey> @event) { try { SnapshotHandler.Apply(Snapshot, @event); } catch (Exception ex) { Logger.LogCritical(ex, "Delivered failed: {0}->{1}->{2}", GrainType.FullName, GrainId.ToString(), Serializer.Serialize(@event, @event.GetType())); } return(Consts.ValueTaskDone); }