-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathchunk_trash_manager_impl.cc
More file actions
400 lines (328 loc) · 14.4 KB
/
chunk_trash_manager_impl.cc
File metadata and controls
400 lines (328 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
Copyright 2023-2024 Leil Storage OÜ
This file is part of SaunaFS.
SaunaFS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
SaunaFS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SaunaFS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/platform.h"
#include <sys/statvfs.h>
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <ctime>
#include <utility>
#include "chunkserver-common/chunk_trash_manager_impl.h"
#include "config/cfg.h"
#include "errors/saunafs_error_codes.h"
#include "global_shared_resources.h"
#include "hdd_stats.h"
#include "hdd_utils.h"
#include "slogger/slogger.h"
namespace fs = std::filesystem;
size_t ChunkTrashManagerImpl::availableThresholdGB = kDefaultAvailableThresholdGB;
size_t ChunkTrashManagerImpl::trashTimeLimitSeconds = kDefaultTrashTimeLimitSeconds;
size_t ChunkTrashManagerImpl::trashGarbageCollectorBulkSize = kDefaultTrashGarbageCollectorBulkSize;
size_t ChunkTrashManagerImpl::garbageCollectorSpaceRecoveryStep =
kDefaultGarbageCollectorSpaceRecoveryStep;
uint64_t ChunkTrashManagerImpl::maxBytesReadPerDisk = 1024 * 1024; //1MiB
uint64_t ChunkTrashManagerImpl::maxBytesWritePerDisk = 1024 * 1024; //1MiB
uint64_t ChunkTrashManagerImpl::previousBytesReadPerDisk = 1024 * 1024; //1MiB
uint64_t ChunkTrashManagerImpl::previousBytesWritePerDisk = 1024 * 1024; //1MiB
const std::string ChunkTrashManagerImpl::kTrashGuardString =
std::string("/") + ChunkTrashManager::kTrashDirname + "/";
std::vector<std::thread> ChunkTrashManagerImpl::removeFromTrashThreads{};
std::unique_ptr<ProducerConsumerQueue> ChunkTrashManagerImpl::removeFromTrashJobQueue =
std::make_unique<ProducerConsumerQueue>();
std::atomic<uint32_t> ChunkTrashManagerImpl::NotIdleThreadCount{0};
void ChunkTrashManagerImpl::reloadConfig() {
availableThresholdGB =
cfg_get("CHUNK_TRASH_FREE_SPACE_THRESHOLD_GB", kDefaultAvailableThresholdGB);
trashTimeLimitSeconds =
cfg_get("CHUNK_TRASH_EXPIRATION_SECONDS", kDefaultTrashTimeLimitSeconds);
trashGarbageCollectorBulkSize =
cfg_get("CHUNK_TRASH_GC_BATCH_SIZE", kDefaultTrashGarbageCollectorBulkSize);
garbageCollectorSpaceRecoveryStep = cfg_get("CHUNK_TRASH_GC_SPACE_RECOVERY_BATCH_SIZE",
kDefaultGarbageCollectorSpaceRecoveryStep);
safs::log_info(
"Reloaded chunk trash manager configuration: "
"CHUNK_TRASH_FREE_SPACE_THRESHOLD_GB={}, "
"CHUNK_TRASH_EXPIRATION_SECONDS={}, "
"CHUNK_TRASH_GC_BATCH_SIZE={}, "
"CHUNK_TRASH_GC_SPACE_RECOVERY_BATCH_SIZE={}",
availableThresholdGB, trashTimeLimitSeconds, trashGarbageCollectorBulkSize,
garbageCollectorSpaceRecoveryStep);
}
std::string ChunkTrashManagerImpl::getTimeString(std::time_t time1) {
std::tm utcTime;
#ifdef _WIN32
if (gmtime_s(&utcTime, &time1) != 0) {
safs::log_error_code(SAUNAFS_ERROR_EINVAL, "Failed to convert time to UTC: {}",
std::strerror(errno));
return "";
}
#else
if (gmtime_r(&time1, &utcTime) == nullptr) {
safs::log_error_code(SAUNAFS_ERROR_EINVAL, "Failed to convert time to UTC: {}",
std::strerror(errno));
return "";
}
#endif
std::ostringstream oss;
oss << std::put_time(&utcTime, kTimeStampFormat.c_str());
return oss.str();
}
std::time_t ChunkTrashManagerImpl::getTimeFromString(const std::string &timeString,
int &errorCode) {
errorCode = SAUNAFS_STATUS_OK;
std::tm time = {};
std::istringstream stringReader(timeString);
stringReader >> std::get_time(&time, kTimeStampFormat.c_str());
if (stringReader.fail()) {
errorCode = SAUNAFS_ERROR_EINVAL;
safs::log_error_code(static_cast<error_type>(errorCode), "Failed to parse time string: {}",
timeString.c_str());
}
return std::mktime(&time);
}
ChunkTrashManagerImpl::error_type ChunkTrashManagerImpl::getMoveDestinationPath(
const std::string &filePath, const std::string &sourceRoot, const std::string &destinationRoot,
std::string &destinationPath) {
auto error_code = SAUNAFS_STATUS_OK;
if (filePath.find(sourceRoot) != 0) {
error_code = SAUNAFS_ERROR_EINVAL;
safs::log_error_code(error_code, "File path is outside the source root: {}",
filePath.c_str());
return error_code;
}
destinationPath = destinationRoot + "/" + filePath.substr(sourceRoot.size());
return error_code;
}
int ChunkTrashManagerImpl::moveToTrash(const fs::path &filePath, const fs::path &diskPath,
const std::time_t &deletionTime) {
std::error_code errorCode_;
if (!fs::exists(filePath, errorCode_)) {
safs::log_error_code(errorCode_, "File does not exist: {}", filePath.string().c_str());
return SAUNAFS_ERROR_ENOENT;
}
const fs::path trashDir = getTrashDir(diskPath);
fs::create_directories(trashDir, errorCode_);
if (errorCode_) {
safs::log_error_code(errorCode_, "Failed to create trash directory: {}",
trashDir.string().c_str());
return SAUNAFS_ERROR_NOTDONE;
}
const std::string deletionTimestamp = getTimeString(deletionTime);
std::string trashFilename;
auto errorCode = getMoveDestinationPath(filePath.string(), diskPath.string(), trashDir.string(),
trashFilename);
if (errorCode != SAUNAFS_STATUS_OK) {
safs::log_error_code(errorCode, "Failed to get destination path for file: {}",
filePath.string().c_str());
return errorCode;
}
trashFilename += "." + deletionTimestamp;
fs::create_directories(fs::path(trashFilename).parent_path(), errorCode_);
if (errorCode_) {
safs::log_error_code(errorCode_, "Failed to create trash directory: {}",
trashFilename.c_str());
return SAUNAFS_ERROR_NOTDONE;
}
fs::rename(filePath, trashFilename, errorCode_);
if (errorCode_) {
safs::log_error_code(errorCode_, "Failed to move file to trash: {}",
filePath.string().c_str());
return SAUNAFS_ERROR_NOTDONE;
}
getTrashIndex().add(deletionTime, trashFilename, diskPath.string());
return SAUNAFS_STATUS_OK;
}
void ChunkTrashManagerImpl::removeTrashFiles(
const ChunkTrashIndex::TrashIndexDiskEntries &filesToRemove) const {
for (const auto &[diskPath, fileEntries] : filesToRemove) {
removeFromTrashJobQueue->put(
0, 1,
reinterpret_cast<uint8_t *>(
new std::pair<ChunkTrashIndex::TrashIndexFileEntries, std::string>(fileEntries,
diskPath)),
1);
}
while(NotIdleThreadCount && !removeFromTrashJobQueue->isEmpty()){
}
}
void ChunkTrashManagerImpl::removeTrashFilesFromDiskThread(uint8_t workerId) {
std::string threadName ="removeTrashFilesFromDisk_worker_" + std::to_string(workerId);
pthread_setname_np(pthread_self(), threadName.c_str());
uint32_t jobId;
uint32_t operation;
uint8_t *jobPtrArg;
while (true) {
removeFromTrashJobQueue->get(&jobId, &operation, &jobPtrArg, nullptr);
if(operation == 0){
break;
}
NotIdleThreadCount ++;
auto tempTuple = reinterpret_cast<std::pair<ChunkTrashIndex::TrashIndexFileEntries, std::string> *>(jobPtrArg);
ChunkTrashIndex::TrashIndexFileEntries filesToRemove = tempTuple->first;
std::string diskPath = tempTuple->second;
for (const auto &fileEntry : filesToRemove) {
if (removeFileFromTrash(fileEntry.second) != SAUNAFS_STATUS_OK) { continue; }
HddStats::gStatsOperationsGCPurge++;
getTrashIndex().remove(fileEntry.first, fileEntry.second, diskPath);
}
delete tempTuple;
NotIdleThreadCount --;
}
}
fs::path ChunkTrashManagerImpl::getTrashDir(const fs::path &diskPath) {
return diskPath / ChunkTrashManager::kTrashDirname;
}
int ChunkTrashManagerImpl::init(const std::string &diskPath) {
reloadConfig();
const fs::path trashDir = getTrashDir(diskPath);
if (!fs::exists(trashDir)) {
std::error_code errorCode_;
fs::create_directories(trashDir, errorCode_);
if (errorCode_) {
safs::log_error_code(errorCode_, "Failed to create trash directory: {}",
trashDir.string().c_str());
return SAUNAFS_ERROR_NOTDONE;
}
}
getTrashIndex().reset(diskPath);
for (const auto &file : fs::recursive_directory_iterator(trashDir)) {
if (fs::is_regular_file(file) && isTrashPath(file.path().string())) {
const std::string filename = file.path().filename().string();
const std::string deletionTimeStr = filename.substr(filename.find_last_of('.') + 1);
if (!isValidTimestampFormat(deletionTimeStr)) {
safs::log_error_code(SAUNAFS_ERROR_EINVAL,
"Invalid timestamp format in file: {}, skipping.",
file.path().string().c_str());
continue;
}
int errorCode;
const std::time_t deletionTime = getTimeFromString(deletionTimeStr, errorCode);
if (errorCode != SAUNAFS_STATUS_OK) {
safs::log_error_code(static_cast<error_type>(errorCode),
"Failed to parse deletion time from file: {}, skipping.",
file.path().string().c_str());
continue;
}
getTrashIndex().add(deletionTime, file.path().string(), diskPath);
}
}
if (ChunkTrashManagerImpl::removeFromTrashThreads.size() < 5) {
ChunkTrashManagerImpl::removeFromTrashThreads.emplace_back(
&ChunkTrashManagerImpl::removeTrashFilesFromDiskThread,
uint8_t(ChunkTrashManagerImpl::removeFromTrashThreads.size()));
}
return SAUNAFS_STATUS_OK;
}
void ChunkTrashManagerImpl::terminate() {
for(uint8_t i=0; i < ChunkTrashManagerImpl::removeFromTrashThreads.size(); i++){
ChunkTrashManagerImpl::removeFromTrashJobQueue->put(0, 0, nullptr, 1);
}
for (auto &thread : ChunkTrashManagerImpl::removeFromTrashThreads) {
if (thread.joinable()) { thread.join(); }
}
ChunkTrashManagerImpl::removeFromTrashThreads.clear();
}
bool ChunkTrashManagerImpl::isValidTimestampFormat(const std::string ×tamp) {
return timestamp.size() == kTimeStampLength && std::ranges::all_of(timestamp, ::isdigit);
}
void ChunkTrashManagerImpl::removeExpiredFiles(const time_t &timeLimit, size_t bulkSize) const {
const auto expiredFilesCollection = getTrashIndex().getExpiredFiles(timeLimit, bulkSize);
removeTrashFiles(expiredFilesCollection);
}
size_t ChunkTrashManagerImpl::checkAvailableSpace(const std::string &diskPath) {
struct statvfs stat {};
if (statvfs(diskPath.c_str(), &stat) != 0) {
safs::log_error_code(errno, "Failed to get file system statistics");
return 0;
}
constexpr size_t kGiBMultiplier = 1 << 30;
size_t const availableGb = stat.f_bavail * stat.f_frsize / kGiBMultiplier;
return availableGb;
}
void ChunkTrashManagerImpl::makeSpace(const std::string &diskPath,
const size_t spaceAvailabilityThreshold,
const size_t recoveryStep) const {
size_t availableSpace = checkAvailableSpace(diskPath);
while (availableSpace < spaceAvailabilityThreshold) {
const auto olderFilesCollection = getTrashIndex().getOlderFiles(diskPath, recoveryStep);
if (olderFilesCollection.empty()) { break; }
removeTrashFiles({{diskPath, olderFilesCollection}});
availableSpace = checkAvailableSpace(diskPath);
}
}
void ChunkTrashManagerImpl::makeSpace(const size_t spaceAvailabilityThreshold,
const size_t recoveryStep) const {
for (const auto &diskPath : getTrashIndex().getDiskPaths()) {
makeSpace(diskPath, spaceAvailabilityThreshold, recoveryStep);
}
}
void ChunkTrashManagerImpl::collectGarbage() {
if (!ChunkTrashManager::isEnabled) { return; }
std::time_t const currentTime = std::time(nullptr);
std::time_t const expirationTime = currentTime - trashTimeLimitSeconds;
uint64_t currentBytesWrite = HddStats::gBytesWrittenSinceLastGCSweep.exchange(0);
uint64_t currentBytesRead = HddStats::gBytesReadSinceLastGCSweep.exchange(0);
uint64_t currentDiskCount = 1;
{
std::lock_guard disksLockGuard(gDisksMutex);
currentDiskCount = std::max(currentDiskCount, gDisks.size());
}
currentBytesRead /= currentDiskCount;
currentBytesWrite /= currentDiskCount;
// 0.99997 ^ (30 cycles/min * 60 minutes an hour * 72 hours a day) = 0.02 (2%)
maxBytesReadPerDisk =
std::max({uint64_t(maxBytesReadPerDisk * 0.99997), currentBytesRead, uint64_t(1'000'000)});
maxBytesWritePerDisk = std::max(
{uint64_t(maxBytesWritePerDisk * 0.99997), currentBytesWrite, uint64_t(1'000'000)});
double totalIOPercentage =
static_cast<double>(currentBytesRead) * 100.0 / maxBytesReadPerDisk +
static_cast<double>(currentBytesWrite) * 100.0 / maxBytesWritePerDisk;
auto invertedSigmoid = [](double val) -> double {
const double steepness = 10.0; // steepness
const double center = 0.15; // center in [0,1]
const double valnorm = val / 100.0; // normalize so that 100% total I/O maps to 1.0
const double res = 1.0 / (1.0 + std::exp(-steepness * (valnorm - center)));
return 1.0 - res;
};
uint64_t bulksizeScaled = trashGarbageCollectorBulkSize * invertedSigmoid(totalIOPercentage);
if ((currentBytesRead + 1) / (previousBytesReadPerDisk + 1) +
(currentBytesWrite + 1) / (previousBytesWritePerDisk + 1) >=
10) {
bulksizeScaled = 0;
}
static constexpr uint64_t kMinGCBulkSizeForActivation = 5;
if (bulksizeScaled >= kMinGCBulkSizeForActivation) {
removeExpiredFiles(expirationTime, bulksizeScaled);
}
makeSpace(availableThresholdGB, garbageCollectorSpaceRecoveryStep);
previousBytesReadPerDisk = currentBytesRead;
previousBytesWritePerDisk = currentBytesWrite;
}
bool ChunkTrashManagerImpl::isTrashPath(const std::string &filePath) {
return filePath.find("/" + ChunkTrashManager::kTrashDirname + "/") != std::string::npos;
}
ChunkTrashManagerImpl::error_type ChunkTrashManagerImpl::removeFileFromTrash(
const std::string &filePath) {
if (!isTrashPath(filePath)) {
safs::log_error_code(SAUNAFS_ERROR_EINVAL, "Invalid trash path: {}", filePath.c_str());
return SAUNAFS_ERROR_EINVAL;
}
std::error_code errorCode;
fs::remove(filePath, errorCode); // Remove the file or directory
if (errorCode) {
safs::log_error_code(errorCode, "Failed to remove file or directory: {}", filePath.c_str());
return SAUNAFS_ERROR_NOTDONE;
}
return SAUNAFS_STATUS_OK;
}