Skip to content

DRIVER-153: negotiate and implement SCYLLA_USE_METADATA_ID extension#770

Open
nikagra wants to merge 2 commits into
scylladb:masterfrom
nikagra:driver-153-scylla-use-metadata-id
Open

DRIVER-153: negotiate and implement SCYLLA_USE_METADATA_ID extension#770
nikagra wants to merge 2 commits into
scylladb:masterfrom
nikagra:driver-153-scylla-use-metadata-id

Conversation

@nikagra

@nikagra nikagra commented Mar 26, 2026

Copy link
Copy Markdown

Summary

Implements the SCYLLA_USE_METADATA_ID Scylla CQL protocol extension (DRIVER-153), which backports the prepared-statement metadata-ID mechanism from CQL v5 to earlier protocol versions.

When the extension is negotiated:

  • The server includes a result metadata hash in the PREPARE response
  • The driver sends that hash back with every EXECUTE request, allowing the server to skip sending full result metadata on every response (skip_meta=True)
  • If the result schema has changed, the server sets the METADATA_CHANGED flag and includes the new metadata ID + new column metadata in the response — the driver picks this up and updates its cached metadata automatically

Changes

cassandra/protocol_features.py

  • Add USE_METADATA_ID = "SCYLLA_USE_METADATA_ID" constant and use_metadata_id field to ProtocolFeatures
  • Parse the extension from the SUPPORTED frame; include it in STARTUP when present

cassandra/protocol.py

  • Bug fix: _write_query_params now actually writes _SKIP_METADATA_FLAG on the wire — it was stored on _QueryMessage but never sent (effectively dead code)
  • recv_results_prepared: read result_metadata_id for Scylla extension (pre-v5) in addition to standard CQL v5+
  • ExecuteMessage: add use_metadata_id flag (default False); send_body gates the result_metadata_id field on ProtocolVersion.uses_prepared_metadata(protocol_version) or self.use_metadata_id — both paths unified. An empty sentinel (b'') is written when the hash is None (LWT / mixed cluster) so the frame layout is always correct and no TypeError crash on v5

cassandra/cluster.py

  • _create_response_future: build ExecuteMessage with safe defaults (skip_meta=False, result_metadata_id=None, use_metadata_id=False)
  • _query: after borrowing the connection, set can_skip_meta, skip_meta, result_metadata_id, and use_metadata_id based on the actual connection's negotiated features. skip_meta is only enabled when the prepared statement has both a result_metadata_id and usable cached result_metadata — guards against LWT/NO_METADATA statements and mixed-cluster scenarios
  • _set_result: on METADATA_CHANGED update prepared_statement.result_metadata then result_metadata_id in that order (safe write ordering for concurrent readers)

docs/scylla-specific.rst

  • Document the extension, its behaviour, and the reference to the ScyllaDB CQL extensions spec

Test plan

  • 19 new unit tests across 3 files covering: feature negotiation, STARTUP options, skip_meta flag encoding, metadata_id in ExecuteMessage (v4/v5, sentinel for None), PREPARE response decoding with/without extension, METADATA_CHANGED and NO_METADATA flag handling, _set_result METADATA_CHANGED path, and per-connection feature gating in _query (5 scenarios: extension on/off, metadata id present/absent, LWT, protocol v5)
  • Full unit test suite passes (57 passed in affected files; 627+ overall)
  • Integration tests against a Scylla node with the extension: verify that schema changes after PREPARE are detected and metadata is updated without re-preparation

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Implements negotiation and support for Scylla’s SCYLLA_USE_METADATA_ID protocol extension to enable metadata-id based skip_meta behavior (backporting CQL v5 prepared-statement metadata-id semantics to earlier protocol versions).

Changes:

  • Adds SCYLLA_USE_METADATA_ID parsing from SUPPORTED and includes it in STARTUP when negotiated.
  • Extends protocol encode/decode to read/write result_metadata_id for PREPARE/EXECUTE on pre-v5 when the extension is used, and fixes on-wire encoding of _SKIP_METADATA_FLAG.
  • Updates execution/result handling to conditionally use skip_meta and to refresh cached prepared metadata when the server reports metadata changes.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
cassandra/protocol_features.py Adds the SCYLLA_USE_METADATA_ID feature flag and includes it in negotiated STARTUP options.
cassandra/protocol.py Writes _SKIP_METADATA_FLAG in query params; adds pre-v5 extension handling for result_metadata_id in PREPARE/EXECUTE.
cassandra/cluster.py Adjusts when skip_meta is enabled and updates cached prepared metadata/id on METADATA_CHANGED responses.
tests/unit/test_protocol_features.py Adds unit tests for feature parsing and STARTUP option inclusion.
tests/unit/test_protocol.py Adds unit tests for skip-meta flag encoding and metadata-id handling in pre-v5 PREPARE/EXECUTE paths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cassandra/cluster.py Outdated
Comment thread cassandra/protocol.py Outdated
Comment on lines +648 to +649
elif self.result_metadata_id is not None:
write_string(f, self.result_metadata_id)

Copilot AI Mar 26, 2026

Copy link

Choose a reason for hiding this comment

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

ExecuteMessage.send_body() now writes result_metadata_id for protocol versions that don’t use standard prepared-metadata (pre-v5) whenever self.result_metadata_id is non-None. For pre-v5 this extra field is only valid when SCYLLA_USE_METADATA_ID was negotiated on that connection; otherwise it changes the wire layout (the server will interpret the metadata id bytes as the query parameters) and the request will fail.

To make this safe, ensure the decision to include result_metadata_id is gated by the negotiated feature (e.g., add an explicit use_metadata_id/send_result_metadata_id flag on the message that the caller sets based on connection.features.use_metadata_id, or guarantee centrally that result_metadata_id is cleared unless the extension is active for that connection).

Suggested change
elif self.result_metadata_id is not None:
write_string(f, self.result_metadata_id)

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed. The elif self.result_metadata_id is not None path in send_body is now only reached when the caller explicitly set the field — which only happens in _query() after confirming connection.features.use_metadata_id (or CQL v5). For any connection that didn't negotiate the extension, result_metadata_id remains None and the branch is never taken, so the wire layout is unaffected.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is still a problem here. If the result id feature is negotiated on the connection, then you need to ALWAYS send some result metadata id in EXECUTE. Skipping the write_string will result in a protocol error.
Your use_metadata_id may be False even if extension was negotiated, if the server decided to skip the metadata in PREPARED response. In such case, you'll skip writing the id here, and encounter protocol error.

Even if you fix this specific case, there is still possibility of mixed cluster, with some nodes supporting the extension. In that case result_metadata_id will be None, and if you send to a node that has the extension negotiated, you'll again not send the id and encounter protocol error.

To sum up: this serialization here should check if feature is negotiated, and base sending this field only on that.

@nikagra nikagra May 14, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

👨‍💻: Addressed your concerns

🤖:

Automated response

Fixed. Added a `use_metadata_id` boolean flag to `ExecuteMessage` (default `False`). `send_body` now gates the `result_metadata_id` field on `ProtocolVersion.uses_prepared_metadata(protocol_version) or self.use_metadata_id` — both branches unified — writing an empty sentinel (`b''`) when the hash is `None` rather than omitting the field or crashing.

In _query(), message.use_metadata_id = connection.features.use_metadata_id is set unconditionally after borrowing the connection, so the decision is always based on what was negotiated on that specific connection.

This also fixes a second issue: the old separate v5 branch would crash with TypeError when result_metadata_id is None (LWT statement on v5, or statement prepared before the extension was active in a mixed cluster). The unified sentinel handles that case correctly on both code paths.

Two new tests cover this: test_execute_message_scylla_metadata_id_none_writes_sentinel (extension active, None hash → sentinel on v4) and test_execute_message_v5_metadata_id_none_writes_sentinel (v5 native path, None hash → sentinel instead of crash).

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cassandra/cluster.py Outdated
@nikagra nikagra force-pushed the driver-153-scylla-use-metadata-id branch from ade35d8 to f42e225 Compare March 27, 2026 12:32
@mykaul

mykaul commented Mar 29, 2026

Copy link
Copy Markdown

I'm not sure where, but we should document this - with reference mainly to the scylladb docs about this feature.

@nikagra

nikagra commented Mar 30, 2026

Copy link
Copy Markdown
Author

@mykaul Documentation I'm aware of is MetadataId extension in CQLv4 Requirement Document

@nikagra nikagra requested a review from sylwiaszunejko April 9, 2026 11:12
@nikagra nikagra marked this pull request as ready for review April 9, 2026 21:30

@dkropachev dkropachev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

One blocking correctness issue below: skip_meta is being enabled for prepared statements that can still have empty/absent cached result metadata.

