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 | | namespace doris { |
27 | | |
28 | | /* |
29 | | This class is used to control compaction permission. To some extent, it can be used to control memory consumption. |
30 | | "permits" should be applied before a compaction task can execute. When the sum of "permites" held by executing |
31 | | compaction tasks reaches a threshold, subsequent compaction task will be no longer allowed, until some "permits" |
32 | | are released by some finished compaction tasks. "compaction score" for tablet is used as "permits" here. |
33 | | */ |
34 | | class CompactionPermitLimiter { |
35 | | public: |
36 | | CompactionPermitLimiter(); |
37 | 339 | virtual ~CompactionPermitLimiter() {} |
38 | | |
39 | | void request(int64_t permits); |
40 | | |
41 | | void release(int64_t permits); |
42 | | |
43 | 6 | int64_t usage() const { return _used_permits.load(std::memory_order_relaxed); } |
44 | | |
45 | | private: |
46 | | // sum of "permits" held by executing compaction tasks currently |
47 | | std::atomic<int64_t> _used_permits; |
48 | | std::mutex _permits_mutex; |
49 | | std::condition_variable _permits_cv; |
50 | | }; |
51 | | } // namespace doris |