-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
PartitionContext.cs
213 lines (184 loc) · 9.39 KB
/
PartitionContext.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.Azure.EventHubs.Processor
{
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs.Primitives;
/// <summary>
/// Encapsulates information related to an Event Hubs partition used by <see cref="IEventProcessor"/>.
/// </summary>
public class PartitionContext
{
readonly EventProcessorHost host;
/// Creates a new <see cref="PartitionContext"/> object.
public PartitionContext(EventProcessorHost host, string partitionId, string eventHubPath, string consumerGroupName, CancellationToken cancellationToken)
{
this.host = host;
this.PartitionId = partitionId;
this.EventHubPath = eventHubPath;
this.ConsumerGroupName = consumerGroupName;
this.CancellationToken = cancellationToken;
this.ThisLock = new object();
this.Offset = EventPosition.FromStart().Offset;
this.SequenceNumber = 0;
this.RuntimeInformation = new ReceiverRuntimeInformation(partitionId);
}
/// <summary>
/// Gets triggered when the partition gets closed.
/// </summary>
public CancellationToken CancellationToken { get; }
/// <summary>
/// Gets the name of the consumer group.
/// </summary>
public string ConsumerGroupName { get; }
/// <summary>
/// Gets the path of the event hub.
/// </summary>
public string EventHubPath { get; }
/// <summary>
/// Gets the partition ID for the context.
/// </summary>
public string PartitionId { get; }
/// <summary>
/// Gets the host owner for the partition.
/// </summary>
public string Owner => this.Lease.Owner;
/// <summary>
/// Gets the approximate receiver runtime information for a logical partition of an Event Hub.
/// To enable the setting, refer to <see cref="EventProcessorOptions.EnableReceiverRuntimeMetric"/>
/// </summary>
public ReceiverRuntimeInformation RuntimeInformation { get; }
internal string Offset { get; set; }
internal long SequenceNumber { get; set; }
/// <summary>
/// Gets the most recent checkpointed lease.
/// </summary>
// Unlike other properties which are immutable after creation, the lease is updated dynamically and needs a setter.
public Lease Lease { get; internal set; }
object ThisLock { get; }
internal void SetOffsetAndSequenceNumber(EventData eventData)
{
Guard.ArgumentNotNull(nameof(eventData), eventData);
lock (this.ThisLock)
{
this.Offset = eventData.SystemProperties.Offset;
this.SequenceNumber = eventData.SystemProperties.SequenceNumber;
}
}
internal async Task<EventPosition> GetInitialOffsetAsync() // throws InterruptedException, ExecutionException
{
Checkpoint startingCheckpoint = await this.host.CheckpointManager.GetCheckpointAsync(this.PartitionId).ConfigureAwait(false);
EventPosition eventPosition;
if (startingCheckpoint == null)
{
// No checkpoint was ever stored. Use the initialOffsetProvider instead.
ProcessorEventSource.Log.PartitionPumpInfo(this.host.HostName, this.PartitionId, "Calling user-provided initial offset provider");
eventPosition = this.host.EventProcessorOptions.InitialOffsetProvider(this.PartitionId);
ProcessorEventSource.Log.PartitionPumpInfo(
this.host.HostName,
this.PartitionId,
$"Initial Position Provider. Offset:{eventPosition.Offset}, SequenceNumber:{eventPosition.SequenceNumber}, DateTime:{eventPosition.EnqueuedTimeUtc}");
}
else
{
this.Offset = startingCheckpoint.Offset;
this.SequenceNumber = startingCheckpoint.SequenceNumber;
ProcessorEventSource.Log.PartitionPumpInfo(
this.host.HostName,
this.PartitionId, $"Retrieved starting offset/sequenceNumber: {this.Offset}/{this.SequenceNumber}");
eventPosition = EventPosition.FromOffset(this.Offset);
}
return eventPosition;
}
/// <summary>
/// Writes the current offset and sequenceNumber to the checkpoint store via the checkpoint manager.
/// </summary>
public Task CheckpointAsync()
{
// Capture the current offset and sequenceNumber. Synchronize to be sure we get a matched pair
// instead of catching an update halfway through. Do the capturing here because by the time the checkpoint
// task runs, the fields in this object may have changed, but we should only write to store what the user
// has directed us to write.
Checkpoint capturedCheckpoint;
lock(this.ThisLock)
{
// Don't checkpoint for start of stream so that we can still respect initial offset provider.
if (this.Offset == "-1")
{
return Task.CompletedTask;
}
capturedCheckpoint = new Checkpoint(this.PartitionId, this.Offset, this.SequenceNumber);
}
return this.PersistCheckpointAsync(capturedCheckpoint);
}
/// <summary>
/// Stores the offset and sequenceNumber from the provided received EventData instance, then writes those
/// values to the checkpoint store via the checkpoint manager.
/// </summary>
/// <param name="eventData">A received EventData with valid offset and sequenceNumber</param>
/// <exception cref="ArgumentNullException">If suplied eventData is null</exception>
/// <exception cref="ArgumentOutOfRangeException">If the sequenceNumber is less than the last checkpointed value</exception>
public Task CheckpointAsync(EventData eventData)
{
Guard.ArgumentNotNull(nameof(eventData), eventData);
// We have never seen this sequence number yet
if (eventData.SystemProperties.SequenceNumber > this.SequenceNumber)
{
throw new ArgumentOutOfRangeException("eventData.SystemProperties.SequenceNumber");
}
return this.PersistCheckpointAsync(new Checkpoint(this.PartitionId, eventData.SystemProperties.Offset, eventData.SystemProperties.SequenceNumber));
}
/// <summary>
/// Writes the current offset and sequenceNumber to the checkpoint store via the checkpoint manager.
/// </summary>
public Task CheckpointAsync(Checkpoint checkPoint)
{
return this.PersistCheckpointAsync(checkPoint);
}
/// <summary>
/// Provides the parition context in the following format:"PartitionContext({EventHubPath}/{ConsumerGroupName}/{PartitionId}/{SequenceNumber})"
/// </summary>
/// <returns></returns>
public override string ToString()
{
return $"PartitionContext({this.EventHubPath}/{this.ConsumerGroupName}/{this.PartitionId}/{this.SequenceNumber})";
}
async Task PersistCheckpointAsync(Checkpoint checkpoint)
{
ProcessorEventSource.Log.PartitionPumpCheckpointStart(this.host.HostName, checkpoint.PartitionId, checkpoint.Offset, checkpoint.SequenceNumber);
try
{
Checkpoint inStoreCheckpoint = await this.host.CheckpointManager.GetCheckpointAsync(checkpoint.PartitionId).ConfigureAwait(false);
if (inStoreCheckpoint == null || checkpoint.SequenceNumber >= inStoreCheckpoint.SequenceNumber)
{
if (inStoreCheckpoint == null)
{
await this.host.CheckpointManager.CreateCheckpointIfNotExistsAsync(checkpoint.PartitionId).ConfigureAwait(false);
}
await this.host.CheckpointManager.UpdateCheckpointAsync(this.Lease, checkpoint).ConfigureAwait(false);
// Update internal lease if update call above is successful.
this.Lease.Offset = checkpoint.Offset;
this.Lease.SequenceNumber = checkpoint.SequenceNumber;
}
else
{
string msg = $"Ignoring out of date checkpoint with offset {checkpoint.Offset}/sequence number {checkpoint.SequenceNumber}" +
$" because current persisted checkpoint has higher offset {inStoreCheckpoint.Offset}/sequence number {inStoreCheckpoint.SequenceNumber}";
ProcessorEventSource.Log.PartitionPumpError(this.host.HostName, checkpoint.PartitionId, msg);
throw new ArgumentOutOfRangeException("offset/sequenceNumber", msg);
}
}
catch (Exception e)
{
ProcessorEventSource.Log.PartitionPumpCheckpointError(this.host.HostName, checkpoint.PartitionId, e.ToString());
throw;
}
finally
{
ProcessorEventSource.Log.PartitionPumpCheckpointStop(this.host.HostName, checkpoint.PartitionId);
}
}
}
}