@@ -790,6 +790,7 @@ Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Setti
790
790
791
791
if (address.is_local )
792
792
info.local_addresses .push_back (address);
793
+ addresses_with_failover.emplace_back (Addresses ({address}));
793
794
794
795
auto pool = ConnectionPoolFactory::instance ().get (
795
796
static_cast <unsigned >(settings[Setting::distributed_connections_pool_size]),
@@ -832,27 +833,37 @@ Cluster::Cluster(Cluster::ReplicasAsShardsTag, const Cluster & from, const Setti
832
833
secret = from.secret ;
833
834
name = from.name ;
834
835
835
- if (max_hosts > 0 && shards_info.size () > max_hosts)
836
- {
837
- pcg64_fast gen{randomSeed ()};
838
- std::shuffle (shards_info.begin (), shards_info.end (), gen);
839
- shards_info.resize (max_hosts);
840
-
841
- shard_num = 0 ;
842
- for (auto & shard_info : shards_info)
843
- shard_info.shard_num = ++shard_num;
844
- }
836
+ constrainShardInfoAndAddressesToMaxHosts (max_hosts);
845
837
846
838
for (size_t i = 0 ; i < shards_info.size (); ++i)
847
- {
848
- addresses_with_failover.emplace_back (shards_info[i].local_addresses );
849
839
slot_to_shard.insert (std::end (slot_to_shard), shards_info[i].weight , i);
850
- }
851
840
852
841
initMisc ();
853
842
}
854
843
855
844
845
+ void Cluster::constrainShardInfoAndAddressesToMaxHosts (size_t max_hosts)
846
+ {
847
+ if (max_hosts == 0 || shards_info.size () <= max_hosts)
848
+ return ;
849
+
850
+ pcg64_fast gen{randomSeed ()};
851
+ std::shuffle (shards_info.begin (), shards_info.end (), gen);
852
+ shards_info.resize (max_hosts);
853
+
854
+ AddressesWithFailover addresses_with_failover_;
855
+
856
+ UInt32 shard_num = 0 ;
857
+ for (auto & shard_info : shards_info)
858
+ {
859
+ addresses_with_failover_.push_back (addresses_with_failover[shard_info.shard_num - 1 ]);
860
+ shard_info.shard_num = ++shard_num;
861
+ }
862
+
863
+ addresses_with_failover.swap (addresses_with_failover_);
864
+ }
865
+
866
+
856
867
Cluster::Cluster (Cluster::SubclusterTag, const Cluster & from, const std::vector<size_t > & indices)
857
868
{
858
869
for (size_t index : indices)
0 commit comments