@substrat-run/adapter-cloudflare 0.213.0 → 0.214.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/control-plane-do.d.ts +89 -6
- package/dist/control-plane-do.d.ts.map +1 -1
- package/dist/control-plane-do.js +297 -9
- package/dist/control-plane-do.js.map +1 -1
- package/dist/directory-split-inventory.d.ts +2 -2
- package/dist/directory-split-inventory.d.ts.map +1 -1
- package/dist/directory-split-inventory.js +8 -2
- package/dist/directory-split-inventory.js.map +1 -1
- package/dist/host.d.ts +3 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +3 -2
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/reconcile-on-wake.d.ts +35 -0
- package/dist/reconcile-on-wake.d.ts.map +1 -0
- package/dist/reconcile-on-wake.js +75 -0
- package/dist/reconcile-on-wake.js.map +1 -0
- package/dist/route-read-model-publisher.d.ts +171 -0
- package/dist/route-read-model-publisher.d.ts.map +1 -0
- package/dist/route-read-model-publisher.js +524 -0
- package/dist/route-read-model-publisher.js.map +1 -0
- package/dist/route-read-model.d.ts +166 -0
- package/dist/route-read-model.d.ts.map +1 -0
- package/dist/route-read-model.js +195 -0
- package/dist/route-read-model.js.map +1 -0
- package/dist/route-resolver.d.ts +41 -3
- package/dist/route-resolver.d.ts.map +1 -1
- package/dist/route-resolver.js +58 -3
- package/dist/route-resolver.js.map +1 -1
- package/dist/scope-do.d.ts.map +1 -1
- package/dist/scope-do.js +38 -2
- package/dist/scope-do.js.map +1 -1
- package/package.json +9 -9
package/dist/control-plane-do.js
CHANGED
|
@@ -9,6 +9,7 @@ import { COPY_BACKFILL_SCOPE_SQL, COPY_BACKFILL_SQL, COPY_BACKFILL_SUPERSEDE_SQL
|
|
|
9
9
|
import { autoAdmittedProdRefusal, listedPromoteRefusal, opsFailureReferenceWhere } from '@substrat-run/kernel';
|
|
10
10
|
import { canonicalInstant } from '@substrat-run/kernel';
|
|
11
11
|
import { doBuiltColumnsOf, doRedactionSql } from './sql.js';
|
|
12
|
+
import { ROUTE_READ_MODEL_TABLE_GLOB, RouteReadModelPublisher, routeKvWriterOf, uninstallRouteReadModel, } from './route-read-model-publisher.js';
|
|
12
13
|
import { assertReplayableDump, AUTO_ADMISSION_NOTE, instant, opsFailureFingerprint, ROUTE_SCOPE_HOSTNAMES_MAX, SCOPE_DUMP_PAGE_MAX_BYTES, SCOPE_GATE_REASONS, substratError } from '@substrat-run/contracts';
|
|
13
14
|
/**
|
|
14
15
|
* The router's per-request directory read (`readRoute`), lifted out so its plan can be asked for
|
|
@@ -823,6 +824,8 @@ export function assertDirectoryDdlPlan(plan) {
|
|
|
823
824
|
* control-plane request goes through, so batches are spaced rather than run back to back.
|
|
824
825
|
*/
|
|
825
826
|
const BACKFILL_PAUSE_MS = 1000;
|
|
827
|
+
/** #3395: how soon a routing read model publish that KV refused is tried again. */
|
|
828
|
+
export const ROUTE_PUBLISH_RETRY_MS = 10_000;
|
|
826
829
|
/** The longest a failing #1764 backfill waits before it tries again. */
|
|
827
830
|
const BACKFILL_BACKOFF_MAX_MS = 60 * 60 * 1000;
|
|
828
831
|
/**
|
|
@@ -985,8 +988,209 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
985
988
|
this.sql = ctx.storage.sql;
|
|
986
989
|
this.kernelSql = switchSqlOver(this.sql);
|
|
987
990
|
this.applyDirectorySchema();
|
|
991
|
+
this.installRouteReadModel(routeKvWriterOf(Reflect.get(env, 'ROUTES')));
|
|
988
992
|
this.backfillArmed = ctx.blockConcurrencyWhile(() => this.armVersionMigrationsBackfill());
|
|
989
993
|
}
|
|
994
|
+
// -- the routing read model (#3395) -------------------------------------------
|
|
995
|
+
/** Set where a `ROUTES` KV namespace is bound; null otherwise, and the read model is off. */
|
|
996
|
+
routePublisher = null;
|
|
997
|
+
routeFlushing = null;
|
|
998
|
+
routeFlushAgain = false;
|
|
999
|
+
/**
|
|
1000
|
+
* How many of this object's methods are running (`kickRouteReadModelAfterEveryCall`). A flush
|
|
1001
|
+
* computes entries synchronously from the directory, so it only starts, and only reads a group
|
|
1002
|
+
* of keys, while this is 0: a method still running may hold a transaction that has not committed,
|
|
1003
|
+
* and a snapshot read inside it could be published and then rolled back.
|
|
1004
|
+
*/
|
|
1005
|
+
routeCallDepth = 0;
|
|
1006
|
+
/**
|
|
1007
|
+
* On where a KV namespace is bound, off (triggers dropped, outbox emptied) where none is. A
|
|
1008
|
+
* directory that had no triggers before marks every key dirty, so switching on IS the backfill;
|
|
1009
|
+
* `publishRouteReadModel` is the operator's handle on the same thing.
|
|
1010
|
+
*/
|
|
1011
|
+
installRouteReadModel(kv) {
|
|
1012
|
+
if (!kv) {
|
|
1013
|
+
uninstallRouteReadModel(this.sql);
|
|
1014
|
+
return;
|
|
1015
|
+
}
|
|
1016
|
+
const publisher = new RouteReadModelPublisher(this.sql, kv);
|
|
1017
|
+
this.ctx.storage.transactionSync(() => {
|
|
1018
|
+
if (publisher.install())
|
|
1019
|
+
publisher.markAll();
|
|
1020
|
+
});
|
|
1021
|
+
this.routePublisher = publisher;
|
|
1022
|
+
// The ledger must describe THIS namespace before anything is published against it. Checked in the
|
|
1023
|
+
// background, so no directory call waits on a KV read; publishing waits for it instead.
|
|
1024
|
+
this.routeLedgerVerified = false;
|
|
1025
|
+
this.routeLedgerCheck = (async () => {
|
|
1026
|
+
try {
|
|
1027
|
+
await publisher.verifyNamespace();
|
|
1028
|
+
this.routeLedgerVerified = true;
|
|
1029
|
+
this.kickRouteReadModel();
|
|
1030
|
+
}
|
|
1031
|
+
catch (err) {
|
|
1032
|
+
console.error('route read model: the namespace check failed, retrying from the alarm', err);
|
|
1033
|
+
this.routeRetryAfter = Date.now() + ROUTE_PUBLISH_RETRY_MS;
|
|
1034
|
+
await this.armRouteReadModelRetry().catch(() => undefined);
|
|
1035
|
+
}
|
|
1036
|
+
})();
|
|
1037
|
+
}
|
|
1038
|
+
/** The ledger has been checked against the bound namespace (`verifyNamespace`). */
|
|
1039
|
+
routeLedgerVerified = false;
|
|
1040
|
+
/** That check, for a caller that must see publishing settle (`settleRouteReadModel`). */
|
|
1041
|
+
routeLedgerCheck = Promise.resolve();
|
|
1042
|
+
/** After a failed publish, nothing publishes before this time but the alarm (`ROUTE_PUBLISH_RETRY_MS`). */
|
|
1043
|
+
routeRetryAfter = 0;
|
|
1044
|
+
/** The alarm's half of the retry: check the namespace again if that failed, then publish. */
|
|
1045
|
+
async retryRouteReadModel() {
|
|
1046
|
+
const publisher = this.routePublisher;
|
|
1047
|
+
if (!publisher)
|
|
1048
|
+
return;
|
|
1049
|
+
this.routeRetryAfter = 0;
|
|
1050
|
+
if (!this.routeLedgerVerified) {
|
|
1051
|
+
try {
|
|
1052
|
+
await publisher.verifyNamespace();
|
|
1053
|
+
this.routeLedgerVerified = true;
|
|
1054
|
+
}
|
|
1055
|
+
catch (err) {
|
|
1056
|
+
console.error('route read model: the namespace check failed again', err);
|
|
1057
|
+
this.routeRetryAfter = Date.now() + ROUTE_PUBLISH_RETRY_MS;
|
|
1058
|
+
await this.armRouteReadModelRetry().catch(() => undefined);
|
|
1059
|
+
return;
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
this.kickRouteReadModel();
|
|
1063
|
+
}
|
|
1064
|
+
/** The flush may read the directory: none of this object's methods is running. */
|
|
1065
|
+
routeQuiet = () => this.routeCallDepth === 0;
|
|
1066
|
+
/**
|
|
1067
|
+
* Publish what the outbox holds, one flush at a time: a kick that lands mid-flush runs one more
|
|
1068
|
+
* round after it, so a change made during a flush is never left waiting for the next one. Called
|
|
1069
|
+
* when the last running method of this class returns (`kickRouteReadModelAfterEveryCall`); a kick
|
|
1070
|
+
* while one is still running waits for that return, so a flush never reads a write whose
|
|
1071
|
+
* transaction has not committed. A KV failure arms the alarm.
|
|
1072
|
+
*/
|
|
1073
|
+
kickRouteReadModel() {
|
|
1074
|
+
const publisher = this.routePublisher;
|
|
1075
|
+
if (!publisher || this.routeCallDepth > 0 || !this.routeLedgerVerified)
|
|
1076
|
+
return;
|
|
1077
|
+
// After a failure, only the alarm retries: a directory write meanwhile must not retry KV at once.
|
|
1078
|
+
if (Date.now() < this.routeRetryAfter)
|
|
1079
|
+
return;
|
|
1080
|
+
if (this.routeFlushing) {
|
|
1081
|
+
this.routeFlushAgain = true;
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
if (!publisher.pending())
|
|
1085
|
+
return;
|
|
1086
|
+
void this.ownRouteFlush(async () => {
|
|
1087
|
+
let result;
|
|
1088
|
+
do {
|
|
1089
|
+
this.routeFlushAgain = false;
|
|
1090
|
+
result = await publisher.flush(undefined, this.routeQuiet);
|
|
1091
|
+
// A deferred flush is restarted by the method it deferred to, when that one returns.
|
|
1092
|
+
} while (!result.deferred && (this.routeFlushAgain || result.remaining > 0));
|
|
1093
|
+
return result;
|
|
1094
|
+
}).catch(async (err) => {
|
|
1095
|
+
console.error('route read model: publish failed, retrying from the alarm', err);
|
|
1096
|
+
this.routeRetryAfter = Date.now() + ROUTE_PUBLISH_RETRY_MS;
|
|
1097
|
+
await this.armRouteReadModelRetry().catch(() => undefined);
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
/**
|
|
1101
|
+
* Run `flush` as THE flush: after any running one, and with every kick meanwhile queued behind it
|
|
1102
|
+
* as one more round. The only way a flush starts, so two never race the same key in KV.
|
|
1103
|
+
*/
|
|
1104
|
+
async ownRouteFlush(flush) {
|
|
1105
|
+
while (this.routeFlushing)
|
|
1106
|
+
await this.routeFlushing;
|
|
1107
|
+
let result;
|
|
1108
|
+
let failure;
|
|
1109
|
+
let failed = false;
|
|
1110
|
+
this.routeFlushing = (async () => {
|
|
1111
|
+
try {
|
|
1112
|
+
result = await flush();
|
|
1113
|
+
}
|
|
1114
|
+
catch (err) {
|
|
1115
|
+
failure = err;
|
|
1116
|
+
failed = true;
|
|
1117
|
+
}
|
|
1118
|
+
})();
|
|
1119
|
+
try {
|
|
1120
|
+
await this.routeFlushing;
|
|
1121
|
+
}
|
|
1122
|
+
finally {
|
|
1123
|
+
this.routeFlushing = null;
|
|
1124
|
+
}
|
|
1125
|
+
// A kick that arrived during this flush asked for another round.
|
|
1126
|
+
if (this.routeFlushAgain) {
|
|
1127
|
+
this.routeFlushAgain = false;
|
|
1128
|
+
this.kickRouteReadModel();
|
|
1129
|
+
}
|
|
1130
|
+
if (failed)
|
|
1131
|
+
throw failure;
|
|
1132
|
+
return result;
|
|
1133
|
+
}
|
|
1134
|
+
/** A retry no later than `ROUTE_PUBLISH_RETRY_MS` from now. An earlier alarm is kept. */
|
|
1135
|
+
async armRouteReadModelRetry() {
|
|
1136
|
+
const at = Date.now() + ROUTE_PUBLISH_RETRY_MS;
|
|
1137
|
+
const current = await this.ctx.storage.getAlarm();
|
|
1138
|
+
if (current === null || current > at)
|
|
1139
|
+
await this.ctx.storage.setAlarm(at);
|
|
1140
|
+
}
|
|
1141
|
+
/** The current flush, for a caller (and a test) that must see it land. */
|
|
1142
|
+
async settleRouteReadModel() {
|
|
1143
|
+
await this.routeLedgerCheck;
|
|
1144
|
+
this.kickRouteReadModel();
|
|
1145
|
+
while (this.routeFlushing)
|
|
1146
|
+
await this.routeFlushing;
|
|
1147
|
+
}
|
|
1148
|
+
/** The read model's counts, for the backfill script and staff. */
|
|
1149
|
+
routeReadModelStatus() {
|
|
1150
|
+
return this.routePublisher ? this.routePublisher.status() : { enabled: false };
|
|
1151
|
+
}
|
|
1152
|
+
/**
|
|
1153
|
+
* The one-off backfill (scripts/route-read-model-backfill.mjs). A dry run reports what the read
|
|
1154
|
+
* model holds and would hold, and writes nothing. `apply` marks every key dirty and publishes up
|
|
1155
|
+
* to `maxKeys` now; the rest is published by the flush rounds that follow, and the status says
|
|
1156
|
+
* how many are left.
|
|
1157
|
+
*/
|
|
1158
|
+
async publishRouteReadModel(opts = {}) {
|
|
1159
|
+
const publisher = this.routePublisher;
|
|
1160
|
+
if (!publisher || !opts.apply)
|
|
1161
|
+
return { status: this.routeReadModelStatus(), flushed: null };
|
|
1162
|
+
this.ctx.storage.transactionSync(() => publisher.markAll());
|
|
1163
|
+
// As THE flush, so a kick meanwhile queues a round instead of racing it.
|
|
1164
|
+
let flushed;
|
|
1165
|
+
try {
|
|
1166
|
+
flushed = await this.ownRouteFlush(() => publisher.flush(opts.maxKeys ?? 2_000, this.routeQuiet));
|
|
1167
|
+
}
|
|
1168
|
+
catch (err) {
|
|
1169
|
+
// As the automatic path does: the alarm retries, and the caller still sees the refusal.
|
|
1170
|
+
this.routeRetryAfter = Date.now() + ROUTE_PUBLISH_RETRY_MS;
|
|
1171
|
+
await this.armRouteReadModelRetry().catch(() => undefined);
|
|
1172
|
+
throw err;
|
|
1173
|
+
}
|
|
1174
|
+
// What this call's bound left queued is published by the rounds that follow, not left for the
|
|
1175
|
+
// next directory write or the scheduled pass.
|
|
1176
|
+
if (flushed.remaining > 0)
|
|
1177
|
+
this.kickRouteReadModel();
|
|
1178
|
+
return { status: publisher.status(), flushed };
|
|
1179
|
+
}
|
|
1180
|
+
/**
|
|
1181
|
+
* The scheduled pass's drift repair: one slice of every source. What it finds is queued in the
|
|
1182
|
+
* outbox (durable) and a flush is started, not awaited: the pass must not wait behind a publish
|
|
1183
|
+
* backlog, such as a large backfill over slow KV. A flush that fails arms the alarm retry, and a
|
|
1184
|
+
* caller that must see the publish land calls `settleRouteReadModel`.
|
|
1185
|
+
*/
|
|
1186
|
+
async reconcileRouteReadModel(opts = {}) {
|
|
1187
|
+
const publisher = this.routePublisher;
|
|
1188
|
+
if (!publisher)
|
|
1189
|
+
return null;
|
|
1190
|
+
const result = await publisher.reconcile(opts.limit);
|
|
1191
|
+
this.kickRouteReadModel();
|
|
1192
|
+
return result;
|
|
1193
|
+
}
|
|
990
1194
|
/**
|
|
991
1195
|
* Schedule the #1764 backfill when a version still carries its SQL in its manifest.
|
|
992
1196
|
*
|
|
@@ -1015,6 +1219,9 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
1015
1219
|
* meanwhile, because a version the backfill has not reached reads from its manifest.
|
|
1016
1220
|
*/
|
|
1017
1221
|
async alarm() {
|
|
1222
|
+
// #3395: the routing read model's retry. It lifts the backoff (and redoes a failed namespace
|
|
1223
|
+
// check); the flush itself starts when this alarm returns, as after any directory method.
|
|
1224
|
+
await this.retryRouteReadModel().catch((err) => console.error('route read model: retry failed', err));
|
|
1018
1225
|
let more;
|
|
1019
1226
|
try {
|
|
1020
1227
|
more = this.ctx.storage.transactionSync(() => splitVersionMigrationsBatch(this.kernelSql)).more;
|
|
@@ -1618,14 +1825,16 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
1618
1825
|
/**
|
|
1619
1826
|
* The tables a dump carries, by name. Not `_cf_*` either: workerd's own tables (the #1764
|
|
1620
1827
|
* backfill's alarm creates `_cf_METADATA`), which are not the directory's and which it refuses
|
|
1621
|
-
* to drop on restore.
|
|
1828
|
+
* to drop on restore. Nor the routing read model's (#3395), which record what KV holds.
|
|
1622
1829
|
*/
|
|
1623
1830
|
dumpTableDefs() {
|
|
1624
1831
|
return this.sql
|
|
1625
1832
|
.exec(`SELECT name, sql FROM sqlite_master
|
|
1626
1833
|
WHERE type = 'table' AND name NOT GLOB 'sqlite_*' AND name NOT GLOB '_cf_*'
|
|
1627
|
-
AND name NOT GLOB ? AND sql IS NOT NULL
|
|
1628
|
-
ORDER BY name`, COPY_GLOB
|
|
1834
|
+
AND name NOT GLOB ? AND name NOT GLOB ? AND sql IS NOT NULL
|
|
1835
|
+
ORDER BY name`, COPY_GLOB,
|
|
1836
|
+
// #3395: the read model's ledger describes KV, not the directory (route-read-model-publisher.ts).
|
|
1837
|
+
ROUTE_READ_MODEL_TABLE_GLOB)
|
|
1629
1838
|
.toArray();
|
|
1630
1839
|
}
|
|
1631
1840
|
/**
|
|
@@ -1653,14 +1862,23 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
1653
1862
|
// TABLE. Pure input validation, so it runs before the first DROP: a refused dump touches
|
|
1654
1863
|
// nothing.
|
|
1655
1864
|
assertReplayableDump(tables, { maxColumns: DO_SQL_LIMITS.columns });
|
|
1865
|
+
// #3395: the routing read model's tables describe KV, survive a restore, and are never in a dump
|
|
1866
|
+
// this code exported. One a dump names (in any case: SQLite's names are) is crafted, and loading
|
|
1867
|
+
// it would make the ledger lie about what KV holds.
|
|
1868
|
+
const routeTable = tables.find((t) => t.name.toLowerCase().startsWith('route_read_model_'));
|
|
1869
|
+
if (routeTable) {
|
|
1870
|
+
throw substratError('validation_failed', `refusing this dump: it names ${routeTable.name}, a table no directory dump carries`);
|
|
1871
|
+
}
|
|
1656
1872
|
await this.ctx.storage.transaction(async () => {
|
|
1657
1873
|
// #1713: the epoch this directory was in, read before the drops take it.
|
|
1658
1874
|
const epochBefore = this.lifecycleEpoch();
|
|
1659
1875
|
this.sql.exec('PRAGMA defer_foreign_keys = ON');
|
|
1660
1876
|
// `_cf_*` is workerd's, and dropping it is refused (SQLITE_AUTH), as `exportDump` says.
|
|
1877
|
+
// The routing read model's ledger survives too (#3395): it says what KV holds, which the
|
|
1878
|
+
// restore does not change, and the re-publish below needs it to delete what KV no longer may.
|
|
1661
1879
|
const existing = this.sql
|
|
1662
1880
|
.exec(`SELECT name FROM sqlite_master
|
|
1663
|
-
WHERE type = 'table' AND name NOT GLOB 'sqlite_*' AND name NOT GLOB '_cf_*'
|
|
1881
|
+
WHERE type = 'table' AND name NOT GLOB 'sqlite_*' AND name NOT GLOB '_cf_*' AND name NOT GLOB ?`, ROUTE_READ_MODEL_TABLE_GLOB)
|
|
1664
1882
|
.toArray();
|
|
1665
1883
|
for (const { name } of existing)
|
|
1666
1884
|
this.sql.exec(`DROP TABLE IF EXISTS "${name}"`);
|
|
@@ -1668,6 +1886,9 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
1668
1886
|
// rebuilds fires (each would reach `transactionSync`, which this async transaction cannot
|
|
1669
1887
|
// hold): every table is created on the current shape.
|
|
1670
1888
|
this.buildDirectorySchema({ holdSwitchRecords: [] });
|
|
1889
|
+
// #3395: the dropped tables took their triggers with them. Back before the load, so the
|
|
1890
|
+
// rows the dump brings in are captured like any other write.
|
|
1891
|
+
this.routePublisher?.install();
|
|
1671
1892
|
// Refuses a table this code does not build, adds unknown columns bare, loads the rows by
|
|
1672
1893
|
// name, then the legacy scope and #1674 backfills, all inside this transaction.
|
|
1673
1894
|
loadDirectoryDump(tables, {
|
|
@@ -1690,6 +1911,9 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
1690
1911
|
// admin table, inside this same import transaction.
|
|
1691
1912
|
if (audit)
|
|
1692
1913
|
this.recordAdmin(audit);
|
|
1914
|
+
// #3395: every key the restored directory routes, and every key KV was given before it —
|
|
1915
|
+
// a route the restored directory does not hold is deleted, not left serving.
|
|
1916
|
+
this.routePublisher?.markAll();
|
|
1693
1917
|
});
|
|
1694
1918
|
// A restore is a new #1764 backfill episode. It replaced the data the old failure count and
|
|
1695
1919
|
// backoff were about, so neither may delay or silence it: the count is cleared and the next
|
|
@@ -2635,12 +2859,15 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
2635
2859
|
};
|
|
2636
2860
|
}
|
|
2637
2861
|
/**
|
|
2638
|
-
*
|
|
2639
|
-
*
|
|
2640
|
-
*
|
|
2641
|
-
*
|
|
2862
|
+
* Request-path gate: only an active hostname, correctly paired active scope and active
|
|
2863
|
+
* owning tenant resolve (#1713). No lifecycle fields cross the wire: refusal is the existing
|
|
2864
|
+
* unresolved route. CP-less direct/background calls and already-dispatched requests are
|
|
2865
|
+
* outside this gate.
|
|
2866
|
+
*
|
|
2867
|
+
* `publish` (#3395) is the router's read-model miss: the route is queued for publishing
|
|
2868
|
+
* unless it is published already. The answer is the same either way.
|
|
2642
2869
|
*/
|
|
2643
|
-
readRoute(hostname) {
|
|
2870
|
+
readRoute(hostname, opts) {
|
|
2644
2871
|
// Join the scope's dispatch script, so the router resolves it in the same one
|
|
2645
2872
|
// directory read (orchestration.md §5.4). A scope whose data lives in the stable
|
|
2646
2873
|
// serving script (#286) routes THERE — per-scope truth, because rerouting a scope
|
|
@@ -2664,6 +2891,17 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
2664
2891
|
// #2005: the predicate runs here, once, rather than travelling as two columns for the
|
|
2665
2892
|
// router to re-derive — every reader of the directory answers it the same way.
|
|
2666
2893
|
const { kind, forked_from, scope_hostnames_json, ...route } = row;
|
|
2894
|
+
if (opts?.publish === true && this.routePublisher) {
|
|
2895
|
+
// Optional: a fill that cannot be queued (storage full) must not fail a read that succeeded. The
|
|
2896
|
+
// route stays unpublished, and the next miss asks again.
|
|
2897
|
+
try {
|
|
2898
|
+
this.routePublisher.requestPublish(hostname);
|
|
2899
|
+
this.kickRouteReadModel();
|
|
2900
|
+
}
|
|
2901
|
+
catch (err) {
|
|
2902
|
+
console.error('route read model: could not queue a miss for publishing', hostname, err);
|
|
2903
|
+
}
|
|
2904
|
+
}
|
|
2667
2905
|
return {
|
|
2668
2906
|
...route,
|
|
2669
2907
|
primary: isPrimaryScopeRow({ kind, forked_from }),
|
|
@@ -5276,4 +5514,54 @@ export class ControlPlaneDO extends DurableObject {
|
|
|
5276
5514
|
inputTokens: row.input_tokens, outputTokens: row.output_tokens }));
|
|
5277
5515
|
}
|
|
5278
5516
|
}
|
|
5517
|
+
/**
|
|
5518
|
+
* #3395: when the last running method of the directory returns, publish what its writes queued.
|
|
5519
|
+
*
|
|
5520
|
+
* The capture is the triggers (route-read-model-publisher.ts), which miss no write path; this is
|
|
5521
|
+
* the matching half for the publish, so it cannot be forgotten either: a method added later is
|
|
5522
|
+
* covered without naming it. The check is one read of an outbox that is empty almost always. It
|
|
5523
|
+
* runs once the method's promise settles, so a flush never reads the writes of a transaction its
|
|
5524
|
+
* own call has not finished. `readRoute` is the hot path and kicks only when it queued something.
|
|
5525
|
+
*/
|
|
5526
|
+
// The publishing methods drive the flush themselves, and count as no running method while they do.
|
|
5527
|
+
const ROUTE_KICK_EXEMPT = new Set([
|
|
5528
|
+
'constructor', 'readRoute', 'kickRouteReadModel', 'settleRouteReadModel', 'publishRouteReadModel',
|
|
5529
|
+
'reconcileRouteReadModel', 'routeReadModelStatus', 'ownRouteFlush', 'routeQuiet',
|
|
5530
|
+
// The retry path: its own return must not start a flush, or a failure is retried at once rather than
|
|
5531
|
+
// after `ROUTE_PUBLISH_RETRY_MS`; the alarm starts it (`retryRouteReadModel`).
|
|
5532
|
+
'armRouteReadModelRetry', 'retryRouteReadModel',
|
|
5533
|
+
]);
|
|
5534
|
+
for (const name of Object.getOwnPropertyNames(ControlPlaneDO.prototype)) {
|
|
5535
|
+
if (ROUTE_KICK_EXEMPT.has(name))
|
|
5536
|
+
continue;
|
|
5537
|
+
const descriptor = Object.getOwnPropertyDescriptor(ControlPlaneDO.prototype, name);
|
|
5538
|
+
if (!descriptor || typeof descriptor.value !== 'function')
|
|
5539
|
+
continue;
|
|
5540
|
+
const original = descriptor.value;
|
|
5541
|
+
Object.defineProperty(ControlPlaneDO.prototype, name, {
|
|
5542
|
+
...descriptor,
|
|
5543
|
+
value: function (...args) {
|
|
5544
|
+
// Counted in and out, so only the OUTERMOST return kicks: a private helper returning inside a
|
|
5545
|
+
// transaction (`bumpLifecycleRevision` within `writeAndAudit`) must not start a flush there.
|
|
5546
|
+
const leave = () => {
|
|
5547
|
+
this.routeCallDepth -= 1;
|
|
5548
|
+
if (this.routeCallDepth === 0)
|
|
5549
|
+
this.kickRouteReadModel();
|
|
5550
|
+
};
|
|
5551
|
+
this.routeCallDepth += 1;
|
|
5552
|
+
let out;
|
|
5553
|
+
try {
|
|
5554
|
+
out = original.apply(this, args);
|
|
5555
|
+
}
|
|
5556
|
+
catch (err) {
|
|
5557
|
+
leave();
|
|
5558
|
+
throw err;
|
|
5559
|
+
}
|
|
5560
|
+
if (out instanceof Promise)
|
|
5561
|
+
return out.finally(leave);
|
|
5562
|
+
leave();
|
|
5563
|
+
return out;
|
|
5564
|
+
},
|
|
5565
|
+
});
|
|
5566
|
+
}
|
|
5279
5567
|
//# sourceMappingURL=control-plane-do.js.map
|