Coverage Report

Created: 2026-06-09 21:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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, WorkloadGroup* wg = nullptr);
48
    void handle_table_memtable_backpressure(std::function<bool()> cancel_check, int64_t table_id);
49
50
    void register_writer(std::weak_ptr<MemTableWriter> writer);
51
52
    void refresh_mem_tracker();
53
54
20
    MemTracker* mem_tracker() { return _mem_tracker.get(); }
55
56
1
    int64_t mem_usage() const { return _mem_usage; }
57
58
0
    bool soft_limit_reached() { return _soft_limit_reached(); }
59
60
private:
61
    static inline int64_t _sys_avail_mem_less_than_warning_water_mark();
62
    static inline int64_t _process_used_mem_more_than_soft_mem_limit();
63
64
    bool _soft_limit_reached();
65
    bool _hard_limit_reached();
66
    bool _load_usage_low();
67
    int64_t _need_flush();
68
    int64_t _table_flush_pending_memtable_count(int64_t table_id);
69
    int64_t _flush_active_memtables(int64_t need_flush);
70
    void _refresh_mem_tracker();
71
    std::mutex _lock;
72
    std::condition_variable _hard_limit_end_cond;
73
    int64_t _mem_usage = 0;
74
    int64_t _flush_mem_usage = 0;
75
    int64_t _queue_mem_usage = 0;
76
    int64_t _active_mem_usage = 0;
77
78
    // sum of all mem table memory.
79
    std::unique_ptr<MemTracker> _mem_tracker;
80
    int64_t _load_hard_mem_limit = -1;
81
    int64_t _load_soft_mem_limit = -1;
82
    int64_t _load_safe_mem_permit = -1;
83
84
    enum Limit { NONE, SOFT, HARD } _last_limit = Limit::NONE;
85
    MonotonicStopWatch _log_timer;
86
    static const int64_t LOG_INTERVAL = 1 * 1000 * 1000 * 1000; // 1s
87
88
    std::vector<std::weak_ptr<MemTableWriter>> _writers;
89
    std::vector<std::weak_ptr<MemTableWriter>> _active_writers;
90
};
91
} // namespace doris