Skip to content

Commit ab79ee4

Browse files
committed
lifecycle: adapt management producers and synchronous APIs
1 parent 98078d5 commit ab79ee4

7 files changed

Lines changed: 839 additions & 77 deletions

File tree

‎cassandra/_lifecycle.py‎

Lines changed: 286 additions & 47 deletions
Large diffs are not rendered by default.

‎cassandra/cluster.py‎

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ def _profiles_without_explicit_lbps(self):
688688
for n in names
689689
)
690690

691+
@_lifecycle.policy_call
691692
def distance(self, host):
692693
if not self.pools_allowed:
693694
return HostDistance.IGNORED
@@ -697,14 +698,17 @@ def distance(self, host):
697698
HostDistance.REMOTE if HostDistance.REMOTE in distances else \
698699
HostDistance.IGNORED
699700

701+
@_lifecycle.policy_call
700702
def populate(self, cluster, hosts):
701703
for p in self.profiles.values():
702704
p.load_balancing_policy.populate(cluster, hosts)
703705

706+
@_lifecycle.policy_call
704707
def check_supported(self):
705708
for p in self.profiles.values():
706709
p.load_balancing_policy.check_supported()
707710

711+
@_lifecycle.policy_call
708712
def on_up(self, host):
709713
if not self.pools_allowed:
710714
return
@@ -713,6 +717,7 @@ def on_up(self, host):
713717
return
714718
p.load_balancing_policy.on_up(host)
715719

720+
@_lifecycle.policy_call
716721
def on_down(self, host):
717722
if not self.pools_allowed:
718723
return
@@ -721,12 +726,14 @@ def on_down(self, host):
721726
if host._is_removed:
722727
return
723728

729+
@_lifecycle.policy_call
724730
def on_add(self, host):
725731
for p in self.profiles.values():
726732
if host._is_removed:
727733
return
728734
p.load_balancing_policy.on_add(host)
729735

736+
@_lifecycle.policy_call
730737
def on_remove(self, host):
731738
for p in self.profiles.values():
732739
p.load_balancing_policy.on_remove(host)
@@ -1762,6 +1769,10 @@ def __init__(self,
17621769
schema_metadata_enabled, token_metadata_enabled,
17631770
schema_meta_page_size=schema_metadata_page_size)
17641771

1772+
@staticmethod
1773+
def _management_start_allowed():
1774+
return not _cluster_scheduler_shutdown_started
1775+
17651776
def _resolve_hostnames(self):
17661777
raw_contact_points = []
17671778
for cp in [cp for cp in self.contact_points if not isinstance(cp, EndPoint)]:
@@ -2082,13 +2093,15 @@ def protocol_downgrade(self, host_endpoint, previous_version):
20822093
"https://python-driver.docs.scylladb.com/stable/api/cassandra/cluster.html#cassandra.cluster.Cluster.protocol_version", self.protocol_version, new_version, host_endpoint)
20832094
self.protocol_version = new_version
20842095

2096+
@_lifecycle.policy_call
20852097
def _populate_hosts(self):
20862098
self.profile_manager.populate(
20872099
weakref.proxy(self), self.metadata.all_hosts())
20882100
self.load_balancing_policy.populate(
20892101
weakref.proxy(self), self.metadata.all_hosts()
20902102
)
20912103

2104+
@_lifecycle.synchronous_api
20922105
def connect(self, keyspace=None, wait_for_all_pools=False):
20932106
"""
20942107
Creates and returns a new :class:`~.Session` object.
@@ -2100,6 +2113,7 @@ def connect(self, keyspace=None, wait_for_all_pools=False):
21002113
established or attempted. Default is `False`, which means it will return when the first
21012114
successful connection is established. Remaining pools are added asynchronously.
21022115
"""
2116+
_lifecycle.assert_synchronous_api(self.connection_class)
21032117
reject_late_connect = False
21042118
shutdown_rejected_cluster = False
21052119
with self._lock:
@@ -2205,6 +2219,14 @@ def shutdown(self):
22052219
else:
22062220
self.is_shutdown = True
22072221

