Utils.cpp 7.5 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

S
starlord 已提交
18
#include "db/Utils.h"
S
starlord 已提交
19
#include "utils/CommonUtil.h"
S
starlord 已提交
20
#include "utils/Log.h"
X
Xu Peng 已提交
21

S
starlord 已提交
22
#include <boost/filesystem.hpp>
X
Xu Peng 已提交
23
#include <chrono>
S
starlord 已提交
24
#include <mutex>
25
#include <regex>
S
starlord 已提交
26
#include <vector>
X
Xu Peng 已提交
27

J
jinhai 已提交
28
namespace milvus {
X
Xu Peng 已提交
29 30 31
namespace engine {
namespace utils {

S
starlord 已提交
32 33
namespace {

S
starlord 已提交
34
const char* TABLES_FOLDER = "/tables/";
S
starlord 已提交
35

J
jinhai 已提交
36 37
uint64_t index_file_counter = 0;
std::mutex index_file_counter_mutex;
38

S
starlord 已提交
39
std::string
S
starlord 已提交
40
ConstructParentFolder(const std::string& db_path, const meta::TableFileSchema& table_file) {
S
starlord 已提交
41 42 43 44 45
    std::string table_path = db_path + TABLES_FOLDER + table_file.table_id_;
    std::string partition_path = table_path + "/" + std::to_string(table_file.date_);
    return partition_path;
}

S
starlord 已提交
46
std::string
S
starlord 已提交
47
GetTableFileParentFolder(const DBMetaOptions& options, const meta::TableFileSchema& table_file) {
S
starlord 已提交
48 49
    uint64_t path_count = options.slave_paths_.size() + 1;
    std::string target_path = options.path_;
50 51
    uint64_t index = 0;

S
starlord 已提交
52
    if (meta::TableFileSchema::NEW_INDEX == table_file.file_type_) {
53 54 55 56 57 58 59 60 61 62 63 64 65
        // index file is large file and to be persisted permanently
        // we need to distribute index files to each db_path averagely
        // round robin according to a file counter
        std::lock_guard<std::mutex> lock(index_file_counter_mutex);
        index = index_file_counter % path_count;
        index_file_counter++;
    } else {
        // for other type files, they could be merged or deleted
        // so we round robin according to their file id
        index = table_file.id_ % path_count;
    }

    if (index > 0) {
S
starlord 已提交
66
        target_path = options.slave_paths_[index - 1];
S
starlord 已提交
67 68 69 70 71
    }

    return ConstructParentFolder(target_path, table_file);
}

S
starlord 已提交
72
}  // namespace
S
starlord 已提交
73

S
starlord 已提交
74 75
int64_t
GetMicroSecTimeStamp() {
X
Xu Peng 已提交
76
    auto now = std::chrono::system_clock::now();
S
starlord 已提交
77
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
X
Xu Peng 已提交
78 79 80 81

    return micros;
}

S
starlord 已提交
82
Status
S
starlord 已提交
83
CreateTablePath(const DBMetaOptions& options, const std::string& table_id) {
S
starlord 已提交
84
    std::string db_path = options.path_;
S
starlord 已提交
85 86
    std::string table_path = db_path + TABLES_FOLDER + table_id;
    auto status = server::CommonUtil::CreateDirectory(table_path);
S
starlord 已提交
87 88 89
    if (!status.ok()) {
        ENGINE_LOG_ERROR << status.message();
        return status;
S
starlord 已提交
90 91
    }

S
starlord 已提交
92
    for (auto& path : options.slave_paths_) {
S
starlord 已提交
93 94
        table_path = path + TABLES_FOLDER + table_id;
        status = server::CommonUtil::CreateDirectory(table_path);
S
starlord 已提交
95 96 97
        if (!status.ok()) {
            ENGINE_LOG_ERROR << status.message();
            return status;
S
starlord 已提交
98 99 100 101 102 103
        }
    }

    return Status::OK();
}

S
starlord 已提交
104
Status
S
starlord 已提交
105
DeleteTablePath(const DBMetaOptions& options, const std::string& table_id, bool force) {
S
starlord 已提交
106 107
    std::vector<std::string> paths = options.slave_paths_;
    paths.push_back(options.path_);
S
starlord 已提交
108

S
starlord 已提交
109
    for (auto& path : paths) {
S
starlord 已提交
110
        std::string table_path = path + TABLES_FOLDER + table_id;
S
starlord 已提交
111
        if (force) {
S
starlord 已提交
112 113
            boost::filesystem::remove_all(table_path);
            ENGINE_LOG_DEBUG << "Remove table folder: " << table_path;
S
starlord 已提交
114
        } else if (boost::filesystem::exists(table_path) && boost::filesystem::is_empty(table_path)) {
S
starlord 已提交
115 116 117
            boost::filesystem::remove_all(table_path);
            ENGINE_LOG_DEBUG << "Remove table folder: " << table_path;
        }
S
starlord 已提交
118 119 120 121 122
    }

    return Status::OK();
}

S
starlord 已提交
123
Status
S
starlord 已提交
124
CreateTableFilePath(const DBMetaOptions& options, meta::TableFileSchema& table_file) {
S
starlord 已提交
125 126 127
    std::string parent_path = GetTableFileParentFolder(options, table_file);

    auto status = server::CommonUtil::CreateDirectory(parent_path);
S
starlord 已提交
128 129 130
    if (!status.ok()) {
        ENGINE_LOG_ERROR << status.message();
        return status;
S
starlord 已提交
131 132 133 134 135 136 137
    }

    table_file.location_ = parent_path + "/" + table_file.file_id_;

    return Status::OK();
}

S
starlord 已提交
138
Status
S
starlord 已提交
139
GetTableFilePath(const DBMetaOptions& options, meta::TableFileSchema& table_file) {
S
starlord 已提交
140
    std::string parent_path = ConstructParentFolder(options.path_, table_file);
S
starlord 已提交
141
    std::string file_path = parent_path + "/" + table_file.file_id_;
S
starlord 已提交
142
    if (boost::filesystem::exists(file_path)) {
S
starlord 已提交
143 144
        table_file.location_ = file_path;
        return Status::OK();
S
starlord 已提交
145 146 147 148 149 150 151 152
    }

    for (auto& path : options.slave_paths_) {
        parent_path = ConstructParentFolder(path, table_file);
        file_path = parent_path + "/" + table_file.file_id_;
        if (boost::filesystem::exists(file_path)) {
            table_file.location_ = file_path;
            return Status::OK();
S
starlord 已提交
153 154 155
        }
    }

S
starlord 已提交
156
    std::string msg = "Table file doesn't exist: " + file_path;
S
starlord 已提交
157
    ENGINE_LOG_ERROR << msg << " in path: " << options.path_ << " for table: " << table_file.table_id_;
S
starlord 已提交
158

S
starlord 已提交
159
    return Status(DB_ERROR, msg);
S
starlord 已提交
160 161
}

S
starlord 已提交
162
Status
S
starlord 已提交
163
DeleteTableFilePath(const DBMetaOptions& options, meta::TableFileSchema& table_file) {
S
starlord 已提交
164 165 166 167 168
    utils::GetTableFilePath(options, table_file);
    boost::filesystem::remove(table_file.location_);
    return Status::OK();
}

S
starlord 已提交
169
bool
S
starlord 已提交
170 171 172
IsSameIndex(const TableIndex& index1, const TableIndex& index2) {
    return index1.engine_type_ == index2.engine_type_ && index1.nlist_ == index2.nlist_ &&
           index1.metric_type_ == index2.metric_type_;
173 174
}

S
starlord 已提交
175
meta::DateT
S
starlord 已提交
176
GetDate(const std::time_t& t, int day_delta) {
177 178 179 180 181 182
    struct tm ltm;
    localtime_r(&t, &ltm);
    if (day_delta > 0) {
        do {
            ++ltm.tm_mday;
            --day_delta;
S
starlord 已提交
183
        } while (day_delta > 0);
184 185 186 187 188
        mktime(&ltm);
    } else if (day_delta < 0) {
        do {
            --ltm.tm_mday;
            ++day_delta;
S
starlord 已提交
189
        } while (day_delta < 0);
190 191 192 193
        mktime(&ltm);
    } else {
        ltm.tm_mday;
    }
S
starlord 已提交
194
    return ltm.tm_year * 10000 + ltm.tm_mon * 100 + ltm.tm_mday;
195 196
}

S
starlord 已提交
197 198
meta::DateT
GetDateWithDelta(int day_delta) {
199 200 201
    return GetDate(std::time(nullptr), day_delta);
}

S
starlord 已提交
202 203
meta::DateT
GetDate() {
204 205 206
    return GetDate(std::time(nullptr), 0);
}

207
// URI format: dialect://username:password@host:port/database
S
starlord 已提交
208
Status
S
starlord 已提交
209
ParseMetaUri(const std::string& uri, MetaUriInfo& info) {
210 211 212 213 214 215
    std::string dialect_regex = "(.*)";
    std::string username_tegex = "(.*)";
    std::string password_regex = "(.*)";
    std::string host_regex = "(.*)";
    std::string port_regex = "(.*)";
    std::string db_name_regex = "(.*)";
S
starlord 已提交
216 217
    std::string uri_regex_str = dialect_regex + "\\:\\/\\/" + username_tegex + "\\:" + password_regex + "\\@" +
                                host_regex + "\\:" + port_regex + "\\/" + db_name_regex;
218 219 220 221 222 223 224 225 226 227 228 229

    std::regex uri_regex(uri_regex_str);
    std::smatch pieces_match;

    if (std::regex_match(uri, pieces_match, uri_regex)) {
        info.dialect_ = pieces_match[1].str();
        info.username_ = pieces_match[2].str();
        info.password_ = pieces_match[3].str();
        info.host_ = pieces_match[4].str();
        info.port_ = pieces_match[5].str();
        info.db_name_ = pieces_match[6].str();

S
starlord 已提交
230
        // TODO(myh): verify host, port...
231 232 233 234 235 236 237
    } else {
        return Status(DB_INVALID_META_URI, "Invalid meta uri: " + uri);
    }

    return Status::OK();
}

S
starlord 已提交
238 239 240
}  // namespace utils
}  // namespace engine
}  // namespace milvus