ValidationUtil.cpp 9.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.

G
groot 已提交
18
#include "utils/ValidationUtil.h"
J
jinhai 已提交
19
#include "Log.h"
G
groot 已提交
20
#include "db/engine/ExecutionEngine.h"
J
jinhai 已提交
21

Z
zhiru 已提交
22
#include <arpa/inet.h>
G
groot 已提交
23
#include <cuda_runtime.h>
Z
zhiru 已提交
24
#include <algorithm>
G
groot 已提交
25 26
#include <regex>
#include <string>
Z
zhiru 已提交
27

J
jinhai 已提交
28 29 30
namespace milvus {
namespace server {

31 32
constexpr size_t TABLE_NAME_SIZE_LIMIT = 255;
constexpr int64_t TABLE_DIMENSION_LIMIT = 16384;
G
groot 已提交
33
constexpr int32_t INDEX_FILE_SIZE_LIMIT = 4096;  // index trigger size max = 4096 MB
J
jinhai 已提交
34

G
groot 已提交
35
Status
G
groot 已提交
36
ValidationUtil::ValidateTableName(const std::string& table_name) {
J
jinhai 已提交
37 38
    // Table name shouldn't be empty.
    if (table_name.empty()) {
G
groot 已提交
39 40 41
        std::string msg = "Empty table name";
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_TABLE_NAME, msg);
J
jinhai 已提交
42 43 44
    }

    // Table name size shouldn't exceed 16384.
45
    if (table_name.size() > TABLE_NAME_SIZE_LIMIT) {
G
groot 已提交
46 47 48
        std::string msg = "Table name size exceed the limitation";
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_TABLE_NAME, msg);
J
jinhai 已提交
49 50 51 52 53
    }

    // Table name first character should be underscore or character.
    char first_char = table_name[0];
    if (first_char != '_' && std::isalpha(first_char) == 0) {
G
groot 已提交
54 55 56
        std::string msg = "Table name first character isn't underscore or character";
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_TABLE_NAME, msg);
J
jinhai 已提交
57 58 59 60 61 62
    }

    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) {
G
groot 已提交
63 64 65
            std::string msg = "Table name character isn't underscore or alphanumber";
            SERVER_LOG_ERROR << msg;
            return Status(SERVER_INVALID_TABLE_NAME, msg);
J
jinhai 已提交
66 67 68
        }
    }

G
groot 已提交
69
    return Status::OK();
J
jinhai 已提交
70 71
}

G
groot 已提交
72
Status
G
groot 已提交
73
ValidationUtil::ValidateTableDimension(int64_t dimension) {
G
groot 已提交
74 75 76 77 78
    if (dimension <= 0) {
        std::string msg = "Dimension value should be greater than 0";
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_VECTOR_DIMENSION, msg);
    } else if (dimension > TABLE_DIMENSION_LIMIT) {
G
groot 已提交
79 80 81
        std::string msg = "Table dimension excceed the limitation: " + std::to_string(TABLE_DIMENSION_LIMIT);
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_VECTOR_DIMENSION, msg);
G
groot 已提交
82
    } else {
G
groot 已提交
83
        return Status::OK();
J
jinhai 已提交
84 85 86
    }
}

G
groot 已提交
87
Status
G
groot 已提交
88
ValidationUtil::ValidateTableIndexType(int32_t index_type) {
G
groot 已提交
89 90
    int engine_type = static_cast<int>(engine::EngineType(index_type));
    if (engine_type <= 0 || engine_type > static_cast<int>(engine::EngineType::MAX_VALUE)) {
G
groot 已提交
91 92 93
        std::string msg = "Invalid index type: " + std::to_string(index_type);
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_INDEX_TYPE, msg);
J
jinhai 已提交
94
    }
95

G
groot 已提交
96
    return Status::OK();
J
jinhai 已提交
97 98
}

G
groot 已提交
99
Status
G
groot 已提交
100
ValidationUtil::ValidateTableIndexNlist(int32_t nlist) {
Z
zhiru 已提交
101
    if (nlist <= 0) {
G
groot 已提交
102 103 104
        std::string msg = "Invalid nlist value: " + std::to_string(nlist);
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_INDEX_NLIST, msg);
G
groot 已提交
105 106
    }

G
groot 已提交
107
    return Status::OK();
G
groot 已提交
108 109
}

G
groot 已提交
110
Status
111
ValidationUtil::ValidateTableIndexFileSize(int64_t index_file_size) {
Z
zhiru 已提交
112
    if (index_file_size <= 0 || index_file_size > INDEX_FILE_SIZE_LIMIT) {
G
groot 已提交
113 114 115
        std::string msg = "Invalid index file size: " + std::to_string(index_file_size);
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_INDEX_FILE_SIZE, msg);
G
groot 已提交
116 117
    }

G
groot 已提交
118
    return Status::OK();
G
groot 已提交
119 120
}

G
groot 已提交
121
Status
G
groot 已提交
122
ValidationUtil::ValidateTableIndexMetricType(int32_t metric_type) {
G
groot 已提交
123 124
    if (metric_type != static_cast<int32_t>(engine::MetricType::L2) &&
        metric_type != static_cast<int32_t>(engine::MetricType::IP)) {
G
groot 已提交
125 126 127
        std::string msg = "Invalid metric type: " + std::to_string(metric_type);
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_INDEX_METRIC_TYPE, msg);
G
groot 已提交
128
    }
G
groot 已提交
129
    return Status::OK();
G
groot 已提交
130 131
}

G
groot 已提交
132
Status
G
groot 已提交
133
ValidationUtil::ValidateSearchTopk(int64_t top_k, const engine::meta::TableSchema& table_schema) {
134
    if (top_k <= 0 || top_k > 2048) {
G
groot 已提交
135
        std::string msg = "Invalid top k value: " + std::to_string(top_k) + ", rational range [1, 2048]";
G
groot 已提交
136 137
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_TOPK, msg);
138 139
    }

G
groot 已提交
140
    return Status::OK();
141 142
}

G
groot 已提交
143
Status
G
groot 已提交
144
ValidationUtil::ValidateSearchNprobe(int64_t nprobe, const engine::meta::TableSchema& table_schema) {
145
    if (nprobe <= 0 || nprobe > table_schema.nlist_) {
G
groot 已提交
146 147
        std::string msg = "Invalid nprobe value: " + std::to_string(nprobe) + ", rational range [1, " +
                          std::to_string(table_schema.nlist_) + "]";
G
groot 已提交
148 149
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_NPROBE, msg);
150 151
    }

G
groot 已提交
152
    return Status::OK();
153 154
}

G
groot 已提交
155
Status
G
groot 已提交
156 157 158
ValidationUtil::ValidateGpuIndex(uint32_t gpu_index) {
    int num_devices = 0;
    auto cuda_err = cudaGetDeviceCount(&num_devices);
G
groot 已提交
159
    if (cuda_err != cudaSuccess) {
G
groot 已提交
160 161 162
        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);
G
groot 已提交
163 164
    }

Z
zhiru 已提交
165
    if (gpu_index >= num_devices) {
G
groot 已提交
166 167 168
        std::string msg = "Invalid gpu index: " + std::to_string(gpu_index);
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_INVALID_ARGUMENT, msg);
G
groot 已提交
169 170
    }

G
groot 已提交
171
    return Status::OK();
G
groot 已提交
172 173
}

G
groot 已提交
174
Status
G
groot 已提交
175
ValidationUtil::GetGpuMemory(uint32_t gpu_index, size_t& memory) {
G
groot 已提交
176 177 178
    cudaDeviceProp deviceProp;
    auto cuda_err = cudaGetDeviceProperties(&deviceProp, gpu_index);
    if (cuda_err) {
G
groot 已提交
179 180 181
        std::string msg = "Failed to get gpu properties, cuda error:" + std::to_string(cuda_err);
        SERVER_LOG_ERROR << msg;
        return Status(SERVER_UNEXPECTED_ERROR, msg);
G
groot 已提交
182 183 184
    }

    memory = deviceProp.totalGlobalMem;
G
groot 已提交
185
    return Status::OK();
G
groot 已提交
186 187
}

G
groot 已提交
188
Status
G
groot 已提交
189
ValidationUtil::ValidateIpAddress(const std::string& ip_address) {
Z
zhiru 已提交
190 191 192 193
    struct in_addr address;

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

Z
zhiru 已提交
194
    switch (result) {
G
groot 已提交
195 196
        case 1:
            return Status::OK();
G
groot 已提交
197 198 199 200 201 202 203 204 205 206
        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 已提交
207 208 209
    }
}

G
groot 已提交
210
Status
G
groot 已提交
211
ValidationUtil::ValidateStringIsNumber(const std::string& str) {
Y
yudong.cai 已提交
212 213
    if (str.empty() || !std::all_of(str.begin(), str.end(), ::isdigit)) {
        return Status(SERVER_INVALID_ARGUMENT, "Invalid number");
Z
zhiru 已提交
214
    }
Y
yudong.cai 已提交
215 216
    try {
        int32_t value = std::stoi(str);
G
groot 已提交
217
    } catch (...) {
Y
yudong.cai 已提交
218
        return Status(SERVER_INVALID_ARGUMENT, "Invalid number");
Z
zhiru 已提交
219
    }
Y
yudong.cai 已提交
220
    return Status::OK();
Z
zhiru 已提交
221 222
}

G
groot 已提交
223
Status
G
groot 已提交
224
ValidationUtil::ValidateStringIsBool(const std::string& str) {
Y
yudong.cai 已提交
225 226
    std::string s = str;
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
G
groot 已提交
227
    if (s == "true" || s == "on" || s == "yes" || s == "1" || s == "false" || s == "off" || s == "no" || s == "0" ||
Y
yudong.cai 已提交
228
        s.empty()) {
G
groot 已提交
229
        return Status::OK();
Z
zhiru 已提交
230
    }
G
groot 已提交
231
    return Status(SERVER_INVALID_ARGUMENT, "Invalid boolean: " + str);
Z
zhiru 已提交
232 233
}

G
groot 已提交
234
Status
G
groot 已提交
235
ValidationUtil::ValidateStringIsFloat(const std::string& str) {
Y
yudong.cai 已提交
236 237
    try {
        float val = std::stof(str);
G
groot 已提交
238
    } catch (...) {
Y
yudong.cai 已提交
239
        return Status(SERVER_INVALID_ARGUMENT, "Invalid float: " + str);
Z
zhiru 已提交
240
    }
Y
yudong.cai 已提交
241
    return Status::OK();
Z
zhiru 已提交
242 243
}

G
groot 已提交
244
Status
G
groot 已提交
245
ValidationUtil::ValidateDbURI(const std::string& uri) {
Z
zhiru 已提交
246 247 248 249 250 251
    std::string dialectRegex = "(.*)";
    std::string usernameRegex = "(.*)";
    std::string passwordRegex = "(.*)";
    std::string hostRegex = "(.*)";
    std::string portRegex = "(.*)";
    std::string dbNameRegex = "(.*)";
G
groot 已提交
252 253
    std::string uriRegexStr = dialectRegex + "\\:\\/\\/" + usernameRegex + "\\:" + passwordRegex + "\\@" + hostRegex +
                              "\\:" + portRegex + "\\/" + dbNameRegex;
Z
zhiru 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266
    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;
        }

G
groot 已提交
267 268 269 270 271 272 273 274 275 276 277
        /*
         *      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 已提交
278 279 280

        std::string port = pieces_match[5].str();
        if (!port.empty()) {
G
groot 已提交
281 282
            auto status = ValidateStringIsNumber(port);
            if (!status.ok()) {
Z
zhiru 已提交
283 284 285 286
                SERVER_LOG_ERROR << "Invalid port in uri = " << port;
                okay = false;
            }
        }
G
groot 已提交
287
    } else {
Z
zhiru 已提交
288 289 290 291
        SERVER_LOG_ERROR << "Wrong URI format: URI = " << uri;
        okay = false;
    }

G
groot 已提交
292
    return (okay ? Status::OK() : Status(SERVER_INVALID_ARGUMENT, "Invalid db backend uri"));
Z
zhiru 已提交
293 294
}

G
groot 已提交
295 296
}  // namespace server
}  // namespace milvus