1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2024-12-04 10:44:46 +02:00
pg_probackup/tests/replica.py

446 lines
16 KiB
Python
Raw Normal View History

2017-05-17 11:46:38 +02:00
import os
2017-06-27 11:43:45 +02:00
import unittest
2017-06-27 07:42:52 +02:00
from .helpers.ptrack_helpers import ProbackupTest, ProbackupException, idx_ptrack
2017-05-17 11:46:38 +02:00
from datetime import datetime, timedelta
import subprocess
import time
2017-05-17 11:46:38 +02:00
2017-07-12 16:28:28 +02:00
module_name = 'replica'
2017-05-17 11:46:38 +02:00
2017-07-12 16:28:28 +02:00
class ReplicaTest(ProbackupTest, unittest.TestCase):
2017-05-17 11:46:38 +02:00
# @unittest.skip("skip")
# @unittest.expectedFailure
2017-07-12 16:28:28 +02: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 11:46:38 +02:00
fname = self.id().split('.')[3]
2017-07-12 16:28:28 +02: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 11:46:38 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
pg_options={
2018-11-16 08:35:41 +02:00
'wal_level': 'replica',
'max_wal_senders': '2',
'ptrack_enable': 'on'}
2017-05-17 11:46:38 +02:00
)
2018-12-25 16:48:49 +02:00
master.slow_start()
2017-06-07 16:52:07 +02:00
self.init_pb(backup_dir)
2017-06-20 12:57:23 +02:00
self.add_instance(backup_dir, 'master', master)
2017-06-07 16:52:07 +02:00
2017-07-12 16:28:28 +02:00
# CREATE TABLE
2017-05-17 11:46:38 +02: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 16:28:28 +02:00
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
2017-05-17 11:46:38 +02:00
2017-07-12 16:28:28 +02:00
# take full backup and restore it
2017-06-20 12:57:23 +02: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 09:32:06 +02:00
replica.cleanup()
2017-07-12 16:28:28 +02:00
self.restore_node(backup_dir, 'master', replica)
self.set_replica(master, replica)
2017-05-17 11:46:38 +02:00
2017-07-12 16:28:28 +02:00
# Check data correctness on replica
replica.slow_start(replica=True)
2017-07-12 16:28:28 +02:00
after = replica.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
2017-05-17 11:46:38 +02: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 16:28:28 +02: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 16:28:28 +02:00
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
self.add_instance(backup_dir, 'replica', replica)
2018-11-11 20:53:00 +02: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 16:28:28 +02:00
self.validate_pb(backup_dir, 'replica')
self.assertEqual(
'OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
2017-07-12 16:28:28 +02: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 16:28:28 +02:00
node.cleanup()
self.restore_node(backup_dir, 'replica', data_dir=node.data_dir)
2018-11-11 20:53:00 +02:00
node.append_conf(
'postgresql.auto.conf', 'port = {0}'.format(node.port))
node.slow_start()
2018-11-11 20:53:00 +02:00
2017-07-12 16:28:28 +02:00
# CHECK DATA CORRECTNESS
after = node.safe_psql("postgres", "SELECT * FROM t_heap")
2017-05-17 11:46:38 +02: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 16:28:28 +02: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 20:53:00 +02:00
2017-07-12 16:28:28 +02:00
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
2018-11-11 20:53:00 +02: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 16:28:28 +02:00
self.validate_pb(backup_dir, 'replica')
self.assertEqual(
'OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
2017-07-12 16:28:28 +02: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 20:53:00 +02:00
node.append_conf(
'postgresql.auto.conf', 'port = {0}'.format(node.port))
node.slow_start()
2018-11-11 20:53:00 +02:00
2017-07-12 16:28:28 +02:00
# CHECK DATA CORRECTNESS
after = node.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
2017-06-27 07:42:52 +02:00
# Clean after yourself
2017-07-12 16:28:28 +02:00
self.del_test_dir(module_name, fname)
2017-06-27 07:42:52 +02:00
2017-06-20 12:57:23 +02:00
# @unittest.skip("skip")
2017-07-12 16:28:28 +02: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 16:28:28 +02: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 09:32:06 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
pg_options={
'wal_level': 'replica',
'max_wal_senders': '2',
2018-11-11 20:53:00 +02:00
'archive_timeout': '10s'}
)
2017-06-20 12:57:23 +02: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 16:28:28 +02:00
replica.cleanup()
2017-06-20 12:57:23 +02: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 10:51:58 +02:00
"from generate_series(0,2560) i")
2017-07-12 16:28:28 +02: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 16:28:28 +02:00
self.restore_node(backup_dir, 'master', replica)
# Settings for Replica
2018-11-16 08:35:41 +02:00
self.add_instance(backup_dir, 'replica', replica)
self.set_replica(master, replica, synchronous=True)
2017-07-12 16:28:28 +02:00
self.set_archiving(backup_dir, 'replica', replica, replica=True)
2018-11-12 10:51:58 +02:00
replica.slow_start(replica=True)
2017-07-12 16:28:28 +02: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 16:28:28 +02: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 08:35:41 +02:00
"from generate_series(256,25120) i")
2018-11-11 20:53:00 +02:00
2017-07-12 16:28:28 +02:00
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
2018-11-11 20:53:00 +02:00
2018-11-16 08:35:41 +02:00
master.psql(
"postgres",
"CHECKPOINT")
2018-11-12 10:51:58 +02:00
2018-11-16 08:35:41 +02:00
self.wait_until_replica_catch_with_master(master, replica)
2018-11-12 10:51:58 +02:00
backup_id = self.backup_node(
backup_dir, 'replica', replica,
options=[
2018-11-16 08:35:41 +02:00
'--archive-timeout=60',
'--master-host=localhost',
'--master-db=postgres',
2018-11-16 08:35:41 +02:00
'--master-port={0}'.format(master.port)])
2018-11-12 10:51:58 +02:00
2017-07-12 16:28:28 +02:00
self.validate_pb(backup_dir, 'replica')
self.assertEqual(
'OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
2017-07-12 16:28:28 +02:00
# RESTORE FULL BACKUP TAKEN FROM replica
node = self.make_simple_node(
base_dir=os.path.join(module_name, fname, 'node'))
2017-07-12 16:28:28 +02:00
node.cleanup()
self.restore_node(backup_dir, 'replica', data_dir=node.data_dir)
2018-11-16 08:35:41 +02:00
node.append_conf(
'postgresql.auto.conf', 'port = {0}'.format(node.port))
2018-11-16 08:35:41 +02:00
node.append_conf(
'postgresql.auto.conf', 'archive_mode = off'.format(node.port))
node.slow_start()
2018-11-11 20:53:00 +02:00
2017-07-12 16:28:28 +02:00
# CHECK DATA CORRECTNESS
after = node.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
2018-11-11 20:53:00 +02:00
node.cleanup()
2017-07-12 16:28:28 +02: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 08:35:41 +02:00
master.pgbench_init(scale=5)
2018-11-11 20:53:00 +02:00
2018-11-16 08:35:41 +02:00
pgbench = master.pgbench(
options=['-T', '30', '-c', '2', '--no-vacuum'])
backup_id = self.backup_node(
2018-11-11 20:53:00 +02:00
backup_dir, 'replica',
replica, backup_type='page',
options=[
2018-11-16 08:35:41 +02:00
'--archive-timeout=60',
'--master-host=localhost',
'--master-db=postgres',
2018-11-16 08:35:41 +02: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 20:53:00 +02:00
2017-07-12 16:28:28 +02:00
self.validate_pb(backup_dir, 'replica')
self.assertEqual(
'OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
2017-07-12 16:28:28 +02:00
# RESTORE PAGE BACKUP TAKEN FROM replica
self.restore_node(
2018-11-16 08:35:41 +02:00
backup_dir, 'replica', data_dir=node.data_dir,
backup_id=backup_id)
2018-11-11 20:53:00 +02:00
node.append_conf(
'postgresql.auto.conf', 'port = {0}'.format(node.port))
2018-11-16 08:35:41 +02:00
2018-11-11 20:53:00 +02:00
node.append_conf(
'postgresql.auto.conf', 'archive_mode = off')
2018-11-16 08:35:41 +02:00
node.slow_start()
2018-11-11 20:53:00 +02:00
2017-07-12 16:28:28 +02:00
# CHECK DATA CORRECTNESS
2018-11-16 08:35:41 +02:00
after = node.safe_psql("postgres", "SELECT * FROM pgbench_accounts")
self.assertEqual(
before, after, 'Restored data is not equal to original')
2017-06-27 07:42:52 +02:00
2018-11-11 20:53:00 +02:00
self.add_instance(backup_dir, 'node', node)
self.backup_node(
backup_dir, 'node', node, options=['--stream'])
2017-06-27 07:42:52 +02:00
# Clean after yourself
2017-07-12 16:28:28 +02:00
self.del_test_dir(module_name, fname)
# @unittest.skip("skip")
def test_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={
2018-11-16 08:35:41 +02:00
'wal_level': 'replica',
'max_wal_senders': '2',
'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 08:35:41 +02: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 20:53:00 +02:00
backup_dir, 'master', replica, options=['-R'])
# Settings for Replica
2018-11-16 08:35:41 +02: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 20:53:00 +02:00
replica.append_conf(
'postgresql.auto.conf', 'hot_standby = on')
replica.slow_start(replica=True)
2018-11-16 08:35:41 +02: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 20:53:00 +02: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 20:53:00 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
pg_options={
2018-11-16 08:35:41 +02:00
'wal_level': 'replica',
'max_wal_senders': '2',
'archive_timeout': '10s'}
2018-11-11 20:53:00 +02: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 20:53:00 +02:00
replica.cleanup()
self.backup_node(backup_dir, 'master', master)
2018-11-16 08:35:41 +02: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 20:53:00 +02: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 08:35:41 +02:00
replica.slow_start(replica=True)
self.wait_until_replica_catch_with_master(master, replica)
2018-11-11 20:53:00 +02:00
replica.append_conf(
'recovery.conf', "recovery_min_apply_delay = '300s'")
replica.stop()
replica.slow_start(replica=True)
2018-11-11 20:53:00 +02:00
master.pgbench_init(scale=10)
pgbench = master.pgbench(
2018-11-16 08:35:41 +02:00
options=['-T', '60', '-c', '2', '--no-vacuum'])
2018-11-11 20:53:00 +02:00
self.backup_node(
2018-11-16 08:35:41 +02:00
backup_dir, 'replica',
replica, options=['--archive-timeout=60s'])
2018-11-11 20:53:00 +02:00
self.backup_node(
backup_dir, 'replica', replica,
2018-11-16 08:35:41 +02:00
data_dir=replica.data_dir,
backup_type='page', options=['--archive-timeout=60s'])
2018-11-11 20:53:00 +02:00
self.backup_node(
2018-11-16 08:35:41 +02:00
backup_dir, 'replica', replica,
backup_type='delta', options=['--archive-timeout=60s'])
2018-11-11 20:53:00 +02: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)