cultcache-ts store: a rewrite of identical content keeps its stamp and version; the push result is used only if it is a stamp
Some checks failed
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Failing after 21m14s
Some checks failed
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Failing after 21m14s
Fixes variants:finding:cut-update-with-retry-ts.s2.flush-remints-unchanged-records and .push-result-unvalidated (ruling variants:ruling:storedat-on-change). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
505b868d90
commit
bd6a65ab53
3 changed files with 71 additions and 10 deletions
|
|
@ -879,7 +879,7 @@ export class CultCache {
|
|||
} else {
|
||||
// A store that re-stamps a write (storedAt later than the record it replaces) reports the stamp; the cache holds that one.
|
||||
const pushed = await home?.push(entry);
|
||||
if (pushed) {
|
||||
if (typeof pushed === "object" && pushed !== null && typeof pushed.storedAt === "string" && pushed.storedAt.length > 0) {
|
||||
hydrated.storedAt = pushed.storedAt;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -155,15 +155,22 @@ export class SingleFileMessagePackBackingStore implements CacheBackingStore {
|
|||
return result;
|
||||
}
|
||||
|
||||
// Writes the supplied records onto the arrived ones and returns the supplied records as written. A supplied record that
|
||||
// replaces an arrived one is never stamped earlier than the record it replaces, so a content version that includes storedAt
|
||||
// Writes the supplied records onto the arrived ones and returns the supplied records as written. A supplied record whose
|
||||
// schema id and payload bytes differ from the arrived one it replaces is never stamped earlier than the record it replaces, so a content version that includes storedAt
|
||||
// cannot repeat across writes (variants:ruling:storedat-on-change): one not later than the arrived stamp is written one
|
||||
// millisecond after it.
|
||||
async #writeAll(supplied: CultCacheEnvelope[], arrived: CultCacheEnvelope[]): Promise<CultCacheEnvelope[]> {
|
||||
const arrivedAt = new Map(arrived.map((entry) => [entry.key, entry.storedAt]));
|
||||
const arrivedByKey = new Map(arrived.map((entry) => [entry.key, entry]));
|
||||
const written = supplied.map((entry) => {
|
||||
const prior = arrivedAt.get(entry.key);
|
||||
return prior === undefined ? entry : { ...entry, storedAt: laterStoredAt(entry.storedAt, prior) };
|
||||
const prior = arrivedByKey.get(entry.key);
|
||||
if (prior === undefined) {
|
||||
return entry;
|
||||
}
|
||||
// A rewrite of the identical record (same schema id and payload bytes) keeps the stamp and so the version it has.
|
||||
if (sameContent(entry, prior)) {
|
||||
return prior;
|
||||
}
|
||||
return { ...entry, storedAt: laterStoredAt(entry.storedAt, prior.storedAt) };
|
||||
});
|
||||
await mkdir(dirname(this.filePath), { recursive: true });
|
||||
|
||||
|
|
@ -181,6 +188,15 @@ export function recordVersion(entry: CultCacheEnvelope): string {
|
|||
.digest("hex");
|
||||
}
|
||||
|
||||
function sameContent(a: CultCacheEnvelope, b: CultCacheEnvelope): boolean {
|
||||
if (schemaIdForEnvelope(a) !== schemaIdForEnvelope(b)) {
|
||||
return false;
|
||||
}
|
||||
const x = documentPayloadBytes(a.payload);
|
||||
const y = documentPayloadBytes(b.payload);
|
||||
return x.length === y.length && x.every((byte, i) => byte === y[i]);
|
||||
}
|
||||
|
||||
// `storedAt` if it is later than `prior`, else `prior` plus one millisecond. A stamp that does not parse is not later; a prior
|
||||
// that does not parse leaves nothing to be later than.
|
||||
function laterStoredAt(storedAt: string, prior: string): string {
|
||||
|
|
|
|||
|
|
@ -5,11 +5,11 @@ import { join } from "node:path";
|
|||
import test from "node:test";
|
||||
import { setImmediate as tick } from "node:timers/promises";
|
||||
|
||||
import { decode } from "@msgpack/msgpack";
|
||||
import { decode, encode } from "@msgpack/msgpack";
|
||||
import { z } from "zod";
|
||||
|
||||
import { CultCache } from "../src/cult-cache";
|
||||
import { defineDocumentRegistry, defineDocumentType, documentPayloadBytes } from "../src/document";
|
||||
import { defineDocumentRegistry, defineDocumentType, documentPayloadBytes, documentPayloadFromBytes } from "../src/document";
|
||||
import { SingleFileMessagePackBackingStore, readSingleFileStore } from "../src/single-file-messagepack-backing-store";
|
||||
import { SchemaConflictError } from "../src/store-format";
|
||||
import type { CacheBackingStore } from "../src/types";
|
||||
|
|
@ -215,7 +215,7 @@ test("put installs the storedAt the store stamped, so the cache and the file agr
|
|||
await cache.put(counterDocument, KEY, { value: 1 });
|
||||
const store = new SingleFileMessagePackBackingStore(path);
|
||||
const [envelope] = readSingleFileStore(path).envelopes;
|
||||
await store.push({ ...envelope!, storedAt: "2099-01-01T00:00:00.000Z" });
|
||||
await store.push({ ...envelope!, payload: documentPayloadFromBytes(encode({ value: 99 })), storedAt: "2099-01-01T00:00:00.000Z" });
|
||||
await cache.put(counterDocument, KEY, { value: 2 });
|
||||
const onDisk = readSingleFileStore(path).envelopes[0]!.storedAt;
|
||||
assert.equal(onDisk, "2099-01-01T00:00:00.001Z");
|
||||
|
|
@ -405,7 +405,7 @@ test("own writes cause no retry, and updated returns the held instance", async (
|
|||
// The cache holds the record as the store wrote it, the stamp the store chose included: over a record stamped in the future
|
||||
// the store writes one millisecond after it, whatever the clock says.
|
||||
const future = readSingleFileStore(path).envelopes[0]!;
|
||||
await new SingleFileMessagePackBackingStore(path).push({ ...future, storedAt: "2099-01-01T00:00:00.000Z" });
|
||||
await new SingleFileMessagePackBackingStore(path).push({ ...future, payload: documentPayloadFromBytes(encode({ value: 99 })), storedAt: "2099-01-01T00:00:00.000Z" });
|
||||
await cache.update(counterDocument, KEY, apply);
|
||||
assert.equal(readSingleFileStore(path).envelopes[0]!.storedAt, "2099-01-01T00:00:00.001Z");
|
||||
assert.equal(cache.getEnvelope(counterDocument, KEY)!.storedAt, "2099-01-01T00:00:00.001Z");
|
||||
|
|
@ -526,3 +526,48 @@ test("the ledger holds under contention: the stored value is the number of updat
|
|||
assert.equal(stored(path), results.reduce((sum, n) => sum + n, 0));
|
||||
});
|
||||
});
|
||||
|
||||
test("a flush of an unchanged cache rewrites the file byte for byte, and an update does not lose a race to it", async () => {
|
||||
await withDirectory(async (path) => {
|
||||
const cache = await open(path);
|
||||
await cache.put(counterDocument, KEY, { value: 1 });
|
||||
await cache.put(counterDocument, "other", { value: 7 });
|
||||
const store = new SingleFileMessagePackBackingStore(path);
|
||||
const before = await readFile(path);
|
||||
// What CultMesh.flush does: push the whole stored image, twice.
|
||||
await store.pushAll(await store.pullAll());
|
||||
await store.pushAll(await store.pullAll());
|
||||
assert.deepEqual(await readFile(path), before);
|
||||
const result = await cache.update(counterDocument, KEY, async (current) => {
|
||||
await store.pushAll(await store.pullAll());
|
||||
return { value: current!.value + 1 };
|
||||
}, { maxAttempts: 1 });
|
||||
assert.equal(result.outcome, "updated");
|
||||
assert.equal(stored(path), 2);
|
||||
});
|
||||
});
|
||||
|
||||
test("a changed record is re-stamped later than the one it replaces, and a revert to earlier bytes too", async () => {
|
||||
await withDirectory(async (path) => {
|
||||
const store = new SingleFileMessagePackBackingStore(path);
|
||||
const cache = await open(path);
|
||||
await cache.put(counterDocument, KEY, { value: 1 });
|
||||
const first = readSingleFileStore(path).envelopes[0]!;
|
||||
const same = await store.push({ ...first, storedAt: "2099-01-01T00:00:00.000Z" });
|
||||
assert.equal(same.storedAt, first.storedAt, "identical content keeps its stamp");
|
||||
await cache.put(counterDocument, KEY, { value: 2 });
|
||||
const second = readSingleFileStore(path).envelopes[0]!;
|
||||
const reverted = await store.push({ ...first, storedAt: second.storedAt });
|
||||
assert.ok(Date.parse(reverted.storedAt) > Date.parse(second.storedAt));
|
||||
});
|
||||
});
|
||||
|
||||
test("a push result that is not a stamp is ignored", async () => {
|
||||
const entries: Parameters<typeof memoryStore>[0] = [];
|
||||
const store = { ...memoryStore(entries), push: async (entry: (typeof entries)[number]) => { entries.push(entry); return true as never; } };
|
||||
const cache = CultCache.builder().withRegistry(registry).withGenericStore(store).build();
|
||||
await cache.put(counterDocument, KEY, { value: 1 });
|
||||
const held = cache.getEnvelope(counterDocument, KEY)!.storedAt;
|
||||
assert.equal(typeof held, "string");
|
||||
assert.ok(held.length > 0);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue