This repository was archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
docs: add initial stream! protocol specification #1454
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f377385
docs: add initial pull sync spec
acud 1e31241
docs: add roundtrip to GetRange, make some wording less vague
acud 3eb7dec
docs: fix typo
acud fe45894
docs: updated diagram layout
acud 3214a32
docs: fix and improve a few small things
acud 335981c
docs: clean up, beautify, add wire protocol section with go structs f…
acud 51b1a65
docs: refine design with @nonsense
acud f674516
docs: add table
acud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
|
|
||
| stream! protocol | ||
| ====== | ||
|
|
||
| | Subject | Description | | ||
| |---|---| | ||
| | Authors | @zelig, @acud, @nonsense | | ||
| | Status | Draft | | ||
| | Created | 2019-06-11 | | ||
|
|
||
|
|
||
| ### definition of stream | ||
| a protocol that facilitates data transmission between two swarm nodes, specifically targeting sequential data in the form of a sequence of chunks as defined by swarm. the protocol should cater for the following requirements: | ||
| - client should be able to request arbitrary ranges from the server | ||
| - client can be assumed to have some of the data already and therefore can opt in to selectivally request chunks based on their hashes | ||
|
|
||
| As mentioned, the client is typically expected to have some of the data in the stream. to mitigate duplicate data transmission the stream protocol provides a configurable message roundtrip before batch delivery which allows the downstream peer to selectively request the chunks which it does not store at the time of the request. | ||
| When delivery batches are pre-negotiated (i.e. when the client selectively tells the server which chunks it would like to receive), we can conclude that the delivery batches are optimsed for _urgency_ rather than for maximising batch utilisation (since the server sends a certain batch that potentially gets reduced into a smaller one by the client before actually being transmitted). | ||
|
|
||
| ### the protocol defines the notions of: | ||
| - **stream** - data source which is composed of a sequence of hashes, referenced by monotonically increasing integers, with unguaranteed contiguity with respect to one particular stream. | ||
| - **client** _(downstream peer)_ - the peer which is requesting data and does not posses it (client) | ||
| - **server** _(upstream peer)_ - the peer that has the data and sends it to the downstream peer (server) | ||
| - **range** - based on the notion of integer indexes we can define a range that designates an interval on the stream. | ||
| - **batch** - a set of chunks constituting an interval in a range are called a batch, with a length not exceeding a ceiling value negotiated when establishing streams | ||
| - **batch delivery** - end of batch delivery should be indicated by an explicit message from the server | ||
| both offered and wanted go together - note this | ||
| - **roundtrip** - a configurable extra message exchange (negotiated on initial message exchange) meant to mitigate and avoid requesting the same data from different peers. when the roundtrip is not used the stream is assumed to be continuous and the order of delivery should be guaranteed, or the stream should be particularly ordered. A roundtrip consists of: | ||
| - **offered hashes** - from the server to the client | ||
| - **wanted hashes** - at the discretion of the client in response to offered hashes | ||
|
|
||
| ### responsibilities: | ||
| - client is able to request a range but doesnt know how many results the interval will return from the server | ||
| - client does not know if interval is continuous or has gaps | ||
| - range is defined by client and should be strictly respected and followed by server | ||
| - all intervals specified in protocol messages are closed (inclusive) | ||
| - when roundtrip is configured - chunk deliveries can be handled concurrently (therefore their order is not guaranteed), but a server end-of-batch with topmost session index must be sent to signal the end of a batch | ||
|
nonsense marked this conversation as resolved.
|
||
| - when roundtrip is not configured - chunks are expected to be sent in order, one after the other | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line 30 and 31 are not clear to me. in the current impl of Swarm, we handle multiple messages concurrently, so depending on how we specify the from a server perspective we always send chunks |
||
| - when a client requests an unbounded range (i.e. FROM=..., TO=nil): | ||
| - if there's no chunks available - server waits until something becomes available then send it to the client | ||
| - server's responsibility to give as much as possible, as fast as possible, with a limit of batch size | ||
| - one range query should result in ONE rountrip + batch delivery | ||
| - when a client requests a bounded range, server should respond to the client range requests with either offered hashes (if roundtrip is required) or chunks (if not) or an end-of-batch message if there are no more to offer. If none of these responses arrive within a timeout interval, client must drop the upstream peer. | ||
| - the server should always respond to the client | ||
|
|
||
|
|
||
| #### stream termination condition: | ||
| - timeout, connection died, we get an error and remove the client, server also gets an error from p2p layer and removes all servers/clients and drops the peer | ||
|
|
||
| ### considerations: | ||
| - server must make sure that chunk got to client in order to account in SWAP (synchronous). if the send does not result in an error - the send should be accounted | ||
| - there is always a max batch size so that clients cannot grieve servers with very large ranges | ||
|
|
||
| ### syncing contracts: | ||
| - stream indexes always > 0 | ||
| - syncing is an implementation of the stream protocol | ||
| - client is expected to manage all intervals, and therefore: | ||
| - server is designed to be stateless, except for the case of managing a offered/wanted roundtrip and the knowledge of a boundedness of a stream (e.g. the server knows that syncing streams are always unbounded from the localstore perspective - data can always enter the system, however this is not the case for live video stream for example) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i suggest we format a bit better the
|
||
| - the server does not terminate streams - it is at the discretion of the downstream peer | ||
| - the server does not initiate any messages unless instructed to | ||
| - the server does not instruct client on which bins to subscribe to it | ||
|
|
||
| Wire Protocol Specifications | ||
| ======= | ||
|
|
||
| ### The wire protocol defines the following messages: | ||
|
|
||
| | Msg Name | From->To | Params | Example | | ||
| | -------- | -------- | -------- | ------- | | ||
| | StreamInfoReq | Client->Server | Streams`[]string` | `SYNC\|6, SYNC\|5` | | ||
| | StreamInfoRes | Server->Client | Streams`[]StreamDescriptor` <br>Stream`string`<br>Cursor`uint64`<br>Bounded`bool` | `SYNC\|6;CUR=1632;bounded, SYNC\|7;CUR=18433;bounded` | | ||
| | GetRange | Client->Server| Ruid`uint`<br>Stream `string`<br>From`uint`<br>To`*uint`(nullable)<br>Roundtrip`bool` | `Ruid: 21321, Stream: SYNC\|6, From: 1, To: 100`(bounded), Roundtrip: true<br>`Stream: SYNC\|7, From: 109, Roundtrip: true`(unbounded) | | ||
| | OfferedHashes | Server->Client| Ruid`uint`<br>Hashes `[]byte` | `Ruid: 21321, Hashes: [cbcbbaddda, bcbbbdbbdc, ....]` | | ||
| | WantedHashes | Client->Server | Ruid`uint`<br>Bitvector`[]byte` | `Ruid: 21321, Bitvector: [0100100100] ` | | ||
| | ChunkDelivery | Server->Client | Ruid`uint`<br>[]Chunk `[]byte` | `Ruid: 21321, Chunk: [001000101]` | | ||
| | BatchDone | Server->Client| Ruid `uint`<br>Last `uint` | `Ruid: 21321, Last: 113331` | | ||
| | StreamState | Client<->Server | Stream`string`<br>Code`uint16`<br>Message`string`| `Stream: SYNC\|6, Code:1, Message:"Stream became bounded"`<br>`Stream: SYNC\|5, Code:2, Message: "No such stream"` | | ||
|
|
||
| Notes: | ||
| * communicating the last bin index when roundtrip is configured - can be done on top of OfferedHashes message (alongside the hashes), or to reuse the ACK from the no-roundtrip config | ||
| * two notions of bounded - on the stream level and on the localstore | ||
| * if TO is not specified - we assume unbounded stream, and we just send whatever, until at most, we fill up an entire batch. | ||
|
|
||
| ### Message struct definitions: | ||
| ```go | ||
| type StreamInfoReq struct { | ||
| Streams []string | ||
| } | ||
| ``` | ||
| ```go | ||
| type StreamInfoRes struct { | ||
| Streams []StreamDescriptor | ||
| } | ||
| ``` | ||
| ```go | ||
| type StreamDescriptor struct { | ||
| Name string | ||
| Cursor uint | ||
| Bounded bool | ||
| } | ||
| ``` | ||
| ```go | ||
| type GetRange struct { | ||
| Ruid uint | ||
| Stream string | ||
| From uint | ||
| To uint `rlp:nil` | ||
| BatchSize uint | ||
| Roundtrip bool | ||
| } | ||
| ``` | ||
| ```go | ||
| type OfferedHashes struct { | ||
| Ruid uint | ||
| LastIndex uint | ||
| Hashes []byte | ||
| } | ||
| ``` | ||
| ```go | ||
| type WantedHashes struct { | ||
| Ruid uint | ||
| BitVector []byte | ||
| } | ||
| ``` | ||
| ```go | ||
| type ChunkDelivery struct { | ||
| Ruid uint | ||
| LastIndex uint | ||
| Chunks [][]byte | ||
| } | ||
| ``` | ||
| ```go | ||
| type BatchDone struct { | ||
| Ruid uint | ||
| Last uint | ||
| } | ||
| ``` | ||
| ```go | ||
| type StreamState struct { | ||
| Stream string | ||
| Code uint16 | ||
| Message string | ||
| } | ||
| ``` | ||
|
|
||
| Message exchange examples: | ||
| ====== | ||
|
|
||
| Initial handshake - client queries server for stream states<br> | ||
|  | ||
| <br> | ||
| GetRange (bounded) - client requests a bounded range within a stream<br> | ||
|  | ||
| <br> | ||
| GetRange (unbounded) - client requests an unbounded range (specifies only `From` parameter)<br> | ||
|  | ||
| <br> | ||
| GetRange (no roundtrip) - client requests an unbounded or bounded range with no roundtrip configured<br> | ||
|  | ||
|
|
||
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aren't those two requirements actually the same requirement? if client can request arbitrary ranges from the server, then it basically means that the client can selectively request chunks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really, because bin index ranges can be asked for, for example, without a roundtrip, and that constitutes a valid request for a range of chunks.
for more granular control you have the possibility of a roundtrip