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