@@ -353,6 +353,13 @@ private Task ReadAndUpdateReminders()
353353 return task ;
354354 }
355355
356+ internal Task TestOnlyRefresh ( )
357+ {
358+ var refreshTask = new Task < Task > ( ReadAndUpdateReminders ) ;
359+ Scheduler . QueueTask ( refreshTask ) ;
360+ return refreshTask . Unwrap ( ) ;
361+ }
362+
356363 private void RemoveOutOfRangeReminders ( List < Task > removedReminderTasks )
357364 {
358365 CheckRuntimeContext ( ) ;
@@ -593,9 +600,15 @@ private void ReconcileTableEntry(ReminderEntry entry, long tableSequence, DateTi
593600 return ;
594601 }
595602
596- // A due-now tombstone represents an occurrence which this owner already fired. Do not resurrect it
597- // from a refresh captured at the same timestamp, but allow due-now entries with no local history.
598- if ( ! isWithinLoadingWindow || ( state is LocalReminderState . Tombstone && nextTick <= now ) )
603+ // Retain the marker for an unchanged occurrence which this owner fired at the same timestamp.
604+ if ( state is LocalReminderState . Tombstone
605+ && localReminder . ShouldRetainFiredOccurrenceTombstone ( entry , nextTick ) )
606+ {
607+ localReminder . LocalSequenceNumber = tableSequence ;
608+ return ;
609+ }
610+
611+ if ( ! isWithinLoadingWindow )
599612 {
600613 LogTraceRemovingReminder ( localReminder ) ;
601614 stopTasks . Add (
@@ -875,6 +888,13 @@ internal static bool IsReminderWithinLoadingWindow(ReminderEntry entry, DateTime
875888 return CalculateNextTickTime ( entry , now ) <= now . AddClamped ( loadingWindow ) ;
876889 }
877890
891+ internal static bool ShouldRetainFiredOccurrenceTombstone (
892+ ReminderEntry localEntry ,
893+ ReminderEntry tableEntry ,
894+ DateTime ? lastFiredTickTime ,
895+ DateTime nextTick )
896+ => lastFiredTickTime == nextTick && StringComparer . Ordinal . Equals ( localEntry . ETag , tableEntry . ETag ) ;
897+
878898 internal static DateTime CalculateFollowingTickTime ( ReminderEntry entry , DateTime previousTickTime , DateTime now )
879899 {
880900 Debug . Assert ( previousTickTime . Kind == DateTimeKind . Utc ) ;
@@ -883,7 +903,7 @@ internal static DateTime CalculateFollowingTickTime(ReminderEntry entry, DateTim
883903 return nextTick > previousTickTime ? nextTick : previousTickTime . AddClamped ( entry . Period ) ;
884904 }
885905
886- private bool TryEvictLocalReminder ( LocalReminderData reminder , long scheduleVersion )
906+ private bool TryEvictLocalReminder ( LocalReminderData reminder , long scheduleVersion , DateTime ? lastFiredTickTime )
887907 {
888908 CheckRuntimeContext ( ) ;
889909
@@ -897,7 +917,7 @@ private bool TryEvictLocalReminder(LocalReminderData reminder, long scheduleVers
897917 }
898918
899919 var sequence = GetLocalMutationSequence ( ) ;
900- if ( ! reminder . TryStopOutsideLoadingWindow ( scheduleVersion , sequence ) )
920+ if ( ! reminder . TryStopOutsideLoadingWindow ( scheduleVersion , sequence , lastFiredTickTime ) )
901921 {
902922 return false ;
903923 }
@@ -924,6 +944,7 @@ private sealed class LocalReminderData
924944#endif
925945 private readonly ReminderIdentity _identity ;
926946 private ReminderEntry ? _entry ;
947+ private DateTime ? _lastFiredTickTime ;
927948 private CancellationTokenSource _scheduleChangedCancellation = new ( ) ;
928949 private long _scheduleVersion ;
929950
@@ -1025,6 +1046,19 @@ public LocalReminderState State
10251046 }
10261047 }
10271048
1049+ public bool ShouldRetainFiredOccurrenceTombstone ( ReminderEntry entry , DateTime nextTick )
1050+ {
1051+ lock ( _lock )
1052+ {
1053+ return _entry is { } localEntry
1054+ && LocalReminderService . ShouldRetainFiredOccurrenceTombstone (
1055+ localEntry ,
1056+ entry ,
1057+ _lastFiredTickTime ,
1058+ nextTick ) ;
1059+ }
1060+ }
1061+
10281062 public bool TryStart ( )
10291063 {
10301064 GrainId grainId ;
@@ -1102,7 +1136,10 @@ public Task StopAsync(ReminderEvents.LocalReminderStopReason reason, long? local
11021136 return runTask ?? Task . CompletedTask ;
11031137 }
11041138
1105- public bool TryStopOutsideLoadingWindow ( long scheduleVersion , long localSequenceNumber )
1139+ public bool TryStopOutsideLoadingWindow (
1140+ long scheduleVersion ,
1141+ long localSequenceNumber ,
1142+ DateTime ? lastFiredTickTime )
11061143 {
11071144 ReminderEntry entry ;
11081145 CancellationTokenSource scheduleChangedCancellation ;
@@ -1114,6 +1151,7 @@ public bool TryStopOutsideLoadingWindow(long scheduleVersion, long localSequence
11141151 }
11151152
11161153 _localSequenceNumber = localSequenceNumber ;
1154+ _lastFiredTickTime = lastFiredTickTime ;
11171155 _stopReason = ( int ) ReminderEvents . LocalReminderStopReason . OutsideLoadingWindow ;
11181156 entry = _entry ?? throw new InvalidOperationException ( $ "Reminder { _identity } is a tombstone.") ;
11191157 scheduleChangedCancellation = _scheduleChangedCancellation ;
@@ -1238,7 +1276,7 @@ private async Task RunAsync(GrainId grainId, string reminderName)
12381276 // a future table refresh will load it again as it approaches the window.
12391277 if ( tickTime > now . AddClamped ( _shared . reminderOptions . ReminderLoadingWindow ) )
12401278 {
1241- if ( _shared . TryEvictLocalReminder ( this , scheduleVersion ) )
1279+ if ( _shared . TryEvictLocalReminder ( this , scheduleVersion , previousTickTime ) )
12421280 {
12431281 return null ;
12441282 }
0 commit comments