Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ public static T ReadValue<T, TInput>(IFieldCodec<T> codec, ref Reader<TInput> re
var field = reader.ReadFieldHeader();
return codec.ReadValue(ref reader, field)!;
}

public static T ReadIndependentValue<T, TInput>(IFieldCodec<T> codec, ref Reader<TInput> reader)
{
var result = ReadValue(codec, ref reader);
// Snapshot values are each encoded with a fresh serializer session.
reader.Session.Reset();
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ private void ApplySnapshot<TInput>(ref Reader<TInput> reader, IDurableDictionary
consumer.Reset(count);
for (var i = 0; i < count; i++)
{
var key = OrleansBinaryCommandCodecHelpers.ReadValue(keyCodec, ref reader);
var value = OrleansBinaryCommandCodecHelpers.ReadValue(valueCodec, ref reader);
var key = OrleansBinaryCommandCodecHelpers.ReadIndependentValue(keyCodec, ref reader);
var value = OrleansBinaryCommandCodecHelpers.ReadIndependentValue(valueCodec, ref reader);
consumer.ApplySet(key, value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private void ApplySnapshot<TInput>(ref Reader<TInput> reader, IDurableListComman
consumer.Reset(count);
for (var i = 0; i < count; i++)
{
consumer.ApplyAdd(OrleansBinaryCommandCodecHelpers.ReadValue(codec, ref reader));
consumer.ApplyAdd(OrleansBinaryCommandCodecHelpers.ReadIndependentValue(codec, ref reader));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private void ApplySnapshot<TInput>(ref Reader<TInput> reader, IDurableQueueComma
consumer.Reset(count);
for (var i = 0; i < count; i++)
{
consumer.ApplyEnqueue(OrleansBinaryCommandCodecHelpers.ReadValue(codec, ref reader));
consumer.ApplyEnqueue(OrleansBinaryCommandCodecHelpers.ReadIndependentValue(codec, ref reader));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void ApplySnapshot<TInput>(ref Reader<TInput> reader, IDurableSetCommand
consumer.Reset(count);
for (var i = 0; i < count; i++)
{
consumer.ApplyAdd(OrleansBinaryCommandCodecHelpers.ReadValue(codec, ref reader));
consumer.ApplyAdd(OrleansBinaryCommandCodecHelpers.ReadIndependentValue(codec, ref reader));
}
}
}
128 changes: 128 additions & 0 deletions test/Orleans.Journaling.Tests/OrleansBinaryCommandCodecTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ public void DictionaryCodec_AllCommands_RoundTrip()
Assert.Empty(consumer.SnapshotItems);
}

[Fact]
public void DictionaryCodec_PreChangeSnapshotPayload_ReplaysIndependentReferenceScopes()
{
var codec = new OrleansBinaryDurableDictionaryCommandCodec<string, SnapshotReferenceRecord>(
ValueCodec<string>(),
ValueCodec<SnapshotReferenceRecord>(),
SessionPool);
var items = CreateReferenceItems();
var payload = CodecTestHelpers.WriteEntry(
writer => codec.WriteSnapshot([new("first", items[0]), new("second", items[1])], writer));

Assert.Equal(
"0705400B6669727374204007010203C105E0400D7365636F6E64204007040506C105E0",
Convert.ToHexString(payload));

Comment thread
ReubenBond marked this conversation as resolved.
Resolved
var consumer = new RecordingDictionaryCommandHandler<string, SnapshotReferenceRecord>();
codec.Apply(CodecTestHelpers.ReadBuffer(payload), consumer);
AssertAliases(consumer.SnapshotItems.Select(static item => item.Value));
}

[Fact]
public void ListCodec_AllCommands_RoundTrip()
{
Expand Down Expand Up @@ -67,6 +87,21 @@ public void ListCodec_AllCommands_RoundTrip()
consumer.Commands);
}

[Fact]
public void ListCodec_PreChangeSnapshotPayload_ReplaysIndependentReferenceScopes()
{
var codec = new OrleansBinaryDurableListCommandCodec<SnapshotReferenceRecord>(
ValueCodec<SnapshotReferenceRecord>(),
SessionPool);
var payload = CodecTestHelpers.WriteEntry(writer => codec.WriteSnapshot(CreateReferenceItems(), writer));

Assert.Equal("0B05204007010203C105E0204007040506C105E0", Convert.ToHexString(payload));

var consumer = new SnapshotCollectionConsumer();
codec.Apply(CodecTestHelpers.ReadBuffer(payload), consumer);
AssertAliases(consumer.Items);
}

[Fact]
public void QueueCodec_AllCommands_RoundTrip()
{
Expand All @@ -92,6 +127,21 @@ public void QueueCodec_AllCommands_RoundTrip()
consumer.Commands);
}

[Fact]
public void QueueCodec_PreChangeSnapshotPayload_ReplaysIndependentReferenceScopes()
{
var codec = new OrleansBinaryDurableQueueCommandCodec<SnapshotReferenceRecord>(
ValueCodec<SnapshotReferenceRecord>(),
SessionPool);
var payload = CodecTestHelpers.WriteEntry(writer => codec.WriteSnapshot(CreateReferenceItems(), writer));

Assert.Equal("0705204007010203C105E0204007040506C105E0", Convert.ToHexString(payload));

var consumer = new SnapshotCollectionConsumer();
codec.Apply(CodecTestHelpers.ReadBuffer(payload), consumer);
AssertAliases(consumer.Items);
}

[Fact]
public void SetCodec_AllCommands_RoundTrip()
{
Expand All @@ -117,6 +167,21 @@ public void SetCodec_AllCommands_RoundTrip()
consumer.Commands);
}

[Fact]
public void SetCodec_PreChangeSnapshotPayload_ReplaysIndependentReferenceScopes()
{
var codec = new OrleansBinaryDurableSetCommandCodec<SnapshotReferenceRecord>(
ValueCodec<SnapshotReferenceRecord>(),
SessionPool);
var payload = CodecTestHelpers.WriteEntry(writer => codec.WriteSnapshot(CreateReferenceItems(), writer));

Assert.Equal("0705204007010203C105E0204007040506C105E0", Convert.ToHexString(payload));

var consumer = new SnapshotCollectionConsumer();
codec.Apply(CodecTestHelpers.ReadBuffer(payload), consumer);
AssertAliases(consumer.Items);
}

[Fact]
public void ValueStateAndTcsCodecs_AllCommands_RoundTrip()
{
Expand Down Expand Up @@ -492,4 +557,67 @@ private static void AssertTrailingDataRejected(Action action)
var exception = Assert.Throws<InvalidOperationException>(action);
Assert.Contains("trailing data", exception.Message);
}

private static SnapshotReferenceRecord[] CreateReferenceItems()
{
var first = new byte[] { 1, 2, 3 };
var second = new byte[] { 4, 5, 6 };
return
[
new() { Payload = first, Alias = first },
new() { Payload = second, Alias = second }
];
}

private static void AssertAliases(IEnumerable<SnapshotReferenceRecord> items)
{
Assert.Collection(
items,
item =>
{
Assert.Equal([1, 2, 3], item.Payload);
Assert.Same(item.Payload, item.Alias);
},
item =>
{
Assert.Equal([4, 5, 6], item.Payload);
Assert.Same(item.Payload, item.Alias);
});
}
}

[GenerateSerializer]
internal sealed class SnapshotReferenceRecord
{
[Id(0)]
public required byte[] Payload { get; init; }

[Id(1)]
public required byte[] Alias { get; init; }
}

internal sealed class SnapshotCollectionConsumer :
IDurableListCommandHandler<SnapshotReferenceRecord>,
IDurableQueueCommandHandler<SnapshotReferenceRecord>,
IDurableSetCommandHandler<SnapshotReferenceRecord>
{
public List<SnapshotReferenceRecord> Items { get; } = [];

public void ApplyAdd(SnapshotReferenceRecord item) => Items.Add(item);

public void ApplySet(int index, SnapshotReferenceRecord item) => throw new NotSupportedException();

public void ApplyInsert(int index, SnapshotReferenceRecord item) => throw new NotSupportedException();

public void ApplyRemoveAt(int index) => throw new NotSupportedException();

public void ApplyClear() => Items.Clear();

public void Reset(int capacityHint) => Items.Clear();

public void ApplyEnqueue(SnapshotReferenceRecord item) => Items.Add(item);

public void ApplyDequeue() => throw new NotSupportedException();

public void ApplyRemove(SnapshotReferenceRecord item) => throw new NotSupportedException();
}
Loading