2222+
if _lifecycle.managed_context(self):
2223+
try:
2224+
self._shutdown_future = _lifecycle.defer_cleanup(
2225+
self, partial(self._shutdown, wait_for_executor=False))
2226+
finally:
2227+
self._lifecycle.close()
2228+
return
2229+
22082230
# Once interpreter scheduler cleanup has begun, executor teardown is
22092231
# imminent and this call may itself be running on an executor worker.
22102232
self._shutdown(
@@ -2214,7 +2236,7 @@ def _shutdown(self, wait_for_executor):
22142236
"""Finish cleanup after shutdown has been claimed under ``self._lock``."""
22152237

22162238
self._lifecycle.close()
2217-
if self._lifecycle.delivering:
2239+
if self._lifecycle.delivering or _lifecycle.execution_context() is not None:
22182240
wait_for_executor = False
22192241

22202242
if self._idle_heartbeat:
@@ -2383,7 +2405,7 @@ def _on_up_prepared(self, host, generation):
23832405
if futures:
23842406
self._lifecycle.collect_futures(
23852407
futures, partial(self._on_up_pools_completed, host,
2386-
generation=generation))
2408+
generation=generation), host=host)
23872409
except Exception:
23882410
log.exception("Unexpected failure handling node %s being marked up:", host)
23892411
for future in futures:
@@ -2469,7 +2491,7 @@ def _start_reconnector(self, host, is_host_addition,
24692491
return reconnector
24702492

24712493
@run_in_executor
2472-
@_lifecycle.event
2494+
@_lifecycle.negative_event
24732495
def on_down_potentially_blocking(self, host, is_host_addition,
24742496
down_event_generation,
24752497
on_add_reconnection=None):
@@ -2841,9 +2863,9 @@ def pools_completed(futures_results):
28412863
addition_token=addition_token)
28422864
return
28432865

2844-
self._lifecycle.collect_futures(futures, pools_completed)
2866+
self._lifecycle.collect_futures(futures, pools_completed, host=host)
28452867

