Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 36x 35x 1x 1x 1x 17x 17x 72x 72x 72x 72x 72x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 4x 4x 4x 4x 4x 4x 4x 17x 17x 17x 17x 17x 17x 17x 40x 40x 40x 40x 40x 36x 36x 36x 40x 4x 1x 1x 36x 36x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 46x 46x 46x 46x 46x 17x 17x 13x 13x 13x 13x 13x 13x 13x 13x 13x 17x 17x 57x 57x 57x 58x 57x 57x 57x 57x 57x 58x 57x 17x 17x 23x 36x 36x 36x 36x 36x 13x 13x 23x 17x 17x 13x 13x 13x 13x 13x 13x 12x 12x 12x 13x 13x 17x 17x 17x 17x 17x 17x 60x 60x 17x 17x 17x 17x 17x 17x 17x 17x | import {type IPathRefresh, type Knex} from '@feasibleone/blong/types';
type KnexQb = Knex;
/**
* The coordinator the runtime hands to edge writers through the adapter context.
* The contract and the reasoning behind it live with the type, in
* `core/blong/types.ts`; this module is the implementation the database adapter
* creates (see `knex.ts`) and `core.triple.merge` drives.
*/
export type {IPathRefresh};
/**
* Create the coordinator for one connection's rebuild procedure.
*
* `procedure` defaults to `access_pathRefresh()`, the procedure the access realm
* declares and the one the runtime already called inline.
*/
export function createPathRefresh(procedure = 'access_pathRefresh'): IPathRefresh {
const generationKey = `${procedure}.generation`;
const coveredKey = `${procedure}.covered`;
const waiters: Array<() => void> = [];
let inFlight = 0;
let deferred = false;
const idle = (): Promise<void> =>
inFlight === 0
? Promise.resolve()
: new Promise<void>(resolve => {
waiters.push(resolve);
});
const counter = (qb: unknown, name: string): number => {
const row = (qb as Array<{counterName: string; counterValue: number | string}>).find(
entry => entry.counterName === name,
);
return Number(row?.counterValue ?? 0);
};
/**
* True when the error says the counters table is not there.
*
* A database with no schema has no counters, so nothing owes a rebuild. A process that was only
* asked to *plan* — the `k8s` intent writes a tree and exits — still reads the deployment's
* configuration, and with it connects to the deployment's database, which in a first deployment
* or a CI run has not been migrated yet; there the startup drain must answer rather than die.
* The match is deliberately narrow: a broken connection is a different failure and still throws.
*/
const missingCounters = (error: unknown): boolean => {
const {code, errno, sqlState} = (error ?? {}) as {
code?: string;
errno?: number;
sqlState?: string;
};
return errno === 1146 || sqlState === '42S02' || code === 'ER_NO_SUCH_TABLE';
};
/**
* Whether the paths may be behind the graph: an edge was written without a
* rebuild and no rebuild has covered it since. Both counters come back in one
* round trip, and neither is this process's memory — that is the point.
*/
const uncovered = async (qb: unknown): Promise<boolean> => {
let rows: Array<{counterName: string; counterValue: number | string}>;
try {
rows = (await (qb as KnexQb)('core_counter')
.select('counterName', 'counterValue')
.whereIn('counterName', [generationKey, coveredKey])) as Array<{
counterName: string;
counterValue: number | string;
}>;
} catch (error) {
if (missingCounters(error)) return false;
throw error;
}
return counter(rows, generationKey) > counter(rows, coveredKey);
};
/**
* Count a write that skipped its rebuild.
*
* Called with the writer's transaction, so the count and the edges it stands
* for become visible together: a rebuild that reads the generation has seen
* every edge counted into it, and one that reads it before this commit leaves
* the write uncovered rather than dropping it.
*/
const bumpGeneration = async (qb: unknown): Promise<void> => {
await (qb as KnexQb)('core_counter')
.insert({counterName: generationKey, counterValue: 1})
.onConflict('counterName')
.merge({counterValue: (qb as KnexQb).raw('counterValue + 1')});
};
const refresh = async (qb: unknown): Promise<void> => {
// The procedure locks the row it publishes coverage to, so two rebuilds
// cannot interleave their DELETE and INSERT over `core_path` — but only
// inside a transaction: without one each of its statements commits on its
// own, and the coverage could outlive a rebuild that half failed. A writer
// that rebuilds inline passes its own transaction instead.
await (qb as KnexQb).transaction(async (trx: KnexQb) => {
await trx.raw(`CALL ${procedure}()`);
});
};
const enter = (): (() => Promise<void>) => {
inFlight += 1;
let settled = false;
return async () => {
if (settled) return;
settled = true;
inFlight -= 1;
if (inFlight === 0) {
for (const wake of waiters.splice(0)) wake();
}
};
};
const drain = async (qb: unknown): Promise<void> => {
for (;;) {
// Wait for this process's writers before deciding: a rebuild that ran
// while one was mid-transaction would leave the counter comparison true
// and cost another rebuild, and waiting turns that into none.
await idle();
if (!(await uncovered(qb))) return;
await refresh(qb);
}
};
const defer = async <T>(qb: unknown, work: () => Promise<T>): Promise<T> => {
const outer = deferred;
if (!outer) deferred = true;
try {
return await work();
} finally {
if (!outer) {
deferred = false;
await drain(qb);
}
}
};
return {
procedure,
generationKey,
coveredKey,
get deferred() {
return deferred;
},
uncovered,
enter,
bumpGeneration,
refresh,
drain,
defer,
};
}
|