-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathJobShard.cs
More file actions
395 lines (350 loc) · 14.5 KB
/
Copy pathJobShard.cs
File metadata and controls
395 lines (350 loc) · 14.5 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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Orleans.Runtime;
namespace Orleans.DurableJobs;
/// <summary>
/// Represents a shard of durable jobs that manages a collection of jobs within a specific time range.
/// A job shard is responsible for storing, retrieving, and managing the lifecycle of durable jobs
/// that fall within its designated time window.
/// </summary>
/// <remarks>
/// Job shards are used to partition durable jobs across time ranges to improve scalability
/// and performance. Each shard has a defined start and end time that determines which jobs
/// it manages. Shards can be marked as complete when all jobs within their time range
/// have been processed.
/// </remarks>
public interface IJobShard : IAsyncDisposable
{
/// <summary>
/// Gets the unique identifier for this job shard.
/// </summary>
string Id { get; }
/// <summary>
/// Gets the start time of the time range managed by this shard.
/// </summary>
DateTimeOffset StartTime { get; }
/// <summary>
/// Gets the end time of the time range managed by this shard.
/// </summary>
DateTimeOffset EndTime { get; }
/// <summary>
/// Gets optional metadata associated with this job shard.
/// </summary>
IDictionary<string, string>? Metadata { get; }
/// <summary>
/// Gets a value indicating whether this shard has been marked as complete and is no longer accepting new jobs.
/// </summary>
/// <remarks>
/// When a shard is marked as complete (via <see cref="MarkAsCompleteAsync"/>), no new jobs can be added to it.
/// </remarks>
bool IsAddingCompleted { get; }
/// <summary>
/// Consumes durable jobs from this shard in order of their due time.
/// </summary>
/// <returns>An asynchronous enumerable of durable job contexts.</returns>
IAsyncEnumerable<IJobRunContext> ConsumeDurableJobsAsync();
/// <summary>
/// Attempts to reserve a dequeued durable job for execution.
/// </summary>
/// <param name="jobContext">The dequeued job context.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>
/// A task containing <see cref="DurableJobMutationResult.Applied"/> when the attempt may start,
/// <see cref="DurableJobMutationResult.JobNotFound"/> when cancellation or completion removed the job first,
/// or <see cref="DurableJobMutationResult.OwnershipLost"/> when this shard is no longer locally owned.
/// </returns>
Task<DurableJobMutationResult> TryStartAttemptAsync(
IJobRunContext jobContext,
CancellationToken cancellationToken);
/// <summary>
/// Gets the number of jobs currently scheduled in this shard.
/// </summary>
/// <returns>A task that represents the asynchronous operation. The task result contains the job count.</returns>
ValueTask<int> GetJobCountAsync();
/// <summary>
/// Marks this shard as complete, preventing new jobs from being scheduled.
/// </summary>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
Task MarkAsCompleteAsync(CancellationToken cancellationToken);
/// <summary>
/// Removes a durable job from this shard.
/// </summary>
/// <param name="jobId">The unique identifier of the job to remove.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task containing the durable mutation outcome.</returns>
Task<DurableJobMutationResult> RemoveJobAsync(string jobId, CancellationToken cancellationToken);
/// <summary>
/// Reschedules a job to be retried at a later time.
/// </summary>
/// <param name="jobContext">The context of the job to retry.</param>
/// <param name="newDueTime">
/// The new due time for the job. This value supersedes <see cref="DurableJob.DueTime"/> on
/// <see cref="IJobRunContext.Job"/>.
/// </param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task containing the durable mutation outcome.</returns>
Task<DurableJobMutationResult> RetryJobLaterAsync(
IJobRunContext jobContext,
DateTimeOffset newDueTime,
CancellationToken cancellationToken);
/// <summary>
/// Reschedules a successfully completed execution with its dequeue count reset.
/// </summary>
/// <param name="jobContext">The context of the completed execution.</param>
/// <param name="newDueTime">
/// The new due time for the job. This value supersedes <see cref="DurableJob.DueTime"/> on
/// <see cref="IJobRunContext.Job"/>.
/// </param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task containing the durable mutation outcome.</returns>
Task<DurableJobMutationResult> RescheduleJobAsync(
IJobRunContext jobContext,
DateTimeOffset newDueTime,
CancellationToken cancellationToken);
/// <summary>
/// Attempts to schedule a new job on this shard.
/// </summary>
/// <param name="request">The request containing the job scheduling parameters.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the durable job if successful, or null if the job could not be scheduled (e.g., the shard was marked as complete).</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the due time is outside the shard's time range.</exception>
Task<DurableJob?> TryScheduleJobAsync(ScheduleJobRequest request, CancellationToken cancellationToken);
}
/// <summary>
/// Describes the outcome of an attempt to mutate a durable job after execution.
/// </summary>
public enum DurableJobMutationResult
{
/// <summary>
/// The mutation was durably applied.
/// </summary>
Applied,
/// <summary>
/// The job was no longer present, typically because cancellation or completion removed it first.
/// </summary>
JobNotFound,
/// <summary>
/// The local silo no longer owned the shard, so the mutation was not applied.
/// </summary>
OwnershipLost
}
/// <summary>
/// Base implementation of <see cref="IJobShard"/> that provides common functionality for job shard implementations.
/// </summary>
public abstract class JobShard : IJobShard
{
private readonly InMemoryJobQueue _jobQueue;
private readonly SemaphoreSlim _mutationLock = new(1, 1);
/// <inheritdoc/>
public string Id { get; protected set; }
/// <inheritdoc/>
public DateTimeOffset StartTime { get; protected set; }
/// <inheritdoc/>
public DateTimeOffset EndTime { get; protected set; }
/// <inheritdoc/>
public IDictionary<string, string>? Metadata { get; protected set; }
/// <inheritdoc/>
public bool IsAddingCompleted { get; protected set; }
/// <summary>
/// Initializes a new instance of the <see cref="JobShard"/> class.
/// </summary>
/// <param name="id">The unique identifier for this job shard.</param>
/// <param name="startTime">The start time of the time range managed by this shard.</param>
/// <param name="endTime">The end time of the time range managed by this shard.</param>
protected JobShard(string id, DateTimeOffset startTime, DateTimeOffset endTime)
{
Id = id;
StartTime = startTime;
EndTime = endTime;
_jobQueue = new InMemoryJobQueue();
}
/// <inheritdoc/>
public ValueTask<int> GetJobCountAsync() => ValueTask.FromResult(_jobQueue.Count);
/// <inheritdoc/>
public IAsyncEnumerable<IJobRunContext> ConsumeDurableJobsAsync()
{
return _jobQueue;
}
/// <inheritdoc/>
public async Task<DurableJobMutationResult> TryStartAttemptAsync(
IJobRunContext jobContext,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(jobContext);
await _mutationLock.WaitAsync(cancellationToken);
try
{
return _jobQueue.ContainsJob(jobContext.Job.Id)
? DurableJobMutationResult.Applied
: DurableJobMutationResult.JobNotFound;
}
finally
{
_mutationLock.Release();
}
}
/// <inheritdoc/>
public async Task<DurableJob?> TryScheduleJobAsync(ScheduleJobRequest request, CancellationToken cancellationToken)
{
request.Validate();
await _mutationLock.WaitAsync(cancellationToken);
try
{
if (IsAddingCompleted)
{
return null;
}
if (request.DueTime < StartTime || request.DueTime > EndTime)
{
throw new ArgumentOutOfRangeException(nameof(request), "Scheduled time is out of shard bounds.");
}
if (request.JobId is { } requestedJobId
&& _jobQueue.TryGetJob(requestedJobId, out var existingJob))
{
if (!request.Matches(existingJob!))
{
throw new InvalidOperationException(
$"Durable job ID '{requestedJobId}' is already scheduled with different properties.");
}
return existingJob;
}
var job = request.CreateJob(Id);
await PersistAddJobAsync(job, cancellationToken);
_jobQueue.Enqueue(job, 0);
return job;
}
finally
{
_mutationLock.Release();
}
}
/// <inheritdoc/>
public async Task<DurableJobMutationResult> RemoveJobAsync(string jobId, CancellationToken cancellationToken)
{
await _mutationLock.WaitAsync(cancellationToken);
try
{
if (!_jobQueue.ContainsJob(jobId))
{
return DurableJobMutationResult.JobNotFound;
}
await PersistRemoveJobAsync(jobId, cancellationToken);
return _jobQueue.RemoveJob(jobId)
? DurableJobMutationResult.Applied
: DurableJobMutationResult.JobNotFound;
}
finally
{
_mutationLock.Release();
}
}
/// <inheritdoc/>
public async Task MarkAsCompleteAsync(CancellationToken cancellationToken)
{
await _mutationLock.WaitAsync(cancellationToken);
try
{
IsAddingCompleted = true;
_jobQueue.MarkAsComplete();
}
finally
{
_mutationLock.Release();
}
}
/// <inheritdoc/>
public async Task<DurableJobMutationResult> RetryJobLaterAsync(
IJobRunContext jobContext,
DateTimeOffset newDueTime,
CancellationToken cancellationToken)
{
await _mutationLock.WaitAsync(cancellationToken);
try
{
if (!_jobQueue.ContainsJob(jobContext.Job.Id))
{
return DurableJobMutationResult.JobNotFound;
}
await PersistRetryJobAsync(jobContext, newDueTime, cancellationToken);
return _jobQueue.RetryJobLater(jobContext.Job.Id, newDueTime, jobContext.DequeueCount)
? DurableJobMutationResult.Applied
: DurableJobMutationResult.JobNotFound;
}
finally
{
_mutationLock.Release();
}
}
/// <inheritdoc/>
public async Task<DurableJobMutationResult> RescheduleJobAsync(
IJobRunContext jobContext,
DateTimeOffset newDueTime,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(jobContext);
await _mutationLock.WaitAsync(cancellationToken);
try
{
if (!_jobQueue.ContainsJob(jobContext.Job.Id))
{
return DurableJobMutationResult.JobNotFound;
}
var executionGeneration = checked(jobContext.Job.ExecutionGeneration + 1);
var resetContext = new JobRunContext(
jobContext.Job.WithExecutionGeneration(executionGeneration),
jobContext.RunId,
retryCount: 0);
await PersistRetryJobAsync(resetContext, newDueTime, cancellationToken);
return _jobQueue.RetryJobLater(jobContext.Job.Id, newDueTime, dequeueCount: 0, executionGeneration)
? DurableJobMutationResult.Applied
: DurableJobMutationResult.JobNotFound;
}
finally
{
_mutationLock.Release();
}
}
/// <summary>
/// Enqueues a job into the in-memory queue with the specified dequeue count.
/// </summary>
/// <param name="job">The job to enqueue.</param>
/// <param name="dequeueCount">The number of times this job has been dequeued.</param>
protected void EnqueueJob(DurableJob job, int dequeueCount)
{
_jobQueue.Enqueue(job, dequeueCount);
}
/// <summary>
/// Persists the addition of a new job to the underlying storage.
/// </summary>
/// <param name="job">The job to persist.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
protected abstract Task PersistAddJobAsync(DurableJob job, CancellationToken cancellationToken);
/// <summary>
/// Persists the removal of a job from the underlying storage.
/// </summary>
/// <param name="jobId">The unique identifier of the job to remove.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
protected abstract Task PersistRemoveJobAsync(string jobId, CancellationToken cancellationToken);
/// <summary>
/// Persists the rescheduling of a job to the underlying storage.
/// </summary>
/// <param name="jobContext">The execution context of the job to retry.</param>
/// <param name="newDueTime">
/// The new due time for the job. This value supersedes <see cref="DurableJob.DueTime"/> on
/// <see cref="IJobRunContext.Job"/>.
/// </param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
protected abstract Task PersistRetryJobAsync(IJobRunContext jobContext, DateTimeOffset newDueTime, CancellationToken cancellationToken);
/// <inheritdoc/>
public virtual ValueTask DisposeAsync()
{
GC.SuppressFinalize(this);
return default;
}
}