wy-shls5t: take the schema-init lock only when there is a migration to run
Every bd invocation against a shared Dolt server ran its schema-init probe
inside the database-scoped GET_LOCK('bd_schema_init:<db>', 5), so N processes
serialized on the lock to each discover they had nothing to migrate. Measured
on an 18-seat rig: 1500 is_free_lock samples over 14.0s found the lock HELD
96.7% of the time; held runs median 673ms, max 4243ms; free gaps median 0ms —
handed straight from holder to holder, with four distinct connections caught
inside GET_LOCK in one 1.5s processlist window. That queue was the 0.4-2.4s
every seat paid on every claim, heartbeat, ready read, comment and mail check.
The statements under the lock are individually cheap and almost always a
no-op — a dozen information_schema probes, two MAX(version) reads, two SHOW
COLUMNS, and a run of INSERT IGNORE INTO dolt_ignore. The cost is the
SERIALIZATION, not the queries, so batching them does not help; answering the
steady-state question WITHOUT the lock does.
MigrateUpWithLock now runs alreadyConverged first, on the same pinned session
and with no writes of its own: the session must be on the target database,
migrationWorkNeeded (the same gate MigrateUp's own no-work short-circuit uses)
must report nothing pending, and every canonical dolt_ignore pattern
seedDoltIgnorePatterns would assert must already be present. Only then does it
return (0, nil) without touching GET_LOCK. Anything else takes the lock and
runs exactly the pass it ran before.
Three things keep this from being a correctness change:
- It is fail-closed. Any error, any unreadable state, a NULL or mismatched
DATABASE(), a missing ignore pattern — all return false and fall through
to the locked path. A false negative costs one ordinary locked pass.
- It never skips work. The predicates are the ones MigrateUp evaluates
itself; when they all hold, the locked pass would acquire the lock, take
the no-work short-circuit with seedChanged false, and return (0, nil).
dolt_ignore presence is read (SELECT pattern), not asserted (INSERT
IGNORE), and is judged on the pattern alone so an operator override
recorded with ignored=false — which INSERT IGNORE would leave untouched —
is not misread as under-seeded.
- A caller carrying fresh-bootstrap heal authority never enters it, so the
#5012 bootstrap sequence is unchanged. A caller's locked preparation is
skipped only once alreadyConverged has proved the session is already on
the target database, which is exactly what preparation's CREATE DATABASE
and USE would have established; on a database it did not create,
preparation captures no authority.
Tests: the fast path issues no GET_LOCK and no locked preparation on a
converged database; a database one migration behind still takes the lock and
applies the pass; a bootstrap-heal caller's first statement is still GET_LOCK;
a wrong or unselected DATABASE() short-circuits before any schema probe; each
canonical ignore pattern in turn, when missing, refuses the fast path; an
unreadable cursor and a failing dolt_ignore read both fail closed.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
62d211937b
commit
0a11d81cb9
5 changed files with 499 additions and 0 deletions
18
CHANGELOG.md
18
CHANGELOG.md
|
|
@ -158,6 +158,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
### Fixed
|
||||
|
||||
- **`bd` no longer serializes every invocation behind the schema-init advisory
|
||||
lock.** The schema-init probe ran entirely inside the database-scoped
|
||||
`GET_LOCK('bd_schema_init:<db>', 5)`, so on a shared Dolt server every `bd`
|
||||
process queued behind every other one to discover that it had nothing to
|
||||
migrate. Measured on an 18-seat rig: 1500 `is_free_lock` samples over 14s
|
||||
found the lock held 96.7% of the time, held runs had a 673ms median and a
|
||||
4.2s max, and free gaps had a 0ms median — the lock was handed straight from
|
||||
holder to holder. That queue was 0.4-2.4s of pure waiting on every claim,
|
||||
heartbeat, list, comment and mail check. `MigrateUpWithLock` now answers the
|
||||
steady-state question first, without the lock — is this session on the target
|
||||
database, are both migration cursors at this binary's latest with the
|
||||
content-hash column and custom-status/type backfills done, and are all the
|
||||
canonical `dolt_ignore` patterns present — and takes `GET_LOCK` only on the
|
||||
path that can actually migrate. The probe is read-only and fails closed: any
|
||||
error, any unreadable state, and anything short of provably converged falls
|
||||
through to the locked path unchanged, as does any caller carrying
|
||||
fresh-bootstrap heal authority.
|
||||
|
||||
- **Incremental auto-export now actually takes the incremental path**
|
||||
([#5806](https://github.com/gastownhall/beads/pull/5806)). Change detection
|
||||
compared `GetStateHash()` values — a hash of the entire database plus
|
||||
|
|
|
|||
120
internal/storage/schema/converged.go
Normal file
120
internal/storage/schema/converged.go
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
package schema
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/dberrors"
|
||||
)
|
||||
|
||||
// alreadyConverged reports whether databaseName is already at this binary's
|
||||
// target schema in every respect MigrateUp checks before its no-work
|
||||
// short-circuit: the session is on the right database, no migration work is
|
||||
// pending, and the canonical dolt_ignore patterns are all present. When it
|
||||
// says yes, a MigrateUp pass would acquire the migration lock, do nothing,
|
||||
// and return (0, nil).
|
||||
//
|
||||
// Why it exists: the whole schema-init probe ran INSIDE the database-scoped
|
||||
// GET_LOCK, so every bd invocation against a shared Dolt server serialized on
|
||||
// it even though the probe is a no-op in the steady state. Measured on an
|
||||
// 18-seat rig, the lock was held 96.7% of a 14s sampling window, held runs had
|
||||
// a 673ms median and a 4.2s max, and free gaps had a 0ms median — handed
|
||||
// straight from holder to holder. That queue was 0.4-2.4s of pure waiting on
|
||||
// every claim, heartbeat, list and comment. The statements under the lock are
|
||||
// individually cheap; the SERIALIZATION is the cost, so batching them does not
|
||||
// help and answering the steady-state question WITHOUT the lock does.
|
||||
//
|
||||
// It is deliberately advisory and fails closed onto the locked path. Any
|
||||
// error, any unreadable state, and anything short of provably converged
|
||||
// returns false, and the caller then behaves exactly as it did before this
|
||||
// check existed. A false negative costs one ordinary locked pass; false
|
||||
// positives are avoided by evaluating the same predicates MigrateUp itself
|
||||
// evaluates, on the same pinned session, with no writes of its own.
|
||||
func alreadyConverged(ctx context.Context, db DBConn, databaseName string) (bool, error) {
|
||||
if databaseName == "" {
|
||||
return false, nil
|
||||
}
|
||||
// The session must already be on the target database. This is what makes
|
||||
// skipping a caller's locked bootstrap preparation safe: preparation
|
||||
// creates the database and USEs it, and a session that is already on
|
||||
// databaseName has provably had both done for it. A NULL result (no
|
||||
// database selected, or one dropped underneath us) is not converged.
|
||||
var current sql.NullString
|
||||
if err := db.QueryRowContext(ctx, "SELECT DATABASE()").Scan(¤t); err != nil {
|
||||
return false, fmt.Errorf("reading current database: %w", err)
|
||||
}
|
||||
if !current.Valid || current.String != databaseName {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Exactly MigrateUp's own gate, and it runs first: on a fresh or
|
||||
// mid-upgrade database it reports work needed from the cursor probe alone,
|
||||
// before any statement that could fail against a missing table.
|
||||
needed, err := migrationWorkNeeded(ctx, db)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("checking schema migration work: %w", err)
|
||||
}
|
||||
if needed {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// MigrateUp re-asserts the canonical dolt_ignore patterns ahead of that
|
||||
// gate precisely because an out-of-band-materialized database can arrive
|
||||
// with its cursors at-latest and the patterns missing. Read the same
|
||||
// question instead of writing it: an under-seeded database is not
|
||||
// converged and must take the locked path, which heals and commits it.
|
||||
return doltIgnoreSeeded(ctx, db)
|
||||
}
|
||||
|
||||
// doltIgnoreSeeded reports whether every canonical dolt_ignore pattern
|
||||
// seedDoltIgnorePatterns would assert is already present. It is the read-only
|
||||
// counterpart of that seed and shares its version gate: a pattern whose flip
|
||||
// migration has not been reached yet is not expected, exactly as the seed
|
||||
// would not insert it.
|
||||
//
|
||||
// Presence is judged on the pattern alone, never on its ignored value, because
|
||||
// INSERT IGNORE would leave an explicit operator override (a pattern recorded
|
||||
// with ignored=false) untouched. Reporting such a row as missing would send
|
||||
// every invocation down the locked path forever.
|
||||
func doltIgnoreSeeded(ctx context.Context, db DBConn) (bool, error) {
|
||||
rows, err := db.QueryContext(ctx, "SELECT pattern FROM dolt_ignore")
|
||||
if err != nil {
|
||||
if dberrors.IsTableNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, fmt.Errorf("reading dolt_ignore: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
present := make(map[string]struct{})
|
||||
for rows.Next() {
|
||||
var pattern string
|
||||
if err := rows.Scan(&pattern); err != nil {
|
||||
return false, fmt.Errorf("reading dolt_ignore: %w", err)
|
||||
}
|
||||
present[pattern] = struct{}{}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return false, fmt.Errorf("reading dolt_ignore: %w", err)
|
||||
}
|
||||
|
||||
for _, pattern := range doltIgnorePatterns {
|
||||
if _, ok := present[pattern]; !ok {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
mainVersion, err := mainSource.currentVersion(ctx, db)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("reading schema version for gated ignore patterns: %w", err)
|
||||
}
|
||||
for _, gated := range versionGatedDoltIgnorePatterns {
|
||||
if mainVersion < gated.minMainVersion {
|
||||
continue
|
||||
}
|
||||
if _, ok := present[gated.pattern]; !ok {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
336
internal/storage/schema/converged_test.go
Normal file
336
internal/storage/schema/converged_test.go
Normal file
|
|
@ -0,0 +1,336 @@
|
|||
package schema
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
)
|
||||
|
||||
// expectConvergedFastPathMiss primes the pre-lock convergence probe to report
|
||||
// a session with no selected database, so the fast path declines on its first
|
||||
// statement and the locked path runs exactly as it did before the probe
|
||||
// existed. Every MigrateUpWithLock test that exercises the locked path leads
|
||||
// with it.
|
||||
func expectConvergedFastPathMiss(mock sqlmock.Sqlmock) {
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT DATABASE()")).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"DATABASE()"}).AddRow(nil))
|
||||
}
|
||||
|
||||
// expectCurrentDatabase mocks the fast path's opening question: which database
|
||||
// is this pinned session actually on?
|
||||
func expectCurrentDatabase(mock sqlmock.Sqlmock, name string) {
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT DATABASE()")).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"DATABASE()"}).AddRow(name))
|
||||
}
|
||||
|
||||
// expectNoMigrationWorkNeeded mocks migrationWorkNeeded on a fully upgraded
|
||||
// database: both cursors at latest, both content_hash columns present, no
|
||||
// custom-status/type backfill pending.
|
||||
func expectNoMigrationWorkNeeded(mock sqlmock.Sqlmock) {
|
||||
expectCursorProbe(mock, "schema_migrations", true)
|
||||
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM schema_migrations", "version", LatestVersion())
|
||||
expectCursorProbe(mock, "ignored_schema_migrations", true)
|
||||
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM ignored_schema_migrations", "version", LatestIgnoredVersion())
|
||||
expectIgnoredSentinelProbes(mock, true)
|
||||
expectContentHashColumnExists(mock)
|
||||
expectContentHashColumnExists(mock)
|
||||
expectScalar(mock, "SELECT COUNT(*) FROM custom_types", "count", 1)
|
||||
expectScalar(mock, "SELECT COUNT(*) FROM custom_statuses", "count", 1)
|
||||
}
|
||||
|
||||
// expectDoltIgnoreRead mocks the read-only dolt_ignore probe, returning
|
||||
// exactly the patterns given, plus the main-cursor read that qualifies the
|
||||
// version-gated patterns.
|
||||
func expectDoltIgnoreRead(mock sqlmock.Sqlmock, patterns []string, mainVersion int) {
|
||||
rows := sqlmock.NewRows([]string{"pattern"})
|
||||
for _, pattern := range patterns {
|
||||
rows.AddRow(pattern)
|
||||
}
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT pattern FROM dolt_ignore")).WillReturnRows(rows)
|
||||
expectCursorProbe(mock, "schema_migrations", true)
|
||||
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM schema_migrations", "version", mainVersion)
|
||||
}
|
||||
|
||||
// seededIgnorePatterns is every pattern seedDoltIgnorePatterns would assert on
|
||||
// a database whose main cursor is at mainVersion.
|
||||
func seededIgnorePatterns(mainVersion int) []string {
|
||||
patterns := append([]string(nil), doltIgnorePatterns...)
|
||||
for _, gated := range versionGatedDoltIgnorePatterns {
|
||||
if mainVersion >= gated.minMainVersion {
|
||||
patterns = append(patterns, gated.pattern)
|
||||
}
|
||||
}
|
||||
return patterns
|
||||
}
|
||||
|
||||
// expectConvergedProbe mocks the whole fast path on a converged database.
|
||||
func expectConvergedProbe(mock sqlmock.Sqlmock, database string) {
|
||||
expectCurrentDatabase(mock, database)
|
||||
expectNoMigrationWorkNeeded(mock)
|
||||
expectDoltIgnoreRead(mock, seededIgnorePatterns(LatestVersion()), LatestVersion())
|
||||
}
|
||||
|
||||
func newMockConn(t *testing.T) (*sql.Conn, sqlmock.Sqlmock, func()) {
|
||||
t.Helper()
|
||||
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("create sql mock: %v", err)
|
||||
}
|
||||
conn, err := db.Conn(context.Background())
|
||||
if err != nil {
|
||||
db.Close()
|
||||
t.Fatalf("pin mock connection: %v", err)
|
||||
}
|
||||
return conn, mock, func() {
|
||||
conn.Close()
|
||||
db.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateUpWithLockSkipsLockWhenAlreadyConverged is the point of the whole
|
||||
// change: on a database that needs nothing, no GET_LOCK is issued at all. The
|
||||
// mock is primed with the probe and nothing else, so a GET_LOCK — or the
|
||||
// caller's locked preparation, whose CREATE DATABASE the fast path also skips
|
||||
// — would surface as an unexpected statement and fail the call.
|
||||
func TestMigrateUpWithLockSkipsLockWhenAlreadyConverged(t *testing.T) {
|
||||
conn, mock, cleanup := newMockConn(t)
|
||||
defer cleanup()
|
||||
|
||||
expectConvergedProbe(mock, "testdb")
|
||||
|
||||
prepared := 0
|
||||
applied, err := MigrateUpWithLock(context.Background(), conn, "testdb",
|
||||
WithLockedPreparation("tcp:test", func(context.Context, *sql.Conn) (*FreshBootstrapHealCapability, error) {
|
||||
prepared++
|
||||
return nil, nil
|
||||
}))
|
||||
if err != nil {
|
||||
t.Fatalf("MigrateUpWithLock() error = %v, want nil (converged database must short-circuit before GET_LOCK)", err)
|
||||
}
|
||||
if applied != 0 {
|
||||
t.Fatalf("MigrateUpWithLock() applied = %d, want 0", applied)
|
||||
}
|
||||
if prepared != 0 {
|
||||
t.Fatalf("locked preparation ran %d times on a converged database, want 0", prepared)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatalf("unmet SQL expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateUpWithLockTakesLockWhenBehind pins the other half of the
|
||||
// contract: a database one migration behind still goes through GET_LOCK and
|
||||
// the full pass. Without this, a fast path that answered "converged"
|
||||
// unconditionally would still pass the test above.
|
||||
func TestMigrateUpWithLockTakesLockWhenBehind(t *testing.T) {
|
||||
conn, mock, cleanup := newMockConn(t)
|
||||
defer cleanup()
|
||||
|
||||
// Fast path: right database, but the main cursor is behind -> work needed.
|
||||
expectCurrentDatabase(mock, "testdb")
|
||||
expectCursorProbe(mock, "schema_migrations", true)
|
||||
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM schema_migrations", "version", LatestVersion()-1)
|
||||
|
||||
lockName := MigrationLockName("testdb")
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT GET_LOCK(?, ?)")).
|
||||
WithArgs(lockName, migrationLockAcquireTimeoutSeconds).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"locked"}).AddRow(1))
|
||||
expectOnePendingMigration(t, mock)
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT RELEASE_LOCK(?)")).
|
||||
WithArgs(lockName).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"released"}).AddRow(1))
|
||||
|
||||
applied, err := MigrateUpWithLock(context.Background(), conn, "testdb")
|
||||
if err != nil {
|
||||
t.Fatalf("MigrateUpWithLock() error = %v", err)
|
||||
}
|
||||
if applied != 1 {
|
||||
t.Fatalf("MigrateUpWithLock() applied = %d, want 1", applied)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatalf("unmet SQL expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateUpWithLockKeepsLockWithBootstrapHeal pins that a caller carrying
|
||||
// fresh-bootstrap reset authority never enters the fast path: the very first
|
||||
// statement it issues is GET_LOCK, so the #5012 bootstrap sequence is
|
||||
// byte-identical to what it was before the probe existed.
|
||||
func TestMigrateUpWithLockKeepsLockWithBootstrapHeal(t *testing.T) {
|
||||
conn, mock, cleanup := newMockConn(t)
|
||||
defer cleanup()
|
||||
|
||||
lockName := MigrationLockName("testdb")
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT GET_LOCK(?, ?)")).
|
||||
WithArgs(lockName, migrationLockAcquireTimeoutSeconds).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"locked"}).AddRow(1))
|
||||
expectDirtyGuardRefusal(t, mock)
|
||||
expectFreshBootstrapIdentityMatch(mock)
|
||||
mock.ExpectQuery(regexp.QuoteMeta("CALL DOLT_RESET('--hard')")).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"status"}))
|
||||
expectOnePendingMigration(t, mock)
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT RELEASE_LOCK(?)")).
|
||||
WithArgs(lockName).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"released"}).AddRow(1))
|
||||
|
||||
applied, err := MigrateUpWithLock(context.Background(), conn, "testdb",
|
||||
WithFreshBootstrapHeal(testFreshBootstrapHealCapability(), testBootstrapEndpoint))
|
||||
if err != nil {
|
||||
t.Fatalf("MigrateUpWithLock() error = %v", err)
|
||||
}
|
||||
if applied != 1 {
|
||||
t.Fatalf("MigrateUpWithLock() applied = %d, want 1", applied)
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatalf("unmet SQL expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlreadyConvergedRequiresTheTargetDatabase(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
current any
|
||||
}{
|
||||
{name: "no database selected", current: nil},
|
||||
{name: "different database", current: "otherdb"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("create sql mock: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT DATABASE()")).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"DATABASE()"}).AddRow(tt.current))
|
||||
|
||||
converged, err := alreadyConverged(context.Background(), db, "testdb")
|
||||
if err != nil {
|
||||
t.Fatalf("alreadyConverged() error = %v", err)
|
||||
}
|
||||
if converged {
|
||||
t.Fatal("alreadyConverged() = true, want false: the session is not on the target database, so nothing has been proved about it")
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatalf("unmet SQL expectations (the database check must short-circuit before any schema probe): %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAlreadyConvergedRejectsUnderSeededDoltIgnore is the case
|
||||
// seedDoltIgnorePatterns exists for: an out-of-band-materialized database
|
||||
// arrives with its cursors at-latest and the ignore patterns missing. The fast
|
||||
// path must refuse it so the locked pass can heal and commit the seed.
|
||||
func TestAlreadyConvergedRejectsUnderSeededDoltIgnore(t *testing.T) {
|
||||
for _, missing := range seededIgnorePatterns(LatestVersion()) {
|
||||
t.Run(missing, func(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("create sql mock: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
var present []string
|
||||
for _, pattern := range seededIgnorePatterns(LatestVersion()) {
|
||||
if pattern != missing {
|
||||
present = append(present, pattern)
|
||||
}
|
||||
}
|
||||
expectCurrentDatabase(mock, "testdb")
|
||||
expectNoMigrationWorkNeeded(mock)
|
||||
expectDoltIgnoreRead(mock, present, LatestVersion())
|
||||
|
||||
converged, err := alreadyConverged(context.Background(), db, "testdb")
|
||||
if err != nil {
|
||||
t.Fatalf("alreadyConverged() error = %v", err)
|
||||
}
|
||||
if converged {
|
||||
t.Fatalf("alreadyConverged() = true with dolt_ignore pattern %q missing, want false", missing)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAlreadyConvergedAcceptsOverriddenIgnorePattern pins that presence is
|
||||
// judged on the pattern alone. An operator override (the row exists with
|
||||
// ignored=false) is what INSERT IGNORE would leave untouched, so treating it
|
||||
// as missing would send every invocation down the locked path forever — the
|
||||
// saturation this change removes.
|
||||
func TestAlreadyConvergedAcceptsOverriddenIgnorePattern(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("create sql mock: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
expectCurrentDatabase(mock, "testdb")
|
||||
expectNoMigrationWorkNeeded(mock)
|
||||
// The probe selects patterns only; the mock never offers the ignored
|
||||
// column, so a query that filtered on it would not match this expectation.
|
||||
expectDoltIgnoreRead(mock, seededIgnorePatterns(LatestVersion()), LatestVersion())
|
||||
|
||||
converged, err := alreadyConverged(context.Background(), db, "testdb")
|
||||
if err != nil {
|
||||
t.Fatalf("alreadyConverged() error = %v", err)
|
||||
}
|
||||
if !converged {
|
||||
t.Fatal("alreadyConverged() = false on a fully seeded database, want true")
|
||||
}
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Fatalf("unmet SQL expectations: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAlreadyConvergedFailsClosedOnUnreadableState pins the fail-closed
|
||||
// contract from both sides. A cursor probe that errors is swallowed by
|
||||
// atLatest into "work needed", so the answer is a plain not-converged; a
|
||||
// dolt_ignore read that errors surfaces the error. MigrateUpWithLock treats
|
||||
// anything but (true, nil) as "take the lock", so both end at the locked path.
|
||||
func TestAlreadyConvergedFailsClosedOnUnreadableState(t *testing.T) {
|
||||
t.Run("cursor probe fails", func(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("create sql mock: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
expectCurrentDatabase(mock, "testdb")
|
||||
mock.ExpectQuery(`SELECT COUNT\(\*\) FROM information_schema\.tables`).
|
||||
WillReturnError(sql.ErrConnDone)
|
||||
|
||||
converged, err := alreadyConverged(context.Background(), db, "testdb")
|
||||
if err != nil {
|
||||
t.Fatalf("alreadyConverged() error = %v", err)
|
||||
}
|
||||
if converged {
|
||||
t.Fatal("alreadyConverged() = true on an unreadable cursor, want false")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("dolt_ignore read fails", func(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("create sql mock: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
expectCurrentDatabase(mock, "testdb")
|
||||
expectNoMigrationWorkNeeded(mock)
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT pattern FROM dolt_ignore")).
|
||||
WillReturnError(sql.ErrConnDone)
|
||||
|
||||
converged, err := alreadyConverged(context.Background(), db, "testdb")
|
||||
if err == nil {
|
||||
t.Fatal("alreadyConverged() error = nil, want the dolt_ignore read failure")
|
||||
}
|
||||
if converged {
|
||||
t.Fatal("alreadyConverged() = true on a failed dolt_ignore read, want false")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
@ -181,6 +181,27 @@ func MigrateUpWithLock(ctx context.Context, conn *sql.Conn, databaseName string,
|
|||
opt(&o)
|
||||
}
|
||||
|
||||
// Steady-state fast path. In the overwhelmingly common case the database
|
||||
// is already at this binary's schema and the whole locked pass is a probe
|
||||
// that finds nothing to do — but running that probe under the
|
||||
// database-scoped GET_LOCK serialized every bd invocation on a shared Dolt
|
||||
// server behind every other one (96.7% lock saturation on an 18-seat rig;
|
||||
// see alreadyConverged). Answer the same question first, without the lock,
|
||||
// and take the lock only on the path that can actually migrate.
|
||||
//
|
||||
// Skipped when the caller carries fresh-bootstrap heal authority: that
|
||||
// capability is issued only to the init that just created the database, so
|
||||
// there is nothing converged to detect and the bootstrap path stays
|
||||
// exactly as it was. A caller's locked preparation is skipped only because
|
||||
// alreadyConverged has proved the session is already on databaseName —
|
||||
// preparation's CREATE DATABASE and USE have therefore already happened,
|
||||
// and it captures no authority on a database it did not create.
|
||||
if o.freshBootstrapHeal == nil {
|
||||
if converged, convergedErr := alreadyConverged(ctx, conn, databaseName); convergedErr == nil && converged {
|
||||
return 0, nil
|
||||
}
|
||||
}
|
||||
|
||||
lockName := MigrationLockName(databaseName)
|
||||
if err := AcquireMigrationLock(ctx, conn, lockName); err != nil {
|
||||
return 0, err
|
||||
|
|
|
|||
|
|
@ -76,6 +76,7 @@ func TestMigrateUpWithLockUsesDatabaseScopedLockOnly(t *testing.T) {
|
|||
defer conn.Close()
|
||||
|
||||
lockName := MigrationLockName("testdb")
|
||||
expectConvergedFastPathMiss(mock)
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT GET_LOCK(?, ?)")).
|
||||
WithArgs(lockName, migrationLockAcquireTimeoutSeconds).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"locked"}).AddRow(1))
|
||||
|
|
@ -111,6 +112,7 @@ func TestMigrateUpWithLockPreparationErrorReleasesAndJoinsReleaseFailure(t *test
|
|||
defer conn.Close()
|
||||
|
||||
lockName := MigrationLockName("testdb")
|
||||
expectConvergedFastPathMiss(mock)
|
||||
preparationErr := errors.New("bootstrap preparation failed")
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT GET_LOCK(?, ?)")).
|
||||
WithArgs(lockName, migrationLockAcquireTimeoutSeconds).
|
||||
|
|
@ -164,6 +166,7 @@ func TestMigrateUpWithLockMigrationErrorNotMaskedByReleaseFailure(t *testing.T)
|
|||
defer conn.Close()
|
||||
|
||||
lockName := MigrationLockName("testdb")
|
||||
expectConvergedFastPathMiss(mock)
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT GET_LOCK(?, ?)")).
|
||||
WithArgs(lockName, migrationLockAcquireTimeoutSeconds).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"locked"}).AddRow(1))
|
||||
|
|
@ -557,6 +560,7 @@ func TestMigrateUpWithLockDirtyGuardStaysFatalWithoutHeal(t *testing.T) {
|
|||
defer conn.Close()
|
||||
|
||||
lockName := MigrationLockName("testdb")
|
||||
expectConvergedFastPathMiss(mock)
|
||||
mock.ExpectQuery(regexp.QuoteMeta("SELECT GET_LOCK(?, ?)")).
|
||||
WithArgs(lockName, migrationLockAcquireTimeoutSeconds).
|
||||
WillReturnRows(sqlmock.NewRows([]string{"locked"}).AddRow(1))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue