Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hotkey): collector can be terminated by timeout #625

Merged
merged 15 commits into from
Oct 22, 2020
Merged
38 changes: 34 additions & 4 deletions src/server/hotkey_collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@
#include <dsn/utility/smart_pointers.h>
#include "base/pegasus_key_schema.h"
#include <dsn/dist/fmt_logging.h>
#include <dsn/utility/flags.h>

namespace pegasus {
namespace server {

DSN_DEFINE_int32(
"pegasus.server",
hotkey_collector_max_work_time,
Smityz marked this conversation as resolved.
Show resolved Hide resolved
150,
"the max time (in seconds) allowed to capture hotkey, will stop if hotkey's not found");

hotkey_collector::hotkey_collector(dsn::replication::hotkey_type::type hotkey_type,
dsn::replication::replica_base *r_base)
: replica_base(r_base),
_state(hotkey_collector_state::STOPPED),
_hotkey_type(hotkey_type),
_internal_collector(std::make_shared<hotkey_empty_data_collector>())
_internal_collector(std::make_shared<hotkey_empty_data_collector>()),
_collector_start_time(0)
{
}

Expand Down Expand Up @@ -65,7 +73,11 @@ void hotkey_collector::capture_hash_key(const dsn::blob &hash_key, int64_t weigh
_internal_collector->capture_data(hash_key, weight);
}

void hotkey_collector::analyse_data() { _internal_collector->analyse_data(); }
void hotkey_collector::analyse_data()
{
terminate_by_timeout();
_internal_collector->analyse_data();
Smityz marked this conversation as resolved.
Show resolved Hide resolved
}

void hotkey_collector::on_start_detect(dsn::replication::detect_hotkey_response &resp)
{
Expand All @@ -88,6 +100,7 @@ void hotkey_collector::on_start_detect(dsn::replication::detect_hotkey_response
dwarn_replica(hint);
return;
case hotkey_collector_state::STOPPED:
_collector_start_time = dsn_now_s();
hycdong marked this conversation as resolved.
Show resolved Hide resolved
// TODO: (Tangyanzhao) start coarse detecting
_state.store(hotkey_collector_state::COARSE_DETECTING);
resp.err = dsn::ERR_OK;
Expand All @@ -105,13 +118,30 @@ void hotkey_collector::on_start_detect(dsn::replication::detect_hotkey_response

void hotkey_collector::on_stop_detect(dsn::replication::detect_hotkey_response &resp)
{
_state.store(hotkey_collector_state::STOPPED);
_internal_collector.reset();
terminate_colletor();
resp.err = dsn::ERR_OK;
std::string hint =
fmt::format("{} hotkey stopped, cache cleared", dsn::enum_to_string(_hotkey_type));
ddebug_replica(hint);
}

void hotkey_collector::terminate_colletor()
Smityz marked this conversation as resolved.
Show resolved Hide resolved
{
_state.store(hotkey_collector_state::STOPPED);
_internal_collector.reset();
_collector_start_time = 0;
}

void hotkey_collector::terminate_by_timeout()
{
if (_collector_start_time == 0)
return;
Smityz marked this conversation as resolved.
Show resolved Hide resolved
if (dsn_now_s() - _collector_start_time >= FLAGS_hotkey_collector_max_work_time) {
Smityz marked this conversation as resolved.
Show resolved Hide resolved
ddebug_replica("hotkey collector work time is exhausted but no hotkey has been found");
terminate_colletor();
return;
}
};

} // namespace server
} // namespace pegasus
4 changes: 4 additions & 0 deletions src/server/hotkey_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ 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 terminate_colletor();
void terminate_by_timeout();
acelyc111 marked this conversation as resolved.
Show resolved Hide resolved

std::atomic<hotkey_collector_state> _state;
const dsn::replication::hotkey_type::type _hotkey_type;
std::shared_ptr<internal_collector_base> _internal_collector;
uint64_t _collector_start_time;
Smityz marked this conversation as resolved.
Show resolved Hide resolved
};

class internal_collector_base
Expand Down