Skip to content

Commit 8364a24

Browse files
authored
Improve overlay stats (#1242)
1 parent f94d1be commit 8364a24

File tree

8 files changed

+179
-104
lines changed

8 files changed

+179
-104
lines changed

overlay/overlay-manager.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void OverlayManager::receive_message(adnl::AdnlNodeIdShort src, adnl::AdnlNodeId
188188
return;
189189
}
190190

191-
td::actor::send_closure(it2->second.overlay, &Overlay::update_throughput_in_ctr, src, (td::uint32)data.size(), false);
191+
td::actor::send_closure(it2->second.overlay, &Overlay::update_throughput_in_ctr, src, data.size(), false, false);
192192
td::actor::send_closure(it2->second.overlay, &Overlay::receive_message, src, std::move(extra), std::move(data));
193193
}
194194

@@ -225,7 +225,14 @@ void OverlayManager::receive_query(adnl::AdnlNodeIdShort src, adnl::AdnlNodeIdSh
225225
return;
226226
}
227227

228-
td::actor::send_closure(it2->second.overlay, &Overlay::update_throughput_in_ctr, src, (td::uint32)data.size(), true);
228+
td::actor::send_closure(it2->second.overlay, &Overlay::update_throughput_in_ctr, src, data.size(), true, false);
229+
promise = [overlay = it2->second.overlay.get(), promise = std::move(promise),
230+
src](td::Result<td::BufferSlice> R) mutable {
231+
if (R.is_ok()) {
232+
td::actor::send_closure(overlay, &Overlay::update_throughput_out_ctr, src, R.ok().size(), false, true);
233+
}
234+
promise.set_result(std::move(R));
235+
};
229236
td::actor::send_closure(it2->second.overlay, &Overlay::receive_query, src, std::move(extra), std::move(data),
230237
std::move(promise));
231238
}
@@ -243,8 +250,14 @@ void OverlayManager::send_query_via(adnl::AdnlNodeIdShort dst, adnl::AdnlNodeIdS
243250
if (it != overlays_.end()) {
244251
auto it2 = it->second.find(overlay_id);
245252
if (it2 != it->second.end()) {
246-
td::actor::send_closure(it2->second.overlay, &Overlay::update_throughput_out_ctr, dst, (td::uint32)query.size(),
247-
true);
253+
td::actor::send_closure(it2->second.overlay, &Overlay::update_throughput_out_ctr, dst, query.size(), true, false);
254+
promise = [overlay = it2->second.overlay.get(), promise = std::move(promise),
255+
dst](td::Result<td::BufferSlice> R) mutable {
256+
if (R.is_ok()) {
257+
td::actor::send_closure(overlay, &Overlay::update_throughput_in_ctr, dst, R.ok().size(), false, true);
258+
}
259+
promise.set_result(std::move(R));
260+
};
248261
if (!it2->second.member_certificate.empty()) {
249262
extra->flags_ |= 1;
250263
extra->certificate_ = it2->second.member_certificate.tl();
@@ -273,7 +286,7 @@ void OverlayManager::send_message_via(adnl::AdnlNodeIdShort dst, adnl::AdnlNodeI
273286
if (it != overlays_.end()) {
274287
auto it2 = it->second.find(overlay_id);
275288
if (it2 != it->second.end()) {
276-
td::actor::send_closure(it2->second.overlay, &Overlay::update_throughput_out_ctr, dst, (td::uint32)object.size(),
289+
td::actor::send_closure(it2->second.overlay, &Overlay::update_throughput_out_ctr, dst, object.size(), false,
277290
false);
278291
if (!it2->second.member_certificate.empty()) {
279292
// do not send certificate here, we hope that all our neighbours already know of out certificate

overlay/overlay-peers.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -574,28 +574,40 @@ void OverlayImpl::update_peer_err_ctr(adnl::AdnlNodeIdShort peer_id, bool is_fec
574574
}
575575
}
576576

577-
void OverlayImpl::update_throughput_out_ctr(adnl::AdnlNodeIdShort peer_id, td::uint32 msg_size, bool is_query) {
577+
void OverlayImpl::update_throughput_out_ctr(adnl::AdnlNodeIdShort peer_id, td::uint64 msg_size, bool is_query,
578+
bool is_response) {
578579
auto out_peer = peer_list_.peers_.get(peer_id);
579580
if (out_peer) {
580-
out_peer->throughput_out_bytes_ctr += msg_size;
581-
out_peer->throughput_out_packets_ctr++;
582-
581+
out_peer->traffic_ctr.add_packet(msg_size, false);
582+
if (is_response) {
583+
out_peer->traffic_responses_ctr.add_packet(msg_size, false);
584+
}
583585
if (is_query) {
584586
out_peer->last_out_query_at = td::Timestamp::now();
585587
}
586588
}
589+
total_traffic_ctr.add_packet(msg_size, false);
590+
if (is_response) {
591+
total_traffic_responses_ctr.add_packet(msg_size, false);
592+
}
587593
}
588594

589-
void OverlayImpl::update_throughput_in_ctr(adnl::AdnlNodeIdShort peer_id, td::uint32 msg_size, bool is_query) {
595+
void OverlayImpl::update_throughput_in_ctr(adnl::AdnlNodeIdShort peer_id, td::uint64 msg_size, bool is_query,
596+
bool is_response) {
590597
auto in_peer = peer_list_.peers_.get(peer_id);
591598
if (in_peer) {
592-
in_peer->throughput_in_bytes_ctr += msg_size;
593-
in_peer->throughput_in_packets_ctr++;
594-
599+
in_peer->traffic_ctr.add_packet(msg_size, true);
600+
if (is_response) {
601+
in_peer->traffic_responses_ctr.add_packet(msg_size, true);
602+
}
595603
if (is_query) {
596604
in_peer->last_in_query_at = td::Timestamp::now();
597605
}
598606
}
607+
total_traffic_ctr.add_packet(msg_size, true);
608+
if (is_response) {
609+
total_traffic_responses_ctr.add_packet(msg_size, true);
610+
}
599611
}
600612

601613
void OverlayImpl::update_peer_ip_str(adnl::AdnlNodeIdShort peer_id, td::string ip_str) {

overlay/overlay.cpp

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,32 @@ namespace overlay {
3838

3939
const OverlayMemberCertificate OverlayNode::empty_certificate_{};
4040

41+
static std::string overlay_actor_name(const OverlayIdFull &overlay_id) {
42+
return PSTRING() << "overlay." << overlay_id.compute_short_id().bits256_value().to_hex().substr(0, 4);
43+
}
44+
4145
td::actor::ActorOwn<Overlay> Overlay::create_public(td::actor::ActorId<keyring::Keyring> keyring,
4246
td::actor::ActorId<adnl::Adnl> adnl,
4347
td::actor::ActorId<OverlayManager> manager,
4448
td::actor::ActorId<dht::Dht> dht_node,
4549
adnl::AdnlNodeIdShort local_id, OverlayIdFull overlay_id,
4650
std::unique_ptr<Overlays::Callback> callback,
4751
OverlayPrivacyRules rules, td::string scope, OverlayOptions opts) {
48-
auto R = td::actor::create_actor<OverlayImpl>(
49-
"overlay", keyring, adnl, manager, dht_node, local_id, std::move(overlay_id), OverlayType::Public,
50-
std::vector<adnl::AdnlNodeIdShort>(), std::vector<PublicKeyHash>(), OverlayMemberCertificate{}, std::move(callback),
51-
std::move(rules), std::move(scope), std::move(opts));
52-
return td::actor::ActorOwn<Overlay>(std::move(R));
52+
return td::actor::create_actor<OverlayImpl>(
53+
overlay_actor_name(overlay_id), keyring, adnl, manager, dht_node, local_id, std::move(overlay_id),
54+
OverlayType::Public, std::vector<adnl::AdnlNodeIdShort>(), std::vector<PublicKeyHash>(),
55+
OverlayMemberCertificate{}, std::move(callback), std::move(rules), std::move(scope), std::move(opts));
5356
}
5457

5558
td::actor::ActorOwn<Overlay> Overlay::create_private(
5659
td::actor::ActorId<keyring::Keyring> keyring, td::actor::ActorId<adnl::Adnl> adnl,
5760
td::actor::ActorId<OverlayManager> manager, td::actor::ActorId<dht::Dht> dht_node, adnl::AdnlNodeIdShort local_id,
5861
OverlayIdFull overlay_id, std::vector<adnl::AdnlNodeIdShort> nodes, std::unique_ptr<Overlays::Callback> callback,
5962
OverlayPrivacyRules rules, std::string scope, OverlayOptions opts) {
60-
auto R = td::actor::create_actor<OverlayImpl>("overlay", keyring, adnl, manager, dht_node, local_id,
61-
std::move(overlay_id), OverlayType::FixedMemberList, std::move(nodes),
62-
std::vector<PublicKeyHash>(), OverlayMemberCertificate{},
63-
std::move(callback), std::move(rules), std::move(scope));
64-
return td::actor::ActorOwn<Overlay>(std::move(R));
63+
return td::actor::create_actor<OverlayImpl>(
64+
overlay_actor_name(overlay_id), keyring, adnl, manager, dht_node, local_id, std::move(overlay_id),
65+
OverlayType::FixedMemberList, std::move(nodes), std::vector<PublicKeyHash>(), OverlayMemberCertificate{},
66+
std::move(callback), std::move(rules), std::move(scope));
6567
}
6668

6769
td::actor::ActorOwn<Overlay> Overlay::create_semiprivate(
@@ -70,11 +72,10 @@ td::actor::ActorOwn<Overlay> Overlay::create_semiprivate(
7072
OverlayIdFull overlay_id, std::vector<adnl::AdnlNodeIdShort> nodes, std::vector<PublicKeyHash> root_public_keys,
7173
OverlayMemberCertificate cert, std::unique_ptr<Overlays::Callback> callback, OverlayPrivacyRules rules,
7274
std::string scope, OverlayOptions opts) {
73-
auto R = td::actor::create_actor<OverlayImpl>(
74-
"overlay", keyring, adnl, manager, dht_node, local_id, std::move(overlay_id), OverlayType::CertificatedMembers,
75-
std::move(nodes), std::move(root_public_keys), std::move(cert), std::move(callback), std::move(rules),
76-
std::move(scope), std::move(opts));
77-
return td::actor::ActorOwn<Overlay>(std::move(R));
75+
return td::actor::create_actor<OverlayImpl>(overlay_actor_name(overlay_id), keyring, adnl, manager, dht_node,
76+
local_id, std::move(overlay_id), OverlayType::CertificatedMembers,
77+
std::move(nodes), std::move(root_public_keys), std::move(cert),
78+
std::move(callback), std::move(rules), std::move(scope), std::move(opts));
7879
}
7980

8081
OverlayImpl::OverlayImpl(td::actor::ActorId<keyring::Keyring> keyring, td::actor::ActorId<adnl::Adnl> adnl,
@@ -281,17 +282,12 @@ void OverlayImpl::alarm() {
281282

282283
auto SelfId = actor_id(this);
283284
iterate_all_peers([&](const adnl::AdnlNodeIdShort &key, OverlayPeer &peer) {
284-
peer.throughput_out_bytes = static_cast<td::uint32>(peer.throughput_out_bytes_ctr / t_elapsed);
285-
peer.throughput_in_bytes = static_cast<td::uint32>(peer.throughput_in_bytes_ctr / t_elapsed);
286-
287-
peer.throughput_out_packets = static_cast<td::uint32>(peer.throughput_out_packets_ctr / t_elapsed);
288-
peer.throughput_in_packets = static_cast<td::uint32>(peer.throughput_in_packets_ctr / t_elapsed);
289-
290-
peer.throughput_out_bytes_ctr = 0;
291-
peer.throughput_in_bytes_ctr = 0;
292-
293-
peer.throughput_out_packets_ctr = 0;
294-
peer.throughput_in_packets_ctr = 0;
285+
peer.traffic = peer.traffic_ctr;
286+
peer.traffic.normalize(t_elapsed);
287+
peer.traffic_ctr = {};
288+
peer.traffic_responses = peer.traffic_responses_ctr;
289+
peer.traffic_responses.normalize(t_elapsed);
290+
peer.traffic_responses_ctr = {};
295291

296292
auto P = td::PromiseCreator::lambda([SelfId, peer_id = key](td::Result<td::string> result) {
297293
result.ensure();
@@ -300,6 +296,12 @@ void OverlayImpl::alarm() {
300296

301297
td::actor::send_closure(adnl_, &adnl::AdnlSenderInterface::get_conn_ip_str, local_id_, key, std::move(P));
302298
});
299+
total_traffic = total_traffic_ctr;
300+
total_traffic.normalize(t_elapsed);
301+
total_traffic_ctr = {};
302+
total_traffic_responses = total_traffic_responses_ctr;
303+
total_traffic_responses.normalize(t_elapsed);
304+
total_traffic_responses_ctr = {};
303305

304306
update_throughput_at_ = td::Timestamp::in(50.0);
305307
last_throughput_update_ = td::Timestamp::now();
@@ -691,12 +693,8 @@ void OverlayImpl::get_stats(td::Promise<tl_object_ptr<ton_api::engine_validator_
691693
iterate_all_peers([&](const adnl::AdnlNodeIdShort &key, const OverlayPeer &peer) {
692694
auto node_obj = create_tl_object<ton_api::engine_validator_overlayStatsNode>();
693695
node_obj->adnl_id_ = key.bits256_value();
694-
node_obj->t_out_bytes_ = peer.throughput_out_bytes;
695-
node_obj->t_in_bytes_ = peer.throughput_in_bytes;
696-
697-
node_obj->t_out_pckts_ = peer.throughput_out_packets;
698-
node_obj->t_in_pckts_ = peer.throughput_in_packets;
699-
696+
node_obj->traffic_ = peer.traffic.tl();
697+
node_obj->traffic_responses_ = peer.traffic_responses.tl();
700698
node_obj->ip_addr_ = peer.ip_addr_str;
701699

702700
node_obj->last_in_query_ = static_cast<td::uint32>(peer.last_in_query_at.at_unix());
@@ -712,6 +710,8 @@ void OverlayImpl::get_stats(td::Promise<tl_object_ptr<ton_api::engine_validator_
712710
res->nodes_.push_back(std::move(node_obj));
713711
});
714712

713+
res->total_traffic_ = total_traffic.tl();
714+
res->total_traffic_responses_ = total_traffic_responses.tl();
715715
res->stats_.push_back(
716716
create_tl_object<ton_api::engine_validator_oneStat>("neighbours_cnt", PSTRING() << neighbours_cnt()));
717717

@@ -732,6 +732,27 @@ bool OverlayImpl::has_valid_broadcast_certificate(const PublicKeyHash &source, s
732732
BroadcastCheckResult::Forbidden;
733733
}
734734

735+
void TrafficStats::add_packet(td::uint64 size, bool in) {
736+
if (in) {
737+
++in_packets;
738+
in_bytes += size;
739+
} else {
740+
++out_packets;
741+
out_bytes += size;
742+
}
743+
}
744+
745+
void TrafficStats::normalize(double elapsed) {
746+
out_bytes = static_cast<td::uint64>(out_bytes / elapsed);
747+
in_bytes = static_cast<td::uint64>(in_bytes / elapsed);
748+
out_packets = static_cast<td::uint32>(out_packets / elapsed);
749+
in_packets = static_cast<td::uint32>(in_packets / elapsed);
750+
}
751+
752+
tl_object_ptr<ton_api::engine_validator_overlayStatsTraffic> TrafficStats::tl() const {
753+
return create_tl_object<ton_api::engine_validator_overlayStatsTraffic>(out_bytes, in_bytes, out_packets, in_packets);
754+
}
755+
735756
} // namespace overlay
736757

737758
} // namespace ton

overlay/overlay.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ class Overlay : public td::actor::Actor {
7272
virtual void receive_nodes_from_db(tl_object_ptr<ton_api::overlay_nodes> nodes) = 0;
7373
virtual void receive_nodes_from_db_v2(tl_object_ptr<ton_api::overlay_nodesV2> nodes) = 0;
7474
virtual void get_stats(td::Promise<tl_object_ptr<ton_api::engine_validator_overlayStats>> promise) = 0;
75-
virtual void update_throughput_out_ctr(adnl::AdnlNodeIdShort peer_id, td::uint32 msg_size, bool is_query) = 0;
76-
virtual void update_throughput_in_ctr(adnl::AdnlNodeIdShort peer_id, td::uint32 msg_size, bool is_query) = 0;
75+
virtual void update_throughput_out_ctr(adnl::AdnlNodeIdShort peer_id, td::uint64 msg_size, bool is_query,
76+
bool is_response) = 0;
77+
virtual void update_throughput_in_ctr(adnl::AdnlNodeIdShort peer_id, td::uint64 msg_size, bool is_query,
78+
bool is_response) = 0;
7779
virtual void update_peer_ip_str(adnl::AdnlNodeIdShort peer_id, td::string ip_str) = 0;
7880
virtual void update_member_certificate(OverlayMemberCertificate cert) = 0;
7981
virtual void update_root_member_list(std::vector<adnl::AdnlNodeIdShort> nodes,

overlay/overlay.hpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ namespace overlay {
5959

6060
class OverlayImpl;
6161

62+
struct TrafficStats {
63+
td::uint64 out_bytes = 0;
64+
td::uint64 in_bytes = 0;
65+
66+
td::uint32 out_packets = 0;
67+
td::uint32 in_packets = 0;
68+
69+
void add_packet(td::uint64 size, bool in);
70+
void normalize(double elapsed);
71+
tl_object_ptr<ton_api::engine_validator_overlayStatsTraffic> tl() const;
72+
};
73+
6274
class OverlayPeer {
6375
public:
6476
adnl::AdnlNodeIdShort get_id() const {
@@ -126,17 +138,8 @@ class OverlayPeer {
126138
return node_.has_full_id();
127139
}
128140

129-
td::uint32 throughput_out_bytes = 0;
130-
td::uint32 throughput_in_bytes = 0;
131-
132-
td::uint32 throughput_out_packets = 0;
133-
td::uint32 throughput_in_packets = 0;
134-
135-
td::uint32 throughput_out_bytes_ctr = 0;
136-
td::uint32 throughput_in_bytes_ctr = 0;
137-
138-
td::uint32 throughput_out_packets_ctr = 0;
139-
td::uint32 throughput_in_packets_ctr = 0;
141+
TrafficStats traffic, traffic_ctr;
142+
TrafficStats traffic_responses, traffic_responses_ctr;
140143

141144
td::uint32 broadcast_errors = 0;
142145
td::uint32 fec_broadcast_errors = 0;
@@ -269,9 +272,11 @@ class OverlayImpl : public Overlay {
269272

270273
void get_stats(td::Promise<tl_object_ptr<ton_api::engine_validator_overlayStats>> promise) override;
271274

272-
void update_throughput_out_ctr(adnl::AdnlNodeIdShort peer_id, td::uint32 msg_size, bool is_query) override;
275+
void update_throughput_out_ctr(adnl::AdnlNodeIdShort peer_id, td::uint64 msg_size, bool is_query,
276+
bool is_response) override;
273277

274-
void update_throughput_in_ctr(adnl::AdnlNodeIdShort peer_id, td::uint32 msg_size, bool is_query) override;
278+
void update_throughput_in_ctr(adnl::AdnlNodeIdShort peer_id, td::uint64 msg_size, bool is_query,
279+
bool is_response) override;
275280

276281
void update_peer_ip_str(adnl::AdnlNodeIdShort peer_id, td::string ip_str) override;
277282

@@ -457,6 +462,8 @@ class OverlayImpl : public Overlay {
457462
td::Timestamp local_cert_is_valid_until_;
458463
td::uint32 local_member_flags_{0};
459464
} peer_list_;
465+
TrafficStats total_traffic, total_traffic_ctr;
466+
TrafficStats total_traffic_responses, total_traffic_responses_ctr;
460467

461468
OverlayOptions opts_;
462469
};

tl/generate/scheme/ton_api.tl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,10 +683,16 @@ engine.validator.proposalVote perm_key:int256 to_send:bytes = engine.validator.P
683683
engine.validator.dhtServerStatus id:int256 status:int = engine.validator.DhtServerStatus;
684684
engine.validator.dhtServersStatus servers:(vector engine.validator.dhtServerStatus) = engine.validator.DhtServersStatus;
685685

686+
engine.validator.overlayStatsTraffic t_out_bytes:long t_in_bytes:long t_out_pckts:int t_in_pckts:int = engine.validator.OverlayStatsTraffic;
687+
686688
engine.validator.overlayStatsNode adnl_id:int256 ip_addr:string is_neighbour:Bool is_alive:Bool node_flags:int
687-
bdcst_errors:int fec_bdcst_errors:int last_in_query:int last_out_query:int t_out_bytes:int t_in_bytes:int t_out_pckts:int t_in_pckts:int = engine.validator.OverlayStatsNode;
689+
bdcst_errors:int fec_bdcst_errors:int last_in_query:int last_out_query:int
690+
traffic:engine.validator.overlayStatsTraffic traffic_responses:engine.validator.overlayStatsTraffic = engine.validator.OverlayStatsNode;
688691

689-
engine.validator.overlayStats overlay_id:int256 overlay_id_full:PublicKey adnl_id:int256 scope:string nodes:(vector engine.validator.overlayStatsNode) stats:(vector engine.validator.oneStat) extra:string = engine.validator.OverlayStats;
692+
engine.validator.overlayStats overlay_id:int256 overlay_id_full:PublicKey adnl_id:int256 scope:string
693+
nodes:(vector engine.validator.overlayStatsNode) stats:(vector engine.validator.oneStat)
694+
total_traffic:engine.validator.overlayStatsTraffic total_traffic_responses:engine.validator.overlayStatsTraffic
695+
extra:string = engine.validator.OverlayStats;
690696
engine.validator.overlaysStats overlays:(vector engine.validator.overlayStats) = engine.validator.OverlaysStats;
691697

692698
engine.validator.onePerfTimerStat time:int min:double avg:double max:double = engine.validator.OnePerfTimerStat;

tl/generate/scheme/ton_api.tlo

312 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)