Use a nicer reload method on hot schema update

Instead of exiting and trusting systemd to restart us, instead leverage
the os.execv() call to reload the process in the current PID context.

Also improves the log messages so it's very clear what's going on.
This commit is contained in:
Joshua Boniface 2021-06-14 17:10:21 -04:00
parent e34a7d4d2a
commit 0a9c0c1ccb
1 changed files with 10 additions and 13 deletions

View File

@ -21,6 +21,7 @@
import kazoo.client import kazoo.client
import libvirt import libvirt
import sys
import os import os
import signal import signal
import psutil import psutil
@ -561,9 +562,10 @@ def update_schema(new_schema_version, stat, event=''):
new_schema_version = int(new_schema_version.decode('ascii')) new_schema_version = int(new_schema_version.decode('ascii'))
if new_schema_version == node_schema_version: if new_schema_version == node_schema_version:
return return True
logger.out('Hot update of schema version started', state='s') logger.out('Hot update of schema version started', state='s')
logger.out('Current version: {} New version: {}'.format(node_schema_version, new_schema_version), state='s')
# Prevent any keepalive updates while this happens # Prevent any keepalive updates while this happens
if update_timer is not None: if update_timer is not None:
@ -595,31 +597,26 @@ def update_schema(new_schema_version, stat, event=''):
time.sleep(1) time.sleep(1)
# Update the local schema version # Update the local schema version
logger.out('Updating node schema version', state='s') logger.out('Updating node target schema version', state='s')
zkhandler.schema.load(new_schema_version)
zkhandler.write([ zkhandler.write([
(('node.data.active_schema', myhostname), new_schema_version) (('node.data.active_schema', myhostname), new_schema_version)
]) ])
node_schema_version = new_schema_version node_schema_version = new_schema_version
time.sleep(1)
# Restart the API daemons if applicable # Restart the API daemons if applicable
logger.out('Restarting API services', state='s') logger.out('Restarting services', state='s')
common.run_os_command('systemctl restart pvcapid-worker.service')
if zkhandler.read('base.config.primary_node') == myhostname: if zkhandler.read('base.config.primary_node') == myhostname:
common.run_os_command('systemctl start pvcapid.service') common.run_os_command('systemctl restart pvcapid.service')
common.run_os_command('systemctl start pvcapid-worker.service')
# Terminate ourselves and restart with the new schema # Restart ourselves with the new schema
# THIS IS SUBOPTIMAL, but since DataWatch and ChildrenWatch elements in Kazoo cannot logger.out('Reloading node daemon', state='s')
# be hot updated, a restart of the service is required for the change to be picked up.
logger.out('Restarting node daemon', state='s')
try: try:
zkhandler.disconnect() zkhandler.disconnect()
del zkhandler del zkhandler
except Exception: except Exception:
pass pass
os.execv(sys.argv[0], sys.argv)
os._exit(150)
# If we are the last node to get a schema update, fire the master update # If we are the last node to get a schema update, fire the master update