mirror of
https://github.com/scylladb/scylladb.git
synced 2026-04-23 01:50:35 +00:00
Replace std::vector<inet_address> with a small_vector of size 3 for replica sets (reflecting the common case of local reads, and the somewhat less common case of single-datacenter writes). Vectors used to describe topology changes are of size 1, reflecting that up to one node is usually involved with topology changes. At those counts and below we save an allocation; above those counts everything still works, but small_vector allocates like std::vector. In a few places we need to convert between std::vector and the new types, but these are all out of the hot paths (or are in a hot path, but behind a cache).
93 lines
3.1 KiB
C++
93 lines
3.1 KiB
C++
/*
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <algorithm>
|
|
#include "simple_strategy.hh"
|
|
#include "utils/class_registrator.hh"
|
|
#include <boost/algorithm/string.hpp>
|
|
#include "utils/sequenced_set.hh"
|
|
|
|
namespace locator {
|
|
|
|
simple_strategy::simple_strategy(const sstring& keyspace_name, const shared_token_metadata& token_metadata, snitch_ptr& snitch, const std::map<sstring, sstring>& config_options) :
|
|
abstract_replication_strategy(keyspace_name, token_metadata, snitch, config_options, replication_strategy_type::simple) {
|
|
for (auto& config_pair : config_options) {
|
|
auto& key = config_pair.first;
|
|
auto& val = config_pair.second;
|
|
|
|
if (boost::iequals(key, "replication_factor")) {
|
|
validate_replication_factor(val);
|
|
_replication_factor = std::stol(val);
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
inet_address_vector_replica_set simple_strategy::calculate_natural_endpoints(const token& t, const token_metadata& tm, can_yield can_yield) const {
|
|
const std::vector<token>& tokens = tm.sorted_tokens();
|
|
|
|
if (tokens.empty()) {
|
|
return inet_address_vector_replica_set();
|
|
}
|
|
|
|
size_t replicas = get_replication_factor();
|
|
utils::sequenced_set<inet_address> endpoints;
|
|
endpoints.reserve(replicas);
|
|
|
|
for (auto& token : tm.ring_range(t)) {
|
|
if (endpoints.size() == replicas) {
|
|
break;
|
|
}
|
|
if (can_yield) {
|
|
seastar::thread::maybe_yield();
|
|
}
|
|
auto ep = tm.get_endpoint(token);
|
|
assert(ep);
|
|
|
|
endpoints.push_back(*ep);
|
|
}
|
|
|
|
return boost::copy_range<inet_address_vector_replica_set>(endpoints.get_vector());
|
|
}
|
|
|
|
size_t simple_strategy::get_replication_factor() const {
|
|
return _replication_factor;
|
|
}
|
|
|
|
void simple_strategy::validate_options() const {
|
|
auto it = _config_options.find("replication_factor");
|
|
if (it == _config_options.end()) {
|
|
throw exceptions::configuration_exception("SimpleStrategy requires a replication_factor strategy option.");
|
|
}
|
|
validate_replication_factor(it->second);
|
|
}
|
|
|
|
std::optional<std::set<sstring>>simple_strategy::recognized_options() const {
|
|
return {{ "replication_factor" }};
|
|
}
|
|
|
|
using registry = class_registrator<abstract_replication_strategy, simple_strategy, const sstring&, const shared_token_metadata&, snitch_ptr&, const std::map<sstring, sstring>&>;
|
|
static registry registrator("org.apache.cassandra.locator.SimpleStrategy");
|
|
static registry registrator_short_name("SimpleStrategy");
|
|
|
|
}
|