2846-
@_lifecycle.event
2868+
@_lifecycle.observation_event('readiness')
28472869
def _finalize_add(self, host, set_up=True, generation=None,
28482870
addition_token=None, advance_health=True):
28492871
if not self._lifecycle.mark_ready(
@@ -3133,6 +3155,7 @@ def get_control_connection_host(self):
31333155
connection = self.control_connection._connection
31343156
return self.control_connection._get_host_for_connection(connection)
31353157

3158+
@_lifecycle.synchronous_api
31363159
def refresh_schema_metadata(self, max_schema_agreement_wait=None):
31373160
"""
31383161
Synchronously refresh all schema metadata.
@@ -3146,51 +3169,61 @@ def refresh_schema_metadata(self, max_schema_agreement_wait=None):
31463169
31473170
An Exception is raised if schema refresh fails for any reason.
31483171
"""
3172+
_lifecycle.assert_synchronous_api(self.connection_class)
31493173
if not self.control_connection.refresh_schema(schema_agreement_wait=max_schema_agreement_wait, force=True):
31503174
raise DriverException("Schema metadata was not refreshed. See log for details.")
31513175

3176+
@_lifecycle.synchronous_api
31523177
def refresh_keyspace_metadata(self, keyspace, max_schema_agreement_wait=None):
31533178
"""
31543179
Synchronously refresh keyspace metadata. This applies to keyspace-level information such as replication
31553180
and durability settings. It does not refresh tables, types, etc. contained in the keyspace.
31563181
31573182
See :meth:`~.Cluster.refresh_schema_metadata` for description of ``max_schema_agreement_wait`` behavior
31583183
"""
3184+
_lifecycle.assert_synchronous_api(self.connection_class)
31593185
if not self.control_connection.refresh_schema(target_type=SchemaTargetType.KEYSPACE, keyspace=keyspace,
31603186
schema_agreement_wait=max_schema_agreement_wait, force=True):
31613187
raise DriverException("Keyspace metadata was not refreshed. See log for details.")
31623188

3189+
@_lifecycle.synchronous_api
31633190
def refresh_table_metadata(self, keyspace, table, max_schema_agreement_wait=None):
31643191
"""
31653192
Synchronously refresh table metadata. This applies to a table, and any triggers or indexes attached
31663193
to the table.
31673194
31683195
See :meth:`~.Cluster.refresh_schema_metadata` for description of ``max_schema_agreement_wait`` behavior
31693196
"""
3197+
_lifecycle.assert_synchronous_api(self.connection_class)
31703198
if not self.control_connection.refresh_schema(target_type=SchemaTargetType.TABLE, keyspace=keyspace, table=table,
31713199
schema_agreement_wait=max_schema_agreement_wait, force=True):
31723200
raise DriverException("Table metadata was not refreshed. See log for details.")
31733201

3202+
@_lifecycle.synchronous_api
31743203
def refresh_materialized_view_metadata(self, keyspace, view, max_schema_agreement_wait=None):
31753204
"""
31763205
Synchronously refresh materialized view metadata.
31773206
31783207
See :meth:`~.Cluster.refresh_schema_metadata` for description of ``max_schema_agreement_wait`` behavior
31793208
"""
3209+
_lifecycle.assert_synchronous_api(self.connection_class)
31803210
if not self.control_connection.refresh_schema(target_type=SchemaTargetType.TABLE, keyspace=keyspace, table=view,
31813211
schema_agreement_wait=max_schema_agreement_wait, force=True):
31823212
raise DriverException("View metadata was not refreshed. See log for details.")
31833213

3214+
@_lifecycle.synchronous_api
31843215
def refresh_user_type_metadata(self, keyspace, user_type, max_schema_agreement_wait=None):
31853216
"""
31863217
Synchronously refresh user defined type metadata.
31873218
31883219
See :meth:`~.Cluster.refresh_schema_metadata` for description of ``max_schema_agreement_wait`` behavior
31893220
"""
3221+
_lifecycle.assert_synchronous_api(self.connection_class)
31903222
if not self.control_connection.refresh_schema(target_type=SchemaTargetType.TYPE, keyspace=keyspace, type=user_type,
31913223
schema_agreement_wait=max_schema_agreement_wait, force=True):
31923224
raise DriverException("User Type metadata was not refreshed. See log for details.")
31933225

3226+
@_lifecycle.synchronous_api
31943227
def refresh_user_function_metadata(self, keyspace, function, max_schema_agreement_wait=None):
31953228
"""
31963229
Synchronously refresh user defined function metadata.
@@ -3199,10 +3232,12 @@ def refresh_user_function_metadata(self, keyspace, function, max_schema_agreemen
31993232
32003233
See :meth:`~.Cluster.refresh_schema_metadata` for description of ``max_schema_agreement_wait`` behavior
32013234
"""
3235+
_lifecycle.assert_synchronous_api(self.connection_class)
32023236
if not self.control_connection.refresh_schema(target_type=SchemaTargetType.FUNCTION, keyspace=keyspace, function=function,
32033237
schema_agreement_wait=max_schema_agreement_wait, force=True):
32043238
raise DriverException("User Function metadata was not refreshed. See log for details.")
32053239

3240+
@_lifecycle.synchronous_api
32063241
def refresh_user_aggregate_metadata(self, keyspace, aggregate, max_schema_agreement_wait=None):
32073242
"""
32083243
Synchronously refresh user defined aggregate metadata.
@@ -3211,10 +3246,12 @@ def refresh_user_aggregate_metadata(self, keyspace, aggregate, max_schema_agreem
32113246
32123247
See :meth:`~.Cluster.refresh_schema_metadata` for description of ``max_schema_agreement_wait`` behavior
32133248
"""
3249+
_lifecycle.assert_synchronous_api(self.connection_class)
32143250
if not self.control_connection.refresh_schema(target_type=SchemaTargetType.AGGREGATE, keyspace=keyspace, aggregate=aggregate,
32153251
schema_agreement_wait=max_schema_agreement_wait, force=True):
32163252
raise DriverException("User Aggregate metadata was not refreshed. See log for details.")
32173253

3254+
@_lifecycle.synchronous_api
32183255
def refresh_nodes(self, force_token_rebuild=False):
32193256
"""
32203257
Synchronously publish refreshed node-list and token metadata.
@@ -3229,6 +3266,7 @@ def refresh_nodes(self, force_token_rebuild=False):
32293266
32303267
An Exception is raised if node refresh fails for any reason.
32313268
"""
3269+
_lifecycle.assert_synchronous_api(self.connection_class)
32323270
if not self.control_connection.refresh_node_list_and_token_map(force_token_rebuild):
32333271
raise DriverException("Node list was not refreshed. See log for details.")
32343272

@@ -3553,12 +3591,15 @@ def default_serial_consistency_level(self, cl):
35533591
_pool_generations = None
35543592
_pool_intents = None
35553593
_pool_creation_work = None
3594+
# Bare Session.__new__ fixtures have no admitted runtime lifetime.
3595+
_management_runtime = None
35563596
_profile_manager = None
35573597
_metrics = None
35583598
_request_init_callbacks = None
35593599

35603600
def __init__(self, cluster, hosts, keyspace=None):
35613601
self.cluster = cluster
3602+
self._management_runtime = cluster._lifecycle._ensure_runtime()
35623603
self.hosts = hosts
35633604
self.keyspace = keyspace
35643605

@@ -4225,11 +4266,20 @@ def shutdown(self):
42254266
for future in initial_connect_futures
42264267
if future in pool_creation_work)
42274268