Comment thread cassandra/cluster.py Outdated
@nikagra nikagra force-pushed the driver-153-scylla-use-metadata-id branch from 6eea397 to a86fd53 Compare April 15, 2026 09:09
@nikagra nikagra requested a review from dkropachev April 15, 2026 09:12
@nikagra nikagra force-pushed the driver-153-scylla-use-metadata-id branch from 7ba5835 to a86fd53 Compare April 15, 2026 11:12
@nikagra nikagra force-pushed the driver-153-scylla-use-metadata-id branch from a86fd53 to 8880f03 Compare April 22, 2026 12:34
@nikagra nikagra force-pushed the driver-153-scylla-use-metadata-id branch from 170fd31 to 5fe1902 Compare May 14, 2026 14:45
@nikagra nikagra requested a review from Lorak-mmk May 14, 2026 14:45
@nikagra nikagra force-pushed the driver-153-scylla-use-metadata-id branch from fcd3eba to 5fe1902 Compare May 15, 2026 09:22
@nikagra nikagra force-pushed the driver-153-scylla-use-metadata-id branch from 5fe1902 to 251b1a8 Compare May 28, 2026 20:32
@coderabbitai

coderabbitai Bot commented May 28, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f8492cce-3eae-4bc9-b5d5-0fba97bdfcc6

📥 Commits

Reviewing files that changed from the base of the PR and between bfc9760 and d3300e2.

📒 Files selected for processing (7)
  • cassandra/cluster.py
  • cassandra/protocol.py
  • cassandra/protocol_features.py
  • docs/scylla-specific.rst
  • tests/unit/test_protocol.py
  • tests/unit/test_protocol_features.py
  • tests/unit/test_response_future.py
✅ Files skipped from review due to trivial changes (1)
  • docs/scylla-specific.rst
🚧 Files skipped from review as they are similar to previous changes (6)
  • cassandra/protocol_features.py
  • tests/unit/test_protocol_features.py
  • tests/unit/test_protocol.py
  • cassandra/protocol.py
  • tests/unit/test_response_future.py
  • cassandra/cluster.py

📝 Walkthrough

Walkthrough

This PR adds Scylla's SCYLLA_USE_METADATA_ID support: negotiates a new protocol feature, encodes/decodes result_metadata_id and skip_meta/use_metadata_id in QUERY/EXECUTE/PREPARE/RESULT messages, defers attaching metadata-id info until a connection is borrowed, and updates prepared-statement cached metadata when servers send new result_metadata_id. It includes docs and unit tests covering v4/v5 and the Scylla extension paths.

Sequence Diagram

sequenceDiagram
  participant Client
  participant Connection
  participant Server
  Client->>Connection: execute(prepared_statement)
  activate Connection
  Connection->>Connection: ResponseFuture._query(): check cached metadata<br/>& connection features
  alt has cached metadata & supports metadata-id
    Connection->>Server: ExecuteMessage(skip_meta=True, result_metadata_id=cached_id)
  else no cached metadata or unsupported
    Connection->>Server: ExecuteMessage(skip_meta=False, result_metadata_id=None)
  end
  activate Server
  alt metadata unchanged
    Server->>Connection: ResultMessage(result_metadata_id=cached_id, no column_metadata)
  else metadata changed
    Server->>Connection: ResultMessage(result_metadata_id=new_id, column_metadata=[...])
  end
  deactivate Server
  Connection->>Connection: ResponseFuture._set_result(): if result_metadata_id present,<br/>update prepared_statement metadata cache
  deactivate Connection
  Connection->>Client: return rows with cached metadata
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 65.96% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: negotiating and implementing the SCYLLA_USE_METADATA_ID protocol extension to support prepared statement metadata caching across all supported protocol versions.
Description check ✅ Passed The PR description comprehensively covers the changes: it explains the feature purpose, documents modifications to each affected file, provides a detailed test plan with 19 unit tests, and includes references to the specification. The description aligns well with the template requirements.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai

coderabbitai Bot commented May 28, 2026

Copy link
Copy Markdown

Actionable comments posted: 0

@nikagra nikagra force-pushed the driver-153-scylla-use-metadata-id branch 2 times, most recently from de8d3fc to bfc9760 Compare June 2, 2026 20:42
@nikagra

nikagra commented Jun 2, 2026

Copy link
Copy Markdown
Author

🤖: All review issues (1–9) have been addressed and the branch has been squashed to the required 2-commit shape. Requesting re-review from @Lorak-mmk and @dkropachev.

What changed since last review:

Production commit (8accdb5a) — no functional changes, two cleanups squashed in:

  • Issue 8 (docs scope claim): corrected docs/scylla-specific.rst to say the extension applies to EXECUTE requests, not PREPARE
  • Issue 9 (GIL comment): added a note in _set_result explaining why the metadata update write ordering is safe under the GIL

