-
Notifications
You must be signed in to change notification settings - Fork 132
/
replica_side_filtering_test.py
558 lines (467 loc) · 28.9 KB
/
replica_side_filtering_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
from abc import abstractmethod
import pytest
from cassandra import ConsistencyLevel as CL
from cassandra.query import SimpleStatement
from dtest import Tester, create_ks, mk_bman_path
from tools.assertions import (assert_all, assert_none, assert_one)
since = pytest.mark.since
keyspace = 'ks'
class ReplicaSideFiltering(Tester):
"""
@jira_ticket CASSANDRA-8272, CASSANDRA-8273
Base consistency test for queries involving replica-side filtering when some of the replicas have stale data.
"""
__test__ = False
def _prepare_cluster(self, create_table, create_index=None, both_nodes=None, only_node1=None, only_node2=None):
"""
:param create_table a table creation CQL query
:param create_index an index creation CQL query, that will be executed depending on ``_create_index`` method
:param both_nodes queries to be executed in both nodes with CL=ALL
:param only_node1 queries to be executed in the first node only, with CL=ONE, while the second node is stopped
:param only_node2 queries to be executed in the second node only, with CL=ONE, while the first node is stopped
:return: a session connected exclusively to the first node with CL=ALL
"""
cluster = self.cluster
# Disable hinted handoff and set batch commit log so this doesn't interfere with the test
if only_node1 or only_node2:
cluster.set_configuration_options(values={'hinted_handoff_enabled': False})
cluster.set_batch_commitlog(enabled=True, use_batch_window = cluster.version() < '5.0')
cluster.populate(2)
node1, node2 = cluster.nodelist()
cluster.start()
session = self.patient_exclusive_cql_connection(node1, consistency_level=CL.ALL)
create_ks(session, keyspace, 2)
session.execute("USE " + keyspace)
# create the table
session.execute(create_table)
# create the index if it's required
if self.create_index():
session.execute(create_index)
# execute the queries for both nodes with CL=ALL
if both_nodes:
for q in both_nodes:
session.execute(q)
# execute the queries for the first node only with the second node stopped
if only_node1:
self._execute_isolated(node_to_update=node1, node_to_stop=node2, queries=only_node1)
# execute the queries for the second node only with the first node stopped
if only_node2:
self._execute_isolated(node_to_update=node2, node_to_stop=node1, queries=only_node2)
# set the session with CL=ALL for testing queries with the created scenario
self.session = self.patient_exclusive_cql_connection(node1, keyspace=keyspace, consistency_level=CL.ALL)
def _execute_isolated(self, node_to_update, node_to_stop, queries):
node_to_stop.flush()
node_to_stop.stop()
session = self.patient_cql_connection(node_to_update, keyspace, consistency_level=CL.ONE)
for q in queries:
session.execute(q)
node_to_stop.start()
def _assert_none(self, query):
"""
Assert query returns nothing.
@param query Query to run
"""
decorated_query = self._decorate_query(query)
assert_none(self.session, decorated_query)
def _assert_one(self, query, row):
"""
Assert query returns one row.
@param query Query to run
@param row Expected result row from query
"""
decorated_query = self._decorate_query(query)
assert_one(self.session, decorated_query, row)
def _assert_all(self, query, rows):
"""
Assert query returns all expected rows in the correct order.
@param query Query to run
@param rows Expected result rows from query
"""
decorated_query = self._decorate_query(query)
assert_all(self.session, decorated_query, rows)
def _decorate_query(self, query):
return query if self.create_index() else query + " ALLOW FILTERING"
def _skip_if_index_on_static_is_not_supported(self):
if self.create_index() and self.cluster.version() < '3.4':
pytest.skip('Secondary indexes on static column are not supported before 3.4 (CASSANDRA-8103)')
def _skip_if_filtering_partition_columns_is_not_supported(self):
if not self.create_index() and self.cluster.version() < '3.11':
pytest.skip('Filtering of partition key parts is not supported before 3.11 (CASSANDRA-13275)')
@abstractmethod
def create_index(self):
"""
:return:``True`` if the tests should create an index, ``False`` if they should use ``ALLOW FILTERING`` instead
"""
pass
def test_update_on_skinny_table(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int PRIMARY KEY, v text)",
create_index="CREATE INDEX ON t(v)",
both_nodes=["INSERT INTO t(k, v) VALUES (0, 'old')"],
only_node1=["UPDATE t SET v = 'new' WHERE k = 0"])
self._assert_none("SELECT * FROM t WHERE v = 'old'")
self._assert_one("SELECT * FROM t WHERE v = 'new'", row=[0, 'new'])
def test_update_on_wide_table(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v text, s int STATIC, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(v)",
both_nodes=["INSERT INTO t(k, s) VALUES (0, 9)",
"INSERT INTO t(k, c, v) VALUES (0, -1, 'old')",
"INSERT INTO t(k, c, v) VALUES (0, 0, 'old')",
"INSERT INTO t(k, c, v) VALUES (0, 1, 'old')"],
only_node1=["UPDATE t SET v = 'new' WHERE k = 0 AND c = 0"])
self._assert_all("SELECT * FROM t WHERE v = 'old'", rows=[[0, -1, 9, 'old'], [0, 1, 9, 'old']])
self._assert_all("SELECT * FROM t WHERE v = 'new'", rows=[[0, 0, 9, 'new']])
def test_update_on_static_column_with_empty_partition(self):
self._skip_if_index_on_static_is_not_supported()
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v int, s text STATIC, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(s)",
both_nodes=["INSERT INTO t(k, s) VALUES (0, 'old')",
"INSERT INTO t(k, s) VALUES (1, 'old')"],
only_node1=["UPDATE t SET s = 'new' WHERE k = 0"])
self._assert_one("SELECT * FROM t WHERE s = 'old'", row=[1, None, 'old', None])
self._assert_one("SELECT * FROM t WHERE s = 'new'", row=[0, None, 'new', None])
def test_update_on_static_column_with_not_empty_partition(self):
self._skip_if_index_on_static_is_not_supported()
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v int, s text STATIC, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(s)",
both_nodes=["INSERT INTO t(k, s) VALUES (0, 'old')",
"INSERT INTO t(k, s) VALUES (1, 'old')",
"INSERT INTO t(k, c, v) VALUES (0, 10, 100)",
"INSERT INTO t(k, c, v) VALUES (0, 20, 200)",
"INSERT INTO t(k, c, v) VALUES (1, 30, 300)",
"INSERT INTO t(k, c, v) VALUES (1, 40, 400)"],
only_node1=["UPDATE t SET s = 'new' WHERE k = 0"])
self._assert_all("SELECT * FROM t WHERE s = 'old'", rows=[[1, 30, 'old', 300], [1, 40, 'old', 400]])
self._assert_all("SELECT * FROM t WHERE s = 'new'", rows=[[0, 10, 'new', 100], [0, 20, 'new', 200]])
def test_update_on_collection(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int PRIMARY KEY, v set<int>)",
create_index="CREATE INDEX ON t(v)",
both_nodes=["INSERT INTO t(k, v) VALUES (0, {-1, 0, 1})"],
only_node1=["UPDATE t SET v = v - {0} WHERE k = 0"])
self._assert_none("SELECT * FROM t WHERE v CONTAINS 0")
self._assert_one("SELECT * FROM t WHERE v CONTAINS 1", row=[0, [-1, 1]])
def test_complementary_deletion_with_limit_on_partition_key_column_with_empty_partitions(self):
self._skip_if_filtering_partition_columns_is_not_supported()
self._prepare_cluster(
create_table="CREATE TABLE t (k1 int, k2 int, c int, s int STATIC, PRIMARY KEY ((k1, k2), c))",
create_index="CREATE INDEX ON t(k1)",
both_nodes=["INSERT INTO t (k1, k2, s) VALUES (0, 1, 10)",
"INSERT INTO t (k1, k2, s) VALUES (0, 2, 20)"],
only_node1=["DELETE FROM t WHERE k1 = 0 AND k2 = 1"],
only_node2=["DELETE FROM t WHERE k1 = 0 AND k2 = 2"])
self._assert_none("SELECT * FROM t WHERE k1 = 0 LIMIT 1")
def test_complementary_deletion_with_limit_on_partition_key_column_with_not_empty_partitions(self):
self._skip_if_filtering_partition_columns_is_not_supported()
self._prepare_cluster(
create_table="CREATE TABLE t (k1 int, k2 int, c int, s int STATIC, PRIMARY KEY ((k1, k2), c))",
create_index="CREATE INDEX ON t(k1)",
both_nodes=["INSERT INTO t (k1, k2, c, s) VALUES (0, 1, 10, 100)",
"INSERT INTO t (k1, k2, c, s) VALUES (0, 2, 20, 200)"],
only_node1=["DELETE FROM t WHERE k1 = 0 AND k2 = 1"],
only_node2=["DELETE FROM t WHERE k1 = 0 AND k2 = 2"])
self._assert_none("SELECT * FROM t WHERE k1 = 0 LIMIT 1")
def test_complementary_deletion_with_limit_on_clustering_key_column(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(c)",
both_nodes=["INSERT INTO t (k, c) VALUES (1, 0)",
"INSERT INTO t (k, c) VALUES (2, 0)"],
only_node1=["DELETE FROM t WHERE k = 1"],
only_node2=["DELETE FROM t WHERE k = 2"])
self._assert_none("SELECT * FROM t WHERE c = 0 LIMIT 1")
def test_complementary_deletion_with_limit_on_static_column_with_empty_partitions(self):
self._skip_if_index_on_static_is_not_supported()
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, s int STATIC, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(s)",
both_nodes=["INSERT INTO t (k, s) VALUES (1, 0)",
"INSERT INTO t (k, s) VALUES (2, 0)"],
only_node1=["DELETE FROM t WHERE k = 1"],
only_node2=["DELETE FROM t WHERE k = 2"])
self._assert_none("SELECT * FROM t WHERE s = 0 LIMIT 1")
def test_complementary_deletion_with_limit_on_static_column_with_empty_partitions_and_rows_after(self):
self._skip_if_index_on_static_is_not_supported()
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, s int STATIC, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(s)",
both_nodes=["INSERT INTO t (k, s) VALUES (1, 0)",
"INSERT INTO t (k, s) VALUES (2, 0)",
"INSERT INTO t (k, s) VALUES (3, 0)",
"INSERT INTO t (k, c) VALUES (3, 1)",
"INSERT INTO t (k, c) VALUES (3, 2)"],
only_node1=["DELETE FROM t WHERE k = 1"],
only_node2=["DELETE FROM t WHERE k = 2"])
self._assert_one("SELECT * FROM t WHERE s = 0 LIMIT 1", row=[3, 1, 0])
self._assert_all("SELECT * FROM t WHERE s = 0 LIMIT 10", rows=[[3, 1, 0], [3, 2, 0]])
self._assert_all("SELECT * FROM t WHERE s = 0", rows=[[3, 1, 0], [3, 2, 0]])
def test_complementary_deletion_with_limit_on_static_column_with_not_empty_partitions(self):
self._skip_if_index_on_static_is_not_supported()
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, s int STATIC, v int, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(s)",
both_nodes=["INSERT INTO t (k, c, v, s) VALUES (1, 10, 100, 0)",
"INSERT INTO t (k, c, v, s) VALUES (2, 20, 200, 0)"],
only_node1=["DELETE FROM t WHERE k = 1"],
only_node2=["DELETE FROM t WHERE k = 2"])
self._assert_none("SELECT * FROM t WHERE s = 0 LIMIT 1")
def test_complementary_deletion_with_limit_on_static_column_with_not_empty_partitions_and_rows_after(self):
self._skip_if_index_on_static_is_not_supported()
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, s int STATIC, v int, PRIMARY KEY(k, c))",
create_index="CREATE INDEX ON t(s)",
both_nodes=["INSERT INTO t (k, c, v, s) VALUES (1, 10, 100, 0)",
"INSERT INTO t (k, c, v, s) VALUES (2, 20, 200, 0)",
"INSERT INTO t (k, s) VALUES (3, 0)",
"INSERT INTO t (k, c) VALUES (3, 1)",
"INSERT INTO t (k, c) VALUES (3, 2)"],
only_node1=["DELETE FROM t WHERE k = 1"],
only_node2=["DELETE FROM t WHERE k = 2"])
self._assert_one("SELECT * FROM t WHERE s = 0 LIMIT 1", row=[3, 1, 0, None])
self._assert_all("SELECT * FROM t WHERE s = 0 LIMIT 10", rows=[[3, 1, 0, None], [3, 2, 0, None]])
self._assert_all("SELECT * FROM t WHERE s = 0", rows=[[3, 1, 0, None], [3, 2, 0, None]])
def test_complementary_deletion_with_limit_on_regular_column(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v int, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(v)",
both_nodes=["INSERT INTO t (k, c, v) VALUES (0, 1, 0)",
"INSERT INTO t (k, c, v) VALUES (0, 2, 0)"],
only_node1=["DELETE FROM t WHERE k = 0 AND c = 1"],
only_node2=["DELETE FROM t WHERE k = 0 AND c = 2"])
self._assert_none("SELECT * FROM t WHERE v = 0 LIMIT 1")
def test_complementary_deletion_with_limit_and_rows_after(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v int, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(v)",
both_nodes=["INSERT INTO t (k, c, v) VALUES (0, 1, 0)",
"INSERT INTO t (k, c, v) VALUES (0, 2, 0)",
"INSERT INTO t (k, c, v) VALUES (0, 3, 0)"],
only_node1=["DELETE FROM t WHERE k = 0 AND c = 1",
"INSERT INTO t (k, c, v) VALUES (0, 4, 0)"],
only_node2=["INSERT INTO t (k, c, v) VALUES (0, 5, 0)",
"DELETE FROM t WHERE k = 0 AND c = 2"])
self._assert_one("SELECT * FROM t WHERE v = 0 LIMIT 1", row=[0, 3, 0])
self._assert_all("SELECT * FROM t WHERE v = 0 LIMIT 2", rows=[[0, 3, 0], [0, 4, 0]])
self._assert_all("SELECT * FROM t WHERE v = 0 LIMIT 3", rows=[[0, 3, 0], [0, 4, 0], [0, 5, 0]])
self._assert_all("SELECT * FROM t WHERE v = 0 LIMIT 4", rows=[[0, 3, 0], [0, 4, 0], [0, 5, 0]])
def test_complementary_deletion_with_limit_and_rows_between(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v int, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(v)",
both_nodes=["INSERT INTO t (k, c, v) VALUES (0, 1, 0)",
"INSERT INTO t (k, c, v) VALUES (0, 4, 0)"],
only_node1=["DELETE FROM t WHERE k = 0 AND c = 1"],
only_node2=["INSERT INTO t (k, c, v) VALUES (0, 2, 0)",
"INSERT INTO t (k, c, v) VALUES (0, 3, 0)",
"DELETE FROM t WHERE k = 0 AND c = 4"])
self._assert_one("SELECT * FROM t WHERE v = 0 LIMIT 1", row=[0, 2, 0])
self._assert_all("SELECT * FROM t WHERE v = 0 LIMIT 2", rows=[[0, 2, 0], [0, 3, 0]])
self._assert_all("SELECT * FROM t WHERE v = 0 LIMIT 3", rows=[[0, 2, 0], [0, 3, 0]])
def test_complementary_update_with_limit_on_static_column_with_empty_partitions(self):
self._skip_if_index_on_static_is_not_supported()
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, s text STATIC, v int, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(s)",
both_nodes=["INSERT INTO t (k, s) VALUES (1, 'old')",
"INSERT INTO t (k, s) VALUES (2, 'old')"],
only_node1=["UPDATE t SET s = 'new' WHERE k = 1"],
only_node2=["UPDATE t SET s = 'new' WHERE k = 2"])
self._assert_none("SELECT * FROM t WHERE s = 'old' LIMIT 1")
self._assert_one("SELECT k, c, v, s FROM t WHERE s = 'new' LIMIT 1", row=[1, None, None, 'new'])
self._assert_all("SELECT k, c, v, s FROM t WHERE s = 'new'",
rows=[[1, None, None, 'new'], [2, None, None, 'new']])
def test_complementary_update_with_limit_on_static_column_with_not_empty_partitions(self):
self._skip_if_index_on_static_is_not_supported()
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, s text STATIC, v int, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(s)",
both_nodes=["INSERT INTO t (k, c, v, s) VALUES (1, 10, 100, 'old')",
"INSERT INTO t (k, c, v, s) VALUES (2, 20, 200, 'old')"],
only_node1=["UPDATE t SET s = 'new' WHERE k = 1"],
only_node2=["UPDATE t SET s = 'new' WHERE k = 2"])
self._assert_none("SELECT * FROM t WHERE s = 'old' LIMIT 1")
self._assert_one("SELECT k, c, v, s FROM t WHERE s = 'new' LIMIT 1", row=[1, 10, 100, 'new'])
self._assert_all("SELECT k, c, v, s FROM t WHERE s = 'new'", rows=[[1, 10, 100, 'new'], [2, 20, 200, 'new']])
def test_complementary_update_with_limit_on_regular_column(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v text, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(v)",
both_nodes=["INSERT INTO t (k, c, v) VALUES (0, 1, 'old')",
"INSERT INTO t (k, c, v) VALUES (0, 2, 'old')"],
only_node1=["UPDATE t SET v = 'new' WHERE k = 0 AND c = 1"],
only_node2=["UPDATE t SET v = 'new' WHERE k = 0 AND c = 2"])
self._assert_none("SELECT * FROM t WHERE v = 'old' LIMIT 1")
self._assert_one("SELECT * FROM t WHERE v = 'new' LIMIT 1", row=[0, 1, 'new'])
self._assert_all("SELECT * FROM t WHERE v = 'new'", rows=[[0, 1, 'new'], [0, 2, 'new']])
def test_complementary_update_with_limit_and_rows_between(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v text, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(v)",
both_nodes=["INSERT INTO t (k, c, v) VALUES (0, 1, 'old')",
"INSERT INTO t (k, c, v) VALUES (0, 4, 'old')"],
only_node1=["UPDATE t SET v = 'new' WHERE k = 0 AND c = 1"],
only_node2=["INSERT INTO t (k, c, v) VALUES (0, 2, 'old')",
"INSERT INTO t (k, c, v) VALUES (0, 3, 'old')",
"UPDATE t SET v = 'new' WHERE k = 0 AND c = 4"])
self._assert_one("SELECT * FROM t WHERE v = 'old' LIMIT 1", row=[0, 2, 'old'])
self._assert_all("SELECT * FROM t WHERE v = 'old' LIMIT 2", rows=[[0, 2, 'old'], [0, 3, 'old']])
self._assert_all("SELECT * FROM t WHERE v = 'old' LIMIT 3", rows=[[0, 2, 'old'], [0, 3, 'old']])
self._assert_one("SELECT * FROM t WHERE v = 'new' LIMIT 1", row=[0, 1, 'new'])
self._assert_all("SELECT * FROM t WHERE v = 'new'", rows=[[0, 1, 'new'], [0, 4, 'new']])
def test_partition_deletion_on_skinny_table(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int PRIMARY KEY, v text)",
create_index="CREATE INDEX ON t(v)",
only_node1=["INSERT INTO t (k, v) VALUES (0, 'old') USING TIMESTAMP 1"],
only_node2=["DELETE FROM t WHERE k = 0"])
self._assert_none("SELECT * FROM t WHERE v = 'old' LIMIT 1")
self._assert_none("SELECT * FROM t WHERE v = 'old'")
def test_partition_deletion_on_wide_table(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v text, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(v)",
only_node1=["INSERT INTO t (k, c, v) VALUES (0, 1, 'old') USING TIMESTAMP 1"],
only_node2=["DELETE FROM t WHERE k = 0"])
self._assert_none("SELECT * FROM t WHERE v = 'old' LIMIT 1")
self._assert_none("SELECT * FROM t WHERE v = 'old'")
def test_row_deletion_on_wide_table(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v text, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(v)",
only_node1=["INSERT INTO t (k, c, v) VALUES (0, 1, 'old') USING TIMESTAMP 1"],
only_node2=["DELETE FROM t WHERE k = 0 AND c = 1"])
self._assert_none("SELECT * FROM t WHERE v = 'old' LIMIT 1")
self._assert_none("SELECT * FROM t WHERE v = 'old'")
def test_range_deletion_on_wide_table(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v text, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(v)",
only_node1=["INSERT INTO t (k, c, v) VALUES (0, 1, 'old') USING TIMESTAMP 1",
"INSERT INTO t (k, c, v) VALUES (0, 2, 'old') USING TIMESTAMP 1",
"INSERT INTO t (k, c, v) VALUES (0, 3, 'old') USING TIMESTAMP 1",
"INSERT INTO t (k, c, v) VALUES (0, 4, 'old') USING TIMESTAMP 1"],
only_node2=["DELETE FROM t WHERE k = 0 AND c > 1 AND c < 4"])
self._assert_one("SELECT * FROM t WHERE v = 'old' LIMIT 1", row=[0, 1, 'old'])
self._assert_all("SELECT * FROM t WHERE v = 'old'", rows=[[0, 1, 'old'], [0, 4, 'old']])
def test_mismatching_insertions_on_skinny_table(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int PRIMARY KEY, v text)",
create_index="CREATE INDEX ON t(v)",
only_node1=["INSERT INTO t (k, v) VALUES (0, 'old') USING TIMESTAMP 1"],
only_node2=["INSERT INTO t (k, v) VALUES (0, 'new') USING TIMESTAMP 2"])
self._assert_none("SELECT * FROM t WHERE v = 'old' LIMIT 1")
self._assert_none("SELECT * FROM t WHERE v = 'old'")
self._assert_one("SELECT * FROM t WHERE v = 'new'", row=[0, 'new'])
def test_mismatching_insertions_on_wide_table(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v text, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(v)",
only_node1=["INSERT INTO t (k, c, v) VALUES (0, 1, 'old') USING TIMESTAMP 1"],
only_node2=["INSERT INTO t (k, c, v) VALUES (0, 1, 'new') USING TIMESTAMP 2"])
self._assert_none("SELECT * FROM t WHERE v = 'old' LIMIT 1")
self._assert_none("SELECT * FROM t WHERE v = 'old'")
self._assert_one("SELECT * FROM t WHERE v = 'new'", row=[0, 1, 'new'])
def test_consistent_skinny_table(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int PRIMARY KEY, v text)",
create_index="CREATE INDEX ON t(v)",
both_nodes=["INSERT INTO t(k, v) VALUES (1, 'old')", # updated to 'new'
"INSERT INTO t(k, v) VALUES (2, 'old')",
"INSERT INTO t(k, v) VALUES (3, 'old')", # updated to 'new'
"INSERT INTO t(k, v) VALUES (4, 'old')",
"INSERT INTO t(k, v) VALUES (5, 'old')", # deleted partition
"UPDATE t SET v = 'new' WHERE k = 1",
"UPDATE t SET v = 'new' WHERE k = 3",
"DELETE FROM t WHERE k = 5"])
self._assert_one("SELECT * FROM t WHERE v = 'old' LIMIT 1", row=[2, 'old'])
self._assert_one("SELECT * FROM t WHERE v = 'new' LIMIT 1", row=[1, 'new'])
self._assert_all("SELECT * FROM t WHERE v = 'old'", rows=[[2, 'old'], [4, 'old']])
self._assert_all("SELECT * FROM t WHERE v = 'new'", rows=[[1, 'new'], [3, 'new']])
def test_consistent_wide_table(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int, c int, v text, PRIMARY KEY (k, c))",
create_index="CREATE INDEX ON t(v)",
both_nodes=["INSERT INTO t(k, c, v) VALUES (0, 1, 'old')", # updated to 'new'
"INSERT INTO t(k, c, v) VALUES (0, 2, 'old')",
"INSERT INTO t(k, c, v) VALUES (0, 3, 'old')", # updated to 'new'
"INSERT INTO t(k, c, v) VALUES (0, 4, 'old')",
"INSERT INTO t(k, c, v) VALUES (0, 5, 'old')", # deleted row
"INSERT INTO t(k, c, v) VALUES (1, 1, 'old')", # deleted partition
"INSERT INTO t(k, c, v) VALUES (1, 2, 'old')", # deleted partition
"UPDATE t SET v = 'new' WHERE k = 0 AND c = 1",
"UPDATE t SET v = 'new' WHERE k = 0 AND c = 3",
"DELETE FROM t WHERE k = 0 AND c = 5",
"DELETE FROM t WHERE k = 1"])
self._assert_one("SELECT * FROM t WHERE v = 'old' LIMIT 1", row=[0, 2, 'old'])
self._assert_one("SELECT * FROM t WHERE v = 'new' LIMIT 1", row=[0, 1, 'new'])
self._assert_all("SELECT * FROM t WHERE v = 'old'", rows=[[0, 2, 'old'], [0, 4, 'old']])
self._assert_all("SELECT * FROM t WHERE v = 'new'", rows=[[0, 1, 'new'], [0, 3, 'new']])
def test_count(self):
self._prepare_cluster(
create_table="CREATE TABLE t (k int PRIMARY KEY, v text)",
create_index="CREATE INDEX ON t(v)",
both_nodes=["INSERT INTO t(k, v) VALUES (1, 'old')",
"INSERT INTO t(k, v) VALUES (2, 'old')",
"INSERT INTO t(k, v) VALUES (3, 'old')",
"INSERT INTO t(k, v) VALUES (4, 'old')",
"INSERT INTO t(k, v) VALUES (5, 'old')"],
only_node1=["UPDATE t SET v = 'new' WHERE k = 2",
"UPDATE t SET v = 'new' WHERE k = 4"])
self._assert_one("SELECT COUNT(*) FROM t WHERE v = 'old' LIMIT 1", row=[3])
self._assert_one("SELECT COUNT(*) FROM t WHERE v = 'old'", row=[3])
self._assert_one("SELECT COUNT(*) FROM t WHERE v = 'new'", row=[2])
@since('3.0.21')
class TestSecondaryIndexes(ReplicaSideFiltering):
"""
@jira_ticket CASSANDRA-8272
Tests the consistency of secondary indexes queries when some of the replicas have stale data.
"""
__test__ = True
def create_index(self):
return True
@since('3.0.21')
class TestAllowFiltering(ReplicaSideFiltering):
"""
@jira_ticket CASSANDRA-8273
Test the consistency of queries using ``ALLOW FILTERING`` when some of the replicas have stale data.
"""
__test__ = True
def create_index(self):
return False
def _test_missed_update_with_transient_replicas(self, missed_by_transient):
cluster = self.cluster
cluster.set_configuration_options(values={'hinted_handoff_enabled': False,
'num_tokens': 1,
'commitlog_sync_period_in_ms': 500,
'enable_transient_replication': True,
'partitioner': 'org.apache.cassandra.dht.OrderPreservingPartitioner'})
cluster.set_batch_commitlog(enabled=True, use_batch_window = cluster.version() < '5.0')
cluster.populate(2, tokens=[0, 1], debug=True, install_byteman=True)
node1, node2 = cluster.nodelist()
cluster.start()
self.session = self.patient_exclusive_cql_connection(node1, consistency_level=CL.ALL)
self.session.execute("CREATE KEYSPACE %s WITH replication = "
"{'class': 'SimpleStrategy', 'replication_factor': '2/1'}" % (keyspace))
self.session.execute("USE " + keyspace)
self.session.execute("CREATE TABLE t (k int PRIMARY KEY, v text)"
" WITH speculative_retry = 'NEVER'"
" AND additional_write_policy = 'NEVER'"
" AND read_repair = 'NONE'")
# insert in both nodes with CL=ALL
self.session.execute("INSERT INTO t(k, v) VALUES (0, 'old')")
# update the previous value with CL=ONE only in one replica
node = cluster.nodelist()[1 if missed_by_transient else 0]
node.byteman_submit([mk_bman_path('stop_writes.btm')])
self.session.execute(SimpleStatement("UPDATE t SET v = 'new' WHERE k = 0", consistency_level=CL.ONE))
# query with CL=ALL to verify that no old values are resurrected
self._assert_none("SELECT * FROM t WHERE v = 'old'")
self._assert_one("SELECT * FROM t WHERE v = 'new'", row=[0, 'new'])
@since('4.0')
def test_update_missed_by_transient_replica(self):
self._test_missed_update_with_transient_replicas(missed_by_transient=True)
@since('4.0')
def test_update_only_on_transient_replica(self):
self._test_missed_update_with_transient_replicas(missed_by_transient=False)