From 03109eb57d1d4c62e0a23e21374159f8698cd2fc Mon Sep 17 00:00:00 2001 From: heyuchen Date: Mon, 28 Dec 2020 12:59:51 +0800 Subject: [PATCH 1/2] feat: update query partition data version --- src/replica/replica_http_service.cpp | 19 +++++++++++++++---- src/replica/replica_stub.cpp | 20 ++++++++------------ src/replica/replica_stub.h | 3 ++- 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..fdccea2633 100644 --- a/src/replica/replica_stub.cpp +++ b/src/replica/replica_stub.cpp @@ -2808,24 +2808,20 @@ 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, + 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; + ddebug_f("partition[{}] data_version = {}", kv.first, 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..d783047e5a 100644 --- a/src/replica/replica_stub.h +++ b/src/replica/replica_stub.h @@ -289,7 +289,8 @@ 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, + /*out*/ 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; From abe8d75e46597d4470e13082385407da7aa287f1 Mon Sep 17 00:00:00 2001 From: heyuchen Date: Mon, 28 Dec 2020 14:27:57 +0800 Subject: [PATCH 2/2] update by code review --- src/replica/replica_stub.cpp | 5 ++--- src/replica/replica_stub.h | 5 +++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/replica/replica_stub.cpp b/src/replica/replica_stub.cpp index fdccea2633..f9585da6ec 100644 --- a/src/replica/replica_stub.cpp +++ b/src/replica/replica_stub.cpp @@ -2808,8 +2808,8 @@ void replica_stub::on_detect_hotkey(detect_hotkey_rpc rpc) } } -void replica_stub::query_app_data_version(int32_t app_id, - std::unordered_map &version_map) +void replica_stub::query_app_data_version( + int32_t app_id, /*pidx => data_version*/ std::unordered_map &version_map) { zauto_read_lock l(_replicas_lock); for (const auto &kv : _replicas) { @@ -2818,7 +2818,6 @@ void replica_stub::query_app_data_version(int32_t app_id, if (rep != nullptr) { uint32_t data_version = rep->query_data_version(); version_map[kv.first.get_partition_index()] = data_version; - ddebug_f("partition[{}] data_version = {}", kv.first, data_version); } } } diff --git a/src/replica/replica_stub.h b/src/replica/replica_stub.h index d783047e5a..1e6ac91644 100644 --- a/src/replica/replica_stub.h +++ b/src/replica/replica_stub.h @@ -289,8 +289,9 @@ class replica_stub : public serverlet, public ref_counter return 0; } - void query_app_data_version(int32_t app_id, - /*out*/ std::unordered_map &version_map); + 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