-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathlocking.ts
More file actions
576 lines (506 loc) · 17.7 KB
/
locking.ts
File metadata and controls
576 lines (506 loc) · 17.7 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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// import { default: Redlock } from "redlock";
const { default: Redlock } = require("redlock");
import { AsyncLocalStorage } from "async_hooks";
import { Redis } from "@internal/redis";
import * as redlock from "redlock";
import { tryCatch } from "@trigger.dev/core";
import { Logger } from "@trigger.dev/core/logger";
import {
startSpan,
Tracer,
Meter,
getMeter,
ValueType,
ObservableResult,
Attributes,
Histogram,
} from "@internal/tracing";
const SemanticAttributes = {
LOCK_TYPE: "run_engine.lock.type",
LOCK_RESOURCES: "run_engine.lock.resources",
LOCK_SUCCESS: "run_engine.lock.success",
};
export class LockAcquisitionTimeoutError extends Error {
constructor(
public readonly resources: string[],
public readonly totalWaitTime: number,
public readonly attempts: number,
message?: string
) {
super(
message ||
`Failed to acquire lock on resources [${resources.join(
", "
)}] after ${totalWaitTime}ms and ${attempts} attempts`
);
this.name = "LockAcquisitionTimeoutError";
}
}
interface LockContext {
resources: string;
signal: redlock.RedlockAbortSignal;
lockType: string;
}
interface ManualLockContext {
lock: redlock.Lock;
timeout: NodeJS.Timeout | null | undefined;
extension: Promise<void> | undefined;
}
export interface LockRetryConfig {
/** Maximum number of locking attempts (default: 10) */
maxAttempts?: number;
/** Initial delay in milliseconds (default: 200) */
baseDelay?: number;
/** Maximum delay cap in milliseconds (default: 5000) */
maxDelay?: number;
/** Exponential backoff multiplier (default: 1.5) */
backoffMultiplier?: number;
/** Jitter factor as percentage (default: 0.1 for 10%) */
jitterFactor?: number;
/** Maximum total wait time in milliseconds (default: 30000) */
maxTotalWaitTime?: number;
}
export class RunLocker {
private redlock: InstanceType<typeof redlock.default>;
private asyncLocalStorage: AsyncLocalStorage<LockContext>;
private logger: Logger;
private tracer: Tracer;
private meter: Meter;
private activeLocks: Map<string, { lockType: string; resources: string[] }> = new Map();
private activeManualContexts: Map<string, ManualLockContext> = new Map();
private lockDurationHistogram: Histogram;
private retryConfig: Required<LockRetryConfig>;
private duration: number;
private automaticExtensionThreshold: number;
constructor(options: {
redis: Redis;
logger: Logger;
tracer: Tracer;
meter?: Meter;
duration?: number;
automaticExtensionThreshold?: number;
retryConfig?: LockRetryConfig;
}) {
// Initialize configuration values
this.duration = options.duration ?? 5000;
this.automaticExtensionThreshold = options.automaticExtensionThreshold ?? 500;
this.redlock = new Redlock([options.redis], {
retryCount: 0, // Disable Redlock's internal retrying - we handle retries ourselves
});
this.asyncLocalStorage = new AsyncLocalStorage<LockContext>();
this.logger = options.logger;
this.tracer = options.tracer;
this.meter = options.meter ?? getMeter("run-engine");
// Initialize retry configuration with defaults
this.retryConfig = {
maxAttempts: options.retryConfig?.maxAttempts ?? 10,
baseDelay: options.retryConfig?.baseDelay ?? 200,
maxDelay: options.retryConfig?.maxDelay ?? 5000,
backoffMultiplier: options.retryConfig?.backoffMultiplier ?? 1.5,
jitterFactor: options.retryConfig?.jitterFactor ?? 0.1,
maxTotalWaitTime: options.retryConfig?.maxTotalWaitTime ?? 30000,
};
const activeLocksObservableGauge = this.meter.createObservableGauge("run_engine.locks.active", {
description: "The number of active locks by type",
unit: "locks",
valueType: ValueType.INT,
});
const lockDurationHistogram = this.meter.createHistogram("run_engine.lock.duration", {
description: "The duration of lock operations",
unit: "ms",
valueType: ValueType.DOUBLE,
});
activeLocksObservableGauge.addCallback(this.#updateActiveLocksCount.bind(this));
this.lockDurationHistogram = lockDurationHistogram;
}
async #updateActiveLocksCount(observableResult: ObservableResult<Attributes>) {
// Group active locks by type
const lockCountsByType = new Map<string, number>();
for (const [_, lockInfo] of this.activeLocks) {
const count = lockCountsByType.get(lockInfo.lockType) || 0;
lockCountsByType.set(lockInfo.lockType, count + 1);
}
// Report metrics for each lock type
for (const [lockType, count] of lockCountsByType) {
observableResult.observe(count, {
[SemanticAttributes.LOCK_TYPE]: lockType,
});
}
}
/** Locks resources using RedLock. It won't lock again if we're already inside a lock with the same resources. */
async lock<T>(
name: string,
resources: string[],
routine: () => Promise<T>,
attributes?: Attributes
): Promise<T> {
const currentContext = this.asyncLocalStorage.getStore();
const joinedResources = [...resources].sort().join(",");
return startSpan(
this.tracer,
"RunLocker.lock",
async (span) => {
if (currentContext && currentContext.resources === joinedResources) {
span.setAttribute("nested", true);
// We're already inside a lock with the same resources, just run the routine
return routine();
}
span.setAttribute("nested", false);
// Different resources or not in a lock, proceed with new lock
const lockId = `${name}:${joinedResources}:${Date.now()}`;
const lockStartTime = performance.now();
const [error, result] = await tryCatch(
this.#acquireAndExecute(name, resources, this.duration, routine, lockId, lockStartTime)
);
if (error) {
// Record failed lock acquisition
const lockDuration = performance.now() - lockStartTime;
this.lockDurationHistogram.record(lockDuration, {
[SemanticAttributes.LOCK_TYPE]: name,
[SemanticAttributes.LOCK_SUCCESS]: "false",
});
this.logger.error("[RunLocker] Error locking resources", {
error,
resources,
duration: this.duration,
});
throw error;
}
return result;
},
{
attributes: { name, resources, timeout: this.duration, ...attributes },
}
);
}
/** Manual lock acquisition with exponential backoff retry logic */
async #acquireAndExecute<T>(
name: string,
resources: string[],
duration: number,
routine: () => Promise<T>,
lockId: string,
lockStartTime: number
): Promise<T> {
// Sort resources to ensure consistent lock acquisition order and prevent deadlocks
const sortedResources = [...resources].sort();
const joinedResources = sortedResources.join(",");
// Use configured retry settings with exponential backoff
const { maxAttempts, baseDelay, maxDelay, backoffMultiplier, jitterFactor, maxTotalWaitTime } =
this.retryConfig;
// Track timing for total wait time limit
let totalWaitTime = 0;
// Retry the lock acquisition with exponential backoff
let lock: redlock.Lock | undefined;
let lastError: Error | undefined;
for (let attempt = 0; attempt <= maxAttempts; attempt++) {
const [error, acquiredLock] = await tryCatch(
startSpan(this.tracer, "RunLocker.acquireLock", async (span) => {
span.setAttributes({
resources: joinedResources,
attempt,
totalWaitTime,
});
return await this.redlock.acquire(sortedResources, duration);
})
);
if (!error && acquiredLock) {
lock = acquiredLock;
if (attempt > 0) {
this.logger.debug("[RunLocker] Lock acquired after retries", {
name,
resources: sortedResources,
attempts: attempt + 1,
totalWaitTime: Math.round(totalWaitTime),
});
}
break;
}
lastError = error instanceof Error ? error : new Error(String(error));
// Check if we've exceeded total wait time limit
if (totalWaitTime >= maxTotalWaitTime) {
this.logger.warn("[RunLocker] Lock acquisition exceeded total wait time limit", {
name,
resources: sortedResources,
attempts: attempt + 1,
totalWaitTime: Math.round(totalWaitTime),
maxTotalWaitTime,
});
throw new LockAcquisitionTimeoutError(
sortedResources,
Math.round(totalWaitTime),
attempt + 1,
`Lock acquisition on resources [${sortedResources.join(
", "
)}] exceeded total wait time limit of ${maxTotalWaitTime}ms`
);
}
// If this is the last attempt, throw timeout error
if (attempt === maxAttempts) {
this.logger.warn("[RunLocker] Lock acquisition exhausted all retries", {
name,
resources: sortedResources,
attempts: attempt + 1,
totalWaitTime: Math.round(totalWaitTime),
lastError: lastError.message,
});
throw new LockAcquisitionTimeoutError(
sortedResources,
Math.round(totalWaitTime),
attempt + 1,
`Lock acquisition on resources [${sortedResources.join(", ")}] failed after ${
attempt + 1
} attempts`
);
}
// Check if it's a retryable error (lock contention)
// ExecutionError: General redlock failure (including lock contention)
// ResourceLockedError: Specific lock contention error (if thrown)
const isRetryableError =
error && (error.name === "ResourceLockedError" || error.name === "ExecutionError");
if (isRetryableError) {
// Calculate exponential backoff delay with jitter and cap
const exponentialDelay = Math.min(
baseDelay * Math.pow(backoffMultiplier, attempt),
maxDelay
);
const jitter = exponentialDelay * jitterFactor * (Math.random() * 2 - 1); // ±jitterFactor% jitter
const delay = Math.min(maxDelay, Math.max(0, Math.round(exponentialDelay + jitter)));
// Update total wait time before delay
totalWaitTime += delay;
this.logger.debug("[RunLocker] Lock acquisition failed, retrying with backoff", {
name,
resources: sortedResources,
attempt: attempt + 1,
delay,
totalWaitTime: Math.round(totalWaitTime),
error: error.message,
errorName: error.name,
});
await new Promise((resolve) => setTimeout(resolve, delay));
continue;
}
// For other errors (non-retryable), throw immediately
this.logger.error("[RunLocker] Lock acquisition failed with non-retryable error", {
name,
resources: sortedResources,
attempt: attempt + 1,
error: lastError.message,
errorName: lastError.name,
});
throw lastError;
}
// Safety guard: ensure lock was successfully acquired
if (!lock) {
this.logger.error("[RunLocker] Lock was not acquired despite completing retry loop", {
name,
resources: sortedResources,
maxAttempts,
totalWaitTime: Math.round(totalWaitTime),
lastError: lastError?.message,
});
throw new LockAcquisitionTimeoutError(
sortedResources,
Math.round(totalWaitTime),
maxAttempts + 1,
`Lock acquisition on resources [${sortedResources.join(", ")}] failed unexpectedly`
);
}
// Create an AbortController for our signal
const controller = new AbortController();
const signal = controller.signal as redlock.RedlockAbortSignal;
const manualContext: ManualLockContext = {
lock,
timeout: undefined,
extension: undefined,
};
// Track the manual context for cleanup
this.activeManualContexts.set(lockId, manualContext);
// Set up auto-extension starting from when lock was actually acquired
this.#setupAutoExtension(manualContext, duration, signal, controller);
try {
const newContext: LockContext = {
resources: joinedResources,
signal,
lockType: name,
};
// Track active lock
this.activeLocks.set(lockId, {
lockType: name,
resources: sortedResources,
});
let lockSuccess = true;
try {
const result = await this.asyncLocalStorage.run(newContext, async () => {
return routine();
});
return result;
} catch (lockError) {
lockSuccess = false;
throw lockError;
} finally {
// Record lock duration
const lockDuration = performance.now() - lockStartTime;
this.lockDurationHistogram.record(lockDuration, {
[SemanticAttributes.LOCK_TYPE]: name,
[SemanticAttributes.LOCK_SUCCESS]: lockSuccess.toString(),
});
// Remove from active locks when done
this.activeLocks.delete(lockId);
}
} finally {
// Remove from active manual contexts
this.activeManualContexts.delete(lockId);
// Clean up extension mechanism - this ensures auto extension stops after routine finishes
this.#cleanupExtension(manualContext);
// Release the lock using tryCatch
const [releaseError] = await tryCatch(
startSpan(this.tracer, "RunLocker.releaseLock", async (span) => {
span.setAttributes({
resources: joinedResources,
lockValue: lock.value,
});
return await lock.release();
})
);
if (releaseError) {
this.logger.warn("[RunLocker] Error releasing lock", {
error: releaseError,
resources: sortedResources,
lockValue: lock.value,
});
}
}
}
/** Set up automatic lock extension */
#setupAutoExtension(
context: ManualLockContext,
duration: number,
signal: redlock.RedlockAbortSignal,
controller: AbortController
): void {
if (this.automaticExtensionThreshold > duration - 100) {
// Don't set up auto-extension if duration is too short
return;
}
const scheduleExtension = (): void => {
const timeUntilExtension =
context.lock.expiration - Date.now() - this.automaticExtensionThreshold;
if (timeUntilExtension > 0) {
// Check for cleanup immediately before scheduling to prevent race condition
if (context.timeout !== null) {
context.timeout = setTimeout(() => {
context.extension = this.#extendLock(
context,
duration,
signal,
controller,
scheduleExtension
);
}, timeUntilExtension);
}
}
};
scheduleExtension();
}
/** Extend a lock */
async #extendLock(
context: ManualLockContext,
duration: number,
signal: redlock.RedlockAbortSignal,
controller: AbortController,
scheduleNext: () => void
): Promise<void> {
// Check if cleanup has started before proceeding
if (context.timeout === null) {
return;
}
context.timeout = undefined;
const [error, newLock] = await tryCatch(context.lock.extend(duration));
if (!error && newLock) {
context.lock = newLock;
// Schedule next extension (cleanup check is now inside scheduleNext)
scheduleNext();
} else {
if (context.lock.expiration > Date.now()) {
// If lock hasn't expired yet, schedule a retry instead of recursing
// This prevents stack overflow from repeated extension failures
if (context.timeout !== null) {
const retryDelay = 100; // Short delay before retry
context.timeout = setTimeout(() => {
context.extension = this.#extendLock(
context,
duration,
signal,
controller,
scheduleNext
);
}, retryDelay);
}
} else {
// Lock has expired, abort the signal
signal.error = error instanceof Error ? error : new Error(String(error));
controller.abort();
}
}
}
/** Clean up extension mechanism */
#cleanupExtension(context: ManualLockContext): void {
// Signal that we're cleaning up by setting timeout to null
if (context.timeout) {
clearTimeout(context.timeout);
}
context.timeout = null;
// Wait for any in-flight extension to complete
if (context.extension) {
context.extension.catch(() => {
// Ignore errors during cleanup
});
}
}
async lockIf<T>(
condition: boolean,
name: string,
resources: string[],
routine: () => Promise<T>
): Promise<T> {
if (condition) {
return this.lock(name, resources, routine);
} else {
return routine();
}
}
isInsideLock(): boolean {
return !!this.asyncLocalStorage.getStore();
}
getCurrentResources(): string | undefined {
return this.asyncLocalStorage.getStore()?.resources;
}
getRetryConfig(): Readonly<Required<LockRetryConfig>> {
return { ...this.retryConfig };
}
getDuration(): number {
return this.duration;
}
getAutomaticExtensionThreshold(): number {
return this.automaticExtensionThreshold;
}
async quit() {
// Clean up all active manual contexts
for (const [lockId, context] of this.activeManualContexts) {
this.#cleanupExtension(context);
// Try to release any remaining locks
const [releaseError] = await tryCatch(context.lock.release());
if (releaseError) {
this.logger.warn("[RunLocker] Error releasing lock during quit", {
error: releaseError,
lockId,
lockValue: context.lock.value,
});
}
}
this.activeManualContexts.clear();
this.activeLocks.clear();
await this.redlock.quit();
}
}