/* * Copyright (C) 2015 ScyllaDB */ /* * This file is part of Scylla. * * Scylla is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Scylla is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Scylla. If not, see . */ #include "message/messaging_service.hh" #include #include "gms/failure_detector.hh" #include "gms/gossiper.hh" #include "service/storage_service.hh" #include "streaming/prepare_message.hh" #include "gms/gossip_digest_syn.hh" #include "gms/gossip_digest_ack.hh" #include "gms/gossip_digest_ack2.hh" #include "gms/gossiper.hh" #include "query-request.hh" #include "query-result.hh" #include #include "db/config.hh" #include "db/system_keyspace.hh" #include "db/view/view_update_backlog.hh" #include "dht/i_partitioner.hh" #include "range.hh" #include "frozen_schema.hh" #include "repair/repair.hh" #include "digest_algorithm.hh" #include "idl/consistency_level.dist.hh" #include "idl/tracing.dist.hh" #include "idl/result.dist.hh" #include "idl/reconcilable_result.dist.hh" #include "idl/ring_position.dist.hh" #include "idl/keys.dist.hh" #include "idl/uuid.dist.hh" #include "idl/frozen_mutation.dist.hh" #include "idl/frozen_schema.dist.hh" #include "idl/streaming.dist.hh" #include "idl/token.dist.hh" #include "idl/gossip_digest.dist.hh" #include "idl/read_command.dist.hh" #include "idl/range.dist.hh" #include "idl/partition_checksum.dist.hh" #include "idl/query.dist.hh" #include "idl/cache_temperature.dist.hh" #include "idl/view.dist.hh" #include "idl/mutation.dist.hh" #include "serializer_impl.hh" #include "serialization_visitors.hh" #include "idl/consistency_level.dist.impl.hh" #include "idl/tracing.dist.impl.hh" #include "idl/result.dist.impl.hh" #include "idl/reconcilable_result.dist.impl.hh" #include "idl/ring_position.dist.impl.hh" #include "idl/keys.dist.impl.hh" #include "idl/uuid.dist.impl.hh" #include "idl/frozen_mutation.dist.impl.hh" #include "idl/frozen_schema.dist.impl.hh" #include "idl/streaming.dist.impl.hh" #include "idl/token.dist.impl.hh" #include "idl/gossip_digest.dist.impl.hh" #include "idl/read_command.dist.impl.hh" #include "idl/range.dist.impl.hh" #include "idl/partition_checksum.dist.impl.hh" #include "idl/query.dist.impl.hh" #include "idl/cache_temperature.dist.impl.hh" #include "idl/mutation.dist.impl.hh" #include #include #include "idl/view.dist.impl.hh" #include "partition_range_compat.hh" #include #include #include "frozen_mutation.hh" #include "flat_mutation_reader.hh" #include "streaming/stream_manager.hh" namespace netw { // thunk from rpc serializers to generate serializers template void write(serializer, Output& out, const T& data) { ser::serialize(out, data); } template T read(serializer, Input& in, boost::type type) { return ser::deserialize(in, type); } template void write(serializer s, Output& out, const foreign_ptr& v) { return write(s, out, *v); } template foreign_ptr read(serializer s, Input& in, boost::type>) { return make_foreign(read(s, in, boost::type())); } template void write(serializer s, Output& out, const lw_shared_ptr& v) { return write(s, out, *v); } template lw_shared_ptr read(serializer s, Input& in, boost::type>) { return make_lw_shared(read(s, in, boost::type())); } static logging::logger mlogger("messaging_service"); static logging::logger rpc_logger("rpc"); using inet_address = gms::inet_address; using gossip_digest_syn = gms::gossip_digest_syn; using gossip_digest_ack = gms::gossip_digest_ack; using gossip_digest_ack2 = gms::gossip_digest_ack2; using rpc_protocol = rpc::protocol; using namespace std::chrono_literals; static rpc::lz4_compressor::factory lz4_compressor_factory; static rpc::multi_algo_compressor_factory compressor_factory(&lz4_compressor_factory); struct messaging_service::rpc_protocol_wrapper : public rpc_protocol { using rpc_protocol::rpc_protocol; }; // This wrapper pretends to be rpc_protocol::client, but also handles // stopping it before destruction, in case it wasn't stopped already. // This should be integrated into messaging_service proper. class messaging_service::rpc_protocol_client_wrapper { std::unique_ptr _p; ::shared_ptr _credentials; public: rpc_protocol_client_wrapper(rpc_protocol& proto, rpc::client_options opts, ipv4_addr addr, ipv4_addr local = ipv4_addr()) : _p(std::make_unique(proto, std::move(opts), addr, local)) { } rpc_protocol_client_wrapper(rpc_protocol& proto, rpc::client_options opts, ipv4_addr addr, ipv4_addr local, ::shared_ptr c) : _p(std::make_unique(proto, std::move(opts), seastar::tls::socket(c), addr, local)) , _credentials(c) {} auto get_stats() const { return _p->get_stats(); } future<> stop() { return _p->stop(); } bool error() { return _p->error(); } operator rpc_protocol::client&() { return *_p; } /** * #3787 Must ensure we use the right type of socker. I.e. tls or not. * See above, we retain credentials object so we here can know if we * are tls or not. */ template future> make_stream_sink() { if (_credentials) { return _p->make_stream_sink(seastar::tls::socket(_credentials)); } return _p->make_stream_sink(); } }; struct messaging_service::rpc_protocol_server_wrapper : public rpc_protocol::server { using rpc_protocol::server::server; }; constexpr int32_t messaging_service::current_version; distributed _the_messaging_service; bool operator==(const msg_addr& x, const msg_addr& y) { // Ignore cpu id for now since we do not really support shard to shard connections return x.addr == y.addr; } bool operator<(const msg_addr& x, const msg_addr& y) { // Ignore cpu id for now since we do not really support shard to shard connections if (x.addr < y.addr) { return true; } else { return false; } } std::ostream& operator<<(std::ostream& os, const msg_addr& x) { return os << x.addr << ":" << x.cpu_id; } size_t msg_addr::hash::operator()(const msg_addr& id) const { // Ignore cpu id for now since we do not really support // shard to shard connections return std::hash()(id.addr.raw_addr()); } messaging_service::shard_info::shard_info(shared_ptr&& client) : rpc_client(std::move(client)) { } rpc::stats messaging_service::shard_info::get_stats() const { return rpc_client->get_stats(); } void messaging_service::foreach_client(std::function f) const { for (unsigned idx = 0; idx < _clients.size(); idx ++) { for (auto i = _clients[idx].cbegin(); i != _clients[idx].cend(); i++) { f(i->first, i->second); } } } void messaging_service::foreach_server_connection_stats(std::function&& f) const { for (auto&& s : _server) { if (s) { s->foreach_connection([f](const rpc_protocol::server::connection& c) { f(c.info(), c.get_stats()); }); } } } void messaging_service::increment_dropped_messages(messaging_verb verb) { _dropped_messages[static_cast(verb)]++; } uint64_t messaging_service::get_dropped_messages(messaging_verb verb) const { return _dropped_messages[static_cast(verb)]; } const uint64_t* messaging_service::get_dropped_messages() const { return _dropped_messages; } messaging_service::drop_notifier_handler messaging_service::register_connection_drop_notifier(std::function cb) { _connection_drop_notifiers.push_back(std::move(cb)); return _connection_drop_notifiers.end(); } void messaging_service::unregister_connection_drop_notifier(messaging_service::drop_notifier_handler h) { _connection_drop_notifiers.erase(h); } int32_t messaging_service::get_raw_version(const gms::inet_address& endpoint) const { // FIXME: messaging service versioning return current_version; } bool messaging_service::knows_version(const gms::inet_address& endpoint) const { // FIXME: messaging service versioning return true; } // Register a handler (a callback lambda) for verb template void register_handler(messaging_service* ms, messaging_verb verb, Func&& func) { ms->rpc()->register_handler(verb, ms->scheduling_group_for_verb(verb), std::move(func)); } messaging_service::messaging_service(gms::inet_address ip, uint16_t port, bool listen_now) : messaging_service(std::move(ip), port, encrypt_what::none, compress_what::none, tcp_nodelay_what::all, 0, nullptr, memory_config{1'000'000}, scheduling_config{}, false, listen_now) {} static rpc::resource_limits rpc_resource_limits(size_t memory_limit) { rpc::resource_limits limits; limits.bloat_factor = 3; limits.basic_request_size = 1000; limits.max_memory = memory_limit; return limits; } void messaging_service::start_listen() { bool listen_to_bc = _should_listen_to_broadcast_address && _listen_address != utils::fb_utilities::get_broadcast_address(); rpc::server_options so; if (_compress_what != compress_what::none) { so.compressor_factory = &compressor_factory; } so.streaming_domain = rpc::streaming_domain_type(0x55AA); so.load_balancing_algorithm = server_socket::load_balancing_algorithm::port; // FIXME: we don't set so.tcp_nodelay, because we can't tell at this point whether the connection will come from a // local or remote datacenter, and whether or not the connection will be used for gossip. We can fix // the first by wrapping its server_socket, but not the second. auto limits = rpc_resource_limits(_mcfg.rpc_memory_limit); if (!_server[0]) { auto listen = [&] (const gms::inet_address& a) { auto addr = ipv4_addr{a.raw_addr(), _port}; return std::unique_ptr(new rpc_protocol_server_wrapper(*_rpc, so, addr, limits)); }; _server[0] = listen(_listen_address); if (listen_to_bc) { _server[1] = listen(utils::fb_utilities::get_broadcast_address()); } } if (!_server_tls[0]) { auto listen = [&] (const gms::inet_address& a) { return std::unique_ptr( [this, &so, &a, limits] () -> std::unique_ptr{ if (_encrypt_what == encrypt_what::none) { return nullptr; } listen_options lo; lo.reuse_address = true; lo.lba = server_socket::load_balancing_algorithm::port; auto addr = make_ipv4_address(ipv4_addr{a.raw_addr(), _ssl_port}); return std::make_unique(*_rpc, so, seastar::tls::listen(_credentials, addr, lo), limits); }()); }; _server_tls[0] = listen(_listen_address); if (listen_to_bc) { _server_tls[1] = listen(utils::fb_utilities::get_broadcast_address()); } } // Do this on just cpu 0, to avoid duplicate logs. if (engine().cpu_id() == 0) { if (_server_tls[0]) { mlogger.info("Starting Encrypted Messaging Service on SSL port {}", _ssl_port); } mlogger.info("Starting Messaging Service on port {}", _port); } } messaging_service::messaging_service(gms::inet_address ip , uint16_t port , encrypt_what ew , compress_what cw , tcp_nodelay_what tnw , uint16_t ssl_port , std::shared_ptr credentials , messaging_service::memory_config mcfg , scheduling_config scfg , bool sltba , bool listen_now) : _listen_address(ip) , _port(port) , _ssl_port(ssl_port) , _encrypt_what(ew) , _compress_what(cw) , _tcp_nodelay_what(tnw) , _should_listen_to_broadcast_address(sltba) , _rpc(new rpc_protocol_wrapper(serializer { })) , _credentials(credentials ? credentials->build_server_credentials() : nullptr) , _mcfg(mcfg) , _scheduling_config(scfg) { _rpc->set_logger([] (const sstring& log) { rpc_logger.info("{}", log); }); register_handler(this, messaging_verb::CLIENT_ID, [] (rpc::client_info& ci, gms::inet_address broadcast_address, uint32_t src_cpu_id, rpc::optional max_result_size) { ci.attach_auxiliary("baddr", broadcast_address); ci.attach_auxiliary("src_cpu_id", src_cpu_id); ci.attach_auxiliary("max_result_size", max_result_size.value_or(query::result_memory_limiter::maximum_result_size)); return rpc::no_wait; }); if (listen_now) { start_listen(); } } msg_addr messaging_service::get_source(const rpc::client_info& cinfo) { return msg_addr{ cinfo.retrieve_auxiliary("baddr"), cinfo.retrieve_auxiliary("src_cpu_id") }; } messaging_service::~messaging_service() = default; uint16_t messaging_service::port() { return _port; } gms::inet_address messaging_service::listen_address() { return _listen_address; } static future<> stop_servers(std::array, 2>& servers) { return parallel_for_each( servers | boost::adaptors::filtered([] (auto& ptr) { return bool(ptr); }) | boost::adaptors::indirected, std::mem_fn(&messaging_service::rpc_protocol_server_wrapper::stop)); } future<> messaging_service::stop_tls_server() { return stop_servers(_server_tls); } future<> messaging_service::stop_nontls_server() { return stop_servers(_server); } future<> messaging_service::stop_client() { return parallel_for_each(_clients, [] (auto& m) { return parallel_for_each(m, [] (std::pair& c) { return c.second.rpc_client->stop(); }); }); } future<> messaging_service::stop() { _stopping = true; return when_all(stop_nontls_server(), stop_tls_server(), stop_client()).discard_result(); } rpc::no_wait_type messaging_service::no_wait() { return rpc::no_wait; } static constexpr unsigned do_get_rpc_client_idx(messaging_verb verb) { switch (verb) { case messaging_verb::CLIENT_ID: case messaging_verb::MUTATION: case messaging_verb::READ_DATA: case messaging_verb::READ_MUTATION_DATA: case messaging_verb::READ_DIGEST: case messaging_verb::GOSSIP_DIGEST_ACK: case messaging_verb::DEFINITIONS_UPDATE: case messaging_verb::TRUNCATE: case messaging_verb::MIGRATION_REQUEST: case messaging_verb::SCHEMA_CHECK: case messaging_verb::COUNTER_MUTATION: return 0; // GET_SCHEMA_VERSION is sent from read/mutate verbs so should be // sent on a different connection to avoid potential deadlocks // as well as reduce latency as there are potentially many requests // blocked on schema version request. case messaging_verb::GOSSIP_DIGEST_SYN: case messaging_verb::GOSSIP_DIGEST_ACK2: case messaging_verb::GOSSIP_SHUTDOWN: case messaging_verb::GOSSIP_ECHO: case messaging_verb::GET_SCHEMA_VERSION: return 1; case messaging_verb::PREPARE_MESSAGE: case messaging_verb::PREPARE_DONE_MESSAGE: case messaging_verb::STREAM_MUTATION: case messaging_verb::STREAM_MUTATION_DONE: case messaging_verb::COMPLETE_MESSAGE: case messaging_verb::REPLICATION_FINISHED: case messaging_verb::REPAIR_CHECKSUM_RANGE: case messaging_verb::STREAM_MUTATION_FRAGMENTS: case messaging_verb::REPAIR_ROW_LEVEL_START: case messaging_verb::REPAIR_ROW_LEVEL_STOP: case messaging_verb::REPAIR_GET_FULL_ROW_HASHES: case messaging_verb::REPAIR_GET_COMBINED_ROW_HASH: case messaging_verb::REPAIR_GET_SYNC_BOUNDARY: case messaging_verb::REPAIR_GET_ROW_DIFF: case messaging_verb::REPAIR_PUT_ROW_DIFF: case messaging_verb::REPAIR_GET_ESTIMATED_PARTITIONS: case messaging_verb::REPAIR_SET_ESTIMATED_PARTITIONS: case messaging_verb::REPAIR_GET_DIFF_ALGORITHMS: return 2; case messaging_verb::MUTATION_DONE: case messaging_verb::MUTATION_FAILED: return 3; case messaging_verb::LAST: return -1; // should never happen } } static constexpr std::array(messaging_verb::LAST)> make_rpc_client_idx_table() { std::array(messaging_verb::LAST)> tab{}; for (size_t i = 0; i < tab.size(); ++i) { tab[i] = do_get_rpc_client_idx(messaging_verb(i)); } return tab; } static std::array(messaging_verb::LAST)> s_rpc_client_idx_table = make_rpc_client_idx_table(); static unsigned get_rpc_client_idx(messaging_verb verb) { return s_rpc_client_idx_table[static_cast(verb)]; } scheduling_group messaging_service::scheduling_group_for_verb(messaging_verb verb) const { static const scheduling_group scheduling_config::*idx_to_group[] = { &scheduling_config::statement, &scheduling_config::gossip, &scheduling_config::streaming, &scheduling_config::statement, }; return _scheduling_config.*(idx_to_group[get_rpc_client_idx(verb)]); } /** * Get an IP for a given endpoint to connect to * * @param ep endpoint to check * * @return preferred IP (local) for the given endpoint if exists and if the * given endpoint resides in the same data center with the current Node. * Otherwise 'ep' itself is returned. */ gms::inet_address messaging_service::get_preferred_ip(gms::inet_address ep) { auto it = _preferred_ip_cache.find(ep); if (it != _preferred_ip_cache.end()) { auto& snitch_ptr = locator::i_endpoint_snitch::get_local_snitch_ptr(); auto my_addr = utils::fb_utilities::get_broadcast_address(); if (snitch_ptr->get_datacenter(ep) == snitch_ptr->get_datacenter(my_addr)) { return it->second; } } // If cache doesn't have an entry for this endpoint - return endpoint itself return ep; } future<> messaging_service::init_local_preferred_ip_cache() { return db::system_keyspace::get_preferred_ips().then([this] (auto ips_cache) { _preferred_ip_cache = ips_cache; // // Reset the connections to the endpoints that have entries in // _preferred_ip_cache so that they reopen with the preferred IPs we've // just read. // for (auto& p : _preferred_ip_cache) { this->remove_rpc_client(msg_addr(p.first)); } }); } void messaging_service::cache_preferred_ip(gms::inet_address ep, gms::inet_address ip) { _preferred_ip_cache[ep] = ip; } shared_ptr messaging_service::get_rpc_client(messaging_verb verb, msg_addr id) { assert(!_stopping); auto idx = get_rpc_client_idx(verb); auto it = _clients[idx].find(id); if (it != _clients[idx].end()) { auto c = it->second.rpc_client; if (!c->error()) { return c; } remove_error_rpc_client(verb, id); } auto must_encrypt = [&id, this] { if (_encrypt_what == encrypt_what::none) { return false; } if (_encrypt_what == encrypt_what::all) { return true; } auto& snitch_ptr = locator::i_endpoint_snitch::get_local_snitch_ptr(); if (_encrypt_what == encrypt_what::dc) { return snitch_ptr->get_datacenter(id.addr) != snitch_ptr->get_datacenter(utils::fb_utilities::get_broadcast_address()); } return snitch_ptr->get_rack(id.addr) != snitch_ptr->get_rack(utils::fb_utilities::get_broadcast_address()); }(); auto must_compress = [&id, this] { if (_compress_what == compress_what::none) { return false; } if (_compress_what == compress_what::dc) { auto& snitch_ptr = locator::i_endpoint_snitch::get_local_snitch_ptr(); return snitch_ptr->get_datacenter(id.addr) != snitch_ptr->get_datacenter(utils::fb_utilities::get_broadcast_address()); } return true; }(); auto must_tcp_nodelay = [&] { if (idx == 1) { return true; // gossip } if (_tcp_nodelay_what == tcp_nodelay_what::local) { auto& snitch_ptr = locator::i_endpoint_snitch::get_local_snitch_ptr(); return snitch_ptr->get_datacenter(id.addr) == snitch_ptr->get_datacenter(utils::fb_utilities::get_broadcast_address()); } return true; }(); auto remote_addr = ipv4_addr(get_preferred_ip(id.addr).raw_addr(), must_encrypt ? _ssl_port : _port); rpc::client_options opts; // send keepalive messages each minute if connection is idle, drop connection after 10 failures opts.keepalive = std::optional({60s, 60s, 10}); if (must_compress) { opts.compressor_factory = &compressor_factory; } opts.tcp_nodelay = must_tcp_nodelay; auto client = must_encrypt ? ::make_shared(*_rpc, std::move(opts), remote_addr, ipv4_addr(), _credentials) : ::make_shared(*_rpc, std::move(opts), remote_addr); it = _clients[idx].emplace(id, shard_info(std::move(client))).first; uint32_t src_cpu_id = engine().cpu_id(); _rpc->make_client(messaging_verb::CLIENT_ID)(*it->second.rpc_client, utils::fb_utilities::get_broadcast_address(), src_cpu_id, query::result_memory_limiter::maximum_result_size); return it->second.rpc_client; } bool messaging_service::remove_rpc_client_one(clients_map& clients, msg_addr id, bool dead_only) { if (_stopping) { // if messaging service is in a processed of been stopped no need to // stop and remove connection here since they are being stopped already // and we'll just interfere return false; } bool found = false; auto it = clients.find(id); if (it != clients.end() && (!dead_only || it->second.rpc_client->error())) { auto client = std::move(it->second.rpc_client); clients.erase(it); // // Explicitly call rpc_protocol_client_wrapper::stop() for the erased // item and hold the messaging_service shared pointer till it's over. // This will make sure messaging_service::stop() blocks until // client->stop() is over. // client->stop().finally([id, client, ms = shared_from_this()] { mlogger.debug("dropped connection to {}", id.addr); }).discard_result(); found = true; } return found; } void messaging_service::remove_error_rpc_client(messaging_verb verb, msg_addr id) { if (remove_rpc_client_one(_clients[get_rpc_client_idx(verb)], id, true)) { for (auto&& cb : _connection_drop_notifiers) { cb(id.addr); } } } void messaging_service::remove_rpc_client(msg_addr id) { for (auto& c : _clients) { remove_rpc_client_one(c, id, false); } } std::unique_ptr& messaging_service::rpc() { return _rpc; } rpc::sink messaging_service::make_sink_for_stream_mutation_fragments(rpc::source& source) { return source.make_sink(); } future, rpc::source> messaging_service::make_sink_and_source_for_stream_mutation_fragments(utils::UUID schema_id, utils::UUID plan_id, utils::UUID cf_id, uint64_t estimated_partitions, streaming::stream_reason reason, msg_addr id) { auto wrapper = get_rpc_client(messaging_verb::STREAM_MUTATION_FRAGMENTS, id); rpc_protocol::client& rpc_client = *wrapper; return wrapper->make_stream_sink().then([this, plan_id, schema_id, cf_id, estimated_partitions, reason, &rpc_client] (rpc::sink sink) mutable { auto rpc_handler = rpc()->make_client (utils::UUID, utils::UUID, utils::UUID, uint64_t, streaming::stream_reason, rpc::sink)>(messaging_verb::STREAM_MUTATION_FRAGMENTS); return rpc_handler(rpc_client , plan_id, schema_id, cf_id, estimated_partitions, reason, sink).then_wrapped([sink] (future> source) mutable { return (source.failed() ? sink.close() : make_ready_future<>()).then([sink = std::move(sink), source = std::move(source)] () mutable { return make_ready_future, rpc::source>(std::move(sink), std::move(source.get0())); }); }); }); } void messaging_service::register_stream_mutation_fragments(std::function> (const rpc::client_info& cinfo, UUID plan_id, UUID schema_id, UUID cf_id, uint64_t estimated_partitions, rpc::optional, rpc::source source)>&& func) { register_handler(this, messaging_verb::STREAM_MUTATION_FRAGMENTS, std::move(func)); } // Send a message for verb template auto send_message(messaging_service* ms, messaging_verb verb, msg_addr id, MsgOut&&... msg) { auto rpc_handler = ms->rpc()->make_client(verb); if (ms->is_stopping()) { using futurator = futurize>; return futurator::make_exception_future(rpc::closed_error()); } auto rpc_client_ptr = ms->get_rpc_client(verb, id); auto& rpc_client = *rpc_client_ptr; return rpc_handler(rpc_client, std::forward(msg)...).then_wrapped([ms = ms->shared_from_this(), id, verb, rpc_client_ptr = std::move(rpc_client_ptr)] (auto&& f) { try { if (f.failed()) { ms->increment_dropped_messages(verb); f.get(); assert(false); // never reached } return std::move(f); } catch (rpc::closed_error&) { // This is a transport error ms->remove_error_rpc_client(verb, id); throw; } catch (...) { // This is expected to be a rpc server error, e.g., the rpc handler throws a std::runtime_error. throw; } }); } // TODO: Remove duplicated code in send_message template auto send_message_timeout(messaging_service* ms, messaging_verb verb, msg_addr id, Timeout timeout, MsgOut&&... msg) { auto rpc_handler = ms->rpc()->make_client(verb); if (ms->is_stopping()) { using futurator = futurize>; return futurator::make_exception_future(rpc::closed_error()); } auto rpc_client_ptr = ms->get_rpc_client(verb, id); auto& rpc_client = *rpc_client_ptr; return rpc_handler(rpc_client, timeout, std::forward(msg)...).then_wrapped([ms = ms->shared_from_this(), id, verb, rpc_client_ptr = std::move(rpc_client_ptr)] (auto&& f) { try { if (f.failed()) { ms->increment_dropped_messages(verb); f.get(); assert(false); // never reached } return std::move(f); } catch (rpc::closed_error&) { // This is a transport error ms->remove_error_rpc_client(verb, id); throw; } catch (...) { // This is expected to be a rpc server error, e.g., the rpc handler throws a std::runtime_error. throw; } }); } // Send one way message for verb template auto send_message_oneway(messaging_service* ms, messaging_verb verb, msg_addr id, MsgOut&&... msg) { return send_message(ms, std::move(verb), std::move(id), std::forward(msg)...); } // Send one way message for verb template auto send_message_oneway_timeout(messaging_service* ms, Timeout timeout, messaging_verb verb, msg_addr id, MsgOut&&... msg) { return send_message_timeout(ms, std::move(verb), std::move(id), timeout, std::forward(msg)...); } // Wrappers for verbs // PREPARE_MESSAGE void messaging_service::register_prepare_message(std::function (const rpc::client_info& cinfo, streaming::prepare_message msg, UUID plan_id, sstring description, rpc::optional reason)>&& func) { register_handler(this, messaging_verb::PREPARE_MESSAGE, std::move(func)); } future messaging_service::send_prepare_message(msg_addr id, streaming::prepare_message msg, UUID plan_id, sstring description, streaming::stream_reason reason) { return send_message(this, messaging_verb::PREPARE_MESSAGE, id, std::move(msg), plan_id, std::move(description), reason); } // PREPARE_DONE_MESSAGE void messaging_service::register_prepare_done_message(std::function (const rpc::client_info& cinfo, UUID plan_id, unsigned dst_cpu_id)>&& func) { register_handler(this, messaging_verb::PREPARE_DONE_MESSAGE, std::move(func)); } future<> messaging_service::send_prepare_done_message(msg_addr id, UUID plan_id, unsigned dst_cpu_id) { return send_message(this, messaging_verb::PREPARE_DONE_MESSAGE, id, plan_id, dst_cpu_id); } // STREAM_MUTATION void messaging_service::register_stream_mutation(std::function (const rpc::client_info& cinfo, UUID plan_id, frozen_mutation fm, unsigned dst_cpu_id, rpc::optional fragmented, rpc::optional reason)>&& func) { register_handler(this, messaging_verb::STREAM_MUTATION, std::move(func)); } future<> messaging_service::send_stream_mutation(msg_addr id, UUID plan_id, frozen_mutation fm, unsigned dst_cpu_id, bool fragmented, streaming::stream_reason reason) { return send_message(this, messaging_verb::STREAM_MUTATION, id, plan_id, std::move(fm), dst_cpu_id, fragmented, reason); } // STREAM_MUTATION_DONE void messaging_service::register_stream_mutation_done(std::function (const rpc::client_info& cinfo, UUID plan_id, dht::token_range_vector ranges, UUID cf_id, unsigned dst_cpu_id)>&& func) { register_handler(this, messaging_verb::STREAM_MUTATION_DONE, [func = std::move(func)] (const rpc::client_info& cinfo, UUID plan_id, std::vector> ranges, UUID cf_id, unsigned dst_cpu_id) mutable { return func(cinfo, plan_id, ::compat::unwrap(std::move(ranges)), cf_id, dst_cpu_id); }); } future<> messaging_service::send_stream_mutation_done(msg_addr id, UUID plan_id, dht::token_range_vector ranges, UUID cf_id, unsigned dst_cpu_id) { return send_message(this, messaging_verb::STREAM_MUTATION_DONE, id, plan_id, std::move(ranges), cf_id, dst_cpu_id); } // COMPLETE_MESSAGE void messaging_service::register_complete_message(std::function (const rpc::client_info& cinfo, UUID plan_id, unsigned dst_cpu_id, rpc::optional failed)>&& func) { register_handler(this, messaging_verb::COMPLETE_MESSAGE, std::move(func)); } future<> messaging_service::send_complete_message(msg_addr id, UUID plan_id, unsigned dst_cpu_id, bool failed) { return send_message(this, messaging_verb::COMPLETE_MESSAGE, id, plan_id, dst_cpu_id, failed); } void messaging_service::register_gossip_echo(std::function ()>&& func) { register_handler(this, messaging_verb::GOSSIP_ECHO, std::move(func)); } void messaging_service::unregister_gossip_echo() { _rpc->unregister_handler(netw::messaging_verb::GOSSIP_ECHO); } future<> messaging_service::send_gossip_echo(msg_addr id) { return send_message_timeout(this, messaging_verb::GOSSIP_ECHO, std::move(id), 3000ms); } void messaging_service::register_gossip_shutdown(std::function&& func) { register_handler(this, messaging_verb::GOSSIP_SHUTDOWN, std::move(func)); } void messaging_service::unregister_gossip_shutdown() { _rpc->unregister_handler(netw::messaging_verb::GOSSIP_SHUTDOWN); } future<> messaging_service::send_gossip_shutdown(msg_addr id, inet_address from) { return send_message_oneway(this, messaging_verb::GOSSIP_SHUTDOWN, std::move(id), std::move(from)); } // gossip syn void messaging_service::register_gossip_digest_syn(std::function&& func) { register_handler(this, messaging_verb::GOSSIP_DIGEST_SYN, std::move(func)); } void messaging_service::unregister_gossip_digest_syn() { _rpc->unregister_handler(netw::messaging_verb::GOSSIP_DIGEST_SYN); } future<> messaging_service::send_gossip_digest_syn(msg_addr id, gossip_digest_syn msg) { return send_message_oneway(this, messaging_verb::GOSSIP_DIGEST_SYN, std::move(id), std::move(msg)); } // gossip ack void messaging_service::register_gossip_digest_ack(std::function&& func) { register_handler(this, messaging_verb::GOSSIP_DIGEST_ACK, std::move(func)); } void messaging_service::unregister_gossip_digest_ack() { _rpc->unregister_handler(netw::messaging_verb::GOSSIP_DIGEST_ACK); } future<> messaging_service::send_gossip_digest_ack(msg_addr id, gossip_digest_ack msg) { return send_message_oneway(this, messaging_verb::GOSSIP_DIGEST_ACK, std::move(id), std::move(msg)); } // gossip ack2 void messaging_service::register_gossip_digest_ack2(std::function&& func) { register_handler(this, messaging_verb::GOSSIP_DIGEST_ACK2, std::move(func)); } void messaging_service::unregister_gossip_digest_ack2() { _rpc->unregister_handler(netw::messaging_verb::GOSSIP_DIGEST_ACK2); } future<> messaging_service::send_gossip_digest_ack2(msg_addr id, gossip_digest_ack2 msg) { return send_message_oneway(this, messaging_verb::GOSSIP_DIGEST_ACK2, std::move(id), std::move(msg)); } void messaging_service::register_definitions_update(std::function fm)>&& func) { register_handler(this, netw::messaging_verb::DEFINITIONS_UPDATE, std::move(func)); } void messaging_service::unregister_definitions_update() { _rpc->unregister_handler(netw::messaging_verb::DEFINITIONS_UPDATE); } future<> messaging_service::send_definitions_update(msg_addr id, std::vector fm) { return send_message_oneway(this, messaging_verb::DEFINITIONS_UPDATE, std::move(id), std::move(fm)); } void messaging_service::register_migration_request(std::function> (const rpc::client_info&)>&& func) { register_handler(this, netw::messaging_verb::MIGRATION_REQUEST, std::move(func)); } void messaging_service::unregister_migration_request() { _rpc->unregister_handler(netw::messaging_verb::MIGRATION_REQUEST); } future> messaging_service::send_migration_request(msg_addr id) { return send_message>(this, messaging_verb::MIGRATION_REQUEST, std::move(id)); } void messaging_service::register_mutation(std::function (const rpc::client_info&, rpc::opt_time_point, frozen_mutation fm, std::vector forward, inet_address reply_to, unsigned shard, response_id_type response_id, rpc::optional> trace_info)>&& func) { register_handler(this, netw::messaging_verb::MUTATION, std::move(func)); } void messaging_service::unregister_mutation() { _rpc->unregister_handler(netw::messaging_verb::MUTATION); } future<> messaging_service::send_mutation(msg_addr id, clock_type::time_point timeout, const frozen_mutation& fm, std::vector forward, inet_address reply_to, unsigned shard, response_id_type response_id, std::optional trace_info) { return send_message_oneway_timeout(this, timeout, messaging_verb::MUTATION, std::move(id), fm, std::move(forward), std::move(reply_to), std::move(shard), std::move(response_id), std::move(trace_info)); } void messaging_service::register_counter_mutation(std::function (const rpc::client_info&, rpc::opt_time_point, std::vector fms, db::consistency_level cl, std::optional trace_info)>&& func) { register_handler(this, netw::messaging_verb::COUNTER_MUTATION, std::move(func)); } void messaging_service::unregister_counter_mutation() { _rpc->unregister_handler(netw::messaging_verb::COUNTER_MUTATION); } future<> messaging_service::send_counter_mutation(msg_addr id, clock_type::time_point timeout, std::vector fms, db::consistency_level cl, std::optional trace_info) { return send_message_timeout(this, messaging_verb::COUNTER_MUTATION, std::move(id), timeout, std::move(fms), cl, std::move(trace_info)); } void messaging_service::register_mutation_done(std::function (const rpc::client_info& cinfo, unsigned shard, response_id_type response_id, rpc::optional backlog)>&& func) { register_handler(this, netw::messaging_verb::MUTATION_DONE, std::move(func)); } void messaging_service::unregister_mutation_done() { _rpc->unregister_handler(netw::messaging_verb::MUTATION_DONE); } future<> messaging_service::send_mutation_done(msg_addr id, unsigned shard, response_id_type response_id, db::view::update_backlog backlog) { return send_message_oneway(this, messaging_verb::MUTATION_DONE, std::move(id), std::move(shard), std::move(response_id), std::move(backlog)); } void messaging_service::register_mutation_failed(std::function (const rpc::client_info& cinfo, unsigned shard, response_id_type response_id, size_t num_failed, rpc::optional backlog)>&& func) { register_handler(this, netw::messaging_verb::MUTATION_FAILED, std::move(func)); } void messaging_service::unregister_mutation_failed() { _rpc->unregister_handler(netw::messaging_verb::MUTATION_FAILED); } future<> messaging_service::send_mutation_failed(msg_addr id, unsigned shard, response_id_type response_id, size_t num_failed, db::view::update_backlog backlog) { return send_message_oneway(this, messaging_verb::MUTATION_FAILED, std::move(id), std::move(shard), std::move(response_id), num_failed, std::move(backlog)); } void messaging_service::register_read_data(std::function>, cache_temperature> (const rpc::client_info&, rpc::opt_time_point t, query::read_command cmd, ::compat::wrapping_partition_range pr, rpc::optional oda)>&& func) { register_handler(this, netw::messaging_verb::READ_DATA, std::move(func)); } void messaging_service::unregister_read_data() { _rpc->unregister_handler(netw::messaging_verb::READ_DATA); } future> messaging_service::send_read_data(msg_addr id, clock_type::time_point timeout, const query::read_command& cmd, const dht::partition_range& pr, query::digest_algorithm da) { return send_message_timeout>>(this, messaging_verb::READ_DATA, std::move(id), timeout, cmd, pr, da); } void messaging_service::register_get_schema_version(std::function(unsigned, table_schema_version)>&& func) { register_handler(this, netw::messaging_verb::GET_SCHEMA_VERSION, std::move(func)); } void messaging_service::unregister_get_schema_version() { _rpc->unregister_handler(netw::messaging_verb::GET_SCHEMA_VERSION); } future messaging_service::send_get_schema_version(msg_addr dst, table_schema_version v) { return send_message(this, messaging_verb::GET_SCHEMA_VERSION, dst, static_cast(dst.cpu_id), v); } void messaging_service::register_schema_check(std::function()>&& func) { register_handler(this, netw::messaging_verb::SCHEMA_CHECK, std::move(func)); } void messaging_service::unregister_schema_check() { _rpc->unregister_handler(netw::messaging_verb::SCHEMA_CHECK); } future messaging_service::send_schema_check(msg_addr dst) { return send_message(this, netw::messaging_verb::SCHEMA_CHECK, dst); } void messaging_service::register_read_mutation_data(std::function>, cache_temperature> (const rpc::client_info&, rpc::opt_time_point t, query::read_command cmd, ::compat::wrapping_partition_range pr)>&& func) { register_handler(this, netw::messaging_verb::READ_MUTATION_DATA, std::move(func)); } void messaging_service::unregister_read_mutation_data() { _rpc->unregister_handler(netw::messaging_verb::READ_MUTATION_DATA); } future> messaging_service::send_read_mutation_data(msg_addr id, clock_type::time_point timeout, const query::read_command& cmd, const dht::partition_range& pr) { return send_message_timeout>>(this, messaging_verb::READ_MUTATION_DATA, std::move(id), timeout, cmd, pr); } void messaging_service::register_read_digest(std::function (const rpc::client_info&, rpc::opt_time_point timeout, query::read_command cmd, ::compat::wrapping_partition_range pr, rpc::optional oda)>&& func) { register_handler(this, netw::messaging_verb::READ_DIGEST, std::move(func)); } void messaging_service::unregister_read_digest() { _rpc->unregister_handler(netw::messaging_verb::READ_DIGEST); } future, rpc::optional> messaging_service::send_read_digest(msg_addr id, clock_type::time_point timeout, const query::read_command& cmd, const dht::partition_range& pr, query::digest_algorithm da) { return send_message_timeout, rpc::optional>>(this, netw::messaging_verb::READ_DIGEST, std::move(id), timeout, cmd, pr, da); } // Wrapper for TRUNCATE void messaging_service::register_truncate(std::function (sstring, sstring)>&& func) { register_handler(this, netw::messaging_verb::TRUNCATE, std::move(func)); } void messaging_service::unregister_truncate() { _rpc->unregister_handler(netw::messaging_verb::TRUNCATE); } future<> messaging_service::send_truncate(msg_addr id, std::chrono::milliseconds timeout, sstring ks, sstring cf) { return send_message_timeout(this, netw::messaging_verb::TRUNCATE, std::move(id), std::move(timeout), std::move(ks), std::move(cf)); } // Wrapper for REPLICATION_FINISHED void messaging_service::register_replication_finished(std::function (inet_address)>&& func) { register_handler(this, messaging_verb::REPLICATION_FINISHED, std::move(func)); } void messaging_service::unregister_replication_finished() { _rpc->unregister_handler(messaging_verb::REPLICATION_FINISHED); } future<> messaging_service::send_replication_finished(msg_addr id, inet_address from) { // FIXME: getRpcTimeout : conf.request_timeout_in_ms return send_message_timeout(this, messaging_verb::REPLICATION_FINISHED, std::move(id), 10000ms, std::move(from)); } // Wrapper for REPAIR_CHECKSUM_RANGE void messaging_service::register_repair_checksum_range( std::function (sstring keyspace, sstring cf, dht::token_range range, rpc::optional hash_version)>&& f) { register_handler(this, messaging_verb::REPAIR_CHECKSUM_RANGE, std::move(f)); } void messaging_service::unregister_repair_checksum_range() { _rpc->unregister_handler(messaging_verb::REPAIR_CHECKSUM_RANGE); } future messaging_service::send_repair_checksum_range( msg_addr id, sstring keyspace, sstring cf, ::dht::token_range range, repair_checksum hash_version) { return send_message(this, messaging_verb::REPAIR_CHECKSUM_RANGE, std::move(id), std::move(keyspace), std::move(cf), std::move(range), hash_version); } // Wrapper for REPAIR_GET_FULL_ROW_HASHES void messaging_service::register_repair_get_full_row_hashes(std::function> (const rpc::client_info& cinfo, uint32_t repair_meta_id)>&& func) { register_handler(this, messaging_verb::REPAIR_GET_FULL_ROW_HASHES, std::move(func)); } void messaging_service::unregister_repair_get_full_row_hashes() { _rpc->unregister_handler(messaging_verb::REPAIR_GET_FULL_ROW_HASHES); } future> messaging_service::send_repair_get_full_row_hashes(msg_addr id, uint32_t repair_meta_id) { return send_message>>(this, messaging_verb::REPAIR_GET_FULL_ROW_HASHES, std::move(id), repair_meta_id); } // Wrapper for REPAIR_GET_COMBINED_ROW_HASH void messaging_service::register_repair_get_combined_row_hash(std::function (const rpc::client_info& cinfo, uint32_t repair_meta_id, std::optional common_sync_boundary)>&& func) { register_handler(this, messaging_verb::REPAIR_GET_COMBINED_ROW_HASH, std::move(func)); } void messaging_service::unregister_repair_get_combined_row_hash() { _rpc->unregister_handler(messaging_verb::REPAIR_GET_COMBINED_ROW_HASH); } future messaging_service::send_repair_get_combined_row_hash(msg_addr id, uint32_t repair_meta_id, std::optional common_sync_boundary) { return send_message>(this, messaging_verb::REPAIR_GET_COMBINED_ROW_HASH, std::move(id), repair_meta_id, std::move(common_sync_boundary)); } void messaging_service::register_repair_get_sync_boundary(std::function (const rpc::client_info& cinfo, uint32_t repair_meta_id, std::optional skipped_sync_boundary)>&& func) { register_handler(this, messaging_verb::REPAIR_GET_SYNC_BOUNDARY, std::move(func)); } void messaging_service::unregister_repair_get_sync_boundary() { _rpc->unregister_handler(messaging_verb::REPAIR_GET_SYNC_BOUNDARY); } future messaging_service::send_repair_get_sync_boundary(msg_addr id, uint32_t repair_meta_id, std::optional skipped_sync_boundary) { return send_message>(this, messaging_verb::REPAIR_GET_SYNC_BOUNDARY, std::move(id), repair_meta_id, std::move(skipped_sync_boundary)); } // Wrapper for REPAIR_GET_ROW_DIFF void messaging_service::register_repair_get_row_diff(std::function (const rpc::client_info& cinfo, uint32_t repair_meta_id, std::unordered_set set_diff, bool needs_all_rows)>&& func) { register_handler(this, messaging_verb::REPAIR_GET_ROW_DIFF, std::move(func)); } void messaging_service::unregister_repair_get_row_diff() { _rpc->unregister_handler(messaging_verb::REPAIR_GET_ROW_DIFF); } future messaging_service::send_repair_get_row_diff(msg_addr id, uint32_t repair_meta_id, std::unordered_set set_diff, bool needs_all_rows) { return send_message>(this, messaging_verb::REPAIR_GET_ROW_DIFF, std::move(id), repair_meta_id, std::move(set_diff), needs_all_rows); } // Wrapper for REPAIR_PUT_ROW_DIFF void messaging_service::register_repair_put_row_diff(std::function (const rpc::client_info& cinfo, uint32_t repair_meta_id, repair_rows_on_wire row_diff)>&& func) { register_handler(this, messaging_verb::REPAIR_PUT_ROW_DIFF, std::move(func)); } void messaging_service::unregister_repair_put_row_diff() { _rpc->unregister_handler(messaging_verb::REPAIR_PUT_ROW_DIFF); } future<> messaging_service::send_repair_put_row_diff(msg_addr id, uint32_t repair_meta_id, repair_rows_on_wire row_diff) { return send_message(this, messaging_verb::REPAIR_PUT_ROW_DIFF, std::move(id), repair_meta_id, std::move(row_diff)); } // Wrapper for REPAIR_ROW_LEVEL_START void messaging_service::register_repair_row_level_start(std::function (const rpc::client_info& cinfo, uint32_t repair_meta_id, sstring keyspace_name, sstring cf_name, dht::token_range range, row_level_diff_detect_algorithm algo, uint64_t max_row_buf_size, uint64_t seed, unsigned remote_shard, unsigned remote_shard_count, unsigned remote_ignore_msb, sstring remote_partitioner_name)>&& func) { register_handler(this, messaging_verb::REPAIR_ROW_LEVEL_START, std::move(func)); } void messaging_service::unregister_repair_row_level_start() { _rpc->unregister_handler(messaging_verb::REPAIR_ROW_LEVEL_START); } future<> messaging_service::send_repair_row_level_start(msg_addr id, uint32_t repair_meta_id, sstring keyspace_name, sstring cf_name, dht::token_range range, row_level_diff_detect_algorithm algo, uint64_t max_row_buf_size, uint64_t seed, unsigned remote_shard, unsigned remote_shard_count, unsigned remote_ignore_msb, sstring remote_partitioner_name) { return send_message(this, messaging_verb::REPAIR_ROW_LEVEL_START, std::move(id), repair_meta_id, std::move(keyspace_name), std::move(cf_name), std::move(range), algo, max_row_buf_size, seed, remote_shard, remote_shard_count, remote_ignore_msb, std::move(remote_partitioner_name)); } // Wrapper for REPAIR_ROW_LEVEL_STOP void messaging_service::register_repair_row_level_stop(std::function (const rpc::client_info& cinfo, uint32_t repair_meta_id, sstring keyspace_name, sstring cf_name, dht::token_range range)>&& func) { register_handler(this, messaging_verb::REPAIR_ROW_LEVEL_STOP, std::move(func)); } void messaging_service::unregister_repair_row_level_stop() { _rpc->unregister_handler(messaging_verb::REPAIR_ROW_LEVEL_STOP); } future<> messaging_service::send_repair_row_level_stop(msg_addr id, uint32_t repair_meta_id, sstring keyspace_name, sstring cf_name, dht::token_range range) { return send_message(this, messaging_verb::REPAIR_ROW_LEVEL_STOP, std::move(id), repair_meta_id, std::move(keyspace_name), std::move(cf_name), std::move(range)); } // Wrapper for REPAIR_GET_ESTIMATED_PARTITIONS void messaging_service::register_repair_get_estimated_partitions(std::function (const rpc::client_info& cinfo, uint32_t repair_meta_id)>&& func) { register_handler(this, messaging_verb::REPAIR_GET_ESTIMATED_PARTITIONS, std::move(func)); } void messaging_service::unregister_repair_get_estimated_partitions() { _rpc->unregister_handler(messaging_verb::REPAIR_GET_ESTIMATED_PARTITIONS); } future messaging_service::send_repair_get_estimated_partitions(msg_addr id, uint32_t repair_meta_id) { return send_message>(this, messaging_verb::REPAIR_GET_ESTIMATED_PARTITIONS, std::move(id), repair_meta_id); } // Wrapper for REPAIR_SET_ESTIMATED_PARTITIONS void messaging_service::register_repair_set_estimated_partitions(std::function (const rpc::client_info& cinfo, uint32_t repair_meta_id, uint64_t estimated_partitions)>&& func) { register_handler(this, messaging_verb::REPAIR_SET_ESTIMATED_PARTITIONS, std::move(func)); } void messaging_service::unregister_repair_set_estimated_partitions() { _rpc->unregister_handler(messaging_verb::REPAIR_SET_ESTIMATED_PARTITIONS); } future<> messaging_service::send_repair_set_estimated_partitions(msg_addr id, uint32_t repair_meta_id, uint64_t estimated_partitions) { return send_message(this, messaging_verb::REPAIR_SET_ESTIMATED_PARTITIONS, std::move(id), repair_meta_id, estimated_partitions); } // Wrapper for REPAIR_GET_DIFF_ALGORITHMS void messaging_service::register_repair_get_diff_algorithms(std::function> (const rpc::client_info& cinfo)>&& func) { register_handler(this, messaging_verb::REPAIR_GET_DIFF_ALGORITHMS, std::move(func)); } void messaging_service::unregister_repair_get_diff_algorithms() { _rpc->unregister_handler(messaging_verb::REPAIR_GET_DIFF_ALGORITHMS); } future> messaging_service::send_repair_get_diff_algorithms(msg_addr id) { return send_message>>(this, messaging_verb::REPAIR_GET_DIFF_ALGORITHMS, std::move(id)); } } // namespace net