ValidationUtil.cpp 11.9 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 "utils/ValidationUtil.h"
J
jinhai 已提交
19
#include "Log.h"
S
starlord 已提交
20
#include "db/engine/ExecutionEngine.h"
21
#include "utils/StringHelpFunctions.h"
J
jinhai 已提交
22

Z
zhiru 已提交
23
#include <arpa/inet.h>
Y
youny626 已提交
24
#ifdef MILVUS_GPU_VERSION
S
starlord 已提交
25
#include <cuda_runtime.h>
Y
youny626 已提交
26
#endif
Z
zhiru 已提交
27
#include <algorithm>
J
JinHai-CN 已提交
28
#include <cmath>
S
starlord 已提交
29 30
#include <regex>
#include <string>
Z
zhiru 已提交
31

J
jinhai 已提交
32 33 34
namespace milvus {
namespace server {

35 36
constexpr size_t TABLE_NAME_SIZE_LIMIT = 255;
constexpr int64_t TABLE_DIMENSION_LIMIT = 16384;
S
starlord 已提交
37
constexpr int32_t INDEX_FILE_SIZE_LIMIT = 4096;  // index trigger size max = 4096 MB
J
jinhai 已提交
38

S
starlord 已提交
39
Status
S
starlord 已提交
40
ValidationUtil::ValidateTableName(const std::string& table_name) {
J
jinhai 已提交
41 42
    // Table name shouldn't be empty.
    if (table_name.empty()) {
43
        std::string msg = "Table name should not be empty.";
S
starlord 已提交
44 45
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_TABLE_NAME, msg);
J
jinhai 已提交
46 47
    }

48
    std::string invalid_msg = "Invalid table name: " + table_name + ". ";
J
jinhai 已提交
49
    // Table name size shouldn't exceed 16384.
50
    if (table_name.size() > TABLE_NAME_SIZE_LIMIT) {
51
        std::string msg = invalid_msg + "The length of a table name must be less than 255 characters.";
S
starlord 已提交
52 53
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_TABLE_NAME, msg);
J
jinhai 已提交
54 55 56 57 58
    }

    // Table name first character should be underscore or character.
    char first_char = table_name[0];
    if (first_char != '_' && std::isalpha(first_char) == 0) {
59
        std::string msg = invalid_msg + "The first character of a table name must be an underscore or letter.";
S
starlord 已提交
60 61
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_TABLE_NAME, msg);
J
jinhai 已提交
62 63 64 65 66 67
    }

    int64_t table_name_size = table_name.size();
    for (int64_t i = 1; i < table_name_size; ++i) {
        char name_char = table_name[i];
        if (name_char != '_' && std::isalnum(name_char) == 0) {
68
            std::string msg = invalid_msg + "Table name can only contain numbers, letters, and underscores.";
S
starlord 已提交
69 70
            SERVER_LOG_ERROR << msg;
            return Status(SERVER_INVALID_TABLE_NAME, msg);
J
jinhai 已提交
71 72 73
        }
    }

S
starlord 已提交
74
    return Status::OK();
J
jinhai 已提交
75 76
}

S
starlord 已提交
77
Status
78
ValidationUtil::ValidateTableDimension(int64_t dimension) {
79
    if (dimension <= 0 || dimension > TABLE_DIMENSION_LIMIT) {
S
starlord 已提交
80 81
        std::string msg = "Invalid table dimension: " + std::to_string(dimension) + ". " +
                          "The table dimension must be within the range of 1 ~ 16384.";
S
starlord 已提交
82 83
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_VECTOR_DIMENSION, msg);
S
starlord 已提交
84
    } else {
S
starlord 已提交
85
        return Status::OK();
J
jinhai 已提交
86 87 88
    }
}

S
starlord 已提交
89
Status
90
ValidationUtil::ValidateTableIndexType(int32_t index_type) {
S
starlord 已提交
91 92
    int engine_type = static_cast<int>(engine::EngineType(index_type));
    if (engine_type <= 0 || engine_type > static_cast<int>(engine::EngineType::MAX_VALUE)) {
S
starlord 已提交
93 94
        std::string msg = "Invalid index type: " + std::to_string(index_type) + ". " +
                          "Make sure the index type is in IndexType list.";
S
starlord 已提交
95 96
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_INDEX_TYPE, msg);
J
jinhai 已提交
97
    }
