Skip to content
Snippets Groups Projects
Commit 08bf31f7 authored by Stéphane Diemer's avatar Stéphane Diemer
Browse files

Test PostgreSQL standby node only if available | refs #33870

parent 30cc77cd
No related branches found
No related tags found
No related merge requests found
......@@ -82,15 +82,16 @@ def get_haproxy_conf(path: str = '/etc/haproxy/haproxy.cfg') -> dict:
return conf
def get_nodes(conf: dict) -> dict:
def get_nodes() -> dict:
'''Get the list of nodes from HAProxy configuration.
:param conf: The HAProxy configuration file content
:type conf: dict
:return: The list of nodes found in HAProxy configuration
:rtype: dict
'''
# get haproxy conf
conf = get_haproxy_conf()
servers = {}
for item in conf.keys():
......@@ -263,24 +264,22 @@ def check_ha(db_conn: dict, errors: int = 0) -> int:
:rtype: int
'''
# get haproxy conf
ha_conf = get_haproxy_conf()
# get nodes data
nodes = get_nodes(ha_conf)
nodes = get_nodes()
# check haproxy
lg.log('Checking local HAProxy frontends:')
if not check_listen(db_conn['host'], 54321):
lg.error('HAProxy pgsql-primary frontend is not listening')
errors += 1
primary_listening = check_listen(db_conn['host'], 54321)
if primary_listening:
lg.success('HAProxy pgsql-primary frontend is listening.')
else:
lg.success('HAProxy pgsql-primary frontend is listening')
if not check_listen(db_conn['host'], 54322):
lg.error('HAProxy pgsql-standby frontend is not listening')
lg.error('HAProxy pgsql-primary frontend is not listening.')
errors += 1
standby_listening = check_listen(db_conn['host'], 54322)
if standby_listening:
lg.info('HAProxy pgsql-standby frontend is listening.')
else:
lg.success('HAProxy pgsql-standby frontend is listening')
lg.info('HAProxy pgsql-standby frontend is not listening.')
# check remotes
lg.log('Checking remote PostgreSQL nodes:')
......@@ -288,10 +287,10 @@ def check_ha(db_conn: dict, errors: int = 0) -> int:
node_host = nodes[node]['host']
node_port = nodes[node]['port']
if not check_listen(node_host, node_port):
lg.error('cannot bind {}:{}'.format(node_host, node_port))
lg.error('Cannot bind {}:{}.'.format(node_host, node_port))
errors += 1
else:
lg.success('can bind {}:{}'.format(node_host, node_port))
lg.success('Can bind {}:{}.'.format(node_host, node_port))
# check fenced
lg.log('Checking cluster state:')
......@@ -300,20 +299,23 @@ def check_ha(db_conn: dict, errors: int = 0) -> int:
lg.error('Node `{}` is fenced'.format(node))
errors += 1
else:
lg.success('No fenced node found')
lg.success('No fenced node found.')
# check replication
lg.log('Checking replication state:')
primary = db_conn.copy()
primary['port'] = 54321
standby = db_conn.copy()
standby['port'] = 54322
status, info = check_replication(primary, standby)
if not status:
lg.error('cannot replicate between primary/standby ({})'.format(info))
errors += 1
if primary_listening and standby_listening:
primary = db_conn.copy()
primary['port'] = 54321
standby = db_conn.copy()
standby['port'] = 54322
status, info = check_replication(primary, standby)
if not status:
lg.error('Cannot replicate between primary/standby ({}).'.format(info))
errors += 1
else:
lg.success('Can replicate between primary/standby ({}).'.format(info))
else:
lg.success('can replicate between primary/standby ({})'.format(info))
lg.info('Cannot check replication because primary and standby are not both accessible.')
return errors
......@@ -336,20 +338,20 @@ def check_local(db_conn: dict, errors: int = 0) -> int:
# check listen
lg.log('Checking local PostgreSQL node:')
if not check_listen(host, port):
lg.error('cannot connect to {}:{}'.format(host, port))
lg.error('Cannot connect to {}:{}.'.format(host, port))
errors += 1
else:
lg.success('can connect to {}:{}'.format(host, port))
lg.success('Can connect to {}:{}.'.format(host, port))
# check read
lg.log('Checking read operation:')
read_query = 'SELECT 1;'
status, info = check_psql(db_conn, read_query)
if not status:
lg.error('cannot read from {}@{}:{} ({})'.format(user, host, port, info))
lg.error('Cannot read from {}@{}:{} ({}).'.format(user, host, port, info))
errors += 1
else:
lg.success('can read from {}@{}:{}'.format(user, host, port))
lg.success('Can read from {}@{}:{}.'.format(user, host, port))
# get replication state if available
if check_listen('127.0.0.1', 8543):
......@@ -360,16 +362,16 @@ def check_local(db_conn: dict, errors: int = 0) -> int:
# check write
lg.log('Checking write operation:')
if state != 'primary':
lg.info('this database is in {} state'.format(state))
lg.info('This database is in {} state.'.format(state))
else:
rand = uuid.uuid4().hex
write_query = 'CREATE TABLE es_test_{} (id serial PRIMARY KEY);'.format(rand)
status, info = check_psql(db_conn, write_query)
if not status:
lg.error('cannot write on {}@{}:{} ({})'.format(user, host, port, info))
lg.error('Cannot write on {}@{}:{} ({}).'.format(user, host, port, info))
errors += 1
else:
lg.success('can write on {}@{}:{}'.format(user, host, port))
lg.success('Can write on {}@{}:{}.'.format(user, host, port))
# remove test table
check_psql(db_conn, 'DROP TABLE es_test_{};'.format(rand))
......@@ -398,11 +400,11 @@ def main():
# determine if HA setup and run according tests
lg.log('Checking availibility mode:')
if check_listen('127.0.0.1', 54321) and check_listen('127.0.0.1', 54322):
lg.info('this setup is using a primary and standby database')
if check_listen('127.0.0.1', 54321):
lg.info('This setup is using a primary and standby database.')
errors = check_ha(db_conn)
else:
lg.info('this setup is using a single database')
lg.info('This setup is using a single database.')
errors = check_local(db_conn)
if errors:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment