forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud_tablet_hotspot.cpp
More file actions
210 lines (193 loc) · 9.06 KB
/
Copy pathcloud_tablet_hotspot.cpp
File metadata and controls
210 lines (193 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// 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 "cloud/cloud_tablet_hotspot.h"
#include <chrono>
#include <mutex>
#include "cloud/config.h"
#include "olap/tablet_fwd.h"
#include "runtime/exec_env.h"
namespace doris {
void TabletHotspot::count(const BaseTablet& tablet) {
size_t slot_idx = tablet.tablet_id() % s_slot_size;
auto& slot = _tablets_hotspot[slot_idx];
std::lock_guard lock(slot.mtx);
HotspotCounterPtr counter;
if (auto iter = slot.map.find(tablet.tablet_id()); iter == slot.map.end()) {
counter = std::make_shared<HotspotCounter>(tablet.table_id(), tablet.index_id(),
tablet.partition_id());
slot.map.insert(std::make_pair(tablet.tablet_id(), counter));
} else {
counter = iter->second;
}
counter->last_access_time = std::chrono::system_clock::now();
counter->cur_counter++;
}
TabletHotspot::TabletHotspot() {
_counter_thread = std::thread(&TabletHotspot::make_dot_point, this);
}
TabletHotspot::~TabletHotspot() {
{
std::lock_guard lock(_mtx);
_closed = true;
}
_cond.notify_all();
if (_counter_thread.joinable()) {
_counter_thread.join();
}
}
void get_return_partitions(
const std::unordered_map<TabletHotspotMapKey,
std::unordered_map<int64_t, TabletHotspotMapValue>, MapKeyHash>&
hot_partition,
const std::unordered_map<TabletHotspotMapKey,
std::unordered_map<int64_t, TabletHotspotMapValue>, MapKeyHash>&
last_hot_partition,
std::vector<THotTableMessage>* hot_tables, int& return_partitions, int N) {
for (const auto& [key, partition_to_value] : hot_partition) {
THotTableMessage msg;
msg.table_id = key.first;
msg.index_id = key.second;
for (const auto& [partition_id, value] : partition_to_value) {
if (return_partitions > N) {
return;
}
auto last_value_iter = last_hot_partition.find(key);
if (last_value_iter != last_hot_partition.end()) {
auto last_partition_iter = last_value_iter->second.find(partition_id);
if (last_partition_iter != last_value_iter->second.end()) {
const auto& last_value = last_partition_iter->second;
if (std::abs(static_cast<int64_t>(value.qpd) -
static_cast<int64_t>(last_value.qpd)) < 5 &&
std::abs(static_cast<int64_t>(value.qpw) -
static_cast<int64_t>(last_value.qpw)) < 10 &&
std::abs(static_cast<int64_t>(value.last_access_time) -
static_cast<int64_t>(last_value.last_access_time)) < 60) {
LOG(INFO) << "skip partition_id=" << partition_id << " qpd=" << value.qpd
<< " qpw=" << value.qpw
<< " last_access_time=" << value.last_access_time
<< " last_qpd=" << last_value.qpd
<< " last_qpw=" << last_value.qpw
<< " last_access_time=" << last_value.last_access_time;
continue;
}
}
}
THotPartition hot_partition;
hot_partition.__set_partition_id(partition_id);
hot_partition.__set_query_per_day(value.qpd);
hot_partition.__set_query_per_week(value.qpw);
hot_partition.__set_last_access_time(value.last_access_time);
msg.hot_partitions.push_back(hot_partition);
return_partitions++;
}
msg.__isset.hot_partitions = !msg.hot_partitions.empty();
hot_tables->push_back(std::move(msg));
}
}
void TabletHotspot::get_top_n_hot_partition(std::vector<THotTableMessage>* hot_tables) {
// map<pair<table_id, index_id>, map<partition_id, value>> for day
std::unordered_map<TabletHotspotMapKey, std::unordered_map<int64_t, TabletHotspotMapValue>,
MapKeyHash>
day_hot_partitions;
// map<pair<table_id, index_id>, map<partition_id, value>> for week
std::unordered_map<TabletHotspotMapKey, std::unordered_map<int64_t, TabletHotspotMapValue>,
MapKeyHash>
week_hot_partitions;
std::for_each(_tablets_hotspot.begin(), _tablets_hotspot.end(), [&](HotspotMap& map) {
std::lock_guard lock(map.mtx);
for (auto& [_, counter] : map.map) {
if (counter->qpd() != 0) {
auto& hot_partition = day_hot_partitions[std::make_pair(
counter->table_id, counter->index_id)][counter->partition_id];
hot_partition.qpd = std::max(hot_partition.qpd, counter->qpd());
hot_partition.qpw = std::max(hot_partition.qpw, counter->qpw());
hot_partition.last_access_time =
std::max<int64_t>(hot_partition.last_access_time,
std::chrono::duration_cast<std::chrono::seconds>(
counter->last_access_time.time_since_epoch())
.count());
} else if (counter->qpw() != 0) {
auto& hot_partition = week_hot_partitions[std::make_pair(
counter->table_id, counter->index_id)][counter->partition_id];
hot_partition.qpd = 0;
hot_partition.qpw = std::max(hot_partition.qpw, counter->qpw());
hot_partition.last_access_time =
std::max<int64_t>(hot_partition.last_access_time,
std::chrono::duration_cast<std::chrono::seconds>(
counter->last_access_time.time_since_epoch())
.count());
}
}
});
constexpr int N = 50;
int return_partitions = 0;
get_return_partitions(day_hot_partitions, _last_day_hot_partitions, hot_tables,
return_partitions, N);
get_return_partitions(week_hot_partitions, _last_week_hot_partitions, hot_tables,
return_partitions, N);
_last_day_hot_partitions = std::move(day_hot_partitions);
_last_week_hot_partitions = std::move(week_hot_partitions);
}
void HotspotCounter::make_dot_point() {
uint64_t value = cur_counter.load();
cur_counter = 0;
if (history_counters.size() == week_counters_size) {
uint64_t week_counter_remove = history_counters.back();
uint64_t day_counter_remove = history_counters[day_counters_size - 1];
week_history_counter = week_history_counter - week_counter_remove + value;
day_history_counter = day_history_counter - day_counter_remove + value;
history_counters.pop_back();
} else if (history_counters.size() < day_counters_size) {
week_history_counter += value;
day_history_counter += value;
} else {
week_history_counter += value;
uint64_t day_counter_remove = history_counters[day_counters_size - 1];
day_history_counter = day_history_counter - day_counter_remove + value;
}
history_counters.push_front(value);
}
uint64_t HotspotCounter::qpd() {
return day_history_counter + cur_counter.load();
}
uint64_t HotspotCounter::qpw() {
return week_history_counter + cur_counter.load();
}
void TabletHotspot::make_dot_point() {
while (true) {
{
std::unique_lock lock(_mtx);
_cond.wait_for(lock, std::chrono::seconds(HotspotCounter::time_interval),
[this]() { return _closed; });
if (_closed) {
break;
}
}
std::for_each(_tablets_hotspot.begin(), _tablets_hotspot.end(), [](HotspotMap& map) {
std::vector<HotspotCounterPtr> counters;
{
std::lock_guard lock(map.mtx);
for (auto& [_, counter] : map.map) {
counters.push_back(counter);
}
}
std::for_each(counters.begin(), counters.end(),
[](HotspotCounterPtr& counter) { counter->make_dot_point(); });
});
}
}
} // namespace doris