Skip to content

Commit

Permalink
Refactor virtual store init
Browse files Browse the repository at this point in the history
Signed-off-by: Jin Hai <[email protected]>
  • Loading branch information
JinHai-CN committed Oct 7, 2024
1 parent 0315a31 commit 84b54a0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 91 deletions.
77 changes: 12 additions & 65 deletions src/storage/io/virtual_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,70 +116,6 @@ String ToString(StorageType storage_type) {
}
}

Status RemoteStore::Init(StorageType storage_type, Map<String, String> &config) {
// Init remote filesystem and local disk cache
storage_type_ = storage_type;
switch (storage_type) {
case StorageType::kMinio: {
auto iter = config.find("url");
if (iter == config.end()) {
return Status::InvalidConfig("Missing MINIO 'URL'");
}
String url = iter->second;

iter = config.find("access_key");
if (iter == config.end()) {
return Status::InvalidConfig("Missing MINIO 'access_key'");
}
String access_key = iter->second;

iter = config.find("secret_key");
if (iter == config.end()) {
return Status::InvalidConfig("Missing MINIO 'secret_key'");
}
String secret_key = iter->second;

iter = config.find("enable_https");
if (iter == config.end()) {
return Status::InvalidConfig("Missing MINIO 'enable_https'");
}
String enable_https_str = iter->second;
bool enable_https{false};
if (enable_https_str == "true") {
enable_https = true;
} else if (enable_https_str == "false") {
enable_https = false;
} else {
return Status::InvalidConfig(fmt::format("Invalid MINIO 'enable_https' value: {}", enable_https_str));
}

minio_base_url_ = MakeUnique<minio::s3::BaseUrl>(url, enable_https);
minio_provider_ = MakeUnique<minio::creds::StaticProvider>(access_key, secret_key);
minio_client_ = MakeUnique<minio::s3::Client>(*minio_base_url_, minio_provider_.get());
break;
}
default: {
return Status::NotSupport(fmt::format("{} isn't support in virtual filesystem", ToString(storage_type)));
}
}
return Status::OK();
}

Status RemoteStore::UnInit() {
switch (storage_type_) {
case StorageType::kMinio: {
minio_base_url_.reset();
minio_provider_.reset();
minio_client_.reset();
break;
}
default: {
return Status::NotSupport(fmt::format("{} isn't support in virtual filesystem", ToString(storage_type_)));
}
}
return Status::OK();
}

Tuple<UniquePtr<LocalFileHandle>, Status> VirtualStore::Open(const String &path, FileAccessMode access_mode) {
i32 fd = -1;
switch (access_mode) {
Expand Down Expand Up @@ -485,7 +421,7 @@ i32 VirtualStore::MunmapFile(const String &file_path) {
}

// Remote storage
StorageType VirtualStore::storage_type_ = StorageType::kMinio;
StorageType VirtualStore::storage_type_ = StorageType::kInvalid;
String VirtualStore::bucket_ = "infinity_bucket";
UniquePtr<S3Client> VirtualStore::s3_client_ = nullptr;

Expand All @@ -497,6 +433,7 @@ Status VirtualStore::InitRemoteStore(StorageType storage_type,
const String &bucket) {
switch (storage_type) {
case StorageType::kMinio: {
storage_type_ = StorageType::kMinio;
s3_client_ = MakeUnique<S3ClientMinio>(URL, HTTPS, access_key, secret_key);
}
default: {
Expand All @@ -507,6 +444,16 @@ Status VirtualStore::InitRemoteStore(StorageType storage_type,
return Status::OK();
}

Status VirtualStore::UnInitRemoteStore() {
VirtualStore::storage_type_ = StorageType::kInvalid;
s3_client_.reset();
return Status::OK();
}

bool VirtualStore::IsInit() {
return s3_client_.get() != nullptr;
}

Status VirtualStore::DownloadObject(const String &file_path, const String &object_name) {
if (VirtualStore::storage_type_ == StorageType::kLocal) {
return Status::OK();
Expand Down
18 changes: 3 additions & 15 deletions src/storage/io/virtual_store.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,6 @@ export struct MmapInfo {
SizeT rc_{};
};

// Only one instance;
export class RemoteStore {
public:
Status Init(StorageType storage_type, Map<String, String> &config);
Status UnInit();

private:
// Using by minio
StorageType storage_type_{StorageType::kLocal};

UniquePtr<minio::s3::BaseUrl> minio_base_url_{};
UniquePtr<minio::creds::StaticProvider> minio_provider_{};
UniquePtr<minio::s3::Client> minio_client_{};
};

export class VirtualStore {
public:
static Tuple<UniquePtr<LocalFileHandle>, Status> Open(const String &path, FileAccessMode access_mode);
Expand Down Expand Up @@ -92,6 +77,9 @@ public:
const String &secret_key = "minioadmin",
const String &bucket = "infinity_bucket");

static Status UnInitRemoteStore();

static bool IsInit();
static Status DownloadObject(const String &file_dir, const String& object_name);
static Status UploadObject(const String &file_dir, const String& object_name);
static Status RemoveObject(const String &object_name);
Expand Down
15 changes: 7 additions & 8 deletions src/storage/storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ void Storage::SetStorageMode(StorageMode target_mode) {
break;
}
case StorageType::kMinio: {
if (remote_store_ != nullptr) {
if (VirtualStore::IsInit()) {
UnrecoverableError("remote storage system was initialized before.");
}
Map<String, String> configs;
configs.emplace("url", config_ptr_->ObjectStorageUrl());
remote_store_->Init(StorageType::kMinio, configs);
Status status = VirtualStore::InitRemoteStore(StorageType::kMinio, config_ptr_->ObjectStorageUrl());
if (!status.ok()) {
UnrecoverableError(status.message());
}
break;
}
default: {
Expand Down Expand Up @@ -255,8 +256,7 @@ void Storage::SetStorageMode(StorageMode target_mode) {
break;
}
case StorageType::kMinio: {
remote_store_->UnInit();
remote_store_.reset();
VirtualStore::UnInitRemoteStore();
break;
}
default: {
Expand Down Expand Up @@ -340,8 +340,7 @@ void Storage::SetStorageMode(StorageMode target_mode) {
break;
}
case StorageType::kMinio: {
remote_store_->UnInit();
remote_store_.reset();
VirtualStore::UnInitRemoteStore();
break;
}
default: {
Expand Down
3 changes: 0 additions & 3 deletions src/storage/storage.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public:

[[nodiscard]] inline PersistenceManager *persistence_manager() noexcept { return persistence_manager_.get(); }

[[nodiscard]] inline RemoteStore *remote_store() noexcept { return remote_store_.get(); }

[[nodiscard]] inline BGTaskProcessor *bg_processor() const noexcept { return bg_processor_.get(); }

[[nodiscard]] inline PeriodicTriggerThread *periodic_trigger_thread() const noexcept { return periodic_trigger_thread_.get(); }
Expand All @@ -77,7 +75,6 @@ private:
UniquePtr<TxnManager> txn_mgr_{};
UniquePtr<WalManager> wal_mgr_{};
UniquePtr<PersistenceManager> persistence_manager_{};
UniquePtr<RemoteStore> remote_store_{};
UniquePtr<BGTaskProcessor> bg_processor_{};
UniquePtr<CompactionProcessor> compact_processor_{};
UniquePtr<PeriodicTriggerThread> periodic_trigger_thread_{};
Expand Down

0 comments on commit 84b54a0

Please sign in to comment.