be/src/cloud/cloud_tablet_hotspot.h
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 | | #pragma once |
19 | | |
20 | | #include <gen_cpp/BackendService.h> |
21 | | |
22 | | #include <atomic> |
23 | | #include <chrono> |
24 | | #include <condition_variable> |
25 | | #include <memory> |
26 | | |
27 | | #include "storage/tablet/tablet.h" |
28 | | |
29 | | namespace doris { |
30 | | |
31 | | // This counter is used to count the tablet query frequency |
32 | | struct HotspotCounter { |
33 | | HotspotCounter(int64_t table_id, int64_t index_id, int64_t partition_id) |
34 | 97.6k | : table_id(table_id), index_id(index_id), partition_id(partition_id) {} |
35 | | // One break point every hour |
36 | | void make_dot_point(); |
37 | | uint64_t qpd(); |
38 | | uint64_t qpw(); |
39 | | int64_t table_id; |
40 | | int64_t index_id; |
41 | | int64_t partition_id; |
42 | | std::chrono::system_clock::time_point last_access_time; |
43 | | std::atomic_uint64_t cur_counter {0}; |
44 | | std::deque<uint64_t> history_counters; |
45 | | std::atomic_uint64_t week_history_counter {0}; |
46 | | std::atomic_uint64_t day_history_counter {0}; |
47 | | static inline int64_t time_interval = 1 * 60 * 60; |
48 | | static inline int64_t week_counters_size = ((7 * 24 * 60 * 60) / time_interval) - 1; |
49 | | static inline int64_t day_counters_size = ((24 * 60 * 60) / time_interval) - 1; |
50 | | }; |
51 | | |
52 | | using HotspotCounterPtr = std::shared_ptr<HotspotCounter>; |
53 | | using TabletHotspotMapKey = std::pair<int64_t, int64_t>; |
54 | | |
55 | | struct TabletHotspotMapValue { |
56 | | uint64_t qpd = 0; // query per day |
57 | | uint64_t qpw = 0; // query per week |
58 | | int64_t last_access_time; |
59 | | }; |
60 | | |
61 | | struct MapKeyHash { |
62 | 79.5k | int64_t operator()(const std::pair<int64_t, int64_t>& key) const { |
63 | 79.5k | return std::hash<int64_t> {}(key.first) + std::hash<int64_t> {}(key.second); |
64 | 79.5k | } |
65 | | }; |
66 | | |
67 | | class TabletHotspot { |
68 | | public: |
69 | | TabletHotspot(); |
70 | | ~TabletHotspot(); |
71 | | // When query the tablet, count it |
72 | | void count(const BaseTablet& tablet); |
73 | | void get_top_n_hot_partition(std::vector<THotTableMessage>* hot_tables); |
74 | | |
75 | | private: |
76 | | void make_dot_point(); |
77 | | |
78 | | struct HotspotMap { |
79 | | std::mutex mtx; |
80 | | std::unordered_map<int64_t, HotspotCounterPtr> map; |
81 | | }; |
82 | | static constexpr size_t s_slot_size = 1024; |
83 | | std::array<HotspotMap, s_slot_size> _tablets_hotspot; |
84 | | std::thread _counter_thread; |
85 | | bool _closed {false}; |
86 | | std::mutex _mtx; |
87 | | std::condition_variable _cond; |
88 | | |
89 | | std::mutex _last_partitions_mtx; |
90 | | std::unordered_map<TabletHotspotMapKey, std::unordered_map<int64_t, TabletHotspotMapValue>, |
91 | | MapKeyHash> |
92 | | _last_day_hot_partitions; |
93 | | std::unordered_map<TabletHotspotMapKey, std::unordered_map<int64_t, TabletHotspotMapValue>, |
94 | | MapKeyHash> |
95 | | _last_week_hot_partitions; |
96 | | }; |
97 | | |
98 | | } // namespace doris |