velocious 1.0.732 → 1.0.734
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/background-jobs/local-store.js +42 -20
- package/build/background-jobs/queue-priority-order.js +71 -19
- package/build/background-jobs/store.js +41 -20
- package/build/src/background-jobs/local-store.d.ts +19 -9
- package/build/src/background-jobs/local-store.d.ts.map +1 -1
- package/build/src/background-jobs/local-store.js +40 -20
- package/build/src/background-jobs/queue-priority-order.d.ts +48 -15
- package/build/src/background-jobs/queue-priority-order.d.ts.map +1 -1
- package/build/src/background-jobs/queue-priority-order.js +68 -20
- package/build/src/background-jobs/store.d.ts +19 -9
- package/build/src/background-jobs/store.d.ts.map +1 -1
- package/build/src/background-jobs/store.js +39 -20
- package/package.json +1 -1
- package/src/background-jobs/local-store.js +42 -20
- package/src/background-jobs/queue-priority-order.js +71 -19
- package/src/background-jobs/store.js +41 -20
package/package.json
CHANGED
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
rescheduledBackgroundJobAtMs,
|
|
22
22
|
retryDelayMs
|
|
23
23
|
} from "./job-semantics.js"
|
|
24
|
-
import {buildLaneFairnessOrderSql, buildQueuePriorityOrderSql} from "./queue-priority-order.js"
|
|
24
|
+
import {buildLaneFairnessOrderSql, buildLaneFairnessRankSql, buildQueuePriorityOrderSql, laneConcurrencyKey} from "./queue-priority-order.js"
|
|
25
25
|
|
|
26
26
|
export const LOCAL_BACKGROUND_JOBS_TABLE = "velocious_local_background_jobs"
|
|
27
27
|
export const LOCAL_BACKGROUND_JOB_CONCURRENCY_TABLE = "velocious_local_background_job_concurrency"
|
|
@@ -925,7 +925,9 @@ export default class LocalBackgroundJobsStore {
|
|
|
925
925
|
const nowMs = this.clock.now()
|
|
926
926
|
const jobsTable = db.quoteTable(LOCAL_BACKGROUND_JOBS_TABLE)
|
|
927
927
|
const concurrencyTable = db.quoteTable(LOCAL_BACKGROUND_JOB_CONCURRENCY_TABLE)
|
|
928
|
-
const
|
|
928
|
+
const laneFairnessRank = this._laneFairnessRankSql(db, nowMs)
|
|
929
|
+
const priorityOrder = this._queuePriorityOrderSql(db, nowMs, laneFairnessRank)
|
|
930
|
+
const laneFairnessOrder = this._laneFairnessOrderSql(db, nowMs)
|
|
929
931
|
let query = db
|
|
930
932
|
.newQuery()
|
|
931
933
|
.from(LOCAL_BACKGROUND_JOBS_TABLE)
|
|
@@ -941,12 +943,6 @@ export default class LocalBackgroundJobsStore {
|
|
|
941
943
|
if (priorityOrder) {
|
|
942
944
|
query = query.order(`${priorityOrder} DESC`)
|
|
943
945
|
|
|
944
|
-
// The aging term levels every aged lane onto the same ceiling rank, so at the
|
|
945
|
-
// ceiling the configured priority no longer separates lanes and the claim would
|
|
946
|
-
// fall through to `scheduled_at_ms` — permanently won by whichever lane holds the
|
|
947
|
-
// deepest aged backlog. Break that tie by lane service instead.
|
|
948
|
-
const laneFairnessOrder = this._laneFairnessOrderSql(db, nowMs)
|
|
949
|
-
|
|
950
946
|
if (laneFairnessOrder) query = query.order(laneFairnessOrder)
|
|
951
947
|
}
|
|
952
948
|
|
|
@@ -1040,7 +1036,7 @@ export default class LocalBackgroundJobsStore {
|
|
|
1040
1036
|
return null
|
|
1041
1037
|
}
|
|
1042
1038
|
|
|
1043
|
-
await this._recordLaneClaim(db, job.
|
|
1039
|
+
await this._recordLaneClaim(db, job.queue, handedOffAtMs)
|
|
1044
1040
|
|
|
1045
1041
|
return {handedOffAtMs, handoffId}
|
|
1046
1042
|
}))
|
|
@@ -1386,17 +1382,25 @@ export default class LocalBackgroundJobsStore {
|
|
|
1386
1382
|
}
|
|
1387
1383
|
|
|
1388
1384
|
/**
|
|
1389
|
-
* Stamps the instant a lane received a claim so {@link
|
|
1390
|
-
* aging-ceiling tie by lane service rather than by backlog age.
|
|
1391
|
-
*
|
|
1392
|
-
*
|
|
1385
|
+
* Stamps the instant a lane received a claim so {@link buildLaneFairnessRankSql} can break the
|
|
1386
|
+
* aging-ceiling tie by lane service rather than by backlog age. The lane is the queue: a job
|
|
1387
|
+
* claimed under an explicit `concurrencyKey` still consumed its queue's turn, and leaving that
|
|
1388
|
+
* claim unstamped made the tie-break term equal the row's own age again — which the deepest
|
|
1389
|
+
* aged backlog won every cycle.
|
|
1393
1390
|
* @param {import("../database/drivers/base.js").default} db - Connection.
|
|
1394
|
-
* @param {string | null}
|
|
1391
|
+
* @param {string | null} queue - Queue of the job that just claimed.
|
|
1395
1392
|
* @param {number} claimedAtMs - Claim instant.
|
|
1396
1393
|
* @returns {Promise<void>} - Resolves when the lane counter is stamped.
|
|
1397
1394
|
*/
|
|
1398
|
-
async _recordLaneClaim(db,
|
|
1399
|
-
|
|
1395
|
+
async _recordLaneClaim(db, queue, claimedAtMs) {
|
|
1396
|
+
const normalizedQueue = queue || DEFAULT_BACKGROUND_JOB_QUEUE
|
|
1397
|
+
const queues = this.configuration.getBackgroundJobsConfig().queues || {}
|
|
1398
|
+
const cap = queues[normalizedQueue]?.maxConcurrent
|
|
1399
|
+
|
|
1400
|
+
if (!Number.isInteger(cap) || Number(cap) <= 0) return
|
|
1401
|
+
|
|
1402
|
+
const concurrencyKey = laneConcurrencyKey(normalizedQueue)
|
|
1403
|
+
await this._ensureConcurrency(db, {concurrencyKey, maxConcurrency: Number(cap), queueDerived: true})
|
|
1400
1404
|
|
|
1401
1405
|
const table = db.quoteTable(LOCAL_BACKGROUND_JOB_CONCURRENCY_TABLE)
|
|
1402
1406
|
|
|
@@ -1461,15 +1465,33 @@ export default class LocalBackgroundJobsStore {
|
|
|
1461
1465
|
* {@link buildQueuePriorityOrderSql} for the ranking contract.
|
|
1462
1466
|
* @param {import("../database/drivers/base.js").default} db - Connection.
|
|
1463
1467
|
* @param {number} nowMs - Dispatch instant.
|
|
1468
|
+
* @param {string | null} laneFairnessRank - Raw lane fairness rank folded into the primary rank.
|
|
1464
1469
|
* @returns {string | null} - Queue priority expression.
|
|
1465
1470
|
*/
|
|
1466
|
-
_queuePriorityOrderSql(db, nowMs) {
|
|
1467
|
-
return buildQueuePriorityOrderSql({configuration: this.configuration, db, nowMs})
|
|
1471
|
+
_queuePriorityOrderSql(db, nowMs, laneFairnessRank) {
|
|
1472
|
+
return buildQueuePriorityOrderSql({configuration: this.configuration, db, nowMs, laneFairnessRank})
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
/**
|
|
1476
|
+
* Builds the lane-fairness rank folded into the primary queued-job claim rank. See
|
|
1477
|
+
* {@link buildLaneFairnessRankSql} for the ranking contract.
|
|
1478
|
+
* @param {import("../database/drivers/base.js").default} db - Connection.
|
|
1479
|
+
* @param {number} nowMs - Dispatch instant.
|
|
1480
|
+
* @returns {string | null} - Raw SQL `ORDER BY … DESC` term, or null when not applicable.
|
|
1481
|
+
*/
|
|
1482
|
+
_laneFairnessRankSql(db, nowMs) {
|
|
1483
|
+
return buildLaneFairnessRankSql({
|
|
1484
|
+
configuration: this.configuration,
|
|
1485
|
+
db,
|
|
1486
|
+
nowMs,
|
|
1487
|
+
jobsTable: db.quoteTable(LOCAL_BACKGROUND_JOBS_TABLE),
|
|
1488
|
+
concurrencyTable: db.quoteTable(LOCAL_BACKGROUND_JOB_CONCURRENCY_TABLE)
|
|
1489
|
+
})
|
|
1468
1490
|
}
|
|
1469
1491
|
|
|
1470
1492
|
/**
|
|
1471
|
-
* Builds the
|
|
1472
|
-
*
|
|
1493
|
+
* Builds the legacy fallback ordering for rows without a durable lane claim stamp. The
|
|
1494
|
+
* durable lane-service age is already part of the primary rank; this remains a tie-break only.
|
|
1473
1495
|
* @param {import("../database/drivers/base.js").default} db - Connection.
|
|
1474
1496
|
* @param {number} nowMs - Dispatch instant.
|
|
1475
1497
|
* @returns {string | null} - Raw SQL `ORDER BY … DESC` term, or null when not applicable.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
|
-
import {DEFAULT_BACKGROUND_JOB_QUEUE} from "./job-semantics.js"
|
|
3
|
+
import {DEFAULT_BACKGROUND_JOB_QUEUE, QUEUE_CONCURRENCY_KEY_PREFIX} from "./job-semantics.js"
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* How many milliseconds of eligible waiting cancel one unit of `queues[name].priority`.
|
|
@@ -45,24 +45,22 @@ export function queuePriorityAgingUnitMs(configuration) {
|
|
|
45
45
|
* non-empty.
|
|
46
46
|
*
|
|
47
47
|
* Aging adds the row's own eligible wait, scaled by the priority unit, and caps that wait per
|
|
48
|
-
* lane at the gap between the lane and the highest configured priority plus one unit. The
|
|
49
|
-
* is
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* waiting, and a lane can never outrank a lane that is itself still below the ceiling. At the
|
|
53
|
-
* ceiling all lanes are equal, so the claim falls back to `scheduled_at_ms` and the oldest
|
|
54
|
-
* overdue row is taken first.
|
|
48
|
+
* lane at the gap between the lane and the highest configured priority plus one unit. The lane
|
|
49
|
+
* service age is folded into this same primary rank, so a lane that has gone without a claim can
|
|
50
|
+
* cross the global ceiling before its own row-age cap. The queue-keyed service term remains
|
|
51
|
+
* constant within a lane, preserving the existing intra-lane ordering and ceiling alternation.
|
|
55
52
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* the
|
|
53
|
+
* Without a prior lane claim, the legacy row-age behavior remains the fallback. After a lane has
|
|
54
|
+
* a durable claim stamp, its service age contributes directly to the rank, reducing the bound
|
|
55
|
+
* from the full priority cap whenever another lane holds a deep aged backlog.
|
|
59
56
|
* @param {object} args - Options.
|
|
60
57
|
* @param {import("../configuration.js").default} args.configuration - Active configuration.
|
|
61
58
|
* @param {import("../database/drivers/base.js").default} args.db - Connection.
|
|
62
59
|
* @param {number} args.nowMs - Dispatch instant.
|
|
60
|
+
* @param {string | null} [args.laneFairnessRank] - Raw lane fairness rank to fold into the primary rank.
|
|
63
61
|
* @returns {string | null} - Raw SQL rank expression, or null when no queue is prioritized.
|
|
64
62
|
*/
|
|
65
|
-
export function buildQueuePriorityOrderSql({configuration, db, nowMs}) {
|
|
63
|
+
export function buildQueuePriorityOrderSql({configuration, db, nowMs, laneFairnessRank}) {
|
|
66
64
|
const queues = configuration.getBackgroundJobsConfig().queues || {}
|
|
67
65
|
/** @type {Array<[string, number]>} */
|
|
68
66
|
const prioritized = []
|
|
@@ -90,9 +88,21 @@ export function buildQueuePriorityOrderSql({configuration, db, nowMs}) {
|
|
|
90
88
|
const agingCap = `CASE ${queueExpression} ${prioritized.map(([queue, priority]) => `WHEN ${db.quote(queue)} THEN ${(highestPriority - priority + 1) * unitMs}`).join(" ")} ELSE ${highestCeilingMs} END`
|
|
91
89
|
const scheduledAtColumn = db.quoteColumn("scheduled_at_ms")
|
|
92
90
|
|
|
91
|
+
const laneFairness = laneFairnessRank ? ` + (${laneFairnessRank})` : ""
|
|
92
|
+
|
|
93
93
|
return `(${priorityRank} * ${unitMs} + ` +
|
|
94
94
|
`CASE WHEN ${scheduledAtColumn} <= (${db.quote(nowMs)} - ${agingCap}) THEN ${agingCap} ` +
|
|
95
|
-
`ELSE (${db.quote(nowMs)} - ${scheduledAtColumn}) END)`
|
|
95
|
+
`ELSE (${db.quote(nowMs)} - ${scheduledAtColumn}) END${laneFairness})`
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The counter row that represents a lane: the queue's own concurrency counter. An explicit
|
|
100
|
+
* `concurrencyKey` groups unrelated jobs, so it never names a lane.
|
|
101
|
+
* @param {string} queue - Normalized queue name.
|
|
102
|
+
* @returns {string} - Queue-derived concurrency key.
|
|
103
|
+
*/
|
|
104
|
+
export function laneConcurrencyKey(queue) {
|
|
105
|
+
return `${QUEUE_CONCURRENCY_KEY_PREFIX}${queue}`
|
|
96
106
|
}
|
|
97
107
|
|
|
98
108
|
/**
|
|
@@ -106,10 +116,17 @@ export function buildQueuePriorityOrderSql({configuration, db, nowMs}) {
|
|
|
106
116
|
* when that cap is far above what the fleet can actually run. Ordering by time-since-last-claim
|
|
107
117
|
* makes the ceiling tie alternate between starved lanes.
|
|
108
118
|
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
119
|
+
* The lane is the queue, not the candidate's concurrency counter. Most of a busy lane's rows can
|
|
120
|
+
* sit under explicit per-group keys (TensorBuzz groups its `builds` backlog by
|
|
121
|
+
* `cleanup-build-run-output-<build group id>`), and those keys are deliberately not stamped —
|
|
122
|
+
* reading them would make the term equal the row's own age again and hand the tie back to the
|
|
123
|
+
* deepest backlog, which is the monopoly this term exists to break. Every row of a queue
|
|
124
|
+
* therefore reads its queue's counter, which keeps the term constant within a lane and leaves
|
|
125
|
+
* intra-lane order untouched.
|
|
126
|
+
*
|
|
127
|
+
* A queue with no counter row (no configured `maxConcurrent`, or a queue name the configuration
|
|
128
|
+
* does not declare), and a counter whose stamp is NULL, fall back to the row's own eligible wait,
|
|
129
|
+
* which reproduces the previous `scheduled_at_ms ASC` ordering exactly.
|
|
113
130
|
*
|
|
114
131
|
* Returns null when aging is opted out (`queuePriorityAgingUnitMs: null`): opting out asks for
|
|
115
132
|
* strict queue-priority ordering, so no time-based reordering is applied.
|
|
@@ -124,10 +141,45 @@ export function buildQueuePriorityOrderSql({configuration, db, nowMs}) {
|
|
|
124
141
|
export function buildLaneFairnessOrderSql({configuration, db, nowMs, jobsTable, concurrencyTable}) {
|
|
125
142
|
if (queuePriorityAgingUnitMs(configuration) === null) return null
|
|
126
143
|
|
|
127
|
-
const
|
|
144
|
+
const queues = configuration.getBackgroundJobsConfig().queues || {}
|
|
145
|
+
const queueExpression = `COALESCE(${jobsTable}.${db.quoteColumn("queue")}, ${db.quote(DEFAULT_BACKGROUND_JOB_QUEUE)})`
|
|
146
|
+
const laneKeyCase = `CASE ${queueExpression} ${Object.keys(queues).map((queue) => `WHEN ${db.quote(queue)} THEN ${db.quote(laneConcurrencyKey(queue))}`).join(" ")} ELSE NULL END`
|
|
128
147
|
const lastClaimedLookup =
|
|
129
148
|
`(SELECT ${concurrencyTable}.${db.quoteColumn("last_claimed_at_ms")} FROM ${concurrencyTable} ` +
|
|
130
|
-
`WHERE ${concurrencyTable}.${
|
|
149
|
+
`WHERE ${concurrencyTable}.${db.quoteColumn("concurrency_key")} = ${laneKeyCase})`
|
|
131
150
|
|
|
132
151
|
return `(${Number(nowMs)} - COALESCE(${lastClaimedLookup}, ${jobsTable}.${db.quoteColumn("scheduled_at_ms")})) DESC`
|
|
133
152
|
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Builds the raw lane-fairness rank used to compose the primary queue rank. A configured capped
|
|
156
|
+
* lane without a claim stamp receives the full global ceiling boost as never-served work; a lane
|
|
157
|
+
* without a durable queue counter contributes zero and keeps the historical row-age fallback.
|
|
158
|
+
* A long-ago claim stamp contributes its full starvation age to the primary rank.
|
|
159
|
+
* @param {object} args - Options.
|
|
160
|
+
* @param {import("../configuration.js").default} args.configuration - Active configuration.
|
|
161
|
+
* @param {import("../database/drivers/base.js").default} args.db - Connection.
|
|
162
|
+
* @param {number} args.nowMs - Dispatch instant.
|
|
163
|
+
* @param {string} args.jobsTable - Quoted jobs table name.
|
|
164
|
+
* @param {string} args.concurrencyTable - Quoted concurrency counter table name.
|
|
165
|
+
* @returns {string | null} - Raw SQL lane fairness rank, or null when not applicable.
|
|
166
|
+
*/
|
|
167
|
+
export function buildLaneFairnessRankSql({configuration, db, nowMs, jobsTable, concurrencyTable}) {
|
|
168
|
+
const unitMs = queuePriorityAgingUnitMs(configuration)
|
|
169
|
+
if (unitMs === null) return null
|
|
170
|
+
|
|
171
|
+
const queues = configuration.getBackgroundJobsConfig().queues || {}
|
|
172
|
+
const highestPriority = Math.max(0, ...Object.values(queues).map((queueConfig) => Number.isFinite(queueConfig?.priority) ? Number(queueConfig.priority) : 0))
|
|
173
|
+
const neverServedBoost = (highestPriority + 1) * unitMs
|
|
174
|
+
const queueExpression = `COALESCE(${jobsTable}.${db.quoteColumn("queue")}, ${db.quote(DEFAULT_BACKGROUND_JOB_QUEUE)})`
|
|
175
|
+
const cappedQueues = Object.entries(queues).filter(([, queueConfig]) => Number.isInteger(queueConfig?.maxConcurrent) && Number(queueConfig.maxConcurrent) > 0)
|
|
176
|
+
// Only queues with a `maxConcurrent` get a durable lane counter. With none configured there is
|
|
177
|
+
// no counter row to read, and a `CASE` with no `WHEN` arms is invalid SQL, so no lane term applies.
|
|
178
|
+
if (cappedQueues.length === 0) return null
|
|
179
|
+
const laneKeyCase = `CASE ${queueExpression} ${cappedQueues.map(([queue]) => `WHEN ${db.quote(queue)} THEN ${db.quote(laneConcurrencyKey(queue))}`).join(" ")} ELSE NULL END`
|
|
180
|
+
const lastClaimedLookup =
|
|
181
|
+
`(SELECT ${concurrencyTable}.${db.quoteColumn("last_claimed_at_ms")} FROM ${concurrencyTable} ` +
|
|
182
|
+
`WHERE ${concurrencyTable}.${db.quoteColumn("concurrency_key")} = ${laneKeyCase})`
|
|
183
|
+
|
|
184
|
+
return `(CASE WHEN ${laneKeyCase} IS NULL THEN 0 WHEN ${lastClaimedLookup} IS NULL THEN ${neverServedBoost} ELSE (${Number(nowMs)} - ${lastClaimedLookup}) END)`
|
|
185
|
+
}
|
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
rescheduledBackgroundJobAtMs,
|
|
28
28
|
retryDelayMs
|
|
29
29
|
} from "./job-semantics.js"
|
|
30
|
-
import {buildLaneFairnessOrderSql, buildQueuePriorityOrderSql} from "./queue-priority-order.js"
|
|
30
|
+
import {buildLaneFairnessOrderSql, buildLaneFairnessRankSql, buildQueuePriorityOrderSql, laneConcurrencyKey} from "./queue-priority-order.js"
|
|
31
31
|
import {
|
|
32
32
|
MAIL_DELIVERY_OPERATIONS_TABLE,
|
|
33
33
|
mailDeliveryOperationForJob,
|
|
@@ -1216,17 +1216,13 @@ export default class BackgroundJobsStore extends BackgroundJobsAdapter {
|
|
|
1216
1216
|
if (executionMode) query = this._whereExecutionMode({db, executionMode, query})
|
|
1217
1217
|
|
|
1218
1218
|
if (scheduledAtOperator === "<=") {
|
|
1219
|
-
const
|
|
1219
|
+
const laneFairnessRank = this._laneFairnessRankSql(db, now)
|
|
1220
|
+
const priorityOrder = this._queuePriorityOrderSql(db, now, laneFairnessRank)
|
|
1221
|
+
const laneFairnessOrder = this._laneFairnessOrderSql(db, now)
|
|
1220
1222
|
|
|
1221
1223
|
if (priorityOrder) {
|
|
1222
1224
|
query = query.order(`${priorityOrder} DESC`)
|
|
1223
1225
|
|
|
1224
|
-
// The aging term levels every aged lane onto the same ceiling rank, so at the
|
|
1225
|
-
// ceiling the configured priority no longer separates lanes and the claim would
|
|
1226
|
-
// fall through to `scheduled_at_ms` — permanently won by whichever lane holds the
|
|
1227
|
-
// deepest aged backlog. Break that tie by lane service instead.
|
|
1228
|
-
const laneFairnessOrder = this._laneFairnessOrderSql(db, now)
|
|
1229
|
-
|
|
1230
1226
|
if (laneFairnessOrder) query = query.order(laneFairnessOrder)
|
|
1231
1227
|
}
|
|
1232
1228
|
|
|
@@ -1263,15 +1259,33 @@ export default class BackgroundJobsStore extends BackgroundJobsAdapter {
|
|
|
1263
1259
|
* plain FIFO ordering is left untouched (and no needless filesort is introduced).
|
|
1264
1260
|
* @param {import("../database/drivers/base.js").default} db - Database connection.
|
|
1265
1261
|
* @param {number} nowMs - Dispatch instant.
|
|
1262
|
+
* @param {string | null} laneFairnessRank - Raw lane fairness rank folded into the primary rank.
|
|
1266
1263
|
* @returns {string | null} - Raw SQL rank expression, or null when no queue is prioritized.
|
|
1267
1264
|
*/
|
|
1268
|
-
_queuePriorityOrderSql(db, nowMs) {
|
|
1269
|
-
return buildQueuePriorityOrderSql({configuration: this.configuration, db, nowMs})
|
|
1265
|
+
_queuePriorityOrderSql(db, nowMs, laneFairnessRank) {
|
|
1266
|
+
return buildQueuePriorityOrderSql({configuration: this.configuration, db, nowMs, laneFairnessRank})
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
/**
|
|
1270
|
+
* Builds the lane-fairness rank folded into the primary queued-job claim rank. See
|
|
1271
|
+
* {@link buildLaneFairnessRankSql} for the ranking contract.
|
|
1272
|
+
* @param {import("../database/drivers/base.js").default} db - Connection.
|
|
1273
|
+
* @param {number} nowMs - Dispatch instant.
|
|
1274
|
+
* @returns {string | null} - Raw SQL `ORDER BY … DESC` term, or null when not applicable.
|
|
1275
|
+
*/
|
|
1276
|
+
_laneFairnessRankSql(db, nowMs) {
|
|
1277
|
+
return buildLaneFairnessRankSql({
|
|
1278
|
+
configuration: this.configuration,
|
|
1279
|
+
db,
|
|
1280
|
+
nowMs,
|
|
1281
|
+
jobsTable: db.quoteTable(JOBS_TABLE),
|
|
1282
|
+
concurrencyTable: db.quoteTable(CONCURRENCY_TABLE)
|
|
1283
|
+
})
|
|
1270
1284
|
}
|
|
1271
1285
|
|
|
1272
1286
|
/**
|
|
1273
|
-
* Builds the
|
|
1274
|
-
*
|
|
1287
|
+
* Builds the legacy fallback ordering for rows without a durable lane claim stamp. The
|
|
1288
|
+
* durable lane-service age is already part of the primary rank; this remains a tie-break only.
|
|
1275
1289
|
* @param {import("../database/drivers/base.js").default} db - Connection.
|
|
1276
1290
|
* @param {number} nowMs - Dispatch instant.
|
|
1277
1291
|
* @returns {string | null} - Raw SQL `ORDER BY … DESC` term, or null when not applicable.
|
|
@@ -1528,7 +1542,7 @@ export default class BackgroundJobsStore extends BackgroundJobsAdapter {
|
|
|
1528
1542
|
return null
|
|
1529
1543
|
}
|
|
1530
1544
|
|
|
1531
|
-
await this._recordLaneClaim(db, queuedJob.
|
|
1545
|
+
await this._recordLaneClaim(db, queuedJob.queue, handedOffAtMs)
|
|
1532
1546
|
await this._recordStatusTransition(db, "queued", "handed_off")
|
|
1533
1547
|
/** @type {import("./types.js").BackgroundJobRow} */
|
|
1534
1548
|
const handedOffJob = {
|
|
@@ -4032,17 +4046,24 @@ export default class BackgroundJobsStore extends BackgroundJobsAdapter {
|
|
|
4032
4046
|
}
|
|
4033
4047
|
|
|
4034
4048
|
/**
|
|
4035
|
-
* Stamps the instant a lane received a claim so {@link
|
|
4036
|
-
* aging-ceiling tie by lane service rather than by backlog age.
|
|
4037
|
-
*
|
|
4038
|
-
*
|
|
4049
|
+
* Stamps the instant a lane received a claim so {@link buildLaneFairnessRankSql} can break the
|
|
4050
|
+
* aging-ceiling tie by lane service rather than by backlog age. The lane is the queue: a job
|
|
4051
|
+
* claimed under an explicit `concurrencyKey` still consumed its queue's turn, and leaving that
|
|
4052
|
+
* claim unstamped made the tie-break term equal the row's own age again — which the deepest
|
|
4053
|
+
* aged backlog won every cycle.
|
|
4039
4054
|
* @param {import("../database/drivers/base.js").default} db - Database connection.
|
|
4040
|
-
* @param {string | null}
|
|
4055
|
+
* @param {string | null} queue - Queue of the job that just claimed.
|
|
4041
4056
|
* @param {number} claimedAtMs - Claim instant.
|
|
4042
4057
|
* @returns {Promise<void>} - Resolves when the lane counter is stamped.
|
|
4043
4058
|
*/
|
|
4044
|
-
async _recordLaneClaim(db,
|
|
4045
|
-
|
|
4059
|
+
async _recordLaneClaim(db, queue, claimedAtMs) {
|
|
4060
|
+
const normalizedQueue = queue || DEFAULT_BACKGROUND_JOB_QUEUE
|
|
4061
|
+
const cap = this._queueMaxConcurrency(normalizedQueue)
|
|
4062
|
+
|
|
4063
|
+
if (cap === null) return
|
|
4064
|
+
|
|
4065
|
+
const concurrencyKey = laneConcurrencyKey(normalizedQueue)
|
|
4066
|
+
await this._ensureQueueConcurrencyKey(db, {concurrencyKey, maxConcurrency: cap})
|
|
4046
4067
|
|
|
4047
4068
|
const table = db.quoteTable(CONCURRENCY_TABLE)
|
|
4048
4069
|
|