4269+
cleanup = partial(self._finish_shutdown, initial_connect_work,
4270+
initial_connect_futures)
4271+
if (_lifecycle.managed_context(self.cluster) and
4272+
not _lifecycle.performing_cleanup()):
4273+
self._shutdown_future = _lifecycle.defer_cleanup(self.cluster, cleanup)
4274+
return
4275+
cleanup()
4276+
4277+
def _finish_shutdown(self, initial_connect_work, initial_connect_futures):
42284278
# PYTHON-673. If shutdown was called shortly after session init, avoid
42294279
# a race by cancelling any initial connection attempts haven't started,
42304280
# then blocking on any that have.
42314281
for future in initial_connect_work:
4232-
future.cancel()
4282+
_cancel_and_notify(future)
42334283
wait_futures(initial_connect_work)
42344284

42354285
for future in initial_connect_futures:
@@ -4276,21 +4326,25 @@ def _pool_intents_locked(self):
42764326
return pool_intents
42774327

42784328
def _submit_pool_creation_work(self, completion, fn, *args, **kwargs):
4279-
"""Submit pool creation and retain its work behind its public proxy."""
4280-
with self._lock:
4281-
work_future = self.submit(fn, *args, **kwargs)
4282-
add_done_callback = (
4283-
getattr(work_future, 'add_done_callback', None)
4284-
if work_future is not None else None)
4329+
"""Retain one charged physical result behind each legacy pool proxy."""
4330+
def work():
4331+
with _lifecycle.physical_work_context():
4332+
return fn(*args, **kwargs)
4333+
4334+
with _lifecycle.compatibility_work(self._management_runtime) as attach:
4335+
with self._lock:
4336+
work_future = self.submit(work)
4337+
add_done_callback = (
4338+
getattr(work_future, 'add_done_callback', None)
4339+
if work_future is not None else None)
4340+
if callable(add_done_callback):
4341+
work_items = self._pool_creation_work
4342+
if work_items is None:
4343+
work_items = self._pool_creation_work = {}
4344+
work_items[completion] = work_future
4345+
attach(work_future)
42854346
if callable(add_done_callback):
4286-
work = self._pool_creation_work
4287-
if work is None:
4288-
work = self._pool_creation_work = {}
4289-
work[completion] = work_future
4290-
4291-
if callable(add_done_callback):
4292-
add_done_callback(partial(
4293-
self._pool_creation_work_done, completion))
4347+
add_done_callback(partial(self._pool_creation_work_done, completion))
42944348
return work_future
42954349

42964350
def _pool_creation_work_done(self, completion, work_future):
@@ -5054,6 +5108,7 @@ def on_remove(self, host, trigger_reconciliation=True):
50545108
else:
50555109
self.remove_pool(host)
50565110

5111+
@_lifecycle.synchronous_api
50575112
def set_keyspace(self, keyspace):
50585113
"""
50595114
Set the default keyspace for all queries made through this Session.
@@ -5063,6 +5118,7 @@ def set_keyspace(self, keyspace):
50635118
:class:`.InvalidRequest` if another Session is using the shared
50645119
connection or another keyspace change is in progress.
50655120
"""
5121+
_lifecycle.assert_synchronous_api(self.cluster.connection_class)
50665122
self.execute('USE %s' % (protect_name(keyspace),))
50675123

50685124
def _set_keyspace_for_all_pools(self, keyspace, callback):

‎tests/unit/test_cluster.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2731,7 +2731,7 @@ def test_initialized_cluster_connect_after_scheduler_cleanup_is_rejected(self):
27312731
allow_control_connection_query_fallback=
27322732
ControlConnectionQueryFallback.SkipPoolCreation,
27332733
)
2734-
with patch.object(cluster.connection_class, 'initialize_reactor'), \
2734+
with patch.object(cluster.connection_class, 'initialize_reactor', wraps=cluster.connection_class.initialize_reactor), \
27352735
patch.object(cluster.control_connection, 'connect'), \
27362736
patch.object(cluster, '_populate_hosts'), \
27372737
patch.object(cluster.profile_manager, 'check_supported'):
@@ -2792,7 +2792,7 @@ def test_late_connect_does_not_join_reentrant_executor_shutdown(self):
27922792
allow_control_connection_query_fallback=
27932793
ControlConnectionQueryFallback.SkipPoolCreation,
27942794
)
2795-
with patch.object(cluster.connection_class, 'initialize_reactor'), \
2795+
with patch.object(cluster.connection_class, 'initialize_reactor', wraps=cluster.connection_class.initialize_reactor), \
27962796
patch.object(cluster.control_connection, 'connect'), \
27972797
patch.object(cluster, '_populate_hosts'), \
27982798
patch.object(cluster.profile_manager, 'check_supported'):
@@ -4130,7 +4130,7 @@ def shutdown_before_submit(fn, *args, **kwargs):
41304130
cluster.sessions = (session, live_session)
41314131
cluster._finalize_add = Mock()
41324132

4133-
def collect_futures(futures, callback):
4133+
def collect_futures(futures, callback, host=None):
41344134
callback([future.exception(timeout=5) or future.result(timeout=5)
41354135
for future in futures])
41364136

@@ -6692,7 +6692,7 @@ class _ResumableConnection(Connection):
66926692
supports_tls_session_resumption = True
66936693

66946694

6695-
class _NonResumableConnection(Connection):
6695+
class _NonResumableConnection(connection_class):
66966696
supports_tls_session_resumption = False
66976697

66986698

‎tests/unit/test_control_connection.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
ConstantReconnectionPolicy,
4848
ExponentialReconnectionPolicy,
4949
IdentityTranslator)
50+
from tests import connection_class
5051
from tests.unit.utils import new_session_with_pool_state
5152

5253
PEER_IP = "foobar"
@@ -161,6 +162,7 @@ def remove_host_by_host_id(self, host_id, endpoint=None):
161162

162163
class MockCluster(object):
163164

165+
connection_class = connection_class
164166
max_schema_agreement_wait = 5
165167
profile_manager = ProfileManager()
166168
reconnection_policy = ConstantReconnectionPolicy(2)

0 commit comments

Comments
 (0)