Repository navigation
Conversation
…Row Decoding Patch by Yifan Cai; Reviewed by TBD for CASSJAVA-130
tejalchak
left a comment
There was a problem hiding this comment.
Solid fix. One follow-up question inline around toMessage worth deciding whether to thread the snapshot there too before/after merge.
| logPrefixJoiner.join(this.sessionName, nodeRequestId, currentExecutionIndex), | ||
| resultMetadataSnapshot(statement)); | ||
| Message message = Conversions.toMessage(statement, executionProfile, context); |
There was a problem hiding this comment.
I was looking at the race condition window here and wanted to get a second pair of eyes ...
While the callback now safely uses the snapshot, toMessage still reads the live metadata state. I'm wondering if a concurrent schema update between capturing the snapshot and the toMessage execution could cause a mismatch (where the request sends the new metadata ID, but the callback decodes using the old snapshot). Should toMessage accept the snapshot to close any chance of discrepancy?
Since ConversionsMetadataRaceTest calls getResultDefinitions directly rather than driving through toMessage, so it might not catch this without some change.
There was a problem hiding this comment.
Make sense. We should share the same metadata snapshot in toMessage
| static class ResultMetadata { | ||
| private final ByteBuffer resultMetadataId; | ||
| private final ColumnDefinitions resultSetDefinitions; | ||
|
|
||
| private ResultMetadata(ByteBuffer resultMetadataId, ColumnDefinitions resultSetDefinitions) { | ||
| this.resultMetadataId = resultMetadataId; | ||
| this.resultSetDefinitions = resultSetDefinitions; | ||
| } | ||
|
|
||
| ByteBuffer getResultMetadataId() { | ||
| return resultMetadataId; | ||
| } | ||
|
|
||
| ColumnDefinitions getResultSetDefinitions() { | ||
| return resultSetDefinitions; | ||
| } |
There was a problem hiding this comment.
A minor thought: if this branch is strictly Java 17+, we could consider using a record to avoid the boilerplate code
static record ResultMetadata(ByteBuffer resultMetadataId, ColumnDefinitions resultSetDefinitions) {}
There was a problem hiding this comment.
the compiler is pinned to java 8. It cannot use the language feature in jdk 17.
SiyaoIsHiding
left a comment
There was a problem hiding this comment.
What do you think of having resultMetadata as a field of BoundStatement?
In DefaultBoundStatement constructor, resultMetadata will basically be a snapshot of the preparedStatement.getCurrentResultMetadata() at that moment.
Then at response decode time, as long as rowsMetadata.columnSpecs.isEmpty(), use the one in BountStatement.resultMetadata, otherwise, update the PreparedStatement.resultMetadata.
@SiyaoIsHiding , thanks for the suggestion! Snapshotting on constructor could make the code more clean. There are three drawbacks:
Therefore, I'd like to stay with the current implementation. Let me know if the above makes sense. |
|
On second thought, what is the race condition that we are trying to fix again?
Under what circumstances does the driver corrupt result metadata? |
Not really, corruption requires two different bound statements of the same prepared statement in flight around a schema change, where the older one gets a SKIP_METADATA reply and its decode is delayed past a newer one's full-metadata reply that updates the shared cache first. The following is the corruption scenario modified from your example.
|
|
Got it. So the protocol is that if the server replies |
|
Thank you for the fix! |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the changes that preserve request-time result metadata.
No open issues 🎉
What changed in this PR
Addresses CASSJAVA-130 in the driver’s CQL execution path by preserving request-time result metadata, preventing concurrent metadata updates from corrupting SKIP_METADATA row decoding.
Changes:
- Exposes an atomic snapshot of a prepared statement’s result metadata ID and column definitions.
- Uses the same snapshot for request encoding and response decoding.
- Adds deterministic regression tests for encode-side and decode-side races and updates a reactive test call.
| File | Description |
|---|---|
core/src/test/java/com/datastax/oss/driver/internal/core/cql/ConversionsMetadataRaceTest.java |
Adds regression tests for concurrent metadata updates. |
core/src/test/java/com/datastax/dse/driver/internal/core/cql/reactive/CqlRequestReactiveProcessorTest.java |
Updates the result-conversion call for the snapshot parameter. |
core/src/main/java/com/datastax/oss/driver/internal/core/cql/DefaultPreparedStatement.java |
Exposes the metadata snapshot and makes its fields final. |
core/src/main/java/com/datastax/oss/driver/internal/core/cql/CqlRequestHandler.java |
Captures and retains metadata for each request attempt. |
core/src/main/java/com/datastax/oss/driver/internal/core/cql/Conversions.java |
Uses captured metadata for encoding and decoding. |
Review effort: Balanced
|
It feels a bit odd to see AI bot giving recommendation on PR approval, but thanks for the reminder. @SiyaoIsHiding, anything else pending before merging the fix? |
|
Hi @yifan-c ! Sorry for the confusion. I'm still not 100% convinced this is the best solution we got, so I still want to spend more time on this PR. |
|
@yifan-c public class BoundExecute extends Execute {
private final ColumnDefinitions resultSetDefinitions;So each message actually holds its own result metadata? A draft: https://github.com/apache/cassandra-java-driver/compare/trunk...SiyaoIsHiding:java-driver:CASSJAVA-130?expand=1 |
|
Bret mentioned (tho he didn't look into this PR deeply) that we can just add a field to public class Execute extends Message {
public final byte[] queryId;
public final byte[] resultMetadataId;
@Nullable public final Iterable<?> resultMetadata; // a pointer to an immutable Iterable, not a copy. `ColumnDefinitions` implements `Iterable`So it will be even cleaner and other implementations of |
Patch by Yifan Cai; Reviewed by TBD for CASSJAVA-130