/Users/deen/code/yugabyte-db/src/yb/tserver/tablet_memory_manager.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #ifndef YB_TSERVER_TABLET_MEMORY_MANAGER_H_ |
15 | | #define YB_TSERVER_TABLET_MEMORY_MANAGER_H_ |
16 | | |
17 | | #include <memory> |
18 | | |
19 | | #include <boost/optional.hpp> |
20 | | |
21 | | #include "yb/tablet/tablet_options.h" |
22 | | |
23 | | #include "yb/util/background_task.h" |
24 | | #include "yb/util/mem_tracker.h" |
25 | | |
26 | | namespace yb { |
27 | | namespace tserver { |
28 | | |
29 | | class TabletMemoryManagerListenerIf { |
30 | | public: |
31 | 1 | virtual ~TabletMemoryManagerListenerIf() {} |
32 | 0 | virtual void StartedFlush(const TabletId& tablet_id) {} |
33 | | }; |
34 | | |
35 | | // TabletMemoryManager keeps track of memory management for a tablet, including: |
36 | | // - Block cache initialization and tracking |
37 | | // - Log cache garbage collection |
38 | | // - Memory flushing once the global memstore limit is reached |
39 | | class TabletMemoryManager { |
40 | | public: |
41 | | // 'default_block_cache_size_percentage' indicates what percentage of tablet memory will be |
42 | | // allocated to the block cache by default, assuming it isn't overridden via flags |
43 | | // (combination of 'db_block_cache_size_bytes' and 'db_block_cache_size_percentage' flags). |
44 | | // |
45 | | // 'peers_fn' is a function to return an up-to-date list of the tablet's peers (unsorted). |
46 | | TabletMemoryManager( |
47 | | tablet::TabletOptions* options, |
48 | | const std::shared_ptr<MemTracker>& mem_tracker, |
49 | | const int32_t default_block_cache_size_percentage, |
50 | | const scoped_refptr<MetricEntity>& metrics, |
51 | | const std::function<std::vector<tablet::TabletPeerPtr>()>& peers_fn); |
52 | | |
53 | 186 | ~TabletMemoryManager() = default; |
54 | | |
55 | | // Init and Shutdown start/stop the background memstore management task. |
56 | | CHECKED_STATUS Init(); |
57 | | void Shutdown(); |
58 | | |
59 | | // The MemTracker associated with the block cache. |
60 | | std::shared_ptr<MemTracker> block_based_table_mem_tracker(); |
61 | | |
62 | | // Flushing function for the memstore. |
63 | | void FlushTabletIfLimitExceeded(); |
64 | | |
65 | | std::vector<std::shared_ptr<TabletMemoryManagerListenerIf>> TEST_listeners; |
66 | | |
67 | | private: |
68 | | // Initializes the block cache and associated mem tracker and garbage collector. |
69 | | // Will use the percentage of memory specified unless modified with the |
70 | | // db_block_cache_size_bytes and db_block_cache_size_percentage flags. |
71 | | void InitBlockCache( |
72 | | const scoped_refptr<MetricEntity>& metrics, |
73 | | const int32_t default_block_cache_size_percentage, |
74 | | tablet::TabletOptions* options); |
75 | | |
76 | | // Initializes the log cache garbage collector. |
77 | | void InitLogCacheGC(); |
78 | | |
79 | | // Initializes the background thread that periodically wakes up to flush memory over the |
80 | | // shared memstore limit. |
81 | | void ConfigureBackgroundTask(tablet::TabletOptions* options); |
82 | | |
83 | | // Log cache garbage collection function bound to the memory tracker. |
84 | | void LogCacheGC(MemTracker* log_cache_mem_tracker, size_t bytes_to_evict); |
85 | | |
86 | | // Determines which tablet has the oldest mutable memtable write time. May return a null ptr |
87 | | // if no tablet meets the criteria. Uses peers_fn_ to determine the full list of peers to check. |
88 | | tablet::TabletPeerPtr TabletToFlush(); |
89 | | |
90 | | // Function to return a log prefix with the tablet's tablet_id and permanent_uuid. |
91 | | std::string LogPrefix(const tablet::TabletPeerPtr& peer) const; |
92 | | |
93 | | // Function that returns all current peers of the server associated with this memory manager. |
94 | | std::function<std::vector<tablet::TabletPeerPtr>()> peers_fn_; |
95 | | |
96 | | std::shared_ptr<MemTracker> server_mem_tracker_; |
97 | | |
98 | | std::shared_ptr<MemTracker> block_based_table_mem_tracker_; |
99 | | |
100 | | std::shared_ptr<GarbageCollector> block_based_table_gc_; |
101 | | |
102 | | std::shared_ptr<GarbageCollector> log_cache_gc_; |
103 | | |
104 | | std::unique_ptr<BackgroundTask> background_task_; |
105 | | |
106 | | std::shared_ptr<rocksdb::MemoryMonitor> memory_monitor_; |
107 | | }; |
108 | | |
109 | | } // namespace tserver |
110 | | } // namespace yb |
111 | | |
112 | | #endif // YB_TSERVER_TABLET_MEMORY_MANAGER_H_ |