src/sydra/server.zig
Purpose
Top-level runtime orchestration and CLI dispatch.
This module:
- Parses CLI arguments
- Loads configuration (or uses defaults)
- Instantiates the engine and ancillary subsystems
- Routes to the HTTP server or subcommands (
pgwire,ingest,query, etc.)
Human-facing banners stay on stderr and are gated to TTY sessions. Command result payloads are emitted on stdout.
Public API
pub fn run(handle: *alloc_mod.AllocatorHandle) !void
Dispatch rules:
- No args or
serve→ start the HTTP server pgwire→ start the PostgreSQL wire protocol listeneringest→ read NDJSON from stdin and ingestquery→ range query byseries_idcompact→ run compactionsnapshot/restore→ snapshot managementstats→ print basic stats (and allocator stats in some modes)
The handle is used to:
- Provide the allocator via
handle.allocator() - Expose allocator statistics for
stats - Pass through to the HTTP server entrypoint
pub fn run(handle: *alloc_mod.AllocatorHandle) !void {
const alloc = handle.allocator();
const args = try std.process.argsAlloc(alloc);
defer std.process.argsFree(alloc, args);
if (args.len <= 1 or std.mem.eql(u8, args[1], "serve")) {
// starts the engine + HTTP server
// ...
return;
}
const cmd = args[1];
if (std.mem.eql(u8, cmd, "pgwire")) return cmdPgWire(alloc, args);
if (std.mem.eql(u8, cmd, "ingest")) return cmdIngest(alloc, args);
if (std.mem.eql(u8, cmd, "query")) return cmdQuery(alloc, args);
if (std.mem.eql(u8, cmd, "compact")) return cmdCompact(alloc, args);
if (std.mem.eql(u8, cmd, "snapshot")) return cmdSnapshot(alloc, args);
if (std.mem.eql(u8, cmd, "restore")) return cmdRestore(alloc, args);
if (std.mem.eql(u8, cmd, "stats")) return cmdStats(handle, alloc, args);
}
Key internal helpers
fn loadConfigOrDefault(alloc: std.mem.Allocator) !config.Config
Loads sydradb.toml from the current working directory. If loading/parsing fails, returns a default config literal.
Notable defaults:
data_dir = "./data"http_port = 8080fsync = intervalflush_interval_ms = 2000memtable_max_bytes = 8 MiBretention_days = 0(keep forever)auth_token = ""(auth disabled)enable_prom = truemem_limit_bytes = 256 MiBretention_ns = StringHashMap(u32)(per-namespace retention map)
Callers must deinit the returned config to free owned allocations.
fn loadConfigOrDefault(alloc: std.mem.Allocator) !config.Config {
return config.load(alloc, "sydradb.toml") catch config.Config{
.data_dir = try alloc.dupe(u8, "./data"),
.http_port = 8080,
.fsync = .interval,
.flush_interval_ms = 2000,
.memtable_max_bytes = 8 * 1024 * 1024,
.retention_days = 0,
.auth_token = try alloc.dupe(u8, ""),
.enable_influx = false,
.enable_prom = true,
.mem_limit_bytes = 256 * 1024 * 1024,
.retention_ns = std.StringHashMap(u32).init(alloc),
};
}
Commands
fn cmdPgWire(alloc: std.mem.Allocator, args: [][:0]u8) !void
Starts a pgwire listener backed by an engine instance.
- Default
address:127.0.0.1(override viaargs[2]) - Default
port:6432(override viaargs[3])
It constructs:
compat.wire.session.SessionConfig{}compat.wire.server.ServerConfig{ address, port, session, engine }
And runs compat.wire.server.run(alloc, server_cfg).
The startup banner is now only printed when stderr is attached to a TTY, so background automation does not need to filter it.
fn cmdIngest(alloc: std.mem.Allocator, args: [][:0]u8) !void
Reads NDJSON from stdin and ingests each line into the engine.
Expected per-line JSON fields:
series(string)ts(integer)value(number, optional)fields(object, optional; first numeric field is used whenvalueis absent)tags(object, optional)
Series IDs:
- This command now uses the same
types.seriesIdFrom(series, tags_json)derivation as HTTP ingest. - The command flushes before exit, so a successful return means the ingested points are queryable from local state.
- The human-readable
ingested {n} pointssummary is only printed when stderr is attached to a TTY.
const parsed = http.parseIngestLine(alloc, slice) catch continue;
defer parsed.deinit(alloc);
_ = try http.applyIngestLine(eng, parsed);
_ = try eng.flushNow();
fn cmdQuery(alloc: std.mem.Allocator, args: [][:0]u8) !void
Usage:
sydradb query <series_id> <start_ts> <end_ts>
Runs Engine.queryRange and prints CSV rows as:
ts,value
The CSV rows are emitted on stdout.
fn cmdCompact(alloc: std.mem.Allocator, args: [][:0]u8) !void
Opens cfg.data_dir, loads or initializes the manifest, then runs storage compaction across all segments.
fn cmdSnapshot(alloc: std.mem.Allocator, args: [][:0]u8) !void
Usage:
sydradb snapshot <dst_dir>
Calls snapshot.zig to write a snapshot.
fn cmdRestore(alloc: std.mem.Allocator, args: [][:0]u8) !void
Usage:
sydradb restore <src_dir>
Calls snapshot.zig to restore from a snapshot.
fn cmdStats(handle: *alloc_mod.AllocatorHandle, alloc: std.mem.Allocator, args: [][:0]u8) !void
- Counts segment files under
<data_dir>/segments/**and printssegments_total. - If built with the
small_poolallocator mode, prints allocator stats viahandle.snapshotSmallPoolStats(). - The stats lines are emitted on stdout.