@zabaca/zbc 0.22.0 → 0.23.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/package.json +1 -1
- package/templates/CLAUDE.md +5 -0
- package/templates/apps/walgit/CONTEXT.md +5 -1
- package/templates/apps/walgit/README.md +159 -24
- package/templates/apps/walgit/package.json +2 -2
- package/templates/apps/walgit/registry.json +1 -1
- package/templates/apps/walgit/shared/browse.ts +18 -4
- package/templates/apps/walgit/shared/capabilities.ts +28 -3
- package/templates/apps/walgit/shared/container-env.ts +41 -2
- package/templates/apps/walgit/shared/container-lifecycle.ts +239 -0
- package/templates/apps/walgit/shared/container-snapshot.ts +219 -0
- package/templates/apps/walgit/shared/edge-refs.ts +612 -0
- package/templates/apps/walgit/{src → shared}/instructions.ts +8 -4
- package/templates/apps/walgit/shared/keys.ts +17 -0
- package/templates/apps/walgit/shared/landing.ts +10 -3
- package/templates/apps/walgit/shared/llms.ts +14 -5
- package/templates/apps/walgit/shared/policy.ts +1 -1
- package/templates/apps/walgit/shared/protocol.ts +35 -0
- package/templates/apps/walgit/shared/sleep-after.ts +44 -0
- package/templates/apps/walgit/shared/wal-index.ts +22 -1
- package/templates/apps/walgit/src/boot-residue.ts +58 -0
- package/templates/apps/walgit/src/cache.ts +9 -0
- package/templates/apps/walgit/src/cli.ts +39 -4
- package/templates/apps/walgit/src/delete-repo.ts +58 -9
- package/templates/apps/walgit/src/edge-refs.ts +148 -0
- package/templates/apps/walgit/src/expire.ts +75 -7
- package/templates/apps/walgit/src/hook-main.ts +2 -0
- package/templates/apps/walgit/src/http.ts +17 -4
- package/templates/apps/walgit/src/materialize.ts +34 -15
- package/templates/apps/walgit/src/push.ts +34 -8
- package/templates/apps/walgit/src/reconcile.ts +6 -1
- package/templates/apps/walgit/src/server.ts +69 -8
- package/templates/apps/walgit/src/sync.ts +10 -2
- package/templates/apps/walgit/worker/container-binding.ts +104 -0
- package/templates/apps/walgit/worker/durable-container.ts +786 -0
- package/templates/apps/walgit/worker/events-do.ts +16 -12
- package/templates/apps/walgit/worker/index.ts +235 -17
- package/templates/apps/walgit/worker/mcp.ts +9 -4
- package/templates/apps/walgit/wrangler.jsonc +44 -33
- package/templates/claude/settings.json +8 -0
- package/templates/infra/modules/cloudflare/index.ts +9 -0
- package/templates/infra/modules/d1/index.ts +30 -4
- package/templates/infra/modules/d1/registry.json +1 -1
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When the container stops, how it is sized, and which failed request may be
|
|
3
|
+
* tried again — the decisions `WalgitDurableContainer` (worker/durable-container.ts)
|
|
4
|
+
* makes about the container's lifetime, as pure functions.
|
|
5
|
+
*
|
|
6
|
+
* They used to be `@cloudflare/containers`' decisions, made inside its
|
|
7
|
+
* `Container` class: a `sleepAfter` timer, an alarm loop that polled the
|
|
8
|
+
* container every few minutes for as long as it ran, and a 500 the library
|
|
9
|
+
* wrote itself when a request landed on a container that had just exited. That
|
|
10
|
+
* class does not support the `durable_object` scheduling policy, so the
|
|
11
|
+
* Durable Object now drives `ctx.container` directly and owns every one of
|
|
12
|
+
* those rules. Here, rather than beside the runtime calls, for the reason
|
|
13
|
+
* `container-env.ts` is: the ORDER of a lifecycle is only observable from
|
|
14
|
+
* outside the object performing it, and a rule that can only be tested by
|
|
15
|
+
* deploying is a rule nobody tests.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The instance size the container is started with.
|
|
20
|
+
*
|
|
21
|
+
* `standard-1` — ½ vCPU, 4 GiB, 8 GB of disk — and it is the smallest size
|
|
22
|
+
* that fits, not a choice between two that do. The `default`-policy
|
|
23
|
+
* application ran `basic` (¼ vCPU, 1 GiB, 4 GB), which the `durable_object`
|
|
24
|
+
* policy does not accept at runtime, and Cloudflare's own mapping offers `lite`
|
|
25
|
+
* or `standard-1` in its place. `lite` is 256 MiB: below the ~330 MiB a
|
|
26
|
+
* compaction of a `WALGIT_MAX_REPO_BYTES`-sized repository peaks at even after
|
|
27
|
+
* its extra pack copy is gone, and far below the ~583 MiB it peaks at with it
|
|
28
|
+
* (the sizing note in wrangler.jsonc). A container killed for memory in the
|
|
29
|
+
* middle of a compaction loses nothing — the lease and the CAS see to that —
|
|
30
|
+
* but it does it again on the next push, forever. A custom size cannot go
|
|
31
|
+
* between the two: custom instances start at 1 vCPU.
|
|
32
|
+
*
|
|
33
|
+
* The disk is the other half of the choice, and it matters more than it did.
|
|
34
|
+
* With snapshots on, the Cache survives a sleep, so it grows to what the
|
|
35
|
+
* deployment serves rather than to what one waking hour touched; 8 GB holds a
|
|
36
|
+
* `SNAPSHOT_MAX_BYTES` restore with as much again free beside it.
|
|
37
|
+
*
|
|
38
|
+
* Changing it needs a new container, and nothing here forces one: the size is
|
|
39
|
+
* read at the next start. Ship it with the deploy that changes it, and the
|
|
40
|
+
* deploy's own `WALGIT_BUILD_ID` replaces the running container.
|
|
41
|
+
*/
|
|
42
|
+
export const CONTAINER_INSTANCE = 'standard-1'
|
|
43
|
+
|
|
44
|
+
/** src/server.ts's PORT default, and the Dockerfile's. */
|
|
45
|
+
export const CONTAINER_PORT = 8080
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* How long one start may wait for `CONTAINER_PORT` to answer. `start()`
|
|
49
|
+
* returns before the process is up, so readiness is asked of the port itself.
|
|
50
|
+
*
|
|
51
|
+
* Short on purpose, and ONE attempt per request. Under the `durable_object`
|
|
52
|
+
* policy a container is up in well under a second and src/server.ts listens a
|
|
53
|
+
* second after that; a start that has not answered in ten has failed for a
|
|
54
|
+
* reason a second try will not fix — most sharply, an application that does not
|
|
55
|
+
* exist yet, which is what every start meets between a deploy putting the
|
|
56
|
+
* Worker live and wrangler creating the application behind it. Under the
|
|
57
|
+
* library, three tries at 28 s each held git for two minutes before saying so;
|
|
58
|
+
* now the request fails in seconds, and the next one tries again.
|
|
59
|
+
*/
|
|
60
|
+
export const READY_TIMEOUT_MS = 10_000
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The whole of what one request may spend starting a container: a snapshot
|
|
64
|
+
* restore that does not come up, then the fresh start that replaces it.
|
|
65
|
+
*/
|
|
66
|
+
export const BOOT_BUDGET_MS = 15_000
|
|
67
|
+
|
|
68
|
+
/** The least a fresh start is given, however much a failed restore spent. */
|
|
69
|
+
const MIN_FRESH_START_MS = 5_000
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* How long the fresh start may wait, after `elapsedMs` already went on a
|
|
73
|
+
* restore. Never below `MIN_FRESH_START_MS`, so a slow failed restore cannot
|
|
74
|
+
* leave the image too little time to come up at all.
|
|
75
|
+
*/
|
|
76
|
+
export function freshStartTimeoutMs(elapsedMs: number): number {
|
|
77
|
+
return Math.min(READY_TIMEOUT_MS, Math.max(BOOT_BUDGET_MS - elapsedMs, MIN_FRESH_START_MS))
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Between two readiness probes. The library's interval. */
|
|
81
|
+
export const READY_POLL_MS = 300
|
|
82
|
+
|
|
83
|
+
/** One readiness probe. The library's: a port that has not answered in 5 s is not up. */
|
|
84
|
+
export const READY_PROBE_TIMEOUT_MS = 5_000
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* How long a SIGTERM'd container has to exit before it is killed.
|
|
88
|
+
*
|
|
89
|
+
* The idle stop only ever fires with nothing in flight (`idleVerdict`), and
|
|
90
|
+
* src/server.ts drains and exits on SIGTERM, so in practice the process is gone
|
|
91
|
+
* well inside this. It is a bound for the case where it is not — a detached
|
|
92
|
+
* compaction holding the process up — and a compaction killed here loses
|
|
93
|
+
* nothing: its lease expires and the next push re-triggers it.
|
|
94
|
+
*/
|
|
95
|
+
export const STOP_GRACE_MS = 15_000
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The ceiling `setInactivityTimeout` accepts. A longer timeout makes the
|
|
99
|
+
* promise reject, which would surface as a start that failed for a reason
|
|
100
|
+
* unrelated to starting.
|
|
101
|
+
*/
|
|
102
|
+
export const MAX_INACTIVITY_TIMEOUT_MS = 6 * 60 * 60 * 1000
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* What the platform's own inactivity timeout is set to, given the idle stop.
|
|
106
|
+
*
|
|
107
|
+
* The two are not the same mechanism and must not race. The ALARM is the stop
|
|
108
|
+
* walgit wants: it fires `sleepAfterMs` after the last request finished,
|
|
109
|
+
* snapshots the disk and stops the container gracefully. The INACTIVITY TIMEOUT
|
|
110
|
+
* is the platform's: without one, Cloudflare stops the container shortly after
|
|
111
|
+
* the Durable Object goes idle — seconds, not minutes — so it is what lets the
|
|
112
|
+
* object sleep while its container stays up. It runs from when the object
|
|
113
|
+
* became inactive, which is at or after the last request, so `sleepAfterMs`
|
|
114
|
+
* plus a margin lands after the alarm, every time.
|
|
115
|
+
*
|
|
116
|
+
* It is therefore a backstop and never the normal path: it stops the container
|
|
117
|
+
* only if the alarm did not, and a container stopped that way leaves no
|
|
118
|
+
* snapshot behind. That costs a cold start, never correctness.
|
|
119
|
+
*/
|
|
120
|
+
export function inactivityTimeoutMs(sleepAfterMs: number): number {
|
|
121
|
+
const margin = 2 * 60 * 1000
|
|
122
|
+
return Math.min(sleepAfterMs + margin, MAX_INACTIVITY_TIMEOUT_MS)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The longest one request may count as in flight.
|
|
127
|
+
*
|
|
128
|
+
* In flight is what holds the idle stop off, and it ends when a response's
|
|
129
|
+
* last byte leaves — which depends on whoever is reading it. A caller that
|
|
130
|
+
* drops a response without reading or cancelling its body leaves a stream that
|
|
131
|
+
* may never finish, and one such request would keep the container up forever:
|
|
132
|
+
* the alarm sees it in flight and re-arms, every time. An hour is far past any
|
|
133
|
+
* clone or push this host takes (the push cap is 99 MiB), so a request still
|
|
134
|
+
* counted after it is let go of, and the idle clock starts.
|
|
135
|
+
*/
|
|
136
|
+
export const INFLIGHT_MAX_MS = 60 * 60 * 1000
|
|
137
|
+
|
|
138
|
+
/** What the idle alarm decided. */
|
|
139
|
+
export type IdleVerdict = { stop: true } | { stop: false; at: number }
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Has the container been idle for `sleepAfterMs`?
|
|
143
|
+
*
|
|
144
|
+
* Idle means NOTHING IN FLIGHT, and that is the rule the old timer broke in
|
|
145
|
+
* spirit if not in letter: a clone of a large repository is one request that
|
|
146
|
+
* streams for minutes, a push is one request whose body uploads for as long,
|
|
147
|
+
* and neither may be cut off because the request that STARTED it is older than
|
|
148
|
+
* the window. So any request in flight postpones the verdict by a whole window,
|
|
149
|
+
* and the window is measured from when the last request FINISHED.
|
|
150
|
+
*
|
|
151
|
+
* `lastActivity` is null when the object has no memory of a request — it was
|
|
152
|
+
* evicted and reconstructed by the alarm itself. An evicted object had nothing
|
|
153
|
+
* in flight (a request in flight keeps it resident), and the alarm that woke it
|
|
154
|
+
* was armed for `sleepAfterMs` after its last request, so it is due.
|
|
155
|
+
*/
|
|
156
|
+
export function idleVerdict(state: {
|
|
157
|
+
now: number
|
|
158
|
+
inflight: number
|
|
159
|
+
lastActivity: number | null
|
|
160
|
+
sleepAfterMs: number
|
|
161
|
+
}): IdleVerdict {
|
|
162
|
+
const { now, inflight, lastActivity, sleepAfterMs } = state
|
|
163
|
+
if (inflight > 0) return { stop: false, at: now + sleepAfterMs }
|
|
164
|
+
if (lastActivity === null) return { stop: true }
|
|
165
|
+
const due = lastActivity + sleepAfterMs
|
|
166
|
+
return due <= now ? { stop: true } : { stop: false, at: due }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Where the alarm should be after a request finishes — or `null` to leave it.
|
|
171
|
+
*
|
|
172
|
+
* Every finished request moves the deadline, and `setAlarm` is a storage write,
|
|
173
|
+
* so moving it on every request would put a write on the end of every clone.
|
|
174
|
+
* Instead an alarm already armed within `slack` of the new deadline is left
|
|
175
|
+
* where it is: when it fires early, `idleVerdict` reads the newer
|
|
176
|
+
* `lastActivity` and re-arms for the true deadline. The only cost of the slack
|
|
177
|
+
* is the one case where that memory is gone — an object evicted between the
|
|
178
|
+
* request and its alarm stops the container up to `slack` early, which is a
|
|
179
|
+
* tenth of the window and never more than half a minute.
|
|
180
|
+
*
|
|
181
|
+
* `scheduled` is what this object last armed, or null when it does not know
|
|
182
|
+
* (it was constructed since). Not knowing re-arms, which is always correct.
|
|
183
|
+
*/
|
|
184
|
+
export function rearmAt(state: {
|
|
185
|
+
now: number
|
|
186
|
+
scheduled: number | null
|
|
187
|
+
sleepAfterMs: number
|
|
188
|
+
}): number | null {
|
|
189
|
+
const { now, scheduled, sleepAfterMs } = state
|
|
190
|
+
const deadline = now + sleepAfterMs
|
|
191
|
+
const slack = Math.min(sleepAfterMs / 10, 30_000)
|
|
192
|
+
if (scheduled !== null && scheduled >= deadline - slack) return null
|
|
193
|
+
return deadline
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* May a request that failed to reach the container be sent again?
|
|
198
|
+
*
|
|
199
|
+
* Once, after a beat, and only a bodiless read. The failure this exists for is
|
|
200
|
+
* the gap between a container exiting — the platform's backstop stop, a crash,
|
|
201
|
+
* a host going away — and the object noticing: the proxy's connection fails
|
|
202
|
+
* where the container was, and a second attempt starts a fresh one. Under the
|
|
203
|
+
* `Container` class that gap surfaced as a 500 the library wrote itself, and
|
|
204
|
+
* the same rule retried it (8a6c4fe); here it surfaces as the proxy's own
|
|
205
|
+
* thrown error, and the rule is unchanged.
|
|
206
|
+
*
|
|
207
|
+
* A push is never retried: its body has been consumed by the first attempt, and
|
|
208
|
+
* a pack is not something to replay on a guess. `HEAD` and `GET` carry none.
|
|
209
|
+
*/
|
|
210
|
+
export function mayRetry(method: string, attempt: number): boolean {
|
|
211
|
+
return attempt === 0 && (method === 'GET' || method === 'HEAD')
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** How long to wait before that one retry. */
|
|
215
|
+
export const RETRY_DELAY_MS = 500
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Which container application the Worker routes to, during the move between
|
|
219
|
+
* scheduling policies — `WALGIT_CONTAINER_POLICY`, edge-only.
|
|
220
|
+
*
|
|
221
|
+
* Two applications exist for a while, and the Worker must not reach the new
|
|
222
|
+
* one before it does. `wrangler deploy` puts the Worker live FIRST, rolls the
|
|
223
|
+
* `default`-policy application, and only then creates the `durable_object` one
|
|
224
|
+
* — so a deploy that both introduced the new application and routed to it would
|
|
225
|
+
* answer every git request with a failed start until wrangler caught up, and
|
|
226
|
+
* forever if the rollout in between threw. So the deploy that introduces it
|
|
227
|
+
* routes nowhere new, and a second deploy, setting this to `durable_object`,
|
|
228
|
+
* moves the traffic (README.md, "Deployment").
|
|
229
|
+
*
|
|
230
|
+
* Anything but the exact word is the old application: a typo must leave a
|
|
231
|
+
* deployment where it was, not move it.
|
|
232
|
+
*/
|
|
233
|
+
export type ContainerPolicy = 'default' | 'durable_object'
|
|
234
|
+
|
|
235
|
+
export function containerPolicyFrom(env: { WALGIT_CONTAINER_POLICY?: string }): ContainerPolicy {
|
|
236
|
+
return (env.WALGIT_CONTAINER_POLICY ?? '').trim() === 'durable_object'
|
|
237
|
+
? 'durable_object'
|
|
238
|
+
: 'default'
|
|
239
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filesystem snapshots of the container: when one is taken, and when one is
|
|
3
|
+
* trusted enough to start from.
|
|
4
|
+
*
|
|
5
|
+
* A cold start used to cost two things — the container booting, and every
|
|
6
|
+
* repository it was asked for being materialized from the log onto an empty
|
|
7
|
+
* disk. On agentgit that was 12–22 s, and the second half was most of it. The
|
|
8
|
+
* `durable_object` scheduling policy can snapshot a running container's root
|
|
9
|
+
* filesystem and start a later one from it, so the Durable Object snapshots on
|
|
10
|
+
* its way to the idle stop and the next start restores the Cache the last one
|
|
11
|
+
* left (`WalgitDurableContainer`, worker/durable-container.ts).
|
|
12
|
+
*
|
|
13
|
+
* WHY THIS IS SAFE is the Cache's own definition, not anything here. The disk
|
|
14
|
+
* is reconciled against `index.json` on every access (src/sync.ts): the Index
|
|
15
|
+
* is read in full every time, refs are rewritten to match it whatever the disk
|
|
16
|
+
* believed (src/reconcile.ts), and objects a ref needs that the disk lacks are
|
|
17
|
+
* materialized. So a restored disk can be minutes or weeks behind the log and
|
|
18
|
+
* the only thing that varies is how much a first access downloads — a stale
|
|
19
|
+
* snapshot costs a re-sync and can never serve a stale ref. What a disk carries
|
|
20
|
+
* that is NOT reconciled — the hand-off records of a push killed mid-flight, a
|
|
21
|
+
* materialize lock — is cleared by the process at boot, before it serves
|
|
22
|
+
* anything (src/boot-residue.ts).
|
|
23
|
+
*
|
|
24
|
+
* Public beta when this was written, which is why it is a switch rather than
|
|
25
|
+
* the behaviour: `WALGIT_SNAPSHOTS`, off unless set, edge-only. Off means every
|
|
26
|
+
* start is the cold start walgit was built for, which is still correct.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { fingerprintEnv } from './container-env'
|
|
30
|
+
import { flagEnabled } from './policy'
|
|
31
|
+
|
|
32
|
+
/** `1` or `true` turns snapshots on, like every walgit flag (`shared/policy.ts`). */
|
|
33
|
+
export function snapshotsEnabled(env: { WALGIT_SNAPSHOTS?: string }): boolean {
|
|
34
|
+
return flagEnabled(env.WALGIT_SNAPSHOTS)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* What the Durable Object keeps about its one snapshot.
|
|
39
|
+
*
|
|
40
|
+
* The platform has no way to list snapshots, so this record is the only place
|
|
41
|
+
* one is remembered — and with it, the two facts that decide whether it may be
|
|
42
|
+
* restored at all.
|
|
43
|
+
*/
|
|
44
|
+
export interface SnapshotRecord {
|
|
45
|
+
/** The handle's id — what `start({ containerSnapshot })` is given. */
|
|
46
|
+
id: string
|
|
47
|
+
/** Bytes, as the platform reported them. */
|
|
48
|
+
size: number
|
|
49
|
+
/**
|
|
50
|
+
* The image reference the snapshotted container booted from. A snapshot is
|
|
51
|
+
* tied to its image version and is not portable to another one.
|
|
52
|
+
*/
|
|
53
|
+
image: string
|
|
54
|
+
/** Which log the Cache on this disk was reconciled against (`storeIdentity`). */
|
|
55
|
+
store: string
|
|
56
|
+
takenAt: number
|
|
57
|
+
/** Creation, or the most recent restore — the platform's TTL runs from here. */
|
|
58
|
+
refreshedAt: number
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* How long the platform keeps a snapshot nobody restores: 30 days from
|
|
63
|
+
* creation or from the last restore, not configurable.
|
|
64
|
+
*/
|
|
65
|
+
export const SNAPSHOT_TTL_MS = 30 * 24 * 60 * 60 * 1000
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A day short of the TTL. A restore that names an expired snapshot does not
|
|
69
|
+
* fail cheaply — it fails as a container that never comes up, after the
|
|
70
|
+
* readiness wait — so a record near the edge is treated as gone.
|
|
71
|
+
*/
|
|
72
|
+
const TTL_MARGIN_MS = 24 * 60 * 60 * 1000
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The largest snapshot worth restoring: half of `CONTAINER_INSTANCE`'s 8 GB.
|
|
76
|
+
*
|
|
77
|
+
* The bound is on the disk, not on the platform (whose limit is 20 GB). Before
|
|
78
|
+
* snapshots the Cache was emptied by every sleep, which was also the only thing
|
|
79
|
+
* that ever emptied it; restored, it grows to whatever the deployment serves.
|
|
80
|
+
* A disk that is half Cache before the first request leaves the other half for
|
|
81
|
+
* the pushes and materializations that follow, and one past that is started
|
|
82
|
+
* fresh instead — the cold start walgit always paid, once, after which the
|
|
83
|
+
* snapshots are small again.
|
|
84
|
+
*/
|
|
85
|
+
export const SNAPSHOT_MAX_BYTES = 4 * 1000 * 1000 * 1000
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Which log a Cache was built against, as a short digest.
|
|
89
|
+
*
|
|
90
|
+
* Part of the identity because the disk is a cache OF a log: pointed at another
|
|
91
|
+
* bucket, reconcile would still make the refs right, but packs named after the
|
|
92
|
+
* old log's WAL keys would be on a disk that has nothing to do with the new one.
|
|
93
|
+
* Starting fresh is the honest answer when the log moved.
|
|
94
|
+
*/
|
|
95
|
+
export function storeIdentity(env: {
|
|
96
|
+
WALGIT_S3_ENDPOINT?: string
|
|
97
|
+
WALGIT_S3_BUCKET?: string
|
|
98
|
+
}): string {
|
|
99
|
+
return fingerprintEnv({
|
|
100
|
+
endpoint: env.WALGIT_S3_ENDPOINT ?? '',
|
|
101
|
+
bucket: env.WALGIT_S3_BUCKET ?? '',
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Why a start is not from the snapshot. Logged, so each is its own word. */
|
|
106
|
+
export type FreshReason =
|
|
107
|
+
| 'disabled'
|
|
108
|
+
| 'none'
|
|
109
|
+
| 'other-image'
|
|
110
|
+
| 'other-store'
|
|
111
|
+
| 'expired'
|
|
112
|
+
| 'too-large'
|
|
113
|
+
|
|
114
|
+
export type RestorePlan = { from: 'snapshot'; id: string } | { from: 'image'; reason: FreshReason }
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Start from the snapshot, or from the image?
|
|
118
|
+
*
|
|
119
|
+
* The snapshot only when every one of these holds, in this order: snapshots
|
|
120
|
+
* are on; one is recorded; it was taken from the image this deploy would start
|
|
121
|
+
* (a snapshot from another image is not portable to this one, and must not be
|
|
122
|
+
* tried); its Cache was built against this log; the platform still has it; and
|
|
123
|
+
* it is small enough to leave the disk room. Anything else is a fresh start,
|
|
124
|
+
* which is never wrong — only slower.
|
|
125
|
+
*/
|
|
126
|
+
export function restorePlan(state: {
|
|
127
|
+
enabled: boolean
|
|
128
|
+
record: SnapshotRecord | null
|
|
129
|
+
image: string
|
|
130
|
+
store: string
|
|
131
|
+
now: number
|
|
132
|
+
}): RestorePlan {
|
|
133
|
+
const { enabled, record, image, store, now } = state
|
|
134
|
+
if (!enabled) return { from: 'image', reason: 'disabled' }
|
|
135
|
+
if (!record) return { from: 'image', reason: 'none' }
|
|
136
|
+
if (record.image !== image) return { from: 'image', reason: 'other-image' }
|
|
137
|
+
if (record.store !== store) return { from: 'image', reason: 'other-store' }
|
|
138
|
+
if (now - record.refreshedAt >= SNAPSHOT_TTL_MS - TTL_MARGIN_MS) {
|
|
139
|
+
return { from: 'image', reason: 'expired' }
|
|
140
|
+
}
|
|
141
|
+
if (record.size > SNAPSHOT_MAX_BYTES) return { from: 'image', reason: 'too-large' }
|
|
142
|
+
return { from: 'snapshot', id: record.id }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Is the running container worth snapshotting on its way to a stop?
|
|
147
|
+
*
|
|
148
|
+
* Only one that booted from the image this deploy would start — directly, or
|
|
149
|
+
* from a snapshot of that image. One booted from anything older would produce a
|
|
150
|
+
* snapshot `restorePlan` refuses on sight, which costs the time it takes and
|
|
151
|
+
* replaces a record that might still be good.
|
|
152
|
+
*/
|
|
153
|
+
export function shouldSnapshot(state: {
|
|
154
|
+
enabled: boolean
|
|
155
|
+
bootedImage: string | null
|
|
156
|
+
image: string
|
|
157
|
+
}): boolean {
|
|
158
|
+
return state.enabled && state.bootedImage !== null && state.bootedImage === state.image
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Where the container marks its own boot, and what the marker holds.
|
|
163
|
+
*
|
|
164
|
+
* Written by `src/server.ts` once boot residue is cleared and before the port
|
|
165
|
+
* opens, holding the repos directory's path. Every file the cache changes after
|
|
166
|
+
* that is newer than the marker, which is how a stop asks "did anything change
|
|
167
|
+
* since this disk was restored?" without the Durable Object knowing where the
|
|
168
|
+
* cache lives. In `/tmp` and rewritten on every boot, so the copy a snapshot
|
|
169
|
+
* carries is always replaced before it could be compared against.
|
|
170
|
+
*/
|
|
171
|
+
export const BOOT_MARKER = '/tmp/walgit-booted'
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The command that answers it: prints a changed path, or nothing.
|
|
175
|
+
*
|
|
176
|
+
* `head -1` rather than `find -quit`, which busybox's `find` may not have.
|
|
177
|
+
* Paths are taken from the marker, so this never names the cache directly.
|
|
178
|
+
*/
|
|
179
|
+
export const DISK_CHANGED_COMMAND = [
|
|
180
|
+
'sh',
|
|
181
|
+
'-c',
|
|
182
|
+
`find "$(cat ${BOOT_MARKER})" -newer ${BOOT_MARKER} | head -1`,
|
|
183
|
+
]
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Does this stop need a new snapshot, or does the one it booted from still hold?
|
|
187
|
+
*
|
|
188
|
+
* A container booted from a snapshot whose cache did not change since has
|
|
189
|
+
* nothing new to save: the record it restored is the same disk, and taking
|
|
190
|
+
* another costs seconds on every stop — which, with a short idle timeout, is
|
|
191
|
+
* most of what a quiet deployment does. Anything uncertain snapshots: a fresh
|
|
192
|
+
* boot from the image, or a change check that could not answer (`null`).
|
|
193
|
+
*/
|
|
194
|
+
export function needsNewSnapshot(state: {
|
|
195
|
+
bootedFrom: 'snapshot' | 'image' | null
|
|
196
|
+
changed: boolean | null
|
|
197
|
+
}): boolean {
|
|
198
|
+
return state.bootedFrom !== 'snapshot' || state.changed !== false
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** The record a fresh snapshot handle becomes. */
|
|
202
|
+
export function recordSnapshot(
|
|
203
|
+
handle: { id: string; size: number },
|
|
204
|
+
context: { image: string; store: string; now: number },
|
|
205
|
+
): SnapshotRecord {
|
|
206
|
+
return {
|
|
207
|
+
id: handle.id,
|
|
208
|
+
size: handle.size,
|
|
209
|
+
image: context.image,
|
|
210
|
+
store: context.store,
|
|
211
|
+
takenAt: context.now,
|
|
212
|
+
refreshedAt: context.now,
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The same record after a successful restore, which restarts the TTL. */
|
|
217
|
+
export function refreshSnapshot(record: SnapshotRecord, now: number): SnapshotRecord {
|
|
218
|
+
return { ...record, refreshedAt: now }
|
|
219
|
+
}
|