Coverage Report

Created: 2026-06-02 16:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/compaction/compaction_permit_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 <atomic>
23
#include <condition_variable>
24
#include <mutex>
25
26
#include "storage/olap_common.h"
27
28
namespace doris {
29
30
/*
31
    This class is used to control compaction permission. To some extent, it can be used to control memory consumption.
32
    "permits" should be applied before a compaction task can execute. When the sum of "permites" held by executing
33
    compaction tasks reaches a threshold, subsequent compaction task will be no longer allowed, until some "permits"
34
    are released by some finished compaction tasks. "compaction score" for tablet is used as "permits" here.
35
*/
36
class CompactionPermitLimiter {
37
public:
38
    CompactionPermitLimiter();
39
378
    virtual ~CompactionPermitLimiter() {}
40
41
    void request(int64_t permits);
42
43
    bool try_request(int64_t permits, CompactionType compaction_type);
44
45
    void release(int64_t permits,
46
                 CompactionType compaction_type = CompactionType::CUMULATIVE_COMPACTION);
47
48
6
    int64_t usage() const { return _used_permits.load(std::memory_order_relaxed); }
49
0
    int64_t binlog_usage() const { return _binlog_used_permits.load(std::memory_order_relaxed); }
50
51
private:
52
    // sum of "permits" held by executing compaction tasks currently
53
    std::atomic<int64_t> _used_permits;
54
    std::atomic<int64_t> _binlog_used_permits;
55
    std::mutex _permits_mutex;
56
    std::condition_variable _permits_cv;
57
};
58
} // namespace doris