Native API

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 withCredential
nothing (open server, --open)empty
--token SECRETthe 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:

Rust
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.

Rust
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:

CodeNameMeaning
1BAD_STATEfor example a request before Hello, or version 2 not known to the server
2SCHEMAdocument rejected; the message says why (for example Range); also unpaired UTF-16 surrogates and duplicate field names in JSON text
3EXISTSInsert on a key that exists
4BAD_FILTERfilter or SQL text did not compile; the message has the position
5OVERLOADEDfor example more than 65,536 streams on a connection
6SHUTTING_DOWNthe server is stopping
7AUTHwrong credential; the connection is closed
8IOa log write or flush failed
9NOT_SUPPORTEDrequest not served (GetBatch, Cancel)
10NOT_FOUNDdatabase or table
11FORBIDDENthe role does not allow it
12READ_ONLYthe server is a replica that has not been promoted
13TOO_BIGreply 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):

RequestKindMeaning
Stream0x13[stream varint][op u8]: op 0 means the following requests belong to the stream, op 1 forgets it; no reply
Defer0x14[on u8]: answer this stream's writes when applied (1) or when durable (0); no reply
Sync0x15empty; Done when the writes that were answered early are durable
Project0x16[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.

See also#

Wire protocol, Pipelining and batching, Authentication