Newer
Older
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
/*
Copyright 2018-2022, Barcelona Supercomputing Center (BSC), Spain
Copyright 2015-2022, Johannes Gutenberg Universitaet Mainz, Germany
This software was partially supported by the
EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
This software was partially supported by the
ADA-FS project under the SPPEXA project funded by the DFG.
This file is part of GekkoFS.
GekkoFS 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, either version 3 of the License, or
(at your option) any later version.
GekkoFS 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 GekkoFS. If not, see <https://www.gnu.org/licenses/>.
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <daemon/daemon.hpp>
#include <daemon/backend/metadata/db.hpp>
#include <daemon/backend/exceptions.hpp>
#include <daemon/backend/metadata/metadata_module.hpp>
#include <common/metadata.hpp>
#include <common/path_util.hpp>
#include <iostream>
#include <daemon/backend/metadata/parallax_backend.hpp>
#include <mutex>
using namespace std;
extern "C" {
#include <sys/stat.h>
}
std::recursive_mutex parallax_mutex_;
namespace gkfs::metadata {
/**
* @brief Destroy the Kreon Backend:: Kreon Backend object
* We remove the file, too large for the CI.
* TODO: Insert option
*/
ParallaxBackend::~ParallaxBackend() {
par_close(par_db_);
}
/**
* Called when the daemon is started: Connects to the KV store
* @param path where KV store data is stored
*/
ParallaxBackend::ParallaxBackend(const std::string& path)
: par_path_(std::move(path)) {
// We try to open options.yml if it exists, if not we create it by default
int options = open("options.yml", O_RDWR | O_CREAT, 0644);
int64_t sizeOptions;
sizeOptions = lseek(options, 0, SEEK_END);
if(sizeOptions == 0) {
std::string optcontent =
"level0_size: 64\ngc_interval: 10\ngrowth_factor: 4\nmedium_log_LRU_cache_size: 400\nlevel_medium_inplace: 3\n";
write(options, optcontent.c_str(), optcontent.length());
}
close(options);
int64_t size;
int fd = open(par_path_.c_str(), O_RDWR | O_CREAT, 0644);
if(fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
// Check size if we want to reuse it
size = lseek(fd, 0, SEEK_END);
if(size == -1) {
printf("[%s:%s:%d] failed to determine volume size exiting...\n",
__FILE__, __func__, __LINE__);
perror("ioctl");
size = GKFS_DATA->parallax_size_md();
lseek(fd, size - 1, SEEK_SET);
std::string tmp = "x";
write(fd, tmp.c_str(), 1);
close(fd);
// We format the database TODO this doesn't work kv_format.parallax is
// not in path
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
std::string cmd = "kv_format.parallax --device " + par_path_ +
" --max_regions_num 1 ";
system(cmd.c_str());
}
par_options_.create_flag = PAR_CREATE_DB;
par_options_.db_name = "test";
par_options_.volume_name = (char*) malloc(par_path_.size() + 1);
strcpy(par_options_.volume_name, par_path_.c_str());
par_options_.volume_start = 0;
par_options_.volume_size = 0;
par_db_ = par_open(&par_options_);
}
/**
* Exception wrapper on Status object. Throws NotFoundException if
* s == "Not Found", general DBException otherwise
* @param String with status
* @throws DBException
*/
void
ParallaxBackend::throw_status_excpt(const std::string& s) {
if(s == "Not Found") {
throw NotFoundException(s);
} else {
throw DBException(s);
}
}
/**
* Convert a String to klc_key
* @param key
* @param par_key struct
*/
inline void
ParallaxBackend::str2par(const std::string& key, struct par_key& K) const {
K.size = key.size();
K.data = key.c_str();
}
/**
* Convert a String to klc_value
* @param value
* @param par_value struct
*/
inline void
ParallaxBackend::str2par(const std::string& value, struct par_value& V) const {
V.val_size = value.size() + 1;
V.val_buffer = (char*) value.c_str();
}
/**
* Gets a KV store value for a key
* @param key
* @return value
* @throws DBException on failure, NotFoundException if entry doesn't exist
*/
std::string
ParallaxBackend::get_impl(const std::string& key) const {
std::string val;
struct par_key K;
struct par_value V;
V.val_buffer = NULL;
str2par(key, K);
par_ret_code ret = par_get(par_db_, &K, &V);
if(ret != PAR_SUCCESS) {
throw_status_excpt("Not Found");
} else {
val = V.val_buffer;
free(V.val_buffer);
}
return val;
}
/**
* Puts an entry into the KV store
* @param key
* @param val
* @throws DBException on failure
*/
void
ParallaxBackend::put_impl(const std::string& key, const std::string& val) {
struct par_key_value key_value;
str2par(key, key_value.k);
str2par(val, key_value.v);
par_ret_code ret = par_put(par_db_, &key_value);
if(ret != PAR_SUCCESS) {
throw_status_excpt("Not Found");
}
}
/**
* Puts an entry into the KV store if it doesn't exist. This function does not
* use a mutex.
* @param key
* @param val
* @throws DBException on failure, ExistException if entry already exists
*/
void
ParallaxBackend::put_no_exist_impl(const std::string& key,
const std::string& val) {
struct par_key_value key_value;
str2par(key, key_value.k);
str2par(val, key_value.v);
par_ret_code ret = par_exists(par_db_, &key_value.k);
if(ret == PAR_KEY_NOT_FOUND) {
par_put(par_db_, &key_value);
} else
throw ExistsException(key);
}
/**
* Removes an entry from the KV store
* @param key
* @throws DBException on failure, NotFoundException if entry doesn't exist
*/
void
ParallaxBackend::remove_impl(const std::string& key) {
struct par_key k;
str2par(key, k);
par_ret_code ret = par_delete(par_db_, &k);
if(ret != PAR_SUCCESS) {
throw_status_excpt("Not Found");
}
}
/**
* checks for existence of an entry
* @param key
* @return true if exists
* @throws DBException on failure
*/
bool
ParallaxBackend::exists_impl(const std::string& key) {
struct par_key k;
str2par(key, k);
par_ret_code ret = par_exists(par_db_, &k);
if(ret == PAR_KEY_NOT_FOUND) {
return true;
}
return false; // TODO it is not the only case, we can have errors
}
/**
* Updates a metadentry atomically and also allows to change keys
* @param old_key
* @param new_key
* @param val
* @throws DBException on failure, NotFoundException if entry doesn't exist
*/
void
ParallaxBackend::update_impl(const std::string& old_key,
const std::string& new_key,
const std::string& val) {
// TODO: Check Parallax transactions/Batches
struct par_key_value n_key_value;
struct par_key o_key;
str2par(new_key, n_key_value.k);
str2par(val, n_key_value.v);
str2par(old_key, o_key);
par_delete(par_db_, &o_key);
par_ret_code ret = par_put(par_db_, &n_key_value);
if(ret != PAR_SUCCESS) {
throw_status_excpt("Not Found");
}
}
/**
* Updates the size on the metadata
* Operation. E.g., called before a write() call
* @param key
* @param size
* @param append
* @throws DBException on failure
*/
void
ParallaxBackend::increase_size_impl(const std::string& key, size_t size,
bool append) {
lock_guard<recursive_mutex> lock_guard(parallax_mutex_);
auto value = get(key);
// Decompress string
Metadata md(value);
if(append)
size += md.size();
md.size(size);
update(key, key, md.serialize());
}
/**
* Decreases the size on the metadata
* Operation E.g., called before a truncate() call
* @param key
* @param size
* @throws DBException on failure
*/
void
ParallaxBackend::decrease_size_impl(const std::string& key, size_t size) {
lock_guard<recursive_mutex> lock_guard(parallax_mutex_);
auto value = get(key);
// Decompress string
Metadata md(value);
md.size(size);
update(key, key, md.serialize());
}
/**
* Return all the first-level entries of the directory @dir
*
* @return vector of pair <std::string name, bool is_dir>,
* where name is the name of the entries and is_dir
* is true in the case the entry is a directory.
*/
std::vector<std::pair<std::string, bool>>
ParallaxBackend::get_dirents_impl(const std::string& dir) const {
auto root_path = dir;
struct par_key K;
str2par(root_path, K);
par_scanner S = par_init_scanner(par_db_, &K, PAR_GREATER_OR_EQUAL);
std::vector<std::pair<std::string, bool>> entries;
while(par_is_valid(S)) {
struct par_key K2 = par_get_key(S);
struct par_value value = par_get_value(S);
std::string k(K2.data, K2.size);
std::string v(value.val_buffer, value.val_size);
if(k.size() < root_path.size() ||
k.substr(0, root_path.size()) != root_path) {
break;
}
if(k.size() == root_path.size()) {
par_get_next(S);
continue;
}
/***** Get File name *****/
auto name = k;
if(name.find_first_of('/', root_path.size()) != std::string::npos) {
// skip stuff deeper then one level depth
par_get_next(S);
continue;
}
// remove prefix
name = name.substr(root_path.size());
// relative path of directory entries must not be empty
assert(!name.empty());
Metadata md(v);
auto is_dir = S_ISDIR(md.mode());
entries.emplace_back(std::move(name), is_dir);
par_get_next(S);
}
// If we don't close the scanner we cannot delete keys
par_close_scanner(S);
return entries;
}
/**
* Return all the first-level entries of the directory @dir
*
* @return vector of pair <std::string name, bool is_dir - size - ctime>,
* where name is the name of the entries and is_dir
* is true in the case the entry is a directory.
*/
std::vector<std::tuple<std::string, bool, size_t, time_t>>
ParallaxBackend::get_dirents_extended_impl(const std::string& dir) const {
auto root_path = dir;
// assert(gkfs::path::is_absolute(root_path));
// add trailing slash if missing
if(!gkfs::path::has_trailing_slash(root_path) && root_path.size() != 1) {
// add trailing slash only if missing and is not the root_folder "/"
root_path.push_back('/');
}
struct par_key K;
str2par(root_path, K);
par_scanner S = par_init_scanner(par_db_, &K, PAR_GREATER_OR_EQUAL);
std::vector<std::tuple<std::string, bool, size_t, time_t>> entries;
while(par_is_valid(S)) {
struct par_key K2 = par_get_key(S);
struct par_value value = par_get_value(S);
std::string k(K2.data, K2.size);
std::string v(value.val_buffer, value.val_size);
if(k.size() < root_path.size() ||
k.substr(0, root_path.size()) != root_path) {
break;
}
if(k.size() == root_path.size()) {
if(par_get_next(S) && !par_is_valid(S))
break;
continue;
}
/***** Get File name *****/
auto name = k;
if(name.find_first_of('/', root_path.size()) != std::string::npos) {
// skip stuff deeper then one level depth
if(par_get_next(S) && !par_is_valid(S))
break;
continue;
}
// remove prefix
name = name.substr(root_path.size());
// relative path of directory entries must not be empty
assert(!name.empty());
Metadata md(v);
auto is_dir = S_ISDIR(md.mode());
entries.emplace_back(std::forward_as_tuple(std::move(name), is_dir,
md.size(), md.ctime()));
if(par_get_next(S) && !par_is_valid(S))
break;
}
// If we don't close the scanner we cannot delete keys
par_close_scanner(S);
return entries;
}
/**
* Code example for iterating all entries in KV store. This is for debug only as
* it is too expensive
*/
void
ParallaxBackend::iterate_all_impl() const {}
} // namespace gkfs::metadata