Skip to content

Commit

Permalink
Fix GSet elements needs to be sorted (#5093)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkatufus committed Jun 15, 2021
1 parent 456d795 commit 0210e21
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Expand Up @@ -7,6 +7,11 @@ namespace Akka.DistributedData.Serialization
{
internal class OtherMessageComparer : IComparer<OtherMessage>
{
public static OtherMessageComparer Instance { get; } = new OtherMessageComparer();

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

0 comments on commit 0210e21

Please sign in to comment.