98

99 100 101 102 103 104 105 106 107
#ifndef CUSTOMIZATION
    // special case, hybird index only available in customize faiss library
    if (engine_type == static_cast<int>(engine::EngineType::FAISS_IVFSQ8H)) {
        std::string msg = "Unsupported index type: " + std::to_string(index_type);
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_INDEX_TYPE, msg);
    }
#endif

S
starlord 已提交
108
    return Status::OK();
J
jinhai 已提交
109 110
}

S
starlord 已提交
111
Status
S
starlord 已提交
112
ValidationUtil::ValidateTableIndexNlist(int32_t nlist) {
Z
zhiru 已提交
113
    if (nlist <= 0) {
S
starlord 已提交
114 115
        std::string msg =
            "Invalid index nlist: " + std::to_string(nlist) + ". " + "The index nlist must be greater than 0.";
S
starlord 已提交
116 117
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_INDEX_NLIST, msg);
S
starlord 已提交
118 119
    }

S
starlord 已提交
120
    return Status::OK();
S
starlord 已提交
121 122
}

S
starlord 已提交
123
Status
124
ValidationUtil::ValidateTableIndexFileSize(int64_t index_file_size) {
Z
zhiru 已提交
125
    if (index_file_size <= 0 || index_file_size > INDEX_FILE_SIZE_LIMIT) {
S
starlord 已提交
126 127 128
        std::string msg = "Invalid index file size: " + std::to_string(index_file_size) + ". " +
                          "The index file size must be within the range of 1 ~ " +
                          std::to_string(INDEX_FILE_SIZE_LIMIT) + ".";
S
starlord 已提交
129 130
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_INDEX_FILE_SIZE, msg);
S
starlord 已提交
131 132
    }

S
starlord 已提交
133
    return Status::OK();
S
starlord 已提交
134 135
}

S
starlord 已提交
136
Status
S
starlord 已提交
137
ValidationUtil::ValidateTableIndexMetricType(int32_t metric_type) {
S
starlord 已提交
138 139
    if (metric_type != static_cast<int32_t>(engine::MetricType::L2) &&
        metric_type != static_cast<int32_t>(engine::MetricType::IP)) {
S
starlord 已提交
140 141
        std::string msg = "Invalid index metric type: " + std::to_string(metric_type) + ". " +
                          "Make sure the metric type is either MetricType.L2 or MetricType.IP.";
S
starlord 已提交
142 143
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_INDEX_METRIC_TYPE, msg);
S
starlord 已提交
144
    }
S
starlord 已提交
145
    return Status::OK();
S
starlord 已提交
146 147
}

S
starlord 已提交
148
Status
S
starlord 已提交
149
ValidationUtil::ValidateSearchTopk(int64_t top_k, const engine::meta::TableSchema& table_schema) {
150
    if (top_k <= 0 || top_k > 2048) {
S
starlord 已提交
151 152
        std::string msg =
            "Invalid topk: " + std::to_string(top_k) + ". " + "The topk must be within the range of 1 ~ 2048.";
S
starlord 已提交
153 154
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_TOPK, msg);
155 156
    }

S
starlord 已提交
157
    return Status::OK();
158 159
}

S
starlord 已提交
160
Status
S
starlord 已提交
161
ValidationUtil::ValidateSearchNprobe(int64_t nprobe, const engine::meta::TableSchema& table_schema) {
162
    if (nprobe <= 0 || nprobe > table_schema.nlist_) {
S
starlord 已提交
163 164
        std::string msg = "Invalid nprobe: " + std::to_string(nprobe) + ". " +
                          "The nprobe must be within the range of 1 ~ index nlist.";
S
starlord 已提交
165 166
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_NPROBE, msg);
167 168
    }

S
starlord 已提交
169
    return Status::OK();
170 171
}

172 173 174 175 176 177 178 179 180 181 182
Status
ValidationUtil::ValidatePartitionName(const std::string& partition_name) {
    if (partition_name.empty()) {
        std::string msg = "Partition name should not be empty.";
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_TABLE_NAME, msg);
    }

    return ValidateTableName(partition_name);
}

