Coverage Report

Created: 2026-07-05 19:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/cloud_storage_engine.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 <chrono>
21
#include <memory>
22
#include <mutex>
23
24
//#include "cloud/cloud_cumulative_compaction.h"
25
//#include "cloud/cloud_base_compaction.h"
26
//#include "cloud/cloud_full_compaction.h"
27
#include "cloud/cloud_committed_rs_mgr.h"
28
#include "cloud/cloud_cumulative_compaction_policy.h"
29
#include "cloud/cloud_tablet.h"
30
#include "cloud/cloud_txn_delete_bitmap_cache.h"
31
#include "cloud/config.h"
32
#include "io/cache/block_file_cache_factory.h"
33
#include "storage/compaction/compaction.h"
34
#include "storage/storage_engine.h"
35
#include "storage/storage_policy.h"
36
#include "util/threadpool.h"
37
38
namespace doris {
39
namespace cloud {
40
class CloudMetaMgr;
41
}
42
namespace io {
43
class FileCacheBlockDownloader;
44
}
45
46
class CloudTabletMgr;
47
class CloudCumulativeCompaction;
48
class CloudBaseCompaction;
49
class CloudFullCompaction;
50
class TabletHotspot;
51
class CloudWarmUpManager;
52
class CloudCompactionStopToken;
53
class CloudSnapshotMgr;
54
class CloudIndexChangeCompaction;
55
56
class CloudStorageEngine final : public BaseStorageEngine {
57
public:
58
    CloudStorageEngine(const EngineOptions& options);
59
60
    ~CloudStorageEngine() override;
61
62
    Status open() override;
63
    void stop() override;
64
    bool stopped() override;
65
66
    /* Parameters:
67
     * - tablet_id: the id of tablet to get
68
     * - sync_stats: the stats of sync rowset
69
     * - force_use_only_cached: whether only use cached tablet meta
70
     * - cache_on_miss: whether cache the tablet meta when missing in cache
71
     */
72
    Result<BaseTabletSPtr> get_tablet(int64_t tablet_id, SyncRowsetStats* sync_stats = nullptr,
73
                                      bool force_use_only_cached = false,
74
                                      bool cache_on_miss = true) override;
75
76
    /*
77
     * Get the tablet meta for a specific tablet
78
     * Parameters:
79
     * - tablet_id: the id of tablet to get meta for
80
     * - tablet_meta: output TabletMeta shared pointer
81
     * - force_use_only_cached: whether only use cached tablet meta (return NotFound on miss)
82
     */
83
    Status get_tablet_meta(int64_t tablet_id, TabletMetaSharedPtr* tablet_meta,
84
                           bool force_use_only_cached = false) override;
85
86
    Status start_bg_threads(std::shared_ptr<WorkloadGroup> wg_sptr = nullptr) override;
87
88
    Status set_cluster_id(int32_t cluster_id) override;
89
90
1.08M
    cloud::CloudMetaMgr& meta_mgr() const { return *_meta_mgr; }
91
92
112k
    CloudTabletMgr& tablet_mgr() const { return *_tablet_mgr; }
93
94
0
    CloudSnapshotMgr& cloud_snapshot_mgr() { return *_cloud_snapshot_mgr; }
95
96
425k
    CloudTxnDeleteBitmapCache& txn_delete_bitmap_cache() const { return *_txn_delete_bitmap_cache; }
97
98
250k
    CloudCommittedRSMgr& committed_rs_mgr() const { return *_committed_rs_mgr; }
99
100
8.72k
    ThreadPool& calc_tablet_delete_bitmap_task_thread_pool() const {
101
8.72k
        return *_calc_tablet_delete_bitmap_task_thread_pool;
102
8.72k
    }
103
0
    ThreadPool& sync_delete_bitmap_thread_pool() const { return *_sync_delete_bitmap_thread_pool; }
104
105
187k
    std::optional<StorageResource> get_storage_resource(const std::string& vault_id) {
106
18.4E
        VLOG_DEBUG << "Getting storage resource for vault_id: " << vault_id;
107
108
187k
        bool synced = false;
109
187k
        do {
110
189k
            if (vault_id.empty() && latest_fs() != nullptr) {
111
189k
                return StorageResource {latest_fs()};
112
189k
            }
113
18.4E
            if (auto storage_resource = doris::get_storage_resource(vault_id); storage_resource) {
114
1
                return storage_resource->first;
115
1
            }
116
18.4E
            if (synced) {
117
0
                break;
118
0
            }
119
18.4E
            sync_storage_vault();
120
18.4E
            synced = true;
121
18.4E
        } while (true);
122
123
18.4E
        return std::nullopt;
124
187k
    }
125
126
377k
    io::RemoteFileSystemSPtr latest_fs() const {
127
377k
        std::lock_guard lock(_latest_fs_mtx);
128
377k
        return _latest_fs;
129
377k
    }
130
131
1
    void set_latest_fs(const io::RemoteFileSystemSPtr& fs) {
132
1
        std::lock_guard lock(_latest_fs_mtx);
133
1
        _latest_fs = fs;
134
1
    }
135
136
    void get_cumu_compaction(int64_t tablet_id,
137
                             std::vector<std::shared_ptr<CloudCumulativeCompaction>>& res);
138
139
    Status submit_compaction_task(const CloudTabletSPtr& tablet, CompactionType compaction_type,
140
                                  int trigger_method = 0);
141
142
    Status get_compaction_status_json(std::string* result);
143
144
490
    bool has_base_compaction(int64_t tablet_id) const {
145
490
        std::lock_guard lock(_compaction_mtx);
146
490
        return _submitted_base_compactions.contains(tablet_id);
147
490
    }
148
149
537
    bool has_cumu_compaction(int64_t tablet_id) const {
150
537
        std::lock_guard lock(_compaction_mtx);
151
537
        return _submitted_cumu_compactions.contains(tablet_id);
152
537
    }
153
154
490
    bool has_full_compaction(int64_t tablet_id) const {
155
490
        std::lock_guard lock(_compaction_mtx);
156
490
        return _submitted_full_compactions.contains(tablet_id);
157
490
    }
158
159
    std::shared_ptr<CloudCumulativeCompactionPolicy> cumu_compaction_policy(
160
            std::string_view compaction_policy);
161
162
    void sync_storage_vault();
163
164
58.6k
    io::FileCacheBlockDownloader& file_cache_block_downloader() const {
165
58.6k
        return *_file_cache_block_downloader;
166
58.6k
    }
167
168
201k
    CloudWarmUpManager& cloud_warm_up_manager() const { return *_cloud_warm_up_manager; }
169
15
    std::shared_ptr<CloudWarmUpManager> cloud_warm_up_manager_ptr() const {
170
15
        return _cloud_warm_up_manager;
171
15
    }
172
173
1.45M
    TabletHotspot& tablet_hotspot() const { return *_tablet_hotspot; }
174
175
0
    ThreadPool& sync_load_for_tablets_thread_pool() const {
176
0
        return *_sync_load_for_tablets_thread_pool;
177
0
    }
178
179
0
    ThreadPool& warmup_cache_async_thread_pool() const { return *_warmup_cache_async_thread_pool; }
180
181
    Status register_compaction_stop_token(CloudTabletSPtr tablet, int64_t initiator);
182
183
    Status unregister_compaction_stop_token(CloudTabletSPtr tablet, bool clear_ms);
184
185
    bool register_index_change_compaction(std::shared_ptr<CloudIndexChangeCompaction> compact,
186
                                          int64_t tablet_id, bool is_base_compact,
187
                                          std::string& err_reason);
188
189
    void unregister_index_change_compaction(int64_t tablet_id, bool is_base_compact);
190
191
356
    std::chrono::time_point<std::chrono::system_clock> startup_timepoint() const {
192
356
        return _startup_timepoint;
193
356
    }
194
195
#ifdef BE_TEST
196
    void set_startup_timepoint(const std::chrono::time_point<std::chrono::system_clock>& tp) {
197
        _startup_timepoint = tp;
198
    }
199
200
    void set_cloud_warm_up_manager(std::unique_ptr<CloudWarmUpManager> manager);
201
#endif
202
203
private:
204
    void _refresh_storage_vault_info_thread_callback();
205
    void _vacuum_stale_rowsets_thread_callback();
206
    void _sync_tablets_thread_callback();
207
    void _compaction_tasks_producer_callback();
208
    std::vector<CloudTabletSPtr> _generate_cloud_compaction_tasks(CompactionType compaction_type,
209
                                                                  bool check_score);
210
    Status _adjust_compaction_thread_num();
211
    Status _submit_base_compaction_task(const CloudTabletSPtr& tablet, int trigger_method = 0);
212
    Status _submit_cumulative_compaction_task(const CloudTabletSPtr& tablet,
213
                                              int trigger_method = 0);
214
    Status _submit_full_compaction_task(const CloudTabletSPtr& tablet, int trigger_method = 0);
215
    Status _request_tablet_global_compaction_lock(ReaderType compaction_type,
216
                                                  const CloudTabletSPtr& tablet,
217
                                                  std::shared_ptr<CloudCompactionMixin> compaction);
218
    Status _check_all_root_path_cluster_id();
219
    void _lease_compaction_thread_callback();
220
    void _check_tablet_delete_bitmap_score_callback();
221
222
    std::atomic_bool _stopped {false};
223
224
    std::unique_ptr<cloud::CloudMetaMgr> _meta_mgr;
225
    std::unique_ptr<CloudTabletMgr> _tablet_mgr;
226
    std::unique_ptr<CloudTxnDeleteBitmapCache> _txn_delete_bitmap_cache;
227
    std::unique_ptr<CloudCommittedRSMgr> _committed_rs_mgr;
228
    std::unique_ptr<ThreadPool> _calc_tablet_delete_bitmap_task_thread_pool;
229
    std::unique_ptr<ThreadPool> _sync_delete_bitmap_thread_pool;
230
231
    // Components for cache warmup
232
    std::unique_ptr<io::FileCacheBlockDownloader> _file_cache_block_downloader;
233
    // Depended by `FileCacheBlockDownloader`
234
    std::shared_ptr<CloudWarmUpManager> _cloud_warm_up_manager;
235
    std::unique_ptr<TabletHotspot> _tablet_hotspot;
236
    std::unique_ptr<ThreadPool> _sync_load_for_tablets_thread_pool;
237
    std::unique_ptr<ThreadPool> _warmup_cache_async_thread_pool;
238
    std::unique_ptr<CloudSnapshotMgr> _cloud_snapshot_mgr;
239
240
    // FileSystem with latest shared storage info, new data will be written to this fs.
241
    mutable std::mutex _latest_fs_mtx;
242
    io::RemoteFileSystemSPtr _latest_fs;
243
244
    std::vector<std::shared_ptr<Thread>> _bg_threads;
245
246
    // ATTN: Compactions in maps depend on `CloudTabletMgr` and `CloudMetaMgr`
247
    mutable std::mutex _compaction_mtx;
248
    mutable std::mutex _cumu_compaction_delay_mtx;
249
    // tablet_id -> submitted base compaction, guarded by `_compaction_mtx`
250
    std::unordered_map<int64_t, std::shared_ptr<CloudBaseCompaction>> _submitted_base_compactions;
251
    // tablet_id -> submitted full compaction, guarded by `_compaction_mtx`
252
    std::unordered_map<int64_t, std::shared_ptr<CloudFullCompaction>> _submitted_full_compactions;
253
    // Store tablets which are preparing cumu compaction, guarded by `_compaction_mtx`
254
    std::unordered_set<int64_t> _tablet_preparing_cumu_compaction;
255
    // tablet_id -> submitted cumu compactions, guarded by `_compaction_mtx`
256
    std::unordered_map<int64_t, std::vector<std::shared_ptr<CloudCumulativeCompaction>>>
257
            _submitted_cumu_compactions;
258
    // tablet_id -> active compaction stop tokens
259
    std::unordered_map<int64_t, std::shared_ptr<CloudCompactionStopToken>>
260
            _active_compaction_stop_tokens;
261
    // tablet_id -> executing cumu compactions, guarded by `_compaction_mtx`
262
    std::unordered_map<int64_t, std::vector<std::shared_ptr<CloudCumulativeCompaction>>>
263
            _executing_cumu_compactions;
264
    // tablet_id -> executing base compactions, guarded by `_compaction_mtx`
265
    std::unordered_map<int64_t, std::shared_ptr<CloudBaseCompaction>> _executing_base_compactions;
266
    // tablet_id -> executing full compactions, guarded by `_compaction_mtx`
267
    std::unordered_map<int64_t, std::shared_ptr<CloudFullCompaction>> _executing_full_compactions;
268
269
    // for index change compaction
270
    std::unordered_map<int64_t, std::shared_ptr<CloudIndexChangeCompaction>>
271
            _submitted_index_change_cumu_compaction;
272
    std::unordered_map<int64_t, std::shared_ptr<CloudIndexChangeCompaction>>
273
            _submitted_index_change_base_compaction;
274
275
    using CumuPolices =
276
            std::unordered_map<std::string_view, std::shared_ptr<CloudCumulativeCompactionPolicy>>;
277
    CumuPolices _cumulative_compaction_policies;
278
279
    std::atomic_bool first_sync_storage_vault {true};
280
281
    EngineOptions _options;
282
    std::mutex _store_lock;
283
284
    std::chrono::time_point<std::chrono::system_clock> _startup_timepoint;
285
};
286
287
} // namespace doris