Repository navigation
feat(api): bidirectional chunk stream protocol - #5618
martinconic wants to merge 18 commits into
Conversation
| s.metrics.ChunkStreamDeliveryCount.WithLabelValues("success").Inc() | ||
|
|
||
| chunkData := chunk.Data() | ||
| resp := make([]byte, 1+swarm.HashSize+len(chunkData)) |
There was a problem hiding this comment.
not sure about this custom serialization by hand thing... it is just specced out in the openapi spec and assumed that implementers should implement it by hand. it is fragile and breakable. why not use some sort of standard serialization format to both decode the request and encode the response?
aloknerurkar
left a comment
There was a problem hiding this comment.
Since we are making breaking changes and also building on the serialization thread — I'd suggest going further than reworking the download framing: use one connection for both directions, with request/response semantics and a request id.
Two things this fixes beyond tidiness:
-
Upload is round-trip bound. The upload loop is strictly sequential — read → put → ack → read, one chunk in flight. Each chunk costs a full RTT, so on a 50ms link that's ~20 chunks/sec regardless of bandwidth. Download already has a 16-worker pool; upload has none. stamper.Stamp already takes issuer.mtx (pkg/postage/stamper.go:43), so concurrent stamping is safe — the ceiling is the protocol, not the storage layer.
-
Neither direction is recoverable. Download replies carry no id, and the upload ack is successWsMsg = []byte{} — an empty frame with no address at all. Clients correlate purely by ordering. On any error both paths call sendErrorClose and drop the connection, so a client that had N requests outstanding cannot tell which completed. For bulk upload that means restarting from zero.
A shared envelope — [type][8-byte request-id][payload] for requests, [type][8-byte request-id][status][payload] for responses — would give:
- one read loop feeding one typed job channel, with a worker pool serving both Get and Put (removes the duplicated read/deadline/close-handler logic between the two handlers)
- pipelined uploads instead of one-at-a-time
- per-request error status instead of connection teardown, so a single bad chunk no longer kills the stream
- correlation for recovery after an error close
I'd keep this as raw binary rather than JSON-RPC or protobuf. JSON-RPC costs more on the wire with base64 and ~15x encode/decode — self-defeating for an endpoint that exists to cut per-chunk overhead. Protobuf is bee's p2p convention but has never appeared in pkg/api; requiring a schema compiler would be a new burden on bee-js. The binary envelope stays a four-line DataView parse in the browser with no dependency.
The upload stream endpoint was not written with care. I was going through the code and there is a lot of scope to cleanup. Multiple putters defined, deferred/direct upload semantics, stamped and unstamped chunks etc. I feel that this would make it much more usable for clients and also close to what elad mentioned about having grpc style chunk get/put API.
| } | ||
|
|
||
| // fetchAndSendChunk retrieves a single chunk and writes exactly one response | ||
| // frame for it. That one-frame-per-requested-address invariant is what lets a |
There was a problem hiding this comment.
Exactly one reply per requested address, and "a dropped frame is indistinguishable from a slow one" because replies carry no request id. But the protocol-error paths in the read loop (sendErrorClose + return on bad message type, bad length, unknown opcode, oversized batch) void every in-flight request silently.
A client that sends 256 addresses and then trips CloseMessageTooBig on its next frame has no way to determine which of the first 256 were answered. With no request id and no per-batch boundary marker in the reply stream, the only recovery is to discard everything and re-request.
The OpenAPI spec documents the close codes, but not that pending replies are lost when they fire. At minimum that consequence belongs in the spec. A framing with a request/batch id — which is largely what @acud point about a standard serialization format would give you for free — would make it recoverable instead.
Thank you for this, sounds like a good change. @acud do you also agree? |
|
@martinconic i generally encourage the initiative from @aloknerurkar, but i'm still not 100% convinced a custom encoding is right.
i think we can progress with this proposal, and make serialization a pluggable implementation. but in general i'd encourage to move away from hand written serialization at least on wire-formats (persistence is a different story). |
I am fine with protobuf. The only reason I didn't suggest it is that bee-js/javascript will be primary consumer if this is successful and I remember there were issues with javascript ecosystem when working with protobuf. I spoke to claude and I see that there is a new typescript compiler which makes things easier. So I am totally on board! |
…ownload # Conflicts: # openapi/Swarm.yaml
Add fuzz targets for decoding chunk stream requests and for chunkForAddress, which validates uploaded chunks against the requested address. Add a test for a stream that ends on a protocol violation while its workers are blocked writing to a client that has stopped reading. The shutdown watcher has exited by then, so only closing the socket before waiting on the workers releases them.
Close the socket when the stream ends on a failed response write, so the read loop is released and node shutdown does not find the stream still open. Ping the client and let its pongs extend the read deadline, so a client that is only waiting for replies is not dropped. Lower the frame size limit to 8 KiB, which bounds the memory a full request queue can hold. Validate an uploaded SOC in a single pass, and do not stamp requests still queued when the stream ends.
Report a chunk that no peer could serve as STATUS_UNAVAILABLE instead of STATUS_NOT_FOUND, as GET /chunks and /bzz now answer 503 for it. Pin a connection with Swarm-Postage-Batch-Id to that batch: a request batch id or pre-signed stamp from another batch is rejected. Send pings from their own goroutine and answer client pings with the response write deadline. gorilla treats a control frame write that times out as fatal, so a short deadline could end the stream of a client that had paused reading. Do not start a retrieval for a request still queued when the stream ends, document close code 1006, and test that the cache option reaches the storer.
A node shutdown closes the API's http.Server only, so a chunk stream is dropped without a close frame (1006). 1001 is sent only when the API service itself is closed.
…ownload # Conflicts: # pkg/api/router.go
Extend the read deadline on client pings as well as pongs, so a client that keeps the stream alive with its own pings is not dropped. Skip validating an upload request that is still queued when the stream ends, and keep the check before stamping, since the stream can end while the chunk is validated. Count STATUS_BUSY replies in the chunk stream delivery metric.
/chunks/stream now always speaks the protobuf request/response protocol. The upload-only mode, the mode query parameter and the swarm-chunk-stream subprotocol are removed; a client still sending raw chunk messages has its connection closed with 1003. OpenAPI is bumped to 9.0.0. Run each stream as a chunkStream under an errgroup: the reader, the download and upload workers, the pinger and a watcher that closes the socket when the stream ends. The first error ends the stream, and request handlers return only reply write errors, so a failed request is still answered. Look up the postage batch for every uploaded chunk instead of caching it per connection, so a batch that expires while the stream is open is no longer used, and validate pre-signed stamps with Stamp.Valid, which also checks the bucket and index. Document that encrypted references are not supported, that clients should read replies while sending, and that node shutdown sends 1001. BREAKING CHANGE: the upload-only /chunks/stream mode is removed.
Checklist
Description
Replaces the upload-only
GET /chunks/streamwith a request/response protocol: a client opens one websocket and pipelines chunk downloads and uploads over it, instead of paying for an HTTP request per chunk.Closes #5417, closes #5599.
The first version of this PR added a download-only mode with hand-rolled
['D'][32-byte address]...framing. Following review (@acud, @aloknerurkar) it now uses protobuf, with typed Get and Put messages and a status per request, so one bad chunk no longer closes the connection and a client can recover after an error.Breaking change: the legacy upload-only stream (raw
[8-byte span][data]messages acknowledged with empty frames) is removed, as is themodequery parameter. A client still sending raw chunk messages has its connection closed with1003. bee-js does not use the endpoint. OpenAPI is bumped to 9.0.0.Protocol
Messages are defined in
pkg/api/pb/chunkstream.proto, one protobuf message per websocket binary message. There is no subprotocol or mode to negotiate.GetRequest(32-byteAddress, optionalCacheOption) andPutRequest(Address,Data, optionalStamp, optionalBatchId).GetResponse(Address,Status,Data,Error) andPutResponse(Address,Status,Error). Every request gets exactly one response, in completion order.OK,NOT_FOUND,ERROR,BAD_REQUEST,BUSYwhen a queue is full (retry with backoff), andUNAVAILABLEwhen no peer could serve a chunk, because none was usable or every peer was tried (the chunk may or may not exist; retry a limited number of times).STATUS_UNSPECIFIED = 0is never sent.Uploads
Dataand accepts it only if it produces the requestedAddress, as a CAC or as a SOC with a valid signature. Small SOC data also parses as a valid CAC; the address settles which one it is. A mismatch is answered withBAD_REQUEST.Stampon the request, the request's ownBatchId, then the connection'sSwarm-Postage-Batch-Id. Without a connection batch, one connection can upload to several batches. A connection withSwarm-Postage-Batch-Idis pinned to that batch, so a proxy that sets the header also decides the batch of every upload; aBatchIdor pre-signedStampfrom a different batch is rejected withBAD_REQUEST. A bad connection batch fails the handshake; a bad per-request batch fails only that request.Stamp.Valid, including their bucket and index, so a stamp the network would reject is not pushed.OKmeans the chunk has been pushed to the network, and is never sent before that.Swarm-Tag/?swarm-tag=is rejected with400at the handshake, so a client expecting deferred uploads finds out immediately instead of having the tag ignored.Downloads
Swarm-Cache/?cache=set the connection default;CacheOptionoverrides it per request.storage.ErrNotFoundmaps toNOT_FOUND, andtopology.ErrNotFound(no peer could serve the chunk) toUNAVAILABLE, matching howGET /chunksand/bzzanswer 503 for it.Errors and closing
A failing request gets its own status and does not close the connection. The connection is closed only for a message that cannot be decoded or has no body (
1003), a message over 8 KiB (1009), or node shutdown (1001, now that #5671 closes the API service on shutdown). A connection dropped without a close frame, for example after 15 minutes without a request, a pong or a ping, shows as1006. Responses received before a close are final; a client should reconnect and resend every request that got no response. This is documented in the OpenAPI spec.Notes for review
chunkStreamrun under anerrgroup: the reader, 8 download and 8 upload workers, a pinger, and a watcher that closes the socket when the stream ends (closing the socket is what releases a blocked read or write). The first error ends the stream, including a clean close by the client; request handlers return only reply write errors, so a failed request is still answered.pb/convention. No new dependency.BUSYrather than queuing without bound.getter.DefaultFetchTimeout, an upload by 30s. The response write deadline is 5 minutes, so a client that pauses reading while its buffer drains doesn't get disconnected.WsPingPeriod, and a pong or a client ping extends the 15-minute read deadline just like a request does. gorilla treats a control frame write that times out as fatal, so pings and pongs get the same 5-minute deadline as responses: a keepalive cannot end the stream of a client that has paused reading.busyfor requests turned away by a full queue), and fetch duration.Testing
27 tests cover the stream: upload and download, interleaving, a Get and a Put for the same address in flight, per-request errors, a failed push, fairness under blocked uploads, a full queue, pre-signed stamps (including one for the wrong bucket) and per-request-batch stamping, a feed-sized SOC, a rejected tag, cache options, protocol violations, shutdown with an idle stream, with pending writes and with a client that has stopped reading, a protocol violation from a client that has stopped reading, a response write timeout followed by shutdown, and a client close ending the stream. Further tests cover the frame size limit, pongs and client pings keeping a stream open, keepalives in both directions while the client has paused reading, download error statuses including
UNAVAILABLE, batch pinning, a batch expiring mid-stream (node stamping and pre-signed), the cache flag reaching the storer for every header, query and request combination, and no stamps or retrievals for requests still queued when the stream ends. Each of these was checked against code with its fix reverted, to confirm it fails when it should. The keepalive test depends on timing: with its fix reverted it fails in most runs (4 of 5 in each direction), and it never fails on correct code.Two fuzz targets in
chunk_stream_fuzz_test.gocover the untrusted input:FuzzChunkStreamRequest(protobuf decode plus a marshal round trip) andFuzzChunkForAddress(an accepted chunk must have the requested address and be a valid CAC or SOC).make build,make lint,make test, the race detector andmake fuzz FUZZPKG=./pkg/api/...all pass; the redocly problem count is unchanged from master.Live test
Run against a Sepolia testnet full node built from this branch, with a Go client that uses
pkg/api/pb:/envelope;1003;BUSYunder a flood;1001 "node shutting down".Missing chunks are answered
STATUS_UNAVAILABLEin about 2s, neverNOT_FOUND, which matches the 503 thatGET /chunksreturns for them.AI Disclosure