db forward: the reads route's lines and the list door's additive marker (plan 2, Task 2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+156
−2
internal/dbdoor/client.go+13 −2internal/dbdoor/forward.go+25 −0internal/dbdoor/forward_test.go+38 −0internal/dbreads/lines.go+39 −0internal/dbreads/lines_test.go+41 −0
diff --git a/internal/dbdoor/client.go b/internal/dbdoor/client.go| index fb54df849..cd2a44225 100644 |
| --- a/internal/dbdoor/client.go |
| +++ b/internal/dbdoor/client.go |
| @@ -58,6 +58,10 @@ type DoorError struct { |
| // RetryAfter is the door's Retry-After, in whole seconds, when it sent |
| // one (a 429 from a full stream bound does); zero otherwise. |
| RetryAfter time.Duration |
| + // Forward is the list door's forward marker on a 409 (db region |
| + // forward spec §5.5): the instance is readable through the forwarded |
| + // read in Forward.Region. nil for every other refusal. |
| + Forward *Forward |
| } |
| func (e *DoorError) Error() string { |
| @@ -164,16 +168,23 @@ func (c *Client) DeleteLTXFiles(context.Context, []*ltx.FileInfo) error { return |
| func (c *Client) DeleteAll(context.Context) error { return errReadOnly } |
| -// doorError reads a refusal's {"error"} sentence. A body that is not that |
| +// doorError reads a refusal's {"error"} sentence, and the db region |
| +// forward marker beside it on a 409. A body that is not that |
| // shape (the stdlib's 404 text, a proxy's page) keeps only the status. |
| func doorError(res *http.Response) error { |
| raw, _ := io.ReadAll(io.LimitReader(res.Body, 64<<10)) |
| var body struct { |
| - Error string `json:"error"` |
| + Error string `json:"error"` |
| + Forward int `json:"forward"` |
| + Region string `json:"region"` |
| + ForwardOps []string `json:"forward_ops"` |
| } |
| e := &DoorError{Status: res.StatusCode} |
| if json.Unmarshal(raw, &body) == nil { |
| e.Message = strings.TrimSpace(body.Error) |
| + if res.StatusCode == http.StatusConflict && body.Forward == 1 && forwardRegion.MatchString(body.Region) { |
| + e.Forward = &Forward{Region: body.Region, Ops: body.ForwardOps} |
| + } |
| } |
| // Seconds only (the door never sends a date), capped so a hostile or |
| // broken value cannot park a download for hours. |
diff --git a/internal/dbdoor/forward.go b/internal/dbdoor/forward.go| new file mode 100644 |
| index 000000000..75a86cfbf |
| --- /dev/null |
| +++ b/internal/dbdoor/forward.go |
| @@ -0,0 +1,25 @@ |
| +package dbdoor |
| + |
| +import "regexp" |
| + |
| +// ForwardBody is the db door's 409 for an instance a forwarding console can |
| +// read in its own region (db region forward spec §5.5 step 1). It is |
| +// additive to the {"error"} body every CLI reads, so an old CLI prints Error |
| +// as it is (DBF-14) and a new one switches to the forwarded read. |
| +type ForwardBody struct { |
| + Error string `json:"error"` |
| + Forward int `json:"forward"` |
| + Region string `json:"region"` |
| + ForwardOps []string `json:"forward_ops"` |
| +} |
| + |
| +// Forward is a DoorError's marker: where the data is, and which operations |
| +// the console forwards there. |
| +type Forward struct { |
| + Region string |
| + Ops []string |
| +} |
| + |
| +// forwardRegion is dbreads.ValidRegion's shape. dbreads imports this |
| +// package, so it is repeated here rather than imported. |
| +var forwardRegion = regexp.MustCompile(`^[a-z][a-z0-9-]{0,31}$`) |
diff --git a/internal/dbdoor/forward_test.go b/internal/dbdoor/forward_test.go| new file mode 100644 |
| index 000000000..2fc692c3a |
| --- /dev/null |
| +++ b/internal/dbdoor/forward_test.go |
| @@ -0,0 +1,38 @@ |
| +package dbdoor |
| + |
| +import ( |
| + "encoding/json" |
| + "io" |
| + "net/http" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +func doorResponse(status int, body string) *http.Response { |
| + return &http.Response{StatusCode: status, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(body))} |
| +} |
| + |
| +// The marker is additive: the sentence stays what every CLI prints, and only |
| +// a 409 with forward 1 and a region carries a Forward. |
| +func TestDoorErrorReadsTheForwardMarker(t *testing.T) { |
| + t.Parallel() |
| + body, _ := json.Marshal(ForwardBody{Error: "shop.example.com keeps its data in region uk.", Forward: 1, Region: "uk", ForwardOps: []string{"query"}}) |
| + door := doorError(doorResponse(http.StatusConflict, string(body))).(*DoorError) |
| + if door.Message != "shop.example.com keeps its data in region uk." || door.Forward == nil || |
| + door.Forward.Region != "uk" || len(door.Forward.Ops) != 1 || door.Forward.Ops[0] != "query" { |
| + t.Fatalf("door = %+v %+v", door, door.Forward) |
| + } |
| + for name, raw := range map[string]string{ |
| + "a plain refusal": `{"error":"no"}`, |
| + "forward 0": `{"error":"no","forward":0,"region":"uk"}`, |
| + "no region": `{"error":"no","forward":1}`, |
| + "a region that is not one": `{"error":"no","forward":1,"region":"../x"}`, |
| + } { |
| + if d := doorError(doorResponse(http.StatusConflict, raw)).(*DoorError); d.Forward != nil { |
| + t.Errorf("%s: a marker", name) |
| + } |
| + } |
| + if d := doorError(doorResponse(http.StatusNotFound, string(body))).(*DoorError); d.Forward != nil { |
| + t.Error("a 404 carried a marker") |
| + } |
| +} |
diff --git a/internal/dbreads/lines.go b/internal/dbreads/lines.go| new file mode 100644 |
| index 000000000..20e1e4112 |
| --- /dev/null |
| +++ b/internal/dbreads/lines.go |
| @@ -0,0 +1,39 @@ |
| +package dbreads |
| + |
| +// The reads route's 200 body is newline-delimited JSON, one Line per line |
| +// (spec §5.5 step 6). MaxLineBytes bounds one line: a ChunkBytes chunk, |
| +// sealed and base64-encoded, is about 350 KiB. |
| +const MaxLineBytes = 1 << 20 |
| + |
| +// Wait lines name what the console is waiting on; ended lines say why the |
| +// response ends without a result. |
| +const ( |
| + WaitPickup = "pickup" |
| + WaitReading = "reading" |
| + EndedNotPicked = "not-picked" // the CLI says DBF-3 |
| + EndedNoResult = "no-result" // the CLI says DBF-12 |
| +) |
| + |
| +// Line is one outcome: admitted (the read's id), picked (the owner box), |
| +// one chunk in sequence, a wait, an ended, or the result, which comes last. |
| +// The ended line is additive to spec §5.5's list ("from here every outcome |
| +// is a line in that body"): it lets the CLI tell nobody picking the read |
| +// (DBF-3) from a read that did not finish (DBF-12), which a console restart |
| +// or a dropped connection also ends with no result. |
| +type Line struct { |
| + Admitted string `json:"admitted,omitempty"` |
| + Picked string `json:"picked,omitempty"` |
| + Chunk int64 `json:"chunk,omitempty"` |
| + Data []byte `json:"data,omitempty"` |
| + Wait string `json:"wait,omitempty"` |
| + Ended string `json:"ended,omitempty"` |
| + Result *Result `json:"result,omitempty"` |
| +} |
| + |
| +// ReadBody is the POST …/db/{host}/reads body (spec §5.5 step 2). |
| +type ReadBody struct { |
| + Op string `json:"op"` |
| + SQL string `json:"sql"` |
| + At string `json:"at"` |
| + ReplyKey string `json:"reply_key"` |
| +} |
diff --git a/internal/dbreads/lines_test.go b/internal/dbreads/lines_test.go| new file mode 100644 |
| index 000000000..f693558d5 |
| --- /dev/null |
| +++ b/internal/dbreads/lines_test.go |
| @@ -0,0 +1,41 @@ |
| +package dbreads |
| + |
| +import ( |
| + "bytes" |
| + "encoding/json" |
| + "testing" |
| +) |
| + |
| +// Each line carries exactly one outcome, and a full-size sealed chunk fits |
| +// MaxLineBytes once base64-encoded. |
| +func TestLinesAreOneOutcomeEach(t *testing.T) { |
| + t.Parallel() |
| + for _, l := range []Line{ |
| + {Admitted: "dbr_aaaaaaaaaaaaaaaaaaaaaaaaaa"}, |
| + {Picked: "uk-1"}, |
| + {Chunk: 1, Data: []byte("sealed")}, |
| + {Wait: WaitPickup}, |
| + {Ended: EndedNotPicked}, |
| + {Result: &Result{State: StateDone, LastSeq: 1}}, |
| + } { |
| + b, err := json.Marshal(l) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var m map[string]any |
| + if err := json.Unmarshal(b, &m); err != nil { |
| + t.Fatal(err) |
| + } |
| + keys := len(m) |
| + if l.Chunk != 0 { |
| + keys-- // a chunk line is two fields, chunk and data |
| + } |
| + if keys != 1 { |
| + t.Fatalf("%s carries %d outcomes", b, keys) |
| + } |
| + } |
| + full, _ := json.Marshal(Line{Chunk: 1, Data: bytes.Repeat([]byte{0xff}, ChunkBytes+SealOverhead)}) |
| + if len(full)+1 > MaxLineBytes { |
| + t.Fatalf("a full chunk line is %d bytes, over MaxLineBytes", len(full)) |
| + } |
| +} |