Skip to content
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
107 changes: 107 additions & 0 deletions benchmarks/micro/bench_isinstance_dispatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Copyright ScyllaDB, Inc.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop the test

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Micro-benchmark: isinstance dispatch order in _create_response_future.

Measures the cost of checking BoundStatement first vs SimpleStatement first
in the isinstance chain that dispatches query types to message constructors.

For prepared-statement workloads (the perf-critical case), BoundStatement is
the most common type. Checking it first saves one wasted isinstance call.

Run:
python benchmarks/micro/bench_isinstance_dispatch.py
"""

import sys
import timeit

from cassandra.query import SimpleStatement, BoundStatement, BatchStatement, Statement


class _FakeGraphStatement(Statement):
"""Stand-in for GraphStatement to avoid importing DSE dependencies."""
pass


def make_bound_statement():
"""Create a minimal BoundStatement-like object for benchmarking."""
# We only need isinstance() to work; no actual prepared statement needed.
bs = object.__new__(BoundStatement)
return bs


def make_simple_statement():
return SimpleStatement("SELECT * FROM t")


def make_batch_statement():
return BatchStatement()


def bench():
bound = make_bound_statement()
simple = make_simple_statement()
batch = make_batch_statement()
graph = _FakeGraphStatement()

# Simulate typical workload mix: ~80% BoundStatement, ~15% SimpleStatement,
# ~4% BatchStatement, ~1% GraphStatement
queries = ([bound] * 80 + [simple] * 15 + [batch] * 4 + [graph] * 1)

def dispatch_simple_first():
"""Original order: SimpleStatement checked first."""
for q in queries:
if isinstance(q, SimpleStatement):
pass
elif isinstance(q, BoundStatement):
pass
elif isinstance(q, BatchStatement):
pass
elif isinstance(q, _FakeGraphStatement):
pass

def dispatch_bound_first():
"""Optimized order: BoundStatement checked first."""
for q in queries:
if isinstance(q, BoundStatement):
pass
elif isinstance(q, SimpleStatement):
pass
elif isinstance(q, BatchStatement):
pass
elif isinstance(q, _FakeGraphStatement):
pass

n = 200_000
t_simple_first = timeit.timeit(dispatch_simple_first, number=n)
t_bound_first = timeit.timeit(dispatch_bound_first, number=n)

total_calls = n * len(queries)
print(f"=== isinstance dispatch order (100 queries x {n} iters = {total_calls:,} dispatches) ===")
print(f"SimpleStatement first: {t_simple_first:.3f}s ({t_simple_first / total_calls * 1e9:.1f} ns/dispatch)")
print(f"BoundStatement first: {t_bound_first:.3f}s ({t_bound_first / total_calls * 1e9:.1f} ns/dispatch)")

if t_bound_first < t_simple_first:
speedup = t_simple_first / t_bound_first
saving_ns = (t_simple_first - t_bound_first) / total_calls * 1e9
print(f"Speedup: {speedup:.2f}x ({saving_ns:.1f} ns/dispatch saved)")
else:
print(f"No improvement (ratio: {t_simple_first / t_bound_first:.2f}x)")


if __name__ == "__main__":
print(f"Python {sys.version}")
bench()
66 changes: 42 additions & 24 deletions cassandra/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -3006,16 +3006,9 @@ def _create_response_future(self, query, parameters, trace, custom_payload,
if token_map is not None and metadata.can_support_partitioner():
routing_token = token_map.token_class.from_key(routing_key)

if isinstance(query, SimpleStatement):
query_string = query.query_string
statement_keyspace = query.keyspace if ProtocolVersion.uses_keyspace_flag(self._protocol_version) else None
if parameters:
query_string = bind_params(query_string, parameters, self.encoder)
message = QueryMessage(
query_string, cl, serial_cl,
fetch_size, paging_state, timestamp,
continuous_paging_options, statement_keyspace)
elif isinstance(query, BoundStatement):
if isinstance(query, BoundStatement):
# Check BoundStatement first: prepared-statement execution is the
# most common hot-path case, saving one isinstance() call (~15 ns).
Comment thread
mykaul marked this conversation as resolved.
prepared_statement = query.prepared_statement
# Snapshot metadata and its id as one atomic pair so the message never
# carries the id of one schema version alongside a skip_meta decision
Expand Down Expand Up @@ -3044,6 +3037,15 @@ def _create_response_future(self, query, parameters, trace, custom_payload,
continuous_paging_options=continuous_paging_options,
result_metadata_id=result_metadata_id,
tablet_version_block=self._compute_tablet_version_block(query, routing_key, routing_token))
elif isinstance(query, SimpleStatement):
query_string = query.query_string
statement_keyspace = query.keyspace if ProtocolVersion.uses_keyspace_flag(self._protocol_version) else None
if parameters:
query_string = bind_params(query_string, parameters, self.encoder)
message = QueryMessage(
query_string, cl, serial_cl,
fetch_size, paging_state, timestamp,
continuous_paging_options, statement_keyspace)
elif isinstance(query, BatchStatement):
if self._protocol_version < 2:
raise UnsupportedOperation(
Expand Down Expand Up @@ -4764,6 +4766,11 @@ def refresh_schema_and_set_result(control_conn, response_future, connection, **k
response_future._set_final_result(None)


# Singleton default so ResponseFuture.__init__ doesn't instantiate a fresh
# mutable RetryPolicy() per call (mutable-default-argument footgun).
_DEFAULT_RETRY_POLICY = RetryPolicy()


class ResponseFuture(object):
"""
An asynchronous response delivery mechanism that is returned from calls
Expand Down Expand Up @@ -4807,10 +4814,9 @@ class ResponseFuture(object):
session = None
row_factory = None
message = None
default_timeout = None
prepared_statement = None

_retry_policy = None
_profile_manager = None

_req_id = None
_final_result = _NOT_SET
Expand All @@ -4833,15 +4839,15 @@ class ResponseFuture(object):
_spec_execution_plan = NoSpeculativeExecutionPlan()
_continuous_paging_session = None
_host = None
_continuous_paging_state = None
_keyspace = None
_control_connection_query_attempted = False
_TABLET_ROUTING_CTYPE = None
_TABLET_ROUTING_V2_CTYPE = None
_bound_result_metadata = None

_warned_timeout = False

def __init__(self, session, message, query, timeout, metrics=None, prepared_statement=None,
retry_policy=RetryPolicy(), row_factory=None, load_balancer=None, start_time=None,
retry_policy=None, row_factory=None, load_balancer=None, start_time=None,
speculative_execution_plan=None, continuous_paging_state=None, host=None,
bound_result_metadata=_NOT_SET, routing_token=None):
self.session = session
Expand All @@ -4851,9 +4857,16 @@ def __init__(self, session, message, query, timeout, metrics=None, prepared_stat
self.message = message
self.query = query
self.timeout = timeout
self._retry_policy = retry_policy
self._metrics = metrics
self.prepared_statement = prepared_statement
# Snapshotted now, not re-read from session.keyspace when the response
# arrives: the session's keyspace can change mid-flight, and the tablet
# cached from the response payload must land under the keyspace the
# request was actually sent under (see _cache_tablet_from_payload).
self._keyspace = (query.keyspace if query is not None else None) or session.keyspace
self._retry_policy = retry_policy if retry_policy is not None else _DEFAULT_RETRY_POLICY
if metrics is not None:
self._metrics = metrics
if prepared_statement is not None:
self.prepared_statement = prepared_statement
# Metadata snapshotted alongside the message's result_metadata_id at construction
# time (see Session._create_response_future). Decoding a skip_meta response uses
# this so the metadata decoded-with always pairs with the id the message sent,
Expand All @@ -4862,7 +4875,8 @@ def __init__(self, session, message, query, timeout, metrics=None, prepared_stat
self._bound_result_metadata = [] if bound_result_metadata is _NOT_SET else bound_result_metadata
self._callback_lock = Lock()
self._start_time = start_time or time.time()
self._host = host
if host is not None:
self._host = host
self._routing_token = routing_token
self._control_connection_query_attempted = False
self._spec_execution_plan = speculative_execution_plan or self._spec_execution_plan
Expand All @@ -4873,7 +4887,8 @@ def __init__(self, session, message, query, timeout, metrics=None, prepared_stat
self._errbacks = []
self.attempted_hosts = []
self._start_timer()
self._continuous_paging_state = continuous_paging_state
if continuous_paging_state is not None:
self._continuous_paging_state = continuous_paging_state

@property
def _time_remaining(self):
Expand Down Expand Up @@ -5258,15 +5273,18 @@ def _cache_tablet_from_payload(self, payload_key, ctype):
layouts differ only by a trailing ``tablet_version`` field, and
``Tablet.from_row`` accepts that as an optional final argument, so
unpacking the decoded tuple positionally serves both. The tablet is
cached under the effective keyspace (the statement's, else the
session's) so a prepared statement executed in a session keyspace lands
cached under the effective keyspace snapshotted at __init__ time (the
statement's, else the session's keyspace as of when the request was
sent) so a prepared statement executed in a session keyspace lands
under the same key ``_compute_tablet_version_block`` looks it up by;
otherwise that lookup always misses.
otherwise that lookup always misses. Using the snapshot instead of
re-reading ``self.session.keyspace`` here avoids caching under the
wrong keyspace if it changed while the request was in flight.
"""
info = self._custom_payload.get(payload_key)
protocol = self.session.cluster.protocol_version
tablet = Tablet.from_row(*ctype.from_binary(info, protocol))
keyspace = self.query.keyspace or self.session.keyspace
keyspace = self._keyspace
table = self.query.table
if tablet and keyspace and table:
self.session.cluster.metadata._tablets.add_tablet(keyspace, table, tablet)
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/test_response_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,3 +1513,51 @@ def test_query_decodes_with_construction_snapshot_not_live_cache(self):
connection.send_msg.assert_called_once()
# _query decodes with the construction snapshot, not the mutated cache
assert connection.send_msg.call_args.kwargs['result_metadata'] is meta_v1

def test_cache_tablet_from_payload_uses_keyspace_snapshotted_at_construction(self):
"""
_cache_tablet_from_payload must cache under the keyspace in effect when the
request was sent, not whatever self.session.keyspace happens to be when the
response arrives. Otherwise a session.keyspace change that lands in between
would cache the tablet under the wrong keyspace.
"""
session = self.make_basic_session()
session.keyspace = 'ks_at_send_time'
session.cluster._default_load_balancing_policy.make_query_plan.return_value = ['ip1']
session._pools.get.return_value = self.make_pool()

query = SimpleStatement("SELECT * FROM foo")
message = QueryMessage(query=query, consistency_level=ConsistencyLevel.ONE)
rf = ResponseFuture(session, message, query, 1)

# keyspace changes on the session after construction, before the response
session.keyspace = 'ks_changed_mid_flight'

rf._custom_payload = {'tablets-routing-v1': b'payload'}
ctype = Mock()
ctype.from_binary.return_value = ('col', 'val')
with patch('cassandra.cluster.Tablet') as MockTablet:
fake_tablet = Mock()
MockTablet.from_row.return_value = fake_tablet
rf.query.table = 'tbl'
rf._cache_tablet_from_payload('tablets-routing-v1', ctype)

session.cluster.metadata._tablets.add_tablet.assert_called_once_with(
'ks_at_send_time', 'tbl', fake_tablet)

def test_init_keyspace_snapshot_handles_none_query(self):
"""
Session.prepare()/prepare_on_all_hosts() construct ResponseFuture with
query=None (there's no Statement yet, just a PrepareMessage). The keyspace
snapshot must fall back to session.keyspace in that case instead of raising
AttributeError on query.keyspace.
"""
session = self.make_basic_session()
session.keyspace = 'ks_from_session'
session.cluster._default_load_balancing_policy.make_query_plan.return_value = ['ip1']
session._pools.get.return_value = self.make_pool()

message = Mock()
rf = ResponseFuture(session, message, query=None, timeout=1)

assert rf._keyspace == 'ks_from_session'
Loading