be/src/load/memtable/memtable_memory_limiter.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 <stdint.h> |
21 | | |
22 | | #include <functional> |
23 | | |
24 | | #include "common/status.h" |
25 | | #include "runtime/memory/mem_tracker.h" |
26 | | #include "runtime/workload_group/workload_group.h" |
27 | | #include "util/countdown_latch.h" |
28 | | #include "util/stopwatch.hpp" |
29 | | |
30 | | namespace doris { |
31 | | class MemTableWriter; |
32 | | struct WriterMemItem { |
33 | | std::weak_ptr<MemTableWriter> writer; |
34 | | int64_t mem_size; |
35 | | }; |
36 | | class MemTableMemoryLimiter { |
37 | | public: |
38 | | MemTableMemoryLimiter(); |
39 | | ~MemTableMemoryLimiter(); |
40 | | |
41 | | Status init(int64_t process_mem_limit); |
42 | | |
43 | | // check if the total mem consumption exceeds limit. |
44 | | // If yes, it will flush memtable to try to reduce memory consumption. |
45 | | // Every write operation will call this API to check if need flush memtable OR hang |
46 | | // when memory is not available. |
47 | | void handle_memtable_flush(std::function<bool()> cancel_check); |
48 | | |
49 | | void register_writer(std::weak_ptr<MemTableWriter> writer); |
50 | | |
51 | | void refresh_mem_tracker(); |
52 | | |
53 | 0 | MemTracker* mem_tracker() { return _mem_tracker.get(); } |
54 | | |
55 | 1 | int64_t mem_usage() const { return _mem_usage; } |
56 | | |
57 | | private: |
58 | | static inline int64_t _sys_avail_mem_less_than_warning_water_mark(); |
59 | | static inline int64_t _process_used_mem_more_than_soft_mem_limit(); |
60 | | |
61 | | bool _soft_limit_reached(); |
62 | | bool _hard_limit_reached(); |
63 | | bool _load_usage_low(); |
64 | | int64_t _need_flush(); |
65 | | int64_t _flush_active_memtables(int64_t need_flush); |
66 | | void _refresh_mem_tracker(); |
67 | | std::mutex _lock; |
68 | | std::condition_variable _hard_limit_end_cond; |
69 | | int64_t _mem_usage = 0; |
70 | | int64_t _flush_mem_usage = 0; |
71 | | int64_t _queue_mem_usage = 0; |
72 | | int64_t _active_mem_usage = 0; |
73 | | |
74 | | // sum of all mem table memory. |
75 | | std::unique_ptr<MemTracker> _mem_tracker; |
76 | | int64_t _load_hard_mem_limit = -1; |
77 | | int64_t _load_soft_mem_limit = -1; |
78 | | int64_t _load_safe_mem_permit = -1; |
79 | | |
80 | | enum Limit { NONE, SOFT, HARD } _last_limit = Limit::NONE; |
81 | | MonotonicStopWatch _log_timer; |
82 | | static const int64_t LOG_INTERVAL = 1 * 1000 * 1000 * 1000; // 1s |
83 | | |
84 | | std::vector<std::weak_ptr<MemTableWriter>> _writers; |
85 | | std::vector<std::weak_ptr<MemTableWriter>> _active_writers; |
86 | | }; |
87 | | } // namespace doris |