Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(db/snapshot) Add root path for snapshot store #2945

Merged
merged 2 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions core/src/db/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
//#include "storage/s3/S3ClientWrapper.h"
#include "utils/CommonUtil.h"
#include "utils/Log.h"
#include "utils/StringHelpFunctions.h"

#include <map>

Expand All @@ -50,15 +49,6 @@ ConstructParentFolder(const std::string& db_path, const meta::SegmentSchema& tab

} // namespace

std::string
ConstructCollectionRootPath(const std::string& root_path) {
if (StringHelpFunctions::EndWithSlash(root_path)) {
return root_path + "db" + TABLES_FOLDER;
}

return root_path + "/db" + TABLES_FOLDER;
}

int64_t
GetMicroSecTimeStamp() {
auto now = std::chrono::system_clock::now();
Expand Down
3 changes: 0 additions & 3 deletions core/src/db/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ namespace utils {
int64_t
GetMicroSecTimeStamp();

std::string
ConstructCollectionRootPath(const std::string& root_path);

Status
CreateCollectionPath(const DBMetaOptions& options, const std::string& collection_id);
Status
Expand Down
6 changes: 3 additions & 3 deletions core/src/db/snapshot/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ResourceGCEvent : public MetaEvent {
public:
using Ptr = std::shared_ptr<ResourceGCEvent>;

explicit ResourceGCEvent(const std::string& root_path, class ResourceT::Ptr res) : dir_root_(root_path), res_(res) {
explicit ResourceGCEvent(class ResourceT::Ptr res) : res_(res) {
}

~ResourceGCEvent() = default;
Expand All @@ -47,7 +47,8 @@ class ResourceGCEvent : public MetaEvent {
STATUS_CHECK((*sd_op)(store));

/* TODO: physically clean resource */
std::string res_path = GetResPath<ResourceT>(dir_root_, res_);
auto res_prefix = store->GetRootPath();
std::string res_path = GetResPath<ResourceT>(res_prefix, res_);
/* if (!boost::filesystem::exists(res_path)) { */
/* return Status::OK(); */
/* } */
Expand All @@ -72,7 +73,6 @@ class ResourceGCEvent : public MetaEvent {

private:
class ResourceT::Ptr res_;
std::string dir_root_;
};

} // namespace snapshot
Expand Down
10 changes: 1 addition & 9 deletions core/src/db/snapshot/Operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#include <chrono>
#include <sstream>

#include "config/Config.h"
#include "db/Utils.h"
#include "db/snapshot/Event.h"
#include "db/snapshot/EventExecutor.h"
#include "db/snapshot/OperationExecutor.h"
Expand Down Expand Up @@ -249,15 +247,9 @@ Operations::PostExecute(StorePtr store) {
template <typename ResourceT>
void
ApplyRollBack(std::set<std::shared_ptr<ResourceContext<ResourceT>>>& step_context_set) {
auto& config = server::Config::GetInstance();
std::string path;
config.GetStorageConfigPath(path);
auto root_path = utils::ConstructCollectionRootPath(path);

for (auto& step_context : step_context_set) {
auto res = step_context->Resource();

auto evt_ptr = std::make_shared<ResourceGCEvent<ResourceT>>(root_path, res);
auto evt_ptr = std::make_shared<ResourceGCEvent<ResourceT>>(res);
EventExecutor::GetInstance().Submit(evt_ptr);
std::cout << "Rollback " << typeid(ResourceT).name() << ": " << res->GetID() << std::endl;
}
Expand Down
21 changes: 4 additions & 17 deletions core/src/db/snapshot/ResourceHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include "db/snapshot/Resources.h"
#include "utils/Status.h"
#include "utils/StringHelpFunctions.h"

namespace milvus::engine::snapshot {

Expand All @@ -35,10 +34,7 @@ template <>
inline std::string
GetResPath<Collection>(const std::string& root, const Collection::Ptr& res_ptr) {
std::stringstream ss;
ss << root;
if (!StringHelpFunctions::EndWithSlash(root)) {
ss << "/";
}
ss << root << "/";
ss << COLLECTION_PREFIX << res_ptr->GetID();

return ss.str();
Expand All @@ -48,10 +44,7 @@ template <>
inline std::string
GetResPath<Partition>(const std::string& root, const Partition::Ptr& res_ptr) {
std::stringstream ss;
ss << root;
if (!StringHelpFunctions::EndWithSlash(root)) {
ss << "/";
}
ss << root << "/";
ss << COLLECTION_PREFIX << res_ptr->GetCollectionId() << "/";
ss << PARTITION_PREFIX << res_ptr->GetID();

Expand All @@ -62,10 +55,7 @@ template <>
inline std::string
GetResPath<Segment>(const std::string& root, const Segment::Ptr& res_ptr) {
std::stringstream ss;
ss << root;
if (!StringHelpFunctions::EndWithSlash(root)) {
ss << "/";
}
ss << root << "/";
ss << COLLECTION_PREFIX << res_ptr->GetCollectionId() << "/";
ss << PARTITION_PREFIX << res_ptr->GetPartitionId() << "/";
ss << SEGMENT_PREFIX << res_ptr->GetID();
Expand All @@ -77,10 +67,7 @@ template <>
inline std::string
GetResPath<SegmentFile>(const std::string& root, const SegmentFile::Ptr& res_ptr) {
std::stringstream ss;
ss << root;
if (!StringHelpFunctions::EndWithSlash(root)) {
ss << "/";
}
ss << root << "/";
ss << COLLECTION_PREFIX << res_ptr->GetCollectionId() << "/";
ss << PARTITION_PREFIX << res_ptr->GetPartitionId() << "/";
ss << SEGMENT_PREFIX << res_ptr->GetSegmentId() << "/";
Expand Down
8 changes: 1 addition & 7 deletions core/src/db/snapshot/ResourceHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <string>
#include <thread>

#include "config/Config.h"
#include "db/snapshot/Event.h"
#include "db/snapshot/EventExecutor.h"
#include "db/snapshot/Operations.h"
Expand Down Expand Up @@ -139,14 +138,9 @@ class ResourceHolder {

virtual void
OnNoRefCallBack(ResourcePtr resource) {
auto& config = server::Config::GetInstance();
std::string path;
config.GetStorageConfigPath(path);
auto root_path = utils::ConstructCollectionRootPath(path);

resource->Deactivate();
Release(resource->GetID());
auto evt_ptr = std::make_shared<ResourceGCEvent<ResourceT>>(root_path, resource);
auto evt_ptr = std::make_shared<ResourceGCEvent<ResourceT>>(resource);
EventExecutor::GetInstance().Submit(evt_ptr);
}

Expand Down
3 changes: 2 additions & 1 deletion core/src/db/snapshot/Snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <tuple>
#include <utility>
#include <vector>

#include "db/snapshot/Store.h"
#include "db/snapshot/Utils.h"
#include "db/snapshot/WrappedTypes.h"
Expand All @@ -44,7 +45,7 @@ using ScopedResourcesT =
class Snapshot : public ReferenceProxy {
public:
using Ptr = std::shared_ptr<Snapshot>;
explicit Snapshot(StorePtr, ID_TYPE);
Snapshot(StorePtr, ID_TYPE);

ID_TYPE
GetID() const {
Expand Down
15 changes: 11 additions & 4 deletions core/src/db/snapshot/Store.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ class Store : public std::enable_shared_from_this<Store> {
public:
using Ptr = typename std::shared_ptr<Store>;

explicit Store(meta::MetaAdapterPtr adapter) : adapter_(adapter) {
explicit Store(meta::MetaAdapterPtr adapter, const std::string& root_path)
: adapter_(adapter), root_path_(root_path) {
}

static Store::Ptr
Build(const std::string& uri) {
Build(const std::string& uri, const std::string& root_path) {
utils::MetaUriInfo uri_info;
LOG_ENGINE_DEBUG_ << "MetaUri: " << uri << std::endl;
auto status = utils::ParseMetaUri(uri, uri_info);
Expand All @@ -69,18 +70,23 @@ class Store : public std::enable_shared_from_this<Store> {
options.backend_uri_ = uri;
auto engine = std::make_shared<meta::MySqlEngine>(options);
auto adapter = std::make_shared<meta::MetaAdapter>(engine);
return std::make_shared<Store>(adapter);
return std::make_shared<Store>(adapter, root_path);
} else if (strcasecmp(uri_info.dialect_.c_str(), "mock") == 0) {
LOG_ENGINE_INFO_ << "Using Mock. Should only be used in test environment";
auto engine = std::make_shared<meta::MockMetaEngine>();
auto adapter = std::make_shared<meta::MetaAdapter>(engine);
return std::make_shared<Store>(adapter);
return std::make_shared<Store>(adapter, root_path);
} else {
LOG_ENGINE_ERROR_ << "Invalid dialect in URI: dialect = " << uri_info.dialect_;
throw InvalidArgumentException("URI dialect is not mysql / sqlite / mock");
}
}

std::string
GetRootPath() const {
return root_path_ + "/tables";
}

template <typename OpT>
Status
ApplyOperation(OpT& op) {
Expand Down Expand Up @@ -364,6 +370,7 @@ class Store : public std::enable_shared_from_this<Store> {
}

meta::MetaAdapterPtr adapter_;
std::string root_path_;
};

using StorePtr = Store::Ptr;
Expand Down
9 changes: 0 additions & 9 deletions core/src/utils/StringHelpFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,4 @@ StringHelpFunctions::ConvertToBoolean(const std::string& str, bool& value) {
return Status::OK();
}

bool
StringHelpFunctions::EndWithSlash(const std::string& path) {
if (path.size() == 0) {
return false;
}

return *path.rbegin() == '/';
}

} // namespace milvus
3 changes: 0 additions & 3 deletions core/src/utils/StringHelpFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ class StringHelpFunctions {
// "false", "off", "no", "0", "" ==> false
static Status
ConvertToBoolean(const std::string& str, bool& value);

static bool
EndWithSlash(const std::string& path);
};

} // namespace milvus
5 changes: 0 additions & 5 deletions core/unittest/server/test_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,11 +788,6 @@ TEST(ValidationUtilTest, VALIDATE_PATH_TEST) {
ASSERT_FALSE(milvus::server::ValidateStoragePath("/tmp//milvus").ok());
}

TEST(UtilTest, ENDWITHSLASH_TEST) {
ASSERT_TRUE(milvus::StringHelpFunctions::EndWithSlash("/tmp/milvus/"));
ASSERT_FALSE(milvus::StringHelpFunctions::EndWithSlash("/tmp/milvus"));
}

TEST(UtilTest, ROLLOUTHANDLER_TEST) {
std::string dir1 = "/tmp/milvus_test";
std::string dir2 = "/tmp/milvus_test/log_test";
Expand Down
2 changes: 1 addition & 1 deletion core/unittest/ssdb/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void
BaseTest::SnapshotStart(bool mock_store) {
/* auto uri = "mysql://root:12345678@127.0.0.1:3307/milvus"; */
auto uri = "mock://:@:/";
auto store = Store::Build(uri);
auto store = Store::Build(uri, "/tmp/milvus_ss/db");

milvus::engine::snapshot::OperationExecutor::Init(store);
milvus::engine::snapshot::OperationExecutor::GetInstance().Start();
Expand Down