@@ -4073,46 +4073,46 @@ def callback(pool, errors):
40734073 else :
40744074 with host .lock :
40754075 endpoint_changed = not self ._endpoints_match (host .endpoint , creation_endpoint )
4076- if endpoint_changed :
4077- log .debug (
4078- "Discarding stale connection pool for host %s; endpoint changed from %s" ,
4079- host , creation_endpoint )
4080- self ._invalidate_pool_creation (host , expected_endpoint = creation_endpoint )
4081- discard_pool = True
4082- else :
4083- # Rebuild by identity so endpoint hash changes do not
4084- # leave stale pool entries behind.
4085- retained_pools = {}
4086- for pool_host , host_pool in self ._pools .items ():
4087- if pool_host is host :
4088- previous_pools .append (host_pool )
4089- else :
4090- retained_pools [pool_host ] = host_pool
4091-
4092- # Keep the current metadata host keyed by identity.
4093- metadata_host = host
4094- if isinstance (self .cluster .metadata , Metadata ):
4095- metadata_host = self .cluster .metadata .get_host_by_host_id (host .host_id )
4096-
4097- target_host = metadata_host if metadata_host is not None else host
4098- target_host_matches = False
4099- for pool_host in tuple (retained_pools ):
4100- if pool_host is target_host :
4101- target_host_matches = True
4102- elif pool_host == target_host :
4103- previous_pools .append (retained_pools .pop (pool_host ))
4104-
4105- if target_host_matches :
4106- reuse_existing_pool = True
4076+ if endpoint_changed :
4077+ log .debug (
4078+ "Discarding stale connection pool for host %s; endpoint changed from %s" ,
4079+ host , creation_endpoint )
4080+ self ._invalidate_pool_creation (host , expected_endpoint = creation_endpoint )
4081+ discard_pool = True
41074082 else :
4108- source_host = new_pool .host
4109- if (source_host is not target_host and
4110- target_host .sharding_info is None ):
4111- target_host .sharding_info = source_host .sharding_info
4112- new_pool .host = target_host
4113- retained_pools [target_host ] = new_pool
4114- self ._pools = retained_pools
4115- self ._clear_pool_creation (host , creation_epoch )
4083+ # Rebuild by identity so endpoint hash changes do not
4084+ # leave stale pool entries behind.
4085+ retained_pools = {}
4086+ for pool_host , host_pool in self ._pools .items ():
4087+ if pool_host is host :
4088+ previous_pools .append (host_pool )
4089+ else :
4090+ retained_pools [pool_host ] = host_pool
4091+
4092+ # Keep the current metadata host keyed by identity.
4093+ metadata_host = host
4094+ if isinstance (self .cluster .metadata , Metadata ):
4095+ metadata_host = self .cluster .metadata .get_host_by_host_id (host .host_id )
4096+
4097+ target_host = metadata_host if metadata_host is not None else host
4098+ target_host_matches = False
4099+ for pool_host in tuple (retained_pools ):
4100+ if pool_host is target_host :
4101+ target_host_matches = True
4102+ elif pool_host == target_host :
4103+ previous_pools .append (retained_pools .pop (pool_host ))
4104+
4105+ if target_host_matches :
4106+ reuse_existing_pool = True
4107+ else :
4108+ source_host = new_pool .host
4109+ if (source_host is not target_host and
4110+ target_host .sharding_info is None ):
4111+ target_host .sharding_info = source_host .sharding_info
4112+ new_pool .host = target_host
4113+ retained_pools [target_host ] = new_pool
4114+ self ._pools = retained_pools
4115+ self ._clear_pool_creation (host , creation_epoch )
41164116
41174117 if reuse_existing_pool :
41184118 log .debug ("Reusing existing connection pool for host %s" , host )
@@ -4141,7 +4141,13 @@ def callback(pool, errors):
41414141 with self ._lock :
41424142 state = self ._get_pool_creation_state (host )
41434143 if state .creation_epoch is not None :
4144- return state .future
4144+ with host .lock :
4145+ endpoint_changed = not self ._endpoints_match (
4146+ host .endpoint , state .endpoint )
4147+ if not endpoint_changed :
4148+ return state .future
4149+ self ._invalidate_pool_creation (
4150+ host , expected_endpoint = state .endpoint )
41454151
41464152 creation_epoch = state .advance ()
41474153 state .creation_epoch = creation_epoch
@@ -4156,7 +4162,8 @@ def callback(pool, errors):
41564162 state .future = future
41574163 return future
41584164
4159- def remove_pool (self , host , expected_host = None , expected_endpoint = None ):
4165+ def remove_pool (self , host , expected_host = None , expected_endpoint = None ,
4166+ expected_pool = None ):
41604167 removed_pools = []
41614168 cleanup_context = _POOL_CLEANUP_EPOCH .get ()
41624169 with self ._lock :
@@ -4187,11 +4194,12 @@ def remove_pool(self, host, expected_host=None, expected_endpoint=None):
41874194 if expected_endpoint is not None :
41884195 remove_all = self ._endpoints_match (host .endpoint , expected_endpoint )
41894196
4190- if remove_all :
4191- self ._invalidate_pool_creation (host )
4192- else :
4193- self ._invalidate_pool_creation (
4194- host , expected_endpoint = expected_endpoint )
4197+ if expected_pool is None :
4198+ if remove_all :
4199+ self ._invalidate_pool_creation (host )
4200+ else :
4201+ self ._invalidate_pool_creation (
4202+ host , expected_endpoint = expected_endpoint )
41954203
41964204 retained_pools = {}
41974205 for pool_host , host_pool in self ._pools .items ():
@@ -4201,7 +4209,8 @@ def remove_pool(self, host, expected_host=None, expected_endpoint=None):
42014209
42024210 matches = self ._pool_matches_expected (
42034211 host_pool , expected_host = expected_host ,
4204- expected_endpoint = None if remove_all else expected_endpoint )
4212+ expected_endpoint = None if remove_all else expected_endpoint ,
4213+ expected_pool = expected_pool )
42054214 if matches :
42064215 removed_pools .append (host_pool )
42074216 else :
@@ -4230,7 +4239,10 @@ def _shutdown_removed_pools(pools):
42304239 for pool in pools :
42314240 pool .shutdown ()
42324241
4233- def _pool_matches_expected (self , pool , expected_host = None , expected_endpoint = None ):
4242+ def _pool_matches_expected (self , pool , expected_host = None ,
4243+ expected_endpoint = None , expected_pool = None ):
4244+ if expected_pool is not None and pool is not expected_pool :
4245+ return False
42344246 if expected_host is not None and pool .host is not expected_host :
42354247 return False
42364248 if expected_endpoint is not None :
0 commit comments