Connections, errors and versions
How a binary connection starts (Hello, credentials, Use), the error codes, the replica rule, and what protocol version 2 adds.
Handshake#
The first request on a connection must be Hello. Any other request before it gets error BAD_STATE.
Hello carries [version varint][credential bytes]. The version must equal raftdb_proto::VERSION (1), or 2 for a client that wants the features below. The credential is:
| Server started with | Credential |
|---|---|
nothing (open server, --open) | empty |
--token SECRET | the token bytes |
--user NAME:PASSWORD[:ROLE[@DB]] | user NUL password (UTF-8, one zero byte between), or a session token from POST /v1/auth/login |
A wrong credential gets error AUTH and the server closes the connection. See Authentication.
Example from the Rust client:
let mut c = Client::connect(srv.addr(), b"s3cret")?; // token
let mut c = Client::connect_user(srv.addr(), "asha", "correct horse")?; // account
c.ping()?;Choosing the table#
A connection starts on main.documents. Use{db, table} switches it; the table must exist (otherwise NOT_FOUND), and the role check applies to the target. Requests already read are first delivered to the old table.
let mut c = Client::connect(srv.addr(), b"")?;
assert_eq!(c.count("{}")?, 1); // main.documents
c.use_table("shop", "orders")?;
assert_eq!(c.count("{}")?, 7);
assert!(c.use_table("shop", "ghost").is_err());. A table that is dropped or renamed while a version 1 connection uses it closes that connection; connect again and Use the new name. A partitioned table answers NOT_SUPPORTED to Use.
Error codes#
An Error reply is [code varint][message]. The codes are in raftdb_proto::code:
| Code | Name | Meaning |
|---|---|---|
| 1 | BAD_STATE | for example a request before Hello, or version 2 not known to the server |
| 2 | SCHEMA | document rejected; the message says why (for example Range); also unpaired UTF-16 surrogates and duplicate field names in JSON text |
| 3 | EXISTS | Insert on a key that exists |
| 4 | BAD_FILTER | filter or SQL text did not compile; the message has the position |
| 5 | OVERLOADED | for example more than 65,536 streams on a connection |
| 6 | SHUTTING_DOWN | the server is stopping |
| 7 | AUTH | wrong credential; the connection is closed |
| 8 | IO | a log write or flush failed |
| 9 | NOT_SUPPORTED | request not served (GetBatch, Cancel) |
| 10 | NOT_FOUND | database or table |
| 11 | FORBIDDEN | the role does not allow it |
| 12 | READ_ONLY | the server is a replica that has not been promoted |
| 13 | TOO_BIG | reply too large for the transport (UDP) |
A malformed frame gets an error reply and the connection is closed.
Protocol version 2#
VERSION (1) is what every client says by default. VERSION_STREAMS (2) is said by a client that puts many callers on one connection (raftdb_client::Mux) or asks for deferred writes and field projection. A server that knows it answers HelloOk with 2. An older server answers BAD_STATE and keeps the connection open, so the client sends Hello again with 1. Client::connect_modern does exactly this, and is_modern() tells which version was agreed. A connection that said 1 is served as before.
Version 2 adds four requests (no other request changes):
| Request | Kind | Meaning |
|---|---|---|
Stream | 0x13 | [stream varint][op u8]: op 0 means the following requests belong to the stream, op 1 forgets it; no reply |
Defer | 0x14 | [on u8]: answer this stream's writes when applied (1) or when durable (0); no reply |
Sync | 0x15 | empty; Done when the writes that were answered early are durable |
Project | 0x16 | [limit][cursor][count][field id]*[filter text]: Filter for binary documents, each cut down to the named fields |
A stream has its own current table and its own order of requests; many streams share one connection and no stream holds up another. Reply ids of streams carry the stream number in the upper 12 bits and a counter in the lower 20. Limits: 65,536 streams per connection, 8 MiB of requests may wait for one stream's helper. Streams are not available with table groups. Details: connector specification section 10 and D69.
Replica nodes#
A replica refuses every write (Put, PutJson, Delete, CreateIndex, FieldId) with READ_ONLY (12); reads and logins still work. See Replication.