Skip to content

Commit 5a6fcb5

Browse files
committed
RUBY-257 - prepared-statement cache should not be host-scoped.
* Fixed some issues in batch request handling.
1 parent a3d6309 commit 5a6fcb5

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

lib/cassandra/cluster/client.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,16 +1061,23 @@ def handle_response(response_future,
10611061
r.data_present,
10621062
retries)
10631063
when Protocol::UnpreparedErrorResponse
1064-
cql = statement.cql
1065-
1066-
synchronize do
1067-
@preparing_statements[host].delete(cql)
1064+
cql = nil
1065+
if statement.is_a?(Cassandra::Statements::Batch)
1066+
# Find the prepared statement with the prepared-statement-id reported by the node.
1067+
unprepared_child = statement.statements.select do |s|
1068+
(s.is_a?(Cassandra::Statements::Prepared) || s.is_a?(Cassandra::Statements::Bound)) && s.id == r.id
1069+
end.first
1070+
cql = unprepared_child ? unprepared_child.cql : nil
1071+
else
1072+
# This is a normal statement, so we have everything we need.
1073+
cql = statement.cql
1074+
synchronize { @preparing_statements[host].delete(cql) }
10681075
end
10691076

10701077
prepare = prepare_statement(host, connection, cql, timeout)
10711078
prepare.on_complete do |_|
10721079
if prepare.resolved?
1073-
request.id = prepare.value
1080+
request.id = prepare.value unless request.is_a?(Cassandra::Protocol::BatchRequest)
10741081
do_send_request_by_plan(host,
10751082
connection,
10761083
promise,
@@ -1174,7 +1181,8 @@ def handle_response(response_future,
11741181
pk_idx ||= @schema.get_pk_idx(metadata)
11751182

11761183
promise.fulfill(
1177-
Statements::Prepared.new(r.custom_payload,
1184+
Statements::Prepared.new(r.id,
1185+
r.custom_payload,
11781186
r.warnings,
11791187
cql,
11801188
metadata,

lib/cassandra/statements/bound.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ class Bound
2828
attr_reader :params
2929
# @private
3030
attr_reader :params_types, :result_metadata, :keyspace, :partition_key
31+
# @private prepared-statement id
32+
attr_reader :id
3133

3234
# @private
33-
def initialize(cql,
35+
def initialize(id,
36+
cql,
3437
params_types,
3538
result_metadata,
3639
params,
3740
keyspace = nil,
3841
partition_key = nil,
3942
idempotent = false)
43+
@id = id
4044
@cql = cql
4145
@params_types = params_types
4246
@result_metadata = result_metadata

lib/cassandra/statements/prepared.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ class Prepared
2727
attr_reader :cql
2828
# @private
2929
attr_reader :result_metadata
30+
# @private prepared-statement id
31+
attr_reader :id
3032

3133
# @private
32-
def initialize(payload,
34+
def initialize(id,
35+
payload,
3336
warnings,
3437
cql,
3538
params_metadata,
@@ -44,6 +47,7 @@ def initialize(payload,
4447
retries,
4548
client,
4649
connection_options)
50+
@id = id
4751
@payload = payload
4852
@warnings = warnings
4953
@cql = cql
@@ -131,7 +135,8 @@ def bind(args = nil)
131135

132136
partition_key = create_partition_key(params)
133137

134-
Bound.new(@cql,
138+
Bound.new(@id,
139+
@cql,
135140
param_types,
136141
@result_metadata,
137142
params,

spec/cassandra/session_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ module Cassandra
131131
let(:cql) { "INSERT INTO songs (id, title, album, artist, tags) VALUES (?, ?, ?, ?, ?)" }
132132
let(:result_metadata) { nil }
133133
let(:params_metadata) { Array.new(5) }
134-
let(:statement) { Statements::Prepared.new(nil, nil, cql, params_metadata, result_metadata, nil, nil, nil, nil, VOID_OPTIONS, nil, nil, nil, nil, nil) }
134+
let(:statement) { Statements::Prepared.new(nil, nil, nil, cql, params_metadata, result_metadata, nil, nil, nil, nil, VOID_OPTIONS, nil, nil, nil, nil, nil) }
135135

136136
it 'binds and executes result' do
137137
promise = double('promise')
@@ -151,7 +151,7 @@ module Cassandra
151151
let(:result_metadata) { nil }
152152
let(:params_metadata) { Array.new(5) }
153153
let(:params) { [1,2,3,4,5] }
154-
let(:statement) { Statements::Bound.new(cql, params_metadata, result_metadata, params) }
154+
let(:statement) { Statements::Bound.new(nil, cql, params_metadata, result_metadata, params) }
155155

156156
it 'executes statement' do
157157
promise = double('promise')

0 commit comments

Comments
 (0)