G
groot 已提交
183 184
Status
ValidationUtil::ValidatePartitionTags(const std::vector<std::string>& partition_tags) {
185 186 187 188 189 190 191
    for (const std::string& tag : partition_tags) {
        // trim side-blank of tag, only compare valid characters
        // for example: " ab cd " is treated as "ab cd"
        std::string valid_tag = tag;
        StringHelpFunctions::TrimStringBlank(valid_tag);
        if (valid_tag.empty()) {
            std::string msg = "Invalid partition tag: " + valid_tag + ". " + "Partition tag should not be empty.";
G
groot 已提交
192 193 194 195 196 197 198 199
            SERVER_LOG_ERROR << msg;
            return Status(SERVER_INVALID_NPROBE, msg);
        }
    }

    return Status::OK();
}

S
starlord 已提交
200
Status
201
ValidationUtil::ValidateGpuIndex(int32_t gpu_index) {
Y
youny626 已提交
202
#ifdef MILVUS_GPU_VERSION
203 204
    int num_devices = 0;
    auto cuda_err = cudaGetDeviceCount(&num_devices);
S
starlord 已提交
205
    if (cuda_err != cudaSuccess) {
S
starlord 已提交
206 207 208
        std::string msg = "Failed to get gpu card number, cuda error:" + std::to_string(cuda_err);
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_UNEXPECTED_ERROR, msg);
209 210
    }

Z
zhiru 已提交
211
    if (gpu_index >= num_devices) {
S
starlord 已提交
212 213 214
        std::string msg = "Invalid gpu index: " + std::to_string(gpu_index);
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_ARGUMENT, msg);
215
    }
Y
youny626 已提交
216
#endif
217

S
starlord 已提交
218
    return Status::OK();
219 220
}

S
starlord 已提交
221
Status
222
ValidationUtil::GetGpuMemory(int32_t gpu_index, size_t& memory) {
Y
youny626 已提交
223 224
#ifdef MILVUS_GPU_VERSION

225 226 227
    cudaDeviceProp deviceProp;
    auto cuda_err = cudaGetDeviceProperties(&deviceProp, gpu_index);
    if (cuda_err) {
Y
youny626 已提交
228 229
        std::string msg = "Failed to get gpu properties for gpu" + std::to_string(gpu_index) +
                          " , cuda error:" + std::to_string(cuda_err);
S
starlord 已提交
230 231
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_UNEXPECTED_ERROR, msg);
232 233 234
    }

    memory = deviceProp.totalGlobalMem;
Y
youny626 已提交
235 236
#endif

S
starlord 已提交
237
    return Status::OK();
238 239
}

S
starlord 已提交
240
Status
S
starlord 已提交
241
ValidationUtil::ValidateIpAddress(const std::string& ip_address) {
Z
zhiru 已提交
242 243 244 245
    struct in_addr address;

    int result = inet_pton(AF_INET, ip_address.c_str(), &address);

Z
zhiru 已提交
246
    switch (result) {
S
starlord 已提交
247 248
        case 1:
            return Status::OK();
S
starlord 已提交
249 250 251 252 253 254 255 256 257 258
        case 0: {
            std::string msg = "Invalid IP address: " + ip_address;
            SERVER_LOG_ERROR << msg;
            return Status(SERVER_INVALID_ARGUMENT, msg);
        }
        default: {
            std::string msg = "IP address conversion error: " + ip_address;
            SERVER_LOG_ERROR << msg;
            return Status(SERVER_UNEXPECTED_ERROR, msg);
        }
Z
zhiru 已提交
259 260 261
    }
}

S
starlord 已提交
262
Status
S
starlord 已提交
263
ValidationUtil::ValidateStringIsNumber(const std::string& str) {
Y
yudong.cai 已提交
264 265
    if (str.empty() || !std::all_of(str.begin(), str.end(), ::isdigit)) {
        return Status(SERVER_INVALID_ARGUMENT, "Invalid number");
Z
zhiru 已提交
266
    }
Y
yudong.cai 已提交
267 268
    try {
        int32_t value = std::stoi(str);
S
starlord 已提交
269
    } catch (...) {
Y
yudong.cai 已提交
270
        return Status(SERVER_INVALID_ARGUMENT, "Invalid number");
Z
zhiru 已提交
271
    }
Y
yudong.cai 已提交
272
    return Status::OK();
Z
zhiru 已提交
273 274
}