Test commit (bfc97602) — all 6 fix/addition commits squashed in, commit message fully replaced to enumerate every test:

  • Issue 1: test_query_no_skip_meta_without_extension fixture corrected (result_metadata=[] was falsy, defeating the assertion)
  • Issue 2: test_execute_after_prepare_updates_result_metadata_id and test_execute_after_prepare_no_metadata_id_in_response added to cover the _execute_after_prepare reprepare path
  • Issue 3: test_recv_results_metadata_no_metadata_flag_skips_metadata_id tightened — now asserts not hasattr(result, 'result_metadata_id') rather than is None, and checks column_metadata not result_metadata
  • Issue 4: test_recv_results_prepared_v5_reads_metadata_id added — covers the v5 native uses_prepared_metadata() decode path
  • Issue 5: test_execute_message_v5_skip_meta_sets_flag added — confirms _SKIP_METADATA_FLAG is correctly written into the 4-byte v5 flags word (this flag was dead code in upstream before this PR)
  • Issue 6: test_repeat_orig_query_after_succesful_reprepare fixed — result_metadata_id value changed from str to bytes; assertion that value is stored on prepared_statement added
  • Issue 7: test_set_result_warns_when_metadata_id_but_column_metadata_is_none added — covers the column_metadata=None (absent) variant of the METADATA_CHANGED warning path

CI on the 8-commit pre-squash branch: 18/19 passed; test libev (3.14t) was cancelled after a 6-hour runner stall (infrastructure timeout — test asyncio (3.14t) on the same commit passed cleanly).

Backport the prepared-statement metadata-ID mechanism from CQL v5 to
earlier protocol versions via the SCYLLA_USE_METADATA_ID Scylla extension.

protocol_features.py:
- Add USE_METADATA_ID constant and use_metadata_id field to ProtocolFeatures
- parse_from_supported: detect SCYLLA_USE_METADATA_ID in SUPPORTED options
- add_startup_options: echo the extension back in STARTUP when negotiated

protocol.py:
- _write_query_params: fix _SKIP_METADATA_FLAG — it was stored but never
  written on the wire (dead code); now correctly sets the flag in the frame
- recv_results_prepared: read result_metadata_id when the Scylla extension
  is active (pre-v5), in addition to the existing CQL v5 path
- ExecuteMessage: add use_metadata_id flag (default False); send_body gates
  the result_metadata_id field on uses_prepared_metadata(protocol_version)
  OR use_metadata_id, writing an empty sentinel (b'') when the hash is
  unavailable (LWT / mixed cluster) instead of omitting the field entirely
  or crashing with TypeError

cluster.py:
- _create_response_future: build ExecuteMessage with defaults (skip_meta=False,
  result_metadata_id=None); set these in _query() once the connection is known
- _query: after borrowing the connection set can_skip_meta, skip_meta,
  result_metadata_id, and use_metadata_id on the ExecuteMessage based on the
  actual connection's negotiated features; skip_meta is only enabled when the
  prepared statement has both a result_metadata_id AND cached result_metadata
  (guards against LWT/NO_METADATA statements and mixed-cluster re-prepare)
- _set_result: on METADATA_CHANGED (new result_metadata_id in EXECUTE response)
  update prepared_statement.result_metadata then result_metadata_id in that
  order — safe write ordering so a concurrent reader using the old id gets
  full metadata from the server while a reader seeing the new id has the
  correct cached metadata immediately

docs/scylla-specific.rst: document the extension and its behaviour
test_protocol_features.py:
- test_use_metadata_id_parsing: SCYLLA_USE_METADATA_ID parsed from SUPPORTED
- test_use_metadata_id_missing: use_metadata_id is False when key is absent
- test_use_metadata_id_startup_options: key present in STARTUP when negotiated
- test_use_metadata_id_not_in_startup_when_not_negotiated: absent otherwise

test_protocol.py -- ExecuteMessage wire encoding:
- test_execute_message_skip_meta_flag: _SKIP_METADATA_FLAG (0x02) written on v4
- test_execute_message_v5_skip_meta_sets_flag: _SKIP_METADATA_FLAG written in the
  4-byte v5 flags word; confirms the flag was dead code in upstream before this PR
- test_execute_message_scylla_metadata_id_v4: result_metadata_id written on v4
  when use_metadata_id=True (Scylla extension)
- test_execute_message_scylla_metadata_id_none_writes_sentinel: extension active
  but result_metadata_id=None writes empty sentinel b'' (LWT / mixed cluster)
- test_execute_message_v5_metadata_id_none_writes_sentinel: v5 with
  result_metadata_id=None writes empty sentinel instead of TypeError crash

test_protocol.py -- ResultMessage decoding:
- test_recv_results_prepared_scylla_extension_reads_metadata_id: result_metadata_id
  read from PREPARE response on v4 when extension is active
- test_recv_results_prepared_no_extension_skips_metadata_id: result_metadata_id
  not read on v4 without extension
- test_recv_results_prepared_v5_reads_metadata_id: result_metadata_id read on v5
  via the native uses_prepared_metadata() path (use_metadata_id=False)
- test_recv_results_metadata_changed_flag: _METADATA_ID_FLAG in ROWS response
  causes result_metadata_id to be read and stored
- test_recv_results_metadata_no_metadata_flag_skips_metadata_id: NO_METADATA early
  return leaves result_metadata_id unset (asserts not hasattr, not merely None)

test_response_future.py -- _set_result METADATA_CHANGED update path:
- test_set_result_updates_metadata_when_metadata_changed: both result_metadata and
  result_metadata_id updated when server sends a new metadata id
- test_set_result_does_not_update_metadata_when_metadata_id_absent: cached metadata
  untouched when response carries no new metadata id (normal skip-meta path)
- test_set_result_warns_when_metadata_id_but_no_column_metadata: warning emitted
  and result_metadata_id updated when new id arrives with empty column list
- test_set_result_warns_when_metadata_id_but_column_metadata_is_none: same as
  above but with column_metadata=None (absent) rather than []

test_response_future.py -- _execute_after_prepare reprepare path:
- test_repeat_orig_query_after_succesful_reprepare: fixed result_metadata_id
  type str->bytes; added assertion that value is stored on prepared_statement
- test_execute_after_prepare_updates_result_metadata_id: both result_metadata and
  result_metadata_id refreshed from reprepare response when extension is active
- test_execute_after_prepare_no_metadata_id_in_response: result_metadata_id left
  unchanged when reprepare response carries no metadata id

test_response_future.py -- _query per-connection feature gating (6 scenarios):
- test_query_sets_skip_meta_with_scylla_extension: skip_meta=True when connection
  has negotiated SCYLLA_USE_METADATA_ID and statement has cached metadata
- test_query_no_skip_meta_without_extension: skip_meta=False when use_metadata_id
  is False; fixture uses truthy result_metadata to isolate this guard correctly
- test_query_no_skip_meta_when_prepared_statement_has_no_metadata_id: skip_meta
  False when result_metadata_id=None (prepared before extension was active)
- test_query_sets_skip_meta_for_protocol_v5: skip_meta=True on v5 via the native
  uses_prepared_metadata() path (use_metadata_id=False)
- test_query_no_skip_meta_when_result_metadata_is_none: LWT guard -- skip_meta
  False when result_metadata=None despite extension being active
@nikagra nikagra force-pushed the driver-153-scylla-use-metadata-id branch from bfc9760 to d3300e2 Compare June 19, 2026 08:54
Comment thread cassandra/protocol.py
Comment on lines 628 to +636
class ExecuteMessage(_QueryMessage):
opcode = 0x0A
name = 'EXECUTE'

def __init__(self, query_id, query_params, consistency_level,
serial_consistency_level=None, fetch_size=None,
paging_state=None, timestamp=None, skip_meta=False,
continuous_paging_options=None, result_metadata_id=None):
continuous_paging_options=None, result_metadata_id=None,
use_metadata_id=False):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here ExecuteMessage got a new parameter, use_metadata_id, in addition to existing result_metadata_id

Comment thread cassandra/cluster.py
Comment on lines 3060 to 3066
elif isinstance(query, BoundStatement):
prepared_statement = query.prepared_statement
message = ExecuteMessage(
prepared_statement.query_id, query.values, cl,
serial_cl, fetch_size, paging_state, timestamp,
skip_meta=bool(prepared_statement.result_metadata),
continuous_paging_options=continuous_paging_options,
result_metadata_id=prepared_statement.result_metadata_id)
continuous_paging_options=continuous_paging_options)
elif isinstance(query, BatchStatement):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

But here you remove result_metadata_id and skip_meta from this call to ExecuteMessage. Why?

