Utils.cpp 5.3 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed 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
J
jinhai 已提交
5
//
6 7 8 9 10
// 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.
J
jinhai 已提交
11

S
starlord 已提交
12
#include "db/Utils.h"
X
Xu Peng 已提交
13

14
#include <fiu/fiu-local.h>
15

G
groot 已提交
16
#include <unistd.h>
S
starlord 已提交
17
#include <boost/filesystem.hpp>
X
Xu Peng 已提交
18
#include <chrono>
G
groot 已提交
19
#include <memory>
S
starlord 已提交
20
#include <mutex>
21
#include <regex>
S
starlord 已提交
22
#include <vector>
X
Xu Peng 已提交
23

C
Cai Yudong 已提交
24
#include "cache/CpuCacheMgr.h"
G
groot 已提交
25 26
#include "db/Types.h"

C
Cai Yudong 已提交
27
#ifdef MILVUS_GPU_VERSION
G
groot 已提交
28

C
Cai Yudong 已提交
29
#include "cache/GpuCacheMgr.h"
G
groot 已提交
30

C
Cai Yudong 已提交
31
#endif
G
groot 已提交
32

W
Wang XiangYu 已提交
33
#include "config/ServerConfig.h"
C
Cai Yudong 已提交
34
//#include "storage/s3/S3ClientWrapper.h"
G
groot 已提交
35
#include "knowhere/index/vector_index/helpers/IndexParameter.h"
36 37 38
#include "utils/CommonUtil.h"
#include "utils/Log.h"

39 40
#include <map>

J
jinhai 已提交
41
namespace milvus {
X
Xu Peng 已提交
42 43 44
namespace engine {
namespace utils {

S
starlord 已提交
45 46
int64_t
GetMicroSecTimeStamp() {
X
Xu Peng 已提交
47
    auto now = std::chrono::system_clock::now();
S
starlord 已提交
48
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
X
Xu Peng 已提交
49 50 51 52

    return micros;
}

S
starlord 已提交
53
bool
54
IsSameIndex(const CollectionIndex& index1, const CollectionIndex& index2) {
G
groot 已提交
55
    return index1.index_type_ == index2.index_type_ && index1.extra_params_ == index2.extra_params_ &&
G
groot 已提交
56
           index1.metric_name_ == index2.metric_name_;
57 58
}

59
bool
G
groot 已提交
60 61 62 63
IsBinaryMetricType(const std::string& metric_type) {
    return (metric_type == knowhere::Metric::HAMMING) || (metric_type == knowhere::Metric::JACCARD) ||
           (metric_type == knowhere::Metric::SUBSTRUCTURE) || (metric_type == knowhere::Metric::SUPERSTRUCTURE) ||
           (metric_type == knowhere::Metric::TANIMOTO);
64 65
}

G
groot 已提交
66
engine::date_t
S
starlord 已提交
67
GetDate(const std::time_t& t, int day_delta) {
68 69 70 71 72 73
    struct tm ltm;
    localtime_r(&t, &ltm);
    if (day_delta > 0) {
        do {
            ++ltm.tm_mday;
            --day_delta;
S
starlord 已提交
74
        } while (day_delta > 0);
75 76 77 78 79
        mktime(&ltm);
    } else if (day_delta < 0) {
        do {
            --ltm.tm_mday;
            ++day_delta;
S
starlord 已提交
80
        } while (day_delta < 0);
81 82
        mktime(&ltm);
    }
S
starlord 已提交
83
    return ltm.tm_year * 10000 + ltm.tm_mon * 100 + ltm.tm_mday;
84 85
}

G
groot 已提交
86
engine::date_t
S
starlord 已提交
87
GetDateWithDelta(int day_delta) {
88 89 90
    return GetDate(std::time(nullptr), day_delta);
}

G
groot 已提交
91
engine::date_t
S
starlord 已提交
92
GetDate() {
93 94 95
    return GetDate(std::time(nullptr), 0);
}

96
// URI format: dialect://username:password@host:port/database
S
starlord 已提交
97
Status
S
starlord 已提交
98
ParseMetaUri(const std::string& uri, MetaUriInfo& info) {
99 100 101 102 103 104
    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 = "(.*)";
C
Cai Yudong 已提交
105 106
    std::string uri_regex_str = dialect_regex + R"(\:\/\/)" + username_tegex + R"(\:)" + password_regex + R"(\@)" +
                                host_regex + R"(\:)" + port_regex + R"(\/)" + db_name_regex;
107 108 109 110 111 112 113 114 115 116 117 118

    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 已提交
119
        // TODO(myh): verify host, port...
120 121 122 123 124 125 126
    } else {
        return Status(DB_INVALID_META_URI, "Invalid meta uri: " + uri);
    }

    return Status::OK();
}

G
groot 已提交
127 128 129 130 131 132 133
void
SendExitSignal() {
    LOG_SERVER_INFO_ << "Send SIGUSR2 signal to exit";
    pid_t pid = getpid();
    kill(pid, SIGUSR2);
}

G
groot 已提交
134 135 136 137 138 139 140
void
GetIDFromChunk(const engine::DataChunkPtr& chunk, engine::IDNumbers& ids) {
    ids.clear();
    if (chunk == nullptr) {
        return;
    }

G
groot 已提交
141
    auto pair = chunk->fixed_fields_.find(engine::FIELD_UID);
G
groot 已提交
142 143 144 145 146
    if (pair == chunk->fixed_fields_.end() || pair->second == nullptr) {
        return;
    }

    if (!pair->second->data_.empty()) {
G
groot 已提交
147
        ids.resize(pair->second->data_.size() / sizeof(engine::idx_t));
C
Cai Yudong 已提交
148
        memcpy(ids.data(), pair->second->data_.data(), pair->second->data_.size());
G
groot 已提交
149 150 151
    }
}

G
groot 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
int64_t
GetSizeOfChunk(const engine::DataChunkPtr& chunk) {
    if (chunk == nullptr) {
        return 0;
    }

    int64_t total_size = 0;
    for (auto& pair : chunk->fixed_fields_) {
        if (pair.second == nullptr) {
            continue;
        }
        total_size += pair.second->Size();
    }
    for (auto& pair : chunk->variable_fields_) {
        if (pair.second == nullptr) {
            continue;
        }
        total_size += pair.second->Size();
    }

    return total_size;
}

G
groot 已提交
175 176 177 178 179 180 181 182
bool
RequireRawFile(const std::string& index_type) {
    return index_type == knowhere::IndexEnum::INDEX_FAISS_IVFFLAT || index_type == knowhere::IndexEnum::INDEX_NSG ||
           index_type == knowhere::IndexEnum::INDEX_HNSW;
}

bool
RequireCompressFile(const std::string& index_type) {
183
    return index_type == knowhere::IndexEnum::INDEX_RHNSWSQ || index_type == knowhere::IndexEnum::INDEX_RHNSWPQ;
G
groot 已提交
184 185
}

S
starlord 已提交
186 187 188
}  // namespace utils
}  // namespace engine
}  // namespace milvus