Coverage Report

Created: 2025-11-20 23:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/snapshot_manager.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 <condition_variable>
21
#include <memory>
22
#include <mutex>
23
#include <string>
24
#include <vector>
25
26
#include "common/status.h"
27
#include "olap/rowset/pending_rowset_helper.h"
28
#include "olap/rowset/rowset_fwd.h"
29
#include "olap/tablet_fwd.h"
30
31
namespace doris {
32
class RowsetMetaPB;
33
class TabletSchemaPB;
34
class TSnapshotRequest;
35
struct RowsetId;
36
class StorageEngine;
37
class MemTrackerLimiter;
38
39
class LocalSnapshotLockGuard;
40
41
// A simple lock to protect the local snapshot path.
42
class LocalSnapshotLock {
43
    friend class LocalSnapshotLockGuard;
44
45
public:
46
1
    LocalSnapshotLock() = default;
47
1
    ~LocalSnapshotLock() = default;
48
    LocalSnapshotLock(const LocalSnapshotLock&) = delete;
49
    LocalSnapshotLock& operator=(const LocalSnapshotLock&) = delete;
50
51
8
    static LocalSnapshotLock& instance() {
52
8
        static LocalSnapshotLock instance;
53
8
        return instance;
54
8
    }
55
56
    // Acquire the lock for the specified path. It will block if the lock is already held by another.
57
    LocalSnapshotLockGuard acquire(const std::string& path);
58
59
private:
60
    void release(const std::string& path);
61
62
    class LocalSnapshotContext {
63
    public:
64
        bool _is_locked = false;
65
        size_t _waiting_count = 0;
66
        std::condition_variable _cv;
67
68
4
        LocalSnapshotContext() = default;
69
        LocalSnapshotContext(const LocalSnapshotContext&) = delete;
70
        LocalSnapshotContext& operator=(const LocalSnapshotContext&) = delete;
71
    };
72
73
    std::mutex _lock;
74
    std::unordered_map<std::string, LocalSnapshotContext> _local_snapshot_contexts;
75
};
76
77
class LocalSnapshotLockGuard {
78
public:
79
4
    LocalSnapshotLockGuard(std::string path) : _snapshot_path(std::move(path)) {}
80
    LocalSnapshotLockGuard(const LocalSnapshotLockGuard&) = delete;
81
    LocalSnapshotLockGuard& operator=(const LocalSnapshotLockGuard&) = delete;
82
4
    ~LocalSnapshotLockGuard() { LocalSnapshotLock::instance().release(_snapshot_path); }
83
84
private:
85
    std::string _snapshot_path;
86
};
87
88
class SnapshotManager {
89
public:
90
    SnapshotManager(StorageEngine& engine);
91
    ~SnapshotManager();
92
93
    /// Create a snapshot
94
    /// snapshot_path: out param, the dir of snapshot
95
    /// allow_incremental_clone: out param, true if it is an incremental clone
96
    Status make_snapshot(const TSnapshotRequest& request, std::string* snapshot_path,
97
                         bool* allow_incremental_clone);
98
99
    std::string static get_schema_hash_full_path(const TabletSharedPtr& ref_tablet,
100
                                                 const std::string& prefix);
101
102
    // @brief 释放snapshot
103
    // @param snapshot_path [in] 要被释放的snapshot的路径,只包含到ID
104
    Status release_snapshot(const std::string& snapshot_path);
105
106
    Result<std::vector<PendingRowsetGuard>> convert_rowset_ids(const std::string& clone_dir,
107
                                                               int64_t tablet_id,
108
                                                               int64_t replica_id, int64_t table_id,
109
                                                               int64_t partition_id,
110
                                                               int32_t schema_hash);
111
112
private:
113
    Status _calc_snapshot_id_path(const TabletSharedPtr& tablet, int64_t timeout_s,
114
                                  std::string* out_path);
115
116
    std::string _get_header_full_path(const TabletSharedPtr& ref_tablet,
117
                                      const std::string& schema_hash_path) const;
118
119
    std::string _get_json_header_full_path(const TabletSharedPtr& ref_tablet,
120
                                           const std::string& schema_hash_path) const;
121
122
    Status _link_index_and_data_files(const std::string& header_path,
123
                                      const TabletSharedPtr& ref_tablet,
124
                                      const std::vector<RowsetSharedPtr>& consistent_rowsets);
125
126
    Status _create_snapshot_files(const TabletSharedPtr& ref_tablet,
127
                                  const TabletSharedPtr& target_tablet,
128
                                  const TSnapshotRequest& request, std::string* snapshot_path,
129
                                  bool* allow_incremental_clone);
130
131
    Status _prepare_snapshot_dir(const TabletSharedPtr& ref_tablet, std::string* snapshot_id_path);
132
133
    Status _rename_rowset_id(const RowsetMetaPB& rs_meta_pb, const std::string& new_tablet_path,
134
                             TabletSchemaSPtr tablet_schema, const RowsetId& next_id,
135
                             RowsetMetaPB* new_rs_meta_pb);
136
137
    Status _rename_index_ids(TabletSchemaPB& schema_pb,
138
                             const TabletSchemaSPtr& tablet_schema) const;
139
140
    StorageEngine& _engine;
141
    std::atomic<uint64_t> _snapshot_base_id {0};
142
143
    std::shared_ptr<MemTrackerLimiter> _mem_tracker;
144
}; // SnapshotManager
145
146
} // namespace doris