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
|
// Copyright 2024 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/buildtool/storage/repository_garbage_collector.hpp"
#include <cstddef>
#include <string>
#include "fmt/core.h"
#include "src/buildtool/execution_api/common/ids.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
#include "src/buildtool/logging/log_level.hpp"
#include "src/buildtool/logging/logger.hpp"
auto RepositoryGarbageCollector::SharedLock(
StorageConfig const& storage_config) noexcept -> std::optional<LockFile> {
return LockFile::Acquire(LockFilePath(storage_config), /*is_shared=*/true);
}
auto RepositoryGarbageCollector::ExclusiveLock(
StorageConfig const& storage_config) noexcept -> std::optional<LockFile> {
return LockFile::Acquire(LockFilePath(storage_config), /*is_shared=*/false);
}
auto RepositoryGarbageCollector::LockFilePath(
StorageConfig const& storage_config) noexcept -> std::filesystem::path {
return storage_config.RepositoryRoot() / "gc.lock";
}
auto RepositoryGarbageCollector::TriggerGarbageCollection(
StorageConfig const& storage_config,
bool drop_only) noexcept -> bool {
auto const remove_me_prefix = std::string{"remove-me"};
auto pid = CreateProcessUniqueId();
if (not pid) {
return false;
}
auto remove_me =
storage_config.RepositoryRoot() / (remove_me_prefix + *pid);
// With a shared lock, we can remove that directory, if it exists,
// as we own the process id.
{
auto lock = SharedLock(storage_config);
if (not lock) {
Logger::Log(LogLevel::Error,
"Failed to get a shared lock the for repository root");
return false;
}
if (FileSystemManager::IsDirectory(remove_me)) {
if (not FileSystemManager::RemoveDirectory(remove_me)) {
Logger::Log(LogLevel::Error,
"Failed to remove directory {}",
remove_me.string());
return false;
}
}
else {
if (not FileSystemManager::RemoveFile(remove_me)) {
Logger::Log(
LogLevel::Error, "Failed to remove {}", remove_me.string());
return false;
}
}
}
// after releasing the shared lock, wait to get an exclusive lock for doing
// the critical renaming
{
auto lock = ExclusiveLock(storage_config);
if (not lock) {
Logger::Log(LogLevel::Error,
"Failed to exclusively lock the local repository root");
return false;
}
if (drop_only) {
if (not FileSystemManager::CreateDirectory(remove_me)) {
Logger::Log(LogLevel::Error,
"Failed to create directory {}",
remove_me.string());
return false;
}
for (std::size_t i = storage_config.num_generations - 1; i > 0;
i--) {
auto from = storage_config.RepositoryGenerationRoot(i);
auto to = remove_me / (fmt::format("generation-{}", i));
if (FileSystemManager::IsDirectory(from)) {
if (not FileSystemManager::Rename(from, to)) {
Logger::Log(LogLevel::Error,
"Failed to rename {} to {}",
from.string(),
to.string());
return false;
}
}
}
}
else {
for (std::size_t i = storage_config.num_generations; i > 0; i--) {
auto from = storage_config.RepositoryGenerationRoot(i - 1);
auto to = i < storage_config.num_generations
? storage_config.RepositoryGenerationRoot(i)
: remove_me;
if (FileSystemManager::IsDirectory(from)) {
if (not FileSystemManager::Rename(from, to)) {
Logger::Log(LogLevel::Error,
"Failed to rename {} to {}",
from.string(),
to.string());
return false;
}
}
}
}
}
// Finally, with a shared lock, clean up the directory to be removed
{
auto lock = SharedLock(storage_config);
if (not lock) {
Logger::Log(LogLevel::Error,
"Failed to get a shared lock for the repository root");
return false;
}
if (FileSystemManager::IsDirectory(remove_me)) {
if (not FileSystemManager::RemoveDirectory(remove_me)) {
Logger::Log(LogLevel::Error,
"Failed to remove directory {}",
remove_me.string());
return false;
}
}
}
return true;
}
|