Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
feat: update query partition data version (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
hycdong authored Dec 28, 2020
1 parent 57e7a34 commit 1fd9cd5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
19 changes: 15 additions & 4 deletions src/replica/replica_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int32_t, uint32_t> 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();
Expand Down
19 changes: 7 additions & 12 deletions src/replica/replica_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int32_t, uint32_t> &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(
Expand Down
4 changes: 3 additions & 1 deletion src/replica/replica_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ class replica_stub : public serverlet<replica_stub>, 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<int32_t, uint32_t> &version_map);

#ifdef DSN_ENABLE_GPERF
// Try to release tcmalloc memory back to operating system
Expand Down
6 changes: 2 additions & 4 deletions src/replica/test/replica_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1fd9cd5

Please sign in to comment.