Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
rgw/sfs: Rename legacy database filename to new if exists on startup
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Lauhoff <marcel.lauhoff@suse.com>
  • Loading branch information
Marcel Lauhoff committed Nov 15, 2023
1 parent 69c0648 commit 834fe55
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/rgw/driver/sfs/sqlite/dbconn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
#include "dbconn.h"

#include <ceph_assert.h>
#include <sqlite3.h>

#include <filesystem>
Expand Down Expand Up @@ -121,6 +122,7 @@ DBConn::DBConn(CephContext* _cct)
storage_pool_mutex(),
cct(_cct),
profile_enabled(_cct->_conf.get_val<bool>("rgw_sfs_sqlite_profile")) {
maybe_rename_database_file();
sqlite3_config(SQLITE_CONFIG_LOG, &sqlite_error_callback, cct);
// get_storage() relies on there already being an entry in the pool
// for the main thread (i.e. the thread that created the DBConn).
Expand Down Expand Up @@ -414,4 +416,65 @@ void DBConn::maybe_upgrade_metadata() {
}
}

void DBConn::maybe_rename_database_file() const {
if (!std::filesystem::exists(getLegacyDBPath(cct))) {
return;
}
if (std::filesystem::exists(getDBPath(cct))) {
return;
}

lsubdout(cct, rgw_sfs, SFS_LOG_STARTUP)
<< fmt::format(
"Migrating legacy database file {} -> {}", getLegacyDBPath(cct),
getDBPath(cct)
)
<< dendl;

dbapi::sqlite::database src_db(getLegacyDBPath(cct));
dbapi::sqlite::database dst_db(getDBPath(cct));
auto state =
std::unique_ptr<sqlite3_backup, decltype(&sqlite3_backup_finish)>(
sqlite3_backup_init(
dst_db.connection().get(), "main", src_db.connection().get(),
"main"
),
sqlite3_backup_finish
);

if (!state) {
lsubdout(cct, rgw_sfs, SFS_LOG_ERROR)
<< fmt::format(
"Error opening legacy database file {} {}. Please migrate "
"s3gw.db to sfs.db manually",
getLegacyDBPath(cct), sqlite3_errmsg(dst_db.connection().get())
)
<< dendl;
ceph_abort_msg("sfs database file migration failed");
}

int rc = sqlite3_backup_step(state.get(), -1);
if (rc != SQLITE_DONE) {
lsubdout(cct, rgw_sfs, SFS_LOG_ERROR)
<< fmt::format(
"Error migrating legacy database file {} {}. Please migrate "
"s3gw.db to sfs.db manually",
getLegacyDBPath(cct), sqlite3_errmsg(dst_db.connection().get())
)
<< dendl;
ceph_abort_msg("sfs database file migration failed");
}

std::error_code ec; // ignore errors
fs::remove(getLegacyDBPath(cct), ec);
fs::remove(getLegacyDBPath(cct) + "-wal", ec);
fs::remove(getLegacyDBPath(cct) + "-shm", ec);

lsubdout(cct, rgw_sfs, SFS_LOG_STARTUP)
<< fmt::format(
"Done migrating legacy database. Continuing startup with {}",
getDBPath(cct)
)
<< dendl;
}
} // namespace rgw::sal::sfs::sqlite
9 changes: 9 additions & 0 deletions src/rgw/driver/sfs/sqlite/dbconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ constexpr int SFS_METADATA_VERSION = 4;
/// minimum required version to upgrade db.
constexpr int SFS_METADATA_MIN_VERSION = 4;

constexpr std::string_view LEGACY_DB_FILENAME = "s3gw.db";
constexpr std::string_view DB_FILENAME = "sfs.db";
constexpr std::string_view DB_WAL_FILENAME = "sfs.db-wal";

Expand Down Expand Up @@ -297,8 +298,16 @@ class DBConn {
return db_path.string();
}

static std::string getLegacyDBPath(CephContext* cct) {
auto rgw_sfs_path = cct->_conf.get_val<std::string>("rgw_sfs_data_path");
auto db_path =
std::filesystem::path(rgw_sfs_path) / std::string(LEGACY_DB_FILENAME);
return db_path.string();
}

void check_metadata_is_compatible() const;
void maybe_upgrade_metadata();
void maybe_rename_database_file() const;
};

using DBConnRef = std::shared_ptr<DBConn>;
Expand Down

0 comments on commit 834fe55

Please sign in to comment.