Coverage Report

Created: 2025-06-08 11:30

/root/doris/be/src/olap/snapshot_manager.h
Line
Count
Source (jump to first uncovered line)
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 <condition_variable>
23
#include <memory>
24
#include <mutex>
25
#include <string>
26
#include <vector>
27
28
#include "common/status.h"
29
#include "olap/rowset/pending_rowset_helper.h"
30
#include "olap/rowset/rowset.h"
31
#include "olap/tablet.h"
32
#include "olap/tablet_schema.h"
33
#include "runtime/memory/mem_tracker_limiter.h"
34
35
namespace doris {
36
class RowsetMetaPB;
37
class TSnapshotRequest;
38
struct RowsetId;
39
40
class LocalSnapshotLockGuard;
41
42
// A simple lock to protect the local snapshot path.
43
class LocalSnapshotLock {
44
    friend class LocalSnapshotLockGuard;
45
46
public:
47
0
    LocalSnapshotLock() = default;
48
0
    ~LocalSnapshotLock() = default;
49
    LocalSnapshotLock(const LocalSnapshotLock&) = delete;
50
    LocalSnapshotLock& operator=(const LocalSnapshotLock&) = delete;
51
52
0
    static LocalSnapshotLock& instance() {
53
0
        static LocalSnapshotLock instance;
54
0
        return instance;
55
0
    }
56
57
    // Acquire the lock for the specified path. It will block if the lock is already held by another.
58
    LocalSnapshotLockGuard acquire(const std::string& path);
59
60
private:
61
    void release(const std::string& path);
62
63
    class LocalSnapshotContext {
64
    public:
65
        bool _is_locked = false;
66
        size_t _waiting_count = 0;
67
        std::condition_variable _cv;
68
69
0
        LocalSnapshotContext() = default;
70
        LocalSnapshotContext(const LocalSnapshotContext&) = delete;
71
        LocalSnapshotContext& operator=(const LocalSnapshotContext&) = delete;
72
    };
73
74
    std::mutex _lock;
75
    std::unordered_map<std::string, LocalSnapshotContext> _local_snapshot_contexts;
76
};
77
78
class LocalSnapshotLockGuard {
79
public:
80
0
    LocalSnapshotLockGuard(std::string path) : _snapshot_path(std::move(path)) {}
81
    LocalSnapshotLockGuard(const LocalSnapshotLockGuard&) = delete;
82
    LocalSnapshotLockGuard& operator=(const LocalSnapshotLockGuard&) = delete;
83
0
    ~LocalSnapshotLockGuard() { LocalSnapshotLock::instance().release(_snapshot_path); }
84
85
private:
86
    std::string _snapshot_path;
87
};
88
89
class SnapshotManager {
90
public:
91
0
    ~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
    static SnapshotManager* instance();
107
108
    Result<std::vector<PendingRowsetGuard>> convert_rowset_ids(const std::string& clone_dir,
109
                                                               int64_t tablet_id,
110
                                                               int64_t replica_id,
111
                                                               int64_t partition_id,
112
                                                               int32_t schema_hash);
113
114
private:
115
1
    SnapshotManager() : _snapshot_base_id(0) {
116
1
        _mem_tracker =
117
1
                MemTrackerLimiter::create_shared(MemTrackerLimiter::Type::OTHER, "SnapshotManager");
118
1
    }
119
120
    Status _calc_snapshot_id_path(const TabletSharedPtr& tablet, int64_t timeout_s,
121
                                  std::string* out_path);
122
123
    std::string _get_header_full_path(const TabletSharedPtr& ref_tablet,
124
                                      const std::string& schema_hash_path) const;
125
126
    std::string _get_json_header_full_path(const TabletSharedPtr& ref_tablet,
127
                                           const std::string& schema_hash_path) const;
128
129
    Status _link_index_and_data_files(const std::string& header_path,
130
                                      const TabletSharedPtr& ref_tablet,
131
                                      const std::vector<RowsetSharedPtr>& consistent_rowsets);
132
133
    Status _create_snapshot_files(const TabletSharedPtr& ref_tablet,
134
                                  const TabletSharedPtr& target_tablet,
135
                                  const TSnapshotRequest& request, std::string* snapshot_path,
136
                                  bool* allow_incremental_clone);
137
138
    Status _prepare_snapshot_dir(const TabletSharedPtr& ref_tablet, std::string* snapshot_id_path);
139
140
    Status _rename_rowset_id(const RowsetMetaPB& rs_meta_pb, const std::string& new_tablet_path,
141
                             TabletSchemaSPtr tablet_schema, const RowsetId& next_id,
142
                             RowsetMetaPB* new_rs_meta_pb);
143
144
private:
145
    static SnapshotManager* _s_instance;
146
    static std::mutex _mlock;
147
148
    // snapshot
149
    std::mutex _snapshot_mutex;
150
    uint64_t _snapshot_base_id;
151
152
    std::shared_ptr<MemTrackerLimiter> _mem_tracker;
153
}; // SnapshotManager
154
155
} // namespace doris