Skip to content

Commit

Permalink
Fix SnapshotMetadata.Timestamp bug (#7354)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkatufus authored Oct 3, 2024
1 parent e5aebed commit d98d37f
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ protected virtual SelectedSnapshot ReadSnapshot(DbDataReader reader)
{
var persistenceId = reader.GetString(0);
var sequenceNr = reader.GetInt64(1);
var timestamp = reader.GetDateTime(2);
var timestamp = DateTime.SpecifyKind(reader.GetDateTime(2), DateTimeKind.Utc);

var metadata = new SnapshotMetadata(persistenceId, sequenceNr, timestamp);
var snapshot = GetSnapshot(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected override SelectedSnapshot ReadSnapshot(DbDataReader reader)
{
var persistenceId = reader.GetString(0);
var sequenceNr = reader.GetInt64(1);
var timestamp = new DateTime(reader.GetInt64(2));
var timestamp = DateTime.SpecifyKind(new DateTime(reader.GetInt64(2)), DateTimeKind.Utc);

var metadata = new SnapshotMetadata(persistenceId, sequenceNr, timestamp);
var snapshot = GetSnapshot(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected override void AfterAll()
public void LocalSnapshotStore_can_snapshot_actors_with_PersistenceId_containing_invalid_path_characters()
{
var pid = @"p\/:*?-1";
SnapshotStore.Tell(new SaveSnapshot(new SnapshotMetadata(pid, 1, Sys.Scheduler.Now.DateTime), "sample data"), TestActor);
SnapshotStore.Tell(new SaveSnapshot(new SnapshotMetadata(pid, 1, Sys.Scheduler.Now.UtcDateTime), "sample data"), TestActor);
ExpectMsg<SaveSnapshotSuccess>();

SnapshotStore.Tell(new LoadSnapshot(pid, SnapshotSelectionCriteria.Latest, long.MaxValue), TestActor);
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Persistence.TCK/Query/PersistenceIdsSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ protected IActorRef WriteSnapshot(string persistenceId, int n)
ExpectMsg($"{persistenceId}-{i}-done");
}

var metadata = new SnapshotMetadata(persistenceId, n + 10, Sys.Scheduler.Now.DateTime);
var metadata = new SnapshotMetadata(persistenceId, n + 10, Sys.Scheduler.Now.UtcDateTime);
SnapshotStore.Tell(new SaveSnapshot(metadata, $"s-{n}"), _senderProbe.Ref);
_senderProbe.ExpectMsg<SaveSnapshotSuccess>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public virtual void SnapshotStore_should_serialize_Payload()

var snapshot = new Test.MySnapshot("a");

var metadata = new SnapshotMetadata(Pid, 1, Sys.Scheduler.Now.DateTime);
var metadata = new SnapshotMetadata(Pid, 1, Sys.Scheduler.Now.UtcDateTime);
SnapshotStore.Tell(new SaveSnapshot(metadata, snapshot), probe.Ref);
probe.ExpectMsg<SaveSnapshotSuccess>();

Expand All @@ -85,7 +85,7 @@ public virtual void SnapshotStore_should_serialize_Payload_with_string_manifest(

var snapshot = new Test.MySnapshot2("a");

var metadata = new SnapshotMetadata(Pid, 1, Sys.Scheduler.Now.DateTime);
var metadata = new SnapshotMetadata(Pid, 1, Sys.Scheduler.Now.UtcDateTime);
SnapshotStore.Tell(new SaveSnapshot(metadata, snapshot), probe.Ref);
probe.ExpectMsg<SaveSnapshotSuccess>();

Expand All @@ -107,7 +107,7 @@ public virtual void SnapshotStore_should_serialize_AtLeastOnceDeliverySnapshot()
};
var atLeastOnceDeliverySnapshot = new AtLeastOnceDeliverySnapshot(17, unconfirmed);

var metadata = new SnapshotMetadata(Pid, 2, Sys.Scheduler.Now.DateTime);
var metadata = new SnapshotMetadata(Pid, 2, Sys.Scheduler.Now.UtcDateTime);
SnapshotStore.Tell(new SaveSnapshot(metadata, atLeastOnceDeliverySnapshot), probe.Ref);
probe.ExpectMsg<SaveSnapshotSuccess>();

Expand All @@ -123,7 +123,7 @@ public virtual void SnapshotStore_should_serialize_AtLeastOnceDeliverySnapshot_w
var unconfirmed = Array.Empty<UnconfirmedDelivery>();
var atLeastOnceDeliverySnapshot = new AtLeastOnceDeliverySnapshot(13, unconfirmed);

var metadata = new SnapshotMetadata(Pid, 2, Sys.Scheduler.Now.DateTime);
var metadata = new SnapshotMetadata(Pid, 2, Sys.Scheduler.Now.UtcDateTime);
SnapshotStore.Tell(new SaveSnapshot(metadata, atLeastOnceDeliverySnapshot), probe.Ref);
probe.ExpectMsg<SaveSnapshotSuccess>();

Expand All @@ -138,7 +138,7 @@ public virtual void SnapshotStore_should_serialize_PersistentFSMSnapshot()

var persistentFSMSnapshot = new PersistentFSM.PersistentFSMSnapshot<string>("mystate", "mydata", TimeSpan.FromDays(4));

var metadata = new SnapshotMetadata(Pid, 2, Sys.Scheduler.Now.DateTime);
var metadata = new SnapshotMetadata(Pid, 2, Sys.Scheduler.Now.UtcDateTime);
SnapshotStore.Tell(new SaveSnapshot(metadata, persistentFSMSnapshot), probe.Ref);
probe.ExpectMsg<SaveSnapshotSuccess>();

Expand Down
8 changes: 4 additions & 4 deletions src/core/Akka.Persistence.TCK/Snapshot/SnapshotStoreSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private IEnumerable<SnapshotMetadata> WriteSnapshots()
{
for (int i = 1; i <= 5; i++)
{
var metadata = new SnapshotMetadata(Pid, i + 10, Sys.Scheduler.Now.DateTime);
var metadata = new SnapshotMetadata(Pid, i + 10, Sys.Scheduler.Now.UtcDateTime);
SnapshotStore.Tell(new SaveSnapshot(metadata, $"s-{i}"), _senderProbe.Ref);
yield return _senderProbe.ExpectMsg<SaveSnapshotSuccess>().Metadata;
}
Expand Down Expand Up @@ -209,7 +209,7 @@ public virtual void SnapshotStore_should_delete_a_single_snapshot_identified_by_
{
var md = Metadata[2];
// In previous incarnation, timestamp argument defaults to DateTime.MinValue
md = new SnapshotMetadata(md.PersistenceId, md.SequenceNr, DateTime.MinValue);
md = new SnapshotMetadata(md.PersistenceId, md.SequenceNr, DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc));
var command = new DeleteSnapshot(md);
var sub = CreateTestProbe();

Expand Down Expand Up @@ -312,7 +312,7 @@ public virtual void SnapshotStore_should_save_and_overwrite_snapshot_with_same_s
[Fact]
public virtual void SnapshotStore_should_save_bigger_size_snapshot()
{
var metadata = new SnapshotMetadata(Pid, 100, Sys.Scheduler.Now.DateTime);
var metadata = new SnapshotMetadata(Pid, 100, Sys.Scheduler.Now.UtcDateTime);
var bigSnapshot = new byte[SnapshotByteSizeLimit];
new Random().NextBytes(bigSnapshot);
SnapshotStore.Tell(new SaveSnapshot(metadata, bigSnapshot), _senderProbe.Ref);
Expand All @@ -326,7 +326,7 @@ public virtual void ShouldSerializeSnapshots()
if (!SupportsSerialization) return;

var probe = CreateTestProbe();
var metadata = new SnapshotMetadata(Pid, 100L, Sys.Scheduler.Now.DateTime);
var metadata = new SnapshotMetadata(Pid, 100L, Sys.Scheduler.Now.UtcDateTime);
var snap = new TestPayload(probe.Ref);

SnapshotStore.Tell(new SaveSnapshot(metadata, snap), _senderProbe.Ref);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// </copyright>
//-----------------------------------------------------------------------

using Xunit.Abstractions;

namespace Akka.Persistence.TestKit.Tests
{
using System;
Expand All @@ -15,7 +17,7 @@ namespace Akka.Persistence.TestKit.Tests

public sealed class TestSnapshotStoreSpec : PersistenceTestKit
{
public TestSnapshotStoreSpec()
public TestSnapshotStoreSpec(ITestOutputHelper output) : base(nameof(TestSnapshotStoreSpec), output)
{
_probe = CreateTestProbe();
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/Akka.Persistence/Eventsourced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public void LoadSnapshot(string persistenceId, SnapshotSelectionCriteria criteri
/// <param name="snapshot">TBD</param>
public void SaveSnapshot(object snapshot)
{
SnapshotStore.Tell(new SaveSnapshot(new SnapshotMetadata(SnapshotterId, SnapshotSequenceNr, Context.System.Scheduler.Now.Date), snapshot));
SnapshotStore.Tell(new SaveSnapshot(new SnapshotMetadata(SnapshotterId, SnapshotSequenceNr, Context.System.Scheduler.Now.UtcDateTime), snapshot));
}

/// <summary>
Expand All @@ -230,7 +230,7 @@ public void SaveSnapshot(object snapshot)
/// <param name="sequenceNr">TBD</param>
public void DeleteSnapshot(long sequenceNr)
{
SnapshotStore.Tell(new DeleteSnapshot(new SnapshotMetadata(SnapshotterId, sequenceNr, DateTime.MinValue)));
SnapshotStore.Tell(new DeleteSnapshot(new SnapshotMetadata(SnapshotterId, sequenceNr, DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc))));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Persistence/Snapshot/LocalSnapshotStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private SnapshotMetadata ExtractSnapshotMetadata(FileInfo fileInfo)

if (long.TryParse(seqNrString, out var sequenceNr) && long.TryParse(timestampTicks, out var ticks))
{
return new SnapshotMetadata(pid, sequenceNr, new DateTime(ticks));
return new SnapshotMetadata(pid, sequenceNr, DateTime.SpecifyKind(new DateTime(ticks), DateTimeKind.Utc));
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/core/Akka.Persistence/Snapshot/MemorySnapshotStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ private static SnapshotEntry ToSnapshotEntry(SnapshotMetadata metadata, object s

private static SelectedSnapshot ToSelectedSnapshot(SnapshotEntry entry)
{
return new SelectedSnapshot(new SnapshotMetadata(entry.PersistenceId, entry.SequenceNr, new DateTime(entry.Timestamp)), entry.Snapshot);
return new SelectedSnapshot(metadata: new SnapshotMetadata(
persistenceId: entry.PersistenceId,
sequenceNr: entry.SequenceNr,
timestamp: DateTime.SpecifyKind(new DateTime(entry.Timestamp), DateTimeKind.Utc)),
snapshot: entry.Snapshot);
}
}

Expand Down

0 comments on commit d98d37f

Please sign in to comment.