|
| 1 | +/* |
| 2 | + * Copyright (c) 2024-present, Qihoo, Inc. All rights reserved. |
| 3 | + * This source code is licensed under the BSD-style license found in the |
| 4 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 5 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "praft/psnapshot.h" |
| 9 | + |
| 10 | +#include <dirent.h> |
| 11 | +#include <sys/stat.h> |
| 12 | +#include <glog/logging.h> |
| 13 | + |
| 14 | +#include "braft/local_file_meta.pb.h" |
| 15 | +#include "butil/files/file_path.h" |
| 16 | +#include "include/pika_conf.h" |
| 17 | +#include "include/pika_server.h" |
| 18 | +#include "storage/storage.h" |
| 19 | +#include "storage/backupable.h" |
| 20 | + |
| 21 | +extern std::unique_ptr<PikaConf> g_pika_conf; |
| 22 | +extern std::unique_ptr<PikaServer> g_pika_server; |
| 23 | + |
| 24 | +static bool IsDirectory(const std::string& path) { |
| 25 | + struct stat st; |
| 26 | + if (stat(path.c_str(), &st) != 0) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + return S_ISDIR(st.st_mode); |
| 30 | +} |
| 31 | + |
| 32 | +static bool IsRegularFile(const std::string& path) { |
| 33 | + struct stat st; |
| 34 | + if (stat(path.c_str(), &st) != 0) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + return S_ISREG(st.st_mode); |
| 38 | +} |
| 39 | + |
| 40 | +static std::string GetRelativePath(const std::string& full_path, const std::string& base_path) { |
| 41 | + if (full_path.find(base_path) == 0) { |
| 42 | + std::string relative = full_path.substr(base_path.length()); |
| 43 | + if (!relative.empty() && relative[0] == '/') { |
| 44 | + relative = relative.substr(1); |
| 45 | + } |
| 46 | + return relative; |
| 47 | + } |
| 48 | + return full_path; |
| 49 | +} |
| 50 | + |
| 51 | +braft::FileAdaptor* PPosixFileSystemAdaptor::open(const std::string& path, int oflag, |
| 52 | + const ::google::protobuf::Message* file_meta, |
| 53 | + butil::File::Error* e) { |
| 54 | + if ((oflag & IS_RDONLY) == 0) { // This is a read operation |
| 55 | + bool snapshots_exists = false; |
| 56 | + std::string snapshot_path; |
| 57 | + |
| 58 | + // parse snapshot path |
| 59 | + butil::FilePath parse_snapshot_path(path); |
| 60 | + std::vector<std::string> components; |
| 61 | + parse_snapshot_path.GetComponents(&components); |
| 62 | + for (const auto& component : components) { |
| 63 | + snapshot_path += component + "/"; |
| 64 | + if (component.find("snapshot_") != std::string::npos) { |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // check whether snapshots have been created |
| 70 | + std::lock_guard<braft::raft_mutex_t> guard(mutex_); |
| 71 | + if (!snapshot_path.empty()) { |
| 72 | + DIR* dir = opendir(snapshot_path.c_str()); |
| 73 | + if (dir) { |
| 74 | + struct dirent* entry; |
| 75 | + while ((entry = readdir(dir)) != nullptr) { |
| 76 | + std::string filename = entry->d_name; |
| 77 | + if (filename != "." && filename != ".." && filename.find(PRAFT_SNAPSHOT_META_FILE) == std::string::npos) { |
| 78 | + std::string full_path = snapshot_path + "/" + filename; |
| 79 | + if (IsRegularFile(full_path) || IsDirectory(full_path)) { |
| 80 | + // If the path directory contains files other than raft_snapshot_meta, snapshots have been generated |
| 81 | + snapshots_exists = true; |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + closedir(dir); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Snapshot generation |
| 91 | + if (!snapshots_exists && !snapshot_path.empty()) { |
| 92 | + braft::LocalSnapshotMetaTable snapshot_meta_memtable; |
| 93 | + std::string meta_path = snapshot_path + "/" PRAFT_SNAPSHOT_META_FILE; |
| 94 | + LOG(INFO) << "start to generate snapshot in path " << snapshot_path; |
| 95 | + braft::FileSystemAdaptor* fs = braft::default_file_system(); |
| 96 | + assert(fs); |
| 97 | + snapshot_meta_memtable.load_from_file(fs, meta_path); |
| 98 | + |
| 99 | + // Get storage and create checkpoint |
| 100 | + if (g_pika_server) { |
| 101 | + auto db = g_pika_server->GetDB("db0"); |
| 102 | + if (db) { |
| 103 | + auto storage = db->storage(); |
| 104 | + if (storage) { |
| 105 | + // Create checkpoint using storage's backup functionality |
| 106 | + std::shared_ptr<storage::BackupEngine> backup_engine_ptr; |
| 107 | + auto status = storage::BackupEngine::Open(storage.get(), backup_engine_ptr); |
| 108 | + if (status.ok() && backup_engine_ptr) { |
| 109 | + status = backup_engine_ptr->SetBackupContent(); |
| 110 | + if (status.ok()) { |
| 111 | + status = backup_engine_ptr->CreateNewBackup(snapshot_path); |
| 112 | + if (status.ok()) { |
| 113 | + LOG(INFO) << "Created checkpoint at " << snapshot_path; |
| 114 | + } else { |
| 115 | + LOG(ERROR) << "Failed to create checkpoint: " << status.ToString(); |
| 116 | + } |
| 117 | + } else { |
| 118 | + LOG(ERROR) << "Failed to set backup content: " << status.ToString(); |
| 119 | + } |
| 120 | + } else { |
| 121 | + LOG(ERROR) << "Failed to open backup engine: " << status.ToString(); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + AddAllFiles(snapshot_path, &snapshot_meta_memtable, snapshot_path); |
| 128 | + |
| 129 | + auto rc = snapshot_meta_memtable.save_to_file(fs, meta_path); |
| 130 | + if (rc == 0) { |
| 131 | + LOG(INFO) << "Succeed to save snapshot in path " << snapshot_path; |
| 132 | + } else { |
| 133 | + LOG(ERROR) << "Fail to save snapshot in path " << snapshot_path; |
| 134 | + } |
| 135 | + LOG(INFO) << "generate snapshot completed in path " << snapshot_path; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return braft::PosixFileSystemAdaptor::open(path, oflag, file_meta, e); |
| 140 | +} |
| 141 | + |
| 142 | +void PPosixFileSystemAdaptor::AddAllFiles(const std::string& dir, |
| 143 | + braft::LocalSnapshotMetaTable* snapshot_meta_memtable, |
| 144 | + const std::string& base_path) { |
| 145 | + assert(snapshot_meta_memtable); |
| 146 | + DIR* dirp = opendir(dir.c_str()); |
| 147 | + if (!dirp) { |
| 148 | + LOG(WARNING) << "Failed to open directory: " << dir; |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + struct dirent* entry; |
| 153 | + while ((entry = readdir(dirp)) != nullptr) { |
| 154 | + std::string filename = entry->d_name; |
| 155 | + if (filename == "." || filename == "..") { |
| 156 | + continue; |
| 157 | + } |
| 158 | + |
| 159 | + std::string full_path = dir + "/" + filename; |
| 160 | + if (IsDirectory(full_path)) { |
| 161 | + LOG(INFO) << "dir_path = " << full_path; |
| 162 | + AddAllFiles(full_path, snapshot_meta_memtable, base_path); |
| 163 | + } else if (IsRegularFile(full_path)) { |
| 164 | + std::string relative_path = GetRelativePath(full_path, base_path); |
| 165 | + LOG(INFO) << "file_path = " << relative_path; |
| 166 | + braft::LocalFileMeta meta; |
| 167 | + if (snapshot_meta_memtable->add_file(relative_path, meta) != 0) { |
| 168 | + LOG(WARNING) << "Failed to add file: " << relative_path; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + closedir(dirp); |
| 173 | +} |
0 commit comments