forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathLocalObjectStorage.cpp
More file actions
213 lines (176 loc) · 6.98 KB
/
LocalObjectStorage.cpp
File metadata and controls
213 lines (176 loc) · 6.98 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
#include <Disks/ObjectStorages/Local/LocalObjectStorage.h>
#include <filesystem>
#include <Disks/IO/AsynchronousBoundedReadBuffer.h>
#include <Disks/IO/ReadBufferFromRemoteFSGather.h>
#include <Disks/IO/createReadBufferFromFileBase.h>
#include <IO/WriteBufferFromFile.h>
#include <IO/copyData.h>
#include <Interpreters/Context.h>
#include <Common/filesystemHelpers.h>
#include <Common/getRandomASCIIString.h>
#include <Common/logger_useful.h>
namespace fs = std::filesystem;
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int CANNOT_UNLINK;
extern const int CANNOT_RMDIR;
extern const int READONLY;
}
LocalObjectStorage::LocalObjectStorage(LocalObjectStorageSettings settings_)
: settings(std::move(settings_))
, log(getLogger("LocalObjectStorage"))
{
if (auto block_device_id = tryGetBlockDeviceId("/"); block_device_id.has_value())
description = *block_device_id;
else
description = "/";
if (!settings.read_only)
fs::create_directories(settings.key_prefix);
}
bool LocalObjectStorage::exists(const StoredObject & object) const
{
return fs::exists(object.remote_path);
}
ReadSettings LocalObjectStorage::patchSettings(const ReadSettings & read_settings) const
{
auto modified_settings{read_settings};
/// Other options might break assertions in AsynchronousBoundedReadBuffer.
modified_settings.local_fs_method = LocalFSReadMethod::pread;
modified_settings.direct_io_threshold = 0; /// Disable.
return IObjectStorage::patchSettings(modified_settings);
}
std::unique_ptr<ReadBufferFromFileBase> LocalObjectStorage::readObject( /// NOLINT
const StoredObject & object,
const ReadSettings & read_settings,
std::optional<size_t> read_hint,
std::optional<size_t> file_size) const
{
if (!file_size)
file_size = tryGetSizeFromFilePath(object.remote_path);
LOG_TEST(log, "Read object: {}", object.remote_path);
return createReadBufferFromFileBase(object.remote_path, patchSettings(read_settings), read_hint, file_size);
}
std::unique_ptr<WriteBufferFromFileBase> LocalObjectStorage::writeObject( /// NOLINT
const StoredObject & object,
WriteMode mode,
std::optional<ObjectAttributes> /* attributes */,
size_t buf_size,
const WriteSettings & /* write_settings */)
{
throwIfReadonly();
if (mode != WriteMode::Rewrite)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "LocalObjectStorage doesn't support append to files");
LOG_TEST(log, "Write object: {}", object.remote_path);
/// Unlike real blob storage, in local fs we cannot create a file with non-existing prefix.
/// So let's create it.
fs::create_directories(fs::path(object.remote_path).parent_path());
return std::make_unique<WriteBufferFromFile>(object.remote_path, buf_size);
}
void LocalObjectStorage::removeObject(const StoredObject & object) const
{
throwIfReadonly();
/// For local object storage files are actually removed when "metadata" is removed.
if (!exists(object))
return;
if (0 != unlink(object.remote_path.data()))
ErrnoException::throwFromPath(ErrorCodes::CANNOT_UNLINK, object.remote_path, "Cannot unlink file {}", object.remote_path);
/// Remove empty directories.
fs::path dir = fs::path(object.remote_path).parent_path();
fs::path root = fs::weakly_canonical(settings.key_prefix);
while (dir.has_parent_path() && dir.has_relative_path() && dir != root && pathStartsWith(dir, root))
{
LOG_TEST(log, "Removing empty directory {}, has_parent_path: {}, has_relative_path: {}, root: {}, starts with root: {}",
std::string(dir), dir.has_parent_path(), dir.has_relative_path(), std::string(root), pathStartsWith(dir, root));
std::string dir_str = dir;
if (0 != rmdir(dir_str.data()))
{
if (errno == ENOTDIR || errno == ENOTEMPTY)
break;
ErrnoException::throwFromPath(ErrorCodes::CANNOT_RMDIR, dir_str, "Cannot remove directory {}", dir_str);
}
dir = dir.parent_path();
}
}
void LocalObjectStorage::removeObjects(const StoredObjects & objects) const
{
throwIfReadonly();
for (const auto & object : objects)
removeObject(object);
}
void LocalObjectStorage::removeObjectIfExists(const StoredObject & object)
{
throwIfReadonly();
if (exists(object))
removeObject(object);
}
void LocalObjectStorage::removeObjectsIfExist(const StoredObjects & objects)
{
throwIfReadonly();
for (const auto & object : objects)
removeObjectIfExists(object);
}
ObjectMetadata LocalObjectStorage::getObjectMetadata(const std::string & path) const
{
ObjectMetadata object_metadata;
LOG_TEST(log, "Getting metadata for path: {}", path);
auto time = fs::last_write_time(path);
object_metadata.size_bytes = fs::file_size(path);
object_metadata.etag = std::to_string(std::chrono::duration_cast<std::chrono::nanoseconds>(time.time_since_epoch()).count());
object_metadata.last_modified = Poco::Timestamp::fromEpochTime(
std::chrono::duration_cast<std::chrono::seconds>(time.time_since_epoch()).count());
return object_metadata;
}
void LocalObjectStorage::listObjects(const std::string & path, PathsWithMetadata & children, size_t/* max_keys */) const
{
if (!fs::exists(path) || !fs::is_directory(path))
return;
for (const auto & entry : fs::directory_iterator(path))
{
if (entry.is_directory())
{
listObjects(entry.path(), children, 0);
continue;
}
children.emplace_back(std::make_shared<PathWithMetadata>(entry.path(), getObjectMetadata(entry.path())));
}
}
bool LocalObjectStorage::existsOrHasAnyChild(const std::string & path) const
{
/// Unlike real object storage, existence of a prefix path can be checked by
/// just checking existence of this prefix directly, so simple exists is enough here.
return exists(StoredObject(path));
}
void LocalObjectStorage::copyObject( // NOLINT
const StoredObject & object_from,
const StoredObject & object_to,
const ReadSettings & read_settings,
const WriteSettings & write_settings,
std::optional<ObjectAttributes> /* object_to_attributes */)
{
throwIfReadonly();
auto in = readObject(object_from, read_settings);
auto out = writeObject(object_to, WriteMode::Rewrite, /* attributes= */ {}, /* buf_size= */ DBMS_DEFAULT_BUFFER_SIZE, write_settings);
copyData(*in, *out);
out->finalize();
}
void LocalObjectStorage::shutdown()
{
}
void LocalObjectStorage::startup()
{
}
void LocalObjectStorage::throwIfReadonly() const
{
if (settings.read_only)
throw Exception(ErrorCodes::READONLY, "Local object storage `{}` is readonly", getName());
}
ObjectStorageKey
LocalObjectStorage::generateObjectKeyForPath(const std::string & /* path */, const std::optional<std::string> & /* key_prefix */) const
{
constexpr size_t key_name_total_size = 32;
return ObjectStorageKey::createAsRelative(settings.key_prefix, getRandomASCIIString(key_name_total_size));
}
}