beads/cmd/bd/dolt_proxied_lifecycle_test.go
Julian Knutsen d803ec9126
fix(dbproxy): retire the proxy when its Dolt backend exits cleanly (#6937)
* fix(dbproxy): retire the proxy when its Dolt backend exits cleanly

DoltServer supervised `dolt sql-server` with cmd.Wait inside an
errgroup, and Running read the errgroup context. errgroup cancels that
context only for a non-nil result, so a backend that shut down
gracefully (dolt exits 0 on SIGTERM) was reported as running forever.
The proxy's backend health watcher never fired: the proxy stayed up and
adoptable in front of a dead store. With --proxied-server-idle-timeout 0
(the setting Gas City uses) it never went away; `bd dolt status` said
"running" and every command, `bd ping` included, failed until
`bd dolt stop`.

The supervising goroutine now reports a clean exit as errBackendExited,
so Running flips on any backend exit and Stop treats it as benign. This
follows the existing proxied lifecycle contract (child death retires
the owned proxy; the next bd invocation starts a fresh proxy/backend
pair) rather than adding in-proxy restarts: spawn, adoption, stop
epochs and identity already live in the on-demand start path, and a
second restart loop inside the proxy would duplicate that and could
mask a crash-looping backend.

`bd dolt status` no longer headlines a proxy whose managed backend is
gone as "running"; it says the proxy is up but not serving and points
at `bd dolt stop` (for proxies from an older bd). The JSON payload is
unchanged. `bd ping` already verifies by running SELECT 1 through the
proxy, so after this fix a successful ping reflects a live pair.

Tests: DoltServer Running goes false after SIGTERM (clean exit) and
SIGKILL, and Stop succeeds; managed-local lane test that SIGTERMs the
backend with idle timeout 0 and requires the proxy to retire and
`bd ping --json` to succeed on a fresh pair; status render case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(dolt status): report running=false for a proxy over a dead managed backend

Review follow-up: the JSON running field now means "serving" (a live proxy
and, on a managed topology, a live dolt behind it), so scripts that key off
running alone are not fooled by a stranded proxy left by an older bd.
proxy_pid/proxy_port and backend_* are unchanged. Adds the PR link to the
CHANGELOG entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(changelog): resolve merge markers left by the origin/main merge

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 01:48:15 -07:00

227 lines
7.8 KiB
Go

package main
import (
"encoding/json"
"path/filepath"
"strings"
"testing"
"time"
"github.com/steveyegge/beads/internal/configfile"
)
// TestProxiedDoltStartRefusalContract pins the parts downstream consumers
// branch on. The code is frozen; the message is informative and may be
// reworded, but it has to keep naming the mode and pointing somewhere useful,
// because "start refused" with no onward path is what sends an operator
// looking for a manual workaround.
func TestProxiedDoltStartRefusalContract(t *testing.T) {
err := proxiedDoltStartRefusal()
if err.Code != "proxy.dolt_start.conflict" {
t.Errorf("code = %q, want %q", err.Code, "proxy.dolt_start.conflict")
}
if err.ExitCode != 1 {
t.Errorf("exit code = %d, want 1", err.ExitCode)
}
if err.Mutates {
t.Error("mutates = true; a refused start changes nothing")
}
for _, want := range []string{"proxied-server mode", "bd dolt status", "bd dolt stop"} {
if !strings.Contains(err.Message, want) {
t.Errorf("message does not mention %q: %q", want, err.Message)
}
}
}
func TestExternalDoltEndpoint(t *testing.T) {
for _, tc := range []struct {
name string
cfg configfile.ExternalDoltConfig
want string
}{
{"tcp", configfile.ExternalDoltConfig{Host: "127.0.0.1", Port: 3310}, "127.0.0.1:3310"},
{"ipv6", configfile.ExternalDoltConfig{Host: "::1", Port: 3310}, "[::1]:3310"},
{"socket wins", configfile.ExternalDoltConfig{Socket: "/tmp/dolt.sock"}, "unix:/tmp/dolt.sock"},
} {
t.Run(tc.name, func(t *testing.T) {
if got := externalDoltEndpoint(tc.cfg); got != tc.want {
t.Errorf("externalDoltEndpoint = %q, want %q", got, tc.want)
}
})
}
}
// TestCollectProxiedDoltStatusQuiescentRoot covers the case an operator hits
// most: nothing is running yet. The answer has to be "not running" WITHOUT
// starting anything — a status command that spawns the topology it is
// reporting on would defeat the idle reaper it is reporting about.
func TestCollectProxiedDoltStatusQuiescentRoot(t *testing.T) {
beadsDir := t.TempDir()
root := filepath.Join(beadsDir, "dolt")
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", root)
status, err := collectProxiedDoltStatus(beadsDir)
if err != nil {
t.Fatalf("collectProxiedDoltStatus: %v", err)
}
if status.Mode != "proxied-server" {
t.Errorf("mode = %q, want %q", status.Mode, "proxied-server")
}
if status.Root != root {
t.Errorf("root = %q, want %q", status.Root, root)
}
if status.Running || status.BackendRunning {
t.Errorf("reported something running for an empty root: %+v", status)
}
if !status.BackendManaged {
t.Error("backend_managed = false with no external sidecar; managed-local is the default topology")
}
if entries, _ := filepath.Glob(filepath.Join(root, "*")); len(entries) != 0 {
t.Errorf("reading status created %v under the proxied root", entries)
}
}
func TestCollectProxiedDoltStatusExternalTopology(t *testing.T) {
beadsDir := t.TempDir()
root := filepath.Join(beadsDir, "dolt")
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", root)
if err := configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{
IdleTimeout: 45 * time.Second,
External: &configfile.ExternalDoltConfig{Host: "10.0.0.5", Port: 3306},
}); err != nil {
t.Fatalf("SaveProxiedServerClientInfo: %v", err)
}
status, err := collectProxiedDoltStatus(beadsDir)
if err != nil {
t.Fatalf("collectProxiedDoltStatus: %v", err)
}
if status.BackendManaged {
t.Error("backend_managed = true for an external topology; bd spawns no dolt there")
}
if status.BackendEndpoint != "10.0.0.5:3306" {
t.Errorf("backend_endpoint = %q, want %q", status.BackendEndpoint, "10.0.0.5:3306")
}
if status.IdleTimeout != "45s" {
t.Errorf("idle_timeout = %q, want %q", status.IdleTimeout, "45s")
}
}
func TestRenderProxiedDoltStatus(t *testing.T) {
running := proxiedDoltStatus{
Mode: "proxied-server", Root: "/w/.beads/dolt", Running: true,
ProxyPID: 111, ProxyPort: 40001,
BackendManaged: true, BackendRunning: true, BackendPID: 222, BackendPort: 40002,
IdleTimeout: "30s",
}
t.Run("text reports both processes", func(t *testing.T) {
defer restoreJSONOutput(t)()
jsonOutput = false
out := captureStdout(t, func() error { renderProxiedDoltStatus(running); return nil })
for _, want := range []string{
"Dolt server: running (proxied-server)",
"Proxy PID: 111",
"Proxy port: 40001",
"Root: /w/.beads/dolt",
"Backend: running (dolt PID 222, port 40002)",
"Idle timeout: 30s",
} {
if !strings.Contains(out, want) {
t.Errorf("missing %q in:\n%s", want, out)
}
}
})
t.Run("text says how to start it when down", func(t *testing.T) {
defer restoreJSONOutput(t)()
jsonOutput = false
down := running
down.Running, down.ProxyPID, down.ProxyPort = false, 0, 0
down.BackendRunning, down.BackendPID, down.BackendPort = false, 0, 0
out := captureStdout(t, func() error { renderProxiedDoltStatus(down); return nil })
for _, want := range []string{
"Dolt server: not running (proxied-server)",
"Backend: not running",
"The proxy starts on demand",
} {
if !strings.Contains(out, want) {
t.Errorf("missing %q in:\n%s", want, out)
}
}
})
t.Run("text does not call a proxy over a dead managed backend running", func(t *testing.T) {
defer restoreJSONOutput(t)()
jsonOutput = false
stranded := running
stranded.Running = false
stranded.BackendRunning, stranded.BackendPID, stranded.BackendPort = false, 0, 0
out := captureStdout(t, func() error { renderProxiedDoltStatus(stranded); return nil })
if strings.Contains(out, "Dolt server: running") {
t.Errorf("proxy with a dead backend reported as running:\n%s", out)
}
for _, want := range []string{
"Dolt server: not serving (proxied-server)",
"Proxy PID: 111",
"Backend: not running",
"bd dolt stop",
} {
if !strings.Contains(out, want) {
t.Errorf("missing %q in:\n%s", want, out)
}
}
})
t.Run("text marks an external backend as somebody else's", func(t *testing.T) {
defer restoreJSONOutput(t)()
jsonOutput = false
external := running
external.BackendManaged, external.BackendRunning = false, false
external.BackendEndpoint = "10.0.0.5:3306"
out := captureStdout(t, func() error { renderProxiedDoltStatus(external); return nil })
if !strings.Contains(out, "Backend: external at 10.0.0.5:3306 (not managed by bd)") {
t.Errorf("external backend not described:\n%s", out)
}
if strings.Contains(out, "Backend: not running") {
t.Errorf("external backend reported as down; bd has no record to say that from:\n%s", out)
}
})
t.Run("json carries the process records", func(t *testing.T) {
defer restoreJSONOutput(t)()
jsonOutput = true
out := captureStdout(t, func() error { renderProxiedDoltStatus(running); return nil })
var got proxiedDoltStatus
if err := json.Unmarshal([]byte(strings.TrimSpace(out)), &got); err != nil {
t.Fatalf("decode %q: %v", out, err)
}
if got != running {
t.Errorf("round-trip mismatch:\ngot %+v\nwant %+v", got, running)
}
})
}
func TestProxiedStatusServing(t *testing.T) {
for _, tc := range []struct {
name string
proxyRunning, managed, backendRunning bool
want bool
}{
{"managed pair live", true, true, true, true},
{"proxy over dead managed backend", true, true, false, false},
{"external backend", true, false, false, true},
{"proxy down", false, true, true, false},
{"nothing running", false, true, false, false},
} {
if got := proxiedStatusServing(tc.proxyRunning, tc.managed, tc.backendRunning); got != tc.want {
t.Errorf("%s: proxiedStatusServing(%v, %v, %v) = %v, want %v", tc.name, tc.proxyRunning, tc.managed, tc.backendRunning, got, tc.want)
}
}
}
func restoreJSONOutput(t *testing.T) func() {
t.Helper()
orig := jsonOutput
return func() { jsonOutput = orig }
}