* feat(backup): run opted-in auto-backup on a managed-local proxied server #6584 routed the five `bd backup` verbs over the proxied provider on managed-local, and #6694 made `bd backup status` admit the remaining gap: the proxied arm of PersistentPostRunE never called runPostRunAutoBackup, so backup.enabled=true was inert on every proxied workspace. Close it. The proxied arm now calls the auto-backup hook before it closes the provider, under the direct arm's maintenance gate (strict --readonly, `bd serve`, a migration freeze) plus previews. maybeAutoBackup picks a backend for the command: the direct store as before, or the proxied provider's RunNonTx seam, which `bd backup sync` already uses. The proxied backend is honored exactly where `bd backup sync` is: the "backup sync" capability row's rule for this workspace's topology, so managed-local only. Auto-backup registers a server-side backup remote, which on a server bd does not own is global to every client; an implicit trigger must not get further than the explicit verb. It never opens a provider of its own: a command that opened none (`bd dolt stop`) or closed it (restore) backs nothing up rather than relaunching the server. The default stays OFF, as on every server-mode workspace. The register/sync-to-a-local-dir sequence that DoltStore and EmbeddedDoltStore each carried a copy of moves to versioncontrolops.BackupToDir (and HEAD lookup to CurrentCommit), so the proxied backend reuses it instead of adding a third copy. Tests: TestManagedLocalProxiedAutoBackup (default off, opt-in writes a restorable backup, change detection, `bd dolt stop` stays quiescent, restore round trip with issue-set equality; fails against origin/main); TestManagedLocalProxiedBackupRestoreRefusedWhileAttached pins the restore design (refused while another bd client holds the workspace, serving child untouched, succeeds once it detaches); unit tests for the backend locality gate, the post-run wiring, the status note and BackupToDir. Both managed-local tests, and the existing round trip, are added to the proxied-local smoke lane's compiled-in list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(backup): serialize auto-backup and bd backup sync with a workspace backup lock Review follow-up on #6879. Auto-backup and an explicit bd backup sync both run under the command's SHARED workspace gate, so nothing stopped them overlapping, and auto-backup's remove -> add -> sync on one fixed remote name races a concurrent copy of itself. A per-workspace flock (.beads/backup.lock, already gitignored by *.lock) now serializes them in every mode. Auto-backup takes it non-blocking and skips when held (the throttle is left alone, so a later command retries). bd backup sync waits up to exclusiveGateWait - the bound restore's exclusive gate acquisition uses - then fails with "another backup is running for this workspace" before touching the store. Restore needs no part in it: its exclusive workspace gate already excludes every command that could hold the lock. Tests: lock semantics (refusal while held, bounded wait, release), auto-backup skipping while held and running once released, and sync failing loudly without reaching the store. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
5 KiB
Go
142 lines
5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/steveyegge/beads/internal/configfile"
|
|
"github.com/steveyegge/beads/internal/storage/uow"
|
|
)
|
|
|
|
// fakeMaintenanceProvider is a proxied provider stand-in exposing the
|
|
// non-transactional seam proxied auto-backup runs on. It never opens a
|
|
// connection; the tests here only assert whether it would be used.
|
|
type fakeMaintenanceProvider struct {
|
|
uow.UnitOfWorkProvider
|
|
runs int
|
|
}
|
|
|
|
func (f *fakeMaintenanceProvider) RunNonTx(context.Context, func(context.Context, *sql.Conn) error) error {
|
|
f.runs++
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeMaintenanceProvider) Close(context.Context) error { return nil }
|
|
|
|
// TestProxiedAutoBackupBackendFollowsLocality pins where auto-backup may run on
|
|
// a proxied workspace: exactly where `bd backup sync` is honored. Auto-backup
|
|
// registers a server-side backup remote, which on a server bd does not own is
|
|
// global to every client of it — the storm the explicit verbs refuse by design
|
|
// — so an implicit trigger must not get further than the explicit one. An
|
|
// unreadable sidecar resolves to the unknown topology and fails closed.
|
|
//
|
|
// Cannot be parallel: mutates the proxiedServerMode and uowProvider globals.
|
|
func TestProxiedAutoBackupBackendFollowsLocality(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
sidecar string
|
|
noProvider bool
|
|
wantBackend bool
|
|
}{
|
|
{name: "managed-local with an open provider", sidecar: `{}`, wantBackend: true},
|
|
{name: "managed-local with no provider open", sidecar: `{}`, noProvider: true},
|
|
{name: "external tcp", sidecar: `{"external":{"host":"db.example.com","port":3306}}`},
|
|
{name: "external unix socket", sidecar: `{"external":{"socket":"/tmp/dolt.sock"}}`},
|
|
{name: "unreadable sidecar", sidecar: `{not json`},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
beadsDir := prepareBackupStatusTest(t)
|
|
proxiedServerMode = true
|
|
if err := os.WriteFile(configfile.ProxiedServerClientInfoPath(beadsDir), []byte(tc.sidecar), 0o600); err != nil {
|
|
t.Fatalf("write proxied sidecar: %v", err)
|
|
}
|
|
|
|
oldProvider := uowProvider
|
|
t.Cleanup(func() { uowProvider = oldProvider })
|
|
fake := &fakeMaintenanceProvider{}
|
|
uowProvider = fake
|
|
if tc.noProvider {
|
|
uowProvider = nil
|
|
}
|
|
|
|
backend, ok := autoBackupBackendForCommand()
|
|
if ok != tc.wantBackend {
|
|
t.Fatalf("autoBackupBackendForCommand() ok = %v, want %v", ok, tc.wantBackend)
|
|
}
|
|
if !ok {
|
|
return
|
|
}
|
|
if _, isProxied := backend.(proxiedLocalBackup); !isProxied {
|
|
t.Fatalf("backend = %T, want proxiedLocalBackup", backend)
|
|
}
|
|
if fake.runs != 0 {
|
|
t.Fatalf("resolving the backend ran %d maintenance call(s); it must not touch the server", fake.runs)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestPersistentPostRunProxiedRunsAutoBackup pins the wiring: the proxied arm
|
|
// of PersistentPostRunE calls the auto-backup hook, under the same gate the
|
|
// direct arm's maintenance net uses, plus previews.
|
|
//
|
|
// Before this hook existed the proxied arm only pruned the journal and closed
|
|
// the provider, so backup.enabled=true was inert on every proxied workspace.
|
|
//
|
|
// Cannot be parallel: mutates PersistentPostRunE's package globals.
|
|
func TestPersistentPostRunProxiedRunsAutoBackup(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
cmd func() *cobra.Command
|
|
readonly bool
|
|
want int
|
|
}{
|
|
{name: "ordinary command", cmd: func() *cobra.Command { return &cobra.Command{Use: "list"} }, want: 1},
|
|
{name: "strict readonly", cmd: func() *cobra.Command { return &cobra.Command{Use: "list"} }, readonly: true},
|
|
{name: "bd serve", cmd: func() *cobra.Command { return &cobra.Command{Use: serveCmdName} }},
|
|
{name: "preview", cmd: func() *cobra.Command {
|
|
c := &cobra.Command{Use: "list"}
|
|
c.Flags().Bool("dry-run", false, "")
|
|
if err := c.Flags().Set("dry-run", "true"); err != nil {
|
|
t.Fatalf("set --dry-run: %v", err)
|
|
}
|
|
return c
|
|
}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Chdir(t.TempDir())
|
|
t.Setenv("BEADS_DIR", "")
|
|
|
|
oldStore, oldProvider := store, uowProvider
|
|
oldReadonly, oldProxied := readonlyMode, proxiedServerMode
|
|
oldRootCtx, oldRootCancel := rootCtx, rootCancel
|
|
oldSpan, oldProfile, oldTrace := commandSpan, profileFile, traceFile
|
|
oldBackup := runPostRunAutoBackup
|
|
t.Cleanup(func() {
|
|
store, uowProvider = oldStore, oldProvider
|
|
readonlyMode, proxiedServerMode = oldReadonly, oldProxied
|
|
rootCtx, rootCancel = oldRootCtx, oldRootCancel
|
|
commandSpan, profileFile, traceFile = oldSpan, oldProfile, oldTrace
|
|
runPostRunAutoBackup = oldBackup
|
|
})
|
|
|
|
calls := 0
|
|
runPostRunAutoBackup = func(context.Context) { calls++ }
|
|
store, uowProvider = nil, nil
|
|
readonlyMode = tc.readonly
|
|
proxiedServerMode = true
|
|
rootCtx, rootCancel = context.Background(), nil
|
|
commandSpan, profileFile, traceFile = nil, nil, nil
|
|
|
|
if err := rootCmd.PersistentPostRunE(tc.cmd(), nil); err != nil {
|
|
t.Fatalf("PersistentPostRunE: %v", err)
|
|
}
|
|
if calls != tc.want {
|
|
t.Fatalf("auto-backup hook calls = %d, want %d", calls, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|