Skip to content

Commit

Permalink
Merge pull request #93 from project-tsurugi/wip/path
Browse files Browse the repository at this point in the history
Wip/path
  • Loading branch information
t-horikawa authored Sep 8, 2023
2 parents eae568a + e26c1f4 commit 750884f
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 84 deletions.
12 changes: 5 additions & 7 deletions src/tateyama/authentication/authentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
*/
#include <iostream>
#include <cstdlib>
#include <filesystem>
#include <fstream>

#include <gflags/gflags.h>

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>

#include <tateyama/logging.h>

#include "authentication.h"
Expand Down Expand Up @@ -67,9 +65,9 @@ void auth_options() {
#endif
return;
}
const auto pre_defined_auth_file = boost::filesystem::path(std::string(pre_defined_auth_file_name));
if (boost::filesystem::exists(pre_defined_auth_file)) {
boost::filesystem::ifstream istrm{};
const auto pre_defined_auth_file = std::filesystem::path(std::string(pre_defined_auth_file_name));
if (std::filesystem::exists(pre_defined_auth_file)) {
std::ifstream istrm{};
istrm.open(pre_defined_auth_file, std::ios_base::in);

istrm.seekg( 0, std::ios_base::end );
Expand Down
11 changes: 6 additions & 5 deletions src/tateyama/configuration/bootstrap_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ static constexpr std::string_view default_configuration { // NOLINT
"port=12345\n"
"threads=104\n"

"[fdw]\n"
"name=tsurugi\n"
"threads=104\n"

"[datastore]\n"
"log_location=\n"
"logging_max_parallelism=112\n"
Expand All @@ -61,8 +57,13 @@ static constexpr std::string_view default_configuration { // NOLINT
"waiting_resolver_threads=2\n"

"[system]\n"
"pid_directory = /var/lock\n"
"pid_directory=/var/lock\n"

"[glog]\n"
"logtostderr=false\n"
"minloglevel=0\n"
"log_dir=\n"
"v=0\n"
};

} // namespace details
Expand Down
47 changes: 23 additions & 24 deletions src/tateyama/configuration/bootstrap_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@
#include <sstream>
#include <filesystem>

#include <boost/filesystem/path.hpp>

#include <tateyama/api/configuration.h>

namespace tateyama::configuration {

static const std::string_view DEFAULT_PID_DIR = "/tmp"; // NOLINT and obsolete
static const std::string_view PID_FILE_PREFIX = "tsurugi";
static const char *ENV_CONF = "TSURUGI_CONF"; // NOLINT
static const char *ENV_HOME = "TSURUGI_HOME"; // NOLINT
static const boost::filesystem::path HOME_CONF_FILE = boost::filesystem::path("var/etc/tsurugi.ini"); // NOLINT
static const std::filesystem::path HOME_CONF_FILE = std::filesystem::path("var/etc/tsurugi.ini"); // NOLINT
std::string_view default_property_for_bootstrap();

class bootstrap_configuration {
Expand All @@ -47,22 +44,22 @@ class bootstrap_configuration {
std::shared_ptr<tateyama::api::configuration::whole> get_configuration() {
return configuration_;
}
[[nodiscard]] boost::filesystem::path lock_file() const {
[[nodiscard]] std::filesystem::path lock_file() const {
return lock_file_;
}
[[nodiscard]] std::string digest() {
return digest(boost::filesystem::canonical(conf_file_).string());
return digest(std::filesystem::canonical(conf_file_).string());
}
[[nodiscard]] bool valid() const {
return valid_;
}
[[nodiscard]] boost::filesystem::path conf_file() const { // for test purpose
[[nodiscard]] std::filesystem::path conf_file() const { // for test purpose
return conf_file_;
}

private:
boost::filesystem::path conf_file_;
boost::filesystem::path lock_file_;
std::filesystem::path conf_file_;
std::filesystem::path lock_file_;
std::shared_ptr<tateyama::api::configuration::whole> configuration_{};
bool valid_{};

Expand All @@ -73,44 +70,46 @@ class bootstrap_configuration {

// tsurugi.ini
if (!fname.empty()) {
conf_file_ = boost::filesystem::path(std::string(fname));
conf_file_ = std::filesystem::path(std::string(fname));
} else {
if (auto env_conf = getenv(ENV_CONF); env_conf != nullptr) {
conf_file_ = boost::filesystem::path(env_conf);
conf_file_ = std::filesystem::path(env_conf);
} else {
if (env_home != nullptr) {
conf_file_ = boost::filesystem::path(env_home) / HOME_CONF_FILE;
conf_file_ = std::filesystem::path(env_home) / HOME_CONF_FILE;
} else {
throw std::runtime_error("no configuration file specified");
}
}
}
// do sanity check for conf_file_
boost::system::error_code error;
const bool result = boost::filesystem::exists(conf_file_, error);
std::error_code error;
const bool result = std::filesystem::exists(conf_file_, error);
if (!result || error) {
throw std::runtime_error(std::string("cannot find configuration file: ") + conf_file_.string());
}
if (boost::filesystem::is_directory(conf_file_)) {
if (std::filesystem::is_directory(conf_file_)) {
throw std::runtime_error(conf_file_.string() + " is a directory");
}
configuration_ = tateyama::api::configuration::create_configuration(conf_file_.string(), default_property_for_bootstrap());

if (env_home != nullptr) {
configuration_->base_path(std::filesystem::path(env_home));
}
std::string directory{DEFAULT_PID_DIR};
if (auto system_config = configuration_->get_section("system"); system_config) {
if (auto pid_dir = system_config->get<std::string>("pid_directory"); pid_dir) {
directory = pid_dir.value();
if (auto pid_dir = system_config->get<std::filesystem::path>("pid_directory"); pid_dir) {
std::filesystem::path directory = pid_dir.value();

std::string pid_file_name(PID_FILE_PREFIX);
pid_file_name += "-";
pid_file_name += digest(std::filesystem::canonical(conf_file_).string());
pid_file_name += ".pid";
lock_file_ = directory / std::filesystem::path(pid_file_name);
valid_ = true;
return;
}
}
std::string pid_file_name(PID_FILE_PREFIX);
pid_file_name += "-";
pid_file_name += digest(boost::filesystem::canonical(conf_file_).string());
pid_file_name += ".pid";
lock_file_ = boost::filesystem::path(std::string(directory)) / boost::filesystem::path(pid_file_name);
valid_ = true;
throw std::runtime_error("error in lock file location");
}
std::string digest(const std::string& path_string) {
auto hash = std::hash<std::string>{}(path_string);
Expand Down
16 changes: 7 additions & 9 deletions src/tateyama/datastore/backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
#include <cstring>
#include <sys/ioctl.h>
#include <termios.h>
#include <filesystem>

#include <gflags/gflags.h>

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>

#include <tateyama/framework/component_ids.h>
#include <tateyama/logging.h>

Expand Down Expand Up @@ -146,22 +144,22 @@ tgctl::return_code tgctl_backup_create(const std::string& path_to_backup) {
if (rtnv == tgctl::return_code::ok) {
auto backup_id = static_cast<std::int64_t>(rbgn.success().id());

auto location = boost::filesystem::path(path_to_backup);
auto location = std::filesystem::path(path_to_backup);

std::size_t total_bytes = 0;
if(!FLAGS_monitor.empty()) {
for (auto&& file : rbgn.success().simple_source().files()) {
auto src = boost::filesystem::path(file);
total_bytes += boost::filesystem::file_size(src);
auto src = std::filesystem::path(file);
total_bytes += std::filesystem::file_size(src);
}
}

std::size_t completed_bytes = 0;
for (auto&& file : rbgn.success().simple_source().files()) {
auto src = boost::filesystem::path(file);
boost::filesystem::copy_file(src, location / src.filename());
auto src = std::filesystem::path(file);
std::filesystem::copy_file(src, location / src.filename());
if(!FLAGS_monitor.empty()) {
completed_bytes += boost::filesystem::file_size(src);
completed_bytes += std::filesystem::file_size(src);
if (total_bytes > 0) {
monitor_output->progress(static_cast<float>(completed_bytes) / static_cast<float>(total_bytes));
} else {
Expand Down
21 changes: 10 additions & 11 deletions src/tateyama/process/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
#include <cstdlib>
#include <unistd.h>
#include <stdexcept> // std::runtime_error
#include <filesystem>

#include <gflags/gflags.h>

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/process.hpp>

#include <tateyama/logging.h>
Expand Down Expand Up @@ -130,13 +129,13 @@ tgctl::return_code tgctl_start(const std::string& argv0, bool need_check, tateya
}

std::string server_name(server_name_string);
boost::filesystem::path path_for_this{};
if (auto a0f = boost::filesystem::path(argv0); a0f.parent_path().string().empty()) {
path_for_this = boost::filesystem::canonical(boost::process::search_path(a0f));
std::filesystem::path path_for_this{};
if (auto a0f = std::filesystem::path(argv0); a0f.is_absolute()) {
path_for_this = std::filesystem::canonical(a0f);
} else{
path_for_this = boost::filesystem::canonical(a0f);
path_for_this = std::filesystem::canonical(std::filesystem::path(boost::process::search_path(argv0).string()));
}
if (!boost::filesystem::exists(path_for_this)) {
if (!std::filesystem::exists(path_for_this)) {
std::cerr << "cannot find " << server_name_string << std::endl;
return tgctl::return_code::err;
}
Expand All @@ -162,17 +161,17 @@ tgctl::return_code tgctl_start(const std::string& argv0, bool need_check, tateya
*static_cast<pid_t*>(shm_data) = 0;

if (fork() == 0) {
auto base = boost::filesystem::canonical(path_for_this).parent_path().parent_path();
auto exec = base / boost::filesystem::path("libexec") / boost::filesystem::path(server_name);
auto base = std::filesystem::canonical(path_for_this).parent_path().parent_path();
auto exec = base / std::filesystem::path("libexec") / std::filesystem::path(server_name);
std::vector<std::string> args{};
build_args(args, mode);
boost::process::child cld(exec, boost::process::args (args));
boost::process::child cld(exec.string(), boost::process::args (args));
pid_t child_pid = cld.id();
*static_cast<pid_t*>(shm_data) = child_pid;
cld.detach();

std::string undertaker_name(undertaker_name_string);
auto undertaker = base / boost::filesystem::path("libexec") / boost::filesystem::path(undertaker_name);
auto undertaker = base / std::filesystem::path("libexec") / std::filesystem::path(undertaker_name);
execl(undertaker.string().c_str(), undertaker.string().c_str(), std::to_string(child_pid).c_str(), nullptr); // NOLINT
exit(-1); // should not reach here
}
Expand Down
22 changes: 10 additions & 12 deletions src/tateyama/process/proc_mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
#include <sys/types.h>
#include <unistd.h>
#include <stdexcept> // std::runtime_error

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
#include <filesystem>
#include <fstream>

namespace tateyama::process {

Expand All @@ -35,9 +33,9 @@ class proc_mutex {
error,
};

explicit proc_mutex(boost::filesystem::path lock_file, bool create_file = true, bool throw_exception = true)
explicit proc_mutex(std::filesystem::path lock_file, bool create_file = true, bool throw_exception = true)
: lock_file_(std::move(lock_file)), create_file_(create_file) {
if (!create_file_ && throw_exception && !boost::filesystem::exists(lock_file_)) {
if (!create_file_ && throw_exception && !std::filesystem::exists(lock_file_)) {
throw std::runtime_error("the lock file does not exist");
}
}
Expand Down Expand Up @@ -81,12 +79,12 @@ class proc_mutex {
return lock_file_.generic_string();
}
[[nodiscard]] lock_state check() {
boost::system::error_code error;
const bool result = boost::filesystem::exists(lock_file_, error);
std::error_code error;
const bool result = std::filesystem::exists(lock_file_, error);
if (!result || error) {
return lock_state::no_file;
}
if (!boost::filesystem::is_regular_file(lock_file_)) {
if (!std::filesystem::is_regular_file(lock_file_)) {
return lock_state::error;
}
if (fd_ = open(lock_file_.generic_string().c_str(), O_WRONLY); fd_ < 0) { // NOLINT
Expand Down Expand Up @@ -118,17 +116,17 @@ class proc_mutex {
}

private:
boost::filesystem::path lock_file_;
std::filesystem::path lock_file_;
int fd_{};
const bool create_file_;

[[nodiscard]] bool contents(std::string& str, bool do_check = true) {
if (do_check && check() != lock_state::locked) {
return false;
}
auto size = static_cast<std::size_t>(boost::filesystem::file_size(lock_file_));
auto size = static_cast<std::size_t>(std::filesystem::file_size(lock_file_));
str.resize(size, '\0');
boost::filesystem::ifstream file(lock_file_);
std::ifstream file(lock_file_);
file.read(str.data(), static_cast<std::streamsize>(size));
return true;
}
Expand Down
19 changes: 8 additions & 11 deletions src/tateyama/server/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

#include "gflags/gflags.h"

#include "boost/filesystem/path.hpp"
#include "boost/filesystem/operations.hpp"

DEFINE_int32(dump_batch_size, 1024, "Batch size for dump"); //NOLINT
DEFINE_int32(load_batch_size, 1024, "Batch size for load"); //NOLINT

Expand All @@ -42,11 +39,11 @@ namespace jogasaki::utils {
"STOCK"
};

boost::filesystem::path prepare(std::string location) {
boost::filesystem::path dir(location);
std::filesystem::path prepare(const std::string& location) {
std::filesystem::path dir(location);
dir = dir / "dump";
if (!boost::filesystem::exists(dir)) {
if (!boost::filesystem::create_directories(dir)) {
if (!std::filesystem::exists(dir)) {
if (!std::filesystem::create_directories(dir)) {
throw std::ios_base::failure("Failed to create directory.");
}
}
Expand All @@ -56,7 +53,7 @@ namespace jogasaki::utils {
void
dump([[maybe_unused]] jogasaki::api::database& tgdb, std::string &location)
{
boost::filesystem::path dir = prepare(location);
std::filesystem::path dir = prepare(location);
for (auto& table : tables) {
std::ofstream ofs((dir / (table+".tbldmp")).c_str());
if (ofs.fail()) {
Expand All @@ -69,7 +66,7 @@ namespace jogasaki::utils {
void
load([[maybe_unused]] jogasaki::api::database& tgdb, std::string &location)
{
boost::filesystem::path dir = prepare(location);
std::filesystem::path dir = prepare(location);
for (auto& table : tables) {
std::ifstream ifs((dir / (table+".tbldmp")).c_str());
if (ifs.fail()) {
Expand All @@ -94,7 +91,7 @@ namespace jogasaki::utils {
void
dump_tpch(jogasaki::api::database& tgdb, std::string &location)
{
boost::filesystem::path dir = prepare(location);
std::filesystem::path dir = prepare(location);
for (auto& table : tpch_tables) {
std::ofstream ofs((dir / (table+".tbldmp")).c_str());
if (ofs.fail()) {
Expand All @@ -107,7 +104,7 @@ namespace jogasaki::utils {
void
load_tpch(jogasaki::api::database& tgdb, std::string &location)
{
boost::filesystem::path dir = prepare(location);
std::filesystem::path dir = prepare(location);
for (auto& table : tpch_tables) {
std::ifstream ifs((dir / (table+".tbldmp")).c_str());
if (ifs.fail()) {
Expand Down
Loading

0 comments on commit 750884f

Please sign in to comment.