1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2025-01-20 11:34:51 +02:00
pg_probackup/tests/replica.py

528 lines
18 KiB
Python
Raw Normal View History

2017-05-17 12:46:38 +03:00
import os
2017-06-27 12:43:45 +03:00
import unittest
2017-06-27 08:42:52 +03:00
from .helpers.ptrack_helpers import ProbackupTest, ProbackupException, idx_ptrack
2017-05-17 12:46:38 +03:00
from datetime import datetime, timedelta
import subprocess
import time
2017-05-17 12:46:38 +03:00
2017-07-12 17:28:28 +03:00
module_name = 'replica'
2017-05-17 12:46:38 +03:00
2017-07-12 17:28:28 +03:00
class ReplicaTest(ProbackupTest, unittest.TestCase):
2017-05-17 12:46:38 +03:00
# @unittest.skip("skip")
# @unittest.expectedFailure
2017-07-12 17:28:28 +03:00
def test_replica_stream_ptrack_backup(self):
"""
make node, take full backup, restore it and make replica from it,
take full stream backup from replica
"""
2017-05-17 12:46:38 +03:00
fname = self.id().split('.')[3]
2017-07-12 17:28:28 +03:00
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
master = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'master'),
2017-05-17 12:46:38 +03:00
set_replication=True,
initdb_params=['--data-checksums'],
pg_options={
2019-04-22 20:52:00 +03:00
'ptrack_enable': 'on'})
2018-12-25 17:48:49 +03:00
master.slow_start()
2017-06-07 17:52:07 +03:00
self.init_pb(backup_dir)
2017-06-20 13:57:23 +03:00
self.add_instance(backup_dir, 'master', master)
2017-06-07 17:52:07 +03:00
2017-07-12 17:28:28 +03:00
# CREATE TABLE
2017-05-17 12:46:38 +03:00
master.psql(
"postgres",
"create table t_heap as select i as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(0,256) i")
2017-07-12 17:28:28 +03:00
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
2017-05-17 12:46:38 +03:00
2017-07-12 17:28:28 +03:00
# take full backup and restore it
2017-06-20 13:57:23 +03:00
self.backup_node(backup_dir, 'master', master, options=['--stream'])
replica = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'replica'))
2017-09-28 10:32:06 +03:00
replica.cleanup()
2017-07-12 17:28:28 +03:00
self.restore_node(backup_dir, 'master', replica)
self.set_replica(master, replica)
2017-05-17 12:46:38 +03:00
2017-07-12 17:28:28 +03:00
# Check data correctness on replica
replica.slow_start(replica=True)
2017-07-12 17:28:28 +03:00
after = replica.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
2017-05-17 12:46:38 +03:00
# Change data on master, take FULL backup from replica,
# restore taken backup and check that restored data equal
# to original data
2017-07-12 17:28:28 +03:00
master.psql(
"postgres",
"insert into t_heap as select i as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(256,512) i")
2017-07-12 17:28:28 +03:00
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
self.add_instance(backup_dir, 'replica', replica)
2018-11-11 21:53:00 +03:00
backup_id = self.backup_node(
backup_dir, 'replica', replica,
options=[
'--stream',
'--master-host=localhost',
'--master-db=postgres',
'--master-port={0}'.format(master.port)])
2017-07-12 17:28:28 +03:00
self.validate_pb(backup_dir, 'replica')
self.assertEqual(
'OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
2017-07-12 17:28:28 +03:00
# RESTORE FULL BACKUP TAKEN FROM PREVIOUS STEP
node = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'node'))
2017-07-12 17:28:28 +03:00
node.cleanup()
self.restore_node(backup_dir, 'replica', data_dir=node.data_dir)
2018-11-11 21:53:00 +03:00
node.append_conf(
'postgresql.auto.conf', 'port = {0}'.format(node.port))
node.slow_start()
2018-11-11 21:53:00 +03:00
2017-07-12 17:28:28 +03:00
# CHECK DATA CORRECTNESS
after = node.safe_psql("postgres", "SELECT * FROM t_heap")
2017-05-17 12:46:38 +03:00
self.assertEqual(before, after)
# Change data on master, take PTRACK backup from replica,
# restore taken backup and check that restored data equal
# to original data
2017-07-12 17:28:28 +03:00
master.psql(
"postgres",
"insert into t_heap as select i as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(512,768) i")
2018-11-11 21:53:00 +03:00
2017-07-12 17:28:28 +03:00
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
2018-11-11 21:53:00 +03:00
backup_id = self.backup_node(
backup_dir, 'replica', replica, backup_type='ptrack',
options=[
'--stream',
'--master-host=localhost',
'--master-db=postgres',
'--master-port={0}'.format(master.port)])
2017-07-12 17:28:28 +03:00
self.validate_pb(backup_dir, 'replica')
self.assertEqual(
'OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
2017-07-12 17:28:28 +03:00
# RESTORE PTRACK BACKUP TAKEN FROM replica
node.cleanup()
self.restore_node(
backup_dir, 'replica', data_dir=node.data_dir, backup_id=backup_id)
2018-11-11 21:53:00 +03:00
node.append_conf(
'postgresql.auto.conf', 'port = {0}'.format(node.port))
node.slow_start()
2018-11-11 21:53:00 +03:00
2017-07-12 17:28:28 +03:00
# CHECK DATA CORRECTNESS
after = node.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
2017-06-27 08:42:52 +03:00
# Clean after yourself
2017-07-12 17:28:28 +03:00
self.del_test_dir(module_name, fname)
2017-06-27 08:42:52 +03:00
2017-06-20 13:57:23 +03:00
# @unittest.skip("skip")
2017-07-12 17:28:28 +03:00
def test_replica_archive_page_backup(self):
"""
make archive master, take full and page archive backups from master,
set replica, make archive backup from replica
"""
fname = self.id().split('.')[3]
2017-07-12 17:28:28 +03:00
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
master = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'master'),
2017-09-28 10:32:06 +03:00
set_replication=True,
initdb_params=['--data-checksums'],
pg_options={
'archive_timeout': '10s',
'checkpoint_timeout': '30s',
2019-04-20 12:42:17 +03:00
'max_wal_size': '32MB'})
2017-06-20 13:57:23 +03:00
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'master', master)
self.set_archiving(backup_dir, 'master', master)
master.slow_start()
replica = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'replica'))
2017-07-12 17:28:28 +03:00
replica.cleanup()
2017-06-20 13:57:23 +03:00
self.backup_node(backup_dir, 'master', master)
master.psql(
"postgres",
"create table t_heap as select i as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
2018-11-12 11:51:58 +03:00
"from generate_series(0,2560) i")
2017-07-12 17:28:28 +03:00
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
backup_id = self.backup_node(
backup_dir, 'master', master, backup_type='page')
2017-07-12 17:28:28 +03:00
self.restore_node(backup_dir, 'master', replica)
# Settings for Replica
2018-11-16 09:35:41 +03:00
self.add_instance(backup_dir, 'replica', replica)
self.set_replica(master, replica, synchronous=True)
2017-07-12 17:28:28 +03:00
self.set_archiving(backup_dir, 'replica', replica, replica=True)
2018-11-12 11:51:58 +03:00
replica.slow_start(replica=True)
2017-07-12 17:28:28 +03:00
# Check data correctness on replica
after = replica.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
# Change data on master, take FULL backup from replica,
# restore taken backup and check that restored data
# equal to original data
2017-07-12 17:28:28 +03:00
master.psql(
"postgres",
"insert into t_heap as select i as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
2018-11-16 09:35:41 +03:00
"from generate_series(256,25120) i")
2018-11-11 21:53:00 +03:00
2017-07-12 17:28:28 +03:00
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
2018-11-11 21:53:00 +03:00
2018-11-16 09:35:41 +03:00
master.psql(
"postgres",
"CHECKPOINT")
2018-11-12 11:51:58 +03:00
2018-11-16 09:35:41 +03:00
self.wait_until_replica_catch_with_master(master, replica)
2018-11-12 11:51:58 +03:00
backup_id = self.backup_node(
backup_dir, 'replica', replica,
options=[
2018-11-16 09:35:41 +03:00
'--archive-timeout=60',
'--master-host=localhost',
'--master-db=postgres',
2018-11-16 09:35:41 +03:00
'--master-port={0}'.format(master.port)])
2018-11-12 11:51:58 +03:00
2017-07-12 17:28:28 +03:00
self.validate_pb(backup_dir, 'replica')
self.assertEqual(
'OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
2017-07-12 17:28:28 +03:00
# RESTORE FULL BACKUP TAKEN FROM replica
node = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'node'))
2017-07-12 17:28:28 +03:00
node.cleanup()
self.restore_node(backup_dir, 'replica', data_dir=node.data_dir)
2018-11-16 09:35:41 +03:00
node.append_conf(
'postgresql.auto.conf', 'port = {0}'.format(node.port))
2018-11-16 09:35:41 +03:00
node.append_conf(
'postgresql.auto.conf', 'archive_mode = off'.format(node.port))
node.slow_start()
2018-11-11 21:53:00 +03:00
2017-07-12 17:28:28 +03:00
# CHECK DATA CORRECTNESS
after = node.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
2018-11-11 21:53:00 +03:00
node.cleanup()
2017-07-12 17:28:28 +03:00
# Change data on master, make PAGE backup from replica,
# restore taken backup and check that restored data equal
# to original data
2018-11-16 09:35:41 +03:00
master.pgbench_init(scale=5)
2018-11-11 21:53:00 +03:00
2018-11-16 09:35:41 +03:00
pgbench = master.pgbench(
options=['-T', '30', '-c', '2', '--no-vacuum'])
backup_id = self.backup_node(
2018-11-11 21:53:00 +03:00
backup_dir, 'replica',
replica, backup_type='page',
options=[
2018-11-16 09:35:41 +03:00
'--archive-timeout=60',
'--master-host=localhost',
'--master-db=postgres',
2018-11-16 09:35:41 +03:00
'--master-port={0}'.format(master.port)])
pgbench.wait()
self.switch_wal_segment(master)
before = master.safe_psql("postgres", "SELECT * FROM pgbench_accounts")
2018-11-11 21:53:00 +03:00
2017-07-12 17:28:28 +03:00
self.validate_pb(backup_dir, 'replica')
self.assertEqual(
'OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
2017-07-12 17:28:28 +03:00
# RESTORE PAGE BACKUP TAKEN FROM replica
self.restore_node(
2018-11-16 09:35:41 +03:00
backup_dir, 'replica', data_dir=node.data_dir,
backup_id=backup_id)
2018-11-11 21:53:00 +03:00
node.append_conf(
'postgresql.auto.conf', 'port = {0}'.format(node.port))
2018-11-16 09:35:41 +03:00
2018-11-11 21:53:00 +03:00
node.append_conf(
'postgresql.auto.conf', 'archive_mode = off')
2018-11-16 09:35:41 +03:00
node.slow_start()
2018-11-11 21:53:00 +03:00
2017-07-12 17:28:28 +03:00
# CHECK DATA CORRECTNESS
2018-11-16 09:35:41 +03:00
after = node.safe_psql("postgres", "SELECT * FROM pgbench_accounts")
self.assertEqual(
before, after, 'Restored data is not equal to original')
2017-06-27 08:42:52 +03:00
2018-11-11 21:53:00 +03:00
self.add_instance(backup_dir, 'node', node)
self.backup_node(
backup_dir, 'node', node, options=['--stream'])
2017-06-27 08:42:52 +03:00
# Clean after yourself
2017-07-12 17:28:28 +03:00
self.del_test_dir(module_name, fname)
# @unittest.skip("skip")
2019-05-28 12:41:03 +03:00
def test_basic_make_replica_via_restore(self):
"""
make archive master, take full and page archive backups from master,
set replica, make archive backup from replica
"""
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
master = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'master'),
set_replication=True,
initdb_params=['--data-checksums'],
pg_options={
2019-04-22 20:52:00 +03:00
'archive_timeout': '10s'})
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'master', master)
self.set_archiving(backup_dir, 'master', master)
# force more frequent wal switch
master.append_conf('postgresql.auto.conf', 'archive_timeout = 10')
master.slow_start()
replica = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'replica'))
replica.cleanup()
self.backup_node(backup_dir, 'master', master)
master.psql(
"postgres",
"create table t_heap as select i as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
2018-11-16 09:35:41 +03:00
"from generate_series(0,8192) i")
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
backup_id = self.backup_node(
backup_dir, 'master', master, backup_type='page')
self.restore_node(
2018-11-11 21:53:00 +03:00
backup_dir, 'master', replica, options=['-R'])
# Settings for Replica
2018-11-16 09:35:41 +03:00
self.add_instance(backup_dir, 'replica', replica)
self.set_archiving(backup_dir, 'replica', replica, replica=True)
replica.append_conf(
'postgresql.auto.conf', 'port = {0}'.format(replica.port))
2018-11-11 21:53:00 +03:00
replica.append_conf(
'postgresql.auto.conf', 'hot_standby = on')
replica.slow_start(replica=True)
2018-11-16 09:35:41 +03:00
self.backup_node(
backup_dir, 'replica', replica,
options=['--archive-timeout=30s', '--stream'])
# Clean after yourself
self.del_test_dir(module_name, fname)
2018-11-11 21:53:00 +03:00
# @unittest.skip("skip")
def test_take_backup_from_delayed_replica(self):
"""
make archive master, take full backups from master,
restore full backup as delayed replica, launch pgbench,
take FULL, PAGE and DELTA backups from replica
"""
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
master = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'master'),
2018-11-11 21:53:00 +03:00
set_replication=True,
initdb_params=['--data-checksums'],
pg_options={
2019-04-22 20:52:00 +03:00
'archive_timeout': '10s'})
2018-11-11 21:53:00 +03:00
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'master', master)
self.set_archiving(backup_dir, 'master', master)
master.slow_start()
replica = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'replica'))
2018-11-11 21:53:00 +03:00
replica.cleanup()
self.backup_node(backup_dir, 'master', master)
2018-11-16 09:35:41 +03:00
master.psql(
"postgres",
"create table t_heap as select i as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(0,165000) i")
master.psql(
"postgres",
"CHECKPOINT")
master.psql(
"postgres",
"create table t_heap_1 as select i as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(0,165000) i")
2018-11-11 21:53:00 +03:00
self.restore_node(
backup_dir, 'master', replica, options=['-R'])
# Settings for Replica
self.add_instance(backup_dir, 'replica', replica)
self.set_archiving(backup_dir, 'replica', replica, replica=True)
replica.append_conf(
'postgresql.auto.conf', 'port = {0}'.format(replica.port))
2018-11-16 09:35:41 +03:00
replica.slow_start(replica=True)
self.wait_until_replica_catch_with_master(master, replica)
2018-11-11 21:53:00 +03:00
replica.append_conf(
'recovery.conf', "recovery_min_apply_delay = '300s'")
replica.stop()
replica.slow_start(replica=True)
2018-11-11 21:53:00 +03:00
master.pgbench_init(scale=10)
pgbench = master.pgbench(
2018-11-16 09:35:41 +03:00
options=['-T', '60', '-c', '2', '--no-vacuum'])
2018-11-11 21:53:00 +03:00
self.backup_node(
2018-11-16 09:35:41 +03:00
backup_dir, 'replica',
replica, options=['--archive-timeout=60s'])
2018-11-11 21:53:00 +03:00
self.backup_node(
backup_dir, 'replica', replica,
2018-11-16 09:35:41 +03:00
data_dir=replica.data_dir,
backup_type='page', options=['--archive-timeout=60s'])
2018-11-11 21:53:00 +03:00
self.backup_node(
2018-11-16 09:35:41 +03:00
backup_dir, 'replica', replica,
backup_type='delta', options=['--archive-timeout=60s'])
2018-11-11 21:53:00 +03:00
pgbench.wait()
pgbench = master.pgbench(
options=['-T', '30', '-c', '2', '--no-vacuum'])
self.backup_node(
backup_dir, 'replica', replica,
options=['--stream'])
self.backup_node(
backup_dir, 'replica', replica,
backup_type='page', options=['--stream'])
self.backup_node(
backup_dir, 'replica', replica,
backup_type='delta', options=['--stream'])
pgbench.wait()
# Clean after yourself
self.del_test_dir(module_name, fname)
# @unittest.skip("skip")
def test_replica_promote(self):
"""
start backup from replica, during backup promote replica
check that backup is failed
"""
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
master = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'master'),
set_replication=True,
initdb_params=['--data-checksums'],
pg_options={
'archive_timeout': '10s',
'checkpoint_timeout': '30s',
'max_wal_size': '32MB'})
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'master', master)
self.set_archiving(backup_dir, 'master', master)
master.slow_start()
replica = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'replica'))
replica.cleanup()
self.backup_node(backup_dir, 'master', master)
master.psql(
"postgres",
"create table t_heap as select i as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(0,165000) i")
self.restore_node(
backup_dir, 'master', replica, options=['-R'])
# Settings for Replica
self.add_instance(backup_dir, 'replica', replica)
self.set_archiving(backup_dir, 'replica', replica, replica=True)
self.set_replica(
master, replica,
replica_name='replica', synchronous=True)
replica.slow_start(replica=True)
master.psql(
"postgres",
"create table t_heap_1 as select i as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(0,165000) i")
self.wait_until_replica_catch_with_master(master, replica)
# start backup from replica
gdb = self.backup_node(
backup_dir, 'replica', replica, gdb=True,
options=['--log-level-file=verbose'])
gdb.set_breakpoint('backup_data_file')
gdb.run_until_break()
gdb.continue_execution_until_break(20)
replica.promote()
gdb.remove_all_breakpoints()
gdb.continue_execution_until_exit()
backup_id = self.show_pb(
backup_dir, 'replica')[0]["id"]
# read log file content
with open(os.path.join(backup_dir, 'log', 'pg_probackup.log')) as f:
log_content = f.read()
f.close
self.assertIn(
'ERROR: the standby was promoted during online backup',
log_content)
self.assertIn(
'WARNING: Backup {0} is running, '
'setting its status to ERROR'.format(backup_id),
log_content)
# Clean after yourself
self.del_test_dir(module_name, fname)