mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
Add more info to session stats (#973)
* Improve validator session stats * Improve validator session stats Add got_submit_at, collation_cached, validation_cached. Fix stats cleanup. * Fix setting timestamp for cached blocks * Add serialize/deserialize time, serialized size to validator session stats, fix setting is_accepted --------- Co-authored-by: SpyCheese <mikle98@yandex.ru>
This commit is contained in:
parent
d2b012c883
commit
9a543c6b28
10 changed files with 309 additions and 68 deletions
|
@ -20,6 +20,7 @@
|
|||
#include "td/utils/Random.h"
|
||||
#include "td/utils/crypto.h"
|
||||
#include "candidate-serializer.h"
|
||||
#include "td/utils/overloaded.h"
|
||||
|
||||
namespace ton {
|
||||
|
||||
|
@ -86,6 +87,7 @@ void ValidatorSessionImpl::process_blocks(std::vector<catchain::CatChainBlock *>
|
|||
|
||||
for (auto &msg : msgs) {
|
||||
VLOG(VALIDATOR_SESSION_INFO) << this << ": applying action: " << msg.get();
|
||||
stats_process_action(local_idx(), *msg);
|
||||
real_state_ = ValidatorSessionState::action(description(), real_state_, local_idx(), att, msg.get());
|
||||
}
|
||||
|
||||
|
@ -167,6 +169,7 @@ void ValidatorSessionImpl::preprocess_block(catchain::CatChainBlock *block) {
|
|||
for (auto &msg : B->actions_) {
|
||||
VLOG(VALIDATOR_SESSION_INFO) << this << "[node " << description().get_source_id(block->source()) << "][block "
|
||||
<< block->hash() << "]: applying action " << msg.get();
|
||||
stats_process_action(block->source(), *msg);
|
||||
state = ValidatorSessionState::action(description(), state, block->source(), att, msg.get());
|
||||
}
|
||||
state = ValidatorSessionState::make_all(description(), state, block->source(), att);
|
||||
|
@ -222,9 +225,11 @@ void ValidatorSessionImpl::process_broadcast(PublicKeyHash src, td::BufferSlice
|
|||
// Note: src is not necessarily equal to the sender of this message:
|
||||
// If requested using get_broadcast_p2p, src is the creator of the block, sender possibly is some other node.
|
||||
auto src_idx = description().get_source_idx(src);
|
||||
td::Timer deserialize_timer;
|
||||
auto R =
|
||||
deserialize_candidate(data, compress_block_candidates_,
|
||||
description().opts().max_block_size + description().opts().max_collated_data_size + 1024);
|
||||
double deserialize_time = deserialize_timer.elapsed();
|
||||
if (R.is_error()) {
|
||||
VLOG(VALIDATOR_SESSION_WARNING) << this << "[node " << src << "][broadcast " << sha256_bits256(data.as_slice())
|
||||
<< "]: failed to parse: " << R.move_as_error();
|
||||
|
@ -255,6 +260,18 @@ void ValidatorSessionImpl::process_broadcast(PublicKeyHash src, td::BufferSlice
|
|||
return;
|
||||
}
|
||||
|
||||
auto stat = stats_get_candidate_stat(block_round, src, block_id);
|
||||
if (stat) {
|
||||
if (stat->block_status == ValidatorSessionStats::status_none) {
|
||||
stat->block_status = ValidatorSessionStats::status_received;
|
||||
}
|
||||
if (stat->block_timestamp <= 0.0) {
|
||||
stat->block_timestamp = td::Clocks::system();
|
||||
}
|
||||
stat->deserialize_time = deserialize_time;
|
||||
stat->serialized_size = data.size();
|
||||
}
|
||||
|
||||
if ((td::int32)block_round < (td::int32)cur_round_ - MAX_PAST_ROUND_BLOCK ||
|
||||
block_round >= cur_round_ + MAX_FUTURE_ROUND_BLOCK) {
|
||||
VLOG(VALIDATOR_SESSION_NOTICE) << this << "[node " << src << "][broadcast " << block_id
|
||||
|
@ -291,7 +308,6 @@ void ValidatorSessionImpl::process_broadcast(PublicKeyHash src, td::BufferSlice
|
|||
CHECK(!pending_reject_.count(block_id));
|
||||
CHECK(!rejected_.count(block_id));
|
||||
|
||||
stats_set_candidate_status(cur_round_, src, block_id, ValidatorSessionStats::status_received);
|
||||
auto v = virtual_state_->choose_blocks_to_approve(description(), local_idx());
|
||||
for (auto &b : v) {
|
||||
if (b && SentBlock::get_block_id(b) == block_id) {
|
||||
|
@ -363,9 +379,16 @@ void ValidatorSessionImpl::process_query(PublicKeyHash src, td::BufferSlice data
|
|||
}
|
||||
|
||||
void ValidatorSessionImpl::candidate_decision_fail(td::uint32 round, ValidatorSessionCandidateId hash,
|
||||
std::string result, td::uint32 src, td::BufferSlice proof) {
|
||||
stats_set_candidate_status(round, description().get_source_id(src), hash, ValidatorSessionStats::status_rejected,
|
||||
result);
|
||||
std::string result, td::uint32 src, td::BufferSlice proof,
|
||||
double validation_time, bool validation_cached) {
|
||||
auto stat = stats_get_candidate_stat(round, description().get_source_id(src), hash);
|
||||
if (stat) {
|
||||
stat->block_status = ValidatorSessionStats::status_rejected;
|
||||
stat->comment = result;
|
||||
stat->validation_time = validation_time;
|
||||
stat->validated_at = td::Clocks::system();
|
||||
stat->validation_cached = validation_cached;
|
||||
}
|
||||
if (round != cur_round_) {
|
||||
return;
|
||||
}
|
||||
|
@ -379,9 +402,17 @@ void ValidatorSessionImpl::candidate_decision_fail(td::uint32 round, ValidatorSe
|
|||
}
|
||||
|
||||
void ValidatorSessionImpl::candidate_decision_ok(td::uint32 round, ValidatorSessionCandidateId hash, RootHash root_hash,
|
||||
FileHash file_hash, td::uint32 src, td::uint32 ok_from) {
|
||||
stats_set_candidate_status(round, description().get_source_id(src), hash, ValidatorSessionStats::status_approved,
|
||||
PSTRING() << "ts=" << ok_from);
|
||||
FileHash file_hash, td::uint32 src, td::uint32 ok_from,
|
||||
double validation_time, bool validation_cached) {
|
||||
auto stat = stats_get_candidate_stat(round, description().get_source_id(src), hash);
|
||||
if (stat) {
|
||||
stat->block_status = ValidatorSessionStats::status_approved;
|
||||
stat->comment = PSTRING() << "ts=" << ok_from;
|
||||
stat->validation_time = validation_time;
|
||||
stat->gen_utime = (double)ok_from;
|
||||
stat->validated_at = td::Clocks::system();
|
||||
stat->validation_cached = validation_cached;
|
||||
}
|
||||
if (round != cur_round_) {
|
||||
return;
|
||||
}
|
||||
|
@ -418,10 +449,8 @@ void ValidatorSessionImpl::candidate_approved_signed(td::uint32 round, Validator
|
|||
}
|
||||
|
||||
void ValidatorSessionImpl::generated_block(td::uint32 round, ValidatorSessionCandidateId root_hash,
|
||||
td::BufferSlice data, td::BufferSlice collated_data) {
|
||||
if (round != cur_round_) {
|
||||
return;
|
||||
}
|
||||
td::BufferSlice data, td::BufferSlice collated_data, double collation_time,
|
||||
bool collation_cached) {
|
||||
if (data.size() > description().opts().max_block_size ||
|
||||
collated_data.size() > description().opts().max_collated_data_size) {
|
||||
LOG(ERROR) << this << ": generated candidate is too big. Dropping. size=" << data.size() << " "
|
||||
|
@ -430,13 +459,27 @@ void ValidatorSessionImpl::generated_block(td::uint32 round, ValidatorSessionCan
|
|||
}
|
||||
auto file_hash = sha256_bits256(data.as_slice());
|
||||
auto collated_data_file_hash = sha256_bits256(collated_data.as_slice());
|
||||
auto block_id = description().candidate_id(local_idx(), root_hash, file_hash, collated_data_file_hash);
|
||||
|
||||
auto stat = stats_get_candidate_stat(round, local_id(), block_id);
|
||||
if (stat) {
|
||||
stat->block_status = ValidatorSessionStats::status_received;
|
||||
stat->collation_time = collation_time;
|
||||
stat->collated_at = td::Clocks::system();
|
||||
stat->block_timestamp = td::Clocks::system();
|
||||
stat->collation_cached = collation_cached;
|
||||
}
|
||||
if (round != cur_round_) {
|
||||
return;
|
||||
}
|
||||
td::Timer serialize_timer;
|
||||
auto b = create_tl_object<ton_api::validatorSession_candidate>(local_id().tl(), round, root_hash, std::move(data),
|
||||
std::move(collated_data));
|
||||
|
||||
auto B = serialize_candidate(b, compress_block_candidates_).move_as_ok();
|
||||
|
||||
auto block_id = description().candidate_id(local_idx(), root_hash, file_hash, collated_data_file_hash);
|
||||
if (stat) {
|
||||
stat->serialize_time = serialize_timer.elapsed();
|
||||
stat->serialized_size = B.size();
|
||||
}
|
||||
|
||||
td::actor::send_closure(catchain_, &catchain::CatChain::send_broadcast, std::move(B));
|
||||
|
||||
|
@ -496,11 +539,11 @@ void ValidatorSessionImpl::check_generate_slot() {
|
|||
td::PerfWarningTimer timer{"too long block generation", 1.0};
|
||||
|
||||
auto P = td::PromiseCreator::lambda([SelfId = actor_id(this), print_id = print_id(), timer = std::move(timer),
|
||||
round = cur_round_](td::Result<BlockCandidate> R) {
|
||||
round = cur_round_](td::Result<GeneratedCandidate> R) {
|
||||
if (R.is_ok()) {
|
||||
auto c = R.move_as_ok();
|
||||
auto c = std::move(R.ok_ref().candidate);
|
||||
td::actor::send_closure(SelfId, &ValidatorSessionImpl::generated_block, round, c.id.root_hash,
|
||||
c.data.clone(), c.collated_data.clone());
|
||||
c.data.clone(), c.collated_data.clone(), timer.elapsed(), R.ok().is_cached);
|
||||
} else {
|
||||
LOG(WARNING) << print_id << ": failed to generate block candidate: " << R.move_as_error();
|
||||
}
|
||||
|
@ -550,6 +593,16 @@ void ValidatorSessionImpl::try_approve_block(const SentBlock *block) {
|
|||
it->second->round_ = std::max<td::uint32>(it->second->round_, cur_round_);
|
||||
td::PerfWarningTimer timer{"too long block validation", 1.0};
|
||||
auto &B = it->second;
|
||||
auto stat = stats_get_candidate_stat(B->round_, PublicKeyHash{B->src_});
|
||||
if (stat) {
|
||||
// Can happen if block is cached from previous round
|
||||
if (stat->block_status == ValidatorSessionStats::status_none) {
|
||||
stat->block_status = ValidatorSessionStats::status_received;
|
||||
}
|
||||
if (stat->block_timestamp <= 0.0) {
|
||||
stat->block_timestamp = td::Clocks::system();
|
||||
}
|
||||
}
|
||||
|
||||
auto P = td::PromiseCreator::lambda([round = cur_round_, hash = block_id, root_hash = block->get_root_hash(),
|
||||
file_hash = block->get_file_hash(), timer = std::move(timer),
|
||||
|
@ -563,10 +616,10 @@ void ValidatorSessionImpl::try_approve_block(const SentBlock *block) {
|
|||
auto R = res.move_as_ok();
|
||||
if (R.is_ok()) {
|
||||
td::actor::send_closure(SelfId, &ValidatorSessionImpl::candidate_decision_ok, round, hash, root_hash,
|
||||
file_hash, src, R.ok_from());
|
||||
file_hash, src, R.ok_from(), timer.elapsed(), R.is_cached());
|
||||
} else {
|
||||
td::actor::send_closure(SelfId, &ValidatorSessionImpl::candidate_decision_fail, round, hash, R.reason(),
|
||||
src, R.proof());
|
||||
src, R.proof(), timer.elapsed(), R.is_cached());
|
||||
}
|
||||
});
|
||||
pending_approve_.insert(block_id);
|
||||
|
@ -824,22 +877,28 @@ void ValidatorSessionImpl::on_new_round(td::uint32 round) {
|
|||
cur_stats_.approve_signatures = (td::uint32)export_approve_sigs.size();
|
||||
cur_stats_.approve_signatures_weight = approve_signatures_weight;
|
||||
cur_stats_.creator = description().get_source_id(block->get_src_idx());
|
||||
auto stat = stats_get_candidate_stat(cur_round_, cur_stats_.creator);
|
||||
if (stat) {
|
||||
stat->is_accepted = true;
|
||||
}
|
||||
auto stats = cur_stats_;
|
||||
while (!stats.rounds.empty() && stats.rounds.size() + stats.first_round - 1 > cur_round_) {
|
||||
stats.rounds.pop_back();
|
||||
}
|
||||
|
||||
if (it == blocks_.end()) {
|
||||
callback_->on_block_committed(cur_round_, description().get_source_public_key(block->get_src_idx()),
|
||||
block->get_root_hash(), block->get_file_hash(), td::BufferSlice(),
|
||||
std::move(export_sigs), std::move(export_approve_sigs), std::move(cur_stats_));
|
||||
std::move(export_sigs), std::move(export_approve_sigs), std::move(stats));
|
||||
} else {
|
||||
callback_->on_block_committed(cur_round_, description().get_source_public_key(block->get_src_idx()),
|
||||
block->get_root_hash(), block->get_file_hash(), it->second->data_.clone(),
|
||||
std::move(export_sigs), std::move(export_approve_sigs), std::move(cur_stats_));
|
||||
std::move(export_sigs), std::move(export_approve_sigs), std::move(stats));
|
||||
}
|
||||
}
|
||||
cur_round_++;
|
||||
if (have_block) {
|
||||
stats_init();
|
||||
} else {
|
||||
stats_add_round();
|
||||
}
|
||||
auto it2 = blocks_.begin();
|
||||
while (it2 != blocks_.end()) {
|
||||
|
@ -997,13 +1056,35 @@ void ValidatorSessionImpl::start_up() {
|
|||
}
|
||||
|
||||
void ValidatorSessionImpl::stats_init() {
|
||||
auto old_rounds = std::move(cur_stats_.rounds);
|
||||
if (stats_inited_ && cur_stats_.first_round + old_rounds.size() > cur_round_) {
|
||||
old_rounds.erase(old_rounds.begin(), old_rounds.end() - (cur_stats_.first_round + old_rounds.size() - cur_round_));
|
||||
} else {
|
||||
old_rounds.clear();
|
||||
}
|
||||
cur_stats_ = ValidatorSessionStats();
|
||||
cur_stats_.rounds = std::move(old_rounds);
|
||||
cur_stats_.first_round = cur_round_;
|
||||
cur_stats_.session_id = unique_hash_;
|
||||
cur_stats_.total_validators = description().get_total_nodes();
|
||||
cur_stats_.total_weight = description().get_total_weight();
|
||||
cur_stats_.self = description().get_source_id(local_idx());
|
||||
stats_add_round();
|
||||
|
||||
for (auto it = stats_pending_approve_.begin(); it != stats_pending_approve_.end(); ) {
|
||||
if (it->first.first < cur_round_) {
|
||||
it = stats_pending_approve_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
for (auto it = stats_pending_sign_.begin(); it != stats_pending_sign_.end(); ) {
|
||||
if (it->first.first < cur_round_) {
|
||||
it = stats_pending_sign_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
stats_inited_ = true;
|
||||
}
|
||||
|
||||
void ValidatorSessionImpl::stats_add_round() {
|
||||
|
@ -1016,6 +1097,9 @@ void ValidatorSessionImpl::stats_add_round() {
|
|||
if (priority >= 0) {
|
||||
CHECK((size_t)priority < round.producers.size());
|
||||
round.producers[priority].id = description().get_source_id(i);
|
||||
round.producers[priority].is_ours = (local_idx() == i);
|
||||
round.producers[priority].approvers.resize(description().get_total_nodes(), false);
|
||||
round.producers[priority].signers.resize(description().get_total_nodes(), false);
|
||||
}
|
||||
}
|
||||
while (!round.producers.empty() && round.producers.back().id.is_zero()) {
|
||||
|
@ -1023,26 +1107,93 @@ void ValidatorSessionImpl::stats_add_round() {
|
|||
}
|
||||
}
|
||||
|
||||
void ValidatorSessionImpl::stats_set_candidate_status(td::uint32 round, PublicKeyHash src,
|
||||
ValidatorSessionCandidateId candidate_id, int status,
|
||||
std::string comment) {
|
||||
if (round < cur_stats_.first_round || round - cur_stats_.first_round >= cur_stats_.rounds.size()) {
|
||||
return;
|
||||
ValidatorSessionStats::Producer *ValidatorSessionImpl::stats_get_candidate_stat(
|
||||
td::uint32 round, PublicKeyHash src, ValidatorSessionCandidateId candidate_id) {
|
||||
if (round < cur_stats_.first_round || round > cur_round_ + 5) {
|
||||
return nullptr;
|
||||
}
|
||||
while (round - cur_stats_.first_round >= cur_stats_.rounds.size()) {
|
||||
stats_add_round();
|
||||
}
|
||||
auto &stats_round = cur_stats_.rounds[round - cur_stats_.first_round];
|
||||
auto it = std::find_if(stats_round.producers.begin(), stats_round.producers.end(),
|
||||
[&](const ValidatorSessionStats::Producer &p) { return p.id == src; });
|
||||
if (it == stats_round.producers.end()) {
|
||||
return;
|
||||
return nullptr;
|
||||
}
|
||||
it->candidate_id = candidate_id;
|
||||
if (it->block_status == ValidatorSessionStats::status_none) {
|
||||
it->block_timestamp = (td::uint64)td::Clocks::system();
|
||||
if (!candidate_id.is_zero()) {
|
||||
it->candidate_id = candidate_id;
|
||||
}
|
||||
it->block_status = status;
|
||||
if (!comment.empty()) {
|
||||
it->comment = std::move(comment);
|
||||
auto it2 = stats_pending_approve_.find({round, it->candidate_id});
|
||||
if (it2 != stats_pending_approve_.end()) {
|
||||
for (td::uint32 node_id : it2->second) {
|
||||
it->set_approved_by(node_id, description().get_node_weight(node_id), description().get_total_weight());
|
||||
}
|
||||
stats_pending_approve_.erase(it2);
|
||||
}
|
||||
it2 = stats_pending_sign_.find({round, it->candidate_id});
|
||||
if (it2 != stats_pending_sign_.end()) {
|
||||
for (td::uint32 node_id : it2->second) {
|
||||
it->set_signed_by(node_id, description().get_node_weight(node_id), description().get_total_weight());
|
||||
}
|
||||
stats_pending_sign_.erase(it2);
|
||||
}
|
||||
return &*it;
|
||||
}
|
||||
|
||||
ValidatorSessionStats::Producer *ValidatorSessionImpl::stats_get_candidate_stat_by_id(
|
||||
td::uint32 round, ValidatorSessionCandidateId candidate_id) {
|
||||
if (round < cur_stats_.first_round || round > cur_round_ + 5) {
|
||||
return nullptr;
|
||||
}
|
||||
while (round - cur_stats_.first_round >= cur_stats_.rounds.size()) {
|
||||
stats_add_round();
|
||||
}
|
||||
auto &stats_round = cur_stats_.rounds[round - cur_stats_.first_round];
|
||||
auto it = std::find_if(stats_round.producers.begin(), stats_round.producers.end(),
|
||||
[&](const ValidatorSessionStats::Producer &p) { return p.candidate_id == candidate_id; });
|
||||
if (it == stats_round.producers.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return &*it;
|
||||
}
|
||||
|
||||
void ValidatorSessionImpl::stats_process_action(td::uint32 node_id, ton_api::validatorSession_round_Message &action) {
|
||||
ton_api::downcast_call(action, td::overloaded(
|
||||
[&](const ton_api::validatorSession_message_submittedBlock &obj) {
|
||||
auto candidate_id = description().candidate_id(
|
||||
node_id, obj.root_hash_, obj.file_hash_, obj.collated_data_file_hash_);
|
||||
auto stat = stats_get_candidate_stat(
|
||||
obj.round_, description().get_source_id(node_id), candidate_id);
|
||||
if (stat && stat->got_submit_at <= 0.0) {
|
||||
stat->got_submit_at = td::Clocks::system();
|
||||
}
|
||||
},
|
||||
[&](const ton_api::validatorSession_message_approvedBlock &obj) {
|
||||
if (obj.candidate_ == skip_round_candidate_id()) {
|
||||
return;
|
||||
}
|
||||
auto stat = stats_get_candidate_stat_by_id(obj.round_, obj.candidate_);
|
||||
if (stat) {
|
||||
stat->set_approved_by(node_id, description().get_node_weight(node_id),
|
||||
description().get_total_weight());
|
||||
} else {
|
||||
stats_pending_approve_[{obj.round_, obj.candidate_}].push_back(node_id);
|
||||
}
|
||||
},
|
||||
[&](const ton_api::validatorSession_message_commit &obj) {
|
||||
if (obj.candidate_ == skip_round_candidate_id()) {
|
||||
return;
|
||||
}
|
||||
auto stat = stats_get_candidate_stat_by_id(obj.round_, obj.candidate_);
|
||||
if (stat) {
|
||||
stat->set_signed_by(node_id, description().get_node_weight(node_id),
|
||||
description().get_total_weight());
|
||||
} else {
|
||||
stats_pending_sign_[{obj.round_, obj.candidate_}].push_back(node_id);
|
||||
}
|
||||
},
|
||||
[](const auto &) {}));
|
||||
}
|
||||
|
||||
td::actor::ActorOwn<ValidatorSession> ValidatorSession::create(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue