Skip to content

Commit

Permalink
CP-48011: [Toolstack] - XAPI Support anti-affinity feature flag
Browse files Browse the repository at this point in the history
Signed-off-by: Bengang Yuan <bengang.yuan@cloud.com>
  • Loading branch information
BengangY committed May 15, 2024
1 parent 8c33011 commit ae33ef1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 37 deletions.
21 changes: 14 additions & 7 deletions ocaml/xapi/xapi_ha_vm_failover.ml
Original file line number Diff line number Diff line change
Expand Up @@ -623,13 +623,20 @@ let vm_anti_affinity_evacuation_plan ~__context total_hosts hosts vms
debug "Plan OK" ; plan
)
in
plan_in_steps ~__context
[
vm_anti_affinity_spread_evenly_plan vm_can_boot_on_host anti_affinity_vms
grp_ranked_hosts []
; vm_anti_affinity_no_breach_plan total_hosts vm_can_boot_on_host
anti_affinity_vms grp_ranked_hosts []
]
if Pool_features.is_enabled ~__context Features.VM_anti_affinity then
plan_in_steps ~__context
[
vm_anti_affinity_spread_evenly_plan vm_can_boot_on_host
anti_affinity_vms grp_ranked_hosts []
; vm_anti_affinity_no_breach_plan total_hosts vm_can_boot_on_host
anti_affinity_vms grp_ranked_hosts []
]
else (
debug
"VM anti-affinity feature is disabled, ignore VM anti-affinity during \
host evacuation" ;
binpack_plan ~__context config vms
)

(** Return a VM -> Host plan for the Host.evacuate code. We assume the VMs are all agile. The returned plan may
be incomplete if there was not enough memory. *)
Expand Down
1 change: 1 addition & 0 deletions ocaml/xapi/xapi_vm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,7 @@ let set_appliance ~__context ~self ~value =
update_allowed_operations ~__context ~self

let set_groups ~__context ~self ~value =
Pool_features.assert_enabled ~__context ~f:Features.VM_anti_affinity ;
if List.length value > 1 then
raise Api_errors.(Server_error (Api_errors.too_many_groups, [])) ;
Db.VM.set_groups ~__context ~self ~value
Expand Down
1 change: 1 addition & 0 deletions ocaml/xapi/xapi_vm_group.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
module D = Debug.Make (struct let name = "xapi_vm_group" end)

let create ~__context ~name_label ~name_description ~placement =
Pool_features.assert_enabled ~__context ~f:Features.VM_anti_affinity ;
let uuid = Uuidx.make () in
let ref = Ref.make () in
Db.VM_group.create ~__context ~ref ~uuid:(Uuidx.to_string uuid) ~name_label
Expand Down
39 changes: 21 additions & 18 deletions ocaml/xapi/xapi_vm_group_helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,27 @@ let check_breach_on_vm_anti_affinity_rules ~__context ~group =
Some h

let report_anti_affinity_alert ~__context ~group ~host =
let group_uuid = Db.VM_group.get_uuid ~__context ~self:group in
let host_uuid = Db.Host.get_uuid ~__context ~self:host in
let body =
String.concat ""
[
"<body><message>Breach on VM anti-affinity rules</message><VM_group>"
; group_uuid
; "</VM_group><host>"
; host_uuid
; "</host></body>"
]
in
let obj_uuid =
Db.Pool.get_uuid ~__context ~self:(Helpers.get_pool ~__context)
in
Xapi_alert.add
~msg:Api_messages.all_running_vms_in_anti_affinity_grp_on_single_host
~cls:`Pool ~obj_uuid ~body
if Pool_features.is_enabled ~__context Features.VM_anti_affinity then
let group_uuid = Db.VM_group.get_uuid ~__context ~self:group in
let host_uuid = Db.Host.get_uuid ~__context ~self:host in
let body =
String.concat ""
[
"<body><message>Breach on VM anti-affinity rules</message><VM_group>"
; group_uuid
; "</VM_group><host>"
; host_uuid
; "</host></body>"
]
in
let obj_uuid =
Db.Pool.get_uuid ~__context ~self:(Helpers.get_pool ~__context)
in
Xapi_alert.add
~msg:Api_messages.all_running_vms_in_anti_affinity_grp_on_single_host
~cls:`Pool ~obj_uuid ~body
else
debug "VM anti-affinity feature is disabled, alert will not be reported"

let get_anti_affinity_alerts ~__context =
Helpers.call_api_functions ~__context (fun rpc session_id ->
Expand Down
31 changes: 19 additions & 12 deletions ocaml/xapi/xapi_vm_helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -913,18 +913,25 @@ let vm_can_run_on_host ~__context ~vm ~snapshot ~do_memory_check host =
with _ -> false

let vm_has_anti_affinity ~__context ~vm =
List.find_opt
(fun g -> Db.VM_group.get_placement ~__context ~self:g = `anti_affinity)
(Db.VM.get_groups ~__context ~self:vm)
|> Option.map (fun group ->
debug
"The VM (uuid %s) is associated with an anti-affinity group (uuid: \
%s, name: %s)"
(Db.VM.get_uuid ~__context ~self:vm)
(Db.VM_group.get_uuid ~__context ~self:group)
(Db.VM_group.get_name_label ~__context ~self:group) ;
`AntiAffinity group
)
if Pool_features.is_enabled ~__context Features.VM_anti_affinity then
List.find_opt
(fun g -> Db.VM_group.get_placement ~__context ~self:g = `anti_affinity)
(Db.VM.get_groups ~__context ~self:vm)
|> Option.map (fun group ->
debug
"The VM (uuid %s) is associated with an anti-affinity group \
(uuid: %s, name: %s)"
(Db.VM.get_uuid ~__context ~self:vm)
(Db.VM_group.get_uuid ~__context ~self:group)
(Db.VM_group.get_name_label ~__context ~self:group) ;
`AntiAffinity group
)
else (
debug
"VM anti-affinity feature is disabled, ignore VM anti-affinity during VM \
start" ;
None
)

let vm_has_vgpu ~__context ~vm =
match Db.VM.get_VGPUs ~__context ~self:vm with
Expand Down

0 comments on commit ae33ef1

Please sign in to comment.