dbreads: pull in the envelope, the pull's cap and chunk size
MaxPullBytes 2 GiB (spec rev 7), PullChunkBytes 1 MiB, MaxLineBytes 2 MiB so a pull chunk's line fits; a pull envelope carries no SQL; the chunk writer's size is a parameter.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+60
−14
internal/dbreads/chunks.go+7 −6internal/dbreads/envelope.go+12 −4internal/dbreads/envelope_test.go+19 −0internal/dbreads/limits.go+7 −0internal/dbreads/lines.go+3 −2internal/dbreads/lines_test.go+10 −0internal/dbreads/read.go+1 −1internal/dbreads/read_test.go+1 −1
diff --git a/internal/dbreads/chunks.go b/internal/dbreads/chunks.go| index d160a1557..f5df0cb94 100644 |
| --- a/internal/dbreads/chunks.go |
| +++ b/internal/dbreads/chunks.go |
| @@ -45,7 +45,7 @@ func putCreateOnly(ctx context.Context, s release.Store, key string, body []byte |
| } |
| // chunkWriter seals a read's output into out/<seq> objects: one every |
| -// ChunkBytes, and whatever is pending every `every`; close seals what is |
| +// size bytes (ChunkBytes for a query, PullChunkBytes for a pull), and whatever is pending every `every`; close seals what is |
| // left, possibly nothing, as the last chunk. Past max nothing more is kept |
| // and Write answers errCapped. Each put is create-only and confirmed before |
| // the next is sealed, under the lock, so seqs are in order (the run |
| @@ -57,6 +57,7 @@ type chunkWriter struct { |
| pub *ecdh.PublicKey |
| stream string |
| max int64 |
| + size int |
| mu sync.Mutex |
| pending []byte |
| @@ -70,8 +71,8 @@ type chunkWriter struct { |
| done chan struct{} |
| } |
| -func newChunkWriter(ctx context.Context, s release.Store, id string, pub *ecdh.PublicKey, stream string, every time.Duration, max int64) *chunkWriter { |
| - c := &chunkWriter{ctx: ctx, store: s, id: id, pub: pub, stream: stream, max: max, sum: sha256.New(), |
| +func newChunkWriter(ctx context.Context, s release.Store, id string, pub *ecdh.PublicKey, stream string, every time.Duration, max int64, size int) *chunkWriter { |
| + c := &chunkWriter{ctx: ctx, store: s, id: id, pub: pub, stream: stream, max: max, size: size, sum: sha256.New(), |
| stop: make(chan struct{}), done: make(chan struct{})} |
| go c.tick(every) |
| return c |
| @@ -112,12 +113,12 @@ func (c *chunkWriter) Write(p []byte) (int, error) { |
| c.sum.Write(p) |
| c.total += int64(len(p)) |
| c.pending = append(c.pending, p...) |
| - for len(c.pending) >= ChunkBytes { |
| - if err := c.flushLocked(c.pending[:ChunkBytes], false); err != nil { |
| + for len(c.pending) >= c.size { |
| + if err := c.flushLocked(c.pending[:c.size], false); err != nil { |
| c.err = err |
| return 0, err |
| } |
| - c.pending = append([]byte(nil), c.pending[ChunkBytes:]...) |
| + c.pending = append([]byte(nil), c.pending[c.size:]...) |
| } |
| if capped { |
| c.isCapped = true |
diff --git a/internal/dbreads/envelope.go b/internal/dbreads/envelope.go| index 3fc536284..f44a583a0 100644 |
| --- a/internal/dbreads/envelope.go |
| +++ b/internal/dbreads/envelope.go |
| @@ -45,8 +45,8 @@ func (r Request) AtTime() (time.Time, error) { |
| return time.Parse(time.RFC3339, r.At) |
| } |
| -// ops are the operations this build serves. Plan 3 adds OpPull. |
| -var ops = map[string]bool{OpQuery: true} |
| +// ops are the operations this build serves. |
| +var ops = map[string]bool{OpQuery: true, OpPull: true} |
| func (r Request) valid() bool { |
| if r.Purpose != Purpose || r.V != 1 || !ValidID(r.ID) || !ValidRegion(r.Region) || |
| @@ -54,8 +54,16 @@ func (r Request) valid() bool { |
| r.Incarnation == "" || !ops[r.Op] || r.By == "" { |
| return false |
| } |
| - if r.Op == OpQuery && (r.SQL == "" || len(r.SQL) > MaxSQLBytes || !utf8.ValidString(r.SQL)) { |
| - return false |
| + switch r.Op { |
| + case OpQuery: |
| + if r.SQL == "" || len(r.SQL) > MaxSQLBytes || !utf8.ValidString(r.SQL) { |
| + return false |
| + } |
| + case OpPull: |
| + // A pull runs nothing: SQL in one is a request nobody built. |
| + if r.SQL != "" { |
| + return false |
| + } |
| } |
| if _, err := r.AtTime(); err != nil { |
| return false |
diff --git a/internal/dbreads/envelope_test.go b/internal/dbreads/envelope_test.go| index 379f1323d..aa26621dc 100644 |
| --- a/internal/dbreads/envelope_test.go |
| +++ b/internal/dbreads/envelope_test.go |
| @@ -175,3 +175,22 @@ func TestQueueMatchesNeedsEveryPart(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// A pull is an op this build signs and serves (plan 3), and carries no SQL. |
| +func TestEnvelopeCarriesAPull(t *testing.T) { |
| + t.Parallel() |
| + pub, priv := rootKeys(t) |
| + req := testRequest(t) |
| + req.Op, req.SQL = OpPull, "" |
| + wire, err := Sign(release.RootSigner(priv), req) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, err := Verify(pub, wire, req.ID, "uk", testNow); err != nil || got.Op != OpPull { |
| + t.Fatalf("%+v %v", got, err) |
| + } |
| + req.SQL = "SELECT 1" |
| + if _, err := Sign(release.RootSigner(priv), req); !errors.Is(err, ErrEnvelope) { |
| + t.Fatalf("a pull carrying SQL signed: %v", err) |
| + } |
| +} |
diff --git a/internal/dbreads/limits.go b/internal/dbreads/limits.go| index 09a085f6a..613224574 100644 |
| --- a/internal/dbreads/limits.go |
| +++ b/internal/dbreads/limits.go |
| @@ -51,6 +51,13 @@ const ( |
| MaxOutputBytes = runs.MaxOutputBytes |
| ChunkBytes = runs.ChunkBytes |
| ChunkEvery = runs.ChunkEvery |
| + // MaxPullBytes is a pull's cap on its planned LTX files (spec §5.7, rev |
| + // 7: measured on dev, 2026-10-23). CARLOS_DB_PULL_MAX_BYTES on a box can |
| + // only lower it, so no box ships more than a CLI accepts. |
| + MaxPullBytes int64 = 2 << 30 |
| + // PullChunkBytes is a pull's chunk: larger than a query's, so a full |
| + // pull is about two thousand objects, not eight. |
| + PullChunkBytes = 1 << 20 |
| ReadsPerBox = 2 |
| MaxConnections = 3 // two reads and one probe (carlos-dbread.socket) |
diff --git a/internal/dbreads/lines.go b/internal/dbreads/lines.go| index 20e1e4112..2b5a81c96 100644 |
| --- a/internal/dbreads/lines.go |
| +++ b/internal/dbreads/lines.go |
| @@ -2,8 +2,9 @@ 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 |
| +// sealed and base64-encoded, is about 350 KiB, and a pull's PullChunkBytes |
| +// chunk about 1.4 MB. |
| +const MaxLineBytes = 2 << 20 |
| // Wait lines name what the console is waiting on; ended lines say why the |
| // response ends without a result. |
diff --git a/internal/dbreads/lines_test.go b/internal/dbreads/lines_test.go| index f693558d5..bb1265cba 100644 |
| --- a/internal/dbreads/lines_test.go |
| +++ b/internal/dbreads/lines_test.go |
| @@ -39,3 +39,13 @@ func TestLinesAreOneOutcomeEach(t *testing.T) { |
| t.Fatalf("a full chunk line is %d bytes, over MaxLineBytes", len(full)) |
| } |
| } |
| + |
| +// A full pull chunk, sealed and base64-encoded in its line, fits |
| +// MaxLineBytes (plan 3: a 1 MiB chunk's line is about 1.4 MB). |
| +func TestAFullPullChunkLineFits(t *testing.T) { |
| + t.Parallel() |
| + full, _ := json.Marshal(Line{Chunk: 1, Data: bytes.Repeat([]byte{0xff}, PullChunkBytes+SealOverhead)}) |
| + if len(full)+1 > MaxLineBytes { |
| + t.Fatalf("a full pull chunk line is %d bytes", len(full)) |
| + } |
| +} |
diff --git a/internal/dbreads/read.go b/internal/dbreads/read.go| index 8ba085390..c3443a52f 100644 |
| --- a/internal/dbreads/read.go |
| +++ b/internal/dbreads/read.go |
| @@ -251,7 +251,7 @@ func (a *Agent) attempt(ctx context.Context, req Request, pub *ecdh.PublicKey, a |
| if err := writeJSONFrame(conn, frameRun, runMsg{SQL: req.SQL}); err != nil { |
| return fail(CodeInterrupted, Summary{}) |
| } |
| - cw := newChunkWriter(ctx, a.cfg.Store, req.ID, pub, StreamRows, a.cfg.ChunkEvery, a.cfg.MaxOutput) |
| + cw := newChunkWriter(ctx, a.cfg.Store, req.ID, pub, StreamRows, a.cfg.ChunkEvery, a.cfg.MaxOutput, ChunkBytes) |
| endCode, sqlErr, rerr := relay(conn, cw) |
| last, n, sum, werr := cw.close() |
| a.logf("dbreads: %s: output %d bytes in %d confirmed chunks in %s", req.ID, n, last, time.Since(runStart).Round(time.Millisecond)) |
diff --git a/internal/dbreads/read_test.go b/internal/dbreads/read_test.go| index b754e860a..ad0bd5d50 100644 |
| --- a/internal/dbreads/read_test.go |
| +++ b/internal/dbreads/read_test.go |
| @@ -265,7 +265,7 @@ func TestChunkWriterCapIsExact(t *testing.T) { |
| for _, extra := range []int{0, 1} { |
| fx := newFixture(t, 0) |
| max := int64(3*ChunkBytes + 7) |
| - cw := newChunkWriter(context.Background(), fx.store, "dbr_aaaaaaaaaaaaaaaaaaaaaaaaaa", fx.reply.PublicKey(), StreamRows, time.Hour, max) |
| + cw := newChunkWriter(context.Background(), fx.store, "dbr_aaaaaaaaaaaaaaaaaaaaaaaaaa", fx.reply.PublicKey(), StreamRows, time.Hour, max, ChunkBytes) |
| big := bytes.Repeat([]byte("x"), 2*ChunkBytes+5) |
| rest := bytes.Repeat([]byte("y"), int(max)-len(big)+extra) |
| cw.Write(big) |