Skip to content

Commit

Permalink
fixup for xapi_vm_helpers.ml
Browse files Browse the repository at this point in the history
Signed-off-by: Gang Ji <gang.ji@citrix.com>
  • Loading branch information
Gang Ji committed May 1, 2024
1 parent 0133712 commit 6be3862
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions ocaml/xapi/xapi_vm_helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,13 @@ let rank_hosts_by_vm_cnt_in_group ~__context group hosts =
(fun h -> RefMap.find_opt h host_map |> Option.value ~default:0)
hosts

let get_affinity_host ~__context ~vm =
match Db.VM.get_affinity ~__context ~self:vm with
| ref when Db.is_valid_ref __context ref ->
Some ref
| _ ->
None

(* Group all hosts to 2 parts:
1. A list of affinity host (only one host).
2. A list of lists, each list contains hosts with the same number of
Expand All @@ -1050,36 +1057,40 @@ let rank_hosts_by_vm_cnt_in_group ~__context group hosts =
]
*)
let rank_hosts_by_placement ~__context ~vm ~group =
let affinity_host =
match Db.VM.get_affinity ~__context ~self:vm with
| ref when Db.is_valid_ref __context ref ->
[ref]
| _ ->
[]
in
let hosts_without_affinity_host =
Db.Host.get_all ~__context
|> List.filter (fun host -> not (List.mem host affinity_host))
let hosts = Db.Host.get_all ~__context in
let affinity_host = get_affinity_host ~__context ~vm in
let hosts_without_affinity =
Option.fold ~none:hosts
~some:(fun host -> List.filter (( <> ) host) hosts)
affinity_host
in
let sorted_hosts =
hosts_without_affinity_host
hosts_without_affinity
|> rank_hosts_by_vm_cnt_in_group ~__context group
|> List.map (fun g -> List.map fst g)
|> List.(map (map fst))
in
affinity_host :: sorted_hosts |> List.filter (( <> ) [])
match affinity_host with
| Some host ->
[host] :: sorted_hosts
| None ->
sorted_hosts

let rec select_host_from_ranked_groups vm host_selector groups_of_hosts =
match groups_of_hosts with
| [] ->
raise (Api_errors.Server_error (Api_errors.no_hosts_available, []))
| hosts :: less_optimal_groups_of_hosts -> (
debug "Attempting to start VM (%s) on one of equally optimal hosts [ %s ]"
(Ref.string_of vm)
(String.concat ";" (List.map Ref.string_of hosts)) ;
let hosts_str = String.concat ";" (List.map Ref.string_of hosts) in
debug
"Attempting to select host for VM (%s) in a group of equally optimal \
hosts [ %s ]"
(Ref.string_of vm) hosts_str ;
try host_selector hosts
with _ ->
info "Failed to start VM (%s) on any of [ %s ]" (Ref.string_of vm)
(String.concat ";" (List.map Ref.string_of hosts)) ;
info
"Failed to select host for VM (%s) in any of [ %s ], continue to \
select from less optimal hosts"
(Ref.string_of vm) hosts_str ;
select_host_from_ranked_groups vm host_selector
less_optimal_groups_of_hosts
)
Expand Down

0 comments on commit 6be3862

Please sign in to comment.