Coverage Report

Created: 2026-08-03 13:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/cloud_meta_mgr.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
#pragma once
18
19
#include <gen_cpp/olap_file.pb.h>
20
21
#include <future>
22
#include <memory>
23
#include <string>
24
#include <tuple>
25
#include <variant>
26
#include <vector>
27
28
#include "cloud/cloud_tablet.h"
29
#include "common/status.h"
30
#include "storage/rowset/rowset_fwd.h"
31
#include "storage/rowset/rowset_meta.h"
32
#include "util/s3_util.h"
33
34
namespace doris {
35
36
class DeleteBitmap;
37
class StreamLoadContext;
38
class CloudTablet;
39
class TabletMeta;
40
class TabletSchema;
41
class TabletMetaPB;
42
class RowsetMeta;
43
44
namespace cloud {
45
46
class FinishTabletJobResponse;
47
class StartTabletJobResponse;
48
class TabletJobInfoPB;
49
class TabletStatsPB;
50
class TabletIndexPB;
51
class HostLevelMSRpcRateLimiters;
52
class MSBackpressureHandler;
53
54
using StorageVaultInfos = std::vector<
55
        std::tuple<std::string, std::variant<S3Conf, HdfsVaultInfo>, StorageVaultPB_PathFormat>>;
56
57
// run tasks in bthread with concurrency and wait until all tasks done
58
// it stops running tasks if there are any tasks return !ok, leaving some tasks untouched
59
// return OK if all tasks successfully done, otherwise return the result of the failed task
60
Status bthread_fork_join(const std::vector<std::function<Status()>>& tasks, int concurrency);
61
62
// An async wrap of `bthread_fork_join` declared previously using promise-future
63
// return OK if fut successfully created, otherwise return error
64
Status bthread_fork_join(std::vector<std::function<Status()>>&& tasks, int concurrency,
65
                         std::future<Status>* fut);
66
67
// Returns the exact actual_code when recognized. An unknown actual_code uses an explicit non-OK
68
// legacy fallback and otherwise fails closed. Responses from a legacy Meta Service use code.
69
// Exposed for unit tests.
70
MetaServiceCode get_response_code(const MetaServiceResponseStatus& status);
71
72
class CloudMetaMgr {
73
public:
74
246
    CloudMetaMgr() = default;
75
    ~CloudMetaMgr() = default;
76
    CloudMetaMgr(const CloudMetaMgr&) = delete;
77
    CloudMetaMgr& operator=(const CloudMetaMgr&) = delete;
78
79
    Status get_tablet_meta(int64_t tablet_id, std::shared_ptr<TabletMeta>* tablet_meta);
80
81
    Status sync_tablet_rowsets(CloudTablet* tablet, const SyncOptions& options = {},
82
                               SyncRowsetStats* sync_stats = nullptr);
83
    Status sync_tablet_rowsets_unlocked(
84
            CloudTablet* tablet, std::unique_lock<bthread::Mutex>& lock /* _sync_meta_lock */,
85
            const SyncOptions& options = {}, SyncRowsetStats* sync_stats = nullptr);
86
87
    Status prepare_rowset(const RowsetMeta& rs_meta, const std::string& job_id, int64_t table_id,
88
                          std::shared_ptr<RowsetMeta>* existed_rs_meta = nullptr);
89
90
    Status commit_rowset(RowsetMeta& rs_meta, const std::string& job_id, int64_t table_id,
91
                         std::shared_ptr<RowsetMeta>* existed_rs_meta = nullptr);
92
    void cache_committed_rowset(RowsetMetaSharedPtr rs_meta, int64_t expiration_time);
93
94
    Status update_tmp_rowset(const RowsetMeta& rs_meta, int64_t table_id);
95
96
    Status update_packed_file_info(const std::string& packed_file_path,
97
                                   const cloud::PackedFileInfoPB& packed_file_info,
98
                                   int64_t table_id);
99
100
    Status commit_txn(const StreamLoadContext& ctx, bool is_2pc);
101
102
    Status abort_txn(const StreamLoadContext& ctx);
103
104
    Status precommit_txn(const StreamLoadContext& ctx);
105
106
    /**
107
     * Prepares a restore job for a tablet to meta-service
108
     * Change the state to PREPARED
109
     * PREPARED state means the meta of tablet has been uploaded but not finalized.
110
     */
111
    Status prepare_restore_job(const TabletMetaPB& tablet_meta);
112
113
    /**
114
     * Commits a restore job for a tablet to meta-service
115
     * Change the state from PREPARED to COMMITTED
116
     * COMMITTED state means the meta of tablet has been finalized.
117
     */
118
    Status commit_restore_job(const int64_t tablet_id);
119
120
    /**
121
     * Finish a restore job for a tablet from meta-service
122
     * Change the state to final state.
123
     * If is_completed = true, change the state from COMMITTED to COMPLETED
124
     * If is_completed = false, change the state to from PREPARED/COMMITTED to DROPPED
125
     * COMPLETED state means the job is finished, the restored data should be visible.
126
     * DROPPED state means the job is aborted.
127
     * COMPLETED/DROPPED are the final states, jobs with final states will be recycled.
128
     */
129
    Status finish_restore_job(const int64_t tablet_id, bool is_completed);
130
131
    /**
132
     * Gets storage vault (storage backends) from meta-service
133
     * 
134
     * @param vault_info output param, all storage backends
135
     * @param is_vault_mode output param, true for pure vault mode, false for legacy mode
136
     * @return status
137
     */
138
    Status get_storage_vault_info(StorageVaultInfos* vault_infos, bool* is_vault_mode);
139
140
    Status prepare_tablet_job(const TabletJobInfoPB& job, StartTabletJobResponse* res);
141
142
    Status commit_tablet_job(const TabletJobInfoPB& job, FinishTabletJobResponse* res);
143
144
    Status abort_tablet_job(const TabletJobInfoPB& job);
145
146
    Status lease_tablet_job(const TabletJobInfoPB& job);
147
148
    Status update_delete_bitmap(const CloudTablet& tablet, int64_t lock_id, int64_t initiator,
149
                                DeleteBitmap* delete_bitmap, DeleteBitmap* delete_bitmap_v2,
150
                                std::string rowset_id,
151
                                std::optional<StorageResource> storage_resource,
152
                                int64_t store_version, int64_t table_id, int64_t txn_id = -1,
153
                                bool is_explicit_txn = false, int64_t next_visible_version = -1);
154
155
    Status cloud_update_delete_bitmap_without_lock(
156
            const CloudTablet& tablet, DeleteBitmap* delete_bitmap,
157
            std::map<std::string, int64_t>& rowset_to_versions, int64_t table_id,
158
            int64_t pre_rowset_agg_start_version = 0, int64_t pre_rowset_agg_end_version = 0);
159
160
    Status get_delete_bitmap_update_lock(const CloudTablet& tablet, int64_t lock_id,
161
                                         int64_t initiator);
162
163
    void remove_delete_bitmap_update_lock(int64_t table_id, int64_t lock_id, int64_t initiator,
164
                                          int64_t tablet_id);
165
166
    // Fill version holes by creating empty rowsets for missing versions
167
    Status fill_version_holes(CloudTablet* tablet, int64_t max_version,
168
                              std::unique_lock<BthreadSharedMutex>& wlock);
169
170
    // Create an empty rowset to fill a version hole
171
    Status create_empty_rowset_for_hole(CloudTablet* tablet, int64_t version,
172
                                        RowsetMetaSharedPtr prev_rowset_meta,
173
                                        RowsetSharedPtr* rowset);
174
175
    Status list_snapshot(std::vector<SnapshotInfoPB>& snapshots);
176
    Status get_snapshot_properties(SnapshotSwitchStatus& switch_status,
177
                                   int64_t& max_reserved_snapshots,
178
                                   int64_t& snapshot_interval_seconds);
179
180
    // Get all cluster status for the instance
181
    // Returns cluster_id -> (status, mtime_ms)
182
    // If my_cluster_id is not null, also returns the requesting node's cluster_id
183
    Status get_cluster_status(std::unordered_map<std::string, std::pair<int32_t, int64_t>>* result,
184
                              std::string* my_cluster_id = nullptr);
185
186
0
    void set_host_level_ms_rpc_rate_limiters(HostLevelMSRpcRateLimiters* limiters) {
187
0
        host_level_ms_rpc_rate_limiters_ = limiters;
188
0
    }
189
190
0
    void set_ms_backpressure_handler(MSBackpressureHandler* handler) {
191
0
        ms_backpressure_handler_ = handler;
192
0
    }
193
194
private:
195
    bool sync_tablet_delete_bitmap_by_cache(CloudTablet* tablet, std::ranges::range auto&& rs_metas,
196
                                            DeleteBitmap* delete_bitmap);
197
198
    Status sync_tablet_delete_bitmap(CloudTablet* tablet, int64_t old_max_version,
199
                                     std::ranges::range auto&& rs_metas, const TabletStatsPB& stats,
200
                                     const TabletIndexPB& idx, DeleteBitmap* delete_bitmap,
201
                                     bool full_sync, SyncRowsetStats* sync_stats,
202
                                     int32_t read_version, bool full_sync_v2);
203
    Status _read_tablet_delete_bitmap_v2(CloudTablet* tablet, int64_t old_max_version,
204
                                         std::ranges::range auto&& rs_metas,
205
                                         DeleteBitmap* delete_bitmap, GetDeleteBitmapResponse& res,
206
                                         int64_t& remote_delete_bitmap_bytes, bool full_sync_v2);
207
    Status _log_mow_delete_bitmap(CloudTablet* tablet, GetRowsetResponse& resp,
208
                                  DeleteBitmap& delete_bitmap, int64_t old_max_version,
209
                                  bool full_sync, int32_t read_version);
210
    Status _check_delete_bitmap_v2_correctness(CloudTablet* tablet, GetRowsetRequest& req,
211
                                               GetRowsetResponse& resp, int64_t old_max_version);
212
213
    Status _get_delete_bitmap_from_ms(GetDeleteBitmapRequest& req, GetDeleteBitmapResponse& res);
214
    Status _get_delete_bitmap_from_ms_by_batch(GetDeleteBitmapRequest& req,
215
                                               GetDeleteBitmapResponse& res,
216
                                               int64_t bytes_threadhold);
217
218
    void check_table_size_correctness(RowsetMeta& rs_meta);
219
    int64_t get_segment_file_size(RowsetMeta& rs_meta);
220
    int64_t get_inverted_index_file_size(RowsetMeta& rs_meta);
221
222
    HostLevelMSRpcRateLimiters* host_level_ms_rpc_rate_limiters_ {nullptr};
223
    MSBackpressureHandler* ms_backpressure_handler_ {nullptr};
224
};
225
226
} // namespace cloud
227
} // namespace doris