Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

acl: version-checked cache for grant reads instead of re-reading the whole table (#499)

Regan Bell48fc1e57935d
diff --git a/src/acl/postgres-grant-store.ts b/src/acl/postgres-grant-store.ts
index 9aedce308..4d0182c85 100644
--- a/src/acl/postgres-grant-store.ts
+++ b/src/acl/postgres-grant-store.ts
@@ -22,15 +22,37 @@ export function createPostgresGrantStore(connectionString: string): GrantPersist
granted_by TEXT NOT NULL,
PRIMARY KEY (owner_scope_id, path, grantee_scope_id, permission)
)`,
+ `CREATE TABLE IF NOT EXISTS acl_grants_version(
+ only_row BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (only_row),
+ v BIGINT NOT NULL
+ )`,
+ `INSERT INTO acl_grants_version(only_row, v) VALUES (TRUE, 0) ON CONFLICT (only_row) DO NOTHING`,
+ `CREATE OR REPLACE FUNCTION acl_grants_bump_version() RETURNS trigger LANGUAGE plpgsql AS $fn$
+ BEGIN
+ INSERT INTO acl_grants_version(only_row, v) VALUES (TRUE, 1)
+ ON CONFLICT (only_row) DO UPDATE SET v = acl_grants_version.v + 1;
+ RETURN NULL;
+ END
+ $fn$`,
+ `DROP TRIGGER IF EXISTS acl_grants_bump ON acl_grants`,
+ `CREATE TRIGGER acl_grants_bump
+ AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON acl_grants
+ FOR EACH STATEMENT EXECUTE FUNCTION acl_grants_bump_version()`,
]);
const { q } = db;
+ let cache: { v: string; grants: readonly Grant[] } | null = null;
return {
async all() {
+ const versionRows = await q("SELECT v FROM acl_grants_version");
+ const version = versionRows.length ? String(versionRows[0]!.v) : null;
+ if (cache && version !== null && cache.v === version) return [...cache.grants];
const rows = await q(
"SELECT owner_scope_id, path, grantee_scope_id, permission, granted_by FROM acl_grants ORDER BY owner_scope_id, path, grantee_scope_id, permission",
);
- return rows.map(rowToGrant);
+ const grants = rows.map(rowToGrant);
+ cache = version === null ? null : { v: version, grants };
+ return [...grants];
},
async put(g) {
const client = await (await db.pool()).connect();
diff --git a/test/postgres-grant-store.test.ts b/test/postgres-grant-store.test.ts
index d143b686f..a50abb7d7 100644
--- a/test/postgres-grant-store.test.ts
+++ b/test/postgres-grant-store.test.ts
@@ -13,6 +13,7 @@ before(async () => {
const pg = (await import("pg")).default;
const p = new pg.Pool({ connectionString: URL });
await p.query("DROP TABLE IF EXISTS acl_grants CASCADE");
+ await p.query("DROP TABLE IF EXISTS acl_grants_version CASCADE");
await p.end();
});
@@ -67,6 +68,101 @@ test("pg grant persistence survives across store instances (no per-process cache
);
});
+test("pg grant persistence: a warm cache sees another instance's writes and revocations", { skip }, async () => {
+ const reader = createPostgresGrantStore(URL!);
+ const writer = createPostgresGrantStore(URL!);
+ await reader.all();
+
+ await writer.put(grant({ ref: "warm-cache.md" }));
+ assert.equal(
+ (await reader.all()).some((g) => g.ref === "warm-cache.md"),
+ true,
+ "a grant written elsewhere is visible on the very next read",
+ );
+
+ await reader.all();
+ await writer.remove(grant({ ref: "warm-cache.md" }));
+ assert.equal(
+ (await reader.all()).some((g) => g.ref === "warm-cache.md"),
+ false,
+ "a revocation elsewhere is visible on the very next read",
+ );
+});
+
+test("pg grant persistence: a warm cache sees writes that bypass the store entirely", { skip }, async () => {
+ const reader = createPostgresGrantStore(URL!);
+ await reader.all();
+
+ const pg = (await import("pg")).default;
+ const p = new pg.Pool({ connectionString: URL });
+ await p.query(
+ "INSERT INTO acl_grants (owner_scope_id, path, grantee_scope_id, permission, granted_by) VALUES ($1, $2, $3, $4, $5)",
+ [owner, "out-of-band.md", carol, "read", "U1"],
+ );
+ await p.end();
+
+ assert.equal(
+ (await reader.all()).some((g) => g.ref === "out-of-band.md"),
+ true,
+ "raw SQL against acl_grants must invalidate warm caches (old instances during blue-green, scripts, manual psql)",
+ );
+});
+
+test("pg grant persistence: the snapshot is served from cache until the version moves", { skip }, async () => {
+ const reader = createPostgresGrantStore(URL!);
+ await reader.put(grant({ ref: "cached.md" }));
+ await reader.all();
+
+ const pg = (await import("pg")).default;
+ const p = new pg.Pool({ connectionString: URL });
+ try {
+ await p.query("ALTER TABLE acl_grants DISABLE TRIGGER acl_grants_bump");
+ await p.query("DELETE FROM acl_grants WHERE path = $1", ["cached.md"]);
+ assert.equal(
+ (await reader.all()).some((g) => g.ref === "cached.md"),
+ true,
+ "an unbumped delete is invisible — proof reads are served from the snapshot",
+ );
+ } finally {
+ await p.query("ALTER TABLE acl_grants ENABLE TRIGGER acl_grants_bump");
+ }
+ await p.query("UPDATE acl_grants_version SET v = v + 1");
+ assert.equal(
+ (await reader.all()).some((g) => g.ref === "cached.md"),
+ false,
+ "the next version bump refreshes the snapshot",
+ );
+ await p.end();
+});
+
+test("pg grant persistence: a conditional replacement invalidates warm caches", { skip }, async () => {
+ const reader = createPostgresGrantStore(URL!);
+ const writer = createPostgresGrantStore(URL!);
+ const before = grant({ ref: "cas-invalidate.md" });
+ await writer.put(before);
+ await reader.all();
+
+ const after = grant({ ref: "cas-invalidate.md", granteeScopeId: scopeId("personal", "U3") });
+ assert.equal(await writer.replaceForResourceIfCurrent(owner, "cas-invalidate.md", [before], [after]), true);
+ assert.equal(
+ (await reader.all()).some((g) => g.ref === "cas-invalidate.md" && g.granteeScopeId === after.granteeScopeId),
+ true,
+ "the replacement is visible on the very next read",
+ );
+});
+
+test("pg grant persistence: cached reads return independent arrays", { skip }, async () => {
+ const store = createPostgresGrantStore(URL!);
+ await store.put(grant({ ref: "aliasing.md" }));
+ const first = await store.all();
+ first.length = 0;
+ assert.equal(
+ (await store.all()).some((g) => g.ref === "aliasing.md"),
+ true,
+ "mutating one caller's result must not corrupt the cache",
+ );
+});
+
test("pg grant persistence serializes competing empty-set conditional replacements", { skip }, async () => {
const left = createPostgresGrantStore(URL!);
const right = createPostgresGrantStore(URL!);