Index lifecycle capacity checks instead of scanning retained jobs

This commit is contained in:
2026-10-06 12:54:10 -04:00
parent 37d380fa45
commit d2108eb604
6 changed files with 95 additions and 8 deletions
+2
View File
@@ -23,6 +23,7 @@ export function migrateNativeInstances(handle: Database.Database): void {
applied_at TEXT, verified_at TEXT
);
CREATE INDEX IF NOT EXISTS native_provisioning_instance ON native_provisioning(instance_id);
CREATE INDEX IF NOT EXISTS native_provisioning_operation ON native_provisioning(operation_id, deployment_id);
CREATE TABLE IF NOT EXISTS provisioning_safety_overrides (
deployment_id TEXT PRIMARY KEY REFERENCES server_deployments(id) ON DELETE CASCADE,
actor_id TEXT NOT NULL, actor_name TEXT NOT NULL, confirmed_at TEXT NOT NULL
@@ -35,6 +36,7 @@ export function migrateNativeInstances(handle: Database.Database): void {
);
CREATE INDEX IF NOT EXISTS ec_host_migrations_source ON ec_host_migrations(source_host_id);
CREATE INDEX IF NOT EXISTS ec_host_migrations_target ON ec_host_migrations(target_host_id);
CREATE INDEX IF NOT EXISTS ec_host_migrations_operation ON ec_host_migrations(remove_operation_id, deployment_id);
CREATE TABLE IF NOT EXISTS native_creation_cancellations (
creation_id TEXT PRIMARY KEY REFERENCES server_deployments(id),
cleanup_id TEXT NOT NULL UNIQUE REFERENCES server_deployments(id)
+5 -1
View File
@@ -393,12 +393,16 @@ export function db(): Database.Database {
ON server_deployments(server, server_deleted_at);
CREATE INDEX IF NOT EXISTS idx_server_deployments_state
ON server_deployments(state, action, server_deleted_at);
CREATE INDEX IF NOT EXISTS idx_server_deployments_server_state
ON server_deployments(server, server_deleted_at, state, action);
CREATE INDEX IF NOT EXISTS idx_server_deployments_started_at
ON server_deployments(started_at);
CREATE INDEX IF NOT EXISTS idx_server_deployments_submission
ON server_deployments(server, server_deleted_at, action, submitted_at);
CREATE INDEX IF NOT EXISTS idx_server_deployments_finished_at
ON server_deployments(finished_at) WHERE server_deleted_at IS NULL;`);
ON server_deployments(finished_at) WHERE server_deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_server_deployments_unsettled
ON server_deployments(id) WHERE state NOT IN ('done','warning','cancelled');`);
installReportRevisions(handle);
} catch (err) {
handle.close();
+2
View File
@@ -44,6 +44,8 @@ export function ensureHostSchema(handle: Database.Database): void {
-- 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 INDEX IF NOT EXISTS ec_host_operations_active ON ec_host_operations(state, id)
WHERE state IN ('pending','accepted','running','unknown');
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
+17 -6
View File
@@ -8,18 +8,29 @@ interface Holder { id: string; action: string; host_id: string | null; target_ho
export function lifecycleHolders(): Holder[] {
ensureHostSchema(db());
// Find capacity owners through their active state or outstanding command.
// A correlated OR over d used to walk every retained job and fetch its agent
// receipt on every admission, even when all that history was completed.
return db().prepare(`SELECT d.id,d.action,i.host_id,m.target_host_id,
CASE WHEN m.remove_operation_id IS NOT NULL AND m.removed_at IS NULL THEN 1 ELSE 0 END AS removing
FROM server_deployments d
LEFT JOIN native_provisioning n ON n.deployment_id=d.id
LEFT JOIN native_instances i ON i.id=n.instance_id
LEFT JOIN ec_host_migrations m ON m.deployment_id=d.id
WHERE d.state IN ('running','awaiting-health')
OR d.id IN (SELECT value FROM json_each(?))
OR EXISTS (SELECT 1 FROM restart_rollout r WHERE r.operation_id=d.id AND r.healthy_at IS NULL)
OR (m.remove_operation_id IS NOT NULL AND d.state NOT IN ('done','warning','cancelled'))
OR EXISTS (SELECT 1 FROM ec_host_operations o WHERE o.id IN (n.operation_id,m.remove_operation_id)
AND o.state IN ('pending','accepted','running','unknown'))`)
WHERE d.id IN (
SELECT id FROM server_deployments WHERE state IN ('running','awaiting-health')
UNION SELECT value FROM json_each(?)
UNION SELECT operation_id FROM restart_rollout WHERE healthy_at IS NULL
UNION SELECT deployment_id FROM ec_host_migrations
WHERE remove_operation_id IS NOT NULL AND deployment_id IN (
SELECT id FROM server_deployments WHERE state NOT IN ('done','warning','cancelled'))
UNION SELECT n.deployment_id FROM ec_host_operations o
JOIN native_provisioning n ON n.operation_id=o.id
WHERE o.state IN ('pending','accepted','running','unknown')
UNION SELECT m.deployment_id FROM ec_host_operations o
JOIN ec_host_migrations m ON m.remove_operation_id=o.id
WHERE o.state IN ('pending','accepted','running','unknown')
)`)
.all(JSON.stringify([...preparingJobs])) as Holder[];
}