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

refactor(collector): sort out the structure of partition hotspot detection #597

Merged
merged 40 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
14f5f52
refactor(collector): sort out the structure of partition hotspot dete…
Smityz Sep 7, 2020
f65aa72
add cpp
Smityz Sep 7, 2020
785be3f
license
Smityz Sep 7, 2020
98bd7f3
del policy
Smityz Sep 8, 2020
54135fb
header
Smityz Sep 8, 2020
76ff93b
Merge branch 'master' into del_old_alg
Smityz Sep 8, 2020
3c8a516
update
Smityz Sep 8, 2020
dfa3091
fix
Smityz Sep 8, 2020
f31953a
Update src/server/info_collector.cpp
Smityz Sep 8, 2020
6575e40
Merge branch 'master' into del_old_alg
Smityz Sep 8, 2020
6d62ad2
rename
Smityz Sep 8, 2020
8a421bf
Merge branch 'del_old_alg' of github.com:Smityz/pegasus into del_old_alg
Smityz Sep 8, 2020
e74ee75
merge
Smityz Sep 8, 2020
ce085fc
rename
Smityz Sep 8, 2020
6a63987
rename
Smityz Sep 8, 2020
1143c74
format
Smityz Sep 9, 2020
8dfeeb7
Update hotspot_partition_calculator.h
Smityz Sep 9, 2020
2cee641
fix
Smityz Sep 9, 2020
b6c5694
Update src/server/hotspot_partition_calculator.h
Smityz Sep 9, 2020
e75f44d
Update src/server/hotspot_partition_calculator.h
Smityz Sep 9, 2020
7c012be
Update src/server/hotspot_partition_calculator.cpp
Smityz Sep 9, 2020
1eb187f
Update src/server/hotspot_partition_calculator.h
Smityz Sep 9, 2020
70e083d
Update src/server/hotspot_partition_calculator.h
Smityz Sep 9, 2020
6804702
update
Smityz Sep 9, 2020
4c50397
Merge branch 'del_old_alg' of github.com:Smityz/pegasus into del_old_alg
Smityz Sep 9, 2020
99954d7
format
Smityz Sep 9, 2020
e44e08c
Update src/server/hotspot_partition_calculator.h
Smityz Sep 9, 2020
b2b97e8
Update src/server/hotspot_partition_calculator.h
Smityz Sep 9, 2020
7e9a76b
Update src/server/hotspot_partition_calculator.cpp
Smityz Sep 9, 2020
0134f50
Update src/server/hotspot_partition_calculator.cpp
Smityz Sep 9, 2020
8ac6320
update
Smityz Sep 9, 2020
5b1a36a
Merge branch 'del_old_alg' of github.com:Smityz/pegasus into del_old_alg
Smityz Sep 9, 2020
120dab2
format
Smityz Sep 9, 2020
cec568f
format
Smityz Sep 9, 2020
012ce98
Update hotspot_partition_calculator.cpp
Smityz Sep 9, 2020
d27d7a2
Update src/server/hotspot_partition_calculator.cpp
Smityz Sep 9, 2020
734f290
format
Smityz Sep 9, 2020
88de731
update
Smityz Sep 10, 2020
05dc899
todo
Smityz Sep 10, 2020
6ff0822
Merge branch 'master' into del_old_alg
acelyc111 Sep 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/server/hotspot_partition_calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "hotspot_partition_calculator.h"

#include <dsn/dist/fmt_logging.h>

namespace pegasus {
namespace server {

void hotspot_partition_calculator::aggregate(const std::vector<row_data> &partitions)
{
while (_app_data.size() > kMaxQueueSize - 1) {
_app_data.pop();
}
std::vector<hotspot_partition_data> temp(partitions.size());
for (int i = 0; i < partitions.size(); i++) {
temp[i] = std::move(hotspot_partition_data(partitions[i]));
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
}
_app_data.emplace(temp);
}

void hotspot_partition_calculator::init_perf_counter(const int perf_counter_count)
Smityz marked this conversation as resolved.
Show resolved Hide resolved
{
std::string counter_name;
std::string counter_desc;
for (int i = 0; i < perf_counter_count; i++) {
string paritition_desc = _app_name + '.' + std::to_string(i);
Smityz marked this conversation as resolved.
Show resolved Hide resolved
counter_name = fmt::format("app.stat.hotspots@{}", paritition_desc);
counter_desc = fmt::format("statistic the hotspots of app {}", paritition_desc);
_points[i].init_app_counter(
"app.pegasus", counter_name.c_str(), COUNTER_TYPE_NUMBER, counter_desc.c_str());
}
}

void hotspot_partition_calculator::start_alg() { _policy->analysis(_app_data, _points); }

} // namespace server
} // namespace pegasus
58 changes: 58 additions & 0 deletions src/server/hotspot_partition_calculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

