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

Commit

Permalink
rgw/sfs: Temporary workaround for using sqlite_modern_cpp and sqlite_orm
Browse files Browse the repository at this point in the history
This adds a temporary workaround for being able to use sqlite_modern_cpp
and sqlite_orm together.

It adds a new pool of connections of std::shared_ptr<sqlite> because the
new database objects from sqlite_modern_cpp has a constructor that gets
that in its constructor and its the only thing needed.

This should be reworked once the code if fully migrated to
sqlite_modern_cpp only.

Signed-off-by: Xavi Garcia <[email protected]>
  • Loading branch information
0xavi0 committed Nov 15, 2023
1 parent 0e21b59 commit 1e15f9c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/rgw/driver/sfs/sqlite/dbconn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <sqlite3.h>

#include <filesystem>
#include <memory>
#include <system_error>

#include "common/dout.h"
Expand Down Expand Up @@ -131,6 +132,14 @@ DBConn::DBConn(CephContext* _cct)
// storage->on_open() called from get_storage(), which has the exclusive
// mutex.
sqlite_conns.emplace_back(db);
std::shared_ptr<sqlite3> db_connection =
std::shared_ptr<sqlite3>(db, [=](sqlite3*) {
// doing nothing for now...
// this is just a workaround to reuse the connection opened from
// sqlite_orm, the real owner of the connection is still sqlite_orm.
// This won't be needed after code is fully ported to new sqlite lib
});
storage_pool_new.emplace(std::this_thread::get_id(), db_connection);

sqlite3_extended_result_codes(db, 1);
sqlite3_busy_timeout(db, 10000);
Expand Down Expand Up @@ -187,6 +196,24 @@ StorageRef DBConn::get_storage() {
}
}

dbapi::sqlite::database DBConn::get() {
std::thread::id this_thread = std::this_thread::get_id();
try {
// using the same mutex as meanwhile code is being ported connections might
// be created for sqlite_orm code or sqlite_modern_cpp
std::shared_lock lock(storage_pool_mutex);
auto connection = storage_pool_new.at(this_thread);
return dbapi::sqlite::database(connection);
} catch (const std::out_of_range& ex) {
// using the same mutex as meanwhile code is being ported connections might
// be created for sqlite_orm code or sqlite_modern_cpp
std::unique_lock lock(storage_pool_mutex);
dbapi::sqlite::database db(getDBPath(cct));
storage_pool_new.emplace(this_thread, db.connection());
return db;
}
}

void DBConn::check_metadata_is_compatible() const {
bool sync_error = false;
std::string result_message;
Expand Down
8 changes: 5 additions & 3 deletions src/rgw/driver/sfs/sqlite/dbconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,15 @@ inline auto _make_storage(const std::string& path) {
using Storage = decltype(_make_storage(""));
using StorageRef = Storage*;

// TODO revisit this when code is fully ported to sqlite modern cpp
using ConnectionNewLib = std::shared_ptr<sqlite3>;

// TODO(https://github.com/aquarist-labs/s3gw/issues/788): Make
// dbapi::sqlite::database the primary interface for sqlite3.
class DBConn {
private:
std::unordered_map<std::thread::id, Storage> storage_pool;
std::unordered_map<std::thread::id, ConnectionNewLib> storage_pool_new;
std::vector<sqlite3*> sqlite_conns;
const std::thread::id main_thread;
mutable std::shared_mutex storage_pool_mutex;
Expand All @@ -285,9 +289,7 @@ class DBConn {
return sqlite_conns;
}

dbapi::sqlite::database get() {
return dbapi::sqlite::database(get_storage()->filename());
}
dbapi::sqlite::database get();

static std::string getDBPath(CephContext* cct) {
auto rgw_sfs_path = cct->_conf.get_val<std::string>("rgw_sfs_data_path");
Expand Down

0 comments on commit 1e15f9c

Please sign in to comment.