Skip to content

Fix addresses_with_failover in cluster requests #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: antalya
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions src/Interpreters/Cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Setti

if (address.is_local)
info.local_addresses.push_back(address);
addresses_with_failover.emplace_back(Addresses({address}));

auto pool = ConnectionPoolFactory::instance().get(
static_cast<unsigned>(settings[Setting::distributed_connections_pool_size]),
Expand Down Expand Up @@ -832,27 +833,37 @@ Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Setti
secret = from.secret;
name = from.name;

if (max_hosts > 0 && shards_info.size() > max_hosts)
{
pcg64_fast gen{randomSeed()};
std::shuffle(shards_info.begin(), shards_info.end(), gen);
shards_info.resize(max_hosts);

shard_num = 0;
for (auto & shard_info : shards_info)
shard_info.shard_num = ++shard_num;
}
constrainShardInfoAndAddressesToMaxHosts(max_hosts);

for (size_t i = 0; i < shards_info.size(); ++i)
{
addresses_with_failover.emplace_back(shards_info[i].local_addresses);
slot_to_shard.insert(std::end(slot_to_shard), shards_info[i].weight, i);
}

initMisc();
}


void Cluster::constrainShardInfoAndAddressesToMaxHosts(size_t max_hosts)
{
if (max_hosts == 0 || shards_info.size() <= max_hosts)
return;

pcg64_fast gen{randomSeed()};
std::shuffle(shards_info.begin(), shards_info.end(), gen);
shards_info.resize(max_hosts);

AddressesWithFailover addresses_with_failover_;

UInt32 shard_num = 0;
for (auto & shard_info : shards_info)
{
addresses_with_failover_.push_back(addresses_with_failover[shard_info.shard_num - 1]);
shard_info.shard_num = ++shard_num;
}

addresses_with_failover.swap(addresses_with_failover_);
}


Cluster::Cluster(Cluster::SubclusterTag, const Cluster & from, const std::vector<size_t> & indices)
{
for (size_t index : indices)
Expand Down
3 changes: 3 additions & 0 deletions src/Interpreters/Cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ class Cluster
ShardInfoInsertPathForInternalReplication insert_paths = {},
bool internal_replication = false);

/// Reduce size of cluster to max_hosts
void constrainShardInfoAndAddressesToMaxHosts(size_t max_hosts);

/// Inter-server secret
String secret;

Expand Down
Loading