From b75f0ea1efbc6a025f356f9db276b3cd44f4ec36 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 3 Aug 2022 20:15:52 -0700 Subject: [PATCH] ircd::m: Move multi-key fetch from vm:: to keys::; minor reorg. --- include/ircd/m/keys.h | 2 + include/ircd/m/vm/eval.h | 7 ++- matrix/keys.cc | 39 +++++++++++++++ matrix/vm_eval.cc | 105 ++++++++++++--------------------------- matrix/vm_execute.cc | 2 +- 5 files changed, 77 insertions(+), 78 deletions(-) diff --git a/include/ircd/m/keys.h b/include/ircd/m/keys.h index 663375bc0..9986772f0 100644 --- a/include/ircd/m/keys.h +++ b/include/ircd/m/keys.h @@ -55,6 +55,7 @@ struct ircd::m::keys { struct cache; + using pdus = vector_view; using queries = vector_view; // using closure = std::function; using closure_bool = std::function; @@ -64,6 +65,7 @@ struct ircd::m::keys static bool get(const string_view &server_name, const string_view &key_id, const closure &); static bool query(const string_view &query_server, const queries &, const closure_bool &); static size_t fetch(const queries &); + static size_t fetch(const pdus &); using super_type::tuple; using super_type::operator=; diff --git a/include/ircd/m/vm/eval.h b/include/ircd/m/vm/eval.h index a919ffd24..e5add65dc 100644 --- a/include/ircd/m/vm/eval.h +++ b/include/ircd/m/vm/eval.h @@ -15,15 +15,14 @@ namespace ircd::m::vm { struct eval; + string_view loghead(const mutable_buffer &, const eval &); + string_view loghead(const eval &); // single tls buffer + const event *find_pdu(const eval &, const event::id &) noexcept; eval *find_parent(const eval &, const ctx::ctx & = ctx::cur()) noexcept; eval *find_root(const eval &, const ctx::ctx & = ctx::cur()) noexcept; - string_view loghead(const mutable_buffer &, const eval &); - string_view loghead(const eval &); // single tls buffer - size_t prefetch_refs(const eval &); - size_t fetch_keys(const eval &); } /// Event Evaluation Device diff --git a/matrix/keys.cc b/matrix/keys.cc index e69552632..06bc6fd25 100644 --- a/matrix/keys.cc +++ b/matrix/keys.cc @@ -284,6 +284,45 @@ ircd::m::keys_get_timeout { "default", 20000L } }; +size_t +ircd::m::keys::fetch(const pdus &pdus) +{ + using fed::key::server_key; + + std::vector q; + for(const auto &event : pdus) + for(const auto &[server_name, signatures] : json::get<"signatures"_>(event)) + for(const auto &[key_id, signature] : json::object(signatures)) + { + const server_key query + { + json::get<"origin"_>(event), key_id + }; + + // Check if we're already making a query. + if(std::binary_search(begin(q), end(q), query)) + continue; + + // Check if we already have the key. + if(cache::has(json::get<"origin"_>(event), key_id)) + continue; + + // If there's a cached error on the host we can skip here. + if(fed::errant(json::get<"origin"_>(event))) + continue; + + q.emplace_back(query); + std::sort(begin(q), end(q)); + } + + const size_t fetched + { + fetch(queries(q)) + }; + + return fetched; +} + size_t ircd::m::keys::fetch(const queries &queries) { diff --git a/matrix/vm_eval.cc b/matrix/vm_eval.cc index 484b9dd98..432dda516 100644 --- a/matrix/vm_eval.cc +++ b/matrix/vm_eval.cc @@ -37,47 +37,6 @@ ircd::m::vm::eval::executing; decltype(ircd::m::vm::eval::injecting) ircd::m::vm::eval::injecting; -size_t -ircd::m::vm::fetch_keys(const eval &eval) -{ - using m::fed::key::server_key; - - std::vector queries; - for(const auto &event : eval.pdus) - for(const auto &[server_name, signatures] : json::get<"signatures"_>(event)) - for(const auto &[key_id, signature] : json::object(signatures)) - { - const server_key query - { - json::get<"origin"_>(event), key_id - }; - - // Check if we're already making a query. - if(std::binary_search(begin(queries), end(queries), query)) - continue; - - // Check if we already have the key. - if(m::keys::cache::has(json::get<"origin"_>(event), key_id)) - continue; - - // If there's a cached error on the host we can skip here. - if(m::fed::errant(json::get<"origin"_>(event))) - continue; - - queries.emplace_back(json::get<"origin"_>(event), key_id); - std::sort(begin(queries), end(queries)); - } - - const size_t fetched - { - !queries.empty()? - m::keys::fetch(queries): - 0UL - }; - - return fetched; -} - size_t ircd::m::vm::prefetch_refs(const eval &eval) { @@ -99,38 +58,6 @@ ircd::m::vm::prefetch_refs(const eval &eval) return prefetched; } -ircd::string_view -ircd::m::vm::loghead(const eval &eval) -{ - thread_local char buf[128]; - return loghead(buf, eval); -} - -ircd::string_view -ircd::m::vm::loghead(const mutable_buffer &buf, - const eval &eval) -{ - return fmt::sprintf - { - buf, "vm:%lu:%lu:%lu parent:%lu %s eval:%lu %s seq:%lu %s", - sequence::retired, - sequence::committed, - sequence::uncommitted, - eval.parent? - eval.parent->id: - 0UL, - eval.parent? - reflect(eval.parent->phase): - reflect(phase::NONE), - eval.id, - reflect(eval.phase), - sequence::get(eval), - eval.event_? - string_view{eval.event_->event_id}: - ""_sv, - }; -} - ircd::m::vm::eval * ircd::m::vm::find_root(const eval &a, const ctx::ctx &c) @@ -185,6 +112,38 @@ noexcept return ret; } +ircd::string_view +ircd::m::vm::loghead(const eval &eval) +{ + thread_local char buf[128]; + return loghead(buf, eval); +} + +ircd::string_view +ircd::m::vm::loghead(const mutable_buffer &buf, + const eval &eval) +{ + return fmt::sprintf + { + buf, "vm:%lu:%lu:%lu parent:%lu %s eval:%lu %s seq:%lu %s", + sequence::retired, + sequence::committed, + sequence::uncommitted, + eval.parent? + eval.parent->id: + 0UL, + eval.parent? + reflect(eval.parent->phase): + reflect(phase::NONE), + eval.id, + reflect(eval.phase), + sequence::get(eval), + eval.event_? + string_view{eval.event_->event_id}: + ""_sv, + }; +} + // // eval::eval // diff --git a/matrix/vm_execute.cc b/matrix/vm_execute.cc index 4bde92767..2fa1a6ee9 100644 --- a/matrix/vm_execute.cc +++ b/matrix/vm_execute.cc @@ -218,7 +218,7 @@ ircd::m::vm::execute(eval &eval, const size_t prefetched_keys { prefetch_keys? - fetch_keys(eval): 0UL + keys::fetch(eval.pdus): 0UL }; const bool prefetch_refs