#include "hotspot_partition_data.h"

#include <algorithm>
#include <gtest/gtest_prod.h>
#include <math.h>

#include <dsn/perf_counter/perf_counter.h>
#include "hotspot_partition_policy.h"

namespace pegasus {
namespace server {

// hotspot_partition_calculator is used to find the hotspot in Pegasus
Smityz marked this conversation as resolved.
Show resolved Hide resolved
class hotspot_partition_calculator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class hotspot_partition_calculator
class hotspot_partition_calculator : public replica_base

replica_base has a app_name() method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hotspot_partition_calculator stores the statistical data of the correspond table, _app_name is for that table, not itself.

{
public:
hotspot_partition_calculator(const std::string &app_name,
const int partition_num,
std::unique_ptr<hotspot_partition_policy> policy)
: _app_name(app_name), _points(partition_num), _policy(std::move(policy))
{
init_perf_counter(partition_num);
}
void aggregate(const std::vector<row_data> &partitions);
void start_alg();
void init_perf_counter(const int perf_counter_count);
Smityz marked this conversation as resolved.
Show resolved Hide resolved

private:
const std::string _app_name;
std::vector<::dsn::perf_counter_wrapper> _points;
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
std::queue<std::vector<hotspot_partition_data>> _app_data;
std::unique_ptr<hotspot_partition_policy> _policy;
static const int kMaxQueueSize = 100;
levy5307 marked this conversation as resolved.
Show resolved Hide resolved

FRIEND_TEST(hotspot_partition_calculator, hotspot_partition_policy);
};

} // namespace server
} // namespace pegasus
67 changes: 67 additions & 0 deletions src/server/hotspot_partition_policy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "hotspot_partition_policy.h"

namespace pegasus {
namespace server {

void hotspot_partition_policy::analysis(
Smityz marked this conversation as resolved.
Show resolved Hide resolved
const std::queue<std::vector<hotspot_partition_data>> &hotspot_app_data,
std::vector<::dsn::perf_counter_wrapper> &perf_counters)
{
dassert(hotspot_app_data.back().size() == perf_counters.size(),
"partition counts error, please check");
std::vector<double> data_samples;
data_samples.reserve(hotspot_app_data.size() * perf_counters.size());
auto temp_data = hotspot_app_data;
double total = 0, sd = 0, avg = 0;
int sample_count = 0;
// avg: Average number
// sd: Standard deviation
// sample_count: Number of samples
while (!temp_data.empty()) {
for (auto partition_data : temp_data.front()) {
if (partition_data.total_qps - 1.00 > 0) {
data_samples.push_back(partition_data.total_qps);
total += partition_data.total_qps;
sample_count++;
}
}
temp_data.pop();
}
if (sample_count == 0) {
ddebug("hotspot_app_data size == 0");
return;
}
avg = total / sample_count;
for (auto data_sample : data_samples) {
sd += pow((data_sample - avg), 2);
}
sd = sqrt(sd / sample_count);
const auto &anly_data = hotspot_app_data.back();
for (int i = 0; i < perf_counters.size(); i++) {
double hot_point = (anly_data[i].total_qps - avg) / sd;
// perf_counter->set can only be unsigned __int64
// use ceil to guarantee conversion results
hot_point = ceil(std::max(hot_point, double(0)));
perf_counters[i]->set(hot_point);
}
}

} // namespace server
} // namespace pegasus
39 changes: 39 additions & 0 deletions src/server/hotspot_partition_policy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

#include "hotspot_partition_data.h"

#include <algorithm>
#include <gtest/gtest_prod.h>
#include <math.h>
Smityz marked this conversation as resolved.
Show resolved Hide resolved

#include <dsn/perf_counter/perf_counter.h>

