Skip to content

Commit

Permalink
feat(hotkey): add an interface to query hotkey on the specified parti…
Browse files Browse the repository at this point in the history
…tion (#662)
  • Loading branch information
Smityz authored Dec 28, 2020
1 parent 013dd83 commit 51578db
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/server/hotkey_collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ inline void hotkey_collector::change_state_by_result()
case hotkey_collector_state::FINE_DETECTING:
if (!_result.hot_hash_key.empty()) {
change_state_to_finished();
derror_replica("Find the hotkey: {}", _result.hot_hash_key);
}
break;
default:
Expand All @@ -185,6 +186,9 @@ void hotkey_collector::handle_rpc(const dsn::replication::detect_hotkey_request
case dsn::replication::detect_action::STOP:
on_stop_detect(resp);
return;
case dsn::replication::detect_action::QUERY:
query_result(resp);
return;
default:
std::string hint = fmt::format("{}: can't find this detect action", req.action);
resp.err = dsn::ERR_INVALID_STATE;
Expand Down Expand Up @@ -272,6 +276,20 @@ void hotkey_collector::on_stop_detect(dsn::replication::detect_hotkey_response &
ddebug_replica(hint);
}

void hotkey_collector::query_result(dsn::replication::detect_hotkey_response &resp)
{
if (_state != hotkey_collector_state::FINISHED) {
resp.err = dsn::ERR_BUSY;
std::string hint = fmt::format("hotkey is detecting now, now state: {}",
dsn::enum_to_string(_hotkey_type));
resp.__set_err_hint(hint);
ddebug_replica(hint);
} else {
resp.err = dsn::ERR_OK;
resp.__set_hotkey_result(_result.hot_hash_key);
}
}

bool hotkey_collector::terminate_if_timeout()
{
if (dsn_now_s() >= _collector_start_time_second.load() + FLAGS_max_seconds_to_detect_hotkey) {
Expand Down
1 change: 1 addition & 0 deletions src/server/hotkey_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class hotkey_collector : public dsn::replication::replica_base
private:
void on_start_detect(dsn::replication::detect_hotkey_response &resp);
void on_stop_detect(dsn::replication::detect_hotkey_response &resp);
void query_result(dsn::replication::detect_hotkey_response &resp);

void change_state_to_stopped();
void change_state_to_coarse_detecting();
Expand Down
3 changes: 2 additions & 1 deletion src/server/pegasus_server_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2841,7 +2841,8 @@ void pegasus_server_impl::on_detect_hotkey(const dsn::replication::detect_hotkey
{

if (dsn_unlikely(req.action != dsn::replication::detect_action::START &&
req.action != dsn::replication::detect_action::STOP)) {
req.action != dsn::replication::detect_action::STOP &&
req.action != dsn::replication::detect_action::QUERY)) {
resp.err = dsn::ERR_INVALID_PARAMETERS;
resp.__set_err_hint("invalid detect_action");
return;
Expand Down
6 changes: 6 additions & 0 deletions src/server/test/hotkey_collector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ TEST_F(hotkey_collector_test, state_transform)
ASSERT_TRUE(result->if_find_result);
ASSERT_EQ(result->hot_hash_key, "ThisisahotkeyThisisahotkey");

on_detect_hotkey(generate_control_rpc(dsn::replication::hotkey_type::READ,
dsn::replication::detect_action::QUERY),
resp);
ASSERT_EQ(resp.err, dsn::ERR_OK);
ASSERT_EQ(resp.hotkey_result, "ThisisahotkeyThisisahotkey");

on_detect_hotkey(generate_control_rpc(dsn::replication::hotkey_type::READ,
dsn::replication::detect_action::STOP),
resp);
Expand Down

0 comments on commit 51578db

Please sign in to comment.