|
| 1 | +// Copyright 2026 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + |
| 24 | + driver "github.com/dolthub/dolt/go/libraries/doltcore/dtestutils/sql_server_driver" |
| 25 | +) |
| 26 | + |
| 27 | +// This test asserts the behavior of the fast-fail file-lock optimization for |
| 28 | +// dolt CLI commands which run against a data dir while a `dolt sql-server` is |
| 29 | +// running in that data dir. |
| 30 | +// |
| 31 | +// When opening a directory of databases, we don't want to wait for the |
| 32 | +// lock-acquire timeout once we've failed to get one. |
| 33 | +// |
| 34 | +// This test creates many databases and runs a server in the data dir. It then |
| 35 | +// spawns a CLI command in the data dir root and times it. The command must |
| 36 | +// finish well under the un-optimized cost, which is ~N * 100ms. |
| 37 | + |
| 38 | +const ( |
| 39 | + // perDatabaseLockTimeout mirrors lockFileTimeout in nbs/journal.go |
| 40 | + perDatabaseLockTimeout = 100 * time.Millisecond |
| 41 | + |
| 42 | + // numReadOnlyDatabases is chosen large enough that the un-optimized, |
| 43 | + // serial lock-wait cost is unmistakably larger than the optimized cost |
| 44 | + numReadOnlyDatabases = 32 |
| 45 | + |
| 46 | + // We will try up to this many times to see good behavior. If we have |
| 47 | + // lots of scheduling contention, we can still be slow enough that we |
| 48 | + // think we are waiting on the lock files but we are actually just |
| 49 | + // running slowly. |
| 50 | + maxTrials = 3 |
| 51 | +) |
| 52 | + |
| 53 | +// makeEmptyDatabases creates n freshly initialized, empty databases as |
| 54 | +// subdirectories of the data dir root |rs|. Empty databases still need |
| 55 | +// locking, and so meet our use case. |
| 56 | +func makeEmptyDatabases(t *testing.T, rs driver.RepoStore, n int) { |
| 57 | + for i := 0; i < n; i++ { |
| 58 | + _, err := rs.MakeRepo(fmt.Sprintf("db_%02d", i)) |
| 59 | + require.NoError(t, err) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// timeShowDatabases runs `dolt sql -q "show databases"` from the data dir root |
| 64 | +// |rs| up to maxTrials times, returning the best time seen. If the time is |
| 65 | +// ever < maxAcceptable, it returns that immediately. |
| 66 | +func timeShowDatabases(t *testing.T, rs driver.RepoStore, maxAcceptable time.Duration) time.Duration { |
| 67 | + var best time.Duration |
| 68 | + for trial := 0; trial < maxTrials; trial++ { |
| 69 | + cmd := rs.DoltCmd("sql", "-q", "show databases") |
| 70 | + start := time.Now() |
| 71 | + out, err := cmd.CombinedOutput() |
| 72 | + elapsed := time.Since(start) |
| 73 | + require.NoError(t, err, "show databases failed, output:\n%s", string(out)) |
| 74 | + require.Regexp(t, "db_00", string(out)) |
| 75 | + if trial == 0 || elapsed < best { |
| 76 | + best = elapsed |
| 77 | + } |
| 78 | + if best < maxAcceptable { |
| 79 | + // We take the very first run. Serially waiting for |
| 80 | + // the timeouts would have definitely taken longer |
| 81 | + // than this. |
| 82 | + return best |
| 83 | + } |
| 84 | + } |
| 85 | + return best |
| 86 | +} |
| 87 | + |
| 88 | +// TestReadOnlyDatabaseLoadSkipsLockTimeout asserts that loading many read-only |
| 89 | +// databases behind a running sql-server does not serially pay the file-lock |
| 90 | +// timeout for every database. |
| 91 | +func TestReadOnlyDatabaseLoadSkipsLockTimeout(t *testing.T) { |
| 92 | + // No Parallel because it's just a bit sensitive to wall-clock time. |
| 93 | + u, err := driver.NewDoltUser() |
| 94 | + require.NoError(t, err) |
| 95 | + t.Cleanup(func() { |
| 96 | + u.Cleanup() |
| 97 | + }) |
| 98 | + |
| 99 | + rs, err := u.MakeRepoStore() |
| 100 | + require.NoError(t, err) |
| 101 | + makeEmptyDatabases(t, rs, numReadOnlyDatabases) |
| 102 | + |
| 103 | + // Start a sql-server in the data dir root. It holds the file lock on every |
| 104 | + // database, forcing the CLI below to open them all read-only. The helper |
| 105 | + // blocks until the server is up and serving, so server startup time is not |
| 106 | + // included in the measurement below. |
| 107 | + var ports DynamicResources |
| 108 | + ports.global = &GlobalPorts |
| 109 | + ports.t = t |
| 110 | + RunServerUntilEndOfTest(t, rs, &driver.Server{ |
| 111 | + Args: []string{"--port", `{{get_port "server"}}`}, |
| 112 | + DynamicPort: "server", |
| 113 | + }, &ports) |
| 114 | + |
| 115 | + unoptimizedCost := numReadOnlyDatabases * perDatabaseLockTimeout |
| 116 | + maxAcceptable := unoptimizedCost / 2 |
| 117 | + |
| 118 | + best := timeShowDatabases(t, rs, maxAcceptable) |
| 119 | + |
| 120 | + require.Lessf(t, best, maxAcceptable, |
| 121 | + "loading %d read-only databases behind a running sql-server took %s; "+ |
| 122 | + "without the fast-fail lock optimization this would have taken about "+ |
| 123 | + "%s (%d serial %s file-lock waits)", |
| 124 | + numReadOnlyDatabases, best, unoptimizedCost, numReadOnlyDatabases, perDatabaseLockTimeout) |
| 125 | +} |
0 commit comments