fix(storage): stop the #5920 gate refusing migrations it authorized or owns (#6088)

The shared-store consent gate (#6048) broke three contracts it never meant
to touch, all red on the Server Dolt matrix.

Authorization lifetime. The gate ran on every attempt of the schema-init
retry loop. An attempt that dies mid-pass leaves the cursor ADVANCED, so the
re-check no longer saw the fresh database it allowed a moment earlier — it
saw "existing database, 65 pending" and refused the very migration it had
just authorized, stranding the open half-migrated. It is now evaluated once
per logical open: the ALLOW is latched, while a gate whose probes hit a
transient catalog race has decided nothing and is still retried. Fresh-
bootstrap heal authority short-circuits it outright, the same standing proof
of creation the proxied path uses in schema.MigrateUpWithLock (#6055).

That alone restores both #5012 fresh-bootstrap tests, including the negative
one: reaching MigrateUp again is what lets the #4566 dirty-table guard fire
on a database this open did not create, instead of the gate pre-empting it.

Predicate. #6048 treated every DoltStore as shared, reasoning that "a
sql-server accepts co-resident clients by construction". True in the
abstract, but it swept in the server bd auto-starts for a single workspace —
a database no other client can observe — and so refused migrations on
workspaces that own their own server, breaking the #4566/#5781 lenient-open
recovery whose whole shape is refuse-then-commit-then-migrate on exactly
such a server. sharedServerDatabase now draws the line where doltserver
already draws it for auto-start: bd-owned lifecycle migrates on open as it
always has; shared-server mode, an operator-managed server, a remote host, a
TLS endpoint, a unix socket, or no workspace at all stays gated. It reads
only signals set on every open — notably not cfg.AutoStart or cfg.ServerMode,
which the CLI sets and library callers leave false, and reading them would
silently ungate a library client attached to a team's server.

The #5920 report, its regression test, and every shared deployment are the
external shape, which stays gated: bd list must still never promote the
cursor there, and does not.

Tests: the three named tests pass unmodified; new TestSharedServerDatabase
pins the predicate in both directions.
This commit is contained in:
Julian Knutsen 2026-08-30 21:38:19 -07:00 • committed by GitHub
commit ed7c268bb3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 177 additions and 8 deletions

View file

@ -0,0 +1,96 @@
package dolt
import (
"os"
"path/filepath"
"testing"
)
// TestSharedServerDatabase pins which topologies the #5920 consent gate
// applies to. Getting this wrong is expensive in both directions: too broad
// and bd refuses to migrate a workspace's own database (the release blocker
// this predicate was introduced to fix), too narrow and an upgraded client
// silently promotes the schema for a whole team.
//
// Every case fixes the environment explicitly — the predicate reads process
// state, so a leaked BEADS_DOLT_* from another test would otherwise decide the
// answer.
func TestSharedServerDatabase(t *testing.T) {
// A workspace with no metadata.json: doltserver resolves it as a server
// whose lifecycle bd owns.
ownedWorkspace := func(t *testing.T) string {
t.Helper()
dir := filepath.Join(t.TempDir(), ".beads")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
return dir
}
clearEnv := func(t *testing.T) {
t.Helper()
for _, k := range []string{
"BEADS_DOLT_SHARED_SERVER", "BEADS_DOLT_SERVER_MODE",
"BEADS_DOLT_SERVER_HOST", "BEADS_DOLT_SERVER_PORT", "BEADS_DOLT_PORT",
} {
t.Setenv(k, "")
}
}
t.Run("bd-owned local server is not shared", func(t *testing.T) {
clearEnv(t)
cfg := &Config{BeadsDir: ownedWorkspace(t), ServerHost: "127.0.0.1", ServerPort: 3307}
if sharedServerDatabase(cfg) {
t.Fatal("a server bd auto-started for this workspace must migrate on open, as embedded does")
}
})
t.Run("no workspace fails closed", func(t *testing.T) {
clearEnv(t)
// A bare dolt.New pointed at an endpoint: nothing proves bd owns it,
// and a library caller attached to a team's server must not migrate it.
cfg := &Config{ServerHost: "127.0.0.1", ServerPort: 3307}
if !sharedServerDatabase(cfg) {
t.Fatal("without a workspace to prove ownership, the database must be treated as shared")
}
if !sharedServerDatabase(nil) {
t.Fatal("a nil config must be treated as shared")
}
})
t.Run("shared-server mode is shared", func(t *testing.T) {
clearEnv(t)
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1")
cfg := &Config{BeadsDir: ownedWorkspace(t), ServerHost: "127.0.0.1", ServerPort: 3308}
if !sharedServerDatabase(cfg) {
t.Fatal("shared-server mode is one server for many workspaces — the #5920 shape")
}
})
t.Run("explicit external server mode is shared", func(t *testing.T) {
clearEnv(t)
t.Setenv("BEADS_DOLT_SERVER_MODE", "1")
cfg := &Config{BeadsDir: ownedWorkspace(t), ServerHost: "127.0.0.1", ServerPort: 3307}
if !sharedServerDatabase(cfg) {
t.Fatal("an operator-managed server is not bd's to migrate unprompted")
}
})
for _, tt := range []struct {
name string
cfg Config
}{
{name: "remote host", cfg: Config{ServerHost: "db.example.com", ServerPort: 3307}},
{name: "TLS endpoint", cfg: Config{ServerHost: "127.0.0.1", ServerPort: 3307, ServerTLS: true}},
{name: "unix socket", cfg: Config{ServerHost: "127.0.0.1", ServerSocket: "/tmp/dolt.sock"}},
} {
t.Run(tt.name+" is shared", func(t *testing.T) {
clearEnv(t)
cfg := tt.cfg
cfg.BeadsDir = ownedWorkspace(t)
if !sharedServerDatabase(&cfg) {
t.Fatalf("%s cannot be a server bd auto-started for this workspace", tt.name)
}
})
}
}

View file

@ -2749,15 +2749,36 @@ func initSchemaOnDBWithRetryAndGateBootstrapHeal(
// Must exceed schema.MigrateUpWithLock's 5s GET_LOCK wait so a contended
// schema migration can time out once and still retry.
schemaBO.MaxElapsedTime = serverRetryMaxElapsed
// The gate authorizes this LOGICAL open, once — it is not a per-attempt
// re-check. Two things make re-checking wrong (gastownhall/beads#5012
// shape, caught by the fresh-bootstrap heal tests):
//
// - an attempt that dies mid-pass leaves the schema cursor ADVANCED, so
// a re-check no longer sees the fresh database it allowed a moment
// ago. It sees "existing database, migrations pending" and refuses the
// very migration it just authorized, stranding the open half-migrated;
// - fresh-bootstrap heal authority is issued only to the open that won
// the bare CREATE for this exact database incarnation, so it is
// standing proof that creating it was consent for its schema. That
// proof survives a retry; a version read does not.
//
// Same argument, same shape as the proxied path's skip in
// schema.MigrateUpWithLock. Only the ALLOW is latched: a gate whose probes
// hit a transient startup/catalog race has not decided anything yet, and
// is still retried below.
gateSatisfied := bootstrapHeal != nil
var applied int
err := backoff.Retry(func() error {
if gate != nil {
if gate != nil && !gateSatisfied {
if gateErr := gate(ctx, db); gateErr != nil {
if !schema.IsRemoteMigrateGateError(gateErr) && isRetryableError(gateErr) {
return gateErr
}
return backoff.Permanent(gateErr)
}
gateSatisfied = true
}
var schemaErr error
applied, schemaErr = initSchemaOnDBWithBootstrapHeal(ctx, db, bootstrapHeal, endpoint)
@ -2772,6 +2793,56 @@ func initSchemaOnDBWithRetryAndGateBootstrapHeal(
return applied, err
}
// sharedServerDatabase reports whether this store's database is served to bd
// clients beyond this workspace — the condition the #5920 consent gate exists
// for, since migrating promotes the schema version for every one of them at
// once and locks out any still on an older bd.
//
// The line is who owns the server's lifecycle, which doltserver already
// resolves for the auto-start decision:
//
// - bd auto-started this server for THIS workspace (ServerModeOwned): the
// database is this workspace's own, the way an embedded database is, and
// bd migrates it on open exactly as it always has. Refusing here would
// leave a single-user workspace unable to migrate itself, and would break
// the #4566/#5781 dirty-table recovery, whose whole shape is
// refuse-then-commit-then-migrate on a bd-owned local server.
// - anything else — shared-server mode, an explicit dolt_server_port in
// metadata.json, host inference, a caller with no workspace at all —
// is someone else's server. Fail closed: without a workspace to prove
// ownership from, assume the database is shared.
//
// This narrows #6048's original predicate, which treated every DoltStore as
// shared on the reasoning that "a sql-server accepts co-resident clients by
// construction". True in the abstract, but it swept in bd's own
// single-workspace servers and so refused migrations that no other client
// could ever observe. The #5920 report, and the regression test for it, are
// both the external-server shape this keeps gated.
// Every signal it reads is one that is set on EVERY open. Deliberately absent
// are cfg.AutoStart, cfg.ServerMode and s.autoStartedServerDir, which all name
// the right idea but are populated only by the CLI (or, for the last, only
// under BEADS_TEST_MODE): a library caller pointed at a genuinely shared
// server leaves them false, and reading them would silently ungate it.
func sharedServerDatabase(cfg *Config) bool {
// No workspace to prove ownership from — a bare dolt.New pointed at some
// endpoint. Fail closed.
if cfg == nil || cfg.BeadsDir == "" {
return true
}
// Somebody else's server by construction: a host that is not this
// machine, a TLS endpoint (Hosted Dolt), or a unix socket — bd's own
// auto-start only ever creates a local TCP listener.
if !isLocalHost(cfg.ServerHost) || cfg.ServerTLS || cfg.ServerSocket != "" {
return true
}
// The one shared topology that is otherwise indistinguishable from an
// owned one: same machine, same TCP shape, one server for many workspaces.
if doltserver.IsSharedServerMode() {
return true
}
return doltserver.ResolveServerMode(cfg.BeadsDir) != doltserver.ServerModeOwned
}
func (s *DoltStore) initSchema(ctx context.Context, bootstrapHeal *schema.FreshBootstrapHealCapability) (int, error) {
// Schema migrations can run arbitrarily long (e.g. full-table recomputes
// such as the is_blocked backfill in migration 0047). The main connection
@ -2820,14 +2891,16 @@ func (s *DoltStore) initSchema(ctx context.Context, bootstrapHeal *schema.FreshB
// depend on that external guard alone.
ReadOnly: s.readOnly,
}
// The shared variant (#5920): a DoltStore is always a sql-server client,
// and a sql-server accepts co-resident clients by construction — that is
// what distinguishes it from the flock-guarded single-writer embedded
// store. So this open must never promote the schema cursor on its own,
// with or without a remote. Auto-started servers included: other bd
// processes attach to them while they run, which is the exact shape #5920
// was reported on.
// #5920: on a database served to OTHER workspaces' bd clients, migrating
// promotes the schema for all of them at once, so this open must not do it
// unprompted. On a server whose lifecycle bd owns for this one workspace,
// the pre-existing contract stands and the open migrates (see
// sharedServerDatabase).
gate := func(ctx context.Context, db *sql.DB) error {
if !sharedServerDatabase(s.cfg) {
return schema.CheckRemoteMigrateGateForRemoteWithRemoteCheckAndAdopt(
ctx, db, s.remote, s.hasPersistedCLIRemote, adopt)
}
return schema.CheckSharedStoreMigrateGate(ctx, db, s.remote, s.hasPersistedCLIRemote, adopt)
}
applied, err := initSchemaOnDBWithRetryAndGateBootstrapHeal(ctx, migDB, gate, bootstrapHeal, s.serverEndpoint)