1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2024-12-13 11:53:59 +02:00
pg_probackup/tests/archive.py

834 lines
31 KiB
Python
Raw Normal View History

2017-07-12 16:28:28 +02:00
import os
import unittest
from .helpers.ptrack_helpers import ProbackupTest, ProbackupException, archive_script
from datetime import datetime, timedelta
import subprocess
from sys import exit
from time import sleep
module_name = 'archive'
class ArchiveTest(ProbackupTest, unittest.TestCase):
# @unittest.expectedFailure
# @unittest.skip("skip")
def test_pgpro434_1(self):
"""Description in jira issue PGPRO-434"""
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2018-01-25 20:37:27 +02:00
node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
2017-07-12 16:28:28 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
2018-01-25 20:37:27 +02:00
pg_options={
'max_wal_senders': '2',
'checkpoint_timeout': '30s'}
2017-07-12 16:28:28 +02:00
)
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
self.set_archiving(backup_dir, 'node', node)
node.slow_start()
2017-07-12 16:28:28 +02:00
node.safe_psql(
"postgres",
2018-01-25 20:37:27 +02:00
"create table t_heap as select 1 as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector from "
"generate_series(0,100) i")
2017-07-12 16:28:28 +02:00
result = node.safe_psql("postgres", "SELECT * FROM t_heap")
2018-01-25 20:37:27 +02:00
self.backup_node(
backup_dir, 'node', node,
options=["--log-level-file=verbose"])
2017-07-12 16:28:28 +02:00
node.cleanup()
self.restore_node(
2018-05-21 16:08:46 +02:00
backup_dir, 'node', node)
node.slow_start()
2017-07-12 16:28:28 +02:00
# Recreate backup calagoue
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
# Make backup
2018-01-25 20:37:27 +02:00
self.backup_node(
backup_dir, 'node', node,
options=["--log-level-file=verbose"])
2017-07-12 16:28:28 +02:00
node.cleanup()
# Restore Database
self.restore_node(
backup_dir, 'node', node,
options=["--recovery-target-action=promote"])
node.slow_start()
2017-07-12 16:28:28 +02:00
2018-01-25 20:37:27 +02:00
self.assertEqual(
result, node.safe_psql("postgres", "SELECT * FROM t_heap"),
2017-07-12 16:28:28 +02:00
'data after restore not equal to original data')
# Clean after yourself
self.del_test_dir(module_name, fname)
# @unittest.skip("skip")
# @unittest.expectedFailure
2017-07-12 16:28:28 +02:00
def test_pgpro434_2(self):
"""
Check that timelines are correct.
WAITING PGPRO-1053 for --immediate
"""
2017-07-12 16:28:28 +02:00
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2018-01-25 20:37:27 +02:00
node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
2017-07-12 16:28:28 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
2018-01-25 20:37:27 +02:00
pg_options={
'max_wal_senders': '2',
'checkpoint_timeout': '30s'}
2017-07-12 16:28:28 +02:00
)
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
self.set_archiving(backup_dir, 'node', node)
node.slow_start()
2017-07-12 16:28:28 +02:00
# FIRST TIMELINE
node.safe_psql(
"postgres",
2018-01-25 20:37:27 +02:00
"create table t_heap as select 1 as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(0,100) i")
2017-07-12 16:28:28 +02:00
backup_id = self.backup_node(backup_dir, 'node', node)
node.safe_psql(
"postgres",
2018-01-25 20:37:27 +02:00
"insert into t_heap select 100501 as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(0,1) i")
2017-07-12 16:28:28 +02:00
# SECOND TIMELIN
node.cleanup()
self.restore_node(
backup_dir, 'node', node,
options=['--immediate', '--recovery-target-action=promote'])
node.slow_start()
2017-07-12 16:28:28 +02:00
if self.verbose:
2018-01-25 20:37:27 +02:00
print(node.safe_psql(
"postgres",
"select redo_wal_file from pg_control_checkpoint()"))
self.assertFalse(
node.execute(
"postgres",
"select exists(select 1 "
"from t_heap where id = 100501)")[0][0],
'data after restore not equal to original data')
2017-07-12 16:28:28 +02:00
node.safe_psql(
"postgres",
2018-01-25 20:37:27 +02:00
"insert into t_heap select 2 as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(100,200) i")
2017-07-12 16:28:28 +02:00
backup_id = self.backup_node(backup_dir, 'node', node)
2017-07-12 16:28:28 +02:00
node.safe_psql(
"postgres",
2018-01-25 20:37:27 +02:00
"insert into t_heap select 100502 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
# THIRD TIMELINE
node.cleanup()
self.restore_node(
backup_dir, 'node', node,
options=['--immediate', '--recovery-target-action=promote'])
node.slow_start()
2018-01-25 20:37:27 +02:00
2017-07-12 16:28:28 +02:00
if self.verbose:
2018-01-25 20:37:27 +02:00
print(
node.safe_psql(
"postgres",
"select redo_wal_file from pg_control_checkpoint()"))
node.safe_psql(
"postgres",
"insert into t_heap select 3 as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(200,300) i")
2017-07-12 16:28:28 +02:00
backup_id = self.backup_node(backup_dir, 'node', node)
2017-07-12 16:28:28 +02:00
result = node.safe_psql("postgres", "SELECT * FROM t_heap")
node.safe_psql(
"postgres",
2018-01-25 20:37:27 +02:00
"insert into t_heap select 100503 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
# FOURTH TIMELINE
node.cleanup()
self.restore_node(
backup_dir, 'node', node,
options=['--immediate', '--recovery-target-action=promote'])
node.slow_start()
2018-07-06 19:40:59 +02:00
2017-07-12 16:28:28 +02:00
if self.verbose:
print('Fourth timeline')
2018-01-25 20:37:27 +02:00
print(node.safe_psql(
"postgres",
"select redo_wal_file from pg_control_checkpoint()"))
2017-07-12 16:28:28 +02:00
# FIFTH TIMELINE
node.cleanup()
self.restore_node(
backup_dir, 'node', node,
options=['--immediate', '--recovery-target-action=promote'])
node.slow_start()
2017-07-12 16:28:28 +02:00
if self.verbose:
print('Fifth timeline')
2018-01-25 20:37:27 +02:00
print(node.safe_psql(
"postgres",
"select redo_wal_file from pg_control_checkpoint()"))
2017-07-12 16:28:28 +02:00
# SIXTH TIMELINE
node.cleanup()
self.restore_node(
backup_dir, 'node', node,
options=['--immediate', '--recovery-target-action=promote'])
node.slow_start()
2017-07-12 16:28:28 +02:00
if self.verbose:
print('Sixth timeline')
2018-01-25 20:37:27 +02:00
print(node.safe_psql(
"postgres",
"select redo_wal_file from pg_control_checkpoint()"))
self.assertFalse(
node.execute(
"postgres",
"select exists(select 1 from t_heap where id > 100500)")[0][0],
2017-07-12 16:28:28 +02:00
'data after restore not equal to original data')
2018-01-25 20:37:27 +02:00
self.assertEqual(
result,
node.safe_psql(
"postgres",
"SELECT * FROM t_heap"),
2017-07-12 16:28:28 +02:00
'data after restore not equal to original data')
# Clean after yourself
2017-10-09 14:32:48 +02:00
self.del_test_dir(module_name, fname)
2017-07-12 16:28:28 +02:00
# @unittest.skip("skip")
def test_pgpro434_3(self):
2018-04-28 17:49:34 +02:00
"""Check pg_stop_backup_timeout, needed backup_timeout"""
2017-07-12 16:28:28 +02:00
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2018-01-25 20:37:27 +02:00
node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
2017-07-12 16:28:28 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
2018-01-25 20:37:27 +02:00
pg_options={
'max_wal_senders': '2',
'checkpoint_timeout': '30s'}
2017-07-12 16:28:28 +02:00
)
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
self.set_archiving(backup_dir, 'node', node)
2018-01-25 20:37:27 +02:00
archive_script_path = os.path.join(backup_dir, 'archive_script.sh')
2017-11-08 16:16:50 +02:00
with open(archive_script_path, 'w+') as f:
2018-01-25 20:37:27 +02:00
f.write(
archive_script.format(
backup_dir=backup_dir, node_name='node', count_limit=2))
2017-07-12 16:28:28 +02:00
2017-11-08 16:16:50 +02:00
st = os.stat(archive_script_path)
os.chmod(archive_script_path, st.st_mode | 0o111)
2018-01-25 20:37:27 +02:00
node.append_conf(
'postgresql.auto.conf', "archive_command = '{0} %p %f'".format(
archive_script_path))
node.slow_start()
2017-07-12 16:28:28 +02:00
try:
2018-01-25 20:37:27 +02:00
self.backup_node(
backup_dir, 'node', node,
options=[
"--archive-timeout=60",
"--log-level-file=verbose",
"--stream"]
)
2017-07-12 16:28:28 +02:00
# we should die here because exception is what we expect to happen
2018-01-25 20:37:27 +02:00
self.assertEqual(
1, 0,
"Expecting Error because pg_stop_backup failed to answer.\n "
"Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
2017-07-12 16:28:28 +02:00
except ProbackupException as e:
2018-01-25 20:37:27 +02:00
self.assertTrue(
"ERROR: pg_stop_backup doesn't answer" in e.message and
"cancel it" in e.message,
'\n Unexpected Error Message: {0}\n CMD: {1}'.format(
repr(e.message), self.cmd))
2017-07-12 16:28:28 +02:00
log_file = os.path.join(node.logs_dir, 'postgresql.log')
with open(log_file, 'r') as f:
log_content = f.read()
2018-01-25 20:37:27 +02:00
self.assertNotIn(
'FailedAssertion',
log_content,
'PostgreSQL crashed because of a failed assert')
2017-07-12 16:28:28 +02:00
# Clean after yourself
self.del_test_dir(module_name, fname)
# @unittest.skip("skip")
def test_arhive_push_file_exists(self):
"""Archive-push if file exists"""
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2018-01-25 20:37:27 +02:00
node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
2017-07-12 16:28:28 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
2018-01-25 20:37:27 +02:00
pg_options={
'max_wal_senders': '2',
'checkpoint_timeout': '30s'}
2017-07-12 16:28:28 +02:00
)
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
self.set_archiving(backup_dir, 'node', node)
wals_dir = os.path.join(backup_dir, 'wal', 'node')
if self.archive_compress:
file = os.path.join(wals_dir, '000000010000000000000001.gz')
else:
file = os.path.join(wals_dir, '000000010000000000000001')
2017-07-12 16:28:28 +02:00
with open(file, 'a') as f:
f.write(b"blablablaadssaaaaaaaaaaaaaaa")
f.flush()
f.close()
node.slow_start()
2017-07-12 16:28:28 +02:00
node.safe_psql(
"postgres",
2018-01-25 20:37:27 +02:00
"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,100500) i")
2017-07-12 16:28:28 +02:00
log_file = os.path.join(node.logs_dir, 'postgresql.log')
2018-01-25 20:37:27 +02:00
2017-07-12 16:28:28 +02:00
with open(log_file, 'r') as f:
log_content = f.read()
2018-01-25 20:37:27 +02:00
self.assertTrue(
'LOG: archive command failed with exit code 1' in log_content and
'DETAIL: The failed archive command was:' in log_content and
'INFO: pg_probackup archive-push from' in log_content and
'ERROR: WAL segment "{0}" already exists.'.format(file) in log_content,
'Expecting error messages about failed archive_command'
2017-07-12 16:28:28 +02:00
)
self.assertFalse('pg_probackup archive-push completed successfully' in log_content)
os.remove(file)
2018-01-25 20:37:27 +02:00
self.switch_wal_segment(node)
2017-07-12 16:28:28 +02:00
sleep(5)
2018-01-25 20:37:27 +02:00
with open(log_file, 'r') as f:
log_content = f.read()
self.assertTrue(
'pg_probackup archive-push completed successfully' in log_content,
'Expecting messages about successfull execution archive_command')
# Clean after yourself
self.del_test_dir(module_name, fname)
# @unittest.skip("skip")
def test_arhive_push_file_exists_overwrite(self):
"""Archive-push if file exists"""
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
set_replication=True,
initdb_params=['--data-checksums'],
pg_options={
'max_wal_senders': '2',
'checkpoint_timeout': '30s'}
)
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
self.set_archiving(backup_dir, 'node', node)
wals_dir = os.path.join(backup_dir, 'wal', 'node')
if self.archive_compress:
file = os.path.join(wals_dir, '000000010000000000000001.gz')
else:
file = os.path.join(wals_dir, '000000010000000000000001')
with open(file, 'a') as f:
f.write(b"blablablaadssaaaaaaaaaaaaaaa")
f.flush()
f.close()
node.slow_start()
2018-01-25 20:37:27 +02:00
node.safe_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,100500) i")
log_file = os.path.join(node.logs_dir, 'postgresql.log')
with open(log_file, 'r') as f:
log_content = f.read()
self.assertTrue(
'LOG: archive command failed with exit code 1' in log_content and
'DETAIL: The failed archive command was:' in log_content and
'INFO: pg_probackup archive-push from' in log_content and
'ERROR: WAL segment "{0}" already exists.'.format(file) in log_content,
'Expecting error messages about failed archive_command'
)
self.assertFalse('pg_probackup archive-push completed successfully' in log_content)
self.set_archiving(backup_dir, 'node', node, overwrite=True)
node.reload()
2017-10-11 17:08:56 +02:00
self.switch_wal_segment(node)
2018-01-25 20:37:27 +02:00
sleep(2)
2017-07-12 16:28:28 +02:00
with open(log_file, 'r') as f:
log_content = f.read()
2018-01-25 20:37:27 +02:00
self.assertTrue(
'pg_probackup archive-push completed successfully' in log_content,
'Expecting messages about successfull execution archive_command')
2017-07-12 16:28:28 +02:00
# Clean after yourself
self.del_test_dir(module_name, fname)
2017-10-09 14:32:48 +02:00
# @unittest.expectedFailure
# @unittest.skip("skip")
2017-07-12 16:28:28 +02:00
def test_replica_archive(self):
2018-06-02 19:35:37 +02:00
"""
make node without archiving, take stream backup and
turn it into replica, set replica with archiving,
make archive backup from replica
"""
2017-07-12 16:28:28 +02:00
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2018-01-25 20:37:27 +02:00
master = self.make_simple_node(
base_dir="{0}/{1}/master".format(module_name, fname),
2017-07-12 16:28:28 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
2018-01-25 20:37:27 +02:00
pg_options={
'max_wal_senders': '2',
'checkpoint_timeout': '30s',
'max_wal_size': '1GB'}
2017-07-12 16:28:28 +02:00
)
self.init_pb(backup_dir)
2017-10-09 14:32:48 +02:00
# ADD INSTANCE 'MASTER'
2017-07-12 16:28:28 +02:00
self.add_instance(backup_dir, 'master', master)
master.slow_start()
2017-07-12 16:28:28 +02:00
2018-01-25 20:37:27 +02:00
replica = self.make_simple_node(
base_dir="{0}/{1}/replica".format(module_name, fname))
2017-07-12 16:28:28 +02:00
replica.cleanup()
master.psql(
"postgres",
2018-01-25 20:37:27 +02:00
"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
self.backup_node(backup_dir, 'master', master, options=['--stream'])
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
# Settings for Replica
self.restore_node(backup_dir, 'master', replica)
2017-09-28 09:32:06 +02:00
self.set_replica(master, replica, synchronous=True)
2018-04-28 17:49:34 +02:00
self.add_instance(backup_dir, 'replica', replica)
2017-07-12 16:28:28 +02:00
self.set_archiving(backup_dir, 'replica', replica, replica=True)
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)
2018-06-02 19:35:37 +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",
2018-01-25 20:37:27 +02:00
"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")
2017-10-09 14:32:48 +02:00
# ADD INSTANCE 'REPLICA'
2018-04-28 17:49:34 +02:00
sleep(1)
2018-01-25 20:37:27 +02:00
backup_id = self.backup_node(
backup_dir, 'replica', replica,
options=[
'--archive-timeout=30',
'--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')
2018-01-25 20:37:27 +02:00
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
2018-01-25 20:37:27 +02:00
node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname))
2017-07-12 16:28:28 +02:00
node.cleanup()
self.restore_node(backup_dir, 'replica', data_dir=node.data_dir)
2018-01-25 20:37:27 +02:00
node.append_conf(
'postgresql.auto.conf', 'port = {0}'.format(node.port))
node.slow_start()
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-06-02 19:35:37 +02:00
# Change data on master, make PAGE 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",
2018-01-25 20:37:27 +02:00
"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")
2017-07-12 16:28:28 +02:00
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
2018-01-25 20:37:27 +02:00
backup_id = self.backup_node(
backup_dir, 'replica',
replica, backup_type='page',
options=[
'--archive-timeout=30', '--log-level-file=verbose',
'--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')
2018-01-25 20:37:27 +02:00
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
node.cleanup()
2018-01-25 20:37:27 +02:00
self.restore_node(
backup_dir, 'replica', data_dir=node.data_dir, backup_id=backup_id)
node.append_conf(
'postgresql.auto.conf', 'port = {0}'.format(node.port))
node.slow_start()
2017-07-12 16:28:28 +02:00
# CHECK DATA CORRECTNESS
after = node.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
# Clean after yourself
self.del_test_dir(module_name, fname)
2017-10-09 14:32:48 +02:00
# @unittest.expectedFailure
# @unittest.skip("skip")
def test_master_and_replica_parallel_archiving(self):
2018-01-25 20:37:27 +02:00
"""
make node 'master 'with archiving,
take archive backup and turn it into replica,
set replica with archiving, make archive backup from replica,
make archive backup from master
"""
2017-10-09 14:32:48 +02:00
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2018-01-25 20:37:27 +02:00
master = self.make_simple_node(
base_dir="{0}/{1}/master".format(module_name, fname),
2017-10-09 14:32:48 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
2018-01-25 20:37:27 +02:00
pg_options={
'checkpoint_timeout': '30s'}
2017-10-09 14:32:48 +02:00
)
2018-01-25 20:37:27 +02:00
replica = self.make_simple_node(
base_dir="{0}/{1}/replica".format(module_name, fname))
2017-10-09 14:32:48 +02:00
replica.cleanup()
self.init_pb(backup_dir)
# ADD INSTANCE 'MASTER'
self.add_instance(backup_dir, 'master', master)
self.set_archiving(backup_dir, 'master', master)
master.slow_start()
2017-10-09 14:32:48 +02:00
master.psql(
"postgres",
2018-01-25 20:37:27 +02:00
"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,10000) i")
2017-10-09 14:32:48 +02:00
# TAKE FULL ARCHIVE BACKUP FROM MASTER
self.backup_node(backup_dir, 'master', master)
# GET LOGICAL CONTENT FROM MASTER
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
# GET PHYSICAL CONTENT FROM MASTER
pgdata_master = self.pgdata_content(master.data_dir)
# Settings for Replica
self.restore_node(backup_dir, 'master', replica)
# CHECK PHYSICAL CORRECTNESS on REPLICA
pgdata_replica = self.pgdata_content(replica.data_dir)
self.compare_pgdata(pgdata_master, pgdata_replica)
self.set_replica(master, replica, synchronous=True)
# ADD INSTANCE REPLICA
self.add_instance(backup_dir, 'replica', replica)
# SET ARCHIVING FOR REPLICA
self.set_archiving(backup_dir, 'replica', replica, replica=True)
replica.slow_start(replica=True)
2017-10-09 14:32:48 +02:00
# CHECK LOGICAL CORRECTNESS on REPLICA
after = replica.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
# TAKE FULL ARCHIVE BACKUP FROM REPLICA
2018-01-25 20:37:27 +02:00
backup_id = self.backup_node(
backup_dir, 'replica', replica,
options=[
'--archive-timeout=20',
'--log-level-file=verbose',
'--master-host=localhost',
'--master-db=postgres',
'--master-port={0}'.format(master.port)]
)
2017-10-09 14:32:48 +02:00
self.validate_pb(backup_dir, 'replica')
2018-01-25 20:37:27 +02:00
self.assertEqual(
'OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
2017-10-09 14:32:48 +02:00
# TAKE FULL ARCHIVE BACKUP FROM MASTER
backup_id = self.backup_node(backup_dir, 'master', master)
self.validate_pb(backup_dir, 'master')
2018-01-25 20:37:27 +02:00
self.assertEqual(
'OK', self.show_pb(backup_dir, 'master', backup_id)['status'])
2017-12-13 10:15:42 +02:00
# Clean after yourself
self.del_test_dir(module_name, fname)
2017-12-13 10:15:42 +02:00
# @unittest.expectedFailure
# @unittest.skip("skip")
def test_master_and_replica_concurrent_archiving(self):
2018-01-25 20:37:27 +02:00
"""
make node 'master 'with archiving,
take archive backup and turn it into replica,
set replica with archiving, make archive backup from replica,
make archive backup from master
"""
2017-12-13 23:29:39 +02:00
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2018-01-25 20:37:27 +02:00
master = self.make_simple_node(
base_dir="{0}/{1}/master".format(module_name, fname),
2017-12-13 23:29:39 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
2018-01-25 20:37:27 +02:00
pg_options={
'checkpoint_timeout': '30s'}
2017-12-13 23:29:39 +02:00
)
2018-01-25 20:37:27 +02:00
replica = self.make_simple_node(
base_dir="{0}/{1}/replica".format(module_name, fname))
replica.cleanup()
2017-12-13 23:29:39 +02:00
self.init_pb(backup_dir)
# ADD INSTANCE 'MASTER'
self.add_instance(backup_dir, 'master', master)
self.set_archiving(backup_dir, 'master', master)
master.slow_start()
2017-12-13 23:29:39 +02:00
master.psql(
2017-12-13 23:29:39 +02:00
"postgres",
2018-01-25 20:37:27 +02:00
"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,10000) i")
2017-12-13 23:29:39 +02:00
# TAKE FULL ARCHIVE BACKUP FROM MASTER
self.backup_node(backup_dir, 'master', master)
# GET LOGICAL CONTENT FROM MASTER
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
# GET PHYSICAL CONTENT FROM MASTER
pgdata_master = self.pgdata_content(master.data_dir)
2017-12-13 23:29:39 +02:00
# Settings for Replica
2018-05-01 12:41:17 +02:00
self.restore_node(
2018-05-21 16:08:46 +02:00
backup_dir, 'master', replica)
# CHECK PHYSICAL CORRECTNESS on REPLICA
pgdata_replica = self.pgdata_content(replica.data_dir)
self.compare_pgdata(pgdata_master, pgdata_replica)
2017-12-13 23:29:39 +02:00
self.set_replica(master, replica, synchronous=True)
# ADD INSTANCE REPLICA
# self.add_instance(backup_dir, 'replica', replica)
# SET ARCHIVING FOR REPLICA
# self.set_archiving(backup_dir, 'replica', replica, replica=True)
replica.slow_start(replica=True)
2017-12-13 23:29:39 +02:00
# CHECK LOGICAL CORRECTNESS on REPLICA
after = replica.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
2017-12-13 23:29:39 +02:00
master.psql(
2017-12-13 23:29:39 +02:00
"postgres",
2018-01-25 20:37:27 +02:00
"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(0,10000) i")
2017-12-13 23:29:39 +02:00
# TAKE FULL ARCHIVE BACKUP FROM REPLICA
2018-01-25 20:37:27 +02:00
backup_id = self.backup_node(
backup_dir, 'master', replica,
options=[
'--archive-timeout=30',
'--master-host=localhost',
'--master-db=postgres',
'--master-port={0}'.format(master.port)])
self.validate_pb(backup_dir, 'master')
2018-01-25 20:37:27 +02:00
self.assertEqual(
'OK', self.show_pb(backup_dir, 'master', backup_id)['status'])
2017-12-13 23:29:39 +02:00
# TAKE FULL ARCHIVE BACKUP FROM MASTER
backup_id = self.backup_node(backup_dir, 'master', master)
self.validate_pb(backup_dir, 'master')
2018-01-25 20:37:27 +02:00
self.assertEqual(
'OK', self.show_pb(backup_dir, 'master', backup_id)['status'])
2017-12-13 23:29:39 +02:00
# Clean after yourself
self.del_test_dir(module_name, fname)
2017-12-13 23:29:39 +02:00
# @unittest.expectedFailure
# @unittest.skip("skip")
2017-12-13 23:29:39 +02:00
def test_archive_pg_receivexlog(self):
"""Test backup with pg_receivexlog wal delivary method"""
2017-12-13 23:29:39 +02:00
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2018-01-25 20:37:27 +02:00
node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
2017-12-13 23:29:39 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
2018-01-25 20:37:27 +02:00
pg_options={
'max_wal_senders': '2',
'checkpoint_timeout': '30s'}
2017-12-13 23:29:39 +02:00
)
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
node.start()
if self.get_version(node) < 100000:
2018-01-28 03:36:27 +02:00
pg_receivexlog_path = self.get_bin_path('pg_receivexlog')
else:
2018-01-28 03:36:27 +02:00
pg_receivexlog_path = self.get_bin_path('pg_receivewal')
2018-01-25 20:37:27 +02:00
pg_receivexlog = self.run_binary(
[
pg_receivexlog_path, '-p', str(node.port), '--synchronous',
'-D', os.path.join(backup_dir, 'wal', 'node')
], async=True)
if pg_receivexlog.returncode:
2018-01-25 20:37:27 +02:00
self.assertFalse(
True,
'Failed to start pg_receivexlog: {0}'.format(
pg_receivexlog.communicate()[1]))
2017-12-13 23:29:39 +02:00
node.safe_psql(
"postgres",
2018-01-25 20:37:27 +02:00
"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,10000) i")
2017-12-13 23:29:39 +02:00
self.backup_node(backup_dir, 'node', node)
2018-01-25 20:37:27 +02:00
# PAGE
node.safe_psql(
"postgres",
2018-01-25 20:37:27 +02:00
"insert into t_heap select i as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(10000,20000) i")
self.backup_node(
backup_dir,
'node',
node,
backup_type='page'
)
result = node.safe_psql("postgres", "SELECT * FROM t_heap")
self.validate_pb(backup_dir)
2017-12-13 23:29:39 +02:00
# Check data correctness
node.cleanup()
self.restore_node(backup_dir, 'node', node)
node.slow_start()
2018-07-06 19:40:59 +02:00
2018-01-25 20:37:27 +02:00
self.assertEqual(
result,
node.safe_psql(
"postgres", "SELECT * FROM t_heap"
),
2017-12-13 23:29:39 +02:00
'data after restore not equal to original data')
2017-12-13 23:29:39 +02:00
# Clean after yourself
pg_receivexlog.kill()
self.del_test_dir(module_name, fname)
2017-12-13 23:29:39 +02:00
# @unittest.expectedFailure
# @unittest.skip("skip")
def test_archive_pg_receivexlog_compression_pg10(self):
"""Test backup with pg_receivewal compressed wal delivary method"""
2017-12-13 10:15:42 +02:00
fname = self.id().split('.')[3]
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2018-01-25 20:37:27 +02:00
node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
2017-12-13 10:15:42 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
2018-01-25 20:37:27 +02:00
pg_options={
'max_wal_senders': '2',
'checkpoint_timeout': '30s'}
2017-12-13 10:15:42 +02:00
)
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
node.start()
2018-01-25 20:37:27 +02:00
if self.get_version(node) < self.version_to_num('10.0'):
return unittest.skip('You need PostgreSQL 10 for this test')
else:
2018-07-06 19:40:59 +02:00
pg_receivexlog_path = self.get_bin_path('pg_receivewal')
2018-01-25 20:37:27 +02:00
pg_receivexlog = self.run_binary(
[
pg_receivexlog_path, '-p', str(node.port), '--synchronous',
'-Z', '9', '-D', os.path.join(backup_dir, 'wal', 'node')
], async=True)
if pg_receivexlog.returncode:
2018-01-25 20:37:27 +02:00
self.assertFalse(
True,
'Failed to start pg_receivexlog: {0}'.format(
pg_receivexlog.communicate()[1]))
2017-12-13 10:15:42 +02:00
node.safe_psql(
"postgres",
2018-01-25 20:37:27 +02:00
"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,10000) i")
2017-12-13 10:15:42 +02:00
self.backup_node(backup_dir, 'node', node)
2018-01-25 20:37:27 +02:00
# PAGE
node.safe_psql(
"postgres",
2018-01-25 20:37:27 +02:00
"insert into t_heap select i as id, md5(i::text) as text, "
"md5(repeat(i::text,10))::tsvector as tsvector "
"from generate_series(10000,20000) i")
2017-12-13 10:15:42 +02:00
2018-01-25 20:37:27 +02:00
self.backup_node(
backup_dir, 'node', node,
backup_type='page'
)
result = node.safe_psql("postgres", "SELECT * FROM t_heap")
self.validate_pb(backup_dir)
# Check data correctness
node.cleanup()
2017-12-13 10:15:42 +02:00
self.restore_node(backup_dir, 'node', node)
node.slow_start()
2018-07-06 19:40:59 +02:00
2018-01-25 20:37:27 +02:00
self.assertEqual(
result, node.safe_psql("postgres", "SELECT * FROM t_heap"),
2017-12-13 10:15:42 +02:00
'data after restore not equal to original data')
2017-12-13 10:15:42 +02:00
# Clean after yourself
pg_receivexlog.kill()
self.del_test_dir(module_name, fname)