diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index cfefd0b..e1760e2 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,11 +36,6 @@ jobs: run: | brew install python sqlite zstd samtools aria2 $(brew --prefix)/bin/pip3 install --break-system-packages pytest pytest-xdist - # test web_vfs using the older system libcurl - brew uninstall --ignore-dependencies curl - curl --version - which curl - otool -L $(which curl) - name: build run: | CXXFLAGS="-I$(brew --prefix)/include -I$(brew --prefix)/opt/sqlite/include" \ @@ -48,6 +43,14 @@ jobs: cmake -DCMAKE_BUILD_TYPE=${{ matrix.BUILD_TYPE }} \ -DCMAKE_PREFIX_PATH=$(brew --prefix)/opt/sqlite \ -B build . + # test web_vfs using the older system libcurl. Uninstall brew's curl only + # after configure: FetchContent clones dependencies with git during + # configure, and the runner's git itself loads brew's libcurl, so removing + # the keg earlier leaves git unable to run at all. + brew uninstall --ignore-dependencies curl + curl --version + which curl + otool -L $(which curl) cmake --build build -j 4 - name: ctest run: | diff --git a/src/SQLiteNestedVFS.h b/src/SQLiteNestedVFS.h index ad5873f..00c42e4 100644 --- a/src/SQLiteNestedVFS.h +++ b/src/SQLiteNestedVFS.h @@ -608,8 +608,12 @@ class InnerDatabaseFile : public SQLiteVFS::File { // 2 above, inner db should always recover to a consistent state. std::unique_ptr txn_; std::unique_ptr insert_page_, update_page_, delete_pages_; + // Retain write-path history across Sync(): connections that never called begin() + // freed no outer pages and need not vacuum at close. + bool wrote_ = false; void begin() { assert(!read_only_); + wrote_ = true; if (!txn_) { txn_.reset(new SQLite::Transaction(*outer_db_)); } @@ -1008,7 +1012,9 @@ class InnerDatabaseFile : public SQLiteVFS::File { SQLiteVFS::File::Close(); // deletes this return rc; } - outer_db_->exec("PRAGMA incremental_vacuum"); + if (wrote_) { + outer_db_->exec("PRAGMA incremental_vacuum"); + } } } catch (SQLite::Exception &exn) { SQLITE_NVFS_LOG(1, exn.what()) diff --git a/test/test.cc b/test/test.cc index b34df8e..b781b5b 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2,8 +2,10 @@ #define SQLITE_CORE #include "SQLiteCpp/SQLiteCpp.h" #include "catch2/catch.hpp" +#include #include #include +#include #include #include "../src/SQLiteNestedVFS.h" @@ -363,6 +365,102 @@ TEST_CASE("cellular_automata") { system(("ls -lh " + testdir).c_str()); } +TEST_CASE("close vacuums the outer database only if the connection wrote") { + string testdir("/tmp/sqlite3_nested_vfs_test_XXXXXX"); + REQUIRE(mkdtemp((char *)testdir.data()) != nullptr); + + setup(); + + // the outer database is the file itself; the inner database lives in its pages table + const string dbfile = testdir + "/vacuum"; + { + SQLite::Database guest(dbfile, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE, 0, "nested"); + guest.exec("CREATE TABLE t (x TEXT)"); + SQLite::Transaction txn(guest); + for (int i = 0; i < 200; i++) { + guest.exec("INSERT INTO t VALUES (zeroblob(1024))"); + } + txn.commit(); + } + + auto outer_freelist_count = [&]() { + SQLite::Database outer(dbfile, SQLite::OPEN_READONLY); + return outer.execAndGet("PRAGMA freelist_count").getInt(); + }; + + // seed the outer freelist without triggering nested close-time vacuuming + { + SQLite::Database outer(dbfile, SQLite::OPEN_READWRITE); + outer.exec("CREATE TABLE scratch (x BLOB)"); + SQLite::Transaction txn(outer); + for (int i = 0; i < 200; i++) { + outer.exec("INSERT INTO scratch VALUES (zeroblob(4000))"); + } + txn.commit(); + outer.exec("DROP TABLE scratch"); + } + const int freelist = outer_freelist_count(); + REQUIRE(freelist > 0); + + // a read-write connection that only reads must leave the existing outer freelist untouched + { + SQLite::Database guest(dbfile, SQLite::OPEN_READWRITE, 0, "nested"); + REQUIRE(guest.execAndGet("SELECT count(*) FROM t").getInt() == 200); + } + REQUIRE(outer_freelist_count() == freelist); + + // a writer's close must still reclaim the outer freelist + { + SQLite::Database guest(dbfile, SQLite::OPEN_READWRITE, 0, "nested"); + guest.exec("INSERT INTO t VALUES ('x')"); + } + REQUIRE(outer_freelist_count() == 0); +} + +TEST_CASE("concurrent opens survive read-write close churn") { + string testdir("/tmp/sqlite3_nested_vfs_test_XXXXXX"); + REQUIRE(mkdtemp((char *)testdir.data()) != nullptr); + + setup(); + + const string dbfile = testdir + "/churn"; + { + SQLite::Database guest(dbfile, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE, 0, "nested"); + guest.exec("CREATE TABLE t (x INTEGER)"); + guest.exec("INSERT INTO t VALUES (1)"); + } + + // a never-wrote close must not take the outer write lock and make concurrent opens fail + const int NTHREADS = 4, NITERS = 1000; + std::atomic n_opened{0}, n_cantopen{0}, n_other_errors{0}; + auto churn = [&]() { + for (int i = 0; i < NITERS; i++) { + try { + SQLite::Database guest(dbfile, SQLite::OPEN_READWRITE, 0, "nested"); + guest.execAndGet("SELECT count(*) FROM t"); + n_opened++; + } catch (SQLite::Exception &exn) { + if ((exn.getErrorCode() & 0xFF) == SQLITE_CANTOPEN) { + n_cantopen++; + } else { + n_other_errors++; + } + } + } + }; + std::vector threads; + for (int i = 0; i < NTHREADS; i++) { + threads.emplace_back(churn); + } + for (auto &thread : threads) { + thread.join(); + } + + REQUIRE(n_other_errors == 0); + REQUIRE(n_cantopen == 0); + REQUIRE(n_opened == NTHREADS * NITERS); +} + /* The following BS is needed for this test executable to use SQLiteCpp even though it's built for use within a loadable extension (SQLITECPP_IN_EXTENSION). The sqlite3Apis definition is copied from