Skip to content

Commit 23fe429

Browse files
branch-4.0: [chore](cloud) Print new value json when set_value #57385 (#57459)
Cherry-picked from #57385 Co-authored-by: Gavin Chou <[email protected]>
1 parent 642b5e5 commit 23fe429

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

cloud/src/meta-service/http_encode_key.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <google/protobuf/util/json_util.h>
2424

2525
#include <bit>
26+
#include <chrono>
2627
#include <fstream>
2728
#include <iomanip>
2829
#include <sstream>
@@ -240,15 +241,16 @@ static std::unordered_map<std::string_view,
240241
};
241242
// clang-format on
242243

243-
static MetaServiceResponseStatus encode_key(const brpc::URI& uri, std::string& key) {
244+
static MetaServiceResponseStatus encode_key(const brpc::URI& uri, std::string* key,
245+
std::string* key_type = nullptr) {
244246
MetaServiceResponseStatus status;
245247
status.set_code(MetaServiceCode::OK);
246-
std::string_view key_type = http_query(uri, "key_type");
247-
auto it = param_set.find(key_type);
248+
std::string_view kt = http_query(uri, "key_type");
249+
if (key_type != nullptr) *key_type = kt;
250+
auto it = param_set.find(kt);
248251
if (it == param_set.end()) {
249252
status.set_code(MetaServiceCode::INVALID_ARGUMENT);
250-
status.set_msg(fmt::format("key_type not supported: {}",
251-
(key_type.empty() ? "(empty)" : key_type)));
253+
status.set_msg(fmt::format("key_type not supported: {}", (kt.empty() ? "(empty)" : kt)));
252254
return status;
253255
}
254256
auto& key_params = std::get<0>(it->second);
@@ -264,16 +266,18 @@ static MetaServiceResponseStatus encode_key(const brpc::URI& uri, std::string& k
264266
params.emplace_back(p);
265267
}
266268
auto& key_encoding_function = std::get<1>(it->second);
267-
key = key_encoding_function(params);
269+
*key = key_encoding_function(params);
268270
return status;
269271
}
270272

271273
HttpResponse process_http_get_value(TxnKv* txn_kv, const brpc::URI& uri) {
272274
std::string key;
275+
std::string key_type;
273276
if (auto hex_key = http_query(uri, "key"); !hex_key.empty()) {
274277
key = unhex(hex_key);
278+
key_type = http_query(uri, "key_type");
275279
} else { // Encode key from params
276-
auto st = encode_key(uri, key);
280+
auto st = encode_key(uri, &key, &key_type);
277281
if (st.code() != MetaServiceCode::OK) {
278282
return http_json_reply(st);
279283
}
@@ -285,7 +289,6 @@ HttpResponse process_http_get_value(TxnKv* txn_kv, const brpc::URI& uri) {
285289
fmt::format("failed to create txn, err={}", err));
286290
}
287291

288-
std::string_view key_type = http_query(uri, "key_type");
289292
auto it = param_set.find(key_type);
290293
if (it == param_set.end()) {
291294
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT,
@@ -330,17 +333,20 @@ HttpResponse process_http_get_value(TxnKv* txn_kv, const brpc::URI& uri) {
330333
}
331334

332335
std::string handle_kv_output(std::string_view key, std::string_view value,
333-
std::string_view original_value_json,
336+
std::string_view original_value_json, std::string_view new_value_json,
334337
std::string_view serialized_value_to_save) {
335338
std::stringstream final_output;
336-
final_output << "original_value_hex=" << hex(value) << "\n"
337-
<< "key_hex=" << hex(key) << "\n"
339+
final_output << "key_hex=" << hex(key) << "\n"
338340
<< "original_value_json=" << original_value_json << "\n"
339-
<< "changed_value_hex=" << hex(serialized_value_to_save) << "\n";
341+
<< "new_value_json=" << new_value_json << "\n"
342+
<< "original_value_hex=" << hex(value) << "\n"
343+
<< "new_value_hex=" << hex(serialized_value_to_save) << "\n";
340344
std::string final_json_str = final_output.str();
341345
LOG(INFO) << final_json_str;
342346
if (final_json_str.size() > 25000) {
343-
std::string file_path = fmt::format("/tmp/{}.txt", hex(key));
347+
using namespace std::chrono;
348+
auto ts = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
349+
std::string file_path = fmt::format("/tmp/{}_{}.txt", hex(key), ts);
344350
LOG(INFO) << "write to file=" << file_path << ", key=" << hex(key)
345351
<< " size=" << final_json_str.size();
346352
try {
@@ -350,7 +356,7 @@ std::string handle_kv_output(std::string_view key, std::string_view value,
350356
kv_file.close();
351357
}
352358
} catch (...) {
353-
LOG(INFO) << "write tmp file failed.";
359+
LOG(INFO) << "write tmp file failed: " << file_path;
354360
}
355361
}
356362

@@ -363,10 +369,12 @@ HttpResponse process_http_set_value(TxnKv* txn_kv, brpc::Controller* cntl) {
363369
LOG(INFO) << "set value, body=" << body;
364370

365371
std::string key;
372+
std::string key_type;
366373
if (auto hex_key = http_query(uri, "key"); !hex_key.empty()) {
367374
key = unhex(hex_key);
375+
key_type = http_query(uri, "key_type");
368376
} else { // Encode key from params
369-
auto st = encode_key(uri, key);
377+
auto st = encode_key(uri, &key, &key_type);
370378
if (st.code() != MetaServiceCode::OK) {
371379
return http_json_reply(st);
372380
}
@@ -378,13 +386,13 @@ HttpResponse process_http_set_value(TxnKv* txn_kv, brpc::Controller* cntl) {
378386
fmt::format("failed to create txn, err={}", err));
379387
}
380388

381-
std::string_view key_type = http_query(uri, "key_type");
382389
auto it = param_set.find(key_type);
383390
if (it == param_set.end()) {
384391
return http_json_reply(MetaServiceCode::INVALID_ARGUMENT,
385392
fmt::format("key_type not supported: {}",
386393
(key_type.empty() ? "(empty)" : key_type)));
387394
}
395+
388396
auto& json_parsing_function = std::get<3>(it->second);
389397
std::shared_ptr<google::protobuf::Message> pb_to_save = json_parsing_function(body);
390398
if (pb_to_save == nullptr) {
@@ -458,14 +466,15 @@ HttpResponse process_http_set_value(TxnKv* txn_kv, brpc::Controller* cntl) {
458466
LOG(WARNING) << "set_value saved, key=" << hex(key);
459467

460468
std::string final_json_str =
461-
handle_kv_output(key, value.value(), original_value_json, serialized_value_to_save);
469+
handle_kv_output(key, value.value(), original_value_json, proto_to_json(*pb_to_save),
470+
serialized_value_to_save);
462471

463472
return http_text_reply(MetaServiceCode::OK, "", final_json_str);
464473
}
465474

466475
HttpResponse process_http_encode_key(const brpc::URI& uri) {
467476
std::string key;
468-
auto st = encode_key(uri, key);
477+
auto st = encode_key(uri, &key);
469478
if (st.code() != MetaServiceCode::OK) {
470479
return http_json_reply(st);
471480
}

cloud/test/http_encode_key_test.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ struct Input {
116116
std::vector<std::string> key;
117117
std::function<std::vector<std::string>()> gen_value;
118118
std::string_view value;
119+
120+
std::string to_string() {
121+
std::stringstream ss;
122+
ss << "key_type=" << key_type << " param=" << param << " key=" << key[0]
123+
<< " value=" << value;
124+
return ss.str();
125+
}
119126
};
120127

121128
// clang-format off
@@ -618,15 +625,15 @@ TEST(HttpGetValueTest, process_http_get_value_test_cover_all_template) {
618625
// std::cout << url << std::endl;
619626
ASSERT_EQ(uri.SetHttpURL(url), 0); // clear and set query string
620627
auto http_res = process_http_get_value(txn_kv.get(), uri);
621-
EXPECT_EQ(http_res.status_code, 200);
628+
EXPECT_EQ(http_res.status_code, 200) << url << " " << input.to_string();
622629
// std::cout << http_res.body << std::endl;
623630
EXPECT_EQ(http_res.body, input.value);
624631
// Key mode
625632
url = gen_url(input, false);
626633
// std::cout << url << std::endl;
627634
ASSERT_EQ(uri.SetHttpURL(url), 0); // clear and set query string
628635
http_res = process_http_get_value(txn_kv.get(), uri);
629-
EXPECT_EQ(http_res.status_code, 200);
636+
EXPECT_EQ(http_res.status_code, 200) << url << " " << input.to_string();
630637
// std::cout << http_res.body << std::endl;
631638
EXPECT_EQ(http_res.body, input.value);
632639
}

cloud/test/meta_service_http_test.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,10 +2096,11 @@ TEST(HttpEncodeKeyTest, ProcessHttpSetValue) {
20962096
auto response = process_http_set_value(txn_kv.get(), &cntl);
20972097
EXPECT_EQ(response.status_code, 200) << response.msg;
20982098
std::stringstream final_json;
2099-
final_json << "original_value_hex=" << hex(initial_rowset_meta.SerializeAsString()) << "\n"
2100-
<< "key_hex=" << hex(initial_key) << "\n"
2099+
final_json << "key_hex=" << hex(initial_key) << "\n"
21012100
<< "original_value_json=" << proto_to_json(initial_rowset_meta) << "\n"
2102-
<< "changed_value_hex=" << hex(new_rowset_meta.SerializeAsString()) << "\n";
2101+
<< "new_value_json=" << proto_to_json(new_rowset_meta) << "\n"
2102+
<< "original_value_hex=" << hex(initial_rowset_meta.SerializeAsString()) << "\n"
2103+
<< "new_value_hex=" << hex(new_rowset_meta.SerializeAsString()) << "\n";
21032104
// std::cout << "xxx " << final_json.str() << std::endl;
21042105
EXPECT_EQ(response.body, final_json.str());
21052106

0 commit comments

Comments
 (0)