be/src/cloud/cloud_tablet_hotspot.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "cloud/cloud_tablet_hotspot.h" |
19 | | |
20 | | #include <chrono> |
21 | | #include <mutex> |
22 | | |
23 | | #include "cloud/config.h" |
24 | | #include "runtime/exec_env.h" |
25 | | #include "storage/tablet/tablet_fwd.h" |
26 | | |
27 | | namespace doris { |
28 | | |
29 | 0 | void TabletHotspot::count(const BaseTablet& tablet) { |
30 | 0 | size_t slot_idx = tablet.tablet_id() % s_slot_size; |
31 | 0 | auto& slot = _tablets_hotspot[slot_idx]; |
32 | 0 | std::lock_guard lock(slot.mtx); |
33 | 0 | HotspotCounterPtr counter; |
34 | 0 | if (auto iter = slot.map.find(tablet.tablet_id()); iter == slot.map.end()) { |
35 | 0 | counter = std::make_shared<HotspotCounter>(tablet.table_id(), tablet.index_id(), |
36 | 0 | tablet.partition_id()); |
37 | 0 | slot.map.insert(std::make_pair(tablet.tablet_id(), counter)); |
38 | 0 | } else { |
39 | 0 | counter = iter->second; |
40 | 0 | } |
41 | 0 | counter->last_access_time = std::chrono::system_clock::now(); |
42 | 0 | counter->cur_counter++; |
43 | 0 | } |
44 | | |
45 | 0 | TabletHotspot::TabletHotspot() { |
46 | 0 | _counter_thread = std::thread(&TabletHotspot::make_dot_point, this); |
47 | 0 | } |
48 | | |
49 | 0 | TabletHotspot::~TabletHotspot() { |
50 | 0 | { |
51 | 0 | std::lock_guard lock(_mtx); |
52 | 0 | _closed = true; |
53 | 0 | } |
54 | 0 | _cond.notify_all(); |
55 | 0 | if (_counter_thread.joinable()) { |
56 | 0 | _counter_thread.join(); |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | | void get_return_partitions( |
61 | | const std::unordered_map<TabletHotspotMapKey, |
62 | | std::unordered_map<int64_t, TabletHotspotMapValue>, MapKeyHash>& |
63 | | hot_partition, |
64 | | const std::unordered_map<TabletHotspotMapKey, |
65 | | std::unordered_map<int64_t, TabletHotspotMapValue>, MapKeyHash>& |
66 | | last_hot_partition, |
67 | 0 | std::vector<THotTableMessage>* hot_tables, int& return_partitions, int N) { |
68 | 0 | for (const auto& [key, partition_to_value] : hot_partition) { |
69 | 0 | THotTableMessage msg; |
70 | 0 | msg.table_id = key.first; |
71 | 0 | msg.index_id = key.second; |
72 | 0 | for (const auto& [partition_id, value] : partition_to_value) { |
73 | 0 | if (return_partitions > N) { |
74 | 0 | return; |
75 | 0 | } |
76 | 0 | auto last_value_iter = last_hot_partition.find(key); |
77 | 0 | if (last_value_iter != last_hot_partition.end()) { |
78 | 0 | auto last_partition_iter = last_value_iter->second.find(partition_id); |
79 | 0 | if (last_partition_iter != last_value_iter->second.end()) { |
80 | 0 | const auto& last_value = last_partition_iter->second; |
81 | 0 | if (std::abs(static_cast<int64_t>(value.qpd) - |
82 | 0 | static_cast<int64_t>(last_value.qpd)) < 5 && |
83 | 0 | std::abs(static_cast<int64_t>(value.qpw) - |
84 | 0 | static_cast<int64_t>(last_value.qpw)) < 10 && |
85 | 0 | std::abs(static_cast<int64_t>(value.last_access_time) - |
86 | 0 | static_cast<int64_t>(last_value.last_access_time)) < 60) { |
87 | 0 | LOG(INFO) << "skip partition_id=" << partition_id << " qpd=" << value.qpd |
88 | 0 | << " qpw=" << value.qpw |
89 | 0 | << " last_access_time=" << value.last_access_time |
90 | 0 | << " last_qpd=" << last_value.qpd |
91 | 0 | << " last_qpw=" << last_value.qpw |
92 | 0 | << " last_access_time=" << last_value.last_access_time; |
93 | 0 | continue; |
94 | 0 | } |
95 | 0 | } |
96 | 0 | } |
97 | 0 | THotPartition new_hot_partition; |
98 | 0 | new_hot_partition.__set_partition_id(partition_id); |
99 | 0 | new_hot_partition.__set_query_per_day(value.qpd); |
100 | 0 | new_hot_partition.__set_query_per_week(value.qpw); |
101 | 0 | new_hot_partition.__set_last_access_time(value.last_access_time); |
102 | 0 | msg.hot_partitions.push_back(new_hot_partition); |
103 | 0 | return_partitions++; |
104 | 0 | } |
105 | 0 | msg.__isset.hot_partitions = !msg.hot_partitions.empty(); |
106 | 0 | hot_tables->push_back(std::move(msg)); |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | 0 | void TabletHotspot::get_top_n_hot_partition(std::vector<THotTableMessage>* hot_tables) { |
111 | | // map<pair<table_id, index_id>, map<partition_id, value>> for day |
112 | 0 | std::unordered_map<TabletHotspotMapKey, std::unordered_map<int64_t, TabletHotspotMapValue>, |
113 | 0 | MapKeyHash> |
114 | 0 | day_hot_partitions; |
115 | | // map<pair<table_id, index_id>, map<partition_id, value>> for week |
116 | 0 | std::unordered_map<TabletHotspotMapKey, std::unordered_map<int64_t, TabletHotspotMapValue>, |
117 | 0 | MapKeyHash> |
118 | 0 | week_hot_partitions; |
119 | |
|
120 | 0 | std::for_each(_tablets_hotspot.begin(), _tablets_hotspot.end(), [&](HotspotMap& map) { |
121 | 0 | std::lock_guard lock(map.mtx); |
122 | 0 | for (auto& [_, counter] : map.map) { |
123 | 0 | if (counter->qpd() != 0) { |
124 | 0 | auto& hot_partition = day_hot_partitions[std::make_pair( |
125 | 0 | counter->table_id, counter->index_id)][counter->partition_id]; |
126 | 0 | hot_partition.qpd = std::max(hot_partition.qpd, counter->qpd()); |
127 | 0 | hot_partition.qpw = std::max(hot_partition.qpw, counter->qpw()); |
128 | 0 | hot_partition.last_access_time = |
129 | 0 | std::max<int64_t>(hot_partition.last_access_time, |
130 | 0 | std::chrono::duration_cast<std::chrono::seconds>( |
131 | 0 | counter->last_access_time.time_since_epoch()) |
132 | 0 | .count()); |
133 | 0 | } else if (counter->qpw() != 0) { |
134 | 0 | auto& hot_partition = week_hot_partitions[std::make_pair( |
135 | 0 | counter->table_id, counter->index_id)][counter->partition_id]; |
136 | 0 | hot_partition.qpd = 0; |
137 | 0 | hot_partition.qpw = std::max(hot_partition.qpw, counter->qpw()); |
138 | 0 | hot_partition.last_access_time = |
139 | 0 | std::max<int64_t>(hot_partition.last_access_time, |
140 | 0 | std::chrono::duration_cast<std::chrono::seconds>( |
141 | 0 | counter->last_access_time.time_since_epoch()) |
142 | 0 | .count()); |
143 | 0 | } |
144 | 0 | } |
145 | 0 | }); |
146 | 0 | constexpr int N = 50; |
147 | 0 | int return_partitions = 0; |
148 | |
|
149 | 0 | std::unique_lock lock(_last_partitions_mtx); |
150 | 0 | get_return_partitions(day_hot_partitions, _last_day_hot_partitions, hot_tables, |
151 | 0 | return_partitions, N); |
152 | 0 | get_return_partitions(week_hot_partitions, _last_week_hot_partitions, hot_tables, |
153 | 0 | return_partitions, N); |
154 | |
|
155 | 0 | _last_day_hot_partitions = std::move(day_hot_partitions); |
156 | 0 | _last_week_hot_partitions = std::move(week_hot_partitions); |
157 | 0 | } |
158 | | |
159 | 0 | void HotspotCounter::make_dot_point() { |
160 | 0 | uint64_t value = cur_counter.load(); |
161 | 0 | cur_counter = 0; |
162 | 0 | if (history_counters.size() == week_counters_size) { |
163 | 0 | uint64_t week_counter_remove = history_counters.back(); |
164 | 0 | uint64_t day_counter_remove = history_counters[day_counters_size - 1]; |
165 | 0 | week_history_counter = week_history_counter - week_counter_remove + value; |
166 | 0 | day_history_counter = day_history_counter - day_counter_remove + value; |
167 | 0 | history_counters.pop_back(); |
168 | 0 | } else if (history_counters.size() < day_counters_size) { |
169 | 0 | week_history_counter += value; |
170 | 0 | day_history_counter += value; |
171 | 0 | } else { |
172 | 0 | week_history_counter += value; |
173 | 0 | uint64_t day_counter_remove = history_counters[day_counters_size - 1]; |
174 | 0 | day_history_counter = day_history_counter - day_counter_remove + value; |
175 | 0 | } |
176 | 0 | history_counters.push_front(value); |
177 | 0 | } |
178 | | |
179 | 0 | uint64_t HotspotCounter::qpd() { |
180 | 0 | return day_history_counter + cur_counter.load(); |
181 | 0 | } |
182 | | |
183 | 0 | uint64_t HotspotCounter::qpw() { |
184 | 0 | return week_history_counter + cur_counter.load(); |
185 | 0 | } |
186 | | |
187 | 0 | void TabletHotspot::make_dot_point() { |
188 | 0 | while (true) { |
189 | 0 | { |
190 | 0 | std::unique_lock lock(_mtx); |
191 | 0 | _cond.wait_for(lock, std::chrono::seconds(HotspotCounter::time_interval), |
192 | 0 | [this]() { return _closed; }); |
193 | 0 | if (_closed) { |
194 | 0 | break; |
195 | 0 | } |
196 | 0 | } |
197 | 0 | std::for_each(_tablets_hotspot.begin(), _tablets_hotspot.end(), [](HotspotMap& map) { |
198 | 0 | std::vector<HotspotCounterPtr> counters; |
199 | 0 | { |
200 | 0 | std::lock_guard lock(map.mtx); |
201 | 0 | for (auto& [_, counter] : map.map) { |
202 | 0 | counters.push_back(counter); |
203 | 0 | } |
204 | 0 | } |
205 | 0 | std::for_each(counters.begin(), counters.end(), |
206 | 0 | [](HotspotCounterPtr& counter) { counter->make_dot_point(); }); |
207 | 0 | }); |
208 | 0 | } |
209 | 0 | } |
210 | | |
211 | | } // namespace doris |