main
ply4 git calls181 ms43 KiB2 cachedprotocol v2
166 lines7.0 KiBRaw
1import {
2 assertSplit,
3 deploymentBody,
4 fullSplit,
5 isFullRollback,
6 isTrafficSplit,
7 parseDeployment,
8 sameSplit,
9 type TrafficSplit,
10 type VersionShare,
11} from './deploy-plan.ts'
12import { cfOk } from './cf.ts'
13import { extractJson } from './json.ts'
14import { activeLog } from './log.ts'
15import { errorMessage } from './proc.ts'
16import { HarnessFault, withTrafficLock, withWriteIntent, type WriteIntent } from './traffic-gate.ts'
18export type Deployment = {
19 readonly id: string | null
20 readonly versions: readonly VersionShare[]
23export async function readDeployment(worker: string, secrets: readonly string[] = []): Promise<Deployment> {
24 const stdout = await cfOk(['workers', 'deployments', 'list', '--worker', worker], secrets)
25 return parseDeployment(extractJson(stdout))
28export async function readShares(worker: string, secrets: readonly string[] = []): Promise<VersionShare[]> {
29 return [...(await readDeployment(worker, secrets)).versions]
32export async function putSplit(worker: string, message: string, split: TrafficSplit, secrets: readonly string[] = []): Promise<void> {
33 const body = deploymentBody(message, split)
34 await cfOk(['workers', 'deployments', 'create', '--worker', worker, '--body', JSON.stringify(body)], secrets)
37/**
38 * `cf workers deployments list` is eventually consistent after a create: run 2026-11-12T21-21-31-209Z created the 10%
39 * split (4918caf9, 21:24:12.338Z) and the read 1 ms after the create returned still listed the prior deployment
40 * (9f0dc7f0, 0%), so the step was judged rejected, the rollback's compare-and-swap then saw 10% and stopped with
41 * state_conflict, and the ramp was left at 10%. The read-back now waits for the write to show.
42 */
43export type ReadBackPolicy = { readonly attempts: number; readonly gapMs: number }
44export const DEFAULT_READ_BACK: ReadBackPolicy = { attempts: 10, gapMs: 1_000 }
45let readBack: ReadBackPolicy = DEFAULT_READ_BACK
47/** Tests: a shorter read-back. Null restores the default. */
48export function useReadBack(policy: ReadBackPolicy | null): void {
49 readBack = policy === null ? DEFAULT_READ_BACK : policy
52/** What one read after a create shows. Pure. */
53export type ReadBack =
54 | { readonly kind: 'applied' }
55 | { readonly kind: 'stale' }
56 | { readonly kind: 'mismatch'; readonly reason: string }
58export function classifyReadBack(before: Deployment, after: Deployment, split: TrafficSplit): ReadBack {
59 const mismatch = assertSplit(after.versions, split)
60 if (mismatch === null) return { kind: 'applied' }
61 // Still the deployment the write replaced (same id, or no ids and the same shares): the list has not caught up.
62 if (before.id !== null && after.id === before.id) return { kind: 'stale' }
63 if (before.id === null && after.id === null && sameSplit(after.versions, before.versions)) return { kind: 'stale' }
64 return { kind: 'mismatch', reason: mismatch }
67export type ApplyResult =
68 | { readonly ok: true }
69 | { readonly ok: false; readonly kind: 'state_conflict'; readonly reason: string }
70 | { readonly ok: false; readonly kind: 'rejected'; readonly reason: string }
72/**
73 * Compare-and-swap. Read the deployment and put `split` only when it is still `expected`.
74 * A mismatch emits `state_conflict` and does not call cf. The caller stops; it does not roll back.
75 * The write runs under the process lock, and only for `intent`.
76 */
77export async function applySplit(
78 worker: string,
79 message: string,
80 split: TrafficSplit,
81 expected: readonly VersionShare[],
82 intent: WriteIntent,
83 secrets: readonly string[] = [],
84): Promise<ApplyResult> {
85 return withTrafficLock(() => withWriteIntent(intent, () => writeSplit(worker, message, split, expected, secrets)))
88async function requireLog(reason: string) {
89 const log = activeLog()
90 if (!log) throw new HarnessFault(reason)
91 return log
94async function writeSplit(
95 worker: string,
96 message: string,
97 split: TrafficSplit,
98 expected: readonly VersionShare[],
99 secrets: readonly string[],
100): Promise<ApplyResult> {
101 const before = await readDeployment(worker, secrets)
102 if (!sameSplit(before.versions, expected)) {
103 const reason = 'deployment does not match the expected prior state'
104 const log = await requireLog('state conflict with no active log')
105 const at = Date.now()
106 const ref = await log.observation({
107 measurement: { source: 'state_conflict', expected: [...expected], actual: [...before.versions] },
108 query: { source: 'cf workers deployments list', worker },
109 from: at,
110 to: at,
111 raw: JSON.stringify({ expected, actual: before.versions }),
112 })
113 await log.decision({
114 inputs: [ref],
115 rules: [{ name: 'deployment_cas', value: 'expected_prior' }],
116 outcome: 'state_conflict',
117 reason,
118 })
119 return { ok: false, kind: 'state_conflict', reason }
120 }
121 await putSplit(worker, message, split, secrets)
122 let after = await readDeployment(worker, secrets)
123 let seen = classifyReadBack(before, after, split)
124 for (let attempt = 1; attempt < readBack.attempts && seen.kind === 'stale'; attempt++) {
125 await new Promise(resolve => setTimeout(resolve, readBack.gapMs))
126 after = await readDeployment(worker, secrets)
127 seen = classifyReadBack(before, after, split)
128 }
129 const log = await requireLog('deployment read-back with no active log')
130 await log.stateChange({ before: before.versions, after: after.versions, deploymentId: after.id })
131 switch (seen.kind) {
132 case 'applied': return { ok: true }
133 case 'mismatch': return { ok: false, kind: 'rejected', reason: seen.reason }
134 case 'stale': return { ok: false, kind: 'rejected', reason: `read-back still lists the prior deployment ${String(after.id)} after ${readBack.attempts} reads ${readBack.gapMs} ms apart` }
135 }
138/** Null when the deployment matches. Otherwise why it does not. */
139export async function confirmSplit(worker: string, split: TrafficSplit, secrets: readonly string[] = []): Promise<string | null> {
140 const versions = await readShares(worker, secrets)
141 return assertSplit(versions, split)
144/**
145 * Move the previous version to 100% alone and read the deployment back.
146 * Returns null when that is what is live. The error names the version that should be at 100%.
147 */
148export async function rollbackTo(
149 worker: string,
150 previous: string,
151 expected: readonly VersionShare[],
152 secrets: readonly string[] = [],
153): Promise<ApplyResult> {
154 const split = fullSplit(previous)
155 if (!isTrafficSplit(split)) return { ok: false, kind: 'rejected', reason: `rollback refused: ${split.reason}` }
156 try {
157 const applied = await withTrafficLock(() => withWriteIntent({ kind: 'rollback' }, () => writeSplit(worker, `rollback ${previous}`, split, expected, secrets)))
158 if (!applied.ok) return applied
159 const versions = await readShares(worker, secrets)
160 if (isFullRollback(versions, previous)) return { ok: true }
161 return { ok: false, kind: 'rejected', reason: `rollback did not land on ${previous} at 100%` }
162 } catch (error) {
163 if (error instanceof HarnessFault) throw error
164 return { ok: false, kind: 'rejected', reason: `rollback to ${previous} failed: ${errorMessage(error)}` }
165 }
Diagnostics4 git calls · 181 ms
CallStatusSizeTime0181 ms
discover200152 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200145 B · hit4 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=4
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=145
git.pkt.payload.max=76
git.ls_refs.count=2
git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 KiB · hit1 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=1
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=1
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=6
git.pkt.data=4
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=41349
git.pkt.payload.max=41254
git.sideband.band1.bytes=41253
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=61
git.pack.bytes=41253
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c
git.pack.full=61
Open pack map
fetch2002.2 KiB176 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=176
git.response.bytes=2298
git.response.cache=MISS
git.response.duration_ms=176
git.fetch.wants=c2d00a117a55071239495bc868979d451dddae47
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2298
git.pkt.payload.max=2277
git.sideband.band1.bytes=2276
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
Ref
refs/heads/main
Commit
526b305930b8ef382f2d7a1f9c0238730a585921
cf-ray
a5e2e33acd784af4
Request
af1f5e89…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format