Comment thread cassandra/cluster.py
Comment on lines +5009 to +5019
if self.prepared_statement and isinstance(message, ExecuteMessage):
has_result_metadata_id = self.prepared_statement.result_metadata_id is not None
has_result_metadata = bool(self.prepared_statement.result_metadata)
can_skip_meta = has_result_metadata_id and has_result_metadata and (
ProtocolVersion.uses_prepared_metadata(connection.protocol_version)
or connection.features.use_metadata_id
)
message.skip_meta = can_skip_meta
message.result_metadata_id = self.prepared_statement.result_metadata_id if can_skip_meta else None
message.use_metadata_id = connection.features.use_metadata_id

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ah, so instead of setting those when creating ExecuteMessage you started to set them here.
This seems a bit inelegant. Why did you decide on this approach? Can't we still pass those params to the constructor? And if we are going with your approach, why do we need those params in the constructor?

Comment thread cassandra/protocol.py
Comment on lines 646 to 650
def send_body(self, f, protocol_version):
write_string(f, self.query_id)
if ProtocolVersion.uses_prepared_metadata(protocol_version):
write_string(f, self.result_metadata_id)
if ProtocolVersion.uses_prepared_metadata(protocol_version) or self.use_metadata_id:
write_string(f, self.result_metadata_id if self.result_metadata_id is not None else b'')
self._write_query_params(f, protocol_version)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A more elegant approach, it seems to me, is to not have use_metadata_id in ExecuteMessage. Instead, pass ProtocolFeatures to this method, use it and version to decide wheter to send an id, and use it from ExecuteMessage

Comment thread cassandra/cluster.py
Comment on lines +5183 to +5203
new_result_metadata_id = getattr(response, 'result_metadata_id', None)
if self.prepared_statement and new_result_metadata_id is not None:
if response.column_metadata:
# Write result_metadata before result_metadata_id intentionally:
# a concurrent reader that still sees the old metadata_id will
# ask the server for full metadata and recover safely; a reader
# that sees the new metadata_id together with the new metadata
# is immediately correct. The opposite write order could expose
# a window where a reader uses a new metadata_id with stale metadata.
# Note: correctness of this ordering relies on CPython's GIL making
# individual attribute reads/writes effectively atomic. Other Python
# implementations (PyPy, Jython, etc.) may not provide this guarantee.
self.prepared_statement.result_metadata = response.column_metadata
else:
log.warning(
"Server sent a new result_metadata_id but no column metadata "
"for prepared statement %r. The cached column metadata will not "
"be updated; only result_metadata_id is refreshed.",
getattr(self.prepared_statement, 'query_id', None)
)
self.prepared_statement.result_metadata_id = new_result_metadata_id

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What is the concurrency model here? I don't think this code works if its possible for it to execute concurrently.
Consider the following:

  • We start with schema X and perform two schema changes, to version Y and then Z.
  • We issue two requests
  • First request receives Y_id and Y, sets result_metadata to Y, and then execution switches to second request
  • Second request receives Z_id and Z, sets result_metadata to Z, then result_metadata_id to Z_id.
  • Execution switches back to first request, it sets result_metadata_id to Y_id.

Now we have result_metadata = Z, and result_metadata_id - Y_id. Any request executed won't receive new metadata (because id is the newest) but will try to deserialize results using older metadata.

Comment thread docs/scylla-specific.rst
Comment on lines +190 to +193
- Statements prepared before the extension was negotiated (e.g., during a rolling
upgrade) retain ``result_metadata_id=None`` and fall back to always requesting
full metadata, which is the safest option.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sure, but at some point we'll receive the metadata and id, and then it will be skipped.

I'm saying that because this point may suggest that clients need to be restarted after such rolling upgrade, but they don't

Comment thread docs/scylla-specific.rst
directly from the data.

For full protocol details see the ScyllaDB CQL extensions documentation:
https://opensource.docs.scylladb.com/stable/cql/cql-extensions.html

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What is this link? Opensource is LONG deprecated, and this link is about CQL language extension, not CQL protocol extensions - totally irrelevant here.

Comment thread cassandra/cluster.py
Comment on lines +5009 to +5013
if self.prepared_statement and isinstance(message, ExecuteMessage):
has_result_metadata_id = self.prepared_statement.result_metadata_id is not None
has_result_metadata = bool(self.prepared_statement.result_metadata)
can_skip_meta = has_result_metadata_id and has_result_metadata and (
ProtocolVersion.uses_prepared_metadata(connection.protocol_version)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When is bool(self.prepared_statement.result_metadata) true?
Is it true if server sent result metadata with 0 columns?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants