Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 53 additions & 19 deletions src/cpp/src/kvs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
********************************************************************************/
#include "kvs.hpp"
#include "internal/kvs_helper.hpp"
#include <unistd.h> // fileno(), fdatasync()
#include <cstdio> // std::fopen, std::fwrite, std::fflush, std::fclose
#include <fstream>
#include <iostream>
#include <sstream>
Expand Down Expand Up @@ -470,6 +472,46 @@ score::ResultBlank Kvs::remove_key(const std::string_view key)
return result;
}

/* Helper: write data to a file and ensure it reaches physical storage.*/
score::ResultBlank Kvs::write_and_sync(const std::string& path, const void* data, std::size_t size)
{
auto file_deleter = [](std::FILE* f) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually a deleter? This looks more like FILE cleanup.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the point of view of a smart pointer (std::unique_ptr in this case) it is a deleter as it frees memory resources (through fclose) when the smart pointer ends its life when exiting the scope according to the RAII paradigm.

if (f != nullptr)
{
(void)std::fclose(f);
}
};
std::unique_ptr<std::FILE, decltype(file_deleter)> file{std::fopen(path.c_str(), "wb"), file_deleter};

if (file == nullptr)
{
logger->LogError() << "Failed to open file '" << path << "'";
return score::MakeUnexpected(ErrorCode::PhysicalStorageFailure);
}

if (std::fwrite(data, sizeof(char), size, file.get()) != size)
{
logger->LogError() << "Failed to write to file '" << path << "'";
return score::MakeUnexpected(ErrorCode::PhysicalStorageFailure);
}

/* Flush the buffer to the OS. */
if (std::fflush(file.get()) != 0)
{
logger->LogError() << "Failed to flush file '" << path << "'";
return score::MakeUnexpected(ErrorCode::PhysicalStorageFailure);
}

/* Request the OS to commit the data from its buffers to physical storage */
if (::fdatasync(::fileno(file.get())) != 0)
{
logger->LogError() << "Failed to sync file '" << path << "'";
return score::MakeUnexpected(ErrorCode::PhysicalStorageFailure);
}

return {};
}

/* Helper Function to write JSON data to a file for flush process (also adds Hash file)*/
score::ResultBlank Kvs::write_json_data(const std::string& buf)
{
Expand All @@ -485,26 +527,18 @@ score::ResultBlank Kvs::write_json_data(const std::string& buf)
}
else
{
std::ofstream out(json_path.CStr(), std::ios::binary);
if (!out.write(buf.data(), buf.size()))
/* Write JSON file */
result = write_and_sync(json_path.Native(), buf.data(), buf.size());
if (!result.has_value())
{
result = score::MakeUnexpected(ErrorCode::PhysicalStorageFailure);
}
else
{
/* Write Hash File */
std::array<uint8_t, 4> hash_bytes = get_hash_bytes(buf);
score::filesystem::Path fn_hash = filename_prefix.Native() + "_0.hash";
std::ofstream hout(fn_hash.CStr(), std::ios::binary);
if (!hout.write(reinterpret_cast<const char*>(hash_bytes.data()), hash_bytes.size()))
{
result = score::MakeUnexpected(ErrorCode::PhysicalStorageFailure);
}
else
{
result = score::ResultBlank{};
}
return result;
}

/* Write Hash File */
std::array<uint8_t, 4> hash_bytes = get_hash_bytes(buf);
score::filesystem::Path fn_hash = filename_prefix.Native() + "_0.hash";

result = write_and_sync(fn_hash.Native(), hash_bytes.data(), hash_bytes.size());
}
}
else
Expand Down Expand Up @@ -600,7 +634,7 @@ score::Result<size_t> Kvs::snapshot_count() const
error = true;
break;
}
count = idx+1;
count = idx + 1;
}
if (error)
{
Expand Down
1 change: 1 addition & 0 deletions src/cpp/src/kvs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ class Kvs final
score::Result<std::unordered_map<std::string, KvsValue>> open_json(const score::filesystem::Path& prefix,
OpenJsonNeedFile need_file);
score::ResultBlank write_json_data(const std::string& buf);
score::ResultBlank write_and_sync(const std::string& path, const void* data, std::size_t size);
};

} /* namespace score::mw::per::kvs */
Expand Down
Loading