forked from apache/cassandra-dtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttl_test.py
617 lines (526 loc) · 24.8 KB
/
ttl_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
import os
import time
import pytest
import logging
from collections import OrderedDict
from distutils import dir_util
from distutils.version import LooseVersion
from cassandra import ConsistencyLevel, InvalidRequest
from cassandra.query import SimpleStatement
from cassandra.util import sortedset
from dtest import Tester, create_ks
from tools.assertions import (assert_all, assert_almost_equal, assert_none,
assert_row_count, assert_unavailable)
since = pytest.mark.since
logger = logging.getLogger(__name__)
from scrub_test import TestHelper
@since('2.0')
class TestTTL(Tester):
""" Test Time To Live Feature """
@pytest.fixture(scope='function', autouse=True)
def fixture_ttl_test_setup(self, fixture_dtest_setup):
self.cluster = fixture_dtest_setup.cluster
self.fixture_dtest_setup = fixture_dtest_setup
self.cluster.populate(1).start()
[node1] = self.cluster.nodelist()
self.session1 = self.patient_cql_connection(node1)
create_ks(self.session1, 'ks', 1)
def prepare(self, default_time_to_live=None):
self.session1.execute("DROP TABLE IF EXISTS ttl_table;")
query = """
CREATE TABLE ttl_table (
key int primary key,
col1 int,
col2 int,
col3 int,
)
"""
if default_time_to_live:
query += " WITH default_time_to_live = {};".format(default_time_to_live)
self.session1.execute(query)
def smart_sleep(self, start_time, time_to_wait):
""" Function that sleep smartly based on the start_time.
Useful when tests are slower than expected.
start_time: The start time of the timed operations
time_to_wait: The time to wait in seconds from the start_time
"""
now = time.time()
real_time_to_wait = time_to_wait - (now - start_time)
if real_time_to_wait > 0:
time.sleep(real_time_to_wait)
def test_default_ttl(self):
""" Test default_time_to_live specified on a table """
self.prepare(default_time_to_live=1)
start = time.time()
self.session1.execute("INSERT INTO ttl_table (key, col1) VALUES (%d, %d)" % (1, 1))
self.session1.execute("INSERT INTO ttl_table (key, col1) VALUES (%d, %d)" % (2, 2))
self.session1.execute("INSERT INTO ttl_table (key, col1) VALUES (%d, %d)" % (3, 3))
self.smart_sleep(start, 3)
assert_row_count(self.session1, 'ttl_table', 0)
def test_insert_ttl_has_priority_on_defaut_ttl(self):
""" Test that a ttl specified during an insert has priority on the default table ttl """
self.prepare(default_time_to_live=1)
start = time.time()
self.session1.execute("""
INSERT INTO ttl_table (key, col1) VALUES (%d, %d) USING TTL 5;
""" % (1, 1))
self.smart_sleep(start, 2)
assert_row_count(self.session1, 'ttl_table', 1) # should still exist
self.smart_sleep(start, 7)
assert_row_count(self.session1, 'ttl_table', 0)
def test_insert_ttl_works_without_default_ttl(self):
""" Test that a ttl specified during an insert works even if a table has no default ttl """
self.prepare()
start = time.time()
self.session1.execute("""
INSERT INTO ttl_table (key, col1) VALUES (%d, %d) USING TTL 1;
""" % (1, 1))
self.smart_sleep(start, 3)
assert_row_count(self.session1, 'ttl_table', 0)
def test_default_ttl_can_be_removed(self):
""" Test that default_time_to_live can be removed """
self.prepare(default_time_to_live=1)
start = time.time()
self.session1.execute("ALTER TABLE ttl_table WITH default_time_to_live = 0;")
self.session1.execute("""
INSERT INTO ttl_table (key, col1) VALUES (%d, %d);
""" % (1, 1))
self.smart_sleep(start, 1.5)
assert_row_count(self.session1, 'ttl_table', 1)
def test_removing_default_ttl_does_not_affect_existing_rows(self):
""" Test that removing a default_time_to_live doesn't affect the existings rows """
self.prepare(default_time_to_live=1)
self.session1.execute("ALTER TABLE ttl_table WITH default_time_to_live = 10;")
start = time.time()
self.session1.execute("""
INSERT INTO ttl_table (key, col1) VALUES (%d, %d);
""" % (1, 1))
self.session1.execute("""
INSERT INTO ttl_table (key, col1) VALUES (%d, %d) USING TTL 15;
""" % (2, 1))
self.session1.execute("ALTER TABLE ttl_table WITH default_time_to_live = 0;")
self.session1.execute("INSERT INTO ttl_table (key, col1) VALUES (%d, %d);" % (3, 1))
self.smart_sleep(start, 5)
assert_row_count(self.session1, 'ttl_table', 3)
self.smart_sleep(start, 12)
assert_row_count(self.session1, 'ttl_table', 2)
self.smart_sleep(start, 20)
assert_row_count(self.session1, 'ttl_table', 1)
def test_update_single_column_ttl(self):
""" Test that specifying a TTL on a single column works """
self.prepare()
self.session1.execute("""
INSERT INTO ttl_table (key, col1, col2, col3) VALUES (%d, %d, %d, %d);
""" % (1, 1, 1, 1))
start = time.time()
self.session1.execute("UPDATE ttl_table USING TTL 3 set col1=42 where key=%s;" % (1,))
assert_all(self.session1, "SELECT * FROM ttl_table;", [[1, 42, 1, 1]])
self.smart_sleep(start, 5)
assert_all(self.session1, "SELECT * FROM ttl_table;", [[1, None, 1, 1]])
def test_update_multiple_columns_ttl(self):
""" Test that specifying a TTL on multiple columns works """
self.prepare()
self.session1.execute("""
INSERT INTO ttl_table (key, col1, col2, col3) VALUES (%d, %d, %d, %d);
""" % (1, 1, 1, 1))
start = time.time()
self.session1.execute("""
UPDATE ttl_table USING TTL 2 set col1=42, col2=42, col3=42 where key=%s;
""" % (1,))
assert_all(self.session1, "SELECT * FROM ttl_table;", [[1, 42, 42, 42]])
self.smart_sleep(start, 4)
assert_all(self.session1, "SELECT * FROM ttl_table;", [[1, None, None, None]])
def test_update_column_ttl_with_default_ttl(self):
"""
Test that specifying a column ttl works when a default ttl is set.
This test specify a lower ttl for the column than the default ttl.
"""
self.prepare(default_time_to_live=8)
start = time.time()
self.session1.execute("""
INSERT INTO ttl_table (key, col1, col2, col3) VALUES (%d, %d, %d, %d);
""" % (1, 1, 1, 1))
self.session1.execute("UPDATE ttl_table USING TTL 3 set col1=42 where key=%s;" % (1,))
assert_all(self.session1, "SELECT * FROM ttl_table;", [[1, 42, 1, 1]])
self.smart_sleep(start, 5)
assert_all(self.session1, "SELECT * FROM ttl_table;", [[1, None, 1, 1]])
self.smart_sleep(start, 10)
assert_row_count(self.session1, 'ttl_table', 0)
def update_column_ttl_with_default_ttl_test2(self):
"""
Test that specifying a column ttl works when a default ttl is set.
This test specify a higher column ttl than the default ttl.
"""
self.prepare(default_time_to_live=2)
start = time.time()
self.session1.execute("""
INSERT INTO ttl_table (key, col1, col2, col3) VALUES (%d, %d, %d, %d);
""" % (1, 1, 1, 1))
self.session1.execute("UPDATE ttl_table USING TTL 6 set col1=42 where key=%s;" % (1,))
self.smart_sleep(start, 4)
assert_all(self.session1, "SELECT * FROM ttl_table;", [[1, 42, None, None]])
self.smart_sleep(start, 8)
assert_row_count(self.session1, 'ttl_table', 0)
def test_remove_column_ttl(self):
"""
Test that removing a column ttl works.
"""
self.prepare()
start = time.time()
self.session1.execute("""
INSERT INTO ttl_table (key, col1, col2, col3) VALUES (%d, %d, %d, %d) USING TTL 2;
""" % (1, 1, 1, 1))
self.session1.execute("UPDATE ttl_table set col1=42 where key=%s;" % (1,))
self.smart_sleep(start, 4)
assert_all(self.session1, "SELECT * FROM ttl_table;", [[1, 42, None, None]])
@since('3.6')
def test_set_ttl_to_zero_to_default_ttl(self):
"""
Test that we can remove the default ttl by setting the ttl explicitly to zero.
CASSANDRA-11207
"""
self.prepare(default_time_to_live=2)
start = time.time()
self.session1.execute("INSERT INTO ttl_table (key, col1, col2, col3) VALUES ({}, {}, {}, {});".format(1, 1, 1, 1))
self.session1.execute("INSERT INTO ttl_table (key, col1, col2, col3) VALUES ({}, {}, {}, {});".format(2, 1, 1, 1))
self.session1.execute("UPDATE ttl_table using ttl 0 set col1=42 where key={};".format(1))
self.session1.execute("UPDATE ttl_table using ttl 3 set col1=42 where key={};".format(2))
self.smart_sleep(start, 5)
# The first row should be deleted, using ttl 0 should fallback to default_time_to_live
assert_all(self.session1, "SELECT * FROM ttl_table;", [[1, 42, None, None]])
@since('2.1', max_version='3.5')
def test_remove_column_ttl_with_default_ttl(self):
"""
Test that we cannot remove a column ttl when a default ttl is set.
"""
self.prepare(default_time_to_live=2)
start = time.time()
self.session1.execute("""
INSERT INTO ttl_table (key, col1, col2, col3) VALUES (%d, %d, %d, %d);
""" % (1, 1, 1, 1))
self.session1.execute("""
INSERT INTO ttl_table (key, col1, col2, col3) VALUES (%d, %d, %d, %d);
""" % (2, 1, 1, 1))
self.session1.execute("UPDATE ttl_table using ttl 0 set col1=42 where key=%s;" % (1,))
self.session1.execute("UPDATE ttl_table using ttl 8 set col1=42 where key=%s;" % (2,))
self.smart_sleep(start, 5)
# The first row should be deleted, using ttl 0 should fallback to default_time_to_live
assert_all(self.session1, "SELECT * FROM ttl_table;", [[2, 42, None, None]])
self.smart_sleep(start, 10)
assert_row_count(self.session1, 'ttl_table', 0)
def test_collection_list_ttl(self):
"""
Test that ttl has a granularity of elements using a list collection.
"""
self.prepare(default_time_to_live=10)
self.session1.execute("ALTER TABLE ttl_table ADD mylist list<int>;""")
start = time.time()
self.session1.execute("""
INSERT INTO ttl_table (key, col1, mylist) VALUES (%d, %d, %s);
""" % (1, 1, [1, 2, 3, 4, 5]))
self.session1.execute("""
UPDATE ttl_table USING TTL 5 SET mylist[0] = 42, mylist[4] = 42 WHERE key=1;
""")
assert_all(self.session1, "SELECT * FROM ttl_table;", [[1, 1, None, None, [42, 2, 3, 4, 42]]])
self.smart_sleep(start, 7)
assert_all(self.session1, "SELECT * FROM ttl_table;", [[1, 1, None, None, [2, 3, 4]]])
self.smart_sleep(start, 12)
assert_row_count(self.session1, 'ttl_table', 0)
def test_collection_set_ttl(self):
"""
Test that ttl has a granularity of elements using a set collection.
"""
self.prepare(default_time_to_live=10)
self.session1.execute("ALTER TABLE ttl_table ADD myset set<int>;""")
start = time.time()
self.session1.execute("""
INSERT INTO ttl_table (key, col1, myset) VALUES (%d, %d, %s);
""" % (1, 1, '{1,2,3,4,5}'))
self.session1.execute("""
UPDATE ttl_table USING TTL 3 SET myset = myset + {42} WHERE key=1;
""")
assert_all(
self.session1,
"SELECT * FROM ttl_table;",
[[1, 1, None, None, sortedset([1, 2, 3, 4, 5, 42])]]
)
self.smart_sleep(start, 5)
assert_all(
self.session1,
"SELECT * FROM ttl_table;",
[[1, 1, None, None, sortedset([1, 2, 3, 4, 5])]]
)
self.smart_sleep(start, 12)
assert_row_count(self.session1, 'ttl_table', 0)
def test_collection_map_ttl(self):
"""
Test that ttl has a granularity of elements using a map collection.
"""
self.prepare(default_time_to_live=6)
self.session1.execute("ALTER TABLE ttl_table ADD mymap map<int, int>;""")
start = time.time()
self.session1.execute("""
INSERT INTO ttl_table (key, col1, mymap) VALUES (%d, %d, %s);
""" % (1, 1, '{1:1,2:2,3:3,4:4,5:5}'))
self.session1.execute("""
UPDATE ttl_table USING TTL 2 SET mymap[1] = 42, mymap[5] = 42 WHERE key=1;
""")
assert_all(
self.session1,
"SELECT * FROM ttl_table;",
[[1, 1, None, None, OrderedDict([(1, 42), (2, 2), (3, 3), (4, 4), (5, 42)])]]
)
self.smart_sleep(start, 4)
assert_all(
self.session1,
"SELECT * FROM ttl_table;",
[[1, 1, None, None, OrderedDict([(2, 2), (3, 3), (4, 4)])]]
)
self.smart_sleep(start, 8)
assert_row_count(self.session1, 'ttl_table', 0)
def test_delete_with_ttl_expired(self):
"""
Updating a row with a ttl does not prevent deletion, test for CASSANDRA-6363
"""
self.session1.execute("DROP TABLE IF EXISTS session")
self.session1.execute("CREATE TABLE session (id text, usr text, valid int, PRIMARY KEY (id))")
self.session1.execute("insert into session (id, usr) values ('abc', 'abc')")
self.session1.execute("update session using ttl 1 set valid = 1 where id = 'abc'")
self.smart_sleep(time.time(), 2)
self.session1.execute("delete from session where id = 'abc' if usr ='abc'")
assert_row_count(self.session1, 'session', 0)
@since('2.1')
def test_expiration_overflow_policy_cap(self):
self._base_expiration_overflow_policy_test(default_ttl=False, policy='CAP')
@since('2.1')
def test_expiration_overflow_policy_cap_default_ttl(self):
self._base_expiration_overflow_policy_test(default_ttl=False, policy='CAP')
@since('3.0')
def test_expiration_overflow_policy_capnowarn(self):
self._base_expiration_overflow_policy_test(default_ttl=False, policy='CAP_NOWARN')
@since('3.0')
def test_expiration_overflow_policy_capnowarn_default_ttl(self):
self._base_expiration_overflow_policy_test(default_ttl=False, policy='CAP_NOWARN')
@since('2.1')
def test_expiration_overflow_policy_reject(self):
self._base_expiration_overflow_policy_test(default_ttl=False, policy='REJECT')
@since('2.1')
def test_expiration_overflow_policy_reject_default_ttl(self):
self._base_expiration_overflow_policy_test(default_ttl=False, policy='REJECT')
def _base_expiration_overflow_policy_test(self, default_ttl, policy):
"""
Checks that expiration date overflow policy is correctly applied
@jira_ticket CASSANDRA-14092
"""
MAX_TTL = 20 * 365 * 24 * 60 * 60 # 20 years in seconds
default_time_to_live = MAX_TTL if default_ttl else None
self.prepare(default_time_to_live=default_time_to_live)
# Restart node with expiration_date_overflow_policy
self.cluster.stop()
self.cluster.start(jvm_args=['-Dcassandra.expiration_date_overflow_policy={}'.format(policy)], wait_for_binary_proto=True)
self.session1 = self.patient_cql_connection(self.cluster.nodelist()[0])
self.session1.execute("USE ks;")
# Try to insert data, should only fail if policy is REJECT
query = 'INSERT INTO ttl_table (key, col1) VALUES (%d, %d)' % (1, 1)
if not default_time_to_live:
query = query + "USING TTL %d" % (MAX_TTL)
try:
result = self.session1.execute_async(query + ";")
result.result()
if policy == 'REJECT':
self.fail("should throw InvalidRequest")
if self.cluster.version() >= '3.0': # client warn only on 3.0+
if policy == 'CAP':
logger.debug("Warning is {}", result.warnings[0])
assert 'exceeds maximum supported expiration' in result.warnings[0], 'Warning not found'
else:
assert not result.warnings, "There should be no warnings"
except InvalidRequest as e:
if policy != 'REJECT':
self.fail("should not throw InvalidRequest")
self.cluster.flush()
# Data should be present unless policy is reject
assert_row_count(self.session1, 'ttl_table', 0 if policy == 'REJECT' else 1)
# Check that warning is always logged, unless policy is REJECT
if policy != 'REJECT':
node1 = self.cluster.nodelist()[0]
prefix = 'default ' if default_ttl else ''
warning = node1.grep_log("Request on table {}.{} with {}ttl of {} seconds exceeds maximum supported expiration"
.format('ks', 'ttl_table', prefix, MAX_TTL))
assert warning, 'Log message should be print for CAP and CAP_NOWARN policy'
class TestDistributedTTL(Tester):
""" Test Time To Live Feature in a distributed environment """
@pytest.fixture(scope='function', autouse=True)
def fixture_set_cluster_settings(self, fixture_dtest_setup):
fixture_dtest_setup.cluster.populate(2).start()
[self.node1, self.node2] = fixture_dtest_setup.cluster.nodelist()
self.session1 = fixture_dtest_setup.patient_cql_connection(self.node1)
create_ks(self.session1, 'ks', 2)
def prepare(self, default_time_to_live=None):
self.session1.execute("DROP TABLE IF EXISTS ttl_table;")
query = """
CREATE TABLE ttl_table (
key int primary key,
col1 int,
col2 int,
col3 int,
)
"""
if default_time_to_live:
query += " WITH default_time_to_live = {};".format(default_time_to_live)
self.session1.execute(query)
def test_ttl_is_replicated(self):
"""
Test that the ttl setting is replicated properly on all nodes
"""
self.prepare(default_time_to_live=5)
session1 = self.patient_exclusive_cql_connection(self.node1)
session2 = self.patient_exclusive_cql_connection(self.node2)
session1.execute("USE ks;")
session2.execute("USE ks;")
query = SimpleStatement(
"INSERT INTO ttl_table (key, col1) VALUES (1, 1);",
consistency_level=ConsistencyLevel.ALL
)
session1.execute(query)
assert_all(
session1,
"SELECT * FROM ttl_table;",
[[1, 1, None, None]],
cl=ConsistencyLevel.ALL
)
ttl_session1 = session1.execute('SELECT ttl(col1) FROM ttl_table;')
ttl_session2 = session2.execute('SELECT ttl(col1) FROM ttl_table;')
# since the two queries are not executed simultaneously, the remaining
# TTLs can differ by one second
assert abs(ttl_session1[0][0] - ttl_session2[0][0]) <= 1
time.sleep(7)
assert_none(session1, "SELECT * FROM ttl_table;", cl=ConsistencyLevel.ALL)
def test_ttl_is_respected_on_delayed_replication(self):
""" Test that ttl is respected on delayed replication """
self.prepare()
self.node2.stop()
self.session1.execute("""
INSERT INTO ttl_table (key, col1) VALUES (1, 1) USING TTL 5;
""")
self.session1.execute("""
INSERT INTO ttl_table (key, col1) VALUES (2, 2) USING TTL 1000;
""")
assert_all(
self.session1,
"SELECT * FROM ttl_table;",
[[1, 1, None, None], [2, 2, None, None]]
)
time.sleep(7)
self.node1.stop()
self.node2.start(wait_for_binary_proto=True)
session2 = self.patient_exclusive_cql_connection(self.node2)
session2.execute("USE ks;")
assert_row_count(session2, 'ttl_table', 0) # should be 0 since node1 is down, no replica yet
self.node1.start(wait_for_binary_proto=True)
self.session1 = self.patient_exclusive_cql_connection(self.node1)
self.session1.execute("USE ks;")
self.node1.cleanup()
assert_all(session2, "SELECT count(*) FROM ttl_table", [[1]], cl=ConsistencyLevel.ALL)
assert_all(
session2,
"SELECT * FROM ttl_table;",
[[2, 2, None, None]],
cl=ConsistencyLevel.ALL
)
# Check that the TTL on both server are the same
ttl_1 = self.session1.execute('SELECT ttl(col1) FROM ttl_table;')[0][0]
ttl_2 = session2.execute('SELECT ttl(col1) FROM ttl_table;')[0][0]
logger.debug("ttl_1 is {}:".format(ttl_1))
logger.debug("ttl_2 is {}:".format(ttl_2))
assert abs(ttl_1 - ttl_2) <= 1
def test_ttl_is_respected_on_repair(self):
""" Test that ttl is respected on repair """
self.prepare()
self.session1.execute("""
ALTER KEYSPACE ks WITH REPLICATION =
{'class' : 'SimpleStrategy', 'replication_factor' : 1};
""")
self.session1.execute("""
INSERT INTO ttl_table (key, col1) VALUES (1, 1) USING TTL 5;
""")
self.session1.execute("""
INSERT INTO ttl_table (key, col1) VALUES (2, 2) USING TTL 1000;
""")
assert_all(
self.session1,
"SELECT * FROM ttl_table;",
[[1, 1, None, None], [2, 2, None, None]]
)
time.sleep(7)
self.node1.stop()
session2 = self.patient_exclusive_cql_connection(self.node2)
session2.execute("USE ks;")
assert_unavailable(session2.execute, "SELECT * FROM ttl_table;")
self.node1.start(wait_for_binary_proto=True)
self.session1 = self.patient_exclusive_cql_connection(self.node1)
self.session1.execute("USE ks;")
self.session1.execute("""
ALTER KEYSPACE ks WITH REPLICATION =
{'class' : 'SimpleStrategy', 'replication_factor' : 2};
""")
self.node1.repair(['ks'])
ttl_start = time.time()
ttl_session1 = self.session1.execute('SELECT ttl(col1) FROM ttl_table;')
self.node1.stop()
assert_row_count(session2, 'ttl_table', 1)
assert_all(
session2,
"SELECT * FROM ttl_table;",
[[2, 2, None, None]]
)
# Check that the TTL on both server are the same
ttl_session2 = session2.execute('SELECT ttl(col1) FROM ttl_table;')
ttl_session1 = ttl_session1[0][0] - (time.time() - ttl_start)
assert_almost_equal(ttl_session1, ttl_session2[0][0], error=0.005)
class TestRecoverNegativeExpirationDate(TestHelper):
@since('2.1')
def test_recover_negative_expiration_date_sstables_with_scrub(self):
"""
@jira_ticket CASSANDRA-14092
Check that row with negative overflowed ttl is recovered by offline scrub
"""
cluster = self.cluster
cluster.populate(1).start(wait_for_binary_proto=True)
[node] = cluster.nodelist()
session = self.patient_cql_connection(node)
create_ks(session, 'ks', 1)
session.execute("DROP TABLE IF EXISTS ttl_table;")
query = """
CREATE TABLE ttl_table (
key int primary key,
col1 int,
col2 int,
col3 int,
)
"""
session.execute(query)
version = '2.1' if self.cluster.version() < LooseVersion('3.0') else \
('3.0' if self.cluster.version() < LooseVersion('3.11') else '3.11')
corrupt_sstable_dir = os.path.join('sstables', 'ttl_test', version)
table_dir = self.get_table_paths('ttl_table')[0]
logger.debug("Copying sstables from {} into {}", corrupt_sstable_dir, table_dir)
dir_util.copy_tree(corrupt_sstable_dir, table_dir)
logger.debug("Load corrupted sstable")
node.nodetool('refresh ks ttl_table')
node.watch_log_for('Loading new SSTables', timeout=10)
logger.debug("Check that there are no rows present")
assert_row_count(session, 'ttl_table', 0)
logger.debug("Shutting down node")
self.cluster.stop()
logger.debug("Will run offline scrub on sstable")
scrubbed_sstables = self.launch_standalone_scrub('ks', 'ttl_table',
reinsert_overflowed_ttl=True,
no_validate=True)
logger.debug("Executed offline scrub on {}", str(scrubbed_sstables))
logger.debug("Starting node again")
self.cluster.start(wait_for_binary_proto=True)
session = self.patient_cql_connection(node)
session.execute("USE ks;")
logger.debug("Check that row was recovered")
assert_all(session, "SELECT * FROM ttl_table;", [[1, 1, None, None]])