-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathMessageSerializer.cs
More file actions
423 lines (364 loc) · 17.3 KB
/
Copy pathMessageSerializer.cs
File metadata and controls
423 lines (364 loc) · 17.3 KB
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
using System;
using System.Buffers;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Pipelines;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using Orleans.Configuration;
using Orleans.Networking.Shared;
using Orleans.Serialization.Buffers;
using Orleans.Serialization.Codecs;
using Orleans.Serialization.GeneratedCodeHelpers;
using Orleans.Serialization.Invocation;
using Orleans.Serialization.Serializers;
using Orleans.Serialization.Session;
using static Orleans.Runtime.Message;
namespace Orleans.Runtime.Messaging
{
internal sealed class MessageSerializer
{
private const int FramingLength = Message.LENGTH_HEADER_SIZE;
private const int MessageSizeHint = 4096;
private const int MaxRequestContextInitialCapacity = 1024;
private readonly Dictionary<Type, ResponseCodec> _rawResponseCodecs = [];
private readonly CodecProvider _codecProvider;
private readonly IFieldCodec<GrainAddressCacheUpdate> _grainAddressCacheUpdateCodec;
private readonly CachingSiloAddressCodec _readerSiloAddressCodec = new();
private readonly CachingSiloAddressCodec _writerSiloAddressCodec = new();
private readonly CachingIdSpanCodec _idSpanCodec = new();
private readonly SerializerSession _serializationSession;
private readonly SerializerSession _deserializationSession;
private readonly int _maxHeaderLength;
private readonly int _maxBodyLength;
private readonly DictionaryCodec<string, object> _requestContextCodec;
private readonly PrefixingBufferWriter _bufferWriter;
public MessageSerializer(
SerializerSessionPool sessionPool,
SharedMemoryPool memoryPool,
MessagingOptions options)
{
_serializationSession = sessionPool.GetSession();
_deserializationSession = sessionPool.GetSession();
_maxHeaderLength = options.MaxMessageHeaderSize;
_maxBodyLength = options.MaxMessageBodySize;
_codecProvider = sessionPool.CodecProvider;
_requestContextCodec = OrleansGeneratedCodeHelper.GetService<DictionaryCodec<string, object>>(this, sessionPool.CodecProvider);
_grainAddressCacheUpdateCodec = OrleansGeneratedCodeHelper.GetService<IFieldCodec<GrainAddressCacheUpdate>>(this, sessionPool.CodecProvider);
_bufferWriter = new(FramingLength, MessageSizeHint, memoryPool.Pool);
}
public (int RequiredBytes, int HeaderLength, int BodyLength) TryRead(ref ReadOnlySequence<byte> input, out Message? message)
{
if (input.Length < FramingLength)
{
message = default;
return (FramingLength, 0, 0);
}
Span<byte> lengthBytes = stackalloc byte[FramingLength];
input.Slice(input.Start, FramingLength).CopyTo(lengthBytes);
var headerLength = BinaryPrimitives.ReadInt32LittleEndian(lengthBytes);
var bodyLength = BinaryPrimitives.ReadInt32LittleEndian(lengthBytes[4..]);
// Check lengths
ThrowIfLengthsInvalid(headerLength, bodyLength);
var requiredBytes = FramingLength + headerLength + bodyLength;
if (input.Length < requiredBytes)
{
message = default;
return (requiredBytes, 0, 0);
}
try
{
// Decode header
var header = input.Slice(FramingLength, headerLength);
// Decode body
int bodyOffset = FramingLength + headerLength;
var body = input.Slice(bodyOffset, bodyLength);
// Build message
message = MessagePool.Get();
if (header.IsSingleSegment)
{
var headersReader = Reader.Create(header.First.Span, _deserializationSession);
Deserialize(ref headersReader, message);
}
else
{
var headersReader = Reader.Create(header, _deserializationSession);
Deserialize(ref headersReader, message);
}
if (bodyLength != 0)
{
_deserializationSession.PartialReset();
// Body deserialization is more likely to fail than header deserialization.
// Separating the two allows for these kinds of errors to be propagated back to the caller.
if (body.IsSingleSegment)
{
var reader = Reader.Create(body.First.Span, _deserializationSession);
ReadBodyObject(message, ref reader);
}
else
{
var reader = Reader.Create(body, _deserializationSession);
ReadBodyObject(message, ref reader);
}
}
return (0, headerLength, bodyLength);
}
finally
{
input = input.Slice(requiredBytes);
_deserializationSession.Reset();
}
}
private void ReadBodyObject<TInput>(Message message, ref Reader<TInput> reader)
{
var field = reader.ReadFieldHeader();
if (message.Result == ResponseTypes.Success)
{
message.Result = ResponseTypes.None; // reset raw response indicator
if (!_rawResponseCodecs.TryGetValue(field.FieldType, out var rawCodec))
rawCodec = GetRawCodec(field.FieldType);
message.BodyObject = rawCodec.ReadRaw(ref reader, ref field);
}
else
{
var bodyCodec = _codecProvider.GetCodec(field.FieldType);
message.BodyObject = bodyCodec.ReadValue(ref reader, field);
}
}
private ResponseCodec GetRawCodec(Type fieldType)
{
var rawCodec = (ResponseCodec)_codecProvider.GetCodec(typeof(Response<>).MakeGenericType(fieldType));
_rawResponseCodecs.Add(fieldType, rawCodec);
return rawCodec;
}
public (int HeaderLength, int BodyLength) Write(PipeWriter writer, Message message)
{
var headers = message.Headers;
IFieldCodec? bodyCodec = null;
ResponseCodec? rawCodec = null;
if (message.BodyObject is not null)
{
bodyCodec = _codecProvider.GetCodec(message.BodyObject.GetType());
if (headers.ResponseType is ResponseTypes.None && bodyCodec is ResponseCodec responseCodec)
{
rawCodec = responseCodec;
headers.ResponseType = ResponseTypes.Success; // indicates a raw simple response (not wrapped in Response<T>)
// The raw encoding changes the type encoded in the field header from Response<T> to T
// and does not encode a null reference value, but otherwise it's identical to normal encoding.
}
}
try
{
var bufferWriter = _bufferWriter;
bufferWriter.Init(writer);
var innerWriter = Writer.Create(new MessageBufferWriter(bufferWriter), _serializationSession);
Serialize(ref innerWriter, message, headers);
innerWriter.Commit();
var headerLength = bufferWriter.CommittedBytes;
_serializationSession.PartialReset();
if (bodyCodec is not null)
{
innerWriter = Writer.Create(new MessageBufferWriter(bufferWriter), _serializationSession);
if (rawCodec != null) rawCodec.WriteRaw(ref innerWriter, message.BodyObject!);
else bodyCodec.WriteField(ref innerWriter, 0, null, message.BodyObject);
innerWriter.Commit();
}
var bodyLength = bufferWriter.CommittedBytes - headerLength;
// Before completing, check lengths
ThrowIfLengthsInvalid(headerLength, bodyLength);
// Write length prefixes, first header length then body length.
var lengthFields = (headerLength, bodyLength);
if (!BitConverter.IsLittleEndian)
{
lengthFields.headerLength = BinaryPrimitives.ReverseEndianness(headerLength);
lengthFields.bodyLength = BinaryPrimitives.ReverseEndianness(bodyLength);
}
bufferWriter.Complete(MemoryMarshal.AsBytes(new Span<(int, int)>(ref lengthFields)));
return (headerLength, bodyLength);
}
finally
{
_bufferWriter.Reset();
_serializationSession.Reset();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ThrowIfLengthsInvalid(int headerLength, int bodyLength)
{
if (headerLength <= 0 || headerLength > _maxHeaderLength) ThrowInvalidHeaderLength(headerLength);
if ((uint)bodyLength > (uint)_maxBodyLength) ThrowInvalidBodyLength(bodyLength);
}
private void ThrowInvalidHeaderLength(int headerLength) => throw new InvalidMessageFrameException($"Invalid header size: {headerLength} (max configured value is {_maxHeaderLength}, see {nameof(MessagingOptions.MaxMessageHeaderSize)})");
private void ThrowInvalidBodyLength(int bodyLength) => throw new InvalidMessageFrameException($"Invalid body size: {bodyLength} (max configured value is {_maxBodyLength}, see {nameof(MessagingOptions.MaxMessageBodySize)})");
private void Serialize<TBufferWriter>(ref Writer<TBufferWriter> writer, Message value, PackedHeaders headers) where TBufferWriter : IBufferWriter<byte>
{
writer.WriteUInt32((uint)headers);
writer.WriteInt64(value.Id.ToInt64());
WriteGrainId(ref writer, value.SendingGrain);
WriteGrainId(ref writer, value.TargetGrain);
_writerSiloAddressCodec.WriteRaw(ref writer, value.SendingSilo);
_writerSiloAddressCodec.WriteRaw(ref writer, value.TargetSilo);
if (headers.HasFlag(MessageFlags.HasTimeToLive))
{
writer.WriteInt32((int)value.GetTimeToLiveMilliseconds());
}
if (headers.HasFlag(MessageFlags.HasInterfaceType))
{
_idSpanCodec.WriteRaw(ref writer, value.InterfaceType.Value);
}
if (headers.HasFlag(MessageFlags.HasInterfaceVersion))
{
writer.WriteVarUInt32(value.InterfaceVersion);
}
if (headers.HasFlag(MessageFlags.HasCacheInvalidationHeader))
{
WriteCacheInvalidationHeaders(ref writer, value);
}
// Always write RequestContext last
if (headers.HasFlag(MessageFlags.HasRequestContextData))
{
WriteRequestContext(ref writer, value.RequestContextData!);
}
}
private void Deserialize<TInput>(ref Reader<TInput> reader, Message result)
{
var headers = (PackedHeaders)reader.ReadUInt32();
result.Headers = headers;
result.Id = new CorrelationId(reader.ReadInt64());
result.SendingGrain = ReadGrainId(ref reader);
result.TargetGrain = ReadGrainId(ref reader);
result.SendingSilo = _readerSiloAddressCodec.ReadRaw(ref reader);
result.TargetSilo = _readerSiloAddressCodec.ReadRaw(ref reader);
if (headers.HasFlag(MessageFlags.HasTimeToLive))
{
result.SetTimeToLiveMilliseconds(reader.ReadInt32());
}
else
{
result.SetInfiniteTimeToLive();
}
if (headers.HasFlag(MessageFlags.HasInterfaceType))
{
var interfaceTypeSpan = _idSpanCodec.ReadRaw(ref reader);
result.InterfaceType = new GrainInterfaceType(interfaceTypeSpan);
}
if (headers.HasFlag(MessageFlags.HasInterfaceVersion))
{
result.InterfaceVersion = (ushort)reader.ReadVarUInt32();
}
if (headers.HasFlag(MessageFlags.HasCacheInvalidationHeader))
{
result.CacheInvalidationHeader = ReadCacheInvalidationHeaders(ref reader);
}
if (headers.HasFlag(MessageFlags.HasRequestContextData))
{
result.RequestContextData = ReadRequestContext(ref reader);
}
}
internal List<GrainAddressCacheUpdate> ReadCacheInvalidationHeaders<TInput>(ref Reader<TInput> reader)
{
var n = (int)reader.ReadVarUInt32();
if (n > 0)
{
var list = new List<GrainAddressCacheUpdate>(n);
for (int i = 0; i < n; i++)
{
list.Add(_grainAddressCacheUpdateCodec.ReadValue(ref reader, reader.ReadFieldHeader()));
}
return list;
}
return [];
}
internal void WriteCacheInvalidationHeaders<TBufferWriter>(ref Writer<TBufferWriter> writer, Message message) where TBufferWriter : IBufferWriter<byte>
{
// Lock during enumeration to avoid concurrent modifications.
// The list can be modified by other threads after the message is queued for sending.
var cacheUpdates = message.CacheInvalidationHeader;
if (cacheUpdates is null)
{
writer.WriteVarUInt32(0u);
}
else
{
lock (cacheUpdates)
{
writer.WriteVarUInt32((uint)cacheUpdates.Count);
foreach (var entry in cacheUpdates)
{
_grainAddressCacheUpdateCodec.WriteField(ref writer, 0, typeof(GrainAddressCacheUpdate), entry);
}
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string? ReadString<TInput>(ref Reader<TInput> reader)
{
var length = (int)reader.ReadVarUInt32() - 1;
if (length <= 0)
{
if (length < 0)
{
return null;
}
return string.Empty;
}
return StringCodec.ReadRaw(ref reader, (uint)length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void WriteString<TBufferWriter>(ref Writer<TBufferWriter> writer, string value) where TBufferWriter : IBufferWriter<byte>
{
if (value is null)
{
writer.WriteByte(1); // Equivalent to `writer.WriteVarUInt32(0);`
return;
}
var numBytes = Encoding.UTF8.GetByteCount(value);
writer.WriteVarUInt32((uint)numBytes + 1);
StringCodec.WriteRaw(ref writer, value, numBytes);
}
private static void WriteRequestContext<TBufferWriter>(ref Writer<TBufferWriter> writer, Dictionary<string, object> value) where TBufferWriter : IBufferWriter<byte>
{
writer.WriteVarUInt32((uint)value.Count);
foreach (var entry in value)
{
WriteString(ref writer, entry.Key);
ObjectCodec.WriteField(ref writer, 0, entry.Value);
}
}
private static Dictionary<string, object> ReadRequestContext<TInput>(ref Reader<TInput> reader)
{
var size = (int)reader.ReadVarUInt32();
var result = new Dictionary<string, object>(GetRequestContextInitialCapacity(size));
for (var i = 0; i < size; i++)
{
var key = ReadString(ref reader);
var value = ObjectCodec.ReadValue(ref reader, reader.ReadFieldHeader());
Debug.Assert(key is not null);
result.Add(key, value);
}
return result;
}
internal static int GetRequestContextInitialCapacity(int size) => size > MaxRequestContextInitialCapacity ? MaxRequestContextInitialCapacity : size;
private GrainId ReadGrainId<TInput>(ref Reader<TInput> reader)
{
var grainType = _idSpanCodec.ReadRaw(ref reader);
var grainKey = IdSpanCodec.ReadRaw(ref reader);
return new GrainId(new GrainType(grainType), grainKey);
}
private void WriteGrainId<TBufferWriter>(ref Writer<TBufferWriter> writer, GrainId value) where TBufferWriter : IBufferWriter<byte>
{
_idSpanCodec.WriteRaw(ref writer, value.Type.Value);
IdSpanCodec.WriteRaw(ref writer, value.Key);
}
}
internal readonly struct MessageBufferWriter : IBufferWriter<byte>
{
private readonly PrefixingBufferWriter _buffer;
public MessageBufferWriter(PrefixingBufferWriter buffer) => _buffer = buffer;
public void Advance(int count) => _buffer.Advance(count);
public Memory<byte> GetMemory(int sizeHint = 0) => _buffer.GetMemory(sizeHint);
public Span<byte> GetSpan(int sizeHint = 0) => _buffer.GetSpan(sizeHint);
}
}