namespace pegasus {
namespace server {
// PauTa Criterion
class hotspot_partition_policy
{
public:
void analysis(const std::queue<std::vector<hotspot_partition_data>> &hotspot_app_data,
std::vector<::dsn::perf_counter_wrapper> &perf_counters);
};

} // namespace server
} // namespace pegasus
30 changes: 9 additions & 21 deletions src/server/info_collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ info_collector::info_collector()
"storage_size_fetch_interval_seconds",
3600, // default value 1h
"storage size fetch interval seconds");
_hotspot_detect_algorithm = dsn_config_get_value_string("pegasus.collector",
"hotspot_detect_algorithm",
"hotspot_algo_qps_variance",
"hotspot_detect_algorithm");
// _storage_size_retry_wait_seconds is in range of [1, 60]
_storage_size_retry_wait_seconds =
std::min(60u, std::max(1u, _storage_size_fetch_interval_seconds / 10));
Expand Down Expand Up @@ -150,15 +146,15 @@ void info_collector::on_app_stat()
// get row data statistics for all of the apps
all_stats.merge(app_stats);

// hotspot_calculator is to detect hotspots
hotspot_calculator *hotspot_calculator =
// hotspot_partition_calculator is to detect hotspots
levy5307 marked this conversation as resolved.
Show resolved Hide resolved
hotspot_partition_calculator *hotspot_partition_calculator =
get_hotspot_calculator(app_rows.first, app_rows.second.size());
if (!hotspot_calculator) {
if (!hotspot_partition_calculator) {
Smityz marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
hotspot_calculator->aggregate(app_rows.second);
hotspot_partition_calculator->aggregate(app_rows.second);
// new policy can be designed by strategy pattern in hotspot_partition_data.h
hotspot_calculator->start_alg();
hotspot_partition_calculator->start_alg();
}
get_app_counters(all_stats.app_name)->set(all_stats);

Expand Down Expand Up @@ -302,25 +298,17 @@ void info_collector::on_storage_size_stat(int remaining_retry_count)
_result_writer->set_result(st_stat.timestamp, "ss", st_stat.dump_to_json());
}

hotspot_calculator *info_collector::get_hotspot_calculator(const std::string &app_name,
const int partition_num)
hotspot_partition_calculator *info_collector::get_hotspot_calculator(const std::string &app_name,
const int partition_num)
hycdong marked this conversation as resolved.
Show resolved Hide resolved
{
// use appname+partition_num as a key can prevent the impact of dynamic partition changes
std::string app_name_pcount = fmt::format("{}.{}", app_name, partition_num);
auto iter = _hotspot_calculator_store.find(app_name_pcount);
if (iter != _hotspot_calculator_store.end()) {
return iter->second;
}
std::unique_ptr<hotspot_policy> policy;
if (_hotspot_detect_algorithm == "hotspot_algo_qps_variance") {
policy.reset(new hotspot_algo_qps_variance());
} else {
dwarn("hotspot detection is disabled");
_hotspot_calculator_store[app_name_pcount] = nullptr;
return nullptr;
}
hotspot_calculator *calculator =
new hotspot_calculator(app_name, partition_num, std::move(policy));
hotspot_partition_calculator *calculator = new hotspot_partition_calculator(
app_name, partition_num, std::make_unique<hotspot_partition_policy>());
_hotspot_calculator_store[app_name_pcount] = calculator;
return calculator;
}
Expand Down
10 changes: 5 additions & 5 deletions src/server/info_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

#include "../shell/commands.h"
#include "table_stats.h"
#include "table_hotspot_policy.h"
#include "hotspot_partition_calculator.h"
#include "hotspot_partition_policy.h"

namespace pegasus {
namespace server {
Expand Down Expand Up @@ -177,15 +178,14 @@ class info_collector
uint32_t _storage_size_fetch_interval_seconds;
uint32_t _storage_size_retry_wait_seconds;
uint32_t _storage_size_retry_max_count;
std::string _hotspot_detect_algorithm;
::dsn::task_ptr _storage_size_stat_timer_task;
::dsn::utils::ex_lock_nr _capacity_unit_update_info_lock;
// mapping 'node address' --> 'last updated timestamp'
std::map<std::string, string> _capacity_unit_update_info;
std::map<std::string, hotspot_calculator *> _hotspot_calculator_store;
std::map<std::string, hotspot_partition_calculator *> _hotspot_calculator_store;
Smityz marked this conversation as resolved.
Show resolved Hide resolved

hotspot_calculator *get_hotspot_calculator(const std::string &app_name,
const int partition_num);
hotspot_partition_calculator *get_hotspot_calculator(const std::string &app_name,
const int partition_num);
};

} // namespace server
Expand Down
40 changes: 0 additions & 40 deletions src/server/table_hotspot_policy.cpp

This file was deleted.

Loading