-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
/build before-merge Signed-off-by: iceseer <[email protected]>
- Loading branch information
Showing
11 changed files
with
529 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
{ | ||
"block_store_path" : "/tmp/block_store/", | ||
"torii_port" : 50051, | ||
"internal_port" : 10001, | ||
"database": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "ametsuchi/impl/rocksdb_block_storage.hpp" | ||
|
||
#include "ametsuchi/impl/rocksdb_common.hpp" | ||
#include "backend/protobuf/block.hpp" | ||
#include "common/byteutils.hpp" | ||
#include "logger/logger.hpp" | ||
|
||
using namespace iroha::ametsuchi; | ||
|
||
#define CHECK_OPERATION(command, ...) \ | ||
if (auto result = (__VA_ARGS__); expected::hasError(result)) { \ | ||
log_->error("Error while block {} " command ". Code: {}. Description: {}", \ | ||
block->height(), \ | ||
result.assumeError().code, \ | ||
result.assumeError().description); \ | ||
return false; \ | ||
} | ||
|
||
namespace { | ||
inline iroha::expected::Result<void, DbError> incrementTotalBlocksCount( | ||
iroha::ametsuchi::RocksDbCommon &common) { | ||
RDB_TRY_GET_VALUE( | ||
opt_count, | ||
forBlocksTotalCount<kDbOperation::kGet, kDbEntry::kCanExist>(common)); | ||
|
||
common.encode(opt_count ? *opt_count + 1ull : 1ull); | ||
RDB_ERROR_CHECK( | ||
forBlocksTotalCount<kDbOperation::kPut, kDbEntry::kMustExist>(common)); | ||
|
||
return {}; | ||
} | ||
} // namespace | ||
|
||
RocksDbBlockStorage::RocksDbBlockStorage( | ||
std::shared_ptr<RocksDBContext> db_context, | ||
std::shared_ptr<shared_model::interface::BlockJsonConverter> json_converter, | ||
logger::LoggerPtr log) | ||
: db_context_(std::move(db_context)), | ||
json_converter_(std::move(json_converter)), | ||
log_(std::move(log)) {} | ||
|
||
bool RocksDbBlockStorage::insert( | ||
std::shared_ptr<const shared_model::interface::Block> block) { | ||
return json_converter_->serialize(*block).match( | ||
[&](const auto &block_json) { | ||
RocksDbCommon common(db_context_); | ||
CHECK_OPERATION("insertion", | ||
forBlock<kDbOperation::kCheck, kDbEntry::kMustNotExist>( | ||
common, block->height())); | ||
|
||
common.valueBuffer() = block_json.value; | ||
CHECK_OPERATION("storing", | ||
forBlock<kDbOperation::kPut>(common, block->height())); | ||
|
||
CHECK_OPERATION("total count storing", | ||
incrementTotalBlocksCount(common)); | ||
return true; | ||
}, | ||
[this](const auto &error) { | ||
log_->warn("Error while block serialization: {}", error.error); | ||
return false; | ||
}); | ||
} | ||
|
||
boost::optional<std::unique_ptr<shared_model::interface::Block>> | ||
RocksDbBlockStorage::fetch( | ||
shared_model::interface::types::HeightType height) const { | ||
RocksDbCommon common(db_context_); | ||
if (auto result = | ||
forBlock<kDbOperation::kGet, kDbEntry::kMustExist>(common, height); | ||
expected::hasError(result)) { | ||
log_->error("Error while block {} reading. Code: {}. Description: {}", | ||
height, | ||
result.assumeError().code, | ||
result.assumeError().description); | ||
return boost::none; | ||
} | ||
|
||
return json_converter_->deserialize(common.valueBuffer()) | ||
.match( | ||
[&](auto &&block) { | ||
return boost::make_optional< | ||
std::unique_ptr<shared_model::interface::Block>>( | ||
std::move(block.value)); | ||
}, | ||
[&](const auto &error) | ||
-> boost::optional< | ||
std::unique_ptr<shared_model::interface::Block>> { | ||
log_->warn("Error while block deserialization: {}", error.error); | ||
return boost::none; | ||
}); | ||
} | ||
|
||
size_t RocksDbBlockStorage::size() const { | ||
RocksDbCommon common(db_context_); | ||
if (auto result = | ||
forBlocksTotalCount<kDbOperation::kGet, kDbEntry::kMustExist>(common); | ||
expected::hasValue(result)) | ||
return *result.assumeValue(); | ||
return 0ull; | ||
} | ||
|
||
void RocksDbBlockStorage::reload() {} | ||
|
||
void RocksDbBlockStorage::clear() { | ||
RocksDbCommon common(db_context_); | ||
|
||
if (auto status = common.filterDelete(fmtstrings::kPathWsv); !status.ok()) | ||
log_->error("Unable to delete WSV. Description: {}", status.ToString()); | ||
|
||
if (auto status = common.filterDelete(fmtstrings::kPathStore); !status.ok()) | ||
log_->error("Unable to delete STORE. Description: {}", status.ToString()); | ||
} | ||
|
||
iroha::expected::Result<void, std::string> RocksDbBlockStorage::forEach( | ||
iroha::ametsuchi::BlockStorage::FunctionType function) const { | ||
uint64_t const blocks_count = size(); | ||
for (uint64_t ix = 1; ix <= blocks_count; ++ix) { | ||
auto maybe_block = fetch(ix); | ||
if (maybe_block) { | ||
auto maybe_error = function(std::move(maybe_block).value()); | ||
if (iroha::expected::hasError(maybe_error)) { | ||
return maybe_error.assumeError(); | ||
} | ||
} else { | ||
return fmt::format("Failed to fetch block {}", ix); | ||
} | ||
} | ||
return {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef IROHA_ROCKSDB_BLOCK_STORAGE_HPP | ||
#define IROHA_ROCKSDB_BLOCK_STORAGE_HPP | ||
|
||
#include "ametsuchi/block_storage.hpp" | ||
|
||
#include "interfaces/iroha_internal/block_json_converter.hpp" | ||
#include "logger/logger_fwd.hpp" | ||
|
||
namespace iroha::ametsuchi { | ||
struct RocksDBContext; | ||
|
||
class RocksDbBlockStorage : public BlockStorage { | ||
public: | ||
RocksDbBlockStorage( | ||
std::shared_ptr<RocksDBContext> db_context, | ||
std::shared_ptr<shared_model::interface::BlockJsonConverter> | ||
json_converter, | ||
logger::LoggerPtr log); | ||
|
||
bool insert( | ||
std::shared_ptr<const shared_model::interface::Block> block) override; | ||
|
||
boost::optional<std::unique_ptr<shared_model::interface::Block>> fetch( | ||
shared_model::interface::types::HeightType height) const override; | ||
|
||
size_t size() const override; | ||
|
||
void reload() override; | ||
|
||
void clear() override; | ||
|
||
expected::Result<void, std::string> forEach( | ||
FunctionType function) const override; | ||
|
||
private: | ||
std::shared_ptr<RocksDBContext> db_context_; | ||
std::shared_ptr<shared_model::interface::BlockJsonConverter> | ||
json_converter_; | ||
logger::LoggerPtr log_; | ||
}; | ||
|
||
} // namespace iroha::ametsuchi | ||
|
||
#endif // IROHA_ROCKSDB_BLOCK_STORAGE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "ametsuchi/impl/rocksdb_block_storage_factory.hpp" | ||
|
||
#include "ametsuchi/impl/rocksdb_block_storage.hpp" | ||
#include "ametsuchi/impl/rocksdb_common.hpp" | ||
|
||
using namespace iroha::ametsuchi; | ||
|
||
RocksDbBlockStorageFactory::RocksDbBlockStorageFactory( | ||
std::shared_ptr<RocksDBContext> db_context, | ||
std::shared_ptr<shared_model::interface::BlockJsonConverter> | ||
json_block_converter, | ||
logger::LoggerManagerTreePtr log_manager) | ||
: db_context_(std::move(db_context)), | ||
json_block_converter_(std::move(json_block_converter)), | ||
log_manager_(std::move(log_manager)) {} | ||
|
||
iroha::expected::Result<std::unique_ptr<BlockStorage>, std::string> | ||
RocksDbBlockStorageFactory::create() { | ||
return std::make_unique<RocksDbBlockStorage>( | ||
db_context_, | ||
json_block_converter_, | ||
log_manager_->getChild("RocksDbBlockFactory")->getLogger()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef IROHA_ROCKSDB_BLOCK_STORAGE_FACTORY_HPP | ||
#define IROHA_ROCKSDB_BLOCK_STORAGE_FACTORY_HPP | ||
|
||
#include "ametsuchi/block_storage_factory.hpp" | ||
|
||
#include "interfaces/iroha_internal/block_json_converter.hpp" | ||
#include "logger/logger_manager.hpp" | ||
|
||
namespace iroha::ametsuchi { | ||
struct RocksDBContext; | ||
|
||
class RocksDbBlockStorageFactory : public BlockStorageFactory { | ||
public: | ||
RocksDbBlockStorageFactory( | ||
std::shared_ptr<RocksDBContext> db_context, | ||
std::shared_ptr<shared_model::interface::BlockJsonConverter> | ||
json_block_converter, | ||
logger::LoggerManagerTreePtr log_manager); | ||
|
||
iroha::expected::Result<std::unique_ptr<BlockStorage>, std::string> create() | ||
override; | ||
|
||
private: | ||
std::shared_ptr<RocksDBContext> db_context_; | ||
std::shared_ptr<shared_model::interface::BlockJsonConverter> | ||
json_block_converter_; | ||
logger::LoggerManagerTreePtr log_manager_; | ||
}; | ||
|
||
} // namespace iroha::ametsuchi | ||
|
||
#endif // IROHA_ROCKSDB_BLOCK_STORAGE_FACTORY_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.