Coverage Report

Created: 2026-03-16 19:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/task/engine_clone_task.cpp
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
#include "storage/task/engine_clone_task.h"
19
20
#include <absl/strings/str_split.h>
21
#include <curl/curl.h>
22
#include <fcntl.h>
23
#include <fmt/format.h>
24
#include <gen_cpp/AgentService_types.h>
25
#include <gen_cpp/BackendService.h>
26
#include <gen_cpp/HeartbeatService_types.h>
27
#include <gen_cpp/MasterService_types.h>
28
#include <gen_cpp/Status_types.h>
29
#include <gen_cpp/Types_constants.h>
30
#include <sys/stat.h>
31
32
#include <filesystem>
33
#include <memory>
34
#include <mutex>
35
#include <ostream>
36
#include <set>
37
#include <shared_mutex>
38
#include <system_error>
39
#include <unordered_map>
40
#include <unordered_set>
41
#include <utility>
42
#include <vector>
43
44
#include "common/config.h"
45
#include "common/logging.h"
46
#include "io/fs/file_system.h"
47
#include "io/fs/local_file_system.h"
48
#include "io/fs/path.h"
49
#include "runtime/memory/mem_tracker_limiter.h"
50
#include "runtime/thread_context.h"
51
#include "service/http/http_client.h"
52
#include "service/http/utils.h"
53
#include "storage/data_dir.h"
54
#include "storage/olap_common.h"
55
#include "storage/olap_define.h"
56
#include "storage/pb_helper.h"
57
#include "storage/rowset/rowset.h"
58
#include "storage/snapshot/snapshot_manager.h"
59
#include "storage/storage_engine.h"
60
#include "storage/tablet/tablet.h"
61
#include "storage/tablet/tablet_manager.h"
62
#include "storage/tablet/tablet_meta.h"
63
#include "util/client_cache.h"
64
#include "util/debug_points.h"
65
#include "util/defer_op.h"
66
#include "util/network_util.h"
67
#include "util/security.h"
68
#include "util/stopwatch.hpp"
69
#include "util/thrift_rpc_helper.h"
70
#include "util/trace.h"
71
72
using std::set;
73
using std::stringstream;
74
75
namespace doris {
76
using namespace ErrorCode;
77
78
namespace {
79
/// if binlog file exist, then check if binlog file md5sum equal
80
/// if equal, then skip link file
81
/// if not equal, then return error
82
/// return value: if binlog file not exist, then return to binlog file path
83
Result<std::string> check_dest_binlog_valid(const std::string& tablet_dir,
84
                                            const std::string& clone_dir,
85
0
                                            const std::string& clone_file, bool* skip_link_file) {
86
0
    std::string from, to;
87
0
    std::string new_clone_file = clone_file;
88
0
    if (clone_file.ends_with(".binlog")) {
89
        // change clone_file suffix from .binlog to .dat
90
0
        new_clone_file.replace(clone_file.size() - 7, 7, ".dat");
91
0
    } else if (clone_file.ends_with(".binlog-index")) {
92
        // change clone_file suffix from .binlog-index to .idx
93
0
        new_clone_file.replace(clone_file.size() - 13, 13, ".idx");
94
0
    }
95
0
    from = fmt::format("{}/{}", clone_dir, clone_file);
96
0
    to = fmt::format("{}/_binlog/{}", tablet_dir, new_clone_file);
97
98
    // check to to file exist
99
0
    bool exists = true;
100
0
    auto status = io::global_local_filesystem()->exists(to, &exists);
101
0
    if (!status.ok()) {
102
0
        return ResultError(std::move(status));
103
0
    }
104
105
0
    if (!exists) {
106
0
        return to;
107
0
    }
108
109
0
    LOG(WARNING) << "binlog file already exist. "
110
0
                 << "tablet_dir=" << tablet_dir << ", clone_file=" << from << ", to=" << to;
111
112
0
    std::string clone_file_md5sum;
113
0
    status = io::global_local_filesystem()->md5sum(from, &clone_file_md5sum);
114
0
    if (!status.ok()) {
115
0
        return ResultError(std::move(status));
116
0
    }
117
0
    std::string to_file_md5sum;
118
0
    status = io::global_local_filesystem()->md5sum(to, &to_file_md5sum);
119
0
    if (!status.ok()) {
120
0
        return ResultError(std::move(status));
121
0
    }
122
123
0
    if (clone_file_md5sum == to_file_md5sum) {
124
        // if md5sum equal, then skip link file
125
0
        *skip_link_file = true;
126
0
        return to;
127
0
    } else {
128
0
        auto err_msg = fmt::format(
129
0
                "binlog file already exist, but md5sum not equal. "
130
0
                "tablet_dir={}, clone_file={}",
131
0
                tablet_dir, clone_file);
132
0
        LOG(WARNING) << err_msg;
133
0
        return ResultError(Status::InternalError(std::move(err_msg)));
134
0
    }
135
0
}
136
} // namespace
137
138
#define RETURN_IF_ERROR_(status, stmt) \
139
0
    do {                               \
140
0
        status = (stmt);               \
141
0
        if (UNLIKELY(!status.ok())) {  \
142
0
            return status;             \
143
0
        }                              \
144
0
    } while (false)
145
146
EngineCloneTask::EngineCloneTask(StorageEngine& engine, const TCloneReq& clone_req,
147
                                 const ClusterInfo* cluster_info, int64_t signature,
148
                                 std::vector<TTabletInfo>* tablet_infos)
149
0
        : _engine(engine),
150
0
          _clone_req(clone_req),
151
0
          _tablet_infos(tablet_infos),
152
0
          _signature(signature),
153
0
          _cluster_info(cluster_info) {
154
0
    _mem_tracker = MemTrackerLimiter::create_shared(
155
0
            MemTrackerLimiter::Type::OTHER,
156
0
            "EngineCloneTask#tabletId=" + std::to_string(_clone_req.tablet_id));
157
0
}
158
159
0
Status EngineCloneTask::execute() {
160
    // register the tablet to avoid it is deleted by gc thread during clone process
161
0
    Status st = _do_clone();
162
0
    _engine.tablet_manager()->update_partitions_visible_version(
163
0
            {{_clone_req.partition_id, _clone_req.version}});
164
0
    return st;
165
0
}
166
167
0
Status EngineCloneTask::_do_clone() {
168
0
    DBUG_EXECUTE_IF("EngineCloneTask.wait_clone", {
169
0
        auto duration = std::chrono::milliseconds(dp->param("duration", 10 * 1000));
170
0
        std::this_thread::sleep_for(duration);
171
0
    });
172
173
0
    DBUG_EXECUTE_IF("EngineCloneTask.failed_clone", {
174
0
        LOG_WARNING("EngineCloneTask.failed_clone")
175
0
                .tag("tablet_id", _clone_req.tablet_id)
176
0
                .tag("replica_id", _clone_req.replica_id)
177
0
                .tag("version", _clone_req.version);
178
0
        return Status::InternalError(
179
0
                "in debug point, EngineCloneTask.failed_clone tablet={}, replica={}, version={}",
180
0
                _clone_req.tablet_id, _clone_req.replica_id, _clone_req.version);
181
0
    });
182
0
    Status status = Status::OK();
183
0
    std::string src_file_path;
184
0
    TBackend src_host;
185
0
    RETURN_IF_ERROR(
186
0
            _engine.tablet_manager()->register_transition_tablet(_clone_req.tablet_id, "clone"));
187
0
    Defer defer {[&]() {
188
0
        _engine.tablet_manager()->unregister_transition_tablet(_clone_req.tablet_id, "clone");
189
0
    }};
190
191
    // Check local tablet exist or not
192
0
    TabletSharedPtr tablet = _engine.tablet_manager()->get_tablet(_clone_req.tablet_id);
193
194
    // The status of a tablet is not ready, indicating that it is a residual tablet after a schema
195
    // change failure. Clone a new tablet from remote be to overwrite it. This situation basically only
196
    // occurs when the be_rebalancer_fuzzy_test configuration is enabled.
197
0
    if (tablet && tablet->tablet_state() == TABLET_NOTREADY) {
198
0
        LOG(WARNING) << "tablet state is not ready when clone, need to drop old tablet, tablet_id="
199
0
                     << tablet->tablet_id();
200
0
        RETURN_IF_ERROR(_engine.tablet_manager()->drop_tablet(tablet->tablet_id(),
201
0
                                                              tablet->replica_id(), false));
202
0
        tablet.reset();
203
0
    }
204
0
    _is_new_tablet = tablet == nullptr;
205
    // try to incremental clone
206
0
    Versions missed_versions;
207
    // try to repair a tablet with missing version
208
0
    if (tablet != nullptr) {
209
0
        std::shared_lock migration_rlock(tablet->get_migration_lock(), std::try_to_lock);
210
0
        if (!migration_rlock.owns_lock()) {
211
0
            return Status::Error<TRY_LOCK_FAILED>(
212
0
                    "EngineCloneTask::_do_clone meet try lock failed");
213
0
        }
214
0
        if (tablet->replica_id() < _clone_req.replica_id) {
215
            // `tablet` may be a dropped replica in FE, e.g:
216
            //   BE1 migrates replica of tablet_1 to BE2, but before BE1 drop this replica, another new replica of tablet_1 is migrated to BE1.
217
            // Clone can still continue in this case. But to keep `replica_id` consitent with FE, MUST reset `replica_id` with request `replica_id`.
218
0
            tablet->tablet_meta()->set_replica_id(_clone_req.replica_id);
219
0
        }
220
221
        // get download path
222
0
        auto local_data_path = fmt::format("{}/{}", tablet->tablet_path(), CLONE_PREFIX);
223
0
        bool allow_incremental_clone = false;
224
225
0
        int64_t specified_version = _clone_req.version;
226
0
        if (tablet->enable_unique_key_merge_on_write()) {
227
0
            int64_t min_pending_ver = _engine.get_pending_publish_min_version(tablet->tablet_id());
228
0
            if (min_pending_ver - 1 < specified_version) {
229
0
                LOG(INFO) << "use min pending publish version for clone, min_pending_ver: "
230
0
                          << min_pending_ver << " visible_version: " << _clone_req.version;
231
0
                specified_version = min_pending_ver - 1;
232
0
            }
233
0
        }
234
235
0
        missed_versions = tablet->get_missed_versions(specified_version);
236
237
        // if missed version size is 0, then it is useless to clone from remote be, it means local data is
238
        // completed. Or remote be will just return header not the rowset files. clone will failed.
239
0
        if (missed_versions.empty()) {
240
0
            LOG(INFO) << "missed version size = 0, skip clone and return success. tablet_id="
241
0
                      << _clone_req.tablet_id << " replica_id=" << _clone_req.replica_id;
242
0
            RETURN_IF_ERROR(_set_tablet_info());
243
0
            return Status::OK();
244
0
        }
245
246
0
        LOG(INFO) << "clone to existed tablet. missed_versions_size=" << missed_versions.size()
247
0
                  << ", allow_incremental_clone=" << allow_incremental_clone
248
0
                  << ", signature=" << _signature << ", tablet_id=" << _clone_req.tablet_id
249
0
                  << ", visible_version=" << _clone_req.version
250
0
                  << ", replica_id=" << _clone_req.replica_id;
251
252
        // try to download missing version from src backend.
253
        // if tablet on src backend does not contains missing version, it will download all versions,
254
        // and set allow_incremental_clone to false
255
0
        RETURN_IF_ERROR(_make_and_download_snapshots(*(tablet->data_dir()), local_data_path,
256
0
                                                     &src_host, &src_file_path, missed_versions,
257
0
                                                     &allow_incremental_clone));
258
0
        RETURN_IF_ERROR(_finish_clone(tablet.get(), local_data_path, specified_version,
259
0
                                      allow_incremental_clone));
260
0
    } else {
261
0
        LOG(INFO) << "clone tablet not exist, begin clone a new tablet from remote be. "
262
0
                  << "signature=" << _signature << ", tablet_id=" << _clone_req.tablet_id
263
0
                  << ", visible_version=" << _clone_req.version
264
0
                  << ", req replica=" << _clone_req.replica_id;
265
        // create a new tablet in this be
266
        // Get local disk from olap
267
0
        std::string local_shard_root_path;
268
0
        DataDir* store = nullptr;
269
0
        RETURN_IF_ERROR(_engine.obtain_shard_path(_clone_req.storage_medium,
270
0
                                                  _clone_req.dest_path_hash, &local_shard_root_path,
271
0
                                                  &store, _clone_req.partition_id));
272
0
        auto tablet_dir = fmt::format("{}/{}/{}", local_shard_root_path, _clone_req.tablet_id,
273
0
                                      _clone_req.schema_hash);
274
275
0
        Defer remove_useless_dir {[&] {
276
0
            if (status.ok()) {
277
0
                return;
278
0
            }
279
0
            LOG(INFO) << "clone failed. want to delete local dir: " << tablet_dir
280
0
                      << ". signature: " << _signature;
281
0
            WARN_IF_ERROR(io::global_local_filesystem()->delete_directory(tablet_dir),
282
0
                          "failed to delete useless clone dir ");
283
0
            WARN_IF_ERROR(DataDir::delete_tablet_parent_path_if_empty(tablet_dir),
284
0
                          "failed to delete parent dir");
285
0
        }};
286
287
0
        bool exists = true;
288
0
        Status exists_st = io::global_local_filesystem()->exists(tablet_dir, &exists);
289
0
        if (!exists_st) {
290
0
            LOG(WARNING) << "cant get path=" << tablet_dir << " state, st=" << exists_st;
291
0
            return exists_st;
292
0
        }
293
0
        if (exists) {
294
0
            LOG(WARNING) << "before clone dest path=" << tablet_dir << " exist, remove it first";
295
0
            RETURN_IF_ERROR(io::global_local_filesystem()->delete_directory(tablet_dir));
296
0
        }
297
298
0
        bool allow_incremental_clone = false;
299
0
        RETURN_IF_ERROR_(status,
300
0
                         _make_and_download_snapshots(*store, tablet_dir, &src_host, &src_file_path,
301
0
                                                      missed_versions, &allow_incremental_clone));
302
303
0
        LOG(INFO) << "clone copy done. src_host: " << src_host.host
304
0
                  << " src_file_path: " << src_file_path;
305
0
        auto tablet_manager = _engine.tablet_manager();
306
0
        RETURN_IF_ERROR_(status, tablet_manager->load_tablet_from_dir(store, _clone_req.tablet_id,
307
0
                                                                      _clone_req.schema_hash,
308
0
                                                                      tablet_dir, false));
309
0
        auto nested_tablet = tablet_manager->get_tablet(_clone_req.tablet_id);
310
0
        if (!nested_tablet) {
311
0
            status = Status::NotFound("tablet not found, tablet_id={}", _clone_req.tablet_id);
312
0
            return status;
313
0
        }
314
        // MUST reset `replica_id` to request `replica_id` to keep consistent with FE
315
0
        nested_tablet->tablet_meta()->set_replica_id(_clone_req.replica_id);
316
        // clone success, delete .hdr file because tablet meta is stored in rocksdb
317
0
        std::string header_path =
318
0
                TabletMeta::construct_header_file_path(tablet_dir, _clone_req.tablet_id);
319
0
        RETURN_IF_ERROR(io::global_local_filesystem()->delete_file(header_path));
320
0
    }
321
322
0
    return _set_tablet_info();
323
0
}
324
325
0
Status EngineCloneTask::_set_tablet_info() {
326
    // Get clone tablet info
327
0
    TTabletInfo tablet_info;
328
0
    tablet_info.__set_tablet_id(_clone_req.tablet_id);
329
0
    tablet_info.__set_replica_id(_clone_req.replica_id);
330
0
    tablet_info.__set_schema_hash(_clone_req.schema_hash);
331
0
    RETURN_IF_ERROR(_engine.tablet_manager()->report_tablet_info(&tablet_info));
332
0
    if (_clone_req.__isset.version && tablet_info.version < _clone_req.version) {
333
        // if it is a new tablet and clone failed, then remove the tablet
334
        // if it is incremental clone, then must not drop the tablet
335
0
        if (_is_new_tablet) {
336
            // we need to check if this cloned table's version is what we expect.
337
            // if not, maybe this is a stale remaining table which is waiting for drop.
338
            // we drop it.
339
0
            LOG(WARNING) << "begin to drop the stale tablet. tablet_id:" << _clone_req.tablet_id
340
0
                         << ", replica_id:" << _clone_req.replica_id
341
0
                         << ", schema_hash:" << _clone_req.schema_hash
342
0
                         << ", signature:" << _signature << ", version:" << tablet_info.version
343
0
                         << ", expected_version: " << _clone_req.version;
344
0
            WARN_IF_ERROR(_engine.tablet_manager()->drop_tablet(_clone_req.tablet_id,
345
0
                                                                _clone_req.replica_id, false),
346
0
                          "drop stale cloned table failed");
347
0
        }
348
0
        return Status::InternalError("unexpected version. tablet version: {}, expected version: {}",
349
0
                                     tablet_info.version, _clone_req.version);
350
0
    }
351
0
    LOG(INFO) << "clone get tablet info success. tablet_id:" << _clone_req.tablet_id
352
0
              << ", schema_hash:" << _clone_req.schema_hash << ", signature:" << _signature
353
0
              << ", replica id:" << _clone_req.replica_id << ", version:" << tablet_info.version;
354
0
    _tablet_infos->push_back(tablet_info);
355
0
    return Status::OK();
356
0
}
357
358
/// This method will do following things:
359
/// 1. Make snapshots on source BE.
360
/// 2. Download all snapshots to CLONE dir.
361
/// 3. Convert rowset ids of downloaded snapshots(would also change the replica id).
362
/// 4. Release the snapshots on source BE.
363
Status EngineCloneTask::_make_and_download_snapshots(DataDir& data_dir,
364
                                                     const std::string& local_data_path,
365
                                                     TBackend* src_host, std::string* snapshot_path,
366
                                                     const std::vector<Version>& missed_versions,
367
0
                                                     bool* allow_incremental_clone) {
368
0
    Status status;
369
370
0
    const auto& token = _cluster_info->token;
371
372
0
    int timeout_s = 0;
373
0
    if (_clone_req.__isset.timeout_s) {
374
0
        timeout_s = _clone_req.timeout_s;
375
0
    }
376
377
0
    for (auto&& src : _clone_req.src_backends) {
378
        // Make snapshot in remote olap engine
379
0
        *src_host = src;
380
        // make snapshot
381
0
        status = _make_snapshot(src.host, src.be_port, _clone_req.tablet_id, _clone_req.schema_hash,
382
0
                                timeout_s, missed_versions, snapshot_path, allow_incremental_clone);
383
0
        if (!status.ok()) [[unlikely]] {
384
0
            LOG_WARNING("failed to make snapshot in remote BE")
385
0
                    .tag("host", src.host)
386
0
                    .tag("port", src.be_port)
387
0
                    .tag("tablet", _clone_req.tablet_id)
388
0
                    .tag("signature", _signature)
389
0
                    .tag("missed_versions", missed_versions)
390
0
                    .error(status);
391
0
            continue; // Try another BE
392
0
        }
393
0
        LOG_INFO("successfully make snapshot in remote BE")
394
0
                .tag("host", src.host)
395
0
                .tag("port", src.be_port)
396
0
                .tag("tablet", _clone_req.tablet_id)
397
0
                .tag("snapshot_path", *snapshot_path)
398
0
                .tag("signature", _signature)
399
0
                .tag("missed_versions", missed_versions);
400
0
        Defer defer {[host = src.host, port = src.be_port, &snapshot_path = *snapshot_path, this] {
401
            // TODO(plat1ko): Async release snapshot
402
0
            auto st = _release_snapshot(host, port, snapshot_path);
403
0
            if (!st.ok()) [[unlikely]] {
404
0
                LOG_WARNING("failed to release snapshot in remote BE")
405
0
                        .tag("host", host)
406
0
                        .tag("port", port)
407
0
                        .tag("snapshot_path", snapshot_path)
408
0
                        .error(st);
409
0
            }
410
0
        }};
411
412
0
        std::string remote_dir;
413
0
        {
414
0
            std::stringstream ss;
415
0
            if (snapshot_path->back() == '/') {
416
0
                ss << *snapshot_path << _clone_req.tablet_id << "/" << _clone_req.schema_hash
417
0
                   << "/";
418
0
            } else {
419
0
                ss << *snapshot_path << "/" << _clone_req.tablet_id << "/" << _clone_req.schema_hash
420
0
                   << "/";
421
0
            }
422
0
            remote_dir = ss.str();
423
0
        }
424
425
0
        std::string address = get_host_port(src.host, src.http_port);
426
0
        if (config::enable_batch_download && is_support_batch_download(address).ok()) {
427
            // download files via batch api.
428
0
            LOG_INFO("remote BE supports batch download, use batch file download")
429
0
                    .tag("address", address)
430
0
                    .tag("remote_dir", remote_dir);
431
0
            status = _batch_download_files(&data_dir, address, remote_dir, local_data_path);
432
0
            if (!status.ok()) [[unlikely]] {
433
0
                LOG_WARNING("failed to download snapshot from remote BE in batch")
434
0
                        .tag("address", address)
435
0
                        .tag("remote_dir", remote_dir)
436
0
                        .error(status);
437
0
                continue; // Try another BE
438
0
            }
439
0
        } else {
440
0
            if (config::enable_batch_download) {
441
0
                LOG_INFO("remote BE does not support batch download, use single file download")
442
0
                        .tag("address", address)
443
0
                        .tag("remote_dir", remote_dir);
444
0
            } else {
445
0
                LOG_INFO("batch download is disabled, use single file download")
446
0
                        .tag("address", address)
447
0
                        .tag("remote_dir", remote_dir);
448
0
            }
449
450
0
            std::string remote_url_prefix;
451
0
            {
452
0
                std::stringstream ss;
453
0
                ss << "http://" << address << HTTP_REQUEST_PREFIX << HTTP_REQUEST_TOKEN_PARAM
454
0
                   << token << HTTP_REQUEST_FILE_PARAM << remote_dir;
455
0
                remote_url_prefix = ss.str();
456
0
            }
457
458
0
            status = _download_files(&data_dir, remote_url_prefix, local_data_path);
459
0
            if (!status.ok()) [[unlikely]] {
460
0
                LOG_WARNING("failed to download snapshot from remote BE")
461
0
                        .tag("url", mask_token(remote_url_prefix))
462
0
                        .error(status);
463
0
                continue; // Try another BE
464
0
            }
465
0
        }
466
467
        // No need to try again with another BE
468
0
        _pending_rs_guards = DORIS_TRY(_engine.snapshot_mgr()->convert_rowset_ids(
469
0
                local_data_path, _clone_req.tablet_id, _clone_req.replica_id, _clone_req.table_id,
470
0
                _clone_req.partition_id, _clone_req.schema_hash));
471
0
        break;
472
0
    } // clone copy from one backend
473
0
    return status;
474
0
}
475
476
Status EngineCloneTask::_make_snapshot(const std::string& ip, int port, TTableId tablet_id,
477
                                       TSchemaHash schema_hash, int timeout_s,
478
                                       const std::vector<Version>& missed_versions,
479
0
                                       std::string* snapshot_path, bool* allow_incremental_clone) {
480
0
    TSnapshotRequest request;
481
0
    request.__set_tablet_id(tablet_id);
482
0
    request.__set_schema_hash(schema_hash);
483
0
    request.__set_preferred_snapshot_version(g_Types_constants.TPREFER_SNAPSHOT_REQ_VERSION);
484
0
    request.__set_version(_clone_req.version);
485
0
    request.__set_is_copy_binlog(true);
486
    // TODO: missing version composed of singleton delta.
487
    // if not, this place should be rewrote.
488
    // we make every TSnapshotRequest sent from be with __isset.missing_version = true
489
    // then if one be received one req with __isset.missing_version = false it means
490
    // this req is sent from FE(FE would never set this field)
491
0
    request.__isset.missing_version = true;
492
0
    for (auto& version : missed_versions) {
493
0
        request.missing_version.push_back(version.first);
494
0
    }
495
0
    if (timeout_s > 0) {
496
0
        request.__set_timeout(timeout_s);
497
0
    }
498
499
0
    TAgentResult result;
500
0
    RETURN_IF_ERROR(ThriftRpcHelper::rpc<BackendServiceClient>(
501
0
            ip, port, [&request, &result](BackendServiceConnection& client) {
502
0
                client->make_snapshot(result, request);
503
0
            }));
504
0
    if (result.status.status_code != TStatusCode::OK) {
505
0
        return Status::create(result.status);
506
0
    }
507
508
0
    if (!result.__isset.snapshot_path) {
509
0
        return Status::InternalError("success snapshot request without snapshot path");
510
0
    }
511
0
    *snapshot_path = result.snapshot_path;
512
0
    if (snapshot_path->at(snapshot_path->length() - 1) != '/') {
513
0
        snapshot_path->append("/");
514
0
    }
515
516
0
    if (result.__isset.allow_incremental_clone) {
517
        // During upgrading, some BE nodes still be installed an old previous old.
518
        // which incremental clone is not ready in those nodes.
519
        // should add a symbol to indicate it.
520
0
        *allow_incremental_clone = result.allow_incremental_clone;
521
0
    }
522
0
    return Status::OK();
523
0
}
524
525
Status EngineCloneTask::_release_snapshot(const std::string& ip, int port,
526
0
                                          const std::string& snapshot_path) {
527
0
    TAgentResult result;
528
0
    RETURN_IF_ERROR(ThriftRpcHelper::rpc<BackendServiceClient>(
529
0
            ip, port, [&snapshot_path, &result](BackendServiceConnection& client) {
530
0
                client->release_snapshot(result, snapshot_path);
531
0
            }));
532
0
    return Status::create(result.status);
533
0
}
534
535
Status EngineCloneTask::_download_files(DataDir* data_dir, const std::string& remote_url_prefix,
536
0
                                        const std::string& local_path) {
537
    // Check local path exist, if exist, remove it, then create the dir
538
    // local_file_full_path = tabletid/clone, for a specific tablet, there should be only one folder
539
    // if this folder exists, then should remove it
540
    // for example, BE clone from BE 1 to download file 1 with version (2,2), but clone from BE 1 failed
541
    // then it will try to clone from BE 2, but it will find the file 1 already exist, but file 1 with same
542
    // name may have different versions.
543
0
    RETURN_IF_ERROR(io::global_local_filesystem()->delete_directory(local_path));
544
0
    RETURN_IF_ERROR(io::global_local_filesystem()->create_directory(local_path));
545
546
    // Get remote dir file list
547
0
    std::string file_list_str;
548
0
    auto list_files_cb = [&remote_url_prefix, &file_list_str](HttpClient* client) {
549
0
        RETURN_IF_ERROR(client->init(remote_url_prefix));
550
0
        client->set_timeout_ms(LIST_REMOTE_FILE_TIMEOUT * 1000);
551
0
        return client->execute(&file_list_str);
552
0
    };
553
0
    RETURN_IF_ERROR(HttpClient::execute_with_retry(DOWNLOAD_FILE_MAX_RETRY, 1, list_files_cb));
554
0
    std::vector<std::string> file_name_list =
555
0
            absl::StrSplit(file_list_str, "\n", absl::SkipWhitespace());
556
557
    // If the header file is not exist, the table couldn't loaded by olap engine.
558
    // Avoid of data is not complete, we copy the header file at last.
559
    // The header file's name is end of .hdr.
560
0
    for (int i = 0; i + 1 < file_name_list.size(); ++i) {
561
0
        if (file_name_list[i].ends_with(".hdr")) {
562
0
            std::swap(file_name_list[i], file_name_list[file_name_list.size() - 1]);
563
0
            break;
564
0
        }
565
0
    }
566
567
    // Get copy from remote
568
0
    uint64_t total_file_size = 0;
569
0
    MonotonicStopWatch watch;
570
0
    watch.start();
571
0
    for (auto& file_name : file_name_list) {
572
0
        auto remote_file_url = remote_url_prefix + file_name;
573
574
        // get file length
575
0
        uint64_t file_size = 0;
576
0
        auto get_file_size_cb = [&remote_file_url, &file_size](HttpClient* client) {
577
0
            RETURN_IF_ERROR(client->init(remote_file_url));
578
0
            client->set_timeout_ms(GET_LENGTH_TIMEOUT * 1000);
579
0
            RETURN_IF_ERROR(client->head());
580
0
            RETURN_IF_ERROR(client->get_content_length(&file_size));
581
0
            return Status::OK();
582
0
        };
583
0
        RETURN_IF_ERROR(
584
0
                HttpClient::execute_with_retry(DOWNLOAD_FILE_MAX_RETRY, 1, get_file_size_cb));
585
        // check disk capacity
586
0
        if (data_dir->reach_capacity_limit(file_size)) {
587
0
            return Status::Error<EXCEEDED_LIMIT>(
588
0
                    "reach the capacity limit of path {}, file_size={}", data_dir->path(),
589
0
                    file_size);
590
0
        }
591
592
0
        total_file_size += file_size;
593
0
        uint64_t estimate_timeout = file_size / config::download_low_speed_limit_kbps / 1024;
594
0
        if (estimate_timeout < config::download_low_speed_time) {
595
0
            estimate_timeout = config::download_low_speed_time;
596
0
        }
597
598
0
        std::string local_file_path = local_path + "/" + file_name;
599
600
0
        LOG(INFO) << "clone begin to download file from: " << mask_token(remote_file_url)
601
0
                  << " to: " << local_file_path << ". size(B): " << file_size
602
0
                  << ", timeout(s): " << estimate_timeout;
603
604
0
        auto download_cb = [&remote_file_url, estimate_timeout, &local_file_path,
605
0
                            file_size](HttpClient* client) {
606
0
            RETURN_IF_ERROR(client->init(remote_file_url));
607
0
            client->set_timeout_ms(estimate_timeout * 1000);
608
0
            RETURN_IF_ERROR(client->download(local_file_path));
609
610
0
            std::error_code ec;
611
            // Check file length
612
0
            uint64_t local_file_size = std::filesystem::file_size(local_file_path, ec);
613
0
            if (ec) {
614
0
                LOG(WARNING) << "download file error" << ec.message();
615
0
                return Status::IOError("can't retrive file_size of {}, due to {}", local_file_path,
616
0
                                       ec.message());
617
0
            }
618
0
            if (local_file_size != file_size) {
619
0
                LOG(WARNING) << "download file length error"
620
0
                             << ", remote_path=" << mask_token(remote_file_url)
621
0
                             << ", file_size=" << file_size
622
0
                             << ", local_file_size=" << local_file_size;
623
0
                return Status::InternalError("downloaded file size is not equal");
624
0
            }
625
0
            return io::global_local_filesystem()->permission(local_file_path,
626
0
                                                             io::LocalFileSystem::PERMS_OWNER_RW);
627
0
        };
628
0
        RETURN_IF_ERROR(HttpClient::execute_with_retry(DOWNLOAD_FILE_MAX_RETRY, 1, download_cb));
629
0
    } // Clone files from remote backend
630
631
0
    uint64_t total_time_ms = watch.elapsed_time() / 1000 / 1000;
632
0
    total_time_ms = total_time_ms > 0 ? total_time_ms : 0;
633
0
    double copy_rate = 0.0;
634
0
    if (total_time_ms > 0) {
635
0
        copy_rate = total_file_size / ((double)total_time_ms) / 1000;
636
0
    }
637
0
    _copy_size = (int64_t)total_file_size;
638
0
    _copy_time_ms = (int64_t)total_time_ms;
639
0
    LOG(INFO) << "succeed to copy tablet " << _signature
640
0
              << ", total files: " << file_name_list.size()
641
0
              << ", total file size: " << total_file_size << " B, cost: " << total_time_ms << " ms"
642
0
              << ", rate: " << copy_rate << " MB/s";
643
0
    return Status::OK();
644
0
}
645
646
Status EngineCloneTask::_batch_download_files(DataDir* data_dir, const std::string& address,
647
                                              const std::string& remote_dir,
648
0
                                              const std::string& local_dir) {
649
0
    constexpr size_t BATCH_FILE_SIZE = 64 << 20; // 64MB
650
0
    constexpr size_t BATCH_FILE_NUM = 64;
651
652
    // Check local path exist, if exist, remove it, then create the dir
653
    // local_file_full_path = tabletid/clone, for a specific tablet, there should be only one folder
654
    // if this folder exists, then should remove it
655
    // for example, BE clone from BE 1 to download file 1 with version (2,2), but clone from BE 1 failed
656
    // then it will try to clone from BE 2, but it will find the file 1 already exist, but file 1 with same
657
    // name may have different versions.
658
0
    RETURN_IF_ERROR(io::global_local_filesystem()->delete_directory(local_dir));
659
0
    RETURN_IF_ERROR(io::global_local_filesystem()->create_directory(local_dir));
660
661
0
    const std::string& token = _cluster_info->token;
662
0
    std::vector<std::pair<std::string, size_t>> file_info_list;
663
0
    RETURN_IF_ERROR(list_remote_files_v2(address, token, remote_dir, &file_info_list));
664
665
    // If the header file is not exist, the table couldn't loaded by olap engine.
666
    // Avoid of data is not complete, we copy the header file at last.
667
    // The header file's name is end of .hdr.
668
0
    for (int i = 0; i + 1 < file_info_list.size(); ++i) {
669
0
        if (file_info_list[i].first.ends_with(".hdr")) {
670
0
            std::swap(file_info_list[i], file_info_list[file_info_list.size() - 1]);
671
0
            break;
672
0
        }
673
0
    }
674
675
0
    MonotonicStopWatch watch;
676
0
    watch.start();
677
678
0
    size_t total_file_size = 0;
679
0
    size_t total_files = file_info_list.size();
680
0
    std::vector<std::pair<std::string, size_t>> batch_files;
681
0
    for (size_t i = 0; i < total_files;) {
682
0
        size_t batch_file_size = 0;
683
0
        for (size_t j = i; j < total_files; j++) {
684
            // Split batchs by file number and file size,
685
0
            if (BATCH_FILE_NUM <= batch_files.size() || BATCH_FILE_SIZE <= batch_file_size ||
686
                // ... or separate the last .hdr file into a single batch.
687
0
                (j + 1 == total_files && !batch_files.empty())) {
688
0
                break;
689
0
            }
690
0
            batch_files.push_back(file_info_list[j]);
691
0
            batch_file_size += file_info_list[j].second;
692
0
        }
693
694
        // check disk capacity
695
0
        if (data_dir->reach_capacity_limit(batch_file_size)) {
696
0
            return Status::Error<EXCEEDED_LIMIT>(
697
0
                    "reach the capacity limit of path {}, file_size={}", data_dir->path(),
698
0
                    batch_file_size);
699
0
        }
700
701
0
        RETURN_IF_ERROR(download_files_v2(address, token, remote_dir, local_dir, batch_files));
702
703
0
        total_file_size += batch_file_size;
704
0
        i += batch_files.size();
705
0
        batch_files.clear();
706
0
    }
707
708
0
    uint64_t total_time_ms = watch.elapsed_time() / 1000 / 1000;
709
0
    total_time_ms = total_time_ms > 0 ? total_time_ms : 0;
710
0
    double copy_rate = 0.0;
711
0
    if (total_time_ms > 0) {
712
0
        copy_rate = total_file_size / ((double)total_time_ms) / 1000;
713
0
    }
714
0
    _copy_size = (int64_t)total_file_size;
715
0
    _copy_time_ms = (int64_t)total_time_ms;
716
0
    LOG(INFO) << "succeed to copy tablet " << _signature
717
0
              << ", total files: " << file_info_list.size()
718
0
              << ", total file size: " << total_file_size << " B, cost: " << total_time_ms << " ms"
719
0
              << ", rate: " << copy_rate << " MB/s";
720
721
0
    return Status::OK();
722
0
}
723
724
/// This method will only be called if tablet already exist in this BE when doing clone.
725
/// This method will do the following things:
726
/// 1. Link all files from CLONE dir to tablet dir if file does not exist in tablet dir
727
/// 2. Call _finish_xx_clone() to revise the tablet meta.
728
Status EngineCloneTask::_finish_clone(Tablet* tablet, const std::string& clone_dir, int64_t version,
729
0
                                      bool is_incremental_clone) {
730
0
    Defer remove_clone_dir {[&]() {
731
0
        std::error_code ec;
732
0
        std::filesystem::remove_all(clone_dir, ec);
733
0
        if (ec) {
734
0
            LOG(WARNING) << "failed to remove=" << clone_dir << " msg=" << ec.message();
735
0
        }
736
0
    }};
737
738
    // check clone dir existed
739
0
    bool exists = true;
740
0
    RETURN_IF_ERROR(io::global_local_filesystem()->exists(clone_dir, &exists));
741
0
    if (!exists) {
742
0
        return Status::InternalError("clone dir not existed. clone_dir={}", clone_dir);
743
0
    }
744
745
    // Load src header.
746
    // The tablet meta info is downloaded from source BE as .hdr file.
747
    // So we load it and generate cloned_tablet_meta.
748
0
    auto cloned_tablet_meta_file = fmt::format("{}/{}.hdr", clone_dir, tablet->tablet_id());
749
0
    auto cloned_tablet_meta = std::make_shared<TabletMeta>();
750
0
    RETURN_IF_ERROR(cloned_tablet_meta->create_from_file(cloned_tablet_meta_file));
751
752
    // remove the cloned meta file
753
0
    RETURN_IF_ERROR(io::global_local_filesystem()->delete_file(cloned_tablet_meta_file));
754
755
    // remove rowset binlog metas
756
0
    const auto& tablet_dir = tablet->tablet_path();
757
0
    auto binlog_metas_file = fmt::format("{}/rowset_binlog_metas.pb", clone_dir);
758
0
    bool binlog_metas_file_exists = false;
759
0
    auto file_exists_status =
760
0
            io::global_local_filesystem()->exists(binlog_metas_file, &binlog_metas_file_exists);
761
0
    if (!file_exists_status.ok()) {
762
0
        return file_exists_status;
763
0
    }
764
0
    bool contain_binlog = false;
765
0
    RowsetBinlogMetasPB rowset_binlog_metas_pb;
766
0
    if (binlog_metas_file_exists) {
767
0
        std::error_code ec;
768
0
        auto binlog_meta_filesize = std::filesystem::file_size(binlog_metas_file, ec);
769
0
        if (ec) {
770
0
            LOG(WARNING) << "get file size error" << ec.message();
771
0
            return Status::IOError("can't retrive file_size of {}, due to {}", binlog_metas_file,
772
0
                                   ec.message());
773
0
        }
774
0
        if (binlog_meta_filesize > 0) {
775
0
            contain_binlog = true;
776
0
            RETURN_IF_ERROR(read_pb(binlog_metas_file, &rowset_binlog_metas_pb));
777
0
        }
778
0
        RETURN_IF_ERROR(io::global_local_filesystem()->delete_file(binlog_metas_file));
779
0
    }
780
0
    if (contain_binlog) {
781
0
        auto binlog_dir = fmt::format("{}/_binlog", tablet_dir);
782
0
        RETURN_IF_ERROR(io::global_local_filesystem()->create_directory(binlog_dir));
783
0
    }
784
785
    // check all files in /clone and /tablet
786
0
    std::vector<io::FileInfo> clone_files;
787
0
    RETURN_IF_ERROR(io::global_local_filesystem()->list(clone_dir, true, &clone_files, &exists));
788
0
    std::unordered_set<std::string> clone_file_names;
789
0
    for (auto& file : clone_files) {
790
0
        clone_file_names.insert(file.file_name);
791
0
    }
792
793
0
    std::vector<io::FileInfo> local_files;
794
0
    RETURN_IF_ERROR(io::global_local_filesystem()->list(tablet_dir, true, &local_files, &exists));
795
0
    std::unordered_set<std::string> local_file_names;
796
0
    for (auto& file : local_files) {
797
0
        local_file_names.insert(file.file_name);
798
0
    }
799
800
0
    Status status;
801
0
    std::vector<std::string> linked_success_files;
802
0
    Defer remove_linked_files {[&]() { // clear linked files if errors happen
803
0
        if (!status.ok()) {
804
0
            std::vector<io::Path> paths;
805
0
            for (auto& file : linked_success_files) {
806
0
                paths.emplace_back(file);
807
0
            }
808
0
            static_cast<void>(io::global_local_filesystem()->batch_delete(paths));
809
0
        }
810
0
    }};
811
    /// Traverse all downloaded clone files in CLONE dir.
812
    /// If it does not exist in local tablet dir, link the file to local tablet dir
813
    /// And save all linked files in linked_success_files.
814
0
    for (const std::string& clone_file : clone_file_names) {
815
0
        if (local_file_names.find(clone_file) != local_file_names.end()) {
816
0
            VLOG_NOTICE << "find same file when clone, skip it. "
817
0
                        << "tablet=" << tablet->tablet_id() << ", clone_file=" << clone_file;
818
0
            continue;
819
0
        }
820
821
        /// if binlog exist in clone dir and md5sum equal, then skip link file
822
0
        bool skip_link_file = false;
823
0
        std::string to;
824
0
        if (clone_file.ends_with(".binlog") || clone_file.ends_with(".binlog-index")) {
825
0
            if (!contain_binlog) {
826
0
                LOG(WARNING) << "clone binlog file, but not contain binlog metas. "
827
0
                             << "tablet=" << tablet->tablet_id() << ", clone_file=" << clone_file;
828
0
                break;
829
0
            }
830
831
0
            if (auto&& result =
832
0
                        check_dest_binlog_valid(tablet_dir, clone_dir, clone_file, &skip_link_file);
833
0
                result) {
834
0
                to = std::move(result.value());
835
0
            } else {
836
0
                status = std::move(result.error());
837
0
                return status;
838
0
            }
839
0
        } else {
840
0
            to = fmt::format("{}/{}", tablet_dir, clone_file);
841
0
        }
842
843
0
        if (!skip_link_file) {
844
0
            auto from = fmt::format("{}/{}", clone_dir, clone_file);
845
0
            status = io::global_local_filesystem()->link_file(from, to);
846
0
            if (!status.ok()) {
847
0
                return status;
848
0
            }
849
0
            linked_success_files.emplace_back(std::move(to));
850
0
        }
851
0
    }
852
0
    if (contain_binlog) {
853
0
        status = tablet->ingest_binlog_metas(&rowset_binlog_metas_pb);
854
0
        if (!status.ok()) {
855
0
            return status;
856
0
        }
857
0
    }
858
859
    // clone and compaction operation should be performed sequentially
860
0
    std::lock_guard base_compaction_lock(tablet->get_base_compaction_lock());
861
0
    std::lock_guard cumulative_compaction_lock(tablet->get_cumulative_compaction_lock());
862
0
    std::lock_guard cold_compaction_lock(tablet->get_cold_compaction_lock());
863
0
    std::lock_guard build_inverted_index_lock(tablet->get_build_inverted_index_lock());
864
0
    std::lock_guard<std::mutex> push_lock(tablet->get_push_lock());
865
0
    std::lock_guard<std::mutex> rwlock(tablet->get_rowset_update_lock());
866
0
    std::lock_guard<std::shared_mutex> wrlock(tablet->get_header_lock());
867
0
    SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD);
868
0
    if (is_incremental_clone) {
869
0
        status = _finish_incremental_clone(tablet, cloned_tablet_meta, version);
870
0
    } else {
871
0
        status = _finish_full_clone(tablet, cloned_tablet_meta);
872
0
    }
873
874
    // if full clone success, need to update cumulative layer point
875
0
    if (!is_incremental_clone && status.ok()) {
876
0
        tablet->set_cumulative_layer_point(Tablet::K_INVALID_CUMULATIVE_POINT);
877
0
    }
878
879
    // clear clone dir
880
0
    return status;
881
0
}
882
883
/// This method will do:
884
/// 1. Get missing version from local tablet again and check if they exist in cloned tablet.
885
/// 2. Revise the local tablet meta to add all incremental cloned rowset's meta.
886
Status EngineCloneTask::_finish_incremental_clone(Tablet* tablet,
887
                                                  const TabletMetaSharedPtr& cloned_tablet_meta,
888
0
                                                  int64_t version) {
889
0
    LOG(INFO) << "begin to finish incremental clone. tablet=" << tablet->tablet_id()
890
0
              << ", visible_version=" << version
891
0
              << ", cloned_tablet_replica_id=" << cloned_tablet_meta->replica_id();
892
893
    /// Get missing versions again from local tablet.
894
    /// We got it before outside the lock, so it has to be got again.
895
0
    Versions missed_versions = tablet->get_missed_versions_unlocked(version);
896
0
    VLOG_NOTICE << "get missed versions again when finish incremental clone. "
897
0
                << "tablet=" << tablet->tablet_id() << ", clone version=" << version
898
0
                << ", missed_versions_size=" << missed_versions.size();
899
900
    // check missing versions exist in clone src
901
0
    std::vector<RowsetSharedPtr> rowsets_to_clone;
902
0
    for (Version nested_version : missed_versions) {
903
0
        auto rs_meta = cloned_tablet_meta->acquire_rs_meta_by_version(nested_version);
904
0
        if (rs_meta == nullptr) {
905
0
            return Status::InternalError("missed version {} is not found in cloned tablet meta",
906
0
                                         nested_version.to_string());
907
0
        }
908
0
        RowsetSharedPtr rs;
909
0
        RETURN_IF_ERROR(tablet->create_rowset(rs_meta, &rs));
910
0
        rowsets_to_clone.push_back(std::move(rs));
911
0
    }
912
913
    /// clone_data to tablet
914
    /// For incremental clone, nothing will be deleted.
915
    /// So versions_to_delete is empty.
916
0
    return tablet->revise_tablet_meta(rowsets_to_clone, {}, true);
917
0
}
918
919
/// This method will do:
920
/// 1. Compare the version of local tablet and cloned tablet to decide which version to keep
921
/// 2. Revise the local tablet meta
922
Status EngineCloneTask::_finish_full_clone(Tablet* tablet,
923
0
                                           const TabletMetaSharedPtr& cloned_tablet_meta) {
924
0
    Version cloned_max_version = cloned_tablet_meta->max_version();
925
0
    LOG(INFO) << "begin to finish full clone. tablet=" << tablet->tablet_id()
926
0
              << ", cloned_max_version=" << cloned_max_version;
927
928
    // Compare the version of local tablet and cloned tablet.
929
    // For example:
930
    // clone version is 8
931
    //
932
    //      local tablet: [0-1] [2-5] [6-6] [7-7] [9-10]
933
    //      clone tablet: [0-1] [2-4] [5-6] [7-8]
934
    //
935
    // after compare, the version mark with "x" will be deleted
936
    //
937
    //      local tablet: [0-1]x [2-5]x [6-6]x [7-7]x [9-10]
938
    //      clone tablet: [0-1]  [2-4]  [5-6]  [7-8]
939
940
0
    std::vector<RowsetSharedPtr> to_delete;
941
0
    std::vector<RowsetSharedPtr> to_add;
942
0
    for (auto& [v, rs] : tablet->rowset_map()) {
943
        // if local version cross src latest, clone failed
944
        // if local version is : 0-0, 1-1, 2-10, 12-14, 15-15,16-16
945
        // cloned max version is 13-13, this clone is failed, because could not
946
        // fill local data by using cloned data.
947
        // It should not happen because if there is a hole, the following delta will not
948
        // do compaction.
949
0
        if (v.first <= cloned_max_version.second && v.second > cloned_max_version.second) {
950
0
            return Status::InternalError(
951
0
                    "version cross src latest. cloned_max_version={}, local_version={}",
952
0
                    cloned_max_version.second, v.to_string());
953
0
        }
954
0
        if (v.second <= cloned_max_version.second) {
955
0
            to_delete.push_back(rs);
956
0
        } else {
957
            // cooldowned rowsets MUST be continuous, so rowsets whose version > missed version MUST be local rowset
958
0
            DCHECK(rs->is_local());
959
0
        }
960
0
    }
961
962
0
    to_add.reserve(cloned_tablet_meta->all_rs_metas().size());
963
0
    for (const auto& [_, rs_meta] : cloned_tablet_meta->all_rs_metas()) {
964
0
        RowsetSharedPtr rs;
965
0
        RETURN_IF_ERROR(tablet->create_rowset(rs_meta, &rs));
966
0
        to_add.push_back(std::move(rs));
967
0
    }
968
0
    {
969
0
        std::shared_lock cooldown_conf_rlock(tablet->get_cooldown_conf_lock());
970
0
        if (tablet->cooldown_conf_unlocked().cooldown_replica_id == tablet->replica_id()) {
971
            // If this replica is cooldown replica, MUST generate a new `cooldown_meta_id` to avoid use `cooldown_meta_id`
972
            // generated in old cooldown term which may lead to such situation:
973
            // Replica A is cooldown replica, cooldown_meta_id=2,
974
            // Replica B: cooldown_replica=A, cooldown_meta_id=1
975
            // Replica A: full clone Replica A, cooldown_meta_id=1, but remote cooldown_meta is still with cooldown_meta_id=2
976
            // After tablet report. FE finds all replicas' cooldowned data is consistent
977
            // Replica A: confirm_unused_remote_files, delete some cooldowned data of cooldown_meta_id=2
978
            // Replica B: follow_cooldown_data, cooldown_meta_id=2, data lost
979
0
            tablet->tablet_meta()->set_cooldown_meta_id(UniqueId::gen_uid());
980
0
        } else {
981
0
            tablet->tablet_meta()->set_cooldown_meta_id(cloned_tablet_meta->cooldown_meta_id());
982
0
        }
983
0
    }
984
0
    if (tablet->enable_unique_key_merge_on_write()) {
985
0
        tablet->tablet_meta()->delete_bitmap().merge(cloned_tablet_meta->delete_bitmap());
986
0
    }
987
0
    return tablet->revise_tablet_meta(to_add, to_delete, false);
988
    // TODO(plat1ko): write cooldown meta to remote if this replica is cooldown replica
989
0
}
990
} // namespace doris