From 853edcbf75ce25a1e86141dbf4faed6322fac855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Botond=20D=C3=A9nes?= Date: Thu, 21 May 2026 13:00:46 +0300 Subject: [PATCH] tracing: add_query(): change query param to utils::chunked_string Having to unconditionally linearize the chunked query string when passing it to tracing undoes the work put into reducing large alloctions on the query path. The add_query() is evaluated eagerly on every query, even if tracing is disabled. Defer the linearization to build_parameres_map(), which is only called if tracing is enabled. --- alternator/server.cc | 3 ++- tracing/trace_state.cc | 13 +++++-------- tracing/trace_state.hh | 24 ++++-------------------- transport/server.cc | 6 +++--- 4 files changed, 14 insertions(+), 32 deletions(-) diff --git a/alternator/server.cc b/alternator/server.cc index 6c6d74caba..be5b354f53 100644 --- a/alternator/server.cc +++ b/alternator/server.cc @@ -9,6 +9,7 @@ #include "alternator/server.hh" #include "audit/audit.hh" #include "alternator/executor_util.hh" +#include "utils/chunked_string.hh" #include "gms/application_state.hh" #include "utils/log.hh" #include @@ -543,7 +544,7 @@ static tracing::trace_state_ptr maybe_trace_query(service::client_state& client_ if (tracing_instance.trace_next_query() || tracing_instance.slow_query_tracing_enabled()) { trace_state = create_tracing_session(tracing_instance); tracing::add_session_param(trace_state, "alternator_op", op); - tracing::add_query(trace_state, truncated_content_view(query, max_users_query_size_in_trace_output).take_as_sstring()); + tracing::add_query(trace_state, utils::chunked_string(truncated_content_view(query, max_users_query_size_in_trace_output).as_view())); tracing::begin(trace_state, seastar::format("Alternator {}", op), client_state.get_client_address()); if (!username.empty()) { tracing::set_username(trace_state, auth::authenticated_user(username)); diff --git a/tracing/trace_state.cc b/tracing/trace_state.cc index 7886095370..7874bfd404 100644 --- a/tracing/trace_state.cc +++ b/tracing/trace_state.cc @@ -11,6 +11,7 @@ #include "cql3/statements/prepared_statement.hh" #include "tracing/trace_state.hh" #include "mutation/timestamp.hh" +#include "utils/chunked_string.hh" #include "cql3/values.hh" #include "cql3/query_options.hh" @@ -29,7 +30,7 @@ struct trace_state::params_values { std::optional batchlog_endpoints; std::optional user_timestamp; - std::vector queries; + std::vector queries; std::optional cl; std::optional serial_cl; std::optional page_size; @@ -75,14 +76,10 @@ void trace_state::set_response_size(size_t s) noexcept { _records->session_rec.response_size = s; } -void trace_state::add_query(sstring &&val) { +void trace_state::add_query(utils::chunked_string val) { _params_ptr->queries.emplace_back(std::move(val)); } -void trace_state::add_query(std::string_view val) { - _params_ptr->queries.emplace_back(sstring{ val }); -} - void trace_state::add_session_param(std::string_view key, std::string_view val) { _records->session_rec.parameters.emplace(std::move(key), std::move(val)); } @@ -145,11 +142,11 @@ void trace_state::build_parameters_map() { auto& queries = vals.queries; if (!queries.empty()) { if (queries.size() == 1) { - params_map.emplace("query", queries[0]); + params_map.emplace("query", queries[0].linearize()); } else { // BATCH for (size_t i = 0; i < queries.size(); ++i) { - params_map.emplace(format("query[{:d}]", i), queries[i]); + params_map.emplace(format("query[{:d}]", i), queries[i].linearize()); } } } diff --git a/tracing/trace_state.hh b/tracing/trace_state.hh index f974c5f6ed..8824e79f9f 100644 --- a/tracing/trace_state.hh +++ b/tracing/trace_state.hh @@ -14,6 +14,7 @@ #include #include #include "tracing/tracing.hh" +#include "utils/chunked_string.hh" #include "gms/inet_address.hh" #include "auth/authenticated_user.hh" #include "db/consistency_level_type.hh" @@ -362,17 +363,7 @@ private: * * @param val the query string */ - void add_query(sstring &&val); - - /** - * Store a query string. - * - * This value will eventually be stored in a params map of a tracing session - * with a 'query' key. - * - * @param val the query string - */ - void add_query(std::string_view val); + void add_query(utils::chunked_string val); /** * Store a custom session parameter. @@ -493,8 +484,7 @@ private: friend void set_request_size(const trace_state_ptr& p, size_t s) noexcept; friend void set_response_size(const trace_state_ptr& p, size_t s) noexcept; friend void set_batchlog_endpoints(const trace_state_ptr& p, const host_id_vector_replica_set& val); - friend void add_query(const trace_state_ptr& p, sstring &&val); - friend void add_query(const trace_state_ptr& p, std::string_view val); + friend void add_query(const trace_state_ptr& p, utils::chunked_string val); friend void add_session_param(const trace_state_ptr& p, std::string_view key, std::string_view val); friend void set_common_query_parameters(const trace_state_ptr& p, db::consistency_level consistency, const std::optional& serial_consistency, api::timestamp_type timestamp); @@ -620,18 +610,12 @@ inline void set_batchlog_endpoints(const trace_state_ptr& p, const host_id_vecto } } -inline void add_query(const trace_state_ptr& p, sstring &&val) { +inline void add_query(const trace_state_ptr& p, utils::chunked_string val) { if (p) { p->add_query(std::move(val)); } } -inline void add_query(const trace_state_ptr& p, std::string_view val) { - if (p) { - p->add_query(val); - } -} - inline void add_session_param(const trace_state_ptr& p, std::string_view key, std::string_view val) { if (p) { p->add_session_param(std::move(key), std::move(val)); diff --git a/transport/server.cc b/transport/server.cc index 975602ef25..34233324b3 100644 --- a/transport/server.cc +++ b/transport/server.cc @@ -1603,7 +1603,7 @@ future> cql_server::connection::process_pr auto query = std::move(query_result).assume_value(); auto dialect = get_dialect(); - tracing::add_query(trace_state, query.linearize()); + tracing::add_query(trace_state, query); tracing::begin(trace_state, "Preparing CQL3 query", client_state.get_client_address()); return _server._query_processor.invoke_on_others([query, &client_state, dialect] (auto& qp) mutable { @@ -1671,7 +1671,7 @@ process_execute_internal(service::client_state& client_state, shardedstatement->raw_cql_statement.linearize())); + tracing::add_query(trace_state, prepared->statement->raw_cql_statement); tracing::add_prepared_statement(trace_state, prepared); tracing::set_common_query_parameters(trace_state, options.get_consistency(), options.get_serial_consistency(), options.get_specific_options().timestamp); @@ -1777,7 +1777,7 @@ process_batch_internal(service::client_state& client_state, shardedchecked_weak_from_this()).second; } if (init_trace) { - tracing::add_query(trace_state, sstring(ps->statement->raw_cql_statement.linearize())); + tracing::add_query(trace_state, ps->statement->raw_cql_statement); } break; }