Use explicit acquire/release instead of with

The with blocks did not seem to work as expected. Go back to exclusive
locks as well since these are more consistent.
This commit is contained in:
Joshua Boniface 2020-10-21 11:32:40 -04:00
parent 42c5f84ba7
commit d42bb74dc9
1 changed files with 52 additions and 30 deletions

View File

@ -276,8 +276,10 @@ def modify_vm(zk_conn, domain, restart, new_vm_config):
zkhandler.writedata(zk_conn, zk_data)
if restart:
with zkhandler.writelock(zk_conn, '/domains/{}/state'.format(dom_uuid)):
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'restart' })
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
lock.acquire()
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'restart' })
lock.release()
return True, ''
@ -301,8 +303,10 @@ def undefine_vm(zk_conn, domain):
current_vm_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
if current_vm_state != 'stop':
# Set the domain into stop mode
with zkhandler.writelock(zk_conn, '/domains/{}/state'.format(dom_uuid)):
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'stop' })
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
lock.acquire()
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'stop' })
lock.release()
# Wait for 2 seconds to allow state to flow to all nodes
time.sleep(2)
@ -328,8 +332,10 @@ def remove_vm(zk_conn, domain):
current_vm_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
if current_vm_state != 'stop':
# Set the domain into stop mode
with zkhandler.writelock(zk_conn, '/domains/{}/state'.format(dom_uuid)):
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'stop' })
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
lock.acquire()
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'stop' })
lock.release()
# Wait for 2 seconds to allow state to flow to all nodes
time.sleep(2)
@ -360,8 +366,10 @@ def start_vm(zk_conn, domain):
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
# Set the VM to start
with zkhandler.writelock(zk_conn, '/domains/{}/state'.format(dom_uuid)):
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'start' })
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
lock.acquire()
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'start' })
lock.release()
return True, 'Starting VM "{}".'.format(domain)
@ -379,8 +387,10 @@ def restart_vm(zk_conn, domain, wait=False):
retmsg = 'Restarting VM "{}".'.format(domain)
# Set the VM to restart
with zkhandler.writelock(zk_conn, '/domains/{}/state'.format(dom_uuid)):
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'restart' })
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
lock.acquire()
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'restart' })
lock.release()
if wait:
while zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid)) == 'restart':
@ -403,8 +413,10 @@ def shutdown_vm(zk_conn, domain, wait=False):
retmsg = 'Shutting down VM "{}"'.format(domain)
# Set the VM to shutdown
with zkhandler.writelock(zk_conn, '/domains/{}/state'.format(dom_uuid)):
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'shutdown' })
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
lock.acquire()
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'shutdown' })
lock.release()
if wait:
while zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid)) == 'shutdown':
@ -423,8 +435,10 @@ def stop_vm(zk_conn, domain):
current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
# Set the VM to start
with zkhandler.writelock(zk_conn, '/domains/{}/state'.format(dom_uuid)):
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'stop' })
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
lock.acquire()
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'stop' })
lock.release()
return True, 'Forcibly stopping VM "{}".'.format(domain)
@ -440,8 +454,10 @@ def disable_vm(zk_conn, domain):
return False, 'ERROR: VM "{}" must be stopped before disabling!'.format(domain)
# Set the VM to start
with zkhandler.writelock(zk_conn, '/domains/{}/state'.format(dom_uuid)):
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'disable' })
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
lock.acquire()
zkhandler.writedata(zk_conn, { '/domains/{}/state'.format(dom_uuid): 'disable' })
lock.release()
return True, 'Marked VM "{}" as disable.'.format(domain)
@ -491,12 +507,14 @@ def move_vm(zk_conn, domain, target_node, wait=False, force_live=False):
retmsg = 'Permanently migrating VM "{}" to node "{}".'.format(domain, target_node)
with zkhandler.writelock(zk_conn, '/domains/{}/state'.format(dom_uuid)):
zkhandler.writedata(zk_conn, {
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
lock.acquire()
zkhandler.writedata(zk_conn, {
'/domains/{}/state'.format(dom_uuid): target_state,
'/domains/{}/node'.format(dom_uuid): target_node,
'/domains/{}/lastnode'.format(dom_uuid): ''
})
lock.release()
if wait:
while zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid)) == target_state:
@ -554,12 +572,14 @@ def migrate_vm(zk_conn, domain, target_node, force_migrate, wait=False, force_li
retmsg = 'Migrating VM "{}" to node "{}".'.format(domain, target_node)
with zkhandler.writelock(zk_conn, '/domains/{}/state'.format(dom_uuid)):
zkhandler.writedata(zk_conn, {
'/domains/{}/state'.format(dom_uuid): target_state,
'/domains/{}/node'.format(dom_uuid): target_node,
'/domains/{}/lastnode'.format(dom_uuid): current_node
})
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
lock.acquire()
zkhandler.writedata(zk_conn, {
'/domains/{}/state'.format(dom_uuid): target_state,
'/domains/{}/node'.format(dom_uuid): target_node,
'/domains/{}/lastnode'.format(dom_uuid): current_node
})
lock.release()
if wait:
while zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid)) == target_state:
@ -592,12 +612,14 @@ def unmigrate_vm(zk_conn, domain, wait=False, force_live=False):
retmsg = 'Unmigrating VM "{}" back to node "{}".'.format(domain, target_node)
with zkhandler.writelock(zk_conn, '/domains/{}/state'.format(dom_uuid)):
zkhandler.writedata(zk_conn, {
'/domains/{}/state'.format(dom_uuid): target_state,
'/domains/{}/node'.format(dom_uuid): target_node,
'/domains/{}/lastnode'.format(dom_uuid): ''
})
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
lock.acquire()
zkhandler.writedata(zk_conn, {
'/domains/{}/state'.format(dom_uuid): target_state,
'/domains/{}/node'.format(dom_uuid): target_node,
'/domains/{}/lastnode'.format(dom_uuid): ''
})
lock.release()
if wait:
while zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid)) == target_state: