-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathvisibility.ts
More file actions
530 lines (462 loc) · 14.9 KB
/
visibility.ts
File metadata and controls
530 lines (462 loc) · 14.9 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
import { createRedisClient, type Redis, type RedisOptions } from "@internal/redis";
import { jumpHash } from "@trigger.dev/core/v3/serverOnly";
import type { ClaimResult, FairQueueKeyProducer, InFlightMessage } from "./types.js";
export interface VisibilityManagerOptions {
redis: RedisOptions;
keys: FairQueueKeyProducer;
shardCount: number;
defaultTimeoutMs: number;
logger?: {
debug: (message: string, context?: Record<string, unknown>) => void;
error: (message: string, context?: Record<string, unknown>) => void;
};
}
/**
* VisibilityManager handles message visibility timeouts for safe message processing.
*
* Features:
* - Claim messages with visibility timeout
* - Heartbeat to extend timeout
* - Automatic reclaim of timed-out messages
* - Per-shard in-flight tracking
*
* Data structures:
* - In-flight sorted set: score = deadline timestamp, member = "{messageId}:{queueId}"
* - In-flight data hash: field = messageId, value = JSON message data
*/
export class VisibilityManager {
private redis: Redis;
private keys: FairQueueKeyProducer;
private shardCount: number;
private defaultTimeoutMs: number;
private logger: NonNullable<VisibilityManagerOptions["logger"]>;
constructor(private options: VisibilityManagerOptions) {
this.redis = createRedisClient(options.redis);
this.keys = options.keys;
this.shardCount = options.shardCount;
this.defaultTimeoutMs = options.defaultTimeoutMs;
this.logger = options.logger ?? {
debug: () => {},
error: () => {},
};
this.#registerCommands();
}
// ============================================================================
// Public Methods
// ============================================================================
/**
* Claim a message for processing.
* Moves the message from its queue to the in-flight set with a visibility timeout.
*
* @param queueId - The queue to claim from
* @param queueKey - The Redis key for the queue sorted set
* @param queueItemsKey - The Redis key for the queue items hash
* @param consumerId - ID of the consumer claiming the message
* @param timeoutMs - Visibility timeout in milliseconds
* @returns Claim result with the message if successful
*/
async claim<TPayload = unknown>(
queueId: string,
queueKey: string,
queueItemsKey: string,
consumerId: string,
timeoutMs?: number
): Promise<ClaimResult<TPayload>> {
const timeout = timeoutMs ?? this.defaultTimeoutMs;
const deadline = Date.now() + timeout;
const shardId = this.#getShardForQueue(queueId);
const inflightKey = this.keys.inflightKey(shardId);
const inflightDataKey = this.keys.inflightDataKey(shardId);
// Use Lua script to atomically:
// 1. Pop oldest message from queue
// 2. Add to in-flight set with deadline
// 3. Store message data
const result = await this.redis.claimMessage(
queueKey,
queueItemsKey,
inflightKey,
inflightDataKey,
queueId,
consumerId,
deadline.toString()
);
if (!result) {
return { claimed: false };
}
const [messageId, payloadJson] = result;
try {
const payload = JSON.parse(payloadJson) as TPayload;
const message: InFlightMessage<TPayload> = {
messageId,
queueId,
payload,
deadline,
consumerId,
};
this.logger.debug("Message claimed", {
messageId,
queueId,
consumerId,
deadline,
});
return { claimed: true, message };
} catch (error) {
// JSON parse error - message data is corrupted
this.logger.error("Failed to parse claimed message", {
messageId,
queueId,
error: error instanceof Error ? error.message : String(error),
});
// Remove the corrupted message from in-flight
await this.#removeFromInflight(shardId, messageId, queueId);
return { claimed: false };
}
}
/**
* Extend the visibility timeout for a message (heartbeat).
*
* @param messageId - The message ID
* @param queueId - The queue ID
* @param extendMs - Additional milliseconds to add to the deadline
* @returns true if the heartbeat was successful
*/
async heartbeat(messageId: string, queueId: string, extendMs: number): Promise<boolean> {
const shardId = this.#getShardForQueue(queueId);
const inflightKey = this.keys.inflightKey(shardId);
const member = this.#makeMember(messageId, queueId);
const newDeadline = Date.now() + extendMs;
// Use Lua script to atomically check existence and update score
// ZADD XX returns 0 even on successful updates, so we use a custom command
const result = await this.redis.heartbeatMessage(inflightKey, member, newDeadline.toString());
const success = result === 1;
if (success) {
this.logger.debug("Heartbeat successful", {
messageId,
queueId,
newDeadline,
});
}
return success;
}
/**
* Mark a message as successfully processed.
* Removes the message from in-flight tracking.
*
* @param messageId - The message ID
* @param queueId - The queue ID
*/
async complete(messageId: string, queueId: string): Promise<void> {
const shardId = this.#getShardForQueue(queueId);
await this.#removeFromInflight(shardId, messageId, queueId);
this.logger.debug("Message completed", {
messageId,
queueId,
});
}
/**
* Release a message back to its queue.
* Used when processing fails or consumer wants to retry later.
*
* @param messageId - The message ID
* @param queueId - The queue ID
* @param queueKey - The Redis key for the queue
* @param queueItemsKey - The Redis key for the queue items hash
* @param masterQueueKey - The Redis key for the master queue
* @param score - Optional score for the message (defaults to now)
*/
async release<TPayload = unknown>(
messageId: string,
queueId: string,
queueKey: string,
queueItemsKey: string,
masterQueueKey: string,
score?: number
): Promise<void> {
const shardId = this.#getShardForQueue(queueId);
const inflightKey = this.keys.inflightKey(shardId);
const inflightDataKey = this.keys.inflightDataKey(shardId);
const member = this.#makeMember(messageId, queueId);
const messageScore = score ?? Date.now();
// Use Lua script to atomically:
// 1. Get message data from in-flight
// 2. Remove from in-flight
// 3. Add back to queue
// 4. Update master queue to ensure queue is picked up
await this.redis.releaseMessage(
inflightKey,
inflightDataKey,
queueKey,
queueItemsKey,
masterQueueKey,
member,
messageId,
messageScore.toString(),
queueId
);
this.logger.debug("Message released", {
messageId,
queueId,
score: messageScore,
});
}
/**
* Reclaim timed-out messages from a shard.
* Returns messages to their original queues.
*
* @param shardId - The shard to check
* @param getQueueKeys - Function to get queue keys for a queue ID
* @returns Number of messages reclaimed
*/
async reclaimTimedOut(
shardId: number,
getQueueKeys: (queueId: string) => {
queueKey: string;
queueItemsKey: string;
masterQueueKey: string;
}
): Promise<number> {
const inflightKey = this.keys.inflightKey(shardId);
const inflightDataKey = this.keys.inflightDataKey(shardId);
const now = Date.now();
// Get all messages past their deadline
const timedOut = await this.redis.zrangebyscore(
inflightKey,
"-inf",
now,
"WITHSCORES",
"LIMIT",
0,
100 // Process in batches
);
let reclaimed = 0;
for (let i = 0; i < timedOut.length; i += 2) {
const member = timedOut[i];
const originalScore = timedOut[i + 1];
if (!member || !originalScore) {
continue;
}
const { messageId, queueId } = this.#parseMember(member);
const { queueKey, queueItemsKey, masterQueueKey } = getQueueKeys(queueId);
try {
// Re-add to queue with original score (or now if not available)
const score = parseFloat(originalScore) || now;
await this.redis.releaseMessage(
inflightKey,
inflightDataKey,
queueKey,
queueItemsKey,
masterQueueKey,
member,
messageId,
score.toString(),
queueId
);
reclaimed++;
this.logger.debug("Reclaimed timed-out message", {
messageId,
queueId,
originalScore,
});
} catch (error) {
this.logger.error("Failed to reclaim message", {
messageId,
queueId,
error: error instanceof Error ? error.message : String(error),
});
}
}
return reclaimed;
}
/**
* Get all in-flight messages for a shard.
*/
async getInflightMessages(shardId: number): Promise<
Array<{
messageId: string;
queueId: string;
deadline: number;
}>
> {
const inflightKey = this.keys.inflightKey(shardId);
const results = await this.redis.zrange(inflightKey, 0, -1, "WITHSCORES");
const messages: Array<{ messageId: string; queueId: string; deadline: number }> = [];
for (let i = 0; i < results.length; i += 2) {
const member = results[i];
const deadlineStr = results[i + 1];
if (!member || !deadlineStr) {
continue;
}
const deadline = parseFloat(deadlineStr);
const { messageId, queueId } = this.#parseMember(member);
messages.push({ messageId, queueId, deadline });
}
return messages;
}
/**
* Get count of in-flight messages for a shard.
*/
async getInflightCount(shardId: number): Promise<number> {
const inflightKey = this.keys.inflightKey(shardId);
return await this.redis.zcard(inflightKey);
}
/**
* Get total in-flight count across all shards.
*/
async getTotalInflightCount(): Promise<number> {
const counts = await Promise.all(
Array.from({ length: this.shardCount }, (_, i) => this.getInflightCount(i))
);
return counts.reduce((sum, count) => sum + count, 0);
}
/**
* Close the Redis connection.
*/
async close(): Promise<void> {
await this.redis.quit();
}
// ============================================================================
// Private Methods
// ============================================================================
/**
* Map queue ID to shard using Jump Consistent Hash.
* Must use same algorithm as MasterQueue for consistency.
*/
#getShardForQueue(queueId: string): number {
return jumpHash(queueId, this.shardCount);
}
#makeMember(messageId: string, queueId: string): string {
return `${messageId}:${queueId}`;
}
#parseMember(member: string): { messageId: string; queueId: string } {
const colonIndex = member.indexOf(":");
if (colonIndex === -1) {
return { messageId: member, queueId: "" };
}
return {
messageId: member.substring(0, colonIndex),
queueId: member.substring(colonIndex + 1),
};
}
async #removeFromInflight(shardId: number, messageId: string, queueId: string): Promise<void> {
const inflightKey = this.keys.inflightKey(shardId);
const inflightDataKey = this.keys.inflightDataKey(shardId);
const member = this.#makeMember(messageId, queueId);
const pipeline = this.redis.pipeline();
pipeline.zrem(inflightKey, member);
pipeline.hdel(inflightDataKey, messageId);
await pipeline.exec();
}
#registerCommands(): void {
// Atomic claim: pop from queue, add to in-flight
this.redis.defineCommand("claimMessage", {
numberOfKeys: 4,
lua: `
local queueKey = KEYS[1]
local queueItemsKey = KEYS[2]
local inflightKey = KEYS[3]
local inflightDataKey = KEYS[4]
local queueId = ARGV[1]
local consumerId = ARGV[2]
local deadline = tonumber(ARGV[3])
-- Get oldest message from queue
local items = redis.call('ZRANGE', queueKey, 0, 0)
if #items == 0 then
return nil
end
local messageId = items[1]
-- Get message data
local payload = redis.call('HGET', queueItemsKey, messageId)
if not payload then
-- Message data missing, remove from queue and return nil
redis.call('ZREM', queueKey, messageId)
return nil
end
-- Remove from queue
redis.call('ZREM', queueKey, messageId)
redis.call('HDEL', queueItemsKey, messageId)
-- Add to in-flight set with deadline
local member = messageId .. ':' .. queueId
redis.call('ZADD', inflightKey, deadline, member)
-- Store message data for potential release
redis.call('HSET', inflightDataKey, messageId, payload)
return {messageId, payload}
`,
});
// Atomic release: remove from in-flight, add back to queue, update master queue
this.redis.defineCommand("releaseMessage", {
numberOfKeys: 5,
lua: `
local inflightKey = KEYS[1]
local inflightDataKey = KEYS[2]
local queueKey = KEYS[3]
local queueItemsKey = KEYS[4]
local masterQueueKey = KEYS[5]
local member = ARGV[1]
local messageId = ARGV[2]
local score = tonumber(ARGV[3])
local queueId = ARGV[4]
-- Get message data from in-flight
local payload = redis.call('HGET', inflightDataKey, messageId)
if not payload then
-- Message not in in-flight or already released
return 0
end
-- Remove from in-flight
redis.call('ZREM', inflightKey, member)
redis.call('HDEL', inflightDataKey, messageId)
-- Add back to queue
redis.call('ZADD', queueKey, score, messageId)
redis.call('HSET', queueItemsKey, messageId, payload)
-- Update master queue to ensure queue is picked up by consumers
-- Use the message score so older messages get priority
redis.call('ZADD', masterQueueKey, score, queueId)
return 1
`,
});
// Atomic heartbeat: check if member exists and update score
// ZADD XX returns 0 even on successful updates (it counts new additions only)
// So we need to check existence first with ZSCORE
this.redis.defineCommand("heartbeatMessage", {
numberOfKeys: 1,
lua: `
local inflightKey = KEYS[1]
local member = ARGV[1]
local newDeadline = tonumber(ARGV[2])
-- Check if member exists in the in-flight set
local score = redis.call('ZSCORE', inflightKey, member)
if not score then
return 0
end
-- Update the deadline
redis.call('ZADD', inflightKey, 'XX', newDeadline, member)
return 1
`,
});
}
}
// Extend Redis interface for custom commands
declare module "@internal/redis" {
interface RedisCommander<Context> {
claimMessage(
queueKey: string,
queueItemsKey: string,
inflightKey: string,
inflightDataKey: string,
queueId: string,
consumerId: string,
deadline: string
): Promise<[string, string] | null>;
releaseMessage(
inflightKey: string,
inflightDataKey: string,
queueKey: string,
queueItemsKey: string,
masterQueueKey: string,
member: string,
messageId: string,
score: string,
queueId: string
): Promise<number>;
heartbeatMessage(inflightKey: string, member: string, newDeadline: string): Promise<number>;
}
}