Commit e4e9b806 authored by Guido Trotter's avatar Guido Trotter
Browse files

backend.InstanceShutdown: small cleanup

1) unhardcode the timeout, abstracting it in a constant
2) Use time.time() rather than hiding the timeout in a range()
3) call hyper.StopInstance multiple times
   -- currently all hypervisors just ignore all calls but once
4) Use hyper.ListInstances() rather than GetInstanceList([hv_name])
   -- it's cheaper :)
5) Change the final message to "forcing" from "using destroy"
Signed-off-by: default avatarGuido Trotter <>
Reviewed-by: default avatarOlivier Tharan <>
parent 88cd08aa
......@@ -971,29 +971,36 @@ def InstanceShutdown(instance):
hv_name = instance.hypervisor
running_instances = GetInstanceList([hv_name])
hyper = hypervisor.GetHypervisor(hv_name)
running_instances = hyper.ListInstances()
iname =
timeout = constants.DEFAULT_SHUTDOWN_TIMEOUT
if iname not in running_instances:"Instance %s not running, doing nothing", iname)
hyper = hypervisor.GetHypervisor(hv_name)
except errors.HypervisorError, err:
_Fail("Failed to stop instance %s: %s", iname, err)
# test every 10secs for 2min
start = time.time()
end = start + timeout
sleep_time = 1
for _ in range(11):
if not in GetInstanceList([hv_name]):
tried_once = False
while not tried_once and time.time() < end:
hyper.StopInstance(instance, retry=tried_once)
except errors.HypervisorError, err:
_Fail("Failed to stop instance %s: %s", iname, err)
tried_once = True
if not in hyper.ListInstances():
if sleep_time < 5:
# 1.2 behaves particularly good for our case:
# it gives us 10 increasing steps and caps just slightly above 5 seconds
sleep_time *= 1.2
# the shutdown did not succeed
logging.error("Shutdown of '%s' unsuccessful, using destroy", iname)
logging.error("Shutdown of '%s' unsuccessful, forcing", iname)
hyper.StopInstance(instance, force=True)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment