forked from thepaul/cassandra-dtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secondary_indexes_test.py
1260 lines (1033 loc) · 58 KB
/
secondary_indexes_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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import random
import re
import time
import uuid
from unittest import skipIf
from cassandra import InvalidRequest
from cassandra.concurrent import (execute_concurrent,
execute_concurrent_with_args)
from cassandra.protocol import ConfigurationException
from cassandra.query import BatchStatement, SimpleStatement
from dtest import (DISABLE_VNODES, OFFHEAP_MEMTABLES, DtestTimeoutError,
Tester, debug, CASSANDRA_VERSION_FROM_BUILD, create_ks, create_cf)
from tools.assertions import assert_bootstrap_state, assert_invalid, assert_none, assert_one, assert_row_count
from tools.data import index_is_built, rows_to_list
from tools.decorators import since
from tools.misc import new_node
class TestSecondaryIndexes(Tester):
@staticmethod
def _index_sstables_files(node, keyspace, table, index):
files = []
for data_dir in node.data_directories():
data_dir = os.path.join(data_dir, keyspace)
base_tbl_dir = os.path.join(data_dir, [s for s in os.listdir(data_dir) if s.startswith(table)][0])
index_sstables_dir = os.path.join(base_tbl_dir, '.' + index)
files.extend(os.listdir(index_sstables_dir))
return set(files)
def data_created_before_index_not_returned_in_where_query_test(self):
"""
@jira_ticket CASSANDRA-3367
"""
cluster = self.cluster
cluster.populate(1).start()
[node1] = cluster.nodelist()
session = self.patient_cql_connection(node1)
create_ks(session, 'ks', 1)
columns = {"password": "varchar", "gender": "varchar", "session_token": "varchar", "state": "varchar", "birth_year": "bigint"}
create_cf(session, 'users', columns=columns)
# insert data
session.execute("INSERT INTO users (KEY, password, gender, state, birth_year) VALUES ('user1', 'ch@ngem3a', 'f', 'TX', 1968);")
session.execute("INSERT INTO users (KEY, password, gender, state, birth_year) VALUES ('user2', 'ch@ngem3b', 'm', 'CA', 1971);")
# create index
session.execute("CREATE INDEX gender_key ON users (gender);")
session.execute("CREATE INDEX state_key ON users (state);")
session.execute("CREATE INDEX birth_year_key ON users (birth_year);")
# insert data
session.execute("INSERT INTO users (KEY, password, gender, state, birth_year) VALUES ('user3', 'ch@ngem3c', 'f', 'FL', 1978);")
session.execute("INSERT INTO users (KEY, password, gender, state, birth_year) VALUES ('user4', 'ch@ngem3d', 'm', 'TX', 1974);")
assert_row_count(session, "users", 4)
assert_row_count(session, "users", 2, "state='TX'")
assert_row_count(session, "users", 1, "state='CA'")
def test_low_cardinality_indexes(self):
"""
Checks that low-cardinality secondary index subqueries are executed
concurrently
"""
cluster = self.cluster
cluster.populate(3).start()
node1, node2, node3 = cluster.nodelist()
session = self.patient_cql_connection(node1)
session.max_trace_wait = 120
session.execute("CREATE KEYSPACE ks WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': '1'};")
session.execute("CREATE TABLE ks.cf (a text PRIMARY KEY, b text);")
session.execute("CREATE INDEX b_index ON ks.cf (b);")
num_rows = 100
for i in range(num_rows):
indexed_value = i % (num_rows / 3)
# use the same indexed value three times
session.execute("INSERT INTO ks.cf (a, b) VALUES ('%d', '%d');" % (i, indexed_value))
cluster.flush()
def check_trace_events(trace):
# we should see multiple requests get enqueued prior to index scan
# execution happening
# Look for messages like:
# Submitting range requests on 769 ranges with a concurrency of 769 (0.0070312 rows per range expected)
regex = r"Submitting range requests on [0-9]+ ranges with a concurrency of (\d+) \(([0-9.]+) rows per range expected\)"
for event in trace.events:
desc = event.description
match = re.match(regex, desc)
if match:
concurrency = int(match.group(1))
expected_per_range = float(match.group(2))
self.assertTrue(concurrency > 1, "Expected more than 1 concurrent range request, got %d" % concurrency)
self.assertTrue(expected_per_range > 0)
break
else:
self.fail("Didn't find matching trace event")
query = SimpleStatement("SELECT * FROM ks.cf WHERE b='1';")
result = session.execute(query, trace=True)
self.assertEqual(3, len(list(result)))
check_trace_events(result.get_query_trace())
query = SimpleStatement("SELECT * FROM ks.cf WHERE b='1' LIMIT 100;")
result = session.execute(query, trace=True)
self.assertEqual(3, len(list(result)))
check_trace_events(result.get_query_trace())
query = SimpleStatement("SELECT * FROM ks.cf WHERE b='1' LIMIT 3;")
result = session.execute(query, trace=True)
self.assertEqual(3, len(list(result)))
check_trace_events(result.get_query_trace())
for limit in (1, 2):
result = list(session.execute("SELECT * FROM ks.cf WHERE b='1' LIMIT %d;" % (limit,)))
self.assertEqual(limit, len(result))
def test_6924_dropping_ks(self):
"""
@jira_ticket CASSANDRA-6924
@jira_ticket CASSANDRA-11729
Data inserted immediately after dropping and recreating a
keyspace with an indexed column familiy is not included
in the index.
This test can be flaky due to concurrency issues during
schema updates. See CASSANDRA-11729 for an explanation.
"""
# Reproducing requires at least 3 nodes:
cluster = self.cluster
cluster.populate(3).start()
node1, node2, node3 = cluster.nodelist()
session = self.patient_cql_connection(node1)
# We have to wait up to RING_DELAY + 1 seconds for the MV Builder task
# to complete, to prevent schema concurrency issues with the drop
# keyspace calls that come later. See CASSANDRA-11729.
if self.cluster.version() > '3.0':
self.cluster.wait_for_any_log('Completed submission of build tasks for any materialized views',
timeout=35, filename='debug.log')
# This only occurs when dropping and recreating with
# the same name, so loop through this test a few times:
for i in range(10):
debug("round %s" % i)
try:
session.execute("DROP KEYSPACE ks")
except ConfigurationException:
pass
create_ks(session, 'ks', 1)
session.execute("CREATE TABLE ks.cf (key text PRIMARY KEY, col1 text);")
session.execute("CREATE INDEX on ks.cf (col1);")
for r in range(10):
stmt = "INSERT INTO ks.cf (key, col1) VALUES ('%s','asdf');" % r
session.execute(stmt)
self.wait_for_schema_agreement(session)
rows = session.execute("select count(*) from ks.cf WHERE col1='asdf'")
count = rows[0][0]
self.assertEqual(count, 10)
def test_6924_dropping_cf(self):
"""
@jira_ticket CASSANDRA-6924
Data inserted immediately after dropping and recreating an
indexed column family is not included in the index.
"""
# Reproducing requires at least 3 nodes:
cluster = self.cluster
cluster.populate(3).start()
node1, node2, node3 = cluster.nodelist()
session = self.patient_cql_connection(node1)
create_ks(session, 'ks', 1)
# This only occurs when dropping and recreating with
# the same name, so loop through this test a few times:
for i in range(10):
debug("round %s" % i)
try:
session.execute("DROP COLUMNFAMILY ks.cf")
except InvalidRequest:
pass
session.execute("CREATE TABLE ks.cf (key text PRIMARY KEY, col1 text);")
session.execute("CREATE INDEX on ks.cf (col1);")
for r in range(10):
stmt = "INSERT INTO ks.cf (key, col1) VALUES ('%s','asdf');" % r
session.execute(stmt)
self.wait_for_schema_agreement(session)
rows = session.execute("select count(*) from ks.cf WHERE col1='asdf'")
count = rows[0][0]
self.assertEqual(count, 10)
def test_8280_validate_indexed_values(self):
"""
@jira_ticket CASSANDRA-8280
Reject inserts & updates where values of any indexed
column is > 64k
"""
cluster = self.cluster
cluster.populate(1).start()
node1 = cluster.nodelist()[0]
session = self.patient_cql_connection(node1)
create_ks(session, 'ks', 1)
self.insert_row_with_oversize_value("CREATE TABLE %s(a int, b int, c text, PRIMARY KEY (a))",
"CREATE INDEX ON %s(c)",
"INSERT INTO %s (a, b, c) VALUES (0, 0, ?)",
session)
self.insert_row_with_oversize_value("CREATE TABLE %s(a int, b text, c int, PRIMARY KEY (a, b))",
"CREATE INDEX ON %s(b)",
"INSERT INTO %s (a, b, c) VALUES (0, ?, 0)",
session)
self.insert_row_with_oversize_value("CREATE TABLE %s(a text, b int, c int, PRIMARY KEY ((a, b)))",
"CREATE INDEX ON %s(a)",
"INSERT INTO %s (a, b, c) VALUES (?, 0, 0)",
session)
self.insert_row_with_oversize_value("CREATE TABLE %s(a int, b text, PRIMARY KEY (a)) WITH COMPACT STORAGE",
"CREATE INDEX ON %s(b)",
"INSERT INTO %s (a, b) VALUES (0, ?)",
session)
def insert_row_with_oversize_value(self, create_table_cql, create_index_cql, insert_cql, session):
""" Validate two variations of the supplied insert statement, first
as it is and then again transformed into a conditional statement
"""
table_name = "table_" + str(int(round(time.time() * 1000)))
session.execute(create_table_cql % table_name)
session.execute(create_index_cql % table_name)
value = "X" * 65536
self._assert_invalid_request(session, insert_cql % table_name, value)
self._assert_invalid_request(session, (insert_cql % table_name) + ' IF NOT EXISTS', value)
def _assert_invalid_request(self, session, insert_cql, value):
""" Perform two executions of the supplied statement, as a
single statement and again as part of a batch
"""
prepared = session.prepare(insert_cql)
self._execute_and_fail(lambda: session.execute(prepared, [value]), insert_cql)
batch = BatchStatement()
batch.add(prepared, [value])
self._execute_and_fail(lambda: session.execute(batch), insert_cql)
def _execute_and_fail(self, operation, cql_string):
try:
operation()
self.fail("Expecting query {} to be invalid".format(cql_string))
except AssertionError as e:
raise e
except InvalidRequest:
pass
def wait_for_schema_agreement(self, session):
rows = list(session.execute("SELECT schema_version FROM system.local"))
local_version = rows[0]
all_match = True
rows = list(session.execute("SELECT schema_version FROM system.peers"))
for peer_version in rows:
if peer_version != local_version:
all_match = False
break
if all_match:
return
else:
time.sleep(1)
self.wait_for_schema_agreement(session)
@since('3.0')
def test_manual_rebuild_index(self):
"""
asserts that new sstables are written when rebuild_index is called from nodetool
"""
cluster = self.cluster
cluster.populate(1).start()
node1, = cluster.nodelist()
session = self.patient_cql_connection(node1)
node1.stress(['write', 'n=50K', 'no-warmup'])
session.execute("use keyspace1;")
lookup_value = session.execute('select "C0" from standard1 limit 1')[0].C0
session.execute('CREATE INDEX ix_c0 ON standard1("C0");')
start = time.time()
while time.time() < start + 30:
debug("waiting for index to build")
time.sleep(1)
if index_is_built(node1, session, 'keyspace1', 'standard1', 'ix_c0'):
break
else:
raise DtestTimeoutError()
stmt = session.prepare('select * from standard1 where "C0" = ?')
self.assertEqual(1, len(list(session.execute(stmt, [lookup_value]))))
before_files = self._index_sstables_files(node1, 'keyspace1', 'standard1', 'ix_c0')
node1.nodetool("rebuild_index keyspace1 standard1 ix_c0")
start = time.time()
while time.time() < start + 30:
debug("waiting for index to rebuild")
time.sleep(1)
if index_is_built(node1, session, 'keyspace1', 'standard1', 'ix_c0'):
break
else:
raise DtestTimeoutError()
after_files = self._index_sstables_files(node1, 'keyspace1', 'standard1', 'ix_c0')
self.assertNotEqual(before_files, after_files)
self.assertEqual(1, len(list(session.execute(stmt, [lookup_value]))))
# verify that only the expected row is present in the build indexes table
self.assertEqual(1, len(list(session.execute("""SELECT * FROM system."IndexInfo";"""))))
@since('4.0')
def test_failing_manual_rebuild_index(self):
"""
@jira_ticket CASSANDRA-10130
Tests the management of index status during manual index rebuilding failures.
"""
cluster = self.cluster
cluster.populate(1, install_byteman=True).start(wait_for_binary_proto=True)
node = cluster.nodelist()[0]
session = self.patient_cql_connection(node)
create_ks(session, 'k', 1)
session.execute("CREATE TABLE k.t (k int PRIMARY KEY, v int)")
session.execute("CREATE INDEX idx ON k.t(v)")
session.execute("INSERT INTO k.t(k, v) VALUES (0, 1)")
session.execute("INSERT INTO k.t(k, v) VALUES (2, 3)")
# Verify that the index is marked as built and it can answer queries
assert_one(session, """SELECT * FROM system."IndexInfo" WHERE table_name='k'""", ['k', 'idx'])
assert_one(session, "SELECT * FROM k.t WHERE v = 1", [0, 1])
# Simulate a failing index rebuild
before_files = self._index_sstables_files(node, 'k', 't', 'idx')
node.byteman_submit(['./byteman/index_build_failure.btm'])
with self.assertRaises(Exception):
node.nodetool("rebuild_index k t idx")
after_files = self._index_sstables_files(node, 'k', 't', 'idx')
# Verify that the index is not rebuilt, not marked as built, and it still can answer queries
self.assertEqual(before_files, after_files)
assert_none(session, """SELECT * FROM system."IndexInfo" WHERE table_name='k'""")
assert_one(session, "SELECT * FROM k.t WHERE v = 1", [0, 1])
# Restart the node to trigger the scheduled index rebuild
before_files = after_files
node.nodetool('drain')
node.stop()
cluster.start()
session = self.patient_cql_connection(node)
session.execute("USE k")
after_files = self._index_sstables_files(node, 'k', 't', 'idx')
# Verify that, the index is rebuilt, marked as built, and it can answer queries
self.assertNotEqual(before_files, after_files)
assert_one(session, """SELECT * FROM system."IndexInfo" WHERE table_name='k'""", ['k', 'idx'])
assert_one(session, "SELECT * FROM k.t WHERE v = 1", [0, 1])
# Simulate another failing index rebuild
before_files = self._index_sstables_files(node, 'k', 't', 'idx')
node.byteman_submit(['./byteman/index_build_failure.btm'])
with self.assertRaises(Exception):
node.nodetool("rebuild_index k t idx")
after_files = self._index_sstables_files(node, 'k', 't', 'idx')
# Verify that the index is not rebuilt, not marked as built, and it still can answer queries
self.assertEqual(before_files, after_files)
assert_none(session, """SELECT * FROM system."IndexInfo" WHERE table_name='k'""")
assert_one(session, "SELECT * FROM k.t WHERE v = 1", [0, 1])
# Successfully rebuild the index
before_files = after_files
node.nodetool("rebuild_index k t idx")
cluster.wait_for_compactions()
after_files = self._index_sstables_files(node, 'k', 't', 'idx')
# Verify that the index is rebuilt, marked as built, and it can answer queries
self.assertNotEqual(before_files, after_files)
assert_one(session, """SELECT * FROM system."IndexInfo" WHERE table_name='k'""", ['k', 'idx'])
assert_one(session, "SELECT * FROM k.t WHERE v = 1", [0, 1])
@since('4.0')
def test_drop_index_while_building(self):
"""
asserts that indexes deleted before they have been completely build are invalidated and not built after restart
"""
cluster = self.cluster
cluster.populate(1).start()
node = cluster.nodelist()[0]
session = self.patient_cql_connection(node)
# Create some thousands of rows to guarantee a long index building
node.stress(['write', 'n=50K', 'no-warmup'])
session.execute("USE keyspace1")
# Create an index and immediately drop it, without waiting for index building
session.execute('CREATE INDEX idx ON standard1("C0")')
session.execute('DROP INDEX idx')
cluster.wait_for_compactions()
# Check that the index is not marked as built nor queryable
assert_none(session, """SELECT * FROM system."IndexInfo" WHERE table_name='keyspace1'""")
assert_invalid(session,
'SELECT * FROM standard1 WHERE "C0" = 0x00',
'Cannot execute this query as it might involve data filtering')
# Restart the node to trigger any eventual unexpected index rebuild
node.nodetool('drain')
node.stop()
cluster.start()
session = self.patient_cql_connection(node)
session.execute("USE keyspace1")
# The index should remain not built nor queryable after restart
assert_none(session, """SELECT * FROM system."IndexInfo" WHERE table_name='keyspace1'""")
assert_invalid(session,
'SELECT * FROM standard1 WHERE "C0" = 0x00',
'Cannot execute this query as it might involve data filtering')
@since('4.0')
def test_index_is_not_always_rebuilt_at_start(self):
"""
@jira_ticket CASSANDRA-10130
Tests the management of index status during manual index rebuilding failures.
"""
cluster = self.cluster
cluster.populate(1, install_byteman=True).start(wait_for_binary_proto=True)
node = cluster.nodelist()[0]
session = self.patient_cql_connection(node)
create_ks(session, 'k', 1)
session.execute("CREATE TABLE k.t (k int PRIMARY KEY, v int)")
session.execute("CREATE INDEX idx ON k.t(v)")
session.execute("INSERT INTO k.t(k, v) VALUES (0, 1)")
session.execute("INSERT INTO k.t(k, v) VALUES (2, 3)")
# Verify that the index is marked as built and it can answer queries
assert_one(session, """SELECT * FROM system."IndexInfo" WHERE table_name='k'""", ['k', 'idx'])
assert_one(session, "SELECT * FROM k.t WHERE v = 1", [0, 1])
# Restart the node to trigger any eventual undesired index rebuild
before_files = self._index_sstables_files(node, 'k', 't', 'idx')
node.nodetool('drain')
node.stop()
cluster.start()
session = self.patient_cql_connection(node)
session.execute("USE k")
after_files = self._index_sstables_files(node, 'k', 't', 'idx')
# Verify that, the index is not rebuilt, marked as built, and it can answer queries
self.assertNotEqual(before_files, after_files)
assert_one(session, """SELECT * FROM system."IndexInfo" WHERE table_name='k'""", ['k', 'idx'])
assert_one(session, "SELECT * FROM k.t WHERE v = 1", [0, 1])
def test_multi_index_filtering_query(self):
"""
asserts that having multiple indexes that cover all predicates still requires ALLOW FILTERING to also be present
"""
cluster = self.cluster
cluster.populate(1).start()
node1, = cluster.nodelist()
session = self.patient_cql_connection(node1)
session.execute("CREATE KEYSPACE ks WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': '1'};")
session.execute("USE ks;")
session.execute("CREATE TABLE tbl (id uuid primary key, c0 text, c1 text, c2 text);")
session.execute("CREATE INDEX ix_tbl_c0 ON tbl(c0);")
session.execute("CREATE INDEX ix_tbl_c1 ON tbl(c1);")
session.execute("INSERT INTO tbl (id, c0, c1, c2) values (uuid(), 'a', 'b', 'c');")
session.execute("INSERT INTO tbl (id, c0, c1, c2) values (uuid(), 'a', 'b', 'c');")
session.execute("INSERT INTO tbl (id, c0, c1, c2) values (uuid(), 'q', 'b', 'c');")
session.execute("INSERT INTO tbl (id, c0, c1, c2) values (uuid(), 'a', 'e', 'f');")
session.execute("INSERT INTO tbl (id, c0, c1, c2) values (uuid(), 'a', 'e', 'f');")
rows = list(session.execute("SELECT * FROM tbl WHERE c0 = 'a';"))
self.assertEqual(4, len(rows))
stmt = "SELECT * FROM tbl WHERE c0 = 'a' AND c1 = 'b';"
assert_invalid(session, stmt, "Cannot execute this query as it might involve data filtering and thus may have "
"unpredictable performance. If you want to execute this query despite the "
"performance unpredictability, use ALLOW FILTERING")
rows = list(session.execute("SELECT * FROM tbl WHERE c0 = 'a' AND c1 = 'b' ALLOW FILTERING;"))
self.assertEqual(2, len(rows))
@since('3.0')
def test_only_coordinator_chooses_index_for_query(self):
"""
Checks that the index to use is selected (once) on the coordinator and
included in the serialized command sent to the replicas.
@jira_ticket CASSANDRA-10215
"""
cluster = self.cluster
cluster.populate(3).start()
node1, node2, node3 = cluster.nodelist()
session = self.patient_exclusive_cql_connection(node3)
session.max_trace_wait = 120
session.execute("CREATE KEYSPACE ks WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': '1'};")
session.execute("CREATE TABLE ks.cf (a text PRIMARY KEY, b text);")
session.execute("CREATE INDEX b_index ON ks.cf (b);")
num_rows = 100
for i in range(num_rows):
indexed_value = i % (num_rows / 3)
# use the same indexed value three times
session.execute("INSERT INTO ks.cf (a, b) VALUES ('{a}', '{b}');"
.format(a=i, b=indexed_value))
cluster.flush()
def check_trace_events(trace, regex, expected_matches, on_failure):
"""
Check for the presence of certain trace events. expected_matches should be a list of
tuple(source, min_count, max_count) indicating that of all the trace events for the
the source, the supplied regex should match at least min_count trace messages & at
most max_count messages. E.g. [(127.0.0.1, 1, 10), (127.0.0.2, 0, 0)]
indicates that the regex should match at least 1, but no more than 10 events emitted
by node1, and that no messages emitted by node2 should match.
"""
match_counts = {}
for event_source, min_matches, max_matches in expected_matches:
match_counts[event_source] = 0
for event in trace.events:
desc = event.description
match = re.match(regex, desc)
if match:
if event.source in match_counts:
match_counts[event.source] += 1
for event_source, min_matches, max_matches in expected_matches:
if match_counts[event_source] < min_matches or match_counts[event_source] > max_matches:
on_failure(trace, regex, expected_matches, match_counts, event_source, min_matches, max_matches)
def halt_on_failure(trace, regex, expected_matches, match_counts, event_source, min_expected, max_expected):
self.fail("Expected to find between {min} and {max} trace events matching {pattern} from {source}, "
"but actually found {actual}. (Full counts: {all})"
.format(min=min_expected, max=max_expected, pattern=regex, source=event_source,
actual=match_counts[event_source], all=match_counts))
def retry_on_failure(trace, regex, expected_matches, match_counts, event_source, min_expected, max_expected):
debug("Trace event inspection did not match expected, sleeping before re-fetching trace events. "
"Expected: {expected} Actual: {actual}".format(expected=expected_matches, actual=match_counts))
time.sleep(2)
trace.populate(max_wait=2.0)
check_trace_events(trace, regex, expected_matches, halt_on_failure)
query = SimpleStatement("SELECT * FROM ks.cf WHERE b='1';")
result = session.execute(query, trace=True)
self.assertEqual(3, len(list(result)))
trace = result.get_query_trace()
# we have forced node3 to act as the coordinator for
# all requests by using an exclusive connection, so
# only node3 should select the index to use
check_trace_events(trace,
"Index mean cardinalities are b_index:[0-9]*. Scanning with b_index.",
[("127.0.0.1", 0, 0), ("127.0.0.2", 0, 0), ("127.0.0.3", 1, 1)],
retry_on_failure)
# check that the index is used on each node, really we only care that the matching
# message appears on every node, so the max count is not important
check_trace_events(trace,
"Executing read on ks.cf using index b_index",
[("127.0.0.1", 1, 200), ("127.0.0.2", 1, 200), ("127.0.0.3", 1, 200)],
retry_on_failure)
@skipIf(DISABLE_VNODES, "Test should only run with vnodes")
def test_query_indexes_with_vnodes(self):
"""
Verifies correct query behaviour in the presence of vnodes
@jira_ticket CASSANDRA-11104
"""
cluster = self.cluster
cluster.populate(2).start()
node1, node2 = cluster.nodelist()
session = self.patient_cql_connection(node1)
session.execute("CREATE KEYSPACE ks WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': '1'};")
session.execute("CREATE TABLE ks.compact_table (a int PRIMARY KEY, b int) WITH COMPACT STORAGE;")
session.execute("CREATE INDEX keys_index ON ks.compact_table (b);")
session.execute("CREATE TABLE ks.regular_table (a int PRIMARY KEY, b int)")
session.execute("CREATE INDEX composites_index on ks.regular_table (b)")
for node in cluster.nodelist():
start = time.time()
while time.time() < start + 10:
debug("waiting for index to build")
time.sleep(1)
if index_is_built(node, session, 'ks', 'regular_table', 'composites_index'):
break
else:
raise DtestTimeoutError()
insert_args = [(i, i % 2) for i in xrange(100)]
execute_concurrent_with_args(session,
session.prepare("INSERT INTO ks.compact_table (a, b) VALUES (?, ?)"),
insert_args)
execute_concurrent_with_args(session,
session.prepare("INSERT INTO ks.regular_table (a, b) VALUES (?, ?)"),
insert_args)
res = session.execute("SELECT * FROM ks.compact_table WHERE b = 0")
self.assertEqual(len(rows_to_list(res)), 50)
res = session.execute("SELECT * FROM ks.regular_table WHERE b = 0")
self.assertEqual(len(rows_to_list(res)), 50)
class TestSecondaryIndexesOnCollections(Tester):
def test_tuple_indexes(self):
"""
Checks that secondary indexes on tuples work for querying
"""
cluster = self.cluster
cluster.populate(1).start()
[node1] = cluster.nodelist()
session = self.patient_cql_connection(node1)
create_ks(session, 'tuple_index_test', 1)
session.execute("use tuple_index_test")
session.execute("""
CREATE TABLE simple_with_tuple (
id uuid primary key,
normal_col int,
single_tuple tuple<int>,
double_tuple tuple<int, int>,
triple_tuple tuple<int, int, int>,
nested_one tuple<int, tuple<int, int>>
)""")
cmds = [("""insert into simple_with_tuple
(id, normal_col, single_tuple, double_tuple, triple_tuple, nested_one)
values
(uuid(), {0}, ({0}), ({0},{0}), ({0},{0},{0}), ({0},({0},{0})))""".format(n), ())
for n in range(50)]
results = execute_concurrent(session, cmds * 5, raise_on_first_error=True, concurrency=200)
for (success, result) in results:
self.assertTrue(success, "didn't get success on insert: {0}".format(result))
session.execute("CREATE INDEX idx_single_tuple ON simple_with_tuple(single_tuple);")
session.execute("CREATE INDEX idx_double_tuple ON simple_with_tuple(double_tuple);")
session.execute("CREATE INDEX idx_triple_tuple ON simple_with_tuple(triple_tuple);")
session.execute("CREATE INDEX idx_nested_tuple ON simple_with_tuple(nested_one);")
time.sleep(10)
# check if indexes work on existing data
for n in range(50):
self.assertEqual(5, len(list(session.execute("select * from simple_with_tuple where single_tuple = ({0});".format(n)))))
self.assertEqual(0, len(list(session.execute("select * from simple_with_tuple where single_tuple = (-1);".format(n)))))
self.assertEqual(5, len(list(session.execute("select * from simple_with_tuple where double_tuple = ({0},{0});".format(n)))))
self.assertEqual(0, len(list(session.execute("select * from simple_with_tuple where double_tuple = ({0},-1);".format(n)))))
self.assertEqual(5, len(list(session.execute("select * from simple_with_tuple where triple_tuple = ({0},{0},{0});".format(n)))))
self.assertEqual(0, len(list(session.execute("select * from simple_with_tuple where triple_tuple = ({0},{0},-1);".format(n)))))
self.assertEqual(5, len(list(session.execute("select * from simple_with_tuple where nested_one = ({0},({0},{0}));".format(n)))))
self.assertEqual(0, len(list(session.execute("select * from simple_with_tuple where nested_one = ({0},({0},-1));".format(n)))))
# check if indexes work on new data inserted after index creation
results = execute_concurrent(session, cmds * 3, raise_on_first_error=True, concurrency=200)
for (success, result) in results:
self.assertTrue(success, "didn't get success on insert: {0}".format(result))
time.sleep(5)
for n in range(50):
self.assertEqual(8, len(list(session.execute("select * from simple_with_tuple where single_tuple = ({0});".format(n)))))
self.assertEqual(8, len(list(session.execute("select * from simple_with_tuple where double_tuple = ({0},{0});".format(n)))))
self.assertEqual(8, len(list(session.execute("select * from simple_with_tuple where triple_tuple = ({0},{0},{0});".format(n)))))
self.assertEqual(8, len(list(session.execute("select * from simple_with_tuple where nested_one = ({0},({0},{0}));".format(n)))))
# check if indexes work on mutated data
for n in range(5):
rows = session.execute("select * from simple_with_tuple where single_tuple = ({0});".format(n))
for row in rows:
session.execute("update simple_with_tuple set single_tuple = (-999) where id = {0}".format(row.id))
rows = session.execute("select * from simple_with_tuple where double_tuple = ({0},{0});".format(n))
for row in rows:
session.execute("update simple_with_tuple set double_tuple = (-999,-999) where id = {0}".format(row.id))
rows = session.execute("select * from simple_with_tuple where triple_tuple = ({0},{0},{0});".format(n))
for row in rows:
session.execute("update simple_with_tuple set triple_tuple = (-999,-999,-999) where id = {0}".format(row.id))
rows = session.execute("select * from simple_with_tuple where nested_one = ({0},({0},{0}));".format(n))
for row in rows:
session.execute("update simple_with_tuple set nested_one = (-999,(-999,-999)) where id = {0}".format(row.id))
for n in range(5):
self.assertEqual(0, len(list(session.execute("select * from simple_with_tuple where single_tuple = ({0});".format(n)))))
self.assertEqual(0, len(list(session.execute("select * from simple_with_tuple where double_tuple = ({0},{0});".format(n)))))
self.assertEqual(0, len(list(session.execute("select * from simple_with_tuple where triple_tuple = ({0},{0},{0});".format(n)))))
self.assertEqual(0, len(list(session.execute("select * from simple_with_tuple where nested_one = ({0},({0},{0}));".format(n)))))
self.assertEqual(40, len(list(session.execute("select * from simple_with_tuple where single_tuple = (-999);"))))
self.assertEqual(40, len(list(session.execute("select * from simple_with_tuple where double_tuple = (-999,-999);"))))
self.assertEqual(40, len(list(session.execute("select * from simple_with_tuple where triple_tuple = (-999,-999,-999);"))))
self.assertEqual(40, len(list(session.execute("select * from simple_with_tuple where nested_one = (-999,(-999,-999));"))))
def test_list_indexes(self):
"""
Checks that secondary indexes on lists work for querying.
"""
cluster = self.cluster
cluster.populate(1).start()
[node1] = cluster.nodelist()
session = self.patient_cql_connection(node1)
create_ks(session, 'list_index_search', 1)
stmt = ("CREATE TABLE list_index_search.users ("
"user_id uuid PRIMARY KEY,"
"email text,"
"uuids list<uuid>"
");")
session.execute(stmt)
# add index and query again (even though there are no rows in the table yet)
stmt = "CREATE INDEX user_uuids on list_index_search.users (uuids);"
session.execute(stmt)
stmt = ("SELECT * from list_index_search.users where uuids contains {some_uuid}").format(some_uuid=uuid.uuid4())
row = list(session.execute(stmt))
self.assertEqual(0, len(row))
# add a row which doesn't specify data for the indexed column, and query again
user1_uuid = uuid.uuid4()
stmt = ("INSERT INTO list_index_search.users (user_id, email)"
"values ({user_id}, '[email protected]')"
).format(user_id=user1_uuid)
session.execute(stmt)
stmt = ("SELECT * from list_index_search.users where uuids contains {some_uuid}").format(some_uuid=uuid.uuid4())
row = list(session.execute(stmt))
self.assertEqual(0, len(row))
_id = uuid.uuid4()
# alter the row to add a single item to the indexed list
stmt = ("UPDATE list_index_search.users set uuids = [{id}] where user_id = {user_id}"
).format(id=_id, user_id=user1_uuid)
session.execute(stmt)
stmt = ("SELECT * from list_index_search.users where uuids contains {some_uuid}").format(some_uuid=_id)
row = list(session.execute(stmt))
self.assertEqual(1, len(row))
# add a bunch of user records and query them back
shared_uuid = uuid.uuid4() # this uuid will be on all records
log = []
for i in range(50000):
user_uuid = uuid.uuid4()
unshared_uuid = uuid.uuid4()
# give each record a unique email address using the int index
stmt = ("INSERT INTO list_index_search.users (user_id, email, uuids)"
"values ({user_uuid}, '{prefix}@example.com', [{s_uuid}, {u_uuid}])"
).format(user_uuid=user_uuid, prefix=i, s_uuid=shared_uuid, u_uuid=unshared_uuid)
session.execute(stmt)
log.append(
{'user_id': user_uuid,
'email': str(i) + '@example.com',
'unshared_uuid': unshared_uuid}
)
# confirm there is now 50k rows with the 'shared' uuid above in the secondary index
stmt = ("SELECT * from list_index_search.users where uuids contains {shared_uuid}").format(shared_uuid=shared_uuid)
rows = list(session.execute(stmt))
result = [row for row in rows]
self.assertEqual(50000, len(result))
# shuffle the log in-place, and double-check a slice of records by querying the secondary index
random.shuffle(log)
for log_entry in log[:1000]:
stmt = ("SELECT user_id, email, uuids FROM list_index_search.users where uuids contains {unshared_uuid}"
).format(unshared_uuid=log_entry['unshared_uuid'])
rows = list(session.execute(stmt))
self.assertEqual(1, len(rows))
db_user_id, db_email, db_uuids = rows[0]
self.assertEqual(db_user_id, log_entry['user_id'])
self.assertEqual(db_email, log_entry['email'])
self.assertEqual(str(db_uuids[0]), str(shared_uuid))
self.assertEqual(str(db_uuids[1]), str(log_entry['unshared_uuid']))
def test_set_indexes(self):
"""
Checks that secondary indexes on sets work for querying.
"""
cluster = self.cluster
cluster.populate(1).start()
[node1] = cluster.nodelist()
session = self.patient_cql_connection(node1)
create_ks(session, 'set_index_search', 1)
stmt = ("CREATE TABLE set_index_search.users ("
"user_id uuid PRIMARY KEY,"
"email text,"
"uuids set<uuid>);")
session.execute(stmt)
# add index and query again (even though there are no rows in the table yet)
stmt = "CREATE INDEX user_uuids on set_index_search.users (uuids);"
session.execute(stmt)
stmt = ("SELECT * from set_index_search.users where uuids contains {some_uuid}").format(some_uuid=uuid.uuid4())
row = list(session.execute(stmt))
self.assertEqual(0, len(row))
# add a row which doesn't specify data for the indexed column, and query again
user1_uuid = uuid.uuid4()
stmt = ("INSERT INTO set_index_search.users (user_id, email) values ({user_id}, '[email protected]')"
).format(user_id=user1_uuid)
session.execute(stmt)
stmt = ("SELECT * from set_index_search.users where uuids contains {some_uuid}").format(some_uuid=uuid.uuid4())
row = list(session.execute(stmt))
self.assertEqual(0, len(row))
_id = uuid.uuid4()
# alter the row to add a single item to the indexed set
stmt = ("UPDATE set_index_search.users set uuids = {{{id}}} where user_id = {user_id}").format(id=_id, user_id=user1_uuid)
session.execute(stmt)
stmt = ("SELECT * from set_index_search.users where uuids contains {some_uuid}").format(some_uuid=_id)
row = list(session.execute(stmt))
self.assertEqual(1, len(row))
# add a bunch of user records and query them back
shared_uuid = uuid.uuid4() # this uuid will be on all records
log = []
for i in range(50000):
user_uuid = uuid.uuid4()
unshared_uuid = uuid.uuid4()
# give each record a unique email address using the int index
stmt = ("INSERT INTO set_index_search.users (user_id, email, uuids)"
"values ({user_uuid}, '{prefix}@example.com', {{{s_uuid}, {u_uuid}}})"
).format(user_uuid=user_uuid, prefix=i, s_uuid=shared_uuid, u_uuid=unshared_uuid)
session.execute(stmt)
log.append(
{'user_id': user_uuid,
'email': str(i) + '@example.com',
'unshared_uuid': unshared_uuid}
)
# confirm there is now 50k rows with the 'shared' uuid above in the secondary index
stmt = ("SELECT * from set_index_search.users where uuids contains {shared_uuid}").format(shared_uuid=shared_uuid)
rows = session.execute(stmt)
result = [row for row in rows]
self.assertEqual(50000, len(result))
# shuffle the log in-place, and double-check a slice of records by querying the secondary index
random.shuffle(log)
for log_entry in log[:1000]:
stmt = ("SELECT user_id, email, uuids FROM set_index_search.users where uuids contains {unshared_uuid}"
).format(unshared_uuid=log_entry['unshared_uuid'])
rows = list(session.execute(stmt))
self.assertEqual(1, len(rows))
db_user_id, db_email, db_uuids = rows[0]
self.assertEqual(db_user_id, log_entry['user_id'])
self.assertEqual(db_email, log_entry['email'])
self.assertTrue(shared_uuid in db_uuids)
self.assertTrue(log_entry['unshared_uuid'] in db_uuids)
@since('3.0')
def test_multiple_indexes_on_single_map_column(self):
"""
verifying functionality of multiple unique secondary indexes on a single column
@jira_ticket CASSANDRA-7771
@since 3.0
"""
cluster = self.cluster
cluster.populate(1).start()
[node1] = cluster.nodelist()
session = self.patient_cql_connection(node1)
create_ks(session, 'map_double_index', 1)
session.execute("""
CREATE TABLE map_tbl (
id uuid primary key,
amap map<text, int>
)
""")
session.execute("CREATE INDEX map_keys ON map_tbl(keys(amap))")
session.execute("CREATE INDEX map_values ON map_tbl(amap)")
session.execute("CREATE INDEX map_entries ON map_tbl(entries(amap))")
# multiple indexes on a single column are allowed but identical duplicate indexes are not
assert_invalid(session,
"CREATE INDEX map_values_2 ON map_tbl(amap)",
'Index map_values_2 is a duplicate of existing index map_values')
session.execute("INSERT INTO map_tbl (id, amap) values (uuid(), {'foo': 1, 'bar': 2});")
session.execute("INSERT INTO map_tbl (id, amap) values (uuid(), {'faz': 1, 'baz': 2});")
value_search = list(session.execute("SELECT * FROM map_tbl WHERE amap CONTAINS 1"))
self.assertEqual(2, len(value_search), "incorrect number of rows when querying on map values")
key_search = list(session.execute("SELECT * FROM map_tbl WHERE amap CONTAINS KEY 'foo'"))
self.assertEqual(1, len(key_search), "incorrect number of rows when querying on map keys")
entries_search = list(session.execute("SELECT * FROM map_tbl WHERE amap['foo'] = 1"))
self.assertEqual(1, len(entries_search), "incorrect number of rows when querying on map entries")
session.cluster.refresh_schema_metadata()
table_meta = session.cluster.metadata.keyspaces["map_double_index"].tables["map_tbl"]
self.assertEqual(3, len(table_meta.indexes))
self.assertItemsEqual(['map_keys', 'map_values', 'map_entries'], table_meta.indexes)
self.assertEqual(3, len(session.cluster.metadata.keyspaces["map_double_index"].indexes))
self.assertTrue('map_keys' in table_meta.export_as_string())
self.assertTrue('map_values' in table_meta.export_as_string())
self.assertTrue('map_entries' in table_meta.export_as_string())
session.execute("DROP TABLE map_tbl")
session.cluster.refresh_schema_metadata()
self.assertEqual(0, len(session.cluster.metadata.keyspaces["map_double_index"].indexes))
@skipIf(OFFHEAP_MEMTABLES, 'Hangs with offheap memtables')
def test_map_indexes(self):
"""
Checks that secondary indexes on maps work for querying on both keys and values
"""
cluster = self.cluster
cluster.populate(1).start()
[node1] = cluster.nodelist()
session = self.patient_cql_connection(node1)
create_ks(session, 'map_index_search', 1)
stmt = ("CREATE TABLE map_index_search.users ("
"user_id uuid PRIMARY KEY,"
"email text,"
"uuids map<uuid, uuid>);")
session.execute(stmt)
# add index on keys and query again (even though there are no rows in the table yet)
stmt = "CREATE INDEX user_uuids on map_index_search.users (KEYS(uuids));"
session.execute(stmt)
stmt = "SELECT * from map_index_search.users where uuids contains key {some_uuid}".format(some_uuid=uuid.uuid4())
rows = list(session.execute(stmt))
self.assertEqual(0, len(rows))
# add a row which doesn't specify data for the indexed column, and query again
user1_uuid = uuid.uuid4()
stmt = ("INSERT INTO map_index_search.users (user_id, email)"
"values ({user_id}, '[email protected]')"
).format(user_id=user1_uuid)
session.execute(stmt)
stmt = ("SELECT * from map_index_search.users where uuids contains key {some_uuid}").format(some_uuid=uuid.uuid4())
rows = list(session.execute(stmt))
self.assertEqual(0, len(rows))
_id = uuid.uuid4()
# alter the row to add a single item to the indexed map
stmt = ("UPDATE map_index_search.users set uuids = {{{id}:{user_id}}} where user_id = {user_id}"
).format(id=_id, user_id=user1_uuid)
session.execute(stmt)
stmt = ("SELECT * from map_index_search.users where uuids contains key {some_uuid}").format(some_uuid=_id)
rows = list(session.execute(stmt))
self.assertEqual(1, len(rows))