Compare commits
1 commit
main
...
fix/bd-mnh
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4319e4820d |
5 changed files with 161 additions and 4 deletions
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
|
@ -107,6 +108,28 @@ func TestCollectProxiedDoltStatusExternalTopology(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestCollectProxiedDoltStatusLegacySidecarReportsDefaultIdleTimeout covers a
|
||||
// workspace provisioned before the sidecar persisted its effective idle
|
||||
// timeout: the key is absent on disk, yet the proxy still reaps at the default.
|
||||
// Status must report that default as it does for a fresh workspace, not drop
|
||||
// the field as if no timeout applied (bd-mnhq6).
|
||||
func TestCollectProxiedDoltStatusLegacySidecarReportsDefaultIdleTimeout(t *testing.T) {
|
||||
beadsDir := t.TempDir()
|
||||
root := filepath.Join(beadsDir, "dolt")
|
||||
t.Setenv("BEADS_PROXIED_SERVER_ROOT_PATH", root)
|
||||
if err := os.WriteFile(configfile.ProxiedServerClientInfoPath(beadsDir), []byte(`{}`), 0o600); err != nil {
|
||||
t.Fatalf("seed legacy sidecar: %v", err)
|
||||
}
|
||||
|
||||
status, err := collectProxiedDoltStatus(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("collectProxiedDoltStatus: %v", err)
|
||||
}
|
||||
if want := configfile.DefaultProxyIdleTimeout.String(); status.IdleTimeout != want {
|
||||
t.Errorf("idle_timeout = %q, want %q", status.IdleTimeout, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderProxiedDoltStatus(t *testing.T) {
|
||||
running := proxiedDoltStatus{
|
||||
Mode: "proxied-server", Root: "/w/.beads/dolt", Running: true,
|
||||
|
|
|
|||
|
|
@ -270,10 +270,13 @@ func migrationSidecarsEquivalent(beadsDir string, a, b *configfile.ProxiedServer
|
|||
if a == nil || b == nil {
|
||||
return a == b
|
||||
}
|
||||
// Compare effective idle timeouts: a journal written by an older bd records
|
||||
// a defaulted timeout as the zero sentinel, which Load now resolves.
|
||||
return a.ResolvedRootPath(beadsDir) == b.ResolvedRootPath(beadsDir) &&
|
||||
a.ResolvedConfigPath(beadsDir) == b.ResolvedConfigPath(beadsDir) &&
|
||||
a.ResolvedLogPath(beadsDir) == b.ResolvedLogPath(beadsDir) &&
|
||||
a.Port == b.Port && a.IdleTimeout == b.IdleTimeout &&
|
||||
a.Port == b.Port &&
|
||||
configfile.EffectiveProxyIdleTimeout(a.IdleTimeout) == configfile.EffectiveProxyIdleTimeout(b.IdleTimeout) &&
|
||||
reflect.DeepEqual(a.External, b.External)
|
||||
}
|
||||
|
||||
|
|
@ -836,6 +839,12 @@ func runMigrateToProxiedServer(dryRun bool, idleTimeout time.Duration, shared bo
|
|||
}
|
||||
}
|
||||
if j.Phase == migrateTargetConfigured {
|
||||
// A journal written by an older bd records a defaulted timeout as the
|
||||
// zero sentinel, which Load now resolves. Resolve the plan the same way
|
||||
// Save does on the uninterrupted path so the comparison below holds.
|
||||
if j.Sidecar != nil {
|
||||
j.Sidecar.IdleTimeout = configfile.EffectiveProxyIdleTimeout(j.Sidecar.IdleTimeout)
|
||||
}
|
||||
if info, e := configfile.LoadProxiedServerClientInfo(beadsDir); e != nil {
|
||||
return HandleError("migration sidecar is unreadable: %v", e)
|
||||
} else if info == nil && j.Sidecar != nil {
|
||||
|
|
|
|||
|
|
@ -768,6 +768,84 @@ func TestMigrateToProxiedServer_ResumeReplaysRecordedIdleTimeout(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestMigrateResume_JournalFromBeforeEffectiveIdleTimeout covers an upgrade in
|
||||
// the middle of a migration. Releases before the sidecar persisted its
|
||||
// effective idle timeout recorded a defaulted timeout as an absent key, in the
|
||||
// journal's plan and in the sidecar alike. The sidecar load now resolves that
|
||||
// to the default, so comparing it raw against the journal's zero would refuse
|
||||
// the resume in both directions over a plan that never changed (bd-mnhq6). A
|
||||
// timeout that really differs must still be refused.
|
||||
func TestMigrateResume_JournalFromBeforeEffectiveIdleTimeout(t *testing.T) {
|
||||
// legacyShape drops idle_timeout from a sidecar, or from the sidecar a
|
||||
// journal records, which is how those releases wrote a defaulted timeout.
|
||||
legacyShape := func(t *testing.T, path, sidecarKey string) {
|
||||
t.Helper()
|
||||
var doc map[string]any
|
||||
require.NoError(t, json.Unmarshal(mustReadFile(t, path), &doc))
|
||||
sidecar := doc
|
||||
if sidecarKey != "" {
|
||||
sidecar = doc[sidecarKey].(map[string]any)
|
||||
}
|
||||
require.Contains(t, sidecar, "idle_timeout")
|
||||
delete(sidecar, "idle_timeout")
|
||||
b, err := json.Marshal(doc)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.WriteFile(path, b, 0o600))
|
||||
}
|
||||
forward := func(t *testing.T) (string, func() error) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
return beadsDir, func() error { return runMigrateToProxiedServer(false, 0, false) }
|
||||
}
|
||||
reverse := func(t *testing.T) (string, func() error) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeProxiedServer)
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, &configfile.ProxiedServerClientInfo{RootPath: filepath.Join(beadsDir, "dolt")}))
|
||||
return beadsDir, func() error { return runMigrateFromProxiedServer(false, false) }
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
setup func(*testing.T) (string, func() error)
|
||||
phase migratePhase
|
||||
changed time.Duration // non-zero: the sidecar on disk no longer holds the default
|
||||
}{
|
||||
{name: "forward at target_configured", setup: forward, phase: migrateTargetConfigured},
|
||||
{name: "reverse at prepared", setup: reverse, phase: migratePrepared},
|
||||
{name: "reverse at target_configured", setup: reverse, phase: migrateTargetConfigured},
|
||||
{name: "forward with a changed timeout refuses", setup: forward, phase: migrateTargetConfigured, changed: 45 * time.Second},
|
||||
{name: "reverse with a changed timeout refuses", setup: reverse, phase: migratePrepared, changed: 45 * time.Second},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
beadsDir, migrate := tc.setup(t)
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", string(tc.phase))
|
||||
require.Error(t, migrate())
|
||||
t.Setenv("BEADS_MIGRATION_FAIL_PHASE", "")
|
||||
|
||||
legacyShape(t, migrateJournalPath(beadsDir), "sidecar")
|
||||
if tc.changed == 0 {
|
||||
legacyShape(t, configfile.ProxiedServerClientInfoPath(beadsDir), "")
|
||||
} else {
|
||||
info, err := configfile.LoadProxiedServerClientInfo(beadsDir)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, info)
|
||||
info.IdleTimeout = tc.changed
|
||||
require.NoError(t, configfile.SaveProxiedServerClientInfo(beadsDir, info))
|
||||
}
|
||||
|
||||
var migrateErr error
|
||||
stderr := captureStderr(t, func() { migrateErr = migrate() })
|
||||
if tc.changed != 0 {
|
||||
require.Error(t, migrateErr)
|
||||
assert.Contains(t, stderr, "migration sidecar does not match prepared state")
|
||||
return
|
||||
}
|
||||
require.NoError(t, migrateErr, "stderr: %s", stderr)
|
||||
j, err := loadMigrateJournal(beadsDir)
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, j, "successful migration removes its journal")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateJournal_UnknownPhaseFailsClosed(t *testing.T) {
|
||||
beadsDir := migrateModeWorkspace(t, configfile.DoltModeServer)
|
||||
path := migrateJournalPath(beadsDir)
|
||||
|
|
|
|||
|
|
@ -912,6 +912,40 @@ func TestSaveProxiedServerClientInfo_WritesEffectiveIdleTimeout(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestLoadProxiedServerClientInfo_ResolvesEffectiveIdleTimeout pins the read
|
||||
// side of the same contract (bd-mnhq6). Sidecars written before the effective
|
||||
// value was persisted omit idle_timeout, and nothing rewrites them, so Load is
|
||||
// where a pre-upgrade workspace stops reporting an absent timeout that readers
|
||||
// cannot tell apart from "unset". A negative value is the "never" sentinel and
|
||||
// must survive the load rather than be swept into the default.
|
||||
func TestLoadProxiedServerClientInfo_ResolvesEffectiveIdleTimeout(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
sidecar string
|
||||
want time.Duration
|
||||
}{
|
||||
{name: "legacy absent key", sidecar: `{"root_path":"/var/lib/beads/proxieddb"}`, want: DefaultProxyIdleTimeout},
|
||||
{name: "explicit zero", sidecar: `{"root_path":"/var/lib/beads/proxieddb","idle_timeout":0}`, want: DefaultProxyIdleTimeout},
|
||||
{name: "never", sidecar: `{"root_path":"/var/lib/beads/proxieddb","idle_timeout":-1}`, want: -1},
|
||||
{name: "other negative", sidecar: `{"root_path":"/var/lib/beads/proxieddb","idle_timeout":-5000000000}`, want: -5 * time.Second},
|
||||
{name: "positive", sidecar: `{"root_path":"/var/lib/beads/proxieddb","idle_timeout":45000000000}`, want: 45 * time.Second},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(ProxiedServerClientInfoPath(dir), []byte(tc.sidecar), 0o600); err != nil {
|
||||
t.Fatalf("seed sidecar: %v", err)
|
||||
}
|
||||
got, err := LoadProxiedServerClientInfo(dir)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("Load: %v got=%v", err, got)
|
||||
}
|
||||
if got.IdleTimeout != tc.want {
|
||||
t.Errorf("IdleTimeout = %s, want %s", got.IdleTimeout, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxiedServerClientInfo_ResolvedPaths(t *testing.T) {
|
||||
beadsDir := "/home/user/project/.beads"
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,17 @@ func ProxiedServerClientInfoPath(beadsDir string) string {
|
|||
return filepath.Join(beadsDir, ProxiedServerClientInfoFileName)
|
||||
}
|
||||
|
||||
// EffectiveProxyIdleTimeout resolves the zero "use the default" sentinel to
|
||||
// DefaultProxyIdleTimeout. Every other value is already effective and is
|
||||
// returned unchanged; a negative value means "never" and must not be swept
|
||||
// into the default.
|
||||
func EffectiveProxyIdleTimeout(d time.Duration) time.Duration {
|
||||
if d == 0 {
|
||||
return DefaultProxyIdleTimeout
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func LoadProxiedServerClientInfo(beadsDir string) (*ProxiedServerClientInfo, error) {
|
||||
path := ProxiedServerClientInfoPath(beadsDir)
|
||||
data, err := os.ReadFile(path) // #nosec G304 - controlled path
|
||||
|
|
@ -41,6 +52,10 @@ func LoadProxiedServerClientInfo(beadsDir string) (*ProxiedServerClientInfo, err
|
|||
if err := json.Unmarshal(data, &info); err != nil {
|
||||
return nil, fmt.Errorf("parsing %s: %w", ProxiedServerClientInfoFileName, err)
|
||||
}
|
||||
// Sidecars written before Save persisted the effective value omit the key,
|
||||
// and nothing rewrites them. Resolve it on read too, so a pre-upgrade
|
||||
// workspace reports the timeout bd applies rather than an absent one.
|
||||
info.IdleTimeout = EffectiveProxyIdleTimeout(info.IdleTimeout)
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
|
|
@ -52,9 +67,7 @@ func SaveProxiedServerClientInfo(beadsDir string, info *ProxiedServerClientInfo)
|
|||
// value so sidecar consumers do not need bd's source code to interpret an
|
||||
// absent key. Update the caller's plan too: migration resume compares the
|
||||
// in-memory plan with the sidecar after this write.
|
||||
if info.IdleTimeout == 0 {
|
||||
info.IdleTimeout = DefaultProxyIdleTimeout
|
||||
}
|
||||
info.IdleTimeout = EffectiveProxyIdleTimeout(info.IdleTimeout)
|
||||
data, err := json.MarshalIndent(info, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshaling %s: %w", ProxiedServerClientInfoFileName, err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue