Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

expected support error message #970

Merged
merged 1 commit into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions include/knowhere/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ class EntryAccess {
class Config {
public:
static Status
FormatAndCheck(const Config& cfg, Json& json);
FormatAndCheck(const Config& cfg, Json& json, std::string* const err_msg = nullptr);

static Status
Load(Config& cfg, const Json& json, PARAM_TYPE type) {
Load(Config& cfg, const Json& json, PARAM_TYPE type, std::string* const err_msg = nullptr) {
for (const auto& it : cfg.__DICT__) {
const auto& var = it.second;

Expand All @@ -278,6 +278,9 @@ class Config {
continue;
}
LOG_KNOWHERE_ERROR_ << "Invalid param [" << it.first << "] in json.";
if (err_msg) {
*err_msg = std::string("invalid param ") + it.first;
}
return Status::invalid_param_in_json;
}
if (json.find(it.first) == json.end()) {
Expand All @@ -286,12 +289,18 @@ class Config {
}
if (!json[it.first].is_number_integer()) {
LOG_KNOWHERE_ERROR_ << "Type conflict in json: param [" << it.first << "] should be integer.";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be integer";
}
return Status::type_conflict_in_json;
}
if (ptr->range.has_value()) {
if (json[it.first].get<long>() > std::numeric_limits<CFG_INT::value_type>::max()) {
LOG_KNOWHERE_ERROR_ << "Arithmetic overflow: param [" << it.first << "] should be at most "
<< std::numeric_limits<CFG_INT::value_type>::max();
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be at most 2147483647";
}
return Status::arithmetic_overflow;
}
CFG_INT::value_type v = json[it.first];
Expand All @@ -300,6 +309,11 @@ class Config {
} else {
LOG_KNOWHERE_ERROR_ << "Out of range in json: param [" << it.first << "] should be in ["
<< ptr->range.value().first << ", " << ptr->range.value().second << "].";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " out of range " + "[ " +
std::to_string(ptr->range.value().first) + "," +
std::to_string(ptr->range.value().second) + " ]";
}
return Status::out_of_range_in_json;
}
} else {
Expand All @@ -316,6 +330,10 @@ class Config {
continue;
}
LOG_KNOWHERE_ERROR_ << "Invalid param [" << it.first << "] in json.";
if (err_msg) {
*err_msg = std::string("invalid param ") + it.first;
}

return Status::invalid_param_in_json;
}
if (json.find(it.first) == json.end()) {
Expand All @@ -324,12 +342,20 @@ class Config {
}
if (!json[it.first].is_number()) {
LOG_KNOWHERE_ERROR_ << "Type conflict in json: param [" << it.first << "] should be a number.";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be a number";
}

return Status::type_conflict_in_json;
}
if (ptr->range.has_value()) {
if (json[it.first].get<double>() > std::numeric_limits<CFG_FLOAT::value_type>::max()) {
LOG_KNOWHERE_ERROR_ << "Arithmetic overflow: param [" << it.first << "] should be at most "
<< std::numeric_limits<CFG_FLOAT::value_type>::max();
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be at most 3.402823e+38";
}

return Status::arithmetic_overflow;
}
CFG_FLOAT::value_type v = json[it.first];
Expand All @@ -338,6 +364,12 @@ class Config {
} else {
LOG_KNOWHERE_ERROR_ << "Out of range in json: param [" << it.first << "] should be in ["
<< ptr->range.value().first << ", " << ptr->range.value().second << "].";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " out of range " + "[ " +
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider creating a temp string for both LOG_KNOWHERE_ERROR_ and assigning to err_msg? may be cleaner if we avoid repeating the formatting code twice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good advice, but the code maybe looks weird.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the below looks slightly better? Unless we have a good reason to have different messages for the logging/exception. Feel free to ignore this comment if you insist.

auto msg = std::string("param ") + it.first + " out of range " + "[ " +
                std::to_string(ptr->range.value().first) + "," +
                std::to_string(ptr->range.value().second) + " ]";
LOG_KNOWHERE_ERROR_ << msg;
if (err_msg) {
    *err_msg = msg;
}

std::to_string(ptr->range.value().first) + "," +
std::to_string(ptr->range.value().second) + " ]";
}

return Status::out_of_range_in_json;
}
} else {
Expand All @@ -354,6 +386,9 @@ class Config {
continue;
}
LOG_KNOWHERE_ERROR_ << "Invalid param [" << it.first << "] in json.";
if (err_msg) {
*err_msg = std::string("invalid param ") + it.first;
}
return Status::invalid_param_in_json;
}
if (json.find(it.first) == json.end()) {
Expand All @@ -362,6 +397,9 @@ class Config {
}
if (!json[it.first].is_string()) {
LOG_KNOWHERE_ERROR_ << "Type conflict in json: param [" << it.first << "] should be a string.";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be a string";
}
return Status::type_conflict_in_json;
}
*ptr->val = json[it.first];
Expand All @@ -376,6 +414,10 @@ class Config {
continue;
}
LOG_KNOWHERE_ERROR_ << "Invalid param [" << it.first << "] in json.";
if (err_msg) {
*err_msg = std::string("invalid param ") + it.first;
}

return Status::invalid_param_in_json;
}
if (json.find(it.first) == json.end()) {
Expand All @@ -384,6 +426,10 @@ class Config {
}
if (!json[it.first].is_array()) {
LOG_KNOWHERE_ERROR_ << "Type conflict in json: param [" << it.first << "] should be an array.";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be an array";
}

return Status::type_conflict_in_json;
}
*ptr->val = CFG_LIST();
Expand All @@ -401,6 +447,10 @@ class Config {
continue;
}
LOG_KNOWHERE_ERROR_ << "Invalid param [" << it.first << "] in json.";
if (err_msg) {
*err_msg = std::string("invalid param ") + it.first;
}

return Status::invalid_param_in_json;
}
if (json.find(it.first) == json.end()) {
Expand All @@ -409,6 +459,10 @@ class Config {
}
if (!json[it.first].is_boolean()) {
LOG_KNOWHERE_ERROR_ << "Type conflict in json: param [" << it.first << "] should be a boolean.";
if (err_msg) {
*err_msg = std::string("param ") + it.first + " should be a boolean";
}

return Status::type_conflict_in_json;
}
*ptr->val = json[it.first];
Expand Down
11 changes: 11 additions & 0 deletions include/knowhere/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ class expected {
return val.value();
}

const std::string&
what() const {
return msg;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel it would be better if the message is attached to a non success Status instead of an excepted: we may have some error info to be returned even if the current method returns only Status.

But now our Status is an enum class and cannot be extended. Would be a large change if we want to do that.

See absl::Status for reference.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your proposed change to pass err_msg to the returned value of Search the code looks like this, which is pretty clumsy:

expected<DataSetPtr> Search() const {
    auto cfg = this->node->CreateConfig();
    std::string err;
    auto status = LoadConfig(cfg.get(), json, knowhere::SEARCH, "Search", &err);
    if (err != Status::success) {
        expected<DataSetPtr> ret(err);
        ret << err;
        return ret;
    }
    ...
}

Status
LoadConfig(BaseConfig* cfg, const Json& json, knowhere::PARAM_TYPE param_type, const std::string& method, std::string* const err = nullptr) const {
    Json json_(json);
    auto res = Config::FormatAndCheck(*cfg, json_, err);
    LOG_KNOWHERE_DEBUG_ << method << " config dump: " << json_.dump();
    RETURN_IF_ERROR(res);
    return Config::Load(*cfg, json_, param_type, err);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the return status, Leads to huge changes.
err_msg, Compatible with old code.
I think just add err_msg for search api.


void
operator<<(const std::string& msg) {
this->msg += msg;
}

expected<T>&
operator=(const Status& err) {
assert(err != Status::success);
Expand All @@ -93,6 +103,7 @@ class expected {
private:
std::optional<T> val = std::nullopt;
std::optional<Status> err = std::nullopt;
std::string msg;
};

// Evaluates expr that returns a Status. Does nothing if the returned Status is
Expand Down
46 changes: 27 additions & 19 deletions src/common/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,32 @@ static const std::unordered_set<std::string> ext_legal_json_keys = {"metric_type
"for_tuning"};

Status
Config::FormatAndCheck(const Config& cfg, Json& json) {
for (auto& it : json.items()) {
bool status = true;
{
auto it_ = cfg.__DICT__.find(it.key());
if (it_ == cfg.__DICT__.end()) {
status = false;
Config::FormatAndCheck(const Config& cfg, Json& json, std::string* const err_msg) {
try {
for (auto& it : json.items()) {
bool status = true;
{
auto it_ = cfg.__DICT__.find(it.key());
if (it_ == cfg.__DICT__.end()) {
status = false;
}
}
}
{
auto it_ = ext_legal_json_keys.find(it.key());
if (it_ == ext_legal_json_keys.end()) {
status |= false;
} else {
status |= true;
{
auto it_ = ext_legal_json_keys.find(it.key());
if (it_ != ext_legal_json_keys.end()) {
status |= true;
}
}
if (!status) {
throw KnowhereException(std::string("invalid json key ") + it.key());
}
}
if (!status) {
LOG_KNOWHERE_ERROR_ << "invalid json key: " << it.key();
return Status::invalid_param_in_json;
} catch (std::exception& e) {
LOG_KNOWHERE_ERROR_ << e.what();
if (err_msg) {
*err_msg = e.what();
}
return Status::invalid_param_in_json;
}

try {
Expand All @@ -82,7 +87,7 @@ Config::FormatAndCheck(const Config& cfg, Json& json) {
auto value_str = json[it.first].get<std::string>();
CFG_INT::value_type v = std::stoi(value_str.c_str(), &sz);
if (sz < value_str.length()) {
throw KnowhereException("wrong data type in json");
throw KnowhereException(std::string("wrong data type in json ") + value_str);
}
json[it.first] = v;
}
Expand All @@ -102,7 +107,10 @@ Config::FormatAndCheck(const Config& cfg, Json& json) {
}
}
} catch (std::exception& e) {
LOG_KNOWHERE_ERROR_ << "Invalid value in json: " << e.what();
LOG_KNOWHERE_ERROR_ << e.what();
if (err_msg) {
*err_msg = e.what();
}
return Status::invalid_value_in_json;
}
return Status::success;
Expand Down