S
starlord 已提交
275
Status
S
starlord 已提交
276
ValidationUtil::ValidateStringIsBool(const std::string& str) {
Y
yudong.cai 已提交
277 278
    std::string s = str;
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
S
starlord 已提交
279
    if (s == "true" || s == "on" || s == "yes" || s == "1" || s == "false" || s == "off" || s == "no" || s == "0" ||
Y
yudong.cai 已提交
280
        s.empty()) {
S
starlord 已提交
281
        return Status::OK();
Z
zhiru 已提交
282
    }
S
starlord 已提交
283
    return Status(SERVER_INVALID_ARGUMENT, "Invalid boolean: " + str);
Z
zhiru 已提交
284 285
}

S
starlord 已提交
286
Status
S
starlord 已提交
287
ValidationUtil::ValidateStringIsFloat(const std::string& str) {
Y
yudong.cai 已提交
288 289
    try {
        float val = std::stof(str);
S
starlord 已提交
290
    } catch (...) {
Y
yudong.cai 已提交
291
        return Status(SERVER_INVALID_ARGUMENT, "Invalid float: " + str);
Z
zhiru 已提交
292
    }
Y
yudong.cai 已提交
293
    return Status::OK();
Z
zhiru 已提交
294 295
}

S
starlord 已提交
296
Status
S
starlord 已提交
297
ValidationUtil::ValidateDbURI(const std::string& uri) {
Z
zhiru 已提交
298 299 300 301 302 303
    std::string dialectRegex = "(.*)";
    std::string usernameRegex = "(.*)";
    std::string passwordRegex = "(.*)";
    std::string hostRegex = "(.*)";
    std::string portRegex = "(.*)";
    std::string dbNameRegex = "(.*)";
S
starlord 已提交
304 305
    std::string uriRegexStr = dialectRegex + "\\:\\/\\/" + usernameRegex + "\\:" + passwordRegex + "\\@" + hostRegex +
                              "\\:" + portRegex + "\\/" + dbNameRegex;
Z
zhiru 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318
    std::regex uriRegex(uriRegexStr);
    std::smatch pieces_match;

    bool okay = true;

    if (std::regex_match(uri, pieces_match, uriRegex)) {
        std::string dialect = pieces_match[1].str();
        std::transform(dialect.begin(), dialect.end(), dialect.begin(), ::tolower);
        if (dialect.find("mysql") == std::string::npos && dialect.find("sqlite") == std::string::npos) {
            SERVER_LOG_ERROR << "Invalid dialect in URI: dialect = " << dialect;
            okay = false;
        }

S
starlord 已提交
319 320 321 322 323 324 325 326 327 328 329
        /*
         *      Could be DNS, skip checking
         *
                std::string host = pieces_match[4].str();
                if (!host.empty() && host != "localhost") {
                    if (ValidateIpAddress(host) != SERVER_SUCCESS) {
                        SERVER_LOG_ERROR << "Invalid host ip address in uri = " << host;
                        okay = false;
                    }
                }
        */
Z
zhiru 已提交
330 331 332

        std::string port = pieces_match[5].str();
        if (!port.empty()) {
S
starlord 已提交
333 334
            auto status = ValidateStringIsNumber(port);
            if (!status.ok()) {
Z
zhiru 已提交
335 336 337 338
                SERVER_LOG_ERROR << "Invalid port in uri = " << port;
                okay = false;
            }
        }
S
starlord 已提交
339
    } else {
Z
zhiru 已提交
340 341 342 343
        SERVER_LOG_ERROR << "Wrong URI format: URI = " << uri;
        okay = false;
    }

S
starlord 已提交
344
    return (okay ? Status::OK() : Status(SERVER_INVALID_ARGUMENT, "Invalid db backend uri"));
Z
zhiru 已提交
345 346
}

S
starlord 已提交
347 348
}  // namespace server
}  // namespace milvus