-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
EventData.cs
493 lines (463 loc) · 25.1 KB
/
EventData.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using Azure.Core.Serialization;
using Azure.Messaging.EventHubs.Consumer;
namespace Azure.Messaging.EventHubs
{
/// <summary>
/// A set of data encapsulating an event and the associated metadata for
/// use with Event Hubs operations.
/// </summary>
///
public class EventData
{
/// <summary>
/// The data associated with the event, in <see cref="BinaryData" /> form, providing support
/// for a variety of data transformations and <see cref="ObjectSerializer" /> integration.
/// </summary>
///
/// <remarks>
/// <para>If the means for deserializing the raw data is not apparent to consumers, a
/// common technique is to make use of <see cref="EventData.Properties" /> to associate serialization hints
/// as an aid to consumers who wish to deserialize the binary data.</para>
/// </remarks>
///
/// <seealso cref="BinaryData" />
/// <seealso cref="EventData.Properties" />
///
public BinaryData EventBody { get; }
/// <summary>
/// The set of free-form event properties which may be used for passing metadata associated with the event body
/// during Event Hubs operations.
/// </summary>
///
/// <remarks>
/// A common use case for <see cref="EventData.Properties" /> is to associate serialization hints for the <see cref="EventData.EventBody" />
/// as an aid to consumers who wish to deserialize the binary data.
/// </remarks>
///
/// <example>
/// <code>
/// var eventData = new EventData(serializedTelemetryData);
/// eventData.Properties["eventType"] = "com.microsoft.Azure.monitoring.EtlEvent";
/// </code>
/// </example>
///
public IDictionary<string, object> Properties { get; }
/// <summary>
/// The set of free-form event properties which were provided by the Event Hubs service to pass metadata associated with the
/// event or associated Event Hubs operation.
/// </summary>
///
/// <remarks>
/// These properties are only populated for events received from the Event Hubs service. The set is otherwise
/// empty.
/// </remarks>
///
public IReadOnlyDictionary<string, object> SystemProperties { get; }
/// <summary>
/// The publishing sequence number assigned to the event at the time it was successfully published.
/// </summary>
///
/// <value>
/// The sequence number that was assigned during publishing, if the event was successfully
/// published by a sequence-aware producer. If the producer was not configured to apply
/// sequence numbering or if the event has not yet been successfully published, this member
/// will be <c>null</c>.
/// </value>
///
/// <remarks>
/// The published sequence number is only populated and relevant when certain features
/// of the producer are enabled. For example, it is used by idempotent publishing.
/// </remarks>
///
internal int? PublishedSequenceNumber { get; private set; }
/// <summary>
/// The sequence number assigned to the event when it was enqueued in the associated Event Hub partition.
/// </summary>
///
/// <remarks>
/// This property is only populated for events received from the Event Hubs service. If this
/// EventData was not received from the Event Hubs service, the value is <see cref="long.MinValue"/>.
/// </remarks>
///
public long SequenceNumber { get; }
/// <summary>
/// The offset of the event when it was received from the associated Event Hub partition.
/// </summary>
///
/// <remarks>
/// This property is only populated for events received from the Event Hubs service. If this
/// EventData was not received from the Event Hubs service, the value is <see cref="long.MinValue"/>.
/// </remarks>
///
public long Offset { get; }
/// <summary>
/// The date and time, in UTC, of when the event was enqueued in the Event Hub partition.
/// </summary>
///
/// <remarks>
/// This property is only populated for events received from the Event Hubs service. If this
/// EventData was not received from the Event Hubs service, the value <c>default(DateTimeOffset)</c>.
/// </remarks>
///
public DateTimeOffset EnqueuedTime { get; }
/// <summary>
/// The partition hashing key applied to the batch that the associated <see cref="EventData"/>, was published with.
/// </summary>
///
/// <remarks>
/// This property is only populated for events received from the Event Hubs service.
/// </remarks>
///
public string PartitionKey { get; }
/// <summary>
/// The data associated with the event.
/// </summary>
///
/// <remarks>
/// This member exists only to preserve backward compatibility. It is recommended to
/// prefer the <see cref="EventBody" /> where possible in order to take advantage of
/// additional functionality and improved usability for common scenarios.
/// </remarks>
///
/// <seealso cref="EventData.EventBody" />
///
[EditorBrowsable(EditorBrowsableState.Never)]
public ReadOnlyMemory<byte> Body => EventBody.ToMemory();
/// <summary>
/// The data associated with the event, in stream form.
/// </summary>
///
/// <value>
/// A <see cref="Stream" /> containing the raw data representing the <see cref="EventBody" />
/// of the event. The caller is assumed to have ownership of the stream, including responsibility
/// for managing its lifespan and ensuring proper disposal.
/// </value>
///
/// <remarks>
/// This member exists only to preserve backward compatibility. It is recommended to
/// prefer the <see cref="EventBody" /> where possible in order to take advantage of
/// additional functionality and improved usability for common scenarios.
/// </remarks>
///
/// <seealso cref="BinaryData.ToStream" />
/// <seealso cref="EventData.EventBody" />
///
[EditorBrowsable(EditorBrowsableState.Never)]
public Stream BodyAsStream => EventBody.ToStream();
/// <summary>
/// The sequence number of the event that was last enqueued into the Event Hub partition from which this
/// event was received.
/// </summary>
///
/// <remarks>
/// This property is only populated for events received using an reader specifying
/// <see cref="ReadEventOptions.TrackLastEnqueuedEventProperties" /> as enabled.
/// </remarks>
///
internal long? LastPartitionSequenceNumber { get; }
/// <summary>
/// The offset of the event that was last enqueued into the Event Hub partition from which this event was
/// received.
/// </summary>
///
/// <remarks>
/// This property is only populated for events received using an reader specifying
/// <see cref="ReadEventOptions.TrackLastEnqueuedEventProperties" /> as enabled.
/// </remarks>
///
internal long? LastPartitionOffset { get; }
/// <summary>
/// The date and time, in UTC, that the last event was enqueued into the Event Hub partition from
/// which this event was received.
/// </summary>
///
/// <remarks>
/// This property is only populated for events received using an reader specifying
/// <see cref="ReadEventOptions.TrackLastEnqueuedEventProperties" /> as enabled.
/// </remarks>
///
internal DateTimeOffset? LastPartitionEnqueuedTime { get; }
/// <summary>
/// The date and time, in UTC, that the last event information for the Event Hub partition was retrieved
/// from the Event Hubs service.
/// </summary>
///
/// <remarks>
/// This property is only populated for events received using an reader specifying
/// <see cref="ReadEventOptions.TrackLastEnqueuedEventProperties" /> as enabled.
/// </remarks>
///
internal DateTimeOffset? LastPartitionPropertiesRetrievalTime { get; }
/// <summary>
/// The publishing sequence number assigned to the event as part of a publishing operation.
/// </summary>
///
/// <value>
/// This member is only populated while a publishing operation is taking place; once the
/// operation has completed, successfully or not, the value is cleared.
/// </value>
///
/// <remarks>
/// The published sequence number is only populated and relevant when certain features
/// of the producer are enabled. For example, it is used by idempotent publishing.
/// </remarks>
///
internal int? PendingPublishSequenceNumber { get; set; }
/// <summary>
/// The producer group identifier assigned to the event as part of a publishing operation.
/// </summary>
///
/// <value>
/// This member is only populated while a publishing operation is taking place; once the
/// operation has completed, successfully or not, the value is cleared.
/// </value>
///
/// <remarks>
/// The producer group identifier is only populated and relevant when certain features
/// of the producer are enabled. For example, it is used by idempotent publishing.
/// </remarks>
///
internal long? PendingProducerGroupId { get; set; }
/// <summary>
/// The producer owner level assigned to the event as part of a publishing operation.
/// </summary>
///
/// <value>
/// This member is only populated while a publishing operation is taking place; once the
/// operation has completed, successfully or not, the value is cleared.
/// </value>
///
/// <remarks>
/// The producer group identifier is only populated and relevant when certain features
/// of the producer are enabled. For example, it is used by idempotent publishing.
/// </remarks>
///
internal short? PendingProducerOwnerLevel { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="EventData"/> class.
/// </summary>
///
/// <param name="eventBody">The raw data as binary to use as the body of the event.</param>
///
public EventData(BinaryData eventBody) : this(eventBody, lastPartitionSequenceNumber: null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EventData"/> class.
/// </summary>
///
/// <param name="eventBody">The raw data to use as the body of the event.</param>
///
public EventData(ReadOnlyMemory<byte> eventBody) : this(new BinaryData(eventBody), lastPartitionSequenceNumber: null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EventData"/> class.
/// </summary>
///
/// <param name="eventBody">The raw data to use as the body of the event.</param>
/// <param name="properties">The set of free-form event properties to send with the event.</param>
/// <param name="systemProperties">The set of system properties received from the Event Hubs service.</param>
/// <param name="sequenceNumber">The sequence number assigned to the event when it was enqueued in the associated Event Hub partition.</param>
/// <param name="offset">The offset of the event when it was received from the associated Event Hub partition.</param>
/// <param name="enqueuedTime">The date and time, in UTC, of when the event was enqueued in the Event Hub partition.</param>
/// <param name="partitionKey">The partition hashing key applied to the batch that the associated <see cref="EventData"/>, was sent with.</param>
/// <param name="lastPartitionSequenceNumber">The sequence number that was last enqueued into the Event Hub partition.</param>
/// <param name="lastPartitionOffset">The offset that was last enqueued into the Event Hub partition.</param>
/// <param name="lastPartitionEnqueuedTime">The date and time, in UTC, of the event that was last enqueued into the Event Hub partition.</param>
/// <param name="lastPartitionPropertiesRetrievalTime">The date and time, in UTC, that the last event information for the Event Hub partition was retrieved from the service.</param>
/// <param name="publishedSequenceNumber">The publishing sequence number assigned to the event at the time it was successfully published.</param>
/// <param name="pendingPublishSequenceNumber">The publishing sequence number assigned to the event as part of a publishing operation.</param>
/// <param name="pendingProducerGroupId">The producer group identifier assigned to the event as part of a publishing operation.</param>
/// <param name="pendingOwnerLevel">The producer owner level assigned to the event as part of a publishing operation.</param>
///
internal EventData(ReadOnlyMemory<byte> eventBody,
IDictionary<string, object> properties = null,
IReadOnlyDictionary<string, object> systemProperties = null,
long sequenceNumber = long.MinValue,
long offset = long.MinValue,
DateTimeOffset enqueuedTime = default,
string partitionKey = null,
long? lastPartitionSequenceNumber = null,
long? lastPartitionOffset = null,
DateTimeOffset? lastPartitionEnqueuedTime = null,
DateTimeOffset? lastPartitionPropertiesRetrievalTime = null,
int? publishedSequenceNumber = null,
int? pendingPublishSequenceNumber = null,
long? pendingProducerGroupId = null,
short? pendingOwnerLevel = null) : this(new BinaryData(eventBody), properties, systemProperties, sequenceNumber, offset, enqueuedTime, partitionKey,
lastPartitionSequenceNumber, lastPartitionOffset, lastPartitionEnqueuedTime, lastPartitionPropertiesRetrievalTime, publishedSequenceNumber, pendingPublishSequenceNumber,
pendingProducerGroupId, pendingOwnerLevel)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EventData"/> class.
/// </summary>
///
/// <param name="eventBody">The raw data as binary to use as the body of the event.</param>
/// <param name="properties">The set of free-form event properties to send with the event.</param>
/// <param name="systemProperties">The set of system properties received from the Event Hubs service.</param>
/// <param name="sequenceNumber">The sequence number assigned to the event when it was enqueued in the associated Event Hub partition.</param>
/// <param name="offset">The offset of the event when it was received from the associated Event Hub partition.</param>
/// <param name="enqueuedTime">The date and time, in UTC, of when the event was enqueued in the Event Hub partition.</param>
/// <param name="partitionKey">The partition hashing key applied to the batch that the associated <see cref="EventData"/>, was sent with.</param>
/// <param name="lastPartitionSequenceNumber">The sequence number that was last enqueued into the Event Hub partition.</param>
/// <param name="lastPartitionOffset">The offset that was last enqueued into the Event Hub partition.</param>
/// <param name="lastPartitionEnqueuedTime">The date and time, in UTC, of the event that was last enqueued into the Event Hub partition.</param>
/// <param name="lastPartitionPropertiesRetrievalTime">The date and time, in UTC, that the last event information for the Event Hub partition was retrieved from the service.</param>
/// <param name="publishedSequenceNumber">The publishing sequence number assigned to the event at the time it was successfully published.</param>
/// <param name="pendingPublishSequenceNumber">The publishing sequence number assigned to the event as part of a publishing operation.</param>
/// <param name="pendingProducerGroupId">The producer group identifier assigned to the event as part of a publishing operation.</param>
/// <param name="pendingOwnerLevel">The producer owner level assigned to the event as part of a publishing operation.</param>
///
internal EventData(BinaryData eventBody,
IDictionary<string, object> properties = null,
IReadOnlyDictionary<string, object> systemProperties = null,
long sequenceNumber = long.MinValue,
long offset = long.MinValue,
DateTimeOffset enqueuedTime = default,
string partitionKey = null,
long? lastPartitionSequenceNumber = null,
long? lastPartitionOffset = null,
DateTimeOffset? lastPartitionEnqueuedTime = null,
DateTimeOffset? lastPartitionPropertiesRetrievalTime = null,
int? publishedSequenceNumber = null,
int? pendingPublishSequenceNumber = null,
long? pendingProducerGroupId = null,
short? pendingOwnerLevel = null)
{
EventBody = eventBody;
Properties = properties ?? new Dictionary<string, object>();
SystemProperties = systemProperties ?? new Dictionary<string, object>();
SequenceNumber = sequenceNumber;
Offset = offset;
EnqueuedTime = enqueuedTime;
PartitionKey = partitionKey;
LastPartitionSequenceNumber = lastPartitionSequenceNumber;
LastPartitionOffset = lastPartitionOffset;
LastPartitionEnqueuedTime = lastPartitionEnqueuedTime;
LastPartitionPropertiesRetrievalTime = lastPartitionPropertiesRetrievalTime;
PublishedSequenceNumber = publishedSequenceNumber;
PendingPublishSequenceNumber = pendingPublishSequenceNumber;
PendingProducerGroupId = pendingProducerGroupId;
PendingProducerOwnerLevel = pendingOwnerLevel;
}
/// <summary>
/// Initializes a new instance of the <see cref="EventData"/> class.
/// </summary>
///
/// <param name="eventBody">The raw data to use as the body of the event.</param>
/// <param name="properties">The set of free-form event properties to send with the event.</param>
/// <param name="systemProperties">The set of system properties received from the Event Hubs service.</param>
/// <param name="sequenceNumber">The sequence number assigned to the event when it was enqueued in the associated Event Hub partition.</param>
/// <param name="offset">The offset of the event when it was received from the associated Event Hub partition.</param>
/// <param name="enqueuedTime">The date and time, in UTC, of when the event was enqueued in the Event Hub partition.</param>
/// <param name="partitionKey">The partition hashing key associated with the event when it was published.</param>
///
protected EventData(BinaryData eventBody,
IDictionary<string, object> properties = null,
IReadOnlyDictionary<string, object> systemProperties = null,
long sequenceNumber = long.MinValue,
long offset = long.MinValue,
DateTimeOffset enqueuedTime = default,
string partitionKey = null) : this(eventBody, properties, systemProperties, sequenceNumber, offset, enqueuedTime, partitionKey, lastPartitionSequenceNumber: null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EventData"/> class.
/// </summary>
///
/// <param name="eventBody">The raw data to use as the body of the event.</param>
/// <param name="properties">The set of free-form event properties to send with the event.</param>
/// <param name="systemProperties">The set of system properties received from the Event Hubs service.</param>
/// <param name="sequenceNumber">The sequence number assigned to the event when it was enqueued in the associated Event Hub partition.</param>
/// <param name="offset">The offset of the event when it was received from the associated Event Hub partition.</param>
/// <param name="enqueuedTime">The date and time, in UTC, of when the event was enqueued in the Event Hub partition.</param>
/// <param name="partitionKey">The partition hashing key associated with the event when it was published.</param>
///
[EditorBrowsable(EditorBrowsableState.Never)]
protected EventData(ReadOnlyMemory<byte> eventBody,
IDictionary<string, object> properties = null,
IReadOnlyDictionary<string, object> systemProperties = null,
long sequenceNumber = long.MinValue,
long offset = long.MinValue,
DateTimeOffset enqueuedTime = default,
string partitionKey = null) : this(new BinaryData(eventBody), properties, systemProperties, sequenceNumber, offset, enqueuedTime, partitionKey, lastPartitionSequenceNumber: null)
{
}
/// <summary>
/// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
/// </summary>
///
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
///
/// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
///
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj) => base.Equals(obj);
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
///
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
///
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => base.GetHashCode();
/// <summary>
/// Converts the instance to string representation.
/// </summary>
///
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
///
[EditorBrowsable(EditorBrowsableState.Never)]
public override string ToString() => base.ToString();
/// <summary>
/// Transitions the pending state to its permanent form.
/// </summary>
///
internal void CommitPublishingState()
{
PublishedSequenceNumber = PendingPublishSequenceNumber;
ClearPublishingState();
}
/// <summary>
/// Clears the pending publishing state.
/// </summary>
///
internal void ClearPublishingState()
{
PendingProducerGroupId = default;
PendingProducerOwnerLevel = default;
PendingPublishSequenceNumber = default;
}
/// <summary>
/// Creates a new copy of the current <see cref="EventData" />, cloning its attributes into a new instance.
/// </summary>
///
/// <returns>A new copy of <see cref="EventData" />.</returns>
///
internal EventData Clone() =>
new EventData
(
EventBody,
new Dictionary<string, object>(Properties),
SystemProperties,
SequenceNumber,
Offset,
EnqueuedTime,
PartitionKey,
LastPartitionSequenceNumber,
LastPartitionOffset,
LastPartitionEnqueuedTime,
LastPartitionPropertiesRetrievalTime,
PublishedSequenceNumber,
PendingPublishSequenceNumber
);
}
}