diff --git a/ocaml/xapi/helpers.ml b/ocaml/xapi/helpers.ml index fe75c4dc0e..39162cca2d 100644 --- a/ocaml/xapi/helpers.ml +++ b/ocaml/xapi/helpers.ml @@ -505,10 +505,10 @@ let group_by f list = (** Groups list elements by equality of result of function application sorted * in order of that result *) -let group_by ?(descending=false) f list = - match descending with - | true -> group_by f list - | false -> List.rev (group_by f list) +let group_by ~ordering f list = + match ordering with + | `descending -> group_by f list + | `ascending -> List.rev (group_by f list) (** Schwarzian transform sort *) let sort_by_schwarzian ?(descending=false) f list = diff --git a/ocaml/xapi/xapi_vm_helpers.ml b/ocaml/xapi/xapi_vm_helpers.ml index 92a32bff8c..2f3514cc46 100644 --- a/ocaml/xapi/xapi_vm_helpers.ml +++ b/ocaml/xapi/xapi_vm_helpers.ml @@ -574,12 +574,13 @@ let group_hosts_by_best_pgpu_in_group ~__context gpu_group vgpu_type = (List.map (fun pgpu -> Db.PGPU.get_host ~__context ~self:pgpu) viable_pgpus) in - Helpers.group_by + let ordering = + match Db.GPU_group.get_allocation_algorithm ~__context ~self:gpu_group with + | `depth_first -> `ascending | `breadth_first -> `descending + in + Helpers.group_by ~ordering (fun host -> - let group_by_capacity pgpus = - (match Db.GPU_group.get_allocation_algorithm ~__context ~self:gpu_group with - | `depth_first -> Helpers.group_by ~descending:false - | `breadth_first -> Helpers.group_by ~descending:true) + let group_by_capacity pgpus = Helpers.group_by ~ordering (fun pgpu -> Xapi_pgpu_helpers.get_remaining_capacity ~__context ~self:pgpu ~vgpu_type) pgpus in