Avoid scanning operation history on agent polls

This commit is contained in:
2026-10-06 11:58:58 -04:00
parent 88a950692a
commit 85e167a606
3 changed files with 49 additions and 4 deletions
+7 -4
View File
@@ -522,11 +522,14 @@ export function pendingHostOperationRows(hostId: string, lane: OperationLane = '
.all(hostId, Number(host.readiness === 'ready' || host.mode === 'draining'), Number(host.inventory.capabilities?.includes('instance.cancel-create') ?? false),
...(lane === 'health' ? [new Date(Date.now() - 30_000).toISOString()] : [])) as OperationDeliveryRow[];
const preparationLimit = fleetSettings().preparationConcurrency;
const preparing = new Set((database().prepare(`SELECT DISTINCT host_id FROM ec_host_operations
WHERE kind IN ('library.download','library.import') AND last_delivered_at IS NOT NULL
AND state IN ('pending','accepted','running','unknown')`).all() as { host_id: string }[]).map(row => row.host_id));
const libraryKinds = ['library.download','library.import'];
const preparing = preparationLimit && rows.some(operation => libraryKinds.includes(operation.kind))
? new Set((database().prepare(`SELECT DISTINCT host_id FROM ec_host_operations
WHERE kind IN ('library.download','library.import') AND last_delivered_at IS NOT NULL
AND state IN ('pending','accepted','running','unknown')`).all() as { host_id: string }[]).map(row => row.host_id))
: new Set<string>();
const admitted = rows.filter(operation => {
if (!['library.download','library.import'].includes(operation.kind) || preparing.has(hostId) || !preparationLimit) return true;
if (!libraryKinds.includes(operation.kind) || preparing.has(hostId) || !preparationLimit) return true;
if (preparing.size >= preparationLimit) return false;
preparing.add(hostId); return true;
});
+1
View File
@@ -43,6 +43,7 @@ export function ensureHostSchema(handle: Database.Database): void {
-- history. The queue index cannot narrow those lookups beyond host_id,
-- causing a history scan for every queued command on Node's main thread.
CREATE INDEX IF NOT EXISTS ec_host_operations_kind ON ec_host_operations(host_id, kind, created_at);
CREATE INDEX IF NOT EXISTS ec_host_operations_kind_state ON ec_host_operations(kind, state);
CREATE TABLE IF NOT EXISTS ec_host_operation_payloads (
operation_id TEXT PRIMARY KEY REFERENCES ec_host_operations(id) ON DELETE CASCADE,
payload TEXT NOT NULL