-
Notifications
You must be signed in to change notification settings - Fork 312
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): capture data part2 - declare coarse collector #624
Changes from 18 commits
97dd0e0
32e2a90
feb4a92
be94413
43045c3
2064167
649d1df
cd819ee
ff7c6fc
4d5cbd6
9b82ac9
b2037b1
7fb43ea
136c9b2
b5480a2
c7b6543
128dc38
21d3ab1
6cf069c
fb4d1b1
6a6f941
08ebf7f
084a6a4
eeda66c
9f9cb08
5301eac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,18 +19,90 @@ | |
|
||
#include <dsn/dist/replication/replication_enums.h> | ||
#include <dsn/utility/smart_pointers.h> | ||
#include <dsn/utility/flags.h> | ||
#include <boost/functional/hash.hpp> | ||
#include "base/pegasus_key_schema.h" | ||
#include <dsn/dist/fmt_logging.h> | ||
|
||
namespace pegasus { | ||
namespace server { | ||
|
||
DSN_DEFINE_int32("pegasus.server", | ||
coarse_data_variance_threshold, | ||
3, | ||
"the threshold of variance calculate to find the outliers"); | ||
|
||
DSN_DEFINE_validator(coarse_data_variance_threshold, | ||
[](int32_t threshold) -> bool { return (threshold >= 0); }); | ||
|
||
DSN_DEFINE_int32("pegasus.server", | ||
data_capture_hash_bucket_num, | ||
37, | ||
"the number of data capture hash buckets"); | ||
Smityz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
DSN_DEFINE_validator(data_capture_hash_bucket_num, [](int32_t bucket_num) -> bool { | ||
if (bucket_num < 3) { | ||
return false; | ||
} | ||
// data_capture_hash_bucket_num should be a prime number | ||
for (int i = 2; i <= bucket_num / i; i++) { | ||
if (bucket_num % i == 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
|
||
// 68–95–99.7 rule, same algorithm as hotspot_partition_calculator::stat_histories_analyse | ||
static bool | ||
find_outlier_index(const std::vector<uint64_t> &captured_keys, int threshold, int &hot_index) | ||
hycdong marked this conversation as resolved.
Show resolved
Hide resolved
Smityz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
dcheck_gt(captured_keys.size(), 2); | ||
int data_size = captured_keys.size(); | ||
// empirical rule to calculate hot point of each partition | ||
// same algorithm as hotspot_partition_calculator::stat_histories_analyse | ||
double table_captured_key_sum = 0; | ||
int hot_value = 0; | ||
for (int i = 0; i < data_size; i++) { | ||
table_captured_key_sum += captured_keys[i]; | ||
if (captured_keys[i] > hot_value) { | ||
hot_index = i; | ||
hot_value = captured_keys[i]; | ||
} | ||
} | ||
// TODO: (Tangyanzhao) increase a judgment of table_captured_key_sum | ||
double captured_keys_avg_count = | ||
(table_captured_key_sum - captured_keys[hot_index]) / (data_size - 1); | ||
double standard_deviation = 0; | ||
for (int i = 0; i < data_size; i++) { | ||
if (i != hot_index) { | ||
standard_deviation += pow((captured_keys[i] - captured_keys_avg_count), 2); | ||
} | ||
} | ||
standard_deviation = sqrt(standard_deviation / (data_size - 2)); | ||
hycdong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
double hot_point = (hot_value - captured_keys_avg_count) / standard_deviation; | ||
detect_hotkey_result result; | ||
Smityz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (hot_point >= threshold) { | ||
result.coarse_bucket_index = hot_index; | ||
return true; | ||
} else { | ||
hot_index = -1; | ||
return false; | ||
} | ||
} | ||
|
||
static int get_bucket_id(dsn::string_view data) | ||
{ | ||
size_t hash_value = boost::hash_range(data.begin(), data.end()); | ||
return static_cast<int>(hash_value % FLAGS_data_capture_hash_bucket_num); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calculate hash value of every key may be wastly, is it possible to use https://github.com/XiaoMi/rdsn/blob/master/src/dsn.layer2.thrift#L92 directly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the current design, it can't get this info There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember to improve it #628 |
||
} | ||
|
||
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>(this)) | ||
{ | ||
} | ||
|
||
|
@@ -65,7 +137,20 @@ 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() | ||
{ | ||
switch (_state.load()) { | ||
case hotkey_collector_state::COARSE_DETECTING: | ||
_internal_collector->analyse_data(_result); | ||
if (_result.coarse_bucket_index != -1) { | ||
// TODO: (Tangyanzhao) reset _internal_collector to hotkey_fine_data_collector | ||
_state.store(hotkey_collector_state::FINE_DETECTING); | ||
} | ||
return; | ||
default: | ||
return; | ||
} | ||
} | ||
|
||
void hotkey_collector::on_start_detect(dsn::replication::detect_hotkey_response &resp) | ||
{ | ||
|
@@ -88,7 +173,7 @@ void hotkey_collector::on_start_detect(dsn::replication::detect_hotkey_response | |
dwarn_replica(hint); | ||
return; | ||
case hotkey_collector_state::STOPPED: | ||
// TODO: (Tangyanzhao) start coarse detecting | ||
_internal_collector.reset(new hotkey_coarse_data_collector(this)); | ||
_state.store(hotkey_collector_state::COARSE_DETECTING); | ||
resp.err = dsn::ERR_OK; | ||
hint = fmt::format("starting to detect {} hotkey", dsn::enum_to_string(_hotkey_type)); | ||
|
@@ -113,5 +198,31 @@ void hotkey_collector::on_stop_detect(dsn::replication::detect_hotkey_response & | |
ddebug_replica(hint); | ||
} | ||
|
||
hotkey_coarse_data_collector::hotkey_coarse_data_collector(replica_base *base) | ||
: internal_collector_base(base), _hash_buckets(FLAGS_data_capture_hash_bucket_num) | ||
{ | ||
for (auto &bucket : _hash_buckets) { | ||
bucket.store(0); | ||
} | ||
} | ||
|
||
void hotkey_coarse_data_collector::capture_data(const dsn::blob &hash_key, uint64_t weight) | ||
{ | ||
_hash_buckets[get_bucket_id(hash_key)].fetch_add(weight); | ||
} | ||
|
||
void hotkey_coarse_data_collector::analyse_data(detect_hotkey_result &result) | ||
{ | ||
std::vector<uint64_t> buckets(FLAGS_data_capture_hash_bucket_num); | ||
for (int i = 0; i < buckets.size(); i++) { | ||
buckets[i] = _hash_buckets[i].load(); | ||
_hash_buckets[i].store(0); | ||
} | ||
if (!find_outlier_index( | ||
buckets, FLAGS_coarse_data_variance_threshold, result.coarse_bucket_index)) { | ||
result.coarse_bucket_index = -1; | ||
} | ||
} | ||
|
||
} // namespace server | ||
} // namespace pegasus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove the blank line