forked from apache/cassandra-dtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot_test.py
579 lines (473 loc) · 25.9 KB
/
snapshot_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
import distutils.dir_util
import glob
import os
import shutil
import subprocess
import time
from distutils.version import LooseVersion
import pytest
import logging
from cassandra.concurrent import execute_concurrent_with_args
from dtest_setup_overrides import DTestSetupOverrides
from dtest import Tester, create_ks
from tools.assertions import assert_one
from tools.files import replace_in_file, safe_mkdtemp
from tools.hacks import advance_to_next_cl_segment
from tools.misc import ImmutableMapping, get_current_test_name
since = pytest.mark.since
logger = logging.getLogger(__name__)
class SnapshotTester(Tester):
def create_schema(self, session):
create_ks(session, 'ks', 1)
session.execute('CREATE TABLE ks.cf ( key int PRIMARY KEY, val text);')
def insert_rows(self, session, start, end):
insert_statement = session.prepare("INSERT INTO ks.cf (key, val) VALUES (?, 'asdf')")
args = [(r,) for r in range(start, end)]
execute_concurrent_with_args(session, insert_statement, args, concurrency=20)
def make_snapshot(self, node, ks, cf, name):
logger.debug("Making snapshot....")
node.nodetool('disableautocompaction')
node.flush()
snapshot_cmd = 'snapshot {ks} -cf {cf} -t {name}'.format(ks=ks, cf=cf, name=name)
logger.debug("Running snapshot cmd: {snapshot_cmd}".format(snapshot_cmd=snapshot_cmd))
node.nodetool(snapshot_cmd)
tmpdir = safe_mkdtemp()
os.mkdir(os.path.join(tmpdir, ks))
os.mkdir(os.path.join(tmpdir, ks, cf))
# Find the snapshot dir, it's different in various C*
x = 0
for data_dir in node.data_directories():
snapshot_dir = "{data_dir}/{ks}/{cf}/snapshots/{name}".format(data_dir=data_dir, ks=ks, cf=cf, name=name)
if not os.path.isdir(snapshot_dir):
snapshot_dirs = glob.glob("{data_dir}/{ks}/{cf}-*/snapshots/{name}".format(data_dir=data_dir, ks=ks, cf=cf, name=name))
if len(snapshot_dirs) > 0:
snapshot_dir = snapshot_dirs[0]
else:
continue
logger.debug("snapshot_dir is : " + snapshot_dir)
logger.debug("snapshot copy is : " + tmpdir)
# Copy files from the snapshot dir to existing temp dir
distutils.dir_util.copy_tree(str(snapshot_dir), os.path.join(tmpdir, str(x), ks, cf))
x += 1
return tmpdir
def restore_snapshot(self, snapshot_dir, node, ks, cf):
logger.debug("Restoring snapshot....")
for x in range(0, self.cluster.data_dir_count):
snap_dir = os.path.join(snapshot_dir, str(x), ks, cf)
if os.path.exists(snap_dir):
ip = node.address()
args = [node.get_tool('sstableloader'), '-d', ip, snap_dir]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
exit_status = p.wait()
if exit_status != 0:
raise Exception("sstableloader command '%s' failed; exit status: %d'; stdout: %s; stderr: %s" %
(" ".join(args), exit_status, stdout.decode("utf-8"), stderr.decode("utf-8")))
def restore_snapshot_schema(self, snapshot_dir, node, ks, cf):
logger.debug("Restoring snapshot schema....")
for x in range(0, self.cluster.data_dir_count):
schema_path = os.path.join(snapshot_dir, str(x), ks, cf, 'schema.cql')
if os.path.exists(schema_path):
node.run_cqlsh(cmds="SOURCE '%s'" % schema_path)
class TestSnapshot(SnapshotTester):
def test_basic_snapshot_and_restore(self):
cluster = self.cluster
cluster.populate(1).start()
(node1,) = cluster.nodelist()
session = self.patient_cql_connection(node1)
self.create_schema(session)
self.insert_rows(session, 0, 100)
snapshot_dir = self.make_snapshot(node1, 'ks', 'cf', 'basic')
# Write more data after the snapshot, this will get thrown
# away when we restore:
self.insert_rows(session, 100, 200)
rows = session.execute('SELECT count(*) from ks.cf')
assert rows[0][0] == 200
# Drop the keyspace, make sure we have no data:
session.execute('DROP KEYSPACE ks')
self.create_schema(session)
rows = session.execute('SELECT count(*) from ks.cf')
assert rows[0][0] == 0
# Restore data from snapshot:
self.restore_snapshot(snapshot_dir, node1, 'ks', 'cf')
node1.nodetool('refresh ks cf')
rows = session.execute('SELECT count(*) from ks.cf')
# clean up
logger.debug("removing snapshot_dir: " + snapshot_dir)
shutil.rmtree(snapshot_dir)
assert rows[0][0] == 100
@since('3.0')
def test_snapshot_and_restore_drop_table_remove_dropped_column(self):
"""
@jira_ticket CASSANDRA-13730
Dropping table should clear entries in dropped_column table
"""
cluster = self.cluster
cluster.populate(1).start()
node1, = cluster.nodelist()
session = self.patient_cql_connection(node1)
# Create schema and insert some data
create_ks(session, 'ks', 1)
session.execute("CREATE TABLE ks.cf (k int PRIMARY KEY, a text, b text)")
session.execute("INSERT INTO ks.cf (k, a, b) VALUES (1, 'a', 'b')")
assert_one(session, "SELECT * FROM ks.cf", [1, "a", "b"])
# Take a snapshot and drop the column and then drop table
snapshot_dir = self.make_snapshot(node1, 'ks', 'cf', 'basic')
session.execute("ALTER TABLE ks.cf DROP b")
assert_one(session, "SELECT * FROM ks.cf", [1, "a"])
session.execute("DROP TABLE ks.cf")
# Restore schema and data from snapshot, data should be the same as input
self.restore_snapshot_schema(snapshot_dir, node1, 'ks', 'cf')
self.restore_snapshot(snapshot_dir, node1, 'ks', 'cf')
node1.nodetool('refresh ks cf')
assert_one(session, "SELECT * FROM ks.cf", [1, "a", "b"])
# Clean up
logger.debug("removing snapshot_dir: " + snapshot_dir)
shutil.rmtree(snapshot_dir)
@since('3.11')
def test_snapshot_and_restore_dropping_a_column(self):
"""
@jira_ticket CASSANDRA-13276
Can't load snapshots of tables with dropped columns.
"""
cluster = self.cluster
cluster.populate(1).start()
node1, = cluster.nodelist()
session = self.patient_cql_connection(node1)
# Create schema and insert some data
create_ks(session, 'ks', 1)
session.execute("CREATE TABLE ks.cf (k int PRIMARY KEY, a text, b text)")
session.execute("INSERT INTO ks.cf (k, a, b) VALUES (1, 'a', 'b')")
assert_one(session, "SELECT * FROM ks.cf", [1, "a", "b"])
# Drop a column
session.execute("ALTER TABLE ks.cf DROP b")
assert_one(session, "SELECT * FROM ks.cf", [1, "a"])
# Take a snapshot and drop the table
snapshot_dir = self.make_snapshot(node1, 'ks', 'cf', 'basic')
session.execute("DROP TABLE ks.cf")
# Restore schema and data from snapshot
self.restore_snapshot_schema(snapshot_dir, node1, 'ks', 'cf')
self.restore_snapshot(snapshot_dir, node1, 'ks', 'cf')
node1.nodetool('refresh ks cf')
assert_one(session, "SELECT * FROM ks.cf", [1, "a"])
# Clean up
logger.debug("removing snapshot_dir: " + snapshot_dir)
shutil.rmtree(snapshot_dir)
class TestArchiveCommitlog(SnapshotTester):
@pytest.fixture(scope='function', autouse=True)
def fixture_dtest_setup_overrides(self, dtest_config):
dtest_setup_overrides = DTestSetupOverrides()
dtest_setup_overrides.cluster_options = ImmutableMapping({'start_rpc': 'true'})
return dtest_setup_overrides
def make_snapshot(self, node, ks, cf, name):
logger.debug("Making snapshot....")
node.nodetool('disableautocompaction')
node.flush()
snapshot_cmd = 'snapshot {ks} -cf {cf} -t {name}'.format(ks=ks, cf=cf, name=name)
logger.debug("Running snapshot cmd: {snapshot_cmd}".format(snapshot_cmd=snapshot_cmd))
node.nodetool(snapshot_cmd)
tmpdirs = []
base_tmpdir = safe_mkdtemp()
data_dir_count = self.cluster.data_dir_count
if self.cluster.version() >= '4.0' and ks in ['system', 'system_schema']:
data_dir_count = 1
for x in range(0, data_dir_count):
tmpdir = os.path.join(base_tmpdir, str(x))
os.mkdir(tmpdir)
# Copy files from the snapshot dir to existing temp dir
distutils.dir_util.copy_tree(os.path.join(node.get_path(), 'data{0}'.format(x), ks), tmpdir)
tmpdirs.append(tmpdir)
return tmpdirs
def restore_snapshot(self, snapshot_dir, node, ks, cf, name):
logger.debug("Restoring snapshot for cf ....")
data_dir = os.path.join(node.get_path(), 'data{0}'.format(os.path.basename(snapshot_dir)))
cfs = [s for s in os.listdir(snapshot_dir) if s.startswith(cf + "-")]
if len(cfs) > 0:
cf_id = cfs[0]
glob_path = "{snapshot_dir}/{cf_id}/snapshots/{name}".format(snapshot_dir=snapshot_dir, cf_id=cf_id, name=name)
globbed = glob.glob(glob_path)
if len(globbed) > 0:
snapshot_dir = globbed[0]
if not os.path.exists(os.path.join(data_dir, ks)):
os.mkdir(os.path.join(data_dir, ks))
os.mkdir(os.path.join(data_dir, ks, cf_id))
logger.debug("snapshot_dir is : " + snapshot_dir)
distutils.dir_util.copy_tree(snapshot_dir, os.path.join(data_dir, ks, cf_id))
def test_archive_commitlog(self):
self.run_archive_commitlog(restore_point_in_time=False)
def test_archive_commitlog_with_active_commitlog(self):
"""
Copy the active commitlogs to the archive directory before restoration
"""
self.run_archive_commitlog(restore_point_in_time=False)
def test_dont_archive_commitlog(self):
"""
Run the archive commitlog test, but forget to add the restore commands
"""
self.run_archive_commitlog(restore_point_in_time=False, restore_archived_commitlog=False)
def test_archive_commitlog_point_in_time(self):
"""
Test archive commit log with restore_point_in_time setting
"""
self.run_archive_commitlog(restore_point_in_time=True)
def test_archive_commitlog_point_in_time_ln(self):
"""
Test archive commit log with restore_point_in_time setting
"""
self.run_archive_commitlog(restore_point_in_time=True, archive_command='ln')
@since('4.1')
def test_archive_commitlog_restore_skip_by_position(self):
"""
Test archive commit log not restored because of specified snapshot commit log position
"""
self.run_archive_commitlog(restore_point_in_time=True, specify_commitlog_position=True, archive_command='ln')
def run_archive_commitlog(self, restore_point_in_time=False, restore_archived_commitlog=True, specify_commitlog_position=False, archive_command='cp'):
"""
Run archive commit log restoration test
"""
cluster = self.cluster
cluster.populate(1)
(node1,) = cluster.nodelist()
# Create a temp directory for storing commitlog archives:
tmp_commitlog = safe_mkdtemp()
logger.debug("tmp_commitlog: " + tmp_commitlog)
# Edit commitlog_archiving.properties and set an archive
# command:
replace_in_file(os.path.join(node1.get_path(), 'conf', 'commitlog_archiving.properties'),
[(r'^archive_command=.*$', 'archive_command={archive_command} %path {tmp_commitlog}/%name'.format(
tmp_commitlog=tmp_commitlog, archive_command=archive_command))])
cluster.start()
session = self.patient_cql_connection(node1)
create_ks(session, 'ks', 1)
# Write until we get a new CL segment. This avoids replaying
# initialization mutations from startup into system tables when
# restoring snapshots. See CASSANDRA-11811.
advance_to_next_cl_segment(
session=session,
commitlog_dir=os.path.join(node1.get_path(), 'commitlogs')
)
session.execute('CREATE TABLE ks.cf ( key bigint PRIMARY KEY, val text);')
logger.debug("Writing first 30,000 rows...")
self.insert_rows(session, 0, 30000)
# Record when this first set of inserts finished:
insert_cutoff_times = [time.gmtime()]
# Delete all commitlog backups so far:
for f in glob.glob(tmp_commitlog + "/*"):
logger.debug('Removing {}'.format(f))
os.remove(f)
snapshot_dirs = self.make_snapshot(node1, 'ks', 'cf', 'basic')
if self.cluster.version() >= '3.0':
system_ks_snapshot_dirs = self.make_snapshot(node1, 'system_schema', 'keyspaces', 'keyspaces')
else:
system_ks_snapshot_dirs = self.make_snapshot(node1, 'system', 'schema_keyspaces', 'keyspaces')
if self.cluster.version() >= '3.0':
system_col_snapshot_dirs = self.make_snapshot(node1, 'system_schema', 'columns', 'columns')
else:
system_col_snapshot_dirs = self.make_snapshot(node1, 'system', 'schema_columns', 'columns')
if self.cluster.version() >= '3.0':
system_ut_snapshot_dirs = self.make_snapshot(node1, 'system_schema', 'types', 'usertypes')
else:
system_ut_snapshot_dirs = self.make_snapshot(node1, 'system', 'schema_usertypes', 'usertypes')
if self.cluster.version() >= '3.0':
system_cfs_snapshot_dirs = self.make_snapshot(node1, 'system_schema', 'tables', 'cfs')
else:
system_cfs_snapshot_dirs = self.make_snapshot(node1, 'system', 'schema_columnfamilies', 'cfs')
systemlocal_dirs = self.make_snapshot(node1, 'system', 'local', 'local')
if self.cluster.version() >= LooseVersion('5.1'):
local_dirs = self.make_snapshot(node1, 'system', 'local_metadata_log', 'local_metadata_log')
metadata_snapshot_dirs = self.make_snapshot(node1, 'system', 'metadata_snapshots', 'metadata_snapshots')
cluster_metadata_snapshot_dirs = self.make_snapshot(node1, 'system_cluster_metadata', 'distributed_metadata_log', 'distributed_metadata_log')
try:
# Write more data:
logger.debug("Writing second 30,000 rows...")
self.insert_rows(session, 30000, 60000)
node1.flush()
time.sleep(10)
# Record when this second set of inserts finished:
insert_cutoff_times.append(time.gmtime())
logger.debug("Writing final 5,000 rows...")
self.insert_rows(session, 60000, 65000)
# Record when the third set of inserts finished:
insert_cutoff_times.append(time.gmtime())
# Flush so we get an accurate view of commitlogs
node1.flush()
rows = session.execute('SELECT count(*) from ks.cf')
# Make sure we have the same amount of rows as when we snapshotted:
assert rows[0][0] == 65000
# Check that there are at least one commit log backed up that
# is not one of the active commit logs:
commitlog_dir = os.path.join(node1.get_path(), 'commitlogs')
logger.debug("node1 commitlog dir: " + commitlog_dir)
logger.debug("node1 commitlog dir contents: " + str(os.listdir(commitlog_dir)))
logger.debug("tmp_commitlog contents: " + str(os.listdir(tmp_commitlog)))
assert_directory_not_empty(tmp_commitlog, commitlog_dir)
cluster.flush()
cluster.compact()
node1.drain()
# Destroy the cluster
cluster.stop()
logger.debug("node1 commitlog dir contents after stopping: " + str(os.listdir(commitlog_dir)))
logger.debug("tmp_commitlog contents after stopping: " + str(os.listdir(tmp_commitlog)))
self.copy_logs(name=get_current_test_name() + "_pre-restore")
self.fixture_dtest_setup.cleanup_and_replace_cluster()
cluster = self.cluster
cluster.populate(1)
nodes = cluster.nodelist()
assert len(nodes) == 1
node1 = nodes[0]
# Restore schema from snapshots:
for system_ks_snapshot_dir in system_ks_snapshot_dirs:
if self.cluster.version() >= '3.0':
self.restore_snapshot(system_ks_snapshot_dir, node1, 'system_schema', 'keyspaces', 'keyspaces')
else:
self.restore_snapshot(system_ks_snapshot_dir, node1, 'system', 'schema_keyspaces', 'keyspaces')
for system_col_snapshot_dir in system_col_snapshot_dirs:
if self.cluster.version() >= '3.0':
self.restore_snapshot(system_col_snapshot_dir, node1, 'system_schema', 'columns', 'columns')
else:
self.restore_snapshot(system_col_snapshot_dir, node1, 'system', 'schema_columns', 'columns')
for system_ut_snapshot_dir in system_ut_snapshot_dirs:
if self.cluster.version() >= '3.0':
self.restore_snapshot(system_ut_snapshot_dir, node1, 'system_schema', 'types', 'usertypes')
else:
self.restore_snapshot(system_ut_snapshot_dir, node1, 'system', 'schema_usertypes', 'usertypes')
for system_cfs_snapshot_dir in system_cfs_snapshot_dirs:
if self.cluster.version() >= '3.0':
self.restore_snapshot(system_cfs_snapshot_dir, node1, 'system_schema', 'tables', 'cfs')
else:
self.restore_snapshot(system_cfs_snapshot_dir, node1, 'system', 'schema_columnfamilies', 'cfs')
for snapshot_dir in snapshot_dirs:
self.restore_snapshot(snapshot_dir, node1, 'ks', 'cf', 'basic')
for local_dir in systemlocal_dirs:
self.restore_snapshot(local_dir, node1, 'system', 'local', 'local')
if self.cluster.version() >= LooseVersion('5.1'):
for local_dir in local_dirs:
self.restore_snapshot(local_dir, node1, 'system', 'local_metadata_log', 'local_metadata_log')
for snapshot_dir in metadata_snapshot_dirs:
self.restore_snapshot(snapshot_dir, node1, 'system', 'metadata_snapshots', 'metadata_snapshots')
for cm_dir in cluster_metadata_snapshot_dirs:
self.restore_snapshot(cm_dir, node1, 'system_cluster_metadata', 'distributed_metadata_log', 'distributed_metadata_log')
cluster.start()
session = self.patient_cql_connection(node1)
node1.nodetool('refresh ks cf')
rows = session.execute('SELECT count(*) from ks.cf')
# Make sure we have the same amount of rows as when we snapshotted:
assert rows[0][0] == 30000
# Edit commitlog_archiving.properties. Remove the archive
# command and set a restore command and restore_directories:
if restore_archived_commitlog:
replace_in_file(os.path.join(node1.get_path(), 'conf', 'commitlog_archiving.properties'),
[(r'^archive_command=.*$', 'archive_command='),
(r'^restore_command=.*$', 'restore_command=cp -f %from %to'),
(r'^restore_directories=.*$', 'restore_directories={tmp_commitlog}'.format(
tmp_commitlog=tmp_commitlog))])
if restore_point_in_time:
restore_time = time.strftime("%Y:%m:%d %H:%M:%S", insert_cutoff_times[1])
replace_in_file(os.path.join(node1.get_path(), 'conf', 'commitlog_archiving.properties'),
[(r'^restore_point_in_time=.*$', 'restore_point_in_time={restore_time}'.format(restore_time=restore_time))])
if specify_commitlog_position:
# specify a high commit log position to skip replaying any commit log data
replace_in_file(os.path.join(node1.get_path(), 'conf', 'commitlog_archiving.properties'),
[(r'^snapshot_commitlog_position=.*$', 'snapshot_commitlog_position={cl_position}'.format(cl_position="9223372036854775807, 0"))])
logger.debug("Restarting node1..")
node1.stop()
node1.start(wait_for_binary_proto=True)
node1.nodetool('flush')
node1.nodetool('compact')
session = self.patient_cql_connection(node1)
rows = session.execute('SELECT count(*) from ks.cf')
# Now we should have 30000 rows from the snapshot + 30000 rows
# from the commitlog backups:
if not restore_archived_commitlog or specify_commitlog_position:
assert rows[0][0] == 30000
elif restore_point_in_time:
assert rows[0][0] == 60000
else:
assert rows[0][0] == 65000
finally:
# clean up
logger.debug("removing snapshot_dir: " + ",".join(snapshot_dirs))
for snapshot_dir in snapshot_dirs:
shutil.rmtree(snapshot_dir)
logger.debug("removing snapshot_dir: " + ",".join(system_ks_snapshot_dirs))
for system_ks_snapshot_dir in system_ks_snapshot_dirs:
shutil.rmtree(system_ks_snapshot_dir)
logger.debug("removing snapshot_dir: " + ",".join(system_cfs_snapshot_dirs))
for system_cfs_snapshot_dir in system_cfs_snapshot_dirs:
shutil.rmtree(system_cfs_snapshot_dir)
logger.debug("removing snapshot_dir: " + ",".join(system_ut_snapshot_dirs))
for system_ut_snapshot_dir in system_ut_snapshot_dirs:
shutil.rmtree(system_ut_snapshot_dir)
logger.debug("removing snapshot_dir: " + ",".join(system_col_snapshot_dirs))
for system_col_snapshot_dir in system_col_snapshot_dirs:
shutil.rmtree(system_col_snapshot_dir)
logger.debug("removing snapshot_dir: " + ",".join(systemlocal_dirs))
for systemlocal_snapshot_dir in systemlocal_dirs:
shutil.rmtree(systemlocal_snapshot_dir)
if self.cluster.version() >= LooseVersion('5.1'):
logger.debug("removing snapshot_dir: " + ",".join(local_dirs))
for local_snapshot_dir in local_dirs:
shutil.rmtree(local_snapshot_dir)
logger.debug("removing snapshot_dir: " + ",".join(metadata_snapshot_dirs))
for metadata_snapshot_dir in metadata_snapshot_dirs:
shutil.rmtree(metadata_snapshot_dir)
logger.debug("removing snapshot_dir: " + ",".join(cluster_metadata_snapshot_dirs))
for cm_snapshot_dir in cluster_metadata_snapshot_dirs:
shutil.rmtree(cm_snapshot_dir)
logger.debug("removing tmp_commitlog: " + tmp_commitlog)
shutil.rmtree(tmp_commitlog)
def test_archive_and_restore_commitlog_repeatedly(self):
"""
@jira_ticket CASSANDRA-10593
Run archive commit log restoration test repeatedly to make sure it is idempotent
and doesn't fail if done repeatedly
"""
cluster = self.cluster
cluster.populate(1)
node1 = cluster.nodelist()[0]
# Create a temp directory for storing commitlog archives:
tmp_commitlog = safe_mkdtemp()
logger.debug("tmp_commitlog: {}".format(tmp_commitlog))
# Edit commitlog_archiving.properties and set an archive
# command:
replace_in_file(os.path.join(node1.get_path(), 'conf', 'commitlog_archiving.properties'),
[(r'^archive_command=.*$', 'archive_command=ln %path {tmp_commitlog}/%name'.format(
tmp_commitlog=tmp_commitlog)),
(r'^restore_command=.*$', 'restore_command=cp -f %from %to'),
(r'^restore_directories=.*$', 'restore_directories={tmp_commitlog}'.format(
tmp_commitlog=tmp_commitlog))])
cluster.start()
logger.debug("Creating initial connection")
session = self.patient_cql_connection(node1)
create_ks(session, 'ks', 1)
session.execute('CREATE TABLE ks.cf ( key bigint PRIMARY KEY, val text);')
logger.debug("Writing 30,000 rows...")
self.insert_rows(session, 0, 60000)
try:
# Check that there are at least one commit log backed up that
# is not one of the active commit logs:
commitlog_dir = os.path.join(node1.get_path(), 'commitlogs')
logger.debug("node1 commitlog dir: " + commitlog_dir)
cluster.flush()
assert_directory_not_empty(tmp_commitlog, commitlog_dir)
logger.debug("Flushing and doing first restart")
cluster.compact()
node1.drain()
# restart the node which causes the active commitlogs to be archived
node1.stop()
node1.start(wait_for_binary_proto=True)
logger.debug("Stopping and second restart")
node1.stop()
node1.start(wait_for_binary_proto=True)
# Shouldn't be any additional data since it's replaying the same stuff repeatedly
session = self.patient_cql_connection(node1)
rows = session.execute('SELECT count(*) from ks.cf')
assert rows[0][0] == 60000
finally:
logger.debug("removing tmp_commitlog: " + tmp_commitlog)
shutil.rmtree(tmp_commitlog)
def assert_directory_not_empty(tmp_commitlog, commitlog_dir):
commitlog_dir_ret = set(commitlog_dir)
for tmp_commitlog_file in set(os.listdir(tmp_commitlog)):
commitlog_dir_ret.discard(tmp_commitlog_file)
assert len(commitlog_dir_ret) != 0