:remove uneccessary locking
This commit is contained in:
parent
1781ed422e
commit
e97532ab5e
14 changed files with 50 additions and 320 deletions
34
beads_cgo.go
34
beads_cgo.go
|
|
@ -4,7 +4,6 @@ package beads
|
|||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
|
|
@ -15,46 +14,29 @@ import (
|
|||
// for the given .beads directory. It reads metadata.json to determine the
|
||||
// configured mode:
|
||||
//
|
||||
// - Embedded mode (default): Opens via the CGo embedded Dolt engine with an
|
||||
// exclusive flock to prevent corruption from concurrent access. This matches
|
||||
// the behavior of the bd CLI.
|
||||
// - Embedded mode (default): Opens via the CGo embedded Dolt engine.
|
||||
// - Server mode: Connects to an external dolt sql-server via OpenFromConfig.
|
||||
//
|
||||
// The returned Storage must be closed when no longer needed. In embedded mode
|
||||
// the caller must also defer the Unlocker returned by the flock; pass nil-safe
|
||||
// patterns such as:
|
||||
//
|
||||
// store, lock, err := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
// if err != nil { ... }
|
||||
// defer lock.Unlock()
|
||||
// defer store.Close()
|
||||
// The returned Storage must be closed when no longer needed.
|
||||
//
|
||||
// beadsDir is the path to the .beads directory.
|
||||
func OpenBestAvailable(ctx context.Context, beadsDir string) (Storage, embeddeddolt.Unlocker, error) {
|
||||
func OpenBestAvailable(ctx context.Context, beadsDir string) (Storage, error) {
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err == nil && cfg != nil && cfg.IsDoltServerMode() {
|
||||
store, err := dolt.NewFromConfig(ctx, beadsDir)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
return store, embeddeddolt.NoopLock{}, nil
|
||||
}
|
||||
|
||||
// Embedded mode: acquire exclusive flock first.
|
||||
dataDir := filepath.Join(beadsDir, "embeddeddolt")
|
||||
lock, err := embeddeddolt.TryLock(dataDir)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return store, nil
|
||||
}
|
||||
|
||||
database := configfile.DefaultDoltDatabase
|
||||
if cfg != nil {
|
||||
database = cfg.GetDoltDatabase()
|
||||
}
|
||||
store, err := embeddeddolt.Open(ctx, beadsDir, database, "main", embeddeddolt.WithLock(lock))
|
||||
store, err := embeddeddolt.Open(ctx, beadsDir, database, "main")
|
||||
if err != nil {
|
||||
lock.Unlock()
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
return store, lock, nil
|
||||
return store, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import (
|
|||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
|
||||
)
|
||||
|
||||
// OpenBestAvailable opens a beads database using the best available backend
|
||||
|
|
@ -16,14 +15,14 @@ import (
|
|||
// supported; embedded mode returns an error directing the user to server mode.
|
||||
//
|
||||
// beadsDir is the path to the .beads directory.
|
||||
func OpenBestAvailable(ctx context.Context, beadsDir string) (Storage, embeddeddolt.Unlocker, error) {
|
||||
func OpenBestAvailable(ctx context.Context, beadsDir string) (Storage, error) {
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err == nil && cfg != nil && cfg.IsDoltServerMode() {
|
||||
store, err := dolt.NewFromConfig(ctx, beadsDir)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
return store, embeddeddolt.NoopLock{}, nil
|
||||
return store, nil
|
||||
}
|
||||
return nil, nil, fmt.Errorf("embedded Dolt requires CGO; use server mode (bd init --server)")
|
||||
return nil, fmt.Errorf("embedded Dolt requires CGO; use server mode (bd init --server)")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -220,11 +220,10 @@ func TestOpenBestAvailable_ServerMode(t *testing.T) {
|
|||
}
|
||||
|
||||
ctx := context.Background()
|
||||
store, lock, err := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
store, err := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("OpenBestAvailable (server mode) failed: %v", err)
|
||||
}
|
||||
defer lock.Unlock()
|
||||
defer store.Close()
|
||||
|
||||
if store == nil {
|
||||
|
|
@ -262,7 +261,7 @@ func TestOpenBestAvailable_ServerMode_FailsWithoutServer(t *testing.T) {
|
|||
}
|
||||
|
||||
ctx := context.Background()
|
||||
_, _, openErr := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
_, openErr := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
if openErr == nil {
|
||||
t.Fatal("OpenBestAvailable (server mode) should fail when no server is running")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ import (
|
|||
"github.com/steveyegge/beads/internal/git"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
|
||||
"github.com/steveyegge/beads/internal/templates/agents"
|
||||
"github.com/steveyegge/beads/internal/ui"
|
||||
"github.com/steveyegge/beads/internal/utils"
|
||||
|
|
@ -766,7 +765,7 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
}
|
||||
}
|
||||
|
||||
store, err := newDoltStore(ctx, doltCfg, embeddeddolt.WithLock(initLock))
|
||||
store, err := newDoltStore(ctx, doltCfg)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: failed to open Dolt store: %v\n", err)
|
||||
os.Exit(1)
|
||||
|
|
|
|||
|
|
@ -1016,7 +1016,7 @@ var rootCmd = &cobra.Command{
|
|||
// Skip auto-import when the user is explicitly running "bd import" —
|
||||
// the import command handles JSONL files itself and auto-importing
|
||||
// first would interfere (double-import / upsert confusion).
|
||||
if store != nil && !useReadOnly && !globalFlag && cmd.Name() != "import" {
|
||||
if store != nil && !useReadOnly && !globalFlag && cmd.Name() != "import" && cmd.Name() != "promote" {
|
||||
maybeAutoImportJSONL(rootCtx, store, beadsDir)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -174,6 +174,7 @@ func TestEmbeddedPromoteCLIConcurrent(t *testing.T) {
|
|||
|
||||
type workerResult struct {
|
||||
worker int
|
||||
out string
|
||||
err error
|
||||
}
|
||||
|
||||
|
|
@ -190,8 +191,9 @@ func TestEmbeddedPromoteCLIConcurrent(t *testing.T) {
|
|||
cmd.Dir = dir
|
||||
cmd.Env = bdEnv(dir)
|
||||
out, err := cmd.CombinedOutput()
|
||||
r.out = string(out)
|
||||
if err != nil {
|
||||
r.err = fmt.Errorf("promote %s: %v\n%s", issueIDs[worker], err, out)
|
||||
r.err = fmt.Errorf("promote %s: %v", issueIDs[worker], err)
|
||||
}
|
||||
|
||||
results[worker] = r
|
||||
|
|
@ -199,30 +201,18 @@ func TestEmbeddedPromoteCLIConcurrent(t *testing.T) {
|
|||
}
|
||||
wg.Wait()
|
||||
|
||||
var successes int
|
||||
// The driver handles concurrency internally — all workers should succeed.
|
||||
for _, r := range results {
|
||||
if r.err != nil {
|
||||
if !strings.Contains(r.err.Error(), "one writer at a time") {
|
||||
t.Errorf("worker %d failed: %v", r.worker, r.err)
|
||||
}
|
||||
continue
|
||||
t.Errorf("worker %d failed: %v", r.worker, r.err)
|
||||
}
|
||||
successes++
|
||||
}
|
||||
if successes == 0 {
|
||||
t.Fatal("all workers failed; expected at least 1 success")
|
||||
}
|
||||
t.Logf("%d/%d workers succeeded (flock contention expected)", successes, numWorkers)
|
||||
|
||||
// Verify only successful workers' issues are permanent
|
||||
for _, r := range results {
|
||||
if r.err != nil {
|
||||
continue
|
||||
}
|
||||
id := issueIDs[r.worker]
|
||||
// Verify all issues are permanent.
|
||||
for i, id := range issueIDs {
|
||||
got := bdShow(t, bd, dir, id)
|
||||
if got.Ephemeral {
|
||||
t.Errorf("expected %s to be permanent after promote", id)
|
||||
t.Errorf("expected %s (worker %d) to be permanent after promote", id, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,11 +40,11 @@ func isEmbeddedMode() bool {
|
|||
// When cfg.ServerMode is true, connects to an external dolt sql-server;
|
||||
// otherwise uses the embedded Dolt engine (default).
|
||||
// Used by bd init and PersistentPreRun.
|
||||
func newDoltStore(ctx context.Context, cfg *dolt.Config, opts ...embeddeddolt.Option) (storage.DoltStorage, error) {
|
||||
func newDoltStore(ctx context.Context, cfg *dolt.Config) (storage.DoltStorage, error) {
|
||||
if cfg.ServerMode {
|
||||
return dolt.New(ctx, cfg)
|
||||
}
|
||||
return embeddeddolt.Open(ctx, cfg.BeadsDir, cfg.Database, "main", opts...)
|
||||
return embeddeddolt.Open(ctx, cfg.BeadsDir, cfg.Database, "main")
|
||||
}
|
||||
|
||||
// acquireEmbeddedLock acquires an exclusive flock on the embeddeddolt data
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ func isEmbeddedMode() bool {
|
|||
|
||||
// newDoltStore creates a server-mode storage backend. Embedded Dolt is not
|
||||
// available without CGO.
|
||||
func newDoltStore(ctx context.Context, cfg *dolt.Config, _ ...embeddeddolt.Option) (storage.DoltStorage, error) {
|
||||
func newDoltStore(ctx context.Context, cfg *dolt.Config) (storage.DoltStorage, error) {
|
||||
if !cfg.ServerMode {
|
||||
return nil, fmt.Errorf("%s", nocgoEmbeddedErrMsg)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,17 +20,15 @@ type cacheEntry struct {
|
|||
}
|
||||
|
||||
// Open returns a cached EmbeddedDoltStore for the given data directory, creating
|
||||
// one via New if no cached instance exists. Subsequent calls with the same
|
||||
// one via newStore if no cached instance exists. Subsequent calls with the same
|
||||
// resolved dataDir return the existing store and increment a reference count.
|
||||
//
|
||||
// Each Open must be paired with a Close. The underlying store is only truly
|
||||
// closed (flock released, resources freed) when the last reference calls Close.
|
||||
// closed when the last reference calls Close.
|
||||
//
|
||||
// This prevents the same-process deadlock that occurs when two code paths open
|
||||
// connectors against the same data directory: the embedded Dolt driver's
|
||||
// internal engine lock combined with infinite-backoff retry means the second
|
||||
// connector spins forever waiting for the first to release.
|
||||
func Open(ctx context.Context, beadsDir, database, branch string, opts ...Option) (*EmbeddedDoltStore, error) {
|
||||
// This prevents redundant engine initializations when multiple code paths open
|
||||
// connectors against the same data directory in the same process.
|
||||
func Open(ctx context.Context, beadsDir, database, branch string) (*EmbeddedDoltStore, error) {
|
||||
key, err := cacheKey(beadsDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -44,9 +42,8 @@ func Open(ctx context.Context, beadsDir, database, branch string, opts ...Option
|
|||
}
|
||||
cacheMu.Unlock()
|
||||
|
||||
// Slow path: create a new store outside the lock. New() acquires the
|
||||
// flock and initializes the schema, which can take significant time.
|
||||
s, err := newStore(ctx, beadsDir, database, branch, opts...)
|
||||
// Slow path: create a new store outside the lock.
|
||||
s, err := newStore(ctx, beadsDir, database, branch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -56,7 +53,7 @@ func Open(ctx context.Context, beadsDir, database, branch string, opts ...Option
|
|||
if entry, ok := cache[key]; ok {
|
||||
cacheMu.Unlock()
|
||||
// Discard the store we just created; use the cached one.
|
||||
_ = s.closeUnderlying()
|
||||
_ = s.Close()
|
||||
entry.refCount++
|
||||
return entry.store, nil
|
||||
}
|
||||
|
|
@ -67,7 +64,7 @@ func Open(ctx context.Context, beadsDir, database, branch string, opts ...Option
|
|||
|
||||
// closeCached decrements the reference count for a cached store.
|
||||
// Returns true when the cache absorbed the close (refs remain, suppress real
|
||||
// close). Returns false when the caller must run closeUnderlying — either the
|
||||
// close). Returns false when the caller must run Close — either the
|
||||
// entry was evicted (last ref) or the store was never cached.
|
||||
func closeCached(s *EmbeddedDoltStore) bool {
|
||||
cacheMu.Lock()
|
||||
|
|
|
|||
|
|
@ -218,8 +218,7 @@ func mustQueryRow(t *testing.T, db *sql.DB, ctx context.Context, query string, a
|
|||
|
||||
// TestConcurrentOpenReturnsCached verifies that opening a second store on
|
||||
// the same data directory returns the cached instance immediately rather than
|
||||
// blocking on the flock (the cache prevents same-process deadlock from the
|
||||
// driver's infinite backoff — GH#2571).
|
||||
// creating a redundant engine initialization.
|
||||
func TestConcurrentOpenReturnsCached(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
|
||||
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt concurrency tests")
|
||||
|
|
@ -246,122 +245,6 @@ func TestConcurrentOpenReturnsCached(t *testing.T) {
|
|||
store2.Close()
|
||||
}
|
||||
|
||||
// TestWaitLockBlocksAndSucceeds verifies that WaitLock blocks until the lock
|
||||
// is released, then acquires it successfully.
|
||||
func TestWaitLockBlocksAndSucceeds(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
|
||||
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt concurrency tests")
|
||||
}
|
||||
|
||||
dataDir := filepath.Join(t.TempDir(), "embeddeddolt")
|
||||
|
||||
// Acquire with TryLock first.
|
||||
lock1, err := embeddeddolt.TryLock(dataDir)
|
||||
if err != nil {
|
||||
t.Fatalf("TryLock: %v", err)
|
||||
}
|
||||
|
||||
ctx := t.Context()
|
||||
type result struct {
|
||||
lock *embeddeddolt.Lock
|
||||
err error
|
||||
}
|
||||
ch := make(chan result, 1)
|
||||
go func() {
|
||||
l, err := embeddeddolt.WaitLock(ctx, dataDir)
|
||||
ch <- result{l, err}
|
||||
}()
|
||||
|
||||
// WaitLock should be blocking.
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
select {
|
||||
case r := <-ch:
|
||||
if r.err != nil {
|
||||
t.Fatalf("WaitLock returned early with error: %v", r.err)
|
||||
}
|
||||
t.Fatal("WaitLock returned before first lock was released")
|
||||
default:
|
||||
}
|
||||
|
||||
// Release first lock.
|
||||
lock1.Unlock()
|
||||
|
||||
select {
|
||||
case r := <-ch:
|
||||
if r.err != nil {
|
||||
t.Fatalf("WaitLock failed after unlock: %v", r.err)
|
||||
}
|
||||
r.lock.Unlock()
|
||||
t.Log("WaitLock succeeded after first lock released")
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("WaitLock did not complete within 10s after unlock")
|
||||
}
|
||||
}
|
||||
|
||||
// TestWaitLockRespectsContextCancellation verifies that WaitLock returns a
|
||||
// context error when the context is cancelled while waiting.
|
||||
func TestWaitLockRespectsContextCancellation(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
|
||||
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt concurrency tests")
|
||||
}
|
||||
|
||||
dataDir := filepath.Join(t.TempDir(), "embeddeddolt")
|
||||
|
||||
// Hold the lock.
|
||||
lock1, err := embeddeddolt.TryLock(dataDir)
|
||||
if err != nil {
|
||||
t.Fatalf("TryLock: %v", err)
|
||||
}
|
||||
defer lock1.Unlock()
|
||||
|
||||
// WaitLock with a short timeout should fail with context error.
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 300*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
_, err = embeddeddolt.WaitLock(ctx, dataDir)
|
||||
if err == nil {
|
||||
t.Fatal("WaitLock succeeded; expected context deadline error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "context deadline exceeded") {
|
||||
t.Fatalf("expected context deadline error, got: %v", err)
|
||||
}
|
||||
t.Logf("WaitLock correctly returned: %v", err)
|
||||
}
|
||||
|
||||
// TestWithLockBypassesDoubleLock verifies that WithLock allows the caller to
|
||||
// supply a pre-acquired lock so New does not attempt a second flock (which
|
||||
// would fail on macOS where flock is per-fd, not per-process).
|
||||
func TestWithLockBypassesDoubleLock(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
|
||||
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt concurrency tests")
|
||||
}
|
||||
|
||||
ctx := t.Context()
|
||||
beadsDir := t.TempDir()
|
||||
dataDir := filepath.Join(beadsDir, "embeddeddolt")
|
||||
|
||||
// Acquire the lock externally (as bd init does).
|
||||
lock, err := embeddeddolt.TryLock(dataDir)
|
||||
if err != nil {
|
||||
t.Fatalf("TryLock: %v", err)
|
||||
}
|
||||
defer lock.Unlock()
|
||||
|
||||
// New with WithLock must succeed without double-locking.
|
||||
store, err := embeddeddolt.Open(ctx, beadsDir, "testdb", "main", embeddeddolt.WithLock(lock))
|
||||
if err != nil {
|
||||
t.Fatalf("New with WithLock: %v", err)
|
||||
}
|
||||
// Close must NOT release the caller-supplied lock.
|
||||
store.Close()
|
||||
|
||||
// The lock should still be held — verify by trying to lock again (should fail).
|
||||
_, err = embeddeddolt.TryLock(dataDir)
|
||||
if err == nil {
|
||||
t.Fatal("TryLock succeeded after Close; expected lock to still be held by caller")
|
||||
}
|
||||
}
|
||||
|
||||
func envInt(name string, def int) int {
|
||||
v := strings.TrimSpace(os.Getenv(name))
|
||||
if v == "" {
|
||||
|
|
|
|||
|
|
@ -3,13 +3,10 @@
|
|||
package embeddeddolt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
backoff "github.com/cenkalti/backoff/v4"
|
||||
"github.com/steveyegge/beads/internal/lockfile"
|
||||
)
|
||||
|
||||
|
|
@ -52,49 +49,6 @@ func TryLock(dataDir string) (*Lock, error) {
|
|||
return &Lock{f: f}, nil
|
||||
}
|
||||
|
||||
// WaitLock blocks until an exclusive flock on <dataDir>/.lock can be acquired
|
||||
// or the context is canceled. It uses exponential backoff with non-blocking
|
||||
// lock attempts so the wait is interruptible via context cancellation.
|
||||
// Non-lock filesystem errors are returned immediately without retrying.
|
||||
func WaitLock(ctx context.Context, dataDir string) (*Lock, error) {
|
||||
if err := os.MkdirAll(dataDir, 0700); err != nil {
|
||||
return nil, fmt.Errorf("embeddeddolt: creating data directory for lock: %w", err)
|
||||
}
|
||||
|
||||
lockPath := filepath.Join(dataDir, ".lock")
|
||||
f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0600) //nolint:gosec // lockPath is derived from dataDir, not user input
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("embeddeddolt: opening lock file: %w", err)
|
||||
}
|
||||
|
||||
bo := backoff.NewExponentialBackOff()
|
||||
bo.InitialInterval = 50 * time.Millisecond
|
||||
bo.MaxInterval = 2 * time.Second
|
||||
bo.MaxElapsedTime = 0 // wait until context cancellation
|
||||
|
||||
err = backoff.Retry(func() error {
|
||||
lockErr := lockfile.FlockExclusiveNonBlocking(f)
|
||||
if lockErr == nil {
|
||||
return nil // acquired
|
||||
}
|
||||
if lockfile.IsLocked(lockErr) {
|
||||
return lockErr // retryable
|
||||
}
|
||||
// Filesystem error — not retryable.
|
||||
return backoff.Permanent(lockErr)
|
||||
}, backoff.WithContext(bo, ctx))
|
||||
|
||||
if err != nil {
|
||||
_ = f.Close()
|
||||
if ctx.Err() != nil {
|
||||
return nil, fmt.Errorf("embeddeddolt: waiting for lock on %s: %w", dataDir, ctx.Err())
|
||||
}
|
||||
return nil, fmt.Errorf("embeddeddolt: acquiring lock: %w", err)
|
||||
}
|
||||
|
||||
return &Lock{f: f}, nil
|
||||
}
|
||||
|
||||
// Unlock releases the flock and closes the underlying file.
|
||||
// Panics on failure to prevent deadlocks.
|
||||
func (l *Lock) Unlock() {
|
||||
|
|
|
|||
|
|
@ -36,10 +36,8 @@ var _ storage.Compactor = (*EmbeddedDoltStore)(nil)
|
|||
// time the embedded engine's write lock is held, reducing contention when
|
||||
// multiple processes access the same database concurrently.
|
||||
//
|
||||
// The store holds an exclusive flock on the data directory for its entire
|
||||
// lifetime. This prevents concurrent processes from initializing the embedded
|
||||
// Dolt engine on the same directory, which causes a nil-pointer panic in
|
||||
// DoltDB.SetCrashOnFatalError (GH#2571).
|
||||
// The dolthub/driver handles its own concurrency internally. File-level locking
|
||||
// is only used during bd init to protect one-time initialization steps.
|
||||
type EmbeddedDoltStore struct {
|
||||
dataDir string
|
||||
beadsDir string
|
||||
|
|
@ -47,8 +45,6 @@ type EmbeddedDoltStore struct {
|
|||
branch string
|
||||
credentialKey []byte
|
||||
closed atomic.Bool
|
||||
lock Unlocker // exclusive flock held for the store's lifetime
|
||||
ownsLock bool // true when New acquired the lock (false when caller supplied it via WithLock)
|
||||
}
|
||||
|
||||
// errClosed is returned when a method is called after Close.
|
||||
|
|
@ -61,44 +57,18 @@ func (s *EmbeddedDoltStore) IsClosed() bool {
|
|||
return s.closed.Load()
|
||||
}
|
||||
|
||||
// Option configures optional behavior for New.
|
||||
type Option func(*options)
|
||||
|
||||
type options struct {
|
||||
lock Unlocker // pre-acquired lock; nil means New acquires its own
|
||||
}
|
||||
|
||||
// WithLock passes a pre-acquired exclusive lock to New so it does not attempt
|
||||
// to acquire a second one. The caller retains ownership — Close will NOT
|
||||
// release a caller-supplied lock. This is used by bd init, which acquires the
|
||||
// lock earlier to protect pre-initialization steps.
|
||||
func WithLock(lock Unlocker) Option {
|
||||
return func(o *options) { o.lock = lock }
|
||||
}
|
||||
|
||||
// newStore creates an EmbeddedDoltStore using the embedded Dolt engine.
|
||||
// beadsDir is the .beads/ root; the data directory is derived as <beadsDir>/embeddeddolt/.
|
||||
// The database is created automatically if it doesn't exist (initSchema handles this).
|
||||
//
|
||||
// An exclusive flock is held on the data directory for the store's entire
|
||||
// lifetime. If another process already holds the lock, newStore queues with
|
||||
// exponential backoff until the lock becomes available or the context is
|
||||
// canceled, instead of panicking during concurrent engine initialization
|
||||
// (GH#2571). The lock is released when Close is called, unless a pre-acquired
|
||||
// lock was supplied via WithLock (in which case the caller is responsible for it).
|
||||
//
|
||||
// Production code should use Open, which routes through a process-scoped cache
|
||||
// to prevent same-process deadlocks from the driver's infinite backoff.
|
||||
func newStore(ctx context.Context, beadsDir, database, branch string, opts ...Option) (*EmbeddedDoltStore, error) {
|
||||
// The dolthub/driver handles its own concurrency internally. File-level locking
|
||||
// is only used during bd init (via TryLock in the init command) to protect
|
||||
// one-time initialization steps — the store itself does not hold any lock.
|
||||
func newStore(ctx context.Context, beadsDir, database, branch string) (*EmbeddedDoltStore, error) {
|
||||
if database == "" {
|
||||
return nil, fmt.Errorf("embeddeddolt: database name must not be empty (caller should default to %q)", "beads")
|
||||
}
|
||||
|
||||
var o options
|
||||
for _, fn := range opts {
|
||||
fn(&o)
|
||||
}
|
||||
|
||||
// Resolve to absolute path — the embedded dolt driver resolves file://
|
||||
// DSN paths relative to its data directory, so relative paths cause
|
||||
// doubled-path errors on subsequent opens.
|
||||
|
|
@ -111,32 +81,14 @@ func newStore(ctx context.Context, beadsDir, database, branch string, opts ...Op
|
|||
return nil, fmt.Errorf("embeddeddolt: creating data directory: %w", err)
|
||||
}
|
||||
|
||||
// Acquire an exclusive flock before initializing the embedded engine.
|
||||
// Without this, concurrent processes race through NewConnector →
|
||||
// DoltDB.SetCrashOnFatalError → newDatabase → CollectDBs and one of
|
||||
// them panics with a nil-pointer dereference (GH#2571).
|
||||
lock := o.lock
|
||||
ownsLock := lock == nil
|
||||
if ownsLock {
|
||||
lock, err = WaitLock(ctx, dataDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
s := &EmbeddedDoltStore{
|
||||
dataDir: dataDir,
|
||||
beadsDir: absBeadsDir,
|
||||
database: database,
|
||||
branch: branch,
|
||||
lock: lock,
|
||||
ownsLock: ownsLock,
|
||||
}
|
||||
|
||||
if err := s.initSchema(ctx); err != nil {
|
||||
if ownsLock {
|
||||
lock.Unlock()
|
||||
}
|
||||
return nil, fmt.Errorf("embeddeddolt: init schema: %w", err)
|
||||
}
|
||||
|
||||
|
|
@ -145,9 +97,6 @@ func newStore(ctx context.Context, beadsDir, database, branch string, opts ...Op
|
|||
// dolt_ignore prevents them from being committed. Server mode handles
|
||||
// this in newServerMode(); embedded mode must do it here. (GH#3270)
|
||||
if err := s.ensureIgnoredTables(ctx); err != nil {
|
||||
if ownsLock {
|
||||
lock.Unlock()
|
||||
}
|
||||
return nil, fmt.Errorf("embeddeddolt: ensure ignored tables: %w", err)
|
||||
}
|
||||
|
||||
|
|
@ -477,30 +426,16 @@ func (s *EmbeddedDoltStore) GetAllEventsSince(ctx context.Context, since time.Ti
|
|||
// Close decrements the reference count if this store was opened via Open (the
|
||||
// process-scoped cache). When other references remain, Close is a no-op — the
|
||||
// store stays alive for the remaining callers. When the last reference calls
|
||||
// Close (or if the store was created directly via New), the underlying
|
||||
// resources are released: orphaned git-remote-cache garbage is cleaned up and
|
||||
// the exclusive flock on the data directory is released (if the store owns it).
|
||||
// Close (or if the store was created directly via newStore), the underlying
|
||||
// resources are released.
|
||||
//
|
||||
// It is safe to call multiple times. When the lock was supplied by the caller
|
||||
// via WithLock, Close does NOT release it — the caller retains ownership.
|
||||
// It is safe to call multiple times.
|
||||
func (s *EmbeddedDoltStore) Close() error {
|
||||
if closeCached(s) {
|
||||
// Other references remain — suppress the real close.
|
||||
return nil
|
||||
}
|
||||
return s.closeUnderlying()
|
||||
}
|
||||
|
||||
// closeUnderlying performs the actual close: marks the store as closed,
|
||||
// cleans up orphaned garbage, and releases the flock.
|
||||
func (s *EmbeddedDoltStore) closeUnderlying() error {
|
||||
// Use CompareAndSwap so we only unlock once even if Close is called
|
||||
// multiple times (the Lock.Unlock method panics on double-unlock).
|
||||
if s.closed.CompareAndSwap(false, true) {
|
||||
s.cleanGitRemoteCacheGarbage()
|
||||
if s.lock != nil && s.ownsLock {
|
||||
s.lock.Unlock()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,17 +14,9 @@ type EmbeddedDoltStore struct {
|
|||
branch string
|
||||
}
|
||||
|
||||
// Option configures optional behavior for Open (stub: no-op).
|
||||
type Option func(*struct{})
|
||||
|
||||
// WithLock is a no-op in non-CGO builds.
|
||||
func WithLock(_ Unlocker) Option {
|
||||
return func(*struct{}) {}
|
||||
}
|
||||
|
||||
var errNoCGO = errors.New("embeddeddolt: requires CGO (build with CGO_ENABLED=1)")
|
||||
|
||||
// Open returns an error when CGO is not enabled.
|
||||
func Open(_ context.Context, _, _, _ string, _ ...Option) (*EmbeddedDoltStore, error) {
|
||||
func Open(_ context.Context, _, _, _ string) (*EmbeddedDoltStore, error) {
|
||||
return nil, errNoCGO
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ func TestOpenBestAvailable_NoCGO_EmbeddedMode_ReturnsError(t *testing.T) {
|
|||
}
|
||||
|
||||
ctx := context.Background()
|
||||
_, _, err := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
_, err := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for embedded mode without CGO")
|
||||
}
|
||||
|
|
@ -45,7 +45,7 @@ func TestOpenBestAvailable_NoCGO_NoMetadata_ReturnsError(t *testing.T) {
|
|||
// No metadata.json — embedded is the default, CGO required.
|
||||
|
||||
ctx := context.Background()
|
||||
_, _, err := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
_, err := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for embedded mode without CGO")
|
||||
}
|
||||
|
|
@ -85,7 +85,7 @@ func TestOpenBestAvailable_NoCGO_ServerMode_FailsWithoutServer(t *testing.T) {
|
|||
}
|
||||
|
||||
ctx := context.Background()
|
||||
_, _, openErr := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
_, openErr := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
if openErr == nil {
|
||||
t.Fatal("OpenBestAvailable (server mode) should fail when no server is running")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue