From 1fd9cd57117de8dc67d42f6b8a5fb05ea82e122f Mon Sep 17 00:00:00 2001 From: HeYuchen <377710264@qq.com> Date: Mon, 28 Dec 2020 15:39:56 +0800 Subject: [PATCH] feat: update query partition data version (#713) --- src/replica/replica_http_service.cpp | 19 +++++++++++++++---- src/replica/replica_stub.cpp | 19 +++++++------------ src/replica/replica_stub.h | 4 +++- src/replica/test/replica_test.cpp | 6 ++---- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/replica/replica_http_service.cpp b/src/replica/replica_http_service.cpp index d4b985eb03..7078cd597a 100644 --- a/src/replica/replica_http_service.cpp +++ b/src/replica/replica_http_service.cpp @@ -73,12 +73,23 @@ void replica_http_service::query_app_data_version_handler(const http_request &re return; } - uint32_t data_version = 0; - error_code ec = _stub->query_app_data_version(app_id, data_version); + // partition_index -> data_version + std::unordered_map version_map; + _stub->query_app_data_version(app_id, version_map); + + if (version_map.size() == 0) { + resp.body = fmt::format("app_id={} not found", it->second); + resp.status_code = http_status_code::not_found; + return; + } dsn::utils::table_printer tp; - tp.add_row_name_and_data("error", ec.to_string()); - tp.add_row_name_and_data("data_version", data_version); + tp.add_title("pidx"); + tp.add_column("data_version"); + for (const auto &kv : version_map) { + tp.add_row(kv.first); + tp.append_data(kv.second); + } std::ostringstream out; tp.output(out, dsn::utils::table_printer::output_format::kJsonCompact); resp.body = out.str(); diff --git a/src/replica/replica_stub.cpp b/src/replica/replica_stub.cpp index 174320e188..f9585da6ec 100644 --- a/src/replica/replica_stub.cpp +++ b/src/replica/replica_stub.cpp @@ -2808,24 +2808,19 @@ void replica_stub::on_detect_hotkey(detect_hotkey_rpc rpc) } } -error_code replica_stub::query_app_data_version(int32_t app_id, /*out*/ uint32_t &data_version) +void replica_stub::query_app_data_version( + int32_t app_id, /*pidx => data_version*/ std::unordered_map &version_map) { - replica_ptr rep = nullptr; zauto_read_lock l(_replicas_lock); for (const auto &kv : _replicas) { if (kv.first.get_app_id() == app_id) { - rep = kv.second; - break; + replica_ptr rep = kv.second; + if (rep != nullptr) { + uint32_t data_version = rep->query_data_version(); + version_map[kv.first.get_partition_index()] = data_version; + } } } - if (rep == nullptr) { - dwarn_f("app({}) is not found", app_id); - return ERR_OBJECT_NOT_FOUND; - } - - data_version = rep->query_data_version(); - ddebug_f("app({}) data_version={}", app_id, data_version); - return ERR_OK; } void replica_stub::query_app_compact_status( diff --git a/src/replica/replica_stub.h b/src/replica/replica_stub.h index 66417ff2ee..1e6ac91644 100644 --- a/src/replica/replica_stub.h +++ b/src/replica/replica_stub.h @@ -289,7 +289,9 @@ class replica_stub : public serverlet, public ref_counter return 0; } - error_code query_app_data_version(int32_t app_id, /*out*/ uint32_t &data_version); + void query_app_data_version( + int32_t app_id, + /*pidx => data_version*/ std::unordered_map &version_map); #ifdef DSN_ENABLE_GPERF // Try to release tcmalloc memory back to operating system diff --git a/src/replica/test/replica_test.cpp b/src/replica/test/replica_test.cpp index 9157100552..f3df84669a 100644 --- a/src/replica/test/replica_test.cpp +++ b/src/replica/test/replica_test.cpp @@ -95,10 +95,8 @@ TEST_F(replica_test, query_data_version_test) {"wrong", http_status_code::bad_request, "invalid app_id=wrong"}, {"2", http_status_code::ok, - R"({"error":"ERR_OK","data_version":"1"})"}, - {"4", - http_status_code::ok, - R"({"error":"ERR_OBJECT_NOT_FOUND","data_version":"0"})"}}; + R"({"1":{"pidx":"1","data_version":"1"}})"}, + {"4", http_status_code::not_found, "app_id=4 not found"}}; for (const auto &test : tests) { http_request req; http_response resp;