Skip to content

Commit

Permalink
Switch over to new user cfg system
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacKhor committed Aug 8, 2024
1 parent 2f7eca9 commit 234981e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/bdev_lsvd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ int bdev_lsvd_create(str img_name, rados_ioctx_t ioctx, lsvd_config cfg)
return 0;
}

int bdev_lsvd_create(str pool_name, str image_name, str cfg_path)
int bdev_lsvd_create(str pool_name, str image_name, str user_cfg)
{
auto be = connect_to_pool(pool_name);
auto cfg = lsvd_config::from_file(cfg_path);
auto cfg = lsvd_config::from_user_cfg(user_cfg);
PR_RET_IF(!cfg.has_value(), -1, "Failed to read config file");

return bdev_lsvd_create(image_name, be, cfg.value());
Expand Down
80 changes: 59 additions & 21 deletions src/config.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include <stdlib.h>
#include <string>
Expand All @@ -7,24 +9,70 @@
#include "config.h"
#include "utils.h"

vec<std::string> cfg_path({"lsvd.conf", "/usr/local/etc/lsvd.conf"});
lsvd_config lsvd_config::get_default() { return lsvd_config(); }

opt<lsvd_config> lsvd_config::from_file(str path)
opt<lsvd_config> lsvd_config::from_user_cfg(str cfg)
{
// write just a simple braindead parser
// syntax: lines of `key = value`
auto c = get_default();
if (cfg.empty())
return c;

try {
c.parse_file("/usr/local/etc/lsvd.json", true);
c.parse_file("./lsvd.json", true);

UNIMPLEMENTED();
if (cfg[0] == '{') {
c.parse_json(cfg);
} else {
c.parse_file(cfg, false);
}
return c;
} catch (const std::exception &e) {
log_error("Failed to parse config '{}': {}", cfg, e.what());
return std::nullopt;
}
}

lsvd_config lsvd_config::get_default() { return lsvd_config(); }
// https://stackoverflow.com/questions/116038/how-do-i-read-an-entire-file-into-a-stdstring-in-c
auto read_file(std::string_view path) -> std::string
{
constexpr auto read_size = std::size_t(4096);
auto stream = std::ifstream(path.data());
stream.exceptions(std::ios_base::badbit);

if (not stream)
throw std::ios_base::failure("file does not exist");

auto out = std::string();
auto buf = std::string(read_size, '\0');
while (stream.read(&buf[0], read_size))
out.append(buf, 0, stream.gcount());
out.append(buf, 0, stream.gcount());
return out;
}

void lsvd_config::parse_file(str path, bool can_be_missing)
{
assert(!path.empty());
if (access(path.c_str(), F_OK) == -1) {
if (can_be_missing) {
log_info("Optional config file missing: {}, continuing...", path);
return;
}
log_error("Config file not found: {}", path);
return;
}

auto cfg = read_file(path);
parse_json(cfg);
}

#define JSON_GET_BOOL(key) \
do { \
if (js.contains(#key)) { \
auto v = js[#key]; \
if (v.is_boolean()) { \
cfg.key = js[#key]; \
this->key = js[#key]; \
} else { \
log_error("Invalid value for key (must be bool): {}", #key); \
} \
Expand All @@ -36,7 +84,7 @@ lsvd_config lsvd_config::get_default() { return lsvd_config(); }
if (js.contains(#key)) { \
auto v = js[#key]; \
if (v.is_number_unsigned()) { \
cfg.key = js[#key]; \
this->key = js[#key]; \
} else { \
log_error("Invalid value for key (must be uint): {}", #key); \
} \
Expand All @@ -48,25 +96,17 @@ lsvd_config lsvd_config::get_default() { return lsvd_config(); }
if (js.contains(#key)) { \
auto v = js[#key]; \
if (v.is_string()) { \
cfg.key = js[#key]; \
this->key = js[#key]; \
} else { \
log_error("Invalid value for key (must be str): {}", #key); \
} \
} \
} while (0)

opt<lsvd_config> lsvd_config::from_json(str json)
void lsvd_config::parse_json(str json)
{
nlohmann::json js;
auto js = nlohmann::json::parse(json);

try {
js = nlohmann::json::parse(json);
} catch (nlohmann::json::parse_error &e) {
log_error("Failed to parse JSON: {}", e.what());
return std::nullopt;
}

lsvd_config cfg;
JSON_GET_STR(rcache_dir);
JSON_GET_UINT(rcache_bytes);
JSON_GET_UINT(rcache_fetch_window);
Expand All @@ -86,6 +126,4 @@ opt<lsvd_config> lsvd_config::from_json(str json)
JSON_GET_UINT(gc_threshold_pc);
JSON_GET_UINT(gc_write_window);
JSON_GET_BOOL(no_gc);

return cfg;
}
11 changes: 9 additions & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ class lsvd_config
return fspath(wlog_dir) / filename;
}

static opt<lsvd_config> from_json(str json);
static opt<lsvd_config> from_file(str path);
/**
* Read LSVD configuration from user-supplied string. The string can be
* either a json string containing the configuration, the path to a file
* containing the same, or empty, in which case it will be ignored.
*/
static opt<lsvd_config> from_user_cfg(str cfg);
static lsvd_config get_default();

private:
lsvd_config() {}

void parse_json(str js);
void parse_file(str path, bool can_be_missing = true);
};
3 changes: 2 additions & 1 deletion src/image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ lsvd_image::lsvd_image(std::string name, rados_ioctx_t io, lsvd_config cfg_)
: imgname(name), cfg(cfg_), io(io)
{
objstore = make_rados_backend(io);
rcache = get_read_cache_instance(cfg.rcache_dir, cfg.rcache_size, objstore);
rcache =
get_read_cache_instance(cfg.rcache_dir, cfg.rcache_bytes, objstore);

read_superblock();
debug("Found checkpoints: {}", checkpoints);
Expand Down
5 changes: 2 additions & 3 deletions src/spdk_frontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ static void start_lsvd(void *arg)
auto trid = get_trid(HOSTNAME, PORT);

// Add lsvd bdev
lsvd_config cfg; // TODO read this in from a config file
auto cfg =
lsvd_config::get_default(); // TODO read this in from a config file
cfg.rcache_bytes = 160 * 1024 * 1024; // small 160mb cache for testing
auto err = bdev_lsvd_create(args->image_name, io_ctx, cfg);
assert(err == 0);
Expand Down Expand Up @@ -226,7 +227,6 @@ int main(int argc, const char **argv)
return 1;
}


auto args = (start_lsvd_args){
.pool_name = argv[1],
.image_name = argv[2],
Expand All @@ -238,7 +238,6 @@ int main(int argc, const char **argv)

debug("Args: pool={}, image={}", args.pool_name, args.image_name);


spdk_app_opts opts = {.shutdown_cb = []() {
log_info("Shutting down LSVD SPDK program ...");
spdk_app_stop(0);
Expand Down
2 changes: 1 addition & 1 deletion src/translate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ void translate_impl::do_gc(std::stop_token &st)
workers->put_locked(req);
lk.unlock();

while ((int)requests.size() > cfg.gc_write_window &&
while (requests.size() > cfg.gc_write_window &&
!st.stop_requested()) {
auto t = requests.front();
t->wait();
Expand Down

0 comments on commit 234981e

Please sign in to comment.