Skip to content

Commit

Permalink
wasmedge compile tmp (#2336)
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <[email protected]>
  • Loading branch information
turuslan authored Jan 14, 2025
1 parent fef2e72 commit 68f26c8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
6 changes: 5 additions & 1 deletion core/runtime/wasm_edge/module_factory_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,12 @@ namespace kagome::runtime::wasm_edge {

CompilerContext compiler = WasmEdge_CompilerCreate(configure_ctx_raw);
SL_INFO(log_, "Start compiling wasm module {}", path_compiled);
// Multiple processes can write to same cache file concurrently,
// write to tmp file first to avoid conflict.
OUTCOME_TRY(tmp, TmpFile::make(path_compiled));
WasmEdge_UNWRAP_COMPILE_ERR(WasmEdge_CompilerCompileFromBuffer(
compiler.raw(), code.data(), code.size(), path_compiled.c_str()));
compiler.raw(), code.data(), code.size(), tmp.path().c_str()));
OUTCOME_TRY(tmp.rename());
SL_INFO(log_, "Compilation finished, saved at {}", path_compiled);
return outcome::success();
}
Expand Down
67 changes: 55 additions & 12 deletions core/utils/write_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
#pragma once

#include <boost/filesystem/operations.hpp>
#include <filesystem>
#include <fstream>
#include <optional>
#include <qtils/bytestr.hpp>
#include <qtils/option_take.hpp>
#include <qtils/outcome.hpp>

namespace kagome {
Expand All @@ -26,20 +29,60 @@ namespace kagome {
return writeFile(path, qtils::byte2str(data));
}

outcome::result<void> writeFileTmp(const std::filesystem::path &path,
auto &&data) {
boost::system::error_code ec1;
auto tmp =
boost::filesystem::unique_path(path.native() + ".%%%%", ec1).native();
if (ec1) {
return ec1;
/**
* Wrapper to generate tmp file name and rename it later.
* Concurrent writers would corrupt file, so they write to tmp file first, and
* atomically rename it after file is completely written.
*/
struct TmpFile {
/**
* Generate tmp file name for path.
* Tmp file is created in same directory as target path,
* to avoid `EXDEV` error from `rename`.
*/
static outcome::result<TmpFile> make(std::filesystem::path path) {
boost::system::error_code ec;
auto tmp =
boost::filesystem::unique_path(path.native() + ".%%%%", ec).native();
if (ec) {
return ec;
}
return TmpFile{std::move(path), std::move(tmp)};
}
OUTCOME_TRY(writeFile(tmp, data));
std::error_code ec2;
std::filesystem::rename(tmp, path, ec2);
if (ec2) {
return ec2;

TmpFile(std::filesystem::path target, std::filesystem::path tmp)
: target{std::move(target)}, tmp{std::move(tmp)} {}

/**
* Get current file path.
*/
std::filesystem::path path() const {
return tmp.value_or(target);
}

/**
* Rename file to target name.
*/
outcome::result<void> rename() {
if (auto tmp = qtils::optionTake(this->tmp)) {
std::error_code ec;
std::filesystem::rename(*tmp, target, ec);
if (ec) {
return ec;
}
}
return outcome::success();
}

std::filesystem::path target;
std::optional<std::filesystem::path> tmp;
};

outcome::result<void> writeFileTmp(const std::filesystem::path &path,
auto &&data) {
OUTCOME_TRY(tmp, TmpFile::make(path));
OUTCOME_TRY(writeFile(tmp.path(), data));
OUTCOME_TRY(tmp.rename());
return outcome::success();
}
} // namespace kagome

0 comments on commit 68f26c8

Please sign in to comment.