The MySQL/MariaDB front-end (raftdb-mysql)
How the connector program is built: its modules, how a table and a row are stored in Raft DB, how it runs in one process or two, how uniqueness and foreign keys are kept, and how it is tested against MySQL and MariaDB.
raftdb-mysql is a separate program that listens on a MySQL port, speaks the MySQL wire protocol (so unmodified drivers, ORMs and tools connect) and translates what clients send into requests to Raft DB. It is a connector, not part of the core: only what Raft DB lacks was added to the core, and the dialect work stays in this crate (decision D52). The engine crates do not depend on it. It depends on workspace crates only: raftdb-proto (SHA-256), raftdb-exec (the JSON reader and writer) and, in one-process mode, the server. For using it, see the connectors and sql-reference chapters.
Running#
The default way is one process, with the server inside the connector (D66, D67). Configuration:
# mysql.conf # raftdb.conf (the server's own file)
listen = 0.0.0.0:3306 dir = /var/lib/raftdb
embed = /etc/raftdb/raftdb.conf addr = 127.0.0.1:7480
raftdb_user = connector shards = 4
raftdb_password = ... user = connector:...:admin
user = app:secretraftdb-mysql --config mysql.conf then serves MySQL clients on 3306 and, from the same process, Raft DB's binary protocol, HTTP and the Studio on the ports of raftdb.conf. With raftdb = host:port and no embed the front-end is a separate process (two processes), which puts it on another machine than the data; every row operation then costs a network round trip. The MySQL users (user = NAME:PASSWORD) are the connector's own configuration; it acts on Raft DB as the one account raftdb_user.
Modules#
| Module | Role |
|---|---|
wire, auth, server | Packets, handshake, password plugins (caching_sha2_password, mysql_native_password), listener and command loop |
sql (and sql/routine.rs), sql.rs | Lexer and parser of the dialect; what is not supported is an error marked unsupported, answered with MySQL error 1235 |
expr, funcs, funcs2, jsonfn, regex, exprtype | Values and evaluation with MySQL's rules, the function library, JSON functions, a regular-expression matcher, expression typing |
session, txn, prepared | Session variables and execution; transactions; the binary prepared-statement protocol |
query, window, with | The general SELECT executor, window functions, WITH and view resolution |
ddl, alter, dml, write, fk, functional, views, temp, sequence, routines, multi | Tables and SHOW, ALTER TABLE, row writes with undo, foreign keys, functional indexes and generated columns, views, temporary tables, sequences, stored routines and triggers, several result sets |
pushdown, data, link, rdbapi | Turning conditions into Raft DB filters, rows over the binary protocol, in-process or TCP link, the HTTP client |
acl, infoschema | Accounts and privileges; virtual information_schema tables |
dialect, mariadb, sysvars_*, charsets_* | The two dialects, MariaDB error wording, system variable lists, character set lists |
How a table is stored in Raft DB#
A MySQL table t of database d is the Raft DB table t of the Raft DB database d. Its definition (columns, types, primary key, indexes, checks, generated columns and so on) is one document in a hidden table of the database (__rdb_mysql, or __rdb_mariadb for the other dialect). A row is a Raft DB document with one field per column, stored under a key built from the primary key (a text key is lowercased because the default collation does not tell A from a; a table without a primary key has a hidden row id __rowid). DDL goes through Raft DB's HTTP JSON API; rows go through the binary protocol, because only it has an atomic insert-if-absent. A database or table name MySQL accepts but Raft DB cannot name is stored encoded (_u_ followed by each byte as itself or as _xx, D86). Names beginning __rdb_, __tmp or __u_ belong to the connector.
Keeping constraints without a core change#
- Primary key: the atomic insert-if-absent is the uniqueness check; six sessions racing for 25 keys produce exactly 25 winners (a test in D52).
- AUTO_INCREMENT: a value is claimed by inserting the row under that key; a taken key means try the next value. The counter is only a place to start looking, so a restart or a second connector costs retries and never a duplicate.
- UNIQUE keys (D52, level 2a): each unique index has a hidden Raft DB table of claims (
__u_<table>_<index>) keyed by the indexed value; a row with a NULL in the index claims nothing. A new row claims every unique value and then inserts itself. A statement keeps the list of what it did and undoes it newest first if it fails. A claim held by nothing is taken over only when it is also older than 30 seconds (CLAIM_GRACE_SECS), so after a crash a value can be unavailable for up to 30 seconds. - Foreign keys (D64,
fk.rs): checked against the parent when made; a child row must refer to an existing parent row (error 1452);RESTRICT/NO ACTIONrefuse (1451),CASCADEfollows to the referring rows (depth 15) andSET NULLclears them. Rows changed by a cascade are logged so that a failing statement or a rollback puts them back.SET DEFAULTis refused as InnoDB refuses it, and keys must be in the same database. - Statements are all or nothing: every row is checked before the first write, and a write that still fails puts back what the statement did. Concurrent readers can see the halfway state; there is no isolation (see MVCC).
- Accounts (D54): users of the configuration file are bootstrap administrators; other accounts are documents in the hidden database
__rdb_accounts, holding double hashes (never the password) and grants. Every statement is checked from its parsed form against the privileges it needs.
Testing against MySQL and MariaDB#
Compatibility is measured, not claimed (D52). A temporary real MySQL is started, the same script is run through the real client against it and against the connector, and the differences are printed; for the MariaDB dialect it does the same with a real MariaDB 11.4. Java programs with Connector/J are compared the same way. Each decision records which scripts are identical to MySQL and what still differs. The statement-level run against the examples of the MariaDB documentation found, at its first run, 1,685 of 3,954 statements the same, 343 refused alike by both, 577 not supported yet and 1,349 different (many of those follow from one unsupported statement); later decisions closed many of these.
Not supported yet#
Real transaction isolation, COM_STMT_FETCH and server-side cursors, ZEROFILL (accepted and ignored), the system schemas mysql, performance_schema and sys as real databases, events, geometry types, partitioning syntax and system-versioned tables (listed as open in decisions D80 to D88; check the coverage documents for the current state). See Decisions D40-D88 in short.