-
Notifications
You must be signed in to change notification settings - Fork 494
/
ChangeFeedProcessorBuilder.cs
322 lines (283 loc) · 15.2 KB
/
ChangeFeedProcessorBuilder.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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using Microsoft.Azure.Cosmos.ChangeFeed.Configuration;
using Microsoft.Azure.Cosmos.ChangeFeed.LeaseManagement;
using static Microsoft.Azure.Cosmos.Container;
/// <summary>
/// Provides a flexible way to create an instance of <see cref="ChangeFeedProcessor"/> with custom set of parameters.
/// </summary>
public class ChangeFeedProcessorBuilder
{
private const string InMemoryDefaultHostName = "InMemory";
private readonly ContainerInternal monitoredContainer;
private readonly ChangeFeedProcessor changeFeedProcessor;
private readonly ChangeFeedLeaseOptions changeFeedLeaseOptions;
private readonly Action<DocumentServiceLeaseStoreManager,
ContainerInternal,
string,
ChangeFeedLeaseOptions,
ChangeFeedProcessorOptions,
ContainerInternal> applyBuilderConfiguration;
private readonly ChangeFeedProcessorOptions changeFeedProcessorOptions = new ChangeFeedProcessorOptions();
private ContainerInternal leaseContainer;
private string InstanceName;
private DocumentServiceLeaseStoreManager LeaseStoreManager;
private bool isBuilt;
internal ChangeFeedProcessorBuilder(
string processorName,
ContainerInternal container,
ChangeFeedProcessor changeFeedProcessor,
Action<DocumentServiceLeaseStoreManager,
ContainerInternal,
string,
ChangeFeedLeaseOptions,
ChangeFeedProcessorOptions,
ContainerInternal> applyBuilderConfiguration)
{
this.changeFeedLeaseOptions = new ChangeFeedLeaseOptions
{
LeasePrefix = processorName
};
this.monitoredContainer = container;
this.changeFeedProcessor = changeFeedProcessor;
this.applyBuilderConfiguration = applyBuilderConfiguration;
}
/// <summary>
/// Sets the compute instance name that will host the processor.
/// </summary>
/// <param name="instanceName">Name of compute instance hosting the processor.</param>
/// <remarks>
/// Instance name refers to the unique identifier of the compute that is running the processor.
/// Examples could be a VM instance identifier, a machine name, a pod id.
/// When distributing a processor across a cluster of compute hosts, each compute host should use a different instance name.
/// </remarks>
/// <returns>The instance of <see cref="ChangeFeedProcessorBuilder"/> to use.</returns>
public ChangeFeedProcessorBuilder WithInstanceName(string instanceName)
{
this.InstanceName = instanceName;
return this;
}
/// <summary>
/// Sets the mode for the change feed processor.
/// </summary>
/// <param name="changeFeedMode"></param>
/// <returns>The instance of <see cref="ChangeFeedProcessorBuilder"/> to use.</returns>
internal ChangeFeedProcessorBuilder WithChangeFeedMode(ChangeFeedMode changeFeedMode)
{
this.changeFeedProcessorOptions.Mode = changeFeedMode;
this.changeFeedLeaseOptions.Mode = changeFeedMode;
return this;
}
/// <summary>
/// Sets a custom configuration to be used by this instance of <see cref="ChangeFeedProcessor"/> to control how leases are maintained in a container when using <see cref="WithLeaseContainer"/>.
/// </summary>
/// <param name="acquireInterval">Interval to kick off a task to verify if leases are distributed evenly among known host instances.</param>
/// <param name="expirationInterval">Interval for which the lease is taken. If the lease is not renewed within this interval, it will cause it to expire and ownership of the lease will move to another processor instance.</param>
/// <param name="renewInterval">Renew interval for all leases currently held by a particular processor instance.</param>
/// <returns>The instance of <see cref="ChangeFeedProcessorBuilder"/> to use.</returns>
public ChangeFeedProcessorBuilder WithLeaseConfiguration(
TimeSpan? acquireInterval = null,
TimeSpan? expirationInterval = null,
TimeSpan? renewInterval = null)
{
this.changeFeedLeaseOptions.LeaseRenewInterval = renewInterval ?? ChangeFeedLeaseOptions.DefaultRenewInterval;
this.changeFeedLeaseOptions.LeaseAcquireInterval = acquireInterval ?? ChangeFeedLeaseOptions.DefaultAcquireInterval;
this.changeFeedLeaseOptions.LeaseExpirationInterval = expirationInterval ?? ChangeFeedLeaseOptions.DefaultExpirationInterval;
return this;
}
/// <summary>
/// Gets or sets the delay in between polling the change feed for new changes, after all current changes are drained.
/// </summary>
/// <remarks>
/// Applies only after a read on the change feed yielded no results.
/// </remarks>
/// <param name="pollInterval">Polling interval value.</param>
/// <returns>The instance of <see cref="ChangeFeedProcessorBuilder"/> to use.</returns>
public ChangeFeedProcessorBuilder WithPollInterval(TimeSpan pollInterval)
{
if (pollInterval == null)
{
throw new ArgumentNullException(nameof(pollInterval));
}
this.changeFeedProcessorOptions.FeedPollDelay = pollInterval;
return this;
}
/// <summary>
/// Indicates whether change feed in the Azure Cosmos DB service should start from beginning.
/// By default it's start from current time.
/// </summary>
/// <remarks>
/// This is only used when:
/// (1) Lease store is not initialized and is ignored if a lease exists and has continuation token.
/// (2) StartContinuation is not specified.
/// (3) StartTime is not specified.
/// </remarks>
/// <returns>The instance of <see cref="ChangeFeedProcessorBuilder"/> to use.</returns>
internal virtual ChangeFeedProcessorBuilder WithStartFromBeginning()
{
if (this.changeFeedProcessorOptions.Mode == ChangeFeedMode.AllVersionsAndDeletes)
{
throw new InvalidOperationException($"Using the '{nameof(WithStartFromBeginning)}' option with ChangeFeedProcessor is not supported with {ChangeFeedMode.AllVersionsAndDeletes} mode.");
}
this.changeFeedProcessorOptions.StartFromBeginning = true;
return this;
}
/// <summary>
/// Sets the time (exclusive) to start looking for changes after.
/// </summary>
/// <remarks>
/// This is only used when:
/// (1) Lease store is not initialized and is ignored if a lease exists and has continuation token.
/// (2) StartContinuation is not specified.
/// If this is specified, StartFromBeginning is ignored.
/// </remarks>
/// <param name="startTime">Date and time when to start looking for changes.</param>
/// <returns>The instance of <see cref="ChangeFeedProcessorBuilder"/> to use.</returns>
public ChangeFeedProcessorBuilder WithStartTime(DateTime startTime)
{
if (this.changeFeedProcessorOptions.Mode == ChangeFeedMode.AllVersionsAndDeletes)
{
throw new InvalidOperationException($"Using the '{nameof(WithStartTime)}' option with ChangeFeedProcessor is not supported with {ChangeFeedMode.AllVersionsAndDeletes} mode.");
}
if (startTime == null)
{
throw new ArgumentNullException(nameof(startTime));
}
this.changeFeedProcessorOptions.StartTime = startTime;
return this;
}
/// <summary>
/// Sets the maximum number of items to be returned in the enumeration operation in the Azure Cosmos DB service.
/// </summary>
/// <param name="maxItemCount">Maximum amount of items to be returned in a Change Feed request.</param>
/// <returns>An instance of <see cref="ChangeFeedProcessorBuilder"/>.</returns>
/// <remarks>This is just a hint to the server which can return less or more items per page. If operations in the container are performed through stored procedures or transactional batch, <see href="https://docs.microsoft.com/azure/cosmos-db/stored-procedures-triggers-udfs#transactions">transaction scope</see> is preserved when reading items from the Change Feed. As a result, the number of items received could be higher than the specified value so that the items changed by the same transaction are returned as part of one atomic batch.</remarks>
public ChangeFeedProcessorBuilder WithMaxItems(int maxItemCount)
{
if (maxItemCount <= 0)
{
throw new ArgumentOutOfRangeException(nameof(maxItemCount));
}
this.changeFeedProcessorOptions.MaxItemCount = maxItemCount;
return this;
}
/// <summary>
/// Sets the Cosmos Container to hold the leases state
/// </summary>
/// <param name="leaseContainer">Instance of a Cosmos Container to hold the leases.</param>
/// <returns>The instance of <see cref="ChangeFeedProcessorBuilder"/> to use.</returns>
public ChangeFeedProcessorBuilder WithLeaseContainer(Container leaseContainer)
{
if (leaseContainer == null)
{
throw new ArgumentNullException(nameof(leaseContainer));
}
if (this.leaseContainer != null)
{
throw new InvalidOperationException("The builder already defined a lease container.");
}
if (this.LeaseStoreManager != null)
{
throw new InvalidOperationException("The builder already defined an in-memory lease container instance.");
}
this.leaseContainer = (ContainerInternal)leaseContainer;
return this;
}
/// <summary>
/// Uses an in-memory container to maintain state of the leases
/// </summary>
/// <remarks>
/// Using an in-memory container restricts the scaling capability to just the instance running the current processor.
/// </remarks>
/// <returns>The instance of <see cref="ChangeFeedProcessorBuilder"/> to use.</returns>
internal virtual ChangeFeedProcessorBuilder WithInMemoryLeaseContainer()
{
if (this.leaseContainer != null)
{
throw new InvalidOperationException("The builder already defined a lease container.");
}
if (this.LeaseStoreManager != null)
{
throw new InvalidOperationException("The builder already defined an in-memory lease container instance.");
}
if (string.IsNullOrEmpty(this.InstanceName))
{
this.InstanceName = ChangeFeedProcessorBuilder.InMemoryDefaultHostName;
}
this.LeaseStoreManager = new DocumentServiceLeaseStoreManagerInMemory();
return this;
}
/// <summary>
/// Defines a delegate to receive notifications on errors that occur during change feed processor execution.
/// </summary>
/// <param name="errorDelegate">A delegate to receive notifications for change feed processor related errors.</param>
/// <returns>The instance of <see cref="ChangeFeedProcessorBuilder"/> to use.</returns>
public ChangeFeedProcessorBuilder WithErrorNotification(ChangeFeedMonitorErrorDelegate errorDelegate)
{
if (errorDelegate == null)
{
throw new ArgumentNullException(nameof(errorDelegate));
}
this.changeFeedProcessorOptions.HealthMonitor.SetErrorDelegate(errorDelegate);
return this;
}
/// <summary>
/// Defines a delegate to receive notifications on lease acquires that occur during change feed processor execution.
/// </summary>
/// <param name="acquireDelegate">A delegate to receive notifications when a change feed processor acquires a lease.</param>
/// <returns>The instance of <see cref="ChangeFeedProcessorBuilder"/> to use.</returns>
public ChangeFeedProcessorBuilder WithLeaseAcquireNotification(ChangeFeedMonitorLeaseAcquireDelegate acquireDelegate)
{
if (acquireDelegate == null)
{
throw new ArgumentNullException(nameof(acquireDelegate));
}
this.changeFeedProcessorOptions.HealthMonitor.SetLeaseAcquireDelegate(acquireDelegate);
return this;
}
/// <summary>
/// Defines a delegate to receive notifications on lease releases that occur during change feed processor execution.
/// </summary>
/// <param name="releaseDelegate">A delegate to receive notifications when a change feed processor releases a lease.</param>
/// <returns>The instance of <see cref="ChangeFeedProcessorBuilder"/> to use.</returns>
public ChangeFeedProcessorBuilder WithLeaseReleaseNotification(ChangeFeedMonitorLeaseReleaseDelegate releaseDelegate)
{
if (releaseDelegate == null)
{
throw new ArgumentNullException(nameof(releaseDelegate));
}
this.changeFeedProcessorOptions.HealthMonitor.SetLeaseReleaseDelegate(releaseDelegate);
return this;
}
/// <summary>
/// Builds a new instance of the <see cref="ChangeFeedProcessor"/> with the specified configuration.
/// </summary>
/// <returns>An instance of <see cref="ChangeFeedProcessor"/>.</returns>
public ChangeFeedProcessor Build()
{
if (this.isBuilt)
{
throw new InvalidOperationException("This builder instance has already been used to build a processor. Create a new instance to build another.");
}
if (this.monitoredContainer == null)
{
throw new InvalidOperationException(nameof(this.monitoredContainer) + " was not specified");
}
if (this.leaseContainer == null && this.LeaseStoreManager == null)
{
throw new InvalidOperationException($"Defining the lease store by WithLeaseContainer or WithInMemoryLeaseContainer is required.");
}
if (this.changeFeedLeaseOptions.LeasePrefix == null)
{
throw new InvalidOperationException("Processor name not specified during creation.");
}
this.applyBuilderConfiguration(this.LeaseStoreManager, this.leaseContainer, this.InstanceName, this.changeFeedLeaseOptions, this.changeFeedProcessorOptions, this.monitoredContainer);
this.isBuilt = true;
return this.changeFeedProcessor;
}
}
}