-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathSerialization.cs
606 lines (536 loc) · 25.6 KB
/
Serialization.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//-----------------------------------------------------------------------
// <copyright file="Serialization.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Runtime.Serialization;
using Akka.Actor;
using Akka.Annotations;
using Akka.Util;
using Akka.Util.Internal;
using Akka.Util.Reflection;
using Akka.Configuration;
using Akka.Event;
namespace Akka.Serialization
{
/// <summary>
/// INTERNAL API.
///
/// Serialization information needed for serializing local actor refs.
/// </summary>
[InternalApi]
public sealed class Information : IEquatable<Information>
{
public Information(Address address, ActorSystem system)
{
Address = address;
System = system;
}
public Address Address { get; }
public ActorSystem System { get; }
public bool Equals(Information other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Address.Equals(other.Address);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Information)obj);
}
public override int GetHashCode()
{
unchecked
{
return (Address.GetHashCode() * 397);
}
}
public static bool operator ==(Information left, Information right)
{
return Equals(left, right);
}
public static bool operator !=(Information left, Information right)
{
return !Equals(left, right);
}
}
/// <summary>
/// The serialization system used by Akka.NET to serialize and deserialize objects
/// per the <see cref="ActorSystem"/>'s serialization configuration.
/// </summary>
public class Serialization
{
/// <summary>
/// Used to determine the manifest for a message, if applicable.
/// </summary>
/// <param name="s">The serializer we want to use on the message.</param>
/// <param name="msg">The message payload.</param>
/// <returns>A populated string is applicable; <see cref="string.Empty"/> otherwise.</returns>
/// <remarks>
/// WARNING: if you change this method it's likely that the DaemonMsgCreateSerializer and other calls will need changes too.
/// </remarks>
public static string ManifestFor(Serializer s, object msg)
{
switch (s)
{
case SerializerWithStringManifest s2:
return s2.Manifest(msg);
case Serializer s3 when s3.IncludeManifest:
return msg.GetType().TypeQualifiedName();
default:
return string.Empty;
}
}
/// <summary>
/// Needs to be INTERNAL so it can be accessed from tests. Should never be set directly.
/// </summary>
[ThreadStatic]
internal static Information CurrentTransportInformation;
/// <summary>
/// Retrieves the <see cref="Information"/> used for serializing and deserializing
/// <see cref="IActorRef"/> instances in all serializers.
/// </summary>
public static Information GetCurrentTransportInformation()
{
if (CurrentTransportInformation == null)
{
throw new InvalidOperationException(
"CurrentTransportInformation is not set. Use Serialization.WithTransport<T>.");
}
return CurrentTransportInformation;
}
/// <summary>
/// TBD
/// </summary>
/// <typeparam name="T">TBD</typeparam>
/// <param name="system">TBD</param>
/// <param name="address">TBD</param>
/// <param name="action">TBD</param>
/// <returns>TBD</returns>
[Obsolete("Obsolete. Use the SerializeWithTransport<T>(ExtendedActorSystem) method instead.")]
public static T WithTransport<T>(ActorSystem system, Address address, Func<T> action)
{
CurrentTransportInformation = new Information(address, system);
var res = action();
CurrentTransportInformation = null;
return res;
}
private readonly Serializer _nullSerializer;
private readonly ConcurrentDictionary<Type, Serializer> _serializerMap = new ConcurrentDictionary<Type, Serializer>();
private readonly Dictionary<int, Serializer> _serializersById = new Dictionary<int, Serializer>();
private readonly Dictionary<string, Serializer> _serializersByName = new Dictionary<string, Serializer>();
private readonly ImmutableHashSet<SerializerDetails> _serializerDetails;
private readonly MinimalLogger _initializationLogger;
/// <summary>
/// Serialization module. Contains methods for serialization and deserialization as well as
/// locating a Serializer for a particular class as defined in the mapping in the configuration.
/// </summary>
/// <param name="system">The ActorSystem to which this serializer belongs.</param>
public Serialization(ExtendedActorSystem system)
{
// We have to use stdout-logger here because serialization system is set up before
// the logging system were set up
_initializationLogger = system.Settings.StdoutLogger;
System = system;
_nullSerializer = new NullSerializer(system);
AddSerializer("null", _nullSerializer);
var serializersConfig = system.Settings.Config.GetConfig("akka.actor.serializers").AsEnumerable().ToList();
var serializerBindingConfig = system.Settings.Config.GetConfig("akka.actor.serialization-bindings").AsEnumerable().ToList();
var serializerSettingsConfig = system.Settings.Config.GetConfig("akka.actor.serialization-settings");
_serializerDetails = system.Settings.Setup.Get<SerializationSetup>()
.Select(x => x.CreateSerializers(system)).GetOrElse(ImmutableHashSet<SerializerDetails>.Empty);
foreach (var kvp in serializersConfig)
{
var serializerTypeName = kvp.Value.GetString();
var serializerType = Type.GetType(serializerTypeName);
if (serializerType == null)
{
system.Log.Warning("The type name for serializer '{0}' did not resolve to an actual Type: '{1}'", kvp.Key, serializerTypeName);
continue;
}
var serializerConfig = serializerSettingsConfig.GetConfig(kvp.Key);
var serializer = !serializerConfig.IsNullOrEmpty()
? (Serializer)Activator.CreateInstance(serializerType, system, serializerConfig)
: (Serializer)Activator.CreateInstance(serializerType, system);
AddSerializer(kvp.Key, serializer);
}
// Add any serializers that are registered via the SerializationSetup
// This has to be done here because SerializationSetup ALWAYS win.
foreach (var details in _serializerDetails)
{
AddSerializer(details.Alias, details.Serializer);
}
foreach (var kvp in serializerBindingConfig)
{
var typename = kvp.Key;
var serializerName = kvp.Value.GetString();
var messageType = Type.GetType(typename);
if (messageType == null)
{
system.Log.Warning("The type name for message/serializer binding '{0}' did not resolve to an actual Type: '{1}'", serializerName, typename);
continue;
}
if (!_serializersByName.TryGetValue(serializerName, out var serializer))
{
system.Log.Warning("Serialization binding to non existing serializer: '{0}'", serializerName);
continue;
}
AddSerializationMap(messageType, serializer);
}
// Add any serializer bindings that are registered via the SerializationSetup
// This has to be done here because SerializationSetup ALWAYS win.
foreach (var details in _serializerDetails)
{
// populate the serialization map
foreach (var t in details.UseFor)
{
AddSerializationMap(t, details.Serializer);
}
}
}
private Information SerializationInfo => System.Provider.SerializationInformation;
/// <summary>
/// Performs the requested serialization function while also setting
/// the <see cref="CurrentTransportInformation"/> based on available data
/// from the <see cref="ActorSystem"/>. Useful when serializing <see cref="IActorRef"/>s.
/// </summary>
/// <typeparam name="T">The type of message being serialized.</typeparam>
/// <param name="system">The <see cref="ActorSystem"/> performing serialization.</param>
/// <param name="action">The serialization function.</param>
/// <returns>The serialization output.</returns>
public static T WithTransport<T>(ExtendedActorSystem system, Func<T> action)
{
var info = system.Provider.SerializationInformation;
if (CurrentTransportInformation == info)
{
// already set
return action();
}
var oldInfo = CurrentTransportInformation;
try
{
CurrentTransportInformation = info;
return action();
}
finally
{
CurrentTransportInformation = oldInfo;
}
}
/// <summary>
/// Performs the requested serialization function while also setting
/// the <see cref="CurrentTransportInformation"/> based on available data
/// from the <see cref="ActorSystem"/>. Useful when serializing <see cref="IActorRef"/>s.
/// </summary>
/// <typeparam name="T">The type of message being serialized.</typeparam>
/// <typeparam name="TState">The type of caller input state to be used in the function</typeparam>
/// <param name="system">The <see cref="ActorSystem"/> performing serialization.</param>
/// <param name="state">the other input state to be passed in to the serialization function</param>
/// <param name="action">The serialization function.</param>
/// <returns>The serialization output.</returns>
public static T WithTransport<TState, T>(ExtendedActorSystem system, TState state, Func<TState, T> action)
{
var info = system.Provider.SerializationInformation;
if (CurrentTransportInformation == info)
{
// already set
return action(state);
}
var oldInfo = CurrentTransportInformation;
try
{
CurrentTransportInformation = info;
return action(state);
}
finally
{
CurrentTransportInformation = oldInfo;
}
}
private T WithTransport<T>(Func<T> action)
{
var oldInfo = CurrentTransportInformation;
try
{
if (oldInfo == null)
CurrentTransportInformation = SerializationInfo;
return action();
}
finally
{
CurrentTransportInformation = oldInfo;
}
}
private Serializer GetSerializerByName(string name)
{
if (name == null)
return null;
_serializersByName.TryGetValue(name, out Serializer serializer);
return serializer;
}
/// <summary>
/// The ActorSystem to which <see cref="Serialization"/> is bound.
/// </summary>
public ExtendedActorSystem System { get; }
/// <summary>
/// Adds the serializer to the internal state of the serialization subsystem
/// </summary>
/// <param name="serializer">Serializer instance</param>
[Obsolete("No longer supported. Use the AddSerializer(name, serializer) overload instead.", true)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddSerializer(Serializer serializer)
{
var id = serializer.Identifier;
if(_serializersById.ContainsKey(id) && _serializersById[id].GetType() != serializer.GetType())
{
LogWarning(
$"Serializer with identifier [{id}] are being overriden " +
$"from [{_serializersById[id].GetType()}] to [{serializer.GetType()}]. " +
"Did you mean to do this?");
}
_serializersById[id] = serializer;
}
/// <summary>
/// Adds the serializer to the internal state of the serialization subsystem
/// </summary>
/// <param name="name">Configuration name of the serializer</param>
/// <param name="serializer">Serializer instance</param>
public void AddSerializer(string name, Serializer serializer)
{
var id = serializer.Identifier;
if(_serializersById.ContainsKey(id) && _serializersById[id].GetType() != serializer.GetType())
{
LogWarning(
$"Serializer with identifier [{id}] are being overriden " +
$"from [{_serializersById[id].GetType()}] to [{serializer.GetType()}]. " +
"Did you mean to do this?");
}
if(_serializersByName.ContainsKey(name) && _serializersByName[name].GetType() != serializer.GetType())
LogWarning(
$"Serializer with name [{serializer.Identifier}] are being overriden " +
$"from [{_serializersByName[name].GetType()}] to [{serializer.GetType()}]. " +
"Did you mean to do this?");
_serializersById[id] = serializer;
_serializersByName[name] = serializer;
}
/// <summary>
/// TBD
/// </summary>
/// <param name="type">TBD</param>
/// <param name="serializer">TBD</param>
/// <returns>TBD</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddSerializationMap(Type type, Serializer serializer)
{
if(_serializerMap.ContainsKey(type) && _serializerMap[type].GetType() != serializer.GetType())
LogWarning(
$"Serializer for type [{type}] are being overriden " +
$"from [{_serializerMap[type].GetType()}] to [{serializer.GetType()}]. " +
"Did you mean to do this?");
_serializerMap[type] = serializer;
}
private void LogWarning(string str)
{
// Logging.StandardOutLogger is a MinimalActorRef, i.e. not a "real" actor
_initializationLogger.Tell(new Warning(nameof(Serialization), typeof(Serialization), str), ActorRefs.Nobody);
}
/// <summary>
/// Serializes the given message into an array of bytes using whatever serializer is currently configured.
/// </summary>
/// <param name="o">The message being serialized.</param>
/// <returns>A byte array containing the serialized message.</returns>
public byte[] Serialize(object o)
{
return WithTransport(() => FindSerializerFor(o).ToBinary(o));
}
/// <summary>
/// Deserializes the given array of bytes using the specified serializer id, using the optional type hint to the Serializer.
/// </summary>
/// <param name="bytes">TBD</param>
/// <param name="serializerId">TBD</param>
/// <param name="type">TBD</param>
/// <exception cref="SerializationException">
/// This exception is thrown if the system cannot find the serializer with the given <paramref name="serializerId"/>.
/// </exception>
/// <returns>The resulting object</returns>
public object Deserialize(byte[] bytes, int serializerId, Type type)
{
return WithTransport(() =>
{
if (!_serializersById.TryGetValue(serializerId, out var serializer))
throw new SerializationException(
$"Cannot find serializer with id [{serializerId}] (class [{type?.Name}]). The most probable reason" +
" is that the configuration entry 'akka.actor.serializers' is not in sync between the two systems." +
$" {Serializer.GetErrorForSerializerId(serializerId)}");
return serializer.FromBinary(bytes, type);
});
}
/// <summary>
/// Deserializes the given array of bytes using the specified serializer id, using the optional type hint to the Serializer.
/// </summary>
/// <param name="bytes">TBD</param>
/// <param name="serializerId">TBD</param>
/// <param name="manifest">TBD</param>
/// <exception cref="SerializationException">
/// This exception is thrown if the system cannot find the serializer with the given <paramref name="serializerId"/>
/// or it couldn't find the given <paramref name="manifest"/> with the given <paramref name="serializerId"/>.
/// </exception>
/// <returns>The resulting object</returns>
public object Deserialize(byte[] bytes, int serializerId, string manifest)
{
if (!_serializersById.TryGetValue(serializerId, out var serializer))
throw new SerializationException(
$"Cannot find serializer with id [{serializerId}] (manifest [{manifest}]). The most probable reason" +
" is that the configuration entry 'akka.actor.serializers' is not in sync between the two systems." +
$" {Serializer.GetErrorForSerializerId(serializerId)}");
// not using `withTransportInformation { () =>` because deserializeByteBuffer is supposed to be the
// possibility for allocation free serialization
var oldInfo = Serialization.CurrentTransportInformation;
try
{
if (oldInfo == null)
Serialization.CurrentTransportInformation = SerializationInfo;
if (serializer is SerializerWithStringManifest stringManifest)
return stringManifest.FromBinary(bytes, manifest);
if (string.IsNullOrEmpty(manifest))
return serializer.FromBinary(bytes, null);
Type type;
try
{
type = TypeCache.GetType(manifest);
}
catch (Exception ex)
{
throw new SerializationException(
$"Cannot find manifest class [{manifest}] for serializer with id [{serializerId}].", ex);
}
return serializer.FromBinary(bytes, type);
}
finally
{
Serialization.CurrentTransportInformation = oldInfo;
}
}
/// <summary>
/// Returns the Serializer configured for the given object, returns the NullSerializer if it's null.
/// </summary>
/// <param name="obj">The object that needs to be serialized</param>
/// <param name="defaultSerializerName">The config name of the serializer to use when no specific binding config is present</param>
/// <returns>The serializer configured for the given object type</returns>
public Serializer FindSerializerFor(object obj, string defaultSerializerName = null)
{
return obj == null ? _nullSerializer : FindSerializerForType(obj.GetType(), defaultSerializerName);
}
//cache to eliminate lots of typeof operator calls
private readonly Type _objectType = typeof(object);
/// <summary>
/// Returns the configured Serializer for the given Class. The configured Serializer
/// is used if the configured class `IsAssignableFrom` from the <see cref="Type">type</see>, i.e.
/// the configured class is a super class or implemented interface. In case of
/// ambiguity it is primarily using the most specific configured class,
/// and secondly the entry configured first.
/// </summary>
/// <param name="objectType">TBD</param>
/// <param name="defaultSerializerName">The config name of the serializer to use when no specific binding config is present</param>
/// <exception cref="SerializationException">
/// This exception is thrown if the serializer of the given <paramref name="objectType"/> could not be found.
/// </exception>
/// <returns>The serializer configured for the given object type</returns>
public Serializer FindSerializerForType(Type objectType, string defaultSerializerName = null)
{
if (_serializerMap.TryGetValue(objectType, out var fullMatchSerializer))
return fullMatchSerializer;
Serializer serializer = null;
Type type = objectType;
// TODO: see if we can do a better job with proper type sorting here - most specific to least specific (object serializer goes last)
foreach (var serializerType in _serializerMap)
{
// force deferral of the base "object" serializer until all other higher-level types have been evaluated
if (serializerType.Key.IsAssignableFrom(type) && serializerType.Key != _objectType)
{
serializer = serializerType.Value;
break;
}
}
if (serializer == null)
serializer = GetSerializerByName(defaultSerializerName);
// do a final check for the "object" serializer
if (serializer == null)
_serializerMap.TryGetValue(_objectType, out serializer);
if (serializer == null)
throw new SerializationException($"Serializer not found for type {objectType.Name}");
AddSerializationMap(type, serializer);
return serializer;
}
/// <summary>
/// The serialized path of an actorRef, based on the current transport serialization information.
/// If there is no external address available for the requested address then the systems default
/// address will be used.
///
/// If there is no external address available in the given <see cref="IActorRef"/> then the systems default
/// address will be used and that is retrieved from the ThreadLocal <see cref="Information"/>
/// that was set with <see cref="WithTransport{T}(ActorSystem, Address, Func{T})"/>
/// </summary>
/// <param name="actorRef">The <see cref="IActorRef"/> to be serialized.</param>
/// <returns>Absolute path to the serialized actor.</returns>
public static string SerializedActorPath(IActorRef actorRef)
{
if (Equals(actorRef, ActorRefs.NoSender))
return String.Empty;
var path = actorRef.Path;
ExtendedActorSystem originalSystem = null;
if (actorRef is ActorRefWithCell)
{
originalSystem = actorRef.AsInstanceOf<ActorRefWithCell>().Underlying.System.AsInstanceOf<ExtendedActorSystem>();
}
if (CurrentTransportInformation == null)
{
if (originalSystem == null)
{
var res = path.ToSerializationFormat();
return res;
}
try
{
var defaultAddress = originalSystem.Provider.DefaultAddress;
var res = path.ToSerializationFormatWithAddress(defaultAddress);
return res;
}
catch
{
return path.ToSerializationFormat();
}
}
//CurrentTransportInformation exists
var system = CurrentTransportInformation.System;
var address = CurrentTransportInformation.Address;
if (originalSystem == null || originalSystem == system)
{
var res = path.ToSerializationFormatWithAddress(address);
return res;
}
else
{
var provider = originalSystem.Provider;
var res =
path.ToSerializationFormatWithAddress(provider.GetExternalAddressFor(address) ?? provider.DefaultAddress);
return res;
}
}
internal Serializer GetSerializerById(int serializerId)
{
return _serializersById[serializerId];
}
}
}