Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions apps/server/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ const migrations = {
yield* sql`ALTER TABLE machine DROP COLUMN name`;
yield* sql`ALTER TABLE machine RENAME COLUMN alias TO name`;
}),

// `boot` is the store's latest open while the index was read up to `cursor`, as `rollup_boot` is
// to `rollup`.
"202611140003_boot": Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`ALTER TABLE machine ADD COLUMN boot TEXT`;
}),
};

const setup = Effect.gen(function* () {
Expand Down
66 changes: 47 additions & 19 deletions apps/server/src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ export const layer = Layer.effect(
Effect.gen(function* () {
const [row] = yield* sql<{ cursor: number | null }>`SELECT cursor FROM thread WHERE id = ${threadId}`;
if (row === undefined) return;
// A first open is the thread's line as the store has it now, so its first batch replaces the
// thread's own detail: after a restored store, entries of the lost line would hold its seqs.
let first = row.cursor === null;
yield* batches<ThreadRow, ThreadSynced>(
live.client("thread.sync", {
sessionId,
Expand All @@ -201,21 +204,30 @@ export const layer = Layer.effect(
? noteEntry(threadId, item.entry).pipe(Effect.as(true))
: Effect.succeed(true),
(rows, synced) =>
transact((append) =>
Effect.gen(function* () {
const changes = yield* Mirror.thread(threadId, rows);
yield* sql`
Effect.gen(function* () {
const whole = first;
yield* transact((append) =>
Effect.gen(function* () {
if (whole) {
yield* sql`DELETE FROM entry WHERE thread_id = ${threadId}`;
yield* sql`DELETE FROM inbox WHERE thread_id = ${threadId}`;
}
const changes = yield* Mirror.thread(threadId, rows);
yield* sql`
UPDATE thread SET cursor = ${synced.rev}, floor = COALESCE(${synced.floor ?? null}, floor)
WHERE id = ${threadId}
`;
if (rows.length > 0 || synced.floor !== undefined)
yield* append({
topic: syncTopics.changed,
source: "sync",
payload: changes.notice({ machineId, threadId, full: false, items: rows.length }),
});
}),
).pipe(Effect.andThen(Effect.suspend(() => readyOf(threadId)))),
if (rows.length > 0 || synced.floor !== undefined)
yield* append({
topic: syncTopics.changed,
source: "sync",
payload: changes.notice({ machineId, threadId, full: false, items: rows.length }),
});
}),
);
first = false;
yield* readyOf(threadId);
}),
);
}).pipe(
Effect.catchCause((cause) =>
Expand Down Expand Up @@ -322,17 +334,28 @@ export const layer = Layer.effect(
return yield* Effect.never;
});

/** One connection's sync: decides between resume and full sync, then follows the index. */
/**
* One connection's sync: decides between resume and full sync, then follows the index. A read
* from zero is a full sync, whose first batch also sweeps what it did not send. A resume that left
* the store's line (by `boot`) comes back from zero with `reset`; a server older than boots is
* read from zero only while its head is behind the cursor.
*/
const connected = (machineId: string, client: Connection) =>
Effect.gen(function* () {
const info = yield* client("server.info", {});
yield* servers.setHost(machineId, info.host?.name ?? null);
const [machine] = yield* sql<{ storeId: string | null; cursor: number; namespace: string }>`
SELECT store_id, cursor, namespace FROM machine WHERE id = ${machineId}
const [machine] = yield* sql<{
storeId: string | null;
cursor: number;
boot: string | null;
namespace: string;
}>`
SELECT store_id, cursor, boot, namespace FROM machine WHERE id = ${machineId}
`;
if (machine === undefined) return yield* Effect.never;
yield* unclaimed(machineId, info.storeId);
let full = info.storeId !== machine.storeId || info.rev < machine.cursor;
const resumed = info.storeId === machine.storeId && info.rev >= machine.cursor;
let full = !resumed;
const live: Live = { client, threads: yield* FiberMap.make<string>() };
yield* Effect.addFinalizer(() =>
Effect.sync(() => {
Expand All @@ -342,13 +365,16 @@ export const layer = Layer.effect(
const daemons = yield* FiberHandle.make<never>();
let following = false;
yield* batches<IndexRow, IndexSynced>(
client("index.sync", { after: full ? 0 : machine.cursor }),
client("index.sync", {
after: resumed ? machine.cursor : 0,
...(resumed && machine.boot !== null ? { boot: machine.boot } : {}),
}),
(item) => item._tag === "Synced",
() => Effect.succeed(true),
(rows, synced) =>
Effect.gen(function* () {
const now = yield* Clock.currentTimeMillis;
const wasFull = full;
const wasFull = full || synced.reset === true;
let spaces = false;
yield* transact((append) =>
Effect.gen(function* () {
Expand All @@ -361,7 +387,9 @@ export const layer = Layer.effect(
if (wasFull) yield* Mirror.unseen(machineId, changes, now);
spaces = changes.space.size > 0;
yield* sql`
UPDATE machine SET cursor = ${synced.rev}, store_id = ${synced.storeId} WHERE id = ${machineId}
UPDATE machine SET cursor = ${synced.rev}, store_id = ${synced.storeId},
boot = ${synced.boot ?? null}
WHERE id = ${machineId}
`;
if (rows.length > 0 || wasFull)
yield* append({
Expand Down
162 changes: 81 additions & 81 deletions apps/server/test/__artifacts__/metrics.simulation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"seeded": {
"cw-a": {
"sessions": 300,
"prompts": 2277,
"requests": 7321,
"aborted": 196,
"failed": 137,
"calls": 7778
"prompts": 2245,
"requests": 7273,
"aborted": 209,
"failed": 140,
"calls": 7827
},
"cw-b": {
"sessions": 120,
Expand All @@ -20,37 +20,37 @@
"connected": {
"before": {
"all": {
"requests": 10160,
"tokens": 496714759,
"cost": 228.254801875,
"timed": 9866,
"prompts": 3158,
"tools": 10315
"requests": 10112,
"tokens": 490774644,
"cost": 221.19770285,
"timed": 9805,
"prompts": 3126,
"tools": 10347
},
"sessions": 420,
"deleted": 0
},
"after": {
"all": {
"requests": 10160,
"tokens": 496714759,
"cost": 228.254801875,
"timed": 9866,
"prompts": 3158,
"tools": 10315
"requests": 10112,
"tokens": 490774644,
"cost": 221.19770285,
"timed": 9805,
"prompts": 3126,
"tools": 10347
},
"sessions": 420,
"deleted": 7
}
},
"live": {
"all": {
"requests": 10166,
"tokens": 496714771,
"cost": 228.254801875,
"timed": 9872,
"prompts": 3161,
"tools": 10318
"requests": 10118,
"tokens": 490774656,
"cost": 221.19770285,
"timed": 9811,
"prompts": 3129,
"tools": 10350
},
"sessions": 421,
"deleted": 7
Expand All @@ -62,12 +62,12 @@
],
"totals": {
"all": {
"requests": 10178,
"tokens": 496714795,
"cost": 228.254801875,
"timed": 9884,
"prompts": 3167,
"tools": 10324
"requests": 10130,
"tokens": 490774680,
"cost": 221.19770285,
"timed": 9823,
"prompts": 3135,
"tools": 10356
},
"sessions": 423,
"deleted": 7
Expand All @@ -76,101 +76,101 @@
"paused": {
"paused": {
"all": {
"requests": 10182,
"tokens": 496714803,
"cost": 228.254801875,
"timed": 9888,
"prompts": 3169,
"tools": 10326
"requests": 10134,
"tokens": 490774688,
"cost": 221.19770285,
"timed": 9827,
"prompts": 3137,
"tools": 10358
},
"sessions": 424,
"deleted": 7
},
"resumed": {
"all": {
"requests": 10182,
"tokens": 496714803,
"cost": 228.254801875,
"timed": 9888,
"prompts": 3169,
"tools": 10326
"requests": 10134,
"tokens": 490774688,
"cost": 221.19770285,
"timed": 9827,
"prompts": 3137,
"tools": 10358
},
"sessions": 424,
"deleted": 7
}
},
"restartedAway": {
"all": {
"requests": 10186,
"tokens": 496714811,
"cost": 228.254801875,
"timed": 9892,
"prompts": 3171,
"tools": 10328
"requests": 10138,
"tokens": 490774696,
"cost": 221.19770285,
"timed": 9831,
"prompts": 3139,
"tools": 10360
},
"sessions": 425,
"deleted": 7
},
"cut": {
"all": {
"requests": 10188,
"tokens": 496714815,
"cost": 228.254801875,
"timed": 9894,
"prompts": 3172,
"tools": 10329
"requests": 10140,
"tokens": 490774700,
"cost": 221.19770285,
"timed": 9833,
"prompts": 3140,
"tools": 10361
},
"sessions": 426,
"deleted": 7
},
"restored": {
"all": {
"requests": 10192,
"tokens": 496714823,
"cost": 228.254801875,
"timed": 9898,
"prompts": 3174,
"tools": 10331
"requests": 10144,
"tokens": 490774708,
"cost": 221.19770285,
"timed": 9837,
"prompts": 3142,
"tools": 10363
},
"sessions": 428,
"deleted": 9
"deleted": 7
},
"readded": {
"kept": {
"all": {
"requests": 10192,
"tokens": 496714823,
"cost": 228.254801875,
"timed": 9898,
"prompts": 3174,
"tools": 10331
"requests": 10144,
"tokens": 490774708,
"cost": 221.19770285,
"timed": 9837,
"prompts": 3142,
"tools": 10363
},
"sessions": 428,
"deleted": 130
"deleted": 128
},
"again": {
"all": {
"requests": 10192,
"tokens": 496714823,
"cost": 228.254801875,
"timed": 9898,
"prompts": 3174,
"tools": 10331
"requests": 10144,
"tokens": 490774708,
"cost": 221.19770285,
"timed": 9837,
"prompts": 3142,
"tools": 10363
},
"sessions": 428,
"deleted": 9
"deleted": 7
}
},
"recreated": {
"all": {
"requests": 10196,
"tokens": 496714831,
"cost": 228.254801875,
"timed": 9902,
"prompts": 3176,
"tools": 10333
"requests": 10148,
"tokens": 490774716,
"cost": 221.19770285,
"timed": 9841,
"prompts": 3144,
"tools": 10365
},
"sessions": 429,
"deleted": 130
"deleted": 128
}
}
Loading