ValidationUtil.cpp 4.0 KB
Newer Older
S
starlord 已提交
1
#include "db/engine/ExecutionEngine.h"
J
jinhai 已提交
2 3 4
#include "ValidationUtil.h"
#include "Log.h"

5
#include <cuda_runtime.h>
J
jinhai 已提交
6 7 8 9 10

namespace zilliz {
namespace milvus {
namespace server {

J
jinhai 已提交
11
constexpr size_t table_name_size_limit = 255;
J
jinhai 已提交
12
constexpr int64_t table_dimension_limit = 16384;
S
starlord 已提交
13
constexpr int32_t index_file_size_limit = 4096; //index trigger size max = 4096 MB
J
jinhai 已提交
14

S
starlord 已提交
15
ErrorCode
16
ValidationUtil::ValidateTableName(const std::string &table_name) {
J
jinhai 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

    // Table name shouldn't be empty.
    if (table_name.empty()) {
        SERVER_LOG_ERROR << "Empty table name";
        return SERVER_INVALID_TABLE_NAME;
    }

    // Table name size shouldn't exceed 16384.
    if (table_name.size() > table_name_size_limit) {
        SERVER_LOG_ERROR << "Table name size exceed the limitation";
        return SERVER_INVALID_TABLE_NAME;
    }

    // Table name first character should be underscore or character.
    char first_char = table_name[0];
    if (first_char != '_' && std::isalpha(first_char) == 0) {
        SERVER_LOG_ERROR << "Table name first character isn't underscore or character: " << first_char;
        return SERVER_INVALID_TABLE_NAME;
    }

    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) {
            SERVER_LOG_ERROR << "Table name character isn't underscore or alphanumber: " << name_char;
            return SERVER_INVALID_TABLE_NAME;
        }
    }

    return SERVER_SUCCESS;
}

S
starlord 已提交
49
ErrorCode
50
ValidationUtil::ValidateTableDimension(int64_t dimension) {
J
jinhai 已提交
51 52 53 54 55 56 57 58
    if (dimension <= 0 || dimension > table_dimension_limit) {
        SERVER_LOG_ERROR << "Table dimension excceed the limitation: " << table_dimension_limit;
        return SERVER_INVALID_VECTOR_DIMENSION;
    } else {
        return SERVER_SUCCESS;
    }
}

S
starlord 已提交
59
ErrorCode
60
ValidationUtil::ValidateTableIndexType(int32_t index_type) {
61 62 63
    int engine_type = (int)engine::EngineType(index_type);
    if(engine_type <= 0 || engine_type > (int)engine::EngineType::MAX_VALUE) {
        return SERVER_INVALID_INDEX_TYPE;
J
jinhai 已提交
64
    }
65 66

    return SERVER_SUCCESS;
J
jinhai 已提交
67 68
}

S
starlord 已提交
69
ErrorCode
S
starlord 已提交
70 71 72 73 74 75 76 77
ValidationUtil::ValidateTableIndexNlist(int32_t nlist) {
    if(nlist <= 0) {
        return SERVER_INVALID_INDEX_NLIST;
    }

    return SERVER_SUCCESS;
}

S
starlord 已提交
78
ErrorCode
79
ValidationUtil::ValidateTableIndexFileSize(int64_t index_file_size) {
S
starlord 已提交
80 81 82 83 84 85 86
    if(index_file_size <= 0 || index_file_size > index_file_size_limit) {
        return SERVER_INVALID_INDEX_FILE_SIZE;
    }

    return SERVER_SUCCESS;
}

S
starlord 已提交
87
ErrorCode
S
starlord 已提交
88 89 90 91 92 93 94
ValidationUtil::ValidateTableIndexMetricType(int32_t metric_type) {
    if(metric_type != (int32_t)engine::MetricType::L2 && metric_type != (int32_t)engine::MetricType::IP) {
        return SERVER_INVALID_INDEX_METRIC_TYPE;
    }
    return SERVER_SUCCESS;
}

S
starlord 已提交
95
ErrorCode
96 97 98 99 100 101 102 103
ValidationUtil::ValidateSearchTopk(int64_t top_k, const engine::meta::TableSchema& table_schema) {
    if (top_k <= 0 || top_k > 1024) {
        return SERVER_INVALID_TOPK;
    }

    return SERVER_SUCCESS;
}

S
starlord 已提交
104
ErrorCode
105 106 107 108 109 110 111 112
ValidationUtil::ValidateSearchNprobe(int64_t nprobe, const engine::meta::TableSchema& table_schema) {
    if (nprobe <= 0 || nprobe > table_schema.nlist_) {
        return SERVER_INVALID_NPROBE;
    }

    return SERVER_SUCCESS;
}

S
starlord 已提交
113
ErrorCode
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
ValidationUtil::ValidateGpuIndex(uint32_t gpu_index) {
    int num_devices = 0;
    auto cuda_err = cudaGetDeviceCount(&num_devices);
    if (cuda_err) {
        SERVER_LOG_ERROR << "Failed to count video card: " << std::to_string(cuda_err);
        return SERVER_UNEXPECTED_ERROR;
    }

    if(gpu_index >= num_devices) {
        return SERVER_INVALID_ARGUMENT;
    }

    return SERVER_SUCCESS;
}

S
starlord 已提交
129
ErrorCode
130 131 132 133 134 135 136 137 138 139 140 141
ValidationUtil::GetGpuMemory(uint32_t gpu_index, size_t& memory) {
    cudaDeviceProp deviceProp;
    auto cuda_err = cudaGetDeviceProperties(&deviceProp, gpu_index);
    if (cuda_err) {
        SERVER_LOG_ERROR << "Failed to get video card properties: " << std::to_string(cuda_err);
        return SERVER_UNEXPECTED_ERROR;
    }

    memory = deviceProp.totalGlobalMem;
    return SERVER_SUCCESS;
}

J
jinhai 已提交
142 143 144
}
}
}