Skip to content

Commit 61df6fc

Browse files
authored
Separate wire protocol from internal models (#6206)
* Separate wire protocol from internal models * Move .proto file and fix wire compat
1 parent 9f84438 commit 61df6fc

File tree

12 files changed

+997
-650
lines changed

12 files changed

+997
-650
lines changed

build.fsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ Target "Protobuf" <| fun _ ->
591591
("Persistence.proto", "/src/core/Akka.Persistence/Serialization/Proto/");
592592
("StreamRefMessages.proto", "/src/core/Akka.Streams/Serialization/Proto/");
593593
("ReplicatorMessages.proto", "/src/contrib/cluster/Akka.DistributedData/Serialization/Proto/");
594-
("ReplicatedDataMessages.proto", "/src/contrib/cluster/Akka.DistributedData/Serialization/Proto/"); ]
594+
("ReplicatedDataMessages.proto", "/src/contrib/cluster/Akka.DistributedData/Serialization/Proto/")
595+
("ClusterMetricsMessages.proto", "/src/contrib/cluster/Akka.Cluster.Metrics/Serialization/Proto/") ]
595596

596597
printfn "Using proto.exe: %s" protocPath
597598

src/contrib/cluster/Akka.Cluster.Metrics/Routing/ClusterMetricsRouting.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Option<WeightedRoutees> UpdateWeightedRoutees()
131131
///
132132
/// The supervision strategy of the router actor can be configured with
133133
/// [[#withSupervisorStrategy]]. If no strategy is provided, routers default to
134-
/// a strategy of “always escalate”. This means that errors are passed up to the
134+
/// a strategy of [[always escalate]]. This means that errors are passed up to the
135135
/// router's supervisor for handling.
136136
///
137137
/// The router's supervisor will treat the error as an error with the router itself.

src/contrib/cluster/Akka.Cluster.Metrics/Serialization/ClusterMetricMessages.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ public interface IClusterMetricMessage { }
2525
/// Envelope adding a sender address to the cluster metrics gossip.
2626
/// </summary>
2727
[InternalApi]
28-
public sealed partial class MetricsGossipEnvelope : IClusterMetricMessage, IDeadLetterSuppression
28+
public sealed class MetricsGossipEnvelope : IClusterMetricMessage, IDeadLetterSuppression
2929
{
3030
/// <summary>
3131
/// Akka's actor address
3232
/// </summary>
3333
public Actor.Address FromAddress { get; }
34+
public MetricsGossip Gossip { get; }
35+
public bool Reply { get; }
3436

3537
/// <summary>
3638
/// Creates new instance of <see cref="MetricsGossipEnvelope"/>

src/contrib/cluster/Akka.Cluster.Metrics/Serialization/ClusterMetricsMessageSerializer.cs

Lines changed: 65 additions & 59 deletions
Large diffs are not rendered by default.

src/contrib/cluster/Akka.Cluster.Metrics/Serialization/EWMA.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ public static partial class Types
3131
/// the sampled value resulting from the previous smoothing iteration.
3232
/// This value is always used as the previous EWMA to calculate the new EWMA.
3333
/// </summary>
34-
public sealed partial class EWMA
34+
public sealed class EWMA : IEquatable<EWMA>
3535
{
36+
public double Value { get; }
37+
public double Alpha { get; }
38+
3639
/// <summary>
3740
/// Creates new instance of <see cref="EWMA"/>
3841
/// </summary>
@@ -47,10 +50,10 @@ public sealed partial class EWMA
4750
public EWMA(double value, double alpha)
4851
{
4952
if (alpha < 0 || alpha > 1)
50-
throw new ArgumentException(nameof(alpha), "alpha must be between 0.0 and 1.0");
53+
throw new ArgumentException("alpha must be between 0.0 and 1.0", nameof(alpha));
5154

52-
value_ = value;
53-
alpha_ = alpha;
55+
Value = value;
56+
Alpha = alpha;
5457
}
5558

5659
/// <summary>
@@ -83,11 +86,31 @@ public static double GetAlpha(TimeSpan halfLife, TimeSpan collectInterval)
8386

8487
var halfLifeMillis = halfLife.TotalMilliseconds;
8588
if (halfLifeMillis <= 0)
86-
throw new ArgumentException(nameof(halfLife), "halfLife must be > 0 s");
89+
throw new ArgumentException("halfLife must be > 0 s", nameof(halfLife));
8790

8891
var decayRate = logOf2 / halfLifeMillis;
8992
return 1 - Math.Exp(-decayRate * collectInterval.TotalMilliseconds);
9093
}
94+
95+
public bool Equals(EWMA other)
96+
{
97+
if (ReferenceEquals(null, other)) return false;
98+
if (ReferenceEquals(this, other)) return true;
99+
return Value.Equals(other.Value) && Alpha.Equals(other.Alpha);
100+
}
101+
102+
public override bool Equals(object obj)
103+
{
104+
return obj is EWMA other && Equals(other);
105+
}
106+
107+
public override int GetHashCode()
108+
{
109+
unchecked
110+
{
111+
return (Value.GetHashCode() * 397) ^ Alpha.GetHashCode();
112+
}
113+
}
91114
}
92115
}
93116
}

src/contrib/cluster/Akka.Cluster.Metrics/Serialization/Metric.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static partial class Types
2323
///
2424
/// Equality of Metric is based on its name index.
2525
/// </summary>
26-
public sealed partial class Metric
26+
public sealed class Metric: IEquatable<Metric>
2727
{
2828
/// <summary>
2929
/// Metric average value
@@ -79,7 +79,6 @@ public Metric(string name, AnyNumber value, Option<EWMA> average)
7979
Name = name;
8080
Value = value;
8181
Average = average;
82-
ewma_ = average.HasValue ? average.Value : default(EWMA);
8382
}
8483

8584
/// <summary>
@@ -163,21 +162,15 @@ public static Either<long, double> ConvertNumber(AnyNumber number)
163162
}
164163
}
165164

166-
/*
167-
* Two methods below, Equals and GetHashCode, should be used instead of generated in ClusterMetrics.Messages.g.cs
168-
* file. Since we do not have an option to not generate those methods for this particular class,
169-
* just stip them from generated code and paste here, with adding Address property check
170-
*/
165+
public override bool Equals(object obj)
166+
=> obj is Metric other && Equals(other);
171167

172-
173-
174168
public bool Equals(Metric other)
175169
{
176170
if (ReferenceEquals(null, other)) return false;
177171
if (ReferenceEquals(this, other)) return true;
178-
return Name == other.Name;
172+
return Name.Equals(other.Name);
179173
}
180-
181174

182175
public override int GetHashCode()
183176
{

src/contrib/cluster/Akka.Cluster.Metrics/Serialization/MetricsGossip.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ namespace Akka.Cluster.Metrics.Serialization
2121
/// Metrics gossip message
2222
/// </summary>
2323
[InternalApi]
24-
public sealed partial class MetricsGossip
24+
public sealed class MetricsGossip
2525
{
26-
public IImmutableSet<NodeMetrics> Nodes { get; private set; } = ImmutableHashSet<NodeMetrics>.Empty;
26+
public IImmutableSet<NodeMetrics> Nodes { get; }
2727

2828
/// <summary>
2929
/// Empty metrics gossip

src/contrib/cluster/Akka.Cluster.Metrics/Serialization/NodeMetrics.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Collections.Immutable;
1111
using System.Linq;
1212
using Akka.Util;
13-
using Google.Protobuf.Collections;
1413

1514
namespace Akka.Cluster.Metrics.Serialization
1615
{
@@ -20,9 +19,11 @@ namespace Akka.Cluster.Metrics.Serialization
2019
///
2120
/// Equality of NodeMetrics is based on its address.
2221
/// </summary>
23-
public sealed partial class NodeMetrics
22+
public sealed partial class NodeMetrics : IEquatable<NodeMetrics>
2423
{
25-
public Actor.Address Address { get; private set; }
24+
public Actor.Address Address { get; }
25+
public ImmutableList<Types.Metric> Metrics { get; }
26+
public long Timestamp { get; }
2627

2728
/// <summary>
2829
/// Creates new instance of <see cref="NodeMetrics"/>
@@ -33,9 +34,8 @@ public sealed partial class NodeMetrics
3334
public NodeMetrics(Actor.Address address, long timestamp, IEnumerable<Types.Metric> metrics)
3435
{
3536
Address = address;
36-
timestamp_ = timestamp;
37-
metrics_ = new RepeatedField<Types.Metric>();
38-
metrics_.AddRange(metrics);
37+
Timestamp = timestamp;
38+
Metrics = metrics.ToImmutableList();
3939
}
4040

4141
/// <summary>
@@ -93,19 +93,19 @@ public NodeMetrics Update(NodeMetrics that)
9393
* just stip them from generated code and paste here, with adding Address property check
9494
*/
9595

96-
96+
public override bool Equals(object obj)
97+
=> obj is NodeMetrics other && Equals(other);
9798

9899
public bool Equals(NodeMetrics other)
99100
{
100101
if (ReferenceEquals(null, other)) return false;
101102
if (ReferenceEquals(this, other)) return true;
102-
return Equals(Address, other.Address);
103+
return Address.Equals(other.Address);
103104
}
104105

105-
106106
public override int GetHashCode()
107107
{
108-
return (Address != null ? Address.GetHashCode() : 0);
108+
return Address.GetHashCode();
109109
}
110110
}
111111
}

0 commit comments

Comments
 (0)