vitess-gh/test/backup.py

178 строки
5.5 KiB
Python
Исходник Обычный вид История

#!/usr/bin/env python
import logging
import unittest
import environment
import tablet
2015-08-31 08:03:22 +03:00
import utils
use_mysqlctld = True
tablet_master = tablet.Tablet(use_mysqlctld=use_mysqlctld)
tablet_replica1 = tablet.Tablet(use_mysqlctld=use_mysqlctld)
2015-05-19 21:35:52 +03:00
tablet_replica2 = tablet.Tablet(use_mysqlctld=use_mysqlctld)
setup_procs = []
def setUpModule():
try:
environment.topo_server().setup()
# start mysql instance external to the test
global setup_procs
setup_procs = [
tablet_master.init_mysql(),
tablet_replica1.init_mysql(),
2015-05-19 21:35:52 +03:00
tablet_replica2.init_mysql(),
]
if use_mysqlctld:
tablet_master.wait_for_mysqlctl_socket()
tablet_replica1.wait_for_mysqlctl_socket()
2015-05-19 21:35:52 +03:00
tablet_replica2.wait_for_mysqlctl_socket()
else:
utils.wait_procs(setup_procs)
except:
tearDownModule()
raise
def tearDownModule():
if utils.options.skip_teardown:
return
if use_mysqlctld:
# Try to terminate mysqlctld gracefully, so it kills its mysqld.
for proc in setup_procs:
utils.kill_sub_process(proc, soft=True)
teardown_procs = setup_procs
else:
teardown_procs = [
tablet_master.teardown_mysql(),
tablet_replica1.teardown_mysql(),
2015-05-19 21:35:52 +03:00
tablet_replica2.teardown_mysql(),
]
utils.wait_procs(teardown_procs, raise_on_error=False)
environment.topo_server().teardown()
utils.kill_sub_processes()
utils.remove_tmp_files()
tablet_master.remove_tree()
tablet_replica1.remove_tree()
2015-05-19 21:35:52 +03:00
tablet_replica2.remove_tree()
class TestBackup(unittest.TestCase):
def tearDown(self):
tablet.Tablet.check_vttablet_count()
environment.topo_server().wipe()
2015-05-19 21:35:52 +03:00
for t in [tablet_master, tablet_replica1, tablet_replica2]:
t.reset_replication()
t.clean_dbs()
_create_vt_insert_test = '''create table vt_insert_test (
id bigint auto_increment,
msg varchar(64),
primary key (id)
) Engine=InnoDB'''
2015-05-19 21:35:52 +03:00
def _insert_master(self, index):
2015-08-26 04:54:04 +03:00
tablet_master.mquery(
'vt_test_keyspace',
"insert into vt_insert_test (msg) values ('test %s')" %
index, write=True)
def test_backup(self):
2015-08-31 08:03:22 +03:00
"""Test backup flow.
test_backup will:
2015-05-19 21:35:52 +03:00
- create a shard with master and replica1 only
- run InitShardMaster
- insert some data
- take a backup
- insert more data on the master
- bring up tablet_replica2 after the fact, let it restore the backup
- check all data is right (before+after backup data)
- list the backup, remove it
2015-05-19 21:35:52 +03:00
"""
for t in tablet_master, tablet_replica1:
t.create_db('vt_test_keyspace')
2015-05-19 21:35:52 +03:00
tablet_master.init_tablet('master', 'test_keyspace', '0', start=True,
supports_backups=True)
tablet_replica1.init_tablet('replica', 'test_keyspace', '0', start=True,
supports_backups=True)
utils.run_vtctl(['InitShardMaster', 'test_keyspace/0',
tablet_master.tablet_alias])
# insert data on master, wait for slave to get it
2015-05-19 21:35:52 +03:00
tablet_master.mquery('vt_test_keyspace', self._create_vt_insert_test)
self._insert_master(1)
timeout = 10
while True:
try:
result = tablet_replica1.mquery(
'vt_test_keyspace', 'select count(*) from vt_insert_test')
if result[0][0] == 1:
break
except:
# ignore exceptions, we'll just timeout (the tablet creation
# can take some time to replicate, and we get a 'table vt_insert_test
# does not exist exception in some rare cases)
logging.exception('exception waiting for data to replicate')
timeout = utils.wait_step('slave tablet getting data', timeout)
# backup the slave
utils.run_vtctl(['Backup', tablet_replica1.tablet_alias], auto_log=True)
2015-05-19 21:35:52 +03:00
# insert more data on the master
self._insert_master(2)
# now bring up the other slave, health check on, init_tablet on, restore on
tablet_replica2.start_vttablet(wait_for_state='SERVING',
target_tablet_type='replica',
init_keyspace='test_keyspace',
init_shard='0',
supports_backups=True)
2015-05-19 21:35:52 +03:00
# check the new slave has the data
timeout = 10
while True:
2015-08-26 04:54:04 +03:00
result = tablet_replica2.mquery(
'vt_test_keyspace', 'select count(*) from vt_insert_test')
2015-05-19 21:35:52 +03:00
if result[0][0] == 2:
break
timeout = utils.wait_step('new slave tablet getting data', timeout)
# list the backups
2015-10-22 03:14:54 +03:00
backups, _ = utils.run_vtctl(tablet.get_backup_storage_flags() +
['ListBackups', 'test_keyspace/0'],
mode=utils.VTCTL_VTCTL, trap_output=True)
backups = backups.splitlines()
2015-08-26 04:54:04 +03:00
logging.debug('list of backups: %s', backups)
self.assertEqual(len(backups), 1)
self.assertTrue(backups[0].endswith(tablet_replica1.tablet_alias))
# remove the backup
2015-08-31 08:03:22 +03:00
utils.run_vtctl(
tablet.get_backup_storage_flags() +
['RemoveBackup', 'test_keyspace/0', backups[0]],
auto_log=True, mode=utils.VTCTL_VTCTL)
# make sure the list of backups is empty now
backups, _ = utils.run_vtctl(tablet.get_backup_storage_flags() +
['ListBackups', 'test_keyspace/0'],
mode=utils.VTCTL_VTCTL, trap_output=True)
backups = backups.splitlines()
2015-08-26 04:54:04 +03:00
logging.debug('list of backups after remove: %s', backups)
self.assertEqual(len(backups), 0)
2015-05-19 21:35:52 +03:00
for t in tablet_master, tablet_replica1, tablet_replica2:
t.kill_vttablet()
if __name__ == '__main__':
utils.main()