//! Finding a .pgdb's server, or starting one. //! //! Commands never host Postgres themselves. If the .pgdb's lock is held, a //! server is running and run.json says where its control socket is. //! Otherwise the command starts `minipg serve --background` as a separate //! process in its own process group, waits for it to report ready, and //! connects, holding the server open until the command exits. That server //! stops after MINIPG_IDLE seconds without clients or holds. const std = @import("std"); const Ctx = @import("ctx.zig").Ctx; const pgdb_mod = @import("pgdb.zig"); const paths = @import("paths.zig"); const control = @import("control.zig"); const sys = @import("sys.zig"); const Pgdb = pgdb_mod.Pgdb; /// The default idle timeout, in seconds. pub const default_idle: i64 = 60; pub const Running = struct { info: control.RunInfo, runtime: []const u8, /// The server this command started, if it did. child: ?std.process.Child = null, /// A `hold` on the server, from `ensure`: it keeps the server from /// idling out under this command until the process exits. hold: ?std.Io.net.Stream = null, }; pub fn runtimeDir(ctx: Ctx, p: Pgdb) ![]const u8 { return ctx.dirs.runtimeFor(ctx.arena, try paths.key(ctx.arena, p.path)); } /// The running server's run.json, or null if none is running. A server /// that holds the lock but hasn't written run.json yet is waited for. pub fn find(ctx: Ctx, p: Pgdb) !?Running { const runtime = try runtimeDir(ctx, p); var tries: u32 = 0; while (true) : (tries += 1) { if (!pgdb_mod.locked(ctx.arena, ctx.io, p)) return null; if (control.readRunInfo(ctx.arena, ctx.io, runtime)) |info| { // A killed server's guest processes can hold the lock for a few // seconds after it's gone; wait them out. if (sys.alive(info.pid)) return .{ .info = info, .runtime = runtime }; if (tries > 400) return error.StaleLock; } else |_| { if (tries > 100) return error.NoRunInfo; } try ctx.io.sleep(.fromMilliseconds(50), .awake); } } /// Asks a running server for its status. pub fn status(ctx: Ctx, r: Running) !control.Status { if (r.info.protocol != control.protocol) { ctx.warn("the server for {s} is minipg {s}, which speaks control protocol {d}; this minipg speaks {d}", .{ r.info.pgdb, r.info.version, r.info.protocol, control.protocol }); return error.ProtocolMismatch; } const reply = try control.request(ctx.arena, ctx.io, r.info.control, "status"); if (!reply.ok) { ctx.warn("server: {s}", .{reply.body}); return error.Refused; } return std.json.parseFromSliceLeaky(control.Status, ctx.arena, reply.body, .{ .ignore_unknown_fields = true }); } pub const EnsureOptions = struct { idle_timeout: i64 = default_idle, }; /// The running server, or a new background one, once it's ready, held /// open for the rest of this process (see `Running.hold`). /// /// A server found idling out is waited for and replaced: an idle timeout /// can expire between finding the server and holding it, or, where control /// requests are slow (Windows), while waiting for it to be ready. pub fn ensure(ctx: Ctx, p: Pgdb, opts: EnsureOptions) !Running { var attempts: u32 = 0; while (true) : (attempts += 1) { return ensureOnce(ctx, p, opts) catch |err| switch (err) { error.ServerStopping => { if (attempts >= 3) return err; try waitUnlocked(ctx, p); continue; }, else => return err, }; } } fn ensureOnce(ctx: Ctx, p: Pgdb, opts: EnsureOptions) !Running { if (try find(ctx, p)) |found| { var r = found; try holdReady(ctx, &r, null); return r; } var r: Running = .{ .info = undefined, .runtime = try runtimeDir(ctx, p) }; r.child = try spawn(ctx, p, opts); const child_pid = sys.childPid(r.child.?); // Wait for run.json, then for ready. var tries: u32 = 0; while (true) : (tries += 1) { if (sys.childExited(&r.child.?)) |code| return serverFailed(ctx, p, code); if (control.readRunInfo(ctx.arena, ctx.io, r.runtime)) |info| { if (info.pid == child_pid) { r.info = info; break; } } else |_| {} if (tries > 600) return error.ServerTimeout; try ctx.io.sleep(.fromMilliseconds(50), .awake); } try holdReady(ctx, &r, p); return r; } /// Holds the server, then waits for it to be ready. Held first, so the /// idle timeout can't start between its becoming ready and this command /// connecting. A server that can't be held is still used, as before holds. fn holdReady(ctx: Ctx, r: *Running, starting: ?Pgdb) !void { r.hold = control.hold(ctx.io, r.info.control) catch null; errdefer if (r.hold) |h| { h.close(ctx.io); r.hold = null; }; try waitReady(ctx, r.*, starting); } /// Waits for a stopping server to release the .pgdb's lock. fn waitUnlocked(ctx: Ctx, p: Pgdb) !void { // Postgres's fast shutdown, or the 30s limit after which the server // stops the machine. var tries: u32 = 0; while (pgdb_mod.locked(ctx.arena, ctx.io, p)) : (tries += 1) { if (tries > 400) return error.StopTimeout; try ctx.io.sleep(.fromMilliseconds(100), .awake); } } fn spawn(ctx: Ctx, p: Pgdb, opts: EnsureOptions) !std.process.Child { const self = try std.process.executablePathAlloc(ctx.io, ctx.arena); const idle = try std.fmt.allocPrint(ctx.arena, "--idle={d}", .{opts.idle_timeout}); const std_handles = sys.StdNoInherit.begin(); defer std_handles.restore(); return std.process.spawn(ctx.io, .{ .argv = &.{ self, "serve", "--background", idle, p.path }, .stdin = .ignore, .stdout = .ignore, .stderr = .ignore, // Its own process group, so a Ctrl-C meant for psql doesn't stop it; // on Windows its own hidden console, which closing this one's // doesn't end either. .pgid = if (sys.is_windows) null else 0, .create_no_window = sys.is_windows, }); } fn serverFailed(ctx: Ctx, p: Pgdb, code: u8) error{ServerFailed} { ctx.warn("the server for {s} failed to start (exit {d}); its log ends:", .{ p.path, code }); printLogTail(ctx, p, 20); return error.ServerFailed; } /// Prints the last `n` lines of the .pgdb's default log file. pub fn printLogTail(ctx: Ctx, p: Pgdb, n: usize) void { const key = paths.key(ctx.arena, p.path) catch return; const path = ctx.dirs.logFor(ctx.arena, key) catch return; const text = std.Io.Dir.cwd().readFileAlloc(ctx.io, path, ctx.arena, .limited(16 << 20)) catch return; var start = text.len; var lines: usize = 0; const trimmed = std.mem.trimEnd(u8, text, "\n"); start = trimmed.len; while (start > 0 and lines < n) { start -= 1; if (trimmed[start] == '\n') lines += 1; } if (start > 0) start += 1; std.debug.print("{s}\n", .{trimmed[start..]}); } /// Polls the server's status until it's ready. `starting` is the .pgdb when /// this command started the server, to tell the user about a first start. fn waitReady(ctx: Ctx, r: Running, starting: ?Pgdb) !void { var tries: u32 = 0; var said = false; // A first start unpacks the root and runs initdb: allow minutes. while (tries < 3000) : (tries += 1) { if (r.child) |ch| { var copy = ch; if (sys.childExited(©)) |code| return serverFailed(ctx, starting.?, code); } else if (!sys.alive(r.info.pid)) { // A server another command started, gone while this one // waited: don't poll its dead socket for minutes. return error.ServerStopping; } if (status(ctx, r)) |st| { if (std.mem.eql(u8, st.state, "ready")) return; if (std.mem.eql(u8, st.state, "stopping")) { // The server this command started stopped before it was // ready: its machine exited, and the reason is in its log. // Wait for the process, so the user sees that, not just // ServerStopping. if (r.child) |ch| return waitFailed(ctx, ch, starting.?); return error.ServerStopping; } } else |err| switch (err) { error.ProtocolMismatch => return err, else => {}, } if (tries == 50 and !said) { said = true; ctx.warn("waiting for the server for {s} to start...", .{r.info.pgdb}); } try ctx.io.sleep(.fromMilliseconds(100), .awake); } return error.ServerTimeout; } /// Waits for a server this command started, which is stopping before it /// was ready, to exit, and reports it with its log's tail. fn waitFailed(ctx: Ctx, child: std.process.Child, p: Pgdb) !void { var copy = child; // The server joins its monitor and closes its log after the machine // exits: a few seconds at most. var tries: u32 = 0; while (tries < 600) : (tries += 1) { if (sys.childExited(©)) |code| { // Exit 0 is a stop asked for from elsewhere, not a failure. if (code == 0) return error.ServerStopping; return serverFailed(ctx, p, code); } try ctx.io.sleep(.fromMilliseconds(100), .awake); } ctx.warn("the server for {s} stopped before it was ready; its log ends:", .{p.path}); printLogTail(ctx, p, 20); return error.ServerFailed; } /// Asks the server to stop and waits until its lock is free. pub fn stop(ctx: Ctx, p: Pgdb, r: Running) !void { const reply = try control.request(ctx.arena, ctx.io, r.info.control, "stop"); if (!reply.ok) { ctx.warn("server: {s}", .{reply.body}); return error.Refused; } try waitUnlocked(ctx, p); }