-
Notifications
You must be signed in to change notification settings - Fork 405
fix(transfer_engine): replace deprecated Json::Reader #938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,22 +45,31 @@ void DefaultConfig::loadFromYAML() { | |
|
|
||
| void DefaultConfig::loadFromJSON() { | ||
| Json::Value root; | ||
| Json::Reader reader; | ||
| std::ifstream file; | ||
| file.open(path_); | ||
|
|
||
xiaguan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!reader.parse(file, root, false)) { | ||
| file.close(); | ||
| throw std::runtime_error("Failed to parse JSON file: " + | ||
| reader.getFormattedErrorMessages()); | ||
| // Read entire file into string | ||
| std::string json_content((std::istreambuf_iterator<char>(file)), | ||
| std::istreambuf_iterator<char>()); | ||
| file.close(); | ||
|
|
||
| // Use thread-safe CharReaderBuilder instead of deprecated Reader | ||
| Json::CharReaderBuilder builder; | ||
| builder["collectComments"] = false; | ||
| std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); | ||
| std::string errs; | ||
|
|
||
| if (!reader->parse(json_content.data(), | ||
| json_content.data() + json_content.size(), &root, | ||
| &errs)) { | ||
| throw std::runtime_error("Failed to parse JSON file: " + errs); | ||
| } | ||
|
|
||
| try { | ||
| processNode(root, ""); | ||
| } catch (const std::exception& e) { | ||
| file.close(); | ||
| throw e; | ||
| } | ||
xiaguan marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
68
to
72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Furthermore, Given the redundancy, the best solution is to remove the processNode(root, "");
xiaguan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| file.close(); | ||
| } | ||
|
|
||
| void DefaultConfig::processNode(const YAML::Node& node, std::string key) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -264,9 +264,20 @@ int Topology::discover(const std::vector<std::string> &filter) { | |||||||
| int Topology::parse(const std::string &topology_json) { | ||||||||
| std::set<std::string> rnic_set; | ||||||||
| Json::Value root; | ||||||||
| Json::Reader reader; | ||||||||
|
|
||||||||
| if (topology_json.empty() || !reader.parse(topology_json, root)) { | ||||||||
| if (topology_json.empty()) { | ||||||||
| return ERR_MALFORMED_JSON; | ||||||||
| } | ||||||||
|
|
||||||||
| // Use thread-safe CharReaderBuilder instead of deprecated Reader | ||||||||
| Json::CharReaderBuilder builder; | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency with other parts of the codebase (e.g.,
Suggested change
|
||||||||
| std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); | ||||||||
| std::string errs; | ||||||||
|
|
||||||||
| if (!reader->parse(topology_json.data(), | ||||||||
| topology_json.data() + topology_json.size(), &root, | ||||||||
| &errs)) { | ||||||||
| LOG(ERROR) << "Topology::parse: JSON parse error: " << errs; | ||||||||
| return ERR_MALFORMED_JSON; | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -303,8 +314,9 @@ std::string Topology::toString() const { | |||||||
|
|
||||||||
| Json::Value Topology::toJson() const { | ||||||||
| Json::Value root; | ||||||||
| Json::Reader reader; | ||||||||
| reader.parse(toString(), root); | ||||||||
| for (const auto &pair : matrix_) { | ||||||||
| root[pair.first] = pair.second.toJson(); | ||||||||
| } | ||||||||
| return root; | ||||||||
| } | ||||||||
|
|
||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,6 +49,21 @@ | |||||||||||||
| #include "config.h" | ||||||||||||||
| #include "error.h" | ||||||||||||||
|
|
||||||||||||||
| // Helper function to parse JSON string using thread-safe CharReaderBuilder | ||||||||||||||
| static bool parseJsonString(const std::string &json_str, Json::Value &value, | ||||||||||||||
| std::string *error_msg = nullptr) { | ||||||||||||||
| Json::CharReaderBuilder builder; | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency with other parts of the codebase (e.g.,
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To maintain consistency with other parts of the codebase (like in
Suggested change
|
||||||||||||||
| std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); | ||||||||||||||
| std::string errs; | ||||||||||||||
|
|
||||||||||||||
| bool success = reader->parse( | ||||||||||||||
| json_str.data(), json_str.data() + json_str.size(), &value, &errs); | ||||||||||||||
| if (!success && error_msg) { | ||||||||||||||
| *error_msg = errs; | ||||||||||||||
| } | ||||||||||||||
| return success; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| namespace mooncake { | ||||||||||||||
| #ifdef USE_REDIS | ||||||||||||||
| struct RedisStoragePlugin : public MetadataStoragePlugin { | ||||||||||||||
|
|
@@ -118,7 +133,6 @@ struct RedisStoragePlugin : public MetadataStoragePlugin { | |||||||||||||
| std::lock_guard<std::mutex> lock(access_client_mutex_); | ||||||||||||||
| if (!client_) return false; | ||||||||||||||
|
|
||||||||||||||
| Json::Reader reader; | ||||||||||||||
| redisReply *resp = | ||||||||||||||
| (redisReply *)redisCommand(client_, "GET %s", key.c_str()); | ||||||||||||||
| if (!resp) { | ||||||||||||||
|
|
@@ -135,7 +149,12 @@ struct RedisStoragePlugin : public MetadataStoragePlugin { | |||||||||||||
|
|
||||||||||||||
| auto json_file = std::string(resp->str); | ||||||||||||||
| freeReplyObject(resp); | ||||||||||||||
| if (!reader.parse(json_file, value)) return false; | ||||||||||||||
|
|
||||||||||||||
| std::string errs; | ||||||||||||||
| if (!parseJsonString(json_file, value, &errs)) { | ||||||||||||||
| LOG(ERROR) << "RedisStoragePlugin: JSON parse error: " << errs; | ||||||||||||||
| return false; | ||||||||||||||
| } | ||||||||||||||
| return true; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -374,15 +393,19 @@ struct EtcdStoragePlugin : public MetadataStoragePlugin { | |||||||||||||
| virtual ~EtcdStoragePlugin() {} | ||||||||||||||
|
|
||||||||||||||
| virtual bool get(const std::string &key, Json::Value &value) { | ||||||||||||||
| Json::Reader reader; | ||||||||||||||
| auto resp = client_.get(key); | ||||||||||||||
| if (!resp.is_ok()) { | ||||||||||||||
| LOG(ERROR) << "EtcdStoragePlugin: unable to get " << key << " from " | ||||||||||||||
| << metadata_uri_ << ": " << resp.error_message(); | ||||||||||||||
| return false; | ||||||||||||||
| } | ||||||||||||||
| auto json_file = resp.value().as_string(); | ||||||||||||||
| if (!reader.parse(json_file, value)) return false; | ||||||||||||||
|
|
||||||||||||||
| std::string errs; | ||||||||||||||
| if (!parseJsonString(json_file, value, &errs)) { | ||||||||||||||
| LOG(ERROR) << "EtcdStoragePlugin: JSON parse error: " << errs; | ||||||||||||||
| return false; | ||||||||||||||
| } | ||||||||||||||
| return true; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -429,7 +452,6 @@ struct EtcdStoragePlugin : public MetadataStoragePlugin { | |||||||||||||
| virtual ~EtcdStoragePlugin() { EtcdCloseWrapper(); } | ||||||||||||||
|
|
||||||||||||||
| virtual bool get(const std::string &key, Json::Value &value) { | ||||||||||||||
| Json::Reader reader; | ||||||||||||||
| char *json_data = nullptr; | ||||||||||||||
| auto ret = EtcdGetWrapper((char *)key.c_str(), &json_data, &err_msg_); | ||||||||||||||
| if (ret) { | ||||||||||||||
|
|
@@ -446,7 +468,12 @@ struct EtcdStoragePlugin : public MetadataStoragePlugin { | |||||||||||||
| auto json_file = std::string(json_data); | ||||||||||||||
| // free the memory allocated by EtcdGetWrapper | ||||||||||||||
| free(json_data); | ||||||||||||||
| if (!reader.parse(json_file, value)) return false; | ||||||||||||||
|
|
||||||||||||||
| std::string errs; | ||||||||||||||
| if (!parseJsonString(json_file, value, &errs)) { | ||||||||||||||
| LOG(ERROR) << "EtcdStoragePlugin: JSON parse error: " << errs; | ||||||||||||||
| return false; | ||||||||||||||
| } | ||||||||||||||
| return true; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -716,16 +743,16 @@ struct SocketHandShakePlugin : public HandShakePlugin { | |||||||||||||
| getNetworkAddress((struct sockaddr *)&addr); | ||||||||||||||
|
|
||||||||||||||
| Json::Value local, peer; | ||||||||||||||
| Json::Reader reader; | ||||||||||||||
|
|
||||||||||||||
| auto [type, json_str] = readString(conn_fd); | ||||||||||||||
| if (!reader.parse(json_str, peer)) { | ||||||||||||||
| LOG(ERROR) << "SocketHandShakePlugin: failed to receive " | ||||||||||||||
| "handshake message, " | ||||||||||||||
| "malformed json format:" | ||||||||||||||
| << reader.getFormattedErrorMessages() | ||||||||||||||
| << ", json string length: " << json_str.size() | ||||||||||||||
| << ", json string content: " << json_str; | ||||||||||||||
| std::string errs; | ||||||||||||||
| if (!parseJsonString(json_str, peer, &errs)) { | ||||||||||||||
| LOG(ERROR) | ||||||||||||||
| << "SocketHandShakePlugin: failed to receive " | ||||||||||||||
| "handshake message, " | ||||||||||||||
| "malformed json format: " | ||||||||||||||
| << errs << ", json string length: " << json_str.size() | ||||||||||||||
| << ", json string content: " << json_str; | ||||||||||||||
| close(conn_fd); | ||||||||||||||
| continue; | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -906,7 +933,6 @@ struct SocketHandShakePlugin : public HandShakePlugin { | |||||||||||||
| return ret; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| Json::Reader reader; | ||||||||||||||
| auto [type, json_str] = readString(conn_fd); | ||||||||||||||
| if (type != HandShakeRequestType::Connection) { | ||||||||||||||
| LOG(ERROR) | ||||||||||||||
|
|
@@ -915,10 +941,11 @@ struct SocketHandShakePlugin : public HandShakePlugin { | |||||||||||||
| return ERR_SOCKET; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (!reader.parse(json_str, peer)) { | ||||||||||||||
| std::string errs; | ||||||||||||||
| if (!parseJsonString(json_str, peer, &errs)) { | ||||||||||||||
| LOG(ERROR) << "SocketHandShakePlugin: failed to receive handshake " | ||||||||||||||
| "message: " | ||||||||||||||
| "malformed json format, check tcp connection"; | ||||||||||||||
| "message: malformed json format: " | ||||||||||||||
| << errs; | ||||||||||||||
| close(conn_fd); | ||||||||||||||
| return ERR_MALFORMED_JSON; | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -981,7 +1008,6 @@ struct SocketHandShakePlugin : public HandShakePlugin { | |||||||||||||
| return ret; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| Json::Reader reader; | ||||||||||||||
| auto [type, json_str] = readString(conn_fd); | ||||||||||||||
| if (type != HandShakeRequestType::Notify) { | ||||||||||||||
| LOG(ERROR) | ||||||||||||||
|
|
@@ -993,10 +1019,11 @@ struct SocketHandShakePlugin : public HandShakePlugin { | |||||||||||||
| // LOG(INFO) << "SocketHandShakePlugin: received metadata message: " | ||||||||||||||
| // << json_str; | ||||||||||||||
|
|
||||||||||||||
| if (!reader.parse(json_str, peer_notify)) { | ||||||||||||||
| std::string errs; | ||||||||||||||
| if (!parseJsonString(json_str, peer_notify, &errs)) { | ||||||||||||||
| LOG(ERROR) << "SocketHandShakePlugin: failed to receive metadata " | ||||||||||||||
| "message, malformed json format: " | ||||||||||||||
| << reader.getFormattedErrorMessages(); | ||||||||||||||
| << errs; | ||||||||||||||
| close(conn_fd); | ||||||||||||||
| return ERR_MALFORMED_JSON; | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -1023,7 +1050,6 @@ struct SocketHandShakePlugin : public HandShakePlugin { | |||||||||||||
| return ret; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| Json::Reader reader; | ||||||||||||||
| auto [type, json_str] = readString(conn_fd); | ||||||||||||||
| if (type != HandShakeRequestType::Metadata) { | ||||||||||||||
| LOG(ERROR) | ||||||||||||||
|
|
@@ -1035,10 +1061,11 @@ struct SocketHandShakePlugin : public HandShakePlugin { | |||||||||||||
| // LOG(INFO) << "SocketHandShakePlugin: received metadata message: " | ||||||||||||||
| // << json_str; | ||||||||||||||
|
|
||||||||||||||
| if (!reader.parse(json_str, peer_metadata)) { | ||||||||||||||
| std::string errs; | ||||||||||||||
| if (!parseJsonString(json_str, peer_metadata, &errs)) { | ||||||||||||||
| LOG(ERROR) << "SocketHandShakePlugin: failed to receive metadata " | ||||||||||||||
| "message, malformed json format: " | ||||||||||||||
| << reader.getFormattedErrorMessages(); | ||||||||||||||
| << errs; | ||||||||||||||
| close(conn_fd); | ||||||||||||||
| return ERR_MALFORMED_JSON; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.