Internals

raftdb-client, the Rust client

The blocking Rust client, its one-shot and streaming calls, the bounded look before a blocking receive, and Mux streams that share connections.

raftdb-client is a blocking client for the binary protocol. It depends only on raftdb-proto; there is no async runtime and no socket library. It was split out of raftdb-server so applications can use it alone, and raftdb_server::client re-exports it. It is the reference connector for the other languages.

Calls#

  • One-shot (one round trip each): connect, connect_str("host:port", credential), connect_user, use_table, ping, put_json, get_json, delete, count, sql_count, filter_page, sql_page, create_index, and the binary calls get, put, insert.
  • Streaming: scan(filter, page) and scan_sql(sql, page) return an iterator over every row as JSON text, following the server's cursor page by page. The low-level send, flush and recv put many requests on the wire before reading replies, which is how the benchmarks reach millions of operations per second.
  • Without a copy: get_with(key, f) and filter_each(text, limit, cursor, f) hand each document to f where it was received. filter_start and filter_finish let a caller have two reads on their way at once.

Errors are ClientError::{Io, Protocol, Server{code, msg}}. Credentials are empty for an open server, the shared token, or connect_user(addr, user, password). There is no TLS.

The look before a blocking receive (wait)#

A thread that blocks in a receive has to be woken when the message arrives, which costs 20 to 30 us on a desktop. A loopback round trip took about 55 us with both ends blocking and about 24 us with both ends already looking (measured on the test PC). Waiter::read is socket.read preceded, when the rule allows, by asking the socket whether bytes have arrived (Windows ioctlsocket(FIONREAD), Linux recv(MSG_PEEK | MSG_DONTWAIT)) and offering the processor to other threads in between, for at most the budget. Elsewhere, and with the portable feature, it never looks.

The rule, all of which must hold: the budget is not 0 (spin_us, default 150, at most 1000); the connection got a message within the last millisecond (an idle connection blocks at once); recent waits on it were shorter than the budget; at most a quarter of the processors' worth of the process's own connections were busy in the last millisecond; and fewer than max_spinners() threads are looking now. The cost is nothing for idle connections and up to one processor for a connection that is asked again and again.

Streams (Mux)#

Mux::new(addr, credential, n) holds n long-lived connections. mux.stream(k) or mux.seat().stream() returns a Client that is a stream on one of them; it has its own current table and its own order of requests, and many streams share a connection (protocol version 2).

  • Sending never waits for a batch. A caller appends to the connection's queue and writes the queue itself unless another caller is writing; that one takes the requests with its next write.
  • Receiving has two forms. With one caller at a time, the caller that finds nobody reading reads the socket itself and hands the lead on when its own reply has come. When callers keep overlapping (4 times within 2 ms a caller found another one reading), the connection's reader thread takes the lead; it gives the lead back and sleeps after 20 ms without two simultaneous waiters.
  • Seats place a caller with several streams on the connection with the fewest callers.
  • Ids: the upper 12 bits of a request id are the stream's number on its connection and the lower 20 count its requests, so at most 2^20 requests of one stream may be unanswered.
  • Failure: when a connection breaks, every stream on it fails with an I/O error and the next Mux::stream connects afresh. A stream that does not read its replies holds up nobody. A caller whose requests would make the unsent queue larger than 4 MiB waits.
  • Deferred writes: set_defer(true), sync() and friends need version 2; without it set_defer returns false and nothing changes.
  • Client::connect_modern says version 2 on a connection of its own and falls back to version 1 against an older server.

Only the standard library is used (sockets, mutexes, condition variables); nothing platform-specific was added. The streams are tested against a running Raft DB server (10 tests).