Move lock flushing to VMInstance

Prepares for reuse of this function via client commands.
This commit is contained in:
Joshua Boniface 2019-08-07 13:36:56 -04:00
parent fac02f8520
commit 496216321e
2 changed files with 32 additions and 24 deletions

View File

@ -34,6 +34,36 @@ import pvcd.zkhandler as zkhandler
import pvcd.VMConsoleWatcherInstance as VMConsoleWatcherInstance import pvcd.VMConsoleWatcherInstance as VMConsoleWatcherInstance
def flush_locks(zk_conn, logger, dom_uuid):
logger.out('Flushing RBD locks for VM "{}"'.format(dom_uuid), state='i')
# Get the list of RBD images
rbd_list = zkhandler.readdata(zk_conn, '/domains/{}/rbdlist'.format(dom_uuid)).split(',')
for rbd in rbd_list:
# Check if a lock exists
lock_list_retcode, lock_list_stdout, lock_list_stderr = common.run_os_command('rbd lock list --format json {}'.format(rbd))
if lock_list_retcode != 0:
logger.out('Failed to obtain lock list for volume "{}"'.format(rbd), state='e')
continue
try:
lock_list = json.loads(lock_list_stdout)
except Exception as e:
logger.out('Failed to parse lock list for volume "{}": {}'.format(rbd, e), state='e')
continue
logger.out('Lock list for volume "{}": {}'.format(rbd, lock_list), state='i')
# If there's at least one lock
if lock_list:
# Loop through the locks
for lock, detail in lock_list.items():
# Free the lock
lock_remove_retcode, lock_remove_stdout, lock_remove_stderr = common.run_os_command('rbd lock remove {} "{}" "{}"'.format(rbd, lock, detail['locker']))
if lock_remove_retcode != 0:
logger.out('Failed to free RBD lock "{}" on volume "{}"'.format(lock, rbd), state='e')
continue
logger.out('Freed RBD lock "{}" on volume "{}"'.format(lock, rbd), state='o')
class VMInstance(object): class VMInstance(object):
# Initialization function # Initialization function
def __init__(self, domuuid, zk_conn, config, logger, this_node): def __init__(self, domuuid, zk_conn, config, logger, this_node):

View File

@ -25,6 +25,7 @@ import threading
import pvcd.zkhandler as zkhandler import pvcd.zkhandler as zkhandler
import pvcd.common as common import pvcd.common as common
import pvcd.VMInstance as VMInstance
# #
# Fence thread entry function # Fence thread entry function
@ -77,30 +78,7 @@ def migrateFromFencedNode(zk_conn, node_name, logger):
logger.out('Migrating VMs from dead node "{}" to new hosts'.format(node_name), state='i') logger.out('Migrating VMs from dead node "{}" to new hosts'.format(node_name), state='i')
dead_node_running_domains = zkhandler.readdata(zk_conn, '/nodes/{}/runningdomains'.format(node_name)).split() dead_node_running_domains = zkhandler.readdata(zk_conn, '/nodes/{}/runningdomains'.format(node_name)).split()
for dom_uuid in dead_node_running_domains: for dom_uuid in dead_node_running_domains:
logger.out('Flushing RBD locks for VM "{}"'.format(dom_uuid), state='i') VMInstance.flush_locks(zk_conn, logger, dom_uuid)
# Get the list of RBD images
rbd_list = zkhandler.readdata(zk_conn, '/domains/{}/rbdlist'.format(dom_uuid)).split(',')
for rbd in rbd_list:
# Check if a lock exists
lock_list_retcode, lock_list_stdout, lock_list_stderr = common.run_os_command('rbd lock list --format json {}'.format(rbd))
if lock_list_retcode != 0:
logger.out('Failed to obtain lock list for volume "{}"'.format(rbd), state='e')
continue
try:
lock_list = json.loads(lock_list_stdout)
except:
lock_list = {}
# If there's at least one lock
if lock_list:
# Loop through the locks
for lock, detail in lock_list.items():
# Free the lock
lock_remove_retcode, lock_remove_stdout, lock_remove_stderr = common.run_os_command('rbd lock remove {} "{}" "{}"'.format(rbd, lock, detail['locker']))
if lock_remove_retcode != 0:
logger.out('Failed to free RBD lock "{}" on volume "{}"'.format(lock, rbd), state='e')
continue
logger.out('Freed RBD lock "{}" on volume "{}"'.format(lock, rbd), state='o')
target_node = common.findTargetHypervisor(zk_conn, 'mem', dom_uuid) target_node = common.findTargetHypervisor(zk_conn, 'mem', dom_uuid)