Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSet elements needs to be sorted during serialization #5093

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -7,6 +7,11 @@ namespace Akka.DistributedData.Serialization
{
internal class OtherMessageComparer : IComparer<OtherMessage>
{
public static OtherMessageComparer Instance { get; } = new OtherMessageComparer();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea


private OtherMessageComparer()
{}

public int Compare(OtherMessage a, OtherMessage b)
{
if (a == null || b == null)
Expand Down
Expand Up @@ -385,8 +385,6 @@ private static ORSet<T> ToGenericORSet<T>(ImmutableDictionary<object, VersionVec
private static readonly MethodInfo ORSetUnknownMaker =
typeof(ReplicatedDataSerializer).GetMethod(nameof(ORSetUnknownToProto), BindingFlags.Instance | BindingFlags.NonPublic);

private static readonly OtherMessageComparer OtherMessageComparer = new OtherMessageComparer();

/// <summary>
/// Called when we're serializing none of the standard object types with ORSet
/// </summary>
Expand All @@ -405,7 +403,7 @@ private Proto.Msg.ORSet ORSetUnknownToProto<T>(IORSet o, Proto.Msg.ORSet b)
otherElements.Add(otherElement);
otherElementsDict[otherElement] = SerializationSupport.VersionVectorToProto(kvp.Value);
}
otherElements.Sort(OtherMessageComparer);
otherElements.Sort(OtherMessageComparer.Instance);

foreach (var val in otherElements)
{
Expand Down Expand Up @@ -531,9 +529,14 @@ private Proto.Msg.GSet GSetToProto<T>(GSet<T> gset)
private Proto.Msg.GSet GSetToProtoUnknown<T>(IGSet g)
{
var gset = (GSet<T>)g;
var p = new Proto.Msg.GSet();
p.TypeInfo = GetTypeDescriptor(typeof(T));
p.OtherElements.Add(gset.Select(x => _ser.OtherMessageToProto(x)));
var otherElements = new List<OtherMessage>(gset.Select(x => _ser.OtherMessageToProto(x)));
otherElements.Sort(OtherMessageComparer.Instance);

var p = new Proto.Msg.GSet
{
TypeInfo = GetTypeDescriptor(typeof(T))
};
p.OtherElements.Add(otherElements);
return p;
}

Expand All @@ -547,25 +550,33 @@ private Proto.Msg.GSet ToProto(IGSet gset)
case GSet<int> ints:
{
var p = GSetToProto(ints);
p.IntElements.Add(ints.Elements);
var intElements = new List<int>(ints.Elements);
intElements.Sort();
p.IntElements.Add(intElements);
return p;
}
case GSet<long> longs:
{
var p = GSetToProto(longs);
p.LongElements.Add(longs.Elements);
var longElements = new List<long>(longs.Elements);
longElements.Sort();
p.LongElements.Add(longElements);
return p;
}
case GSet<string> strings:
{
var p = GSetToProto(strings);
p.StringElements.Add(strings.Elements);
var stringElements = new List<string>(strings.Elements);
stringElements.Sort();
p.StringElements.Add(stringElements);
return p;
}
case GSet<IActorRef> refs:
{
var p = GSetToProto(refs);
p.ActorRefElements.Add(refs.Select(Akka.Serialization.Serialization.SerializedActorPath));
var refElements = new List<IActorRef>(refs.Elements);
refElements.Sort();
p.ActorRefElements.Add(refElements.Select(Akka.Serialization.Serialization.SerializedActorPath));
return p;
}
default: // unknown type
Expand Down