Skip to content

Commit 0fc3c5d

Browse files
committed
Feature/rdb metrics (#1692)
* rocksdb metrics Signed-off-by: iceseer <[email protected]> Signed-off-by: Alexander Lednev <[email protected]>
1 parent 24ad890 commit 0fc3c5d

File tree

11 files changed

+157
-3
lines changed

11 files changed

+157
-3
lines changed

irohad/ametsuchi/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,5 @@ target_link_libraries(ametsuchi_rocksdb
258258
shared_model_interfaces_factories
259259
rocksdb_indexer
260260
shared_model_interfaces
261+
async_subscription
261262
)

irohad/ametsuchi/impl/rocksdb_command_executor.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "interfaces/commands/set_setting_value.hpp"
3535
#include "interfaces/commands/subtract_asset_quantity.hpp"
3636
#include "interfaces/commands/transfer_asset.hpp"
37+
#include "main/rdb_status.hpp"
38+
#include "main/subscription.hpp"
3739

3840
using namespace iroha;
3941
using namespace iroha::ametsuchi;
@@ -53,6 +55,23 @@ RocksDbCommandExecutor::RocksDbCommandExecutor(
5355
vm_caller_{vm_caller},
5456
db_transaction_(db_context_) {
5557
assert(db_context_);
58+
59+
getSubscription()->dispatcher()->repeat(
60+
SubscriptionEngineHandlers::kMetrics,
61+
std::chrono::seconds(5ull), /// repeat task execution period
62+
[wdb_context_(utils::make_weak(db_context_))]() {
63+
if (auto db_context = wdb_context_.lock()) {
64+
RocksDbCommon common(db_context);
65+
getSubscription()->notify(
66+
EventTypes::kOnRdbStats,
67+
RocksDbStatus{common.propGetBlockCacheCapacity(),
68+
common.propGetBlockCacheUsage(),
69+
common.propGetCurSzAllMemTables(),
70+
common.propGetNumSnapshots(),
71+
common.propGetTotalSSTFilesSize()});
72+
}
73+
},
74+
[]() { return true; });
5675
}
5776

5877
RocksDbCommandExecutor::~RocksDbCommandExecutor() = default;

irohad/ametsuchi/impl/rocksdb_common.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,24 @@ namespace iroha::ametsuchi {
534534
}
535535
}
536536

537+
std::optional<uint64_t> getPropUInt64(const rocksdb::Slice &property) {
538+
if (transaction_db_) {
539+
uint64_t value;
540+
transaction_db_->GetIntProperty(property, &value);
541+
return value;
542+
}
543+
return std::nullopt;
544+
}
545+
546+
std::optional<std::string> getPropStr(const rocksdb::Slice &property) {
547+
if (transaction_db_) {
548+
std::string value;
549+
transaction_db_->GetProperty(property, &value);
550+
return value;
551+
}
552+
return std::nullopt;
553+
}
554+
537555
private:
538556
std::unique_ptr<rocksdb::OptimisticTransactionDB> transaction_db_;
539557
std::optional<std::string> db_name_;
@@ -655,6 +673,29 @@ namespace iroha::ametsuchi {
655673
tx_context_->db_port->printStatus(log);
656674
}
657675

676+
auto propGetBlockCacheUsage() {
677+
return tx_context_->db_port->getPropUInt64("rocksdb.block-cache-usage");
678+
}
679+
680+
auto propGetCurSzAllMemTables() {
681+
return tx_context_->db_port->getPropUInt64(
682+
"rocksdb.cur-size-all-mem-tables");
683+
}
684+
685+
auto propGetNumSnapshots() {
686+
return tx_context_->db_port->getPropUInt64("rocksdb.num-snapshots");
687+
}
688+
689+
auto propGetTotalSSTFilesSize() {
690+
return tx_context_->db_port->getPropUInt64(
691+
"rocksdb.total-sst-files-size");
692+
}
693+
694+
auto propGetBlockCacheCapacity() {
695+
return tx_context_->db_port->getPropUInt64(
696+
"rocksdb.block-cache-capacity");
697+
}
698+
658699
/// Makes commit to DB
659700
auto commit() {
660701
rocksdb::Status status;

irohad/iroha_migrate/iroha_migrate.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "validators/default_validator.hpp"
4646
#include "validators/protobuf/proto_block_validator.hpp"
4747
#include "validators/protobuf/proto_query_validator.hpp"
48+
#include "main/subscription.hpp"
4849

4950
#define STR(y) STRH(y)
5051
#define STRH(x) #x
@@ -280,7 +281,13 @@ expected::Result<void, std::string> restoreWsv() {
280281
return {};
281282
}
282283

284+
std::shared_ptr<iroha::Subscription> subscription_manager;
283285
int main(int argc, char *argv[]) try {
286+
subscription_manager = iroha::getSubscription();
287+
std::unique_ptr<int, void(*)(int*)> keeper((int*)0x01, [](auto *){
288+
subscription_manager->dispose();
289+
});
290+
284291
gflags::SetVersionString("1.2");
285292
gflags::ParseCommandLineFlags(&argc, &argv, true);
286293
gflags::SetUsageMessage(

irohad/main/rdb_status.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef IROHA_RDB_STATUS_HPP
7+
#define IROHA_RDB_STATUS_HPP
8+
9+
#include <optional>
10+
11+
namespace iroha {
12+
13+
struct RocksDbStatus {
14+
std::optional<uint64_t> block_cache_capacity;
15+
std::optional<uint64_t> block_cache_usage;
16+
std::optional<uint64_t> all_mem_tables_sz;
17+
std::optional<uint64_t> num_snapshots;
18+
std::optional<uint64_t> sst_files_size;
19+
};
20+
21+
} // namespace iroha
22+
23+
#endif // IROHA_RDB_STATUS_HPP

irohad/main/subscription_fwd.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ namespace iroha {
4343
kOnConsensusGateEvent,
4444
kSendBatchComplete,
4545

46+
// RDB
47+
kOnRdbStats,
48+
4649
// Node status
4750
kOnIrohaStatus,
4851

irohad/maintenance/metrics.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,58 @@ Metrics::Metrics(std::string const &listen_addr,
186186
number_of_pending_mst_transactions.Set(std::get<1>(mstmetr));
187187
});
188188

189+
////////////////////////////////////////////////////////////
190+
191+
auto &param_block_cache_cap = BuildGauge()
192+
.Name("rdb_block_cache_capacity")
193+
.Help("RocksDB block cache capacity")
194+
.Register(*registry_)
195+
.Add({});
196+
197+
auto &param_block_cache_usage = BuildGauge()
198+
.Name("rdb_block_cache_usage")
199+
.Help("RocksDB block cache usage")
200+
.Register(*registry_)
201+
.Add({});
202+
203+
auto &param_all_mem_tables_sz = BuildGauge()
204+
.Name("rdb_all_mem_tables_sz")
205+
.Help("RocksDB all mem tables size")
206+
.Register(*registry_)
207+
.Add({});
208+
209+
auto &param_num_snapshots = BuildGauge()
210+
.Name("rdb_num_snapshots")
211+
.Help("RocksDB number of snapshots")
212+
.Register(*registry_)
213+
.Add({});
214+
215+
auto &param_sst_files_size = BuildGauge()
216+
.Name("rdb_sst_files_size")
217+
.Help("RocksDB SST files size")
218+
.Register(*registry_)
219+
.Add({});
220+
221+
rdb_subscriber_ =
222+
SubscriberCreator<bool, iroha::RocksDbStatus>::template create<
223+
EventTypes::kOnRdbStats>(
224+
SubscriptionEngineHandlers::kMetrics,
225+
[&](auto &, iroha::RocksDbStatus status) {
226+
if (status.block_cache_capacity)
227+
param_block_cache_cap.Set(*status.block_cache_capacity);
228+
229+
if (status.block_cache_usage)
230+
param_block_cache_usage.Set(*status.block_cache_usage);
231+
232+
if (status.all_mem_tables_sz)
233+
param_all_mem_tables_sz.Set(*status.all_mem_tables_sz);
234+
235+
if (status.num_snapshots)
236+
param_num_snapshots.Set(*status.num_snapshots);
237+
238+
if (status.sst_files_size)
239+
param_sst_files_size.Set(*status.sst_files_size);
240+
});
189241
///////////////////////////////
190242

191243
auto calc_uptime_ms = [uptime_start_timepoint_(uptime_start_timepoint_)] {

irohad/maintenance/metrics.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "interfaces/common_objects/types.hpp"
2121
#include "interfaces/iroha_internal/block.hpp"
2222
#include "logger/logger_fwd.hpp"
23+
#include "main/rdb_status.hpp"
2324
#include "main/iroha_status.hpp"
2425
#include "main/subscription.hpp"
2526
#include "network/ordering_gate_common.hpp"
@@ -32,13 +33,15 @@ class Metrics : public std::enable_shared_from_this<Metrics> {
3233
using BlockSubscriber = iroha::BaseSubscriber<bool, BlockPtr>;
3334
using MstMetrics = std::tuple<size_t, size_t>;
3435
using MstSubscriber = iroha::BaseSubscriber<bool, MstMetrics>;
36+
using RdbSubscriber = iroha::BaseSubscriber<bool, iroha::RocksDbStatus>;
3537

3638
std::string listen_addr_port_;
3739
std::shared_ptr<prometheus::Exposer> exposer_;
3840
std::shared_ptr<prometheus::Registry> registry_;
3941
std::shared_ptr<iroha::ametsuchi::Storage> storage_;
4042
std::shared_ptr<BlockSubscriber> block_subscriber_;
4143
std::shared_ptr<MstSubscriber> mst_subscriber_;
44+
std::shared_ptr<RdbSubscriber> rdb_subscriber_;
4245
logger::LoggerPtr logger_;
4346
std::chrono::steady_clock::time_point uptime_start_timepoint_;
4447
std::thread uptime_thread_;

irohad/subscription/sync_dispatcher_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace iroha::subscription {
4040
std::chrono::microseconds timeout,
4141
typename Parent::Task &&task,
4242
typename Parent::Predicate &&pred) override {
43-
while (!pred || pred()) task();
43+
if (!pred || pred()) task();
4444
}
4545

4646
std::optional<Tid> bind(std::shared_ptr<IScheduler> scheduler) override {

test/integration/executor/executor_fixture_param.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
using namespace executor_testing;
1111

1212
ExecutorTestParam::ExecutorTestParam()
13-
: vm_caller_(std::make_unique<iroha::ametsuchi::MockVmCaller>()) {}
13+
: vm_caller_(std::make_unique<iroha::ametsuchi::MockVmCaller>()),
14+
subscription_manager_(iroha::getSubscription()) {}
1415

15-
ExecutorTestParam::~ExecutorTestParam() = default;
16+
ExecutorTestParam::~ExecutorTestParam() {
17+
subscription_manager_->dispose();
18+
}

0 commit comments

Comments
 (0)