Skip to content

Commit 96b072b

Browse files
committed
fix(redis-worker): rank fair-queue tenants by age, not raw timestamp
selectTopTenantQueues weighted tenants by the average of their queue scores, but a score is the oldest-message timestamp (lower means older). That ranked newer tenants higher and, since timestamps are all close in magnitude, made the weights nearly identical. Weight by age (now - score) so the tenants waiting the longest are prioritized.
1 parent d37d5ae commit 96b072b

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/redis-worker": patch
3+
---
4+
5+
Fix fair queue tenant selection so that, when a maximum tenant count is set, the tenants that have been waiting the longest are picked first instead of being ranked by their raw timestamp.

packages/redis-worker/src/fair-queue/schedulers/weighted.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class WeightedScheduler extends BaseScheduler {
150150

151151
// Apply maximum tenant count if configured
152152
if (this.maximumTenantCount > 0) {
153-
rawQueues = this.#selectTopTenantQueues(rawQueues);
153+
rawQueues = this.#selectTopTenantQueues(rawQueues, now);
154154
}
155155

156156
// Build tenant data
@@ -230,7 +230,7 @@ export class WeightedScheduler extends BaseScheduler {
230230
return queues;
231231
}
232232

233-
#selectTopTenantQueues(queues: QueueWithScore[]): QueueWithScore[] {
233+
#selectTopTenantQueues(queues: QueueWithScore[], now: number): QueueWithScore[] {
234234
// Group by tenant and calculate average age
235235
const queuesByTenant = new Map<string, QueueWithScore[]>();
236236
for (const queue of queues) {
@@ -239,9 +239,11 @@ export class WeightedScheduler extends BaseScheduler {
239239
queuesByTenant.set(queue.tenantId, tenantQueues);
240240
}
241241

242-
// Calculate average age per tenant
242+
// Calculate average age per tenant. A queue's score is its oldest message
243+
// timestamp, so age is now - score. Older queues have a higher age and
244+
// should get more weight when we pick the top tenants.
243245
const tenantAges = Array.from(queuesByTenant.entries()).map(([tenantId, tQueues]) => {
244-
const avgAge = tQueues.reduce((sum, q) => sum + q.score, 0) / tQueues.length;
246+
const avgAge = tQueues.reduce((sum, q) => sum + (now - q.score), 0) / tQueues.length;
245247
return { tenantId, avgAge };
246248
});
247249

0 commit comments

Comments
 (0)