Skip to content

Truly independent web browser

LibHTTP: Remove legacy disk cache migrations

Shannon Booth782755d53652
diff --git a/Libraries/LibHTTP/Cache/CacheIndex.cpp b/Libraries/LibHTTP/Cache/CacheIndex.cpp
index 1b94f96dcf0..3eb584f68d6 100644
--- a/Libraries/LibHTTP/Cache/CacheIndex.cpp
+++ b/Libraries/LibHTTP/Cache/CacheIndex.cpp
@@ -18,11 +18,7 @@
namespace HTTP {
-static constexpr u32 INDEX_SCHEMA_BASELINE_VERSION = 1u;
-static constexpr u32 INDEX_SCHEMA_PARTITIONED_CACHE_KEYS_VERSION = 2u;
-static constexpr u32 INDEX_SCHEMA_VARY_KEY_VERSION = 3u;
-static constexpr u32 INDEX_SCHEMA_RESPONSES_ONLY_VERSION = 4u;
-static constexpr u32 INDEX_SCHEMA_SHA256_KEYS_VERSION = 5u;
+static constexpr u32 INDEX_SCHEMA_BASELINE_VERSION = 5u;
static ReadonlyBytes encode_cache_key_for_database(CacheDigest const& key)
{
@@ -125,7 +121,7 @@ static void log_orphaned_disk_cache_entries(Database::Database& database)
// a migration here without invalidating the cache entries on disk. Entry files embed
// CACHE_VERSION in their headers and are validated when read, so an entry format break must
// append a migration that deletes the index rows referencing the now-unreadable entries.
-static_assert(CACHE_VERSION == 9, "Bumping CACHE_VERSION requires appending a CacheIndex migration that deletes the index rows referencing the old entry format");
+static_assert(CACHE_VERSION == 1, "Bumping CACHE_VERSION requires appending a CacheIndex migration that deletes the index rows referencing the old entry format");
ErrorOr<Database::MigrationOutcome> CacheIndex::migrate_schema(Database::Database& database, Database::MigrationMode mode)
{
@@ -134,62 +130,6 @@ ErrorOr<Database::MigrationOutcome> CacheIndex::migrate_schema(Database::Databas
.version = INDEX_SCHEMA_BASELINE_VERSION,
.sql = R"#(
CREATE TABLE IF NOT EXISTS CacheIndex (
- cache_key INTEGER,
- vary_key INTEGER,
- url TEXT,
- request_headers BLOB,
- response_headers BLOB,
- data_size INTEGER,
- associated_data_size INTEGER,
- request_time INTEGER,
- response_time INTEGER,
- last_access_time INTEGER,
- PRIMARY KEY(cache_key, vary_key)
- );
- )#"sv,
- },
- {
- // Cache keys now include the cache partition. No request can reach an entry stored under an older key, so
- // drop those entries instead of leaving them to take up space until eviction.
- .version = INDEX_SCHEMA_PARTITIONED_CACHE_KEYS_VERSION,
- .sql = "DELETE FROM CacheIndex;"sv,
- .backfill = [](Database::Database& database) -> ErrorOr<void> {
- for_each_cache_entry_file(database, [](LexicalPath const& cache_entry) {
- (void)FileSystem::remove(cache_entry.string(), FileSystem::RecursionMode::Disallowed);
- });
- return {};
- },
- },
- {
- // CACHE_VERSION 8 changed how vary keys are computed.
- .version = INDEX_SCHEMA_VARY_KEY_VERSION,
- .sql = "DELETE FROM CacheIndex;"sv,
- .backfill = [](Database::Database& database) -> ErrorOr<void> {
- for_each_cache_entry_file(database, [](LexicalPath const& cache_entry) {
- (void)FileSystem::remove(cache_entry.string(), FileSystem::RecursionMode::Disallowed);
- });
- return {};
- },
- },
- {
- // The cache now holds responses only. Stored responses stay valid, but the JavaScript bytecode files beside
- // them, and any such file left half-written, are no longer read or accounted for.
- .version = INDEX_SCHEMA_RESPONSES_ONLY_VERSION,
- .sql = "ALTER TABLE CacheIndex DROP COLUMN associated_data_size;"sv,
- .backfill = [](Database::Database& database) -> ErrorOr<void> {
- for_each_cache_entry_file(database, [](LexicalPath const& cache_entry) {
- if (cache_entry.basename().contains('.'))
- (void)FileSystem::remove(cache_entry.string(), FileSystem::RecursionMode::Disallowed);
- });
- return {};
- },
- },
- {
- // CACHE_VERSION 9 changed cache and vary keys to full SHA-256 digests.
- .version = INDEX_SCHEMA_SHA256_KEYS_VERSION,
- .sql = R"#(
- DROP TABLE CacheIndex;
- CREATE TABLE CacheIndex (
cache_key BLOB,
vary_key BLOB,
url TEXT,
@@ -202,12 +142,6 @@ ErrorOr<Database::MigrationOutcome> CacheIndex::migrate_schema(Database::Databas
PRIMARY KEY(cache_key, vary_key)
);
)#"sv,
- .backfill = [](Database::Database& database) -> ErrorOr<void> {
- for_each_cache_entry_file(database, [](LexicalPath const& cache_entry) {
- (void)FileSystem::remove(cache_entry.string(), FileSystem::RecursionMode::Disallowed);
- });
- return {};
- },
},
});
diff --git a/Libraries/LibHTTP/Cache/Version.h b/Libraries/LibHTTP/Cache/Version.h
index bbec50e6164..b4840136293 100644
--- a/Libraries/LibHTTP/Cache/Version.h
+++ b/Libraries/LibHTTP/Cache/Version.h
@@ -12,6 +12,6 @@ namespace HTTP {
// Increment this version when a breaking change is made to the cache entry file format.
// Index-only schema changes are handled by the CacheIndex schema migrations instead.
-static constexpr inline u32 CACHE_VERSION = 9u;
+static constexpr inline u32 CACHE_VERSION = 1u;
}
diff --git a/Tests/LibHTTP/TestCacheIndex.cpp b/Tests/LibHTTP/TestCacheIndex.cpp
index e98f0a303eb..d094155d765 100644
--- a/Tests/LibHTTP/TestCacheIndex.cpp
+++ b/Tests/LibHTTP/TestCacheIndex.cpp
@@ -8,13 +8,10 @@
#include <AK/LexicalPath.h>
#include <AK/QuickSort.h>
#include <LibCore/Directory.h>
-#include <LibCore/File.h>
#include <LibCore/StandardPaths.h>
#include <LibDatabase/Database.h>
-#include <LibFileSystem/FileSystem.h>
#include <LibHTTP/Cache/CacheIndex.h>
#include <LibHTTP/Cache/Utilities.h>
-#include <LibTest/TemporaryDirectory.h>
#include <LibTest/TestCase.h>
struct CacheIndexTestState {
@@ -142,49 +139,6 @@ TEST_CASE(newer_cache_index_schema_reports_database_too_new)
EXPECT_EQ(TRY_OR_FAIL(HTTP::CacheIndex::migrate_schema(*database, Database::MigrationMode::CheckOnly)), Database::MigrationOutcome::DatabaseTooNew);
}
-TEST_CASE(entries_with_previous_vary_keys_are_dropped_on_migration)
-{
- auto database = TRY_OR_FAIL(Database::Database::create_memory_backed());
-
- TRY_OR_FAIL(database->execute_raw("CREATE TABLE SchemaVersions (store TEXT PRIMARY KEY, version INTEGER NOT NULL);"sv));
- TRY_OR_FAIL(database->execute_raw("INSERT INTO SchemaVersions (store, version) VALUES ('CacheIndex', 2);"sv));
- TRY_OR_FAIL(database->execute_raw("CREATE TABLE CacheIndex (cache_key INTEGER, vary_key INTEGER, url TEXT, request_headers BLOB, response_headers BLOB, data_size INTEGER, associated_data_size INTEGER, request_time INTEGER, response_time INTEGER, last_access_time INTEGER, PRIMARY KEY(cache_key, vary_key));"sv));
- TRY_OR_FAIL(database->execute_raw("INSERT INTO CacheIndex VALUES (1, 0, 'https://example.com', x'', x'', 10, 0, 0, 0, 0);"sv));
-
- EXPECT_EQ(TRY_OR_FAIL(HTTP::CacheIndex::migrate_schema(*database)), Database::MigrationOutcome::Success);
-
- auto index = MUST(HTTP::CacheIndex::create(*database, cache_directory()));
- EXPECT(!index.find_entry(key(1), *HTTP::HeaderList::create()).has_value());
- EXPECT_EQ(index.estimate_cache_size_accessed_since(UnixDateTime::earliest()).total, 0u);
-}
-
-TEST_CASE(legacy_response_and_bytecode_files_are_dropped_on_migration)
-{
- Test::TemporaryDirectory const directory { "TestCacheIndex"sv };
- auto database = TRY_OR_FAIL(Database::Database::create(directory.path().string(), "INDEX"sv));
-
- TRY_OR_FAIL(database->execute_raw("CREATE TABLE SchemaVersions (store TEXT PRIMARY KEY, version INTEGER NOT NULL);"sv));
- TRY_OR_FAIL(database->execute_raw("INSERT INTO SchemaVersions (store, version) VALUES ('CacheIndex', 3);"sv));
- TRY_OR_FAIL(database->execute_raw("CREATE TABLE CacheIndex (cache_key INTEGER, vary_key INTEGER, url TEXT, request_headers BLOB, response_headers BLOB, data_size INTEGER, associated_data_size INTEGER, request_time INTEGER, response_time INTEGER, last_access_time INTEGER, PRIMARY KEY(cache_key, vary_key));"sv));
- TRY_OR_FAIL(database->execute_raw("INSERT INTO CacheIndex VALUES (1, 0, 'https://example.com', x'', x'', 10, 5, 0, 0, 0);"sv));
-
- auto entry_file = directory.path().append("0000000000000001"sv);
- auto bytecode_file = directory.path().append("0000000000000001.jsbc"sv);
- auto partial_bytecode_file = directory.path().append("0000000000000001.jsbc.tmp"sv);
- for (auto const& path : { entry_file, bytecode_file, partial_bytecode_file })
- (void)TRY_OR_FAIL(Core::File::open(path.string(), Core::File::OpenMode::Write));
-
- EXPECT_EQ(TRY_OR_FAIL(HTTP::CacheIndex::migrate_schema(*database)), Database::MigrationOutcome::Success);
-
- EXPECT(!FileSystem::exists(entry_file.string()));
- EXPECT(!FileSystem::exists(bytecode_file.string()));
- EXPECT(!FileSystem::exists(partial_bytecode_file.string()));
-
- auto index = MUST(HTTP::CacheIndex::create(*database, directory.path()));
- EXPECT(!index.has_entry(key(1), {}));
- EXPECT_EQ(index.estimate_cache_size_accessed_since(UnixDateTime::earliest()).total, 0u);
-}
-
TEST_CASE(negative_stored_sizes_are_skipped_as_corrupt)
{
auto state = create_cache_index();