-
Notifications
You must be signed in to change notification settings - Fork 292
[From REQ-477] Cherry-pick of shutdown changes #3473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
edwintorok
merged 6 commits into
xapi-project:master
from
mcintyre94:CP-26970-master-shutdown
Mar 1, 2018
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
526bbc8
CA-274585: allow unplugging statefile VDI
edwintorok 9e1558c
CA-274585: unplug all local PBDs on shutdown/reboot
edwintorok 8879ced
CA-277346: do not get stuck detaching metadata VDI
edwintorok 5caaeeb
CA-277346: stop (DB) requests when shutting down the master
edwintorok baa25b0
Tasks.with_tasks_destroy: add a function that waits for tasks with a …
edwintorok b5cded1
Convert host evacuation script into ocaml
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
module D=Debug.Make(struct let name="xapi" end) | ||
open D | ||
|
||
let estimate_evacuate_timeout ~__context ~host = | ||
let mref = Db.Host.get_metrics ~__context ~self:host in | ||
let metrics = Db.Host_metrics.get_record ~__context ~self:mref in | ||
let memory_used = Int64.sub metrics.API.host_metrics_memory_total metrics.API.host_metrics_memory_free in | ||
(* Conservative estimation based on 1000Mbps link, and the memory usage of | ||
Dom0 (which is not going to be transferred) is an intentional surplus *) | ||
let t = ((Int64.to_float memory_used) *. 8. /. (1000. *. 1024. *. 1024.)) in | ||
max 240. t | ||
|
||
(* Returns a tuple of lists: The first containing the control domains, and the second containing the regular VMs *) | ||
let get_resident_vms ~__context ~self = | ||
let my_resident_vms = Db.Host.get_resident_VMs ~__context ~self in | ||
List.partition (fun vm -> Db.VM.get_is_control_domain ~__context ~self:vm) my_resident_vms | ||
|
||
let ensure_no_vms ~__context ~rpc ~session_id ~evacuate_timeout = | ||
let open Client in | ||
|
||
let is_running vm = | ||
Db.VM.get_power_state ~__context ~self:vm = `Running | ||
in | ||
|
||
let host = Helpers.get_localhost ~__context in | ||
let self_managed_poweroff vm = | ||
let result = Db.VM.get_other_config ~__context ~self:vm | ||
|> List.mem_assoc "auto_poweroff" in | ||
if result then | ||
debug "Skip running VM %s: has self-managed poweroff" (Db.VM.get_name_label ~__context ~self:vm); | ||
result | ||
in | ||
let get_running_domains () = | ||
get_resident_vms ~__context ~self:host |> snd | ||
|> List.filter (fun vm -> is_running vm && not (self_managed_poweroff vm)) | ||
in | ||
|
||
let cancel_vm_tasks self = | ||
Db.VM.get_current_operations ~__context ~self | ||
|> List.rev_map fst | ||
|> List.rev_map Ref.of_string | ||
|> List.iter (fun (task:[`task] Ref.t) -> | ||
let name = Db.VM.get_name_label ~__context ~self in | ||
debug "Canceling operation on VM %s" name; | ||
log_and_ignore_exn (fun () -> Client.Task.cancel ~rpc ~session_id ~task)) | ||
in | ||
|
||
let evacuate () = | ||
TaskHelper.exn_if_cancelling ~__context; (* First check if _we_ have been cancelled *) | ||
info "Requesting evacuation of host"; | ||
let timeout = if evacuate_timeout > 0. then evacuate_timeout | ||
else estimate_evacuate_timeout ~__context ~host in | ||
let tasks = [ Client.Async.Host.evacuate ~rpc ~session_id ~host ] in | ||
if not (Tasks.with_tasks_destroy ~rpc ~session_id ~timeout ~tasks) then begin | ||
get_running_domains () | ||
|> List.iter cancel_vm_tasks | ||
end | ||
in | ||
|
||
let clean_shutdown vms = | ||
TaskHelper.exn_if_cancelling ~__context; (* First check if _we_ have been cancelled *) | ||
let tasks = | ||
vms | ||
|> List.filter (fun vm -> | ||
List.mem `clean_shutdown (Client.VM.get_allowed_operations ~rpc ~session_id ~self:vm)) | ||
|> List.map (fun vm -> | ||
let name_label = Client.VM.get_name_label ~rpc ~session_id ~self:vm in | ||
debug "Requesting clean shutdown of VM: %s" name_label; | ||
Client.Async.VM.clean_shutdown ~rpc ~session_id ~vm) in | ||
Tasks.with_tasks_destroy ~rpc ~session_id ~timeout:60. ~tasks |> ignore | ||
in | ||
|
||
let hard_shutdown vms = | ||
TaskHelper.exn_if_cancelling ~__context; (* First check if _we_ have been cancelled *) | ||
let tasks = | ||
vms | ||
|> List.map (fun vm -> | ||
let name_label = Client.VM.get_name_label ~rpc ~session_id ~self:vm in | ||
debug "Requesting hard shutdown of VM: %s" name_label; | ||
Client.Async.VM.hard_shutdown ~rpc ~session_id ~vm) in | ||
(* no timeout: we need the VMs to be off *) | ||
Tasks.wait_for_all ~rpc ~session_id ~tasks; | ||
vms | ||
|> List.filter is_running | ||
|> List.iter (fun vm -> | ||
let name_label = Client.VM.get_name_label ~rpc ~session_id ~self:vm in | ||
info "Failure performing hard shutdown of VM: %s" name_label) | ||
in | ||
|
||
let shutdown vms = | ||
log_and_ignore_exn (fun () -> clean_shutdown vms); | ||
(* We can unplug the PBD if a VM is suspended or halted, but not if | ||
* it is running or paused, i.e. "live" *) | ||
vms | ||
|> List.filter (fun self -> Xapi_vm_lifecycle.is_live ~__context ~self) | ||
|> hard_shutdown | ||
in | ||
|
||
log_and_ignore_exn (fun () -> | ||
Client.Host.get_vms_which_prevent_evacuation ~rpc ~session_id ~self:host | ||
|> Xapi_stdext_std.Listext.List.filter_map (fun (vm, _) -> | ||
if self_managed_poweroff vm then None | ||
else Some vm) | ||
|> shutdown; | ||
|
||
evacuate ()); | ||
|
||
log_and_ignore_exn (fun () -> get_running_domains () |> shutdown) | ||
|
||
let ensure_no_vms ~__context ~evacuate_timeout = | ||
Helpers.call_api_functions ~__context (fun rpc session_id -> | ||
ensure_no_vms ~__context ~rpc ~session_id ~evacuate_timeout) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need to update the opam file as well?