forked from dotnet/orleans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SiloAddress.cs
332 lines (285 loc) · 13.6 KB
/
SiloAddress.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using Orleans.Core.Abstractions.Internal;
namespace Orleans.Runtime
{
/// <summary>
/// Data class encapsulating the details of silo addresses.
/// </summary>
[Serializable]
[DebuggerDisplay("SiloAddress {ToString()}")]
public class SiloAddress : IEquatable<SiloAddress>, IComparable<SiloAddress>, IComparable
{
internal static readonly int SizeBytes = 24; // 16 for the address, 4 for the port, 4 for the generation
/// <summary> Special constant value to indicate an empty SiloAddress. </summary>
public static SiloAddress Zero { get; private set; }
private const int INTERN_CACHE_INITIAL_SIZE = InternerConstants.SIZE_MEDIUM;
private static readonly TimeSpan internCacheCleanupInterval = TimeSpan.Zero;
private int hashCode = 0;
private bool hashCodeSet = false;
[NonSerialized]
private List<uint> uniformHashCache;
public IPEndPoint Endpoint { get; private set; }
public int Generation { get; private set; }
private const char SEPARATOR = '@';
private static readonly DateTime epoch = new DateTime(2010, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private static readonly Interner<SiloAddress, SiloAddress> siloAddressInterningCache;
static SiloAddress()
{
siloAddressInterningCache = new Interner<SiloAddress, SiloAddress>(INTERN_CACHE_INITIAL_SIZE, internCacheCleanupInterval);
var sa = new SiloAddress(new IPEndPoint(0, 0), 0);
Zero = siloAddressInterningCache.Intern(sa, sa);
}
/// <summary>
/// Factory for creating new SiloAddresses with specified IP endpoint address and silo generation number.
/// </summary>
/// <param name="ep">IP endpoint address of the silo.</param>
/// <param name="gen">Generation number of the silo.</param>
/// <returns>SiloAddress object initialized with specified address and silo generation.</returns>
public static SiloAddress New(IPEndPoint ep, int gen)
{
var sa = new SiloAddress(ep, gen);
return siloAddressInterningCache.Intern(sa, sa);
}
private SiloAddress(IPEndPoint endpoint, int gen)
{
Endpoint = endpoint;
Generation = gen;
}
public bool IsClient { get { return Generation < 0; } }
/// <summary> Allocate a new silo generation number. </summary>
/// <returns>A new silo generation number.</returns>
public static int AllocateNewGeneration()
{
long elapsed = (DateTime.UtcNow.Ticks - epoch.Ticks) / TimeSpan.TicksPerSecond;
return unchecked((int)elapsed); // Unchecked to truncate any bits beyond the lower 32
}
/// <summary>
/// Return this SiloAddress in a standard string form, suitable for later use with the <c>FromParsableString</c> method.
/// </summary>
/// <returns>SiloAddress in a standard string format.</returns>
public string ToParsableString()
{
// This must be the "inverse" of FromParsableString, and must be the same across all silos in a deployment.
// Basically, this should never change unless the data content of SiloAddress changes
return String.Format("{0}:{1}@{2}", Endpoint.Address, Endpoint.Port, Generation);
}
/// <summary>
/// Create a new SiloAddress object by parsing string in a standard form returned from <c>ToParsableString</c> method.
/// </summary>
/// <param name="addr">String containing the SiloAddress info to be parsed.</param>
/// <returns>New SiloAddress object created from the input data.</returns>
public static SiloAddress FromParsableString(string addr)
{
// This must be the "inverse" of ToParsableString, and must be the same across all silos in a deployment.
// Basically, this should never change unless the data content of SiloAddress changes
// First is the IPEndpoint; then '@'; then the generation
int atSign = addr.LastIndexOf(SEPARATOR);
if (atSign < 0)
{
throw new FormatException("Invalid string SiloAddress: " + addr);
}
var epString = addr.Substring(0, atSign);
var genString = addr.Substring(atSign + 1);
// IPEndpoint is the host, then ':', then the port
int lastColon = epString.LastIndexOf(':');
if (lastColon < 0) throw new FormatException("Invalid string SiloAddress: " + addr);
var hostString = epString.Substring(0, lastColon);
var portString = epString.Substring(lastColon + 1);
var host = IPAddress.Parse(hostString);
int port = Int32.Parse(portString);
return New(new IPEndPoint(host, port), Int32.Parse(genString));
}
/// <summary> Object.ToString method override. </summary>
public override string ToString()
{
return String.Format("{0}{1}:{2}", (IsClient ? "C" : "S"), Endpoint, Generation);
}
/// <summary>
/// Return a long string representation of this SiloAddress.
/// </summary>
/// <remarks>
/// Note: This string value is not comparable with the <c>FromParsableString</c> method -- use the <c>ToParsableString</c> method for that purpose.
/// </remarks>
/// <returns>String representaiton of this SiloAddress.</returns>
public string ToLongString()
{
return ToString();
}
/// <summary>
/// Return a long string representation of this SiloAddress, including it's consistent hash value.
/// </summary>
/// <remarks>
/// Note: This string value is not comparable with the <c>FromParsableString</c> method -- use the <c>ToParsableString</c> method for that purpose.
/// </remarks>
/// <returns>String representaiton of this SiloAddress.</returns>
public string ToStringWithHashCode()
{
return String.Format("{0}/x{1, 8:X8}", ToString(), GetConsistentHashCode());
}
/// <summary> Object.Equals method override. </summary>
public override bool Equals(object obj)
{
return Equals(obj as SiloAddress);
}
/// <summary> Object.GetHashCode method override. </summary>
public override int GetHashCode()
{
// Note that Port cannot be used because Port==0 matches any non-zero Port value for .Equals
return Endpoint.GetHashCode() ^ Generation.GetHashCode();
}
/// <summary>Get a consistent hash value for this silo address.</summary>
/// <returns>Consistent hash value for this silo address.</returns>
public int GetConsistentHashCode()
{
if (hashCodeSet) return hashCode;
// Note that Port cannot be used because Port==0 matches any non-zero Port value for .Equals
string siloAddressInfoToHash = Endpoint + Generation.ToString(CultureInfo.InvariantCulture);
hashCode = CalculateIdHash(siloAddressInfoToHash);
hashCodeSet = true;
return hashCode;
}
// This is the same method as Utils.CalculateIdHash
private static int CalculateIdHash(string text)
{
SHA256 sha = SHA256.Create(); // This is one implementation of the abstract class SHA1.
int hash = 0;
try
{
byte[] data = Encoding.Unicode.GetBytes(text);
byte[] result = sha.ComputeHash(data);
for (int i = 0; i < result.Length; i += 4)
{
int tmp = (result[i] << 24) | (result[i + 1] << 16) | (result[i + 2] << 8) | (result[i + 3]);
hash = hash ^ tmp;
}
}
finally
{
sha.Dispose();
}
return hash;
}
public List<uint> GetUniformHashCodes(int numHashes)
{
if (uniformHashCache != null) return uniformHashCache;
uniformHashCache = GetUniformHashCodesImpl(numHashes);
return uniformHashCache;
}
private List<uint> GetUniformHashCodesImpl(int numHashes)
{
var hashes = new List<uint>();
var bytes = new byte[16 + sizeof(int) + sizeof(int) + sizeof(int)]; // ip + port + generation + extraBit
var tmpInt = new int[1];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = 9;
}
// Endpoint IP Address
if (this.Endpoint.AddressFamily == AddressFamily.InterNetwork) // IPv4
{
for (int i = 0; i < 12; i++)
{
bytes[i] = 0;
}
Buffer.BlockCopy(this.Endpoint.Address.GetAddressBytes(), 0, bytes, 12, 4);
}
else // IPv6
{
Buffer.BlockCopy(this.Endpoint.Address.GetAddressBytes(), 0, bytes, 0, 16);
}
var offset = 16;
// Port
tmpInt[0] = this.Endpoint.Port;
Buffer.BlockCopy(tmpInt, 0, bytes, offset, sizeof(int));
offset += sizeof(int);
// Generation
tmpInt[0] = this.Generation;
Buffer.BlockCopy(tmpInt, 0, bytes, offset, sizeof(int));
offset += sizeof(int);
for (int extraBit = 0; extraBit < numHashes; extraBit++)
{
// extraBit
tmpInt[0] = extraBit;
Buffer.BlockCopy(tmpInt, 0, bytes, offset, sizeof(int));
hashes.Add(JenkinsHash.ComputeHash(bytes));
}
return hashes;
}
/// <summary>
/// Two silo addresses match if they are equal or if one generation or the other is 0
/// </summary>
/// <param name="other"> The other SiloAddress to compare this one with. </param>
/// <returns> Returns <c>true</c> if the two SiloAddresses are considered to match -- if they are equal or if one generation or the other is 0. </returns>
internal bool Matches(SiloAddress other)
{
return other != null && Endpoint.Address.Equals(other.Endpoint.Address) && (Endpoint.Port == other.Endpoint.Port) &&
((Generation == other.Generation) || (Generation == 0) || (other.Generation == 0));
}
#region IEquatable<SiloAddress> Members
/// <summary> IEquatable.Equals method override. </summary>
public bool Equals(SiloAddress other)
{
return other != null && Endpoint.Address.Equals(other.Endpoint.Address) && (Endpoint.Port == other.Endpoint.Port) &&
((Generation == other.Generation));
}
#endregion
// non-generic version of CompareTo is needed by some contexts. Just calls generic version.
public int CompareTo(object obj)
{
return CompareTo((SiloAddress)obj);
}
public int CompareTo(SiloAddress other)
{
if (other == null) return 1;
// Compare Generation first. It gives a cheap and fast way to compare, avoiding allocations
// and is also semantically meaningfull - older silos (with smaller Generation) will appear first in the comparison order.
// Only if Generations are the same, go on to compare Ports and IPAddress (which is more expansive to compare).
// Alternatively, we could compare ConsistentHashCode or UniformHashCode.
int comp = Generation.CompareTo(other.Generation);
if (comp != 0) return comp;
comp = Endpoint.Port.CompareTo(other.Endpoint.Port);
if (comp != 0) return comp;
return CompareIpAddresses(Endpoint.Address, other.Endpoint.Address);
}
// The comparions code is taken from: http://www.codeproject.com/Articles/26550/Extending-the-IPAddress-object-to-allow-relative-c
// Also note that this comparison does not handle semantic equivalence of IPv4 and IPv6 addresses.
// In particular, 127.0.0.1 and::1 are semanticaly the same, but not syntacticaly.
// For more information refer to: http://stackoverflow.com/questions/16618810/compare-ipv4-addresses-in-ipv6-notation
// and http://stackoverflow.com/questions/22187690/ip-address-class-getaddressbytes-method-putting-octets-in-odd-indices-of-the-byt
// and dual stack sockets, described at https://msdn.microsoft.com/en-us/library/system.net.ipaddress.maptoipv6(v=vs.110).aspx
private static int CompareIpAddresses(IPAddress one, IPAddress two)
{
int returnVal = 0;
if (one.AddressFamily == two.AddressFamily)
{
byte[] b1 = one.GetAddressBytes();
byte[] b2 = two.GetAddressBytes();
for (int i = 0; i < b1.Length; i++)
{
if (b1[i] < b2[i])
{
returnVal = -1;
break;
}
else if (b1[i] > b2[i])
{
returnVal = 1;
break;
}
}
}
else
{
returnVal = one.AddressFamily.CompareTo(two.AddressFamily);
}
return returnVal;
}
}
}