diff --git a/cloud/src/meta-service/http_encode_key.cpp b/cloud/src/meta-service/http_encode_key.cpp index cae6fb322f7fb4..4ee76af5c9f4e0 100644 --- a/cloud/src/meta-service/http_encode_key.cpp +++ b/cloud/src/meta-service/http_encode_key.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -240,15 +241,16 @@ static std::unordered_map(it->second); @@ -264,16 +266,18 @@ static MetaServiceResponseStatus encode_key(const brpc::URI& uri, std::string& k params.emplace_back(p); } auto& key_encoding_function = std::get<1>(it->second); - key = key_encoding_function(params); + *key = key_encoding_function(params); return status; } HttpResponse process_http_get_value(TxnKv* txn_kv, const brpc::URI& uri) { std::string key; + std::string key_type; if (auto hex_key = http_query(uri, "key"); !hex_key.empty()) { key = unhex(hex_key); + key_type = http_query(uri, "key_type"); } else { // Encode key from params - auto st = encode_key(uri, key); + auto st = encode_key(uri, &key, &key_type); if (st.code() != MetaServiceCode::OK) { return http_json_reply(st); } @@ -285,7 +289,6 @@ HttpResponse process_http_get_value(TxnKv* txn_kv, const brpc::URI& uri) { fmt::format("failed to create txn, err={}", err)); } - std::string_view key_type = http_query(uri, "key_type"); auto it = param_set.find(key_type); if (it == param_set.end()) { return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, @@ -330,17 +333,20 @@ HttpResponse process_http_get_value(TxnKv* txn_kv, const brpc::URI& uri) { } std::string handle_kv_output(std::string_view key, std::string_view value, - std::string_view original_value_json, + std::string_view original_value_json, std::string_view new_value_json, std::string_view serialized_value_to_save) { std::stringstream final_output; - final_output << "original_value_hex=" << hex(value) << "\n" - << "key_hex=" << hex(key) << "\n" + final_output << "key_hex=" << hex(key) << "\n" << "original_value_json=" << original_value_json << "\n" - << "changed_value_hex=" << hex(serialized_value_to_save) << "\n"; + << "new_value_json=" << new_value_json << "\n" + << "original_value_hex=" << hex(value) << "\n" + << "new_value_hex=" << hex(serialized_value_to_save) << "\n"; std::string final_json_str = final_output.str(); LOG(INFO) << final_json_str; if (final_json_str.size() > 25000) { - std::string file_path = fmt::format("/tmp/{}.txt", hex(key)); + using namespace std::chrono; + auto ts = duration_cast(system_clock::now().time_since_epoch()).count(); + std::string file_path = fmt::format("/tmp/{}_{}.txt", hex(key), ts); LOG(INFO) << "write to file=" << file_path << ", key=" << hex(key) << " size=" << final_json_str.size(); try { @@ -350,7 +356,7 @@ std::string handle_kv_output(std::string_view key, std::string_view value, kv_file.close(); } } catch (...) { - LOG(INFO) << "write tmp file failed."; + LOG(INFO) << "write tmp file failed: " << file_path; } } @@ -363,10 +369,12 @@ HttpResponse process_http_set_value(TxnKv* txn_kv, brpc::Controller* cntl) { LOG(INFO) << "set value, body=" << body; std::string key; + std::string key_type; if (auto hex_key = http_query(uri, "key"); !hex_key.empty()) { key = unhex(hex_key); + key_type = http_query(uri, "key_type"); } else { // Encode key from params - auto st = encode_key(uri, key); + auto st = encode_key(uri, &key, &key_type); if (st.code() != MetaServiceCode::OK) { return http_json_reply(st); } @@ -378,13 +386,13 @@ HttpResponse process_http_set_value(TxnKv* txn_kv, brpc::Controller* cntl) { fmt::format("failed to create txn, err={}", err)); } - std::string_view key_type = http_query(uri, "key_type"); auto it = param_set.find(key_type); if (it == param_set.end()) { return http_json_reply(MetaServiceCode::INVALID_ARGUMENT, fmt::format("key_type not supported: {}", (key_type.empty() ? "(empty)" : key_type))); } + auto& json_parsing_function = std::get<3>(it->second); std::shared_ptr pb_to_save = json_parsing_function(body); if (pb_to_save == nullptr) { @@ -458,14 +466,15 @@ HttpResponse process_http_set_value(TxnKv* txn_kv, brpc::Controller* cntl) { LOG(WARNING) << "set_value saved, key=" << hex(key); std::string final_json_str = - handle_kv_output(key, value.value(), original_value_json, serialized_value_to_save); + handle_kv_output(key, value.value(), original_value_json, proto_to_json(*pb_to_save), + serialized_value_to_save); return http_text_reply(MetaServiceCode::OK, "", final_json_str); } HttpResponse process_http_encode_key(const brpc::URI& uri) { std::string key; - auto st = encode_key(uri, key); + auto st = encode_key(uri, &key); if (st.code() != MetaServiceCode::OK) { return http_json_reply(st); } diff --git a/cloud/test/http_encode_key_test.cpp b/cloud/test/http_encode_key_test.cpp index d38351b6ec6163..de1ffe42ffe94e 100644 --- a/cloud/test/http_encode_key_test.cpp +++ b/cloud/test/http_encode_key_test.cpp @@ -116,6 +116,13 @@ struct Input { std::vector key; std::function()> gen_value; std::string_view value; + + std::string to_string() { + std::stringstream ss; + ss << "key_type=" << key_type << " param=" << param << " key=" << key[0] + << " value=" << value; + return ss.str(); + } }; // clang-format off @@ -618,7 +625,7 @@ TEST(HttpGetValueTest, process_http_get_value_test_cover_all_template) { // std::cout << url << std::endl; ASSERT_EQ(uri.SetHttpURL(url), 0); // clear and set query string auto http_res = process_http_get_value(txn_kv.get(), uri); - EXPECT_EQ(http_res.status_code, 200); + EXPECT_EQ(http_res.status_code, 200) << url << " " << input.to_string(); // std::cout << http_res.body << std::endl; EXPECT_EQ(http_res.body, input.value); // Key mode @@ -626,7 +633,7 @@ TEST(HttpGetValueTest, process_http_get_value_test_cover_all_template) { // std::cout << url << std::endl; ASSERT_EQ(uri.SetHttpURL(url), 0); // clear and set query string http_res = process_http_get_value(txn_kv.get(), uri); - EXPECT_EQ(http_res.status_code, 200); + EXPECT_EQ(http_res.status_code, 200) << url << " " << input.to_string(); // std::cout << http_res.body << std::endl; EXPECT_EQ(http_res.body, input.value); } diff --git a/cloud/test/meta_service_http_test.cpp b/cloud/test/meta_service_http_test.cpp index 302dccf7b600b2..8ae106bc861ae8 100644 --- a/cloud/test/meta_service_http_test.cpp +++ b/cloud/test/meta_service_http_test.cpp @@ -2096,10 +2096,11 @@ TEST(HttpEncodeKeyTest, ProcessHttpSetValue) { auto response = process_http_set_value(txn_kv.get(), &cntl); EXPECT_EQ(response.status_code, 200) << response.msg; std::stringstream final_json; - final_json << "original_value_hex=" << hex(initial_rowset_meta.SerializeAsString()) << "\n" - << "key_hex=" << hex(initial_key) << "\n" + final_json << "key_hex=" << hex(initial_key) << "\n" << "original_value_json=" << proto_to_json(initial_rowset_meta) << "\n" - << "changed_value_hex=" << hex(new_rowset_meta.SerializeAsString()) << "\n"; + << "new_value_json=" << proto_to_json(new_rowset_meta) << "\n" + << "original_value_hex=" << hex(initial_rowset_meta.SerializeAsString()) << "\n" + << "new_value_hex=" << hex(new_rowset_meta.SerializeAsString()) << "\n"; // std::cout << "xxx " << final_json.str() << std::endl; EXPECT_EQ(response.body, final_json.str());