Skip to content

Commit

Permalink
Merge pull request #5637 from BengangY/private/bengangy/CP-48570
Browse files Browse the repository at this point in the history
CP-48570: Load recommendations from config file when Xapi starts
  • Loading branch information
minglumlu authored May 23, 2024
2 parents da8ad7f + 1f9d39e commit d03febe
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 5 deletions.
5 changes: 5 additions & 0 deletions ocaml/idl/datamodel_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,11 @@ let t =
; field ~qualifier:DynamicRO ~lifecycle:[] ~ty:Bool
~default_value:(Some (VBool false)) "update_sync_enabled"
"Whether periodic update synchronization is enabled or not"
; field ~qualifier:DynamicRO ~lifecycle:[]
~ty:(Map (String, String))
~default_value:(Some (VMap [])) "recommendations"
"The recommended pool properties for clients to respect for \
optimal performance. e.g. max-vm-group=5"
]
)
()
2 changes: 1 addition & 1 deletion ocaml/idl/schematest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let hash x = Digest.string x |> Digest.to_hex
(* BEWARE: if this changes, check that schema has been bumped accordingly in
ocaml/idl/datamodel_common.ml, usually schema_minor_vsn *)

let last_known_schema_hash = "81ba6b9699b3f6c2969dd5510cbbe4aa"
let last_known_schema_hash = "024b6ce32246d7549898310675631d51"

let current_schema_hash : string =
let open Datamodel_types in
Expand Down
5 changes: 3 additions & 2 deletions ocaml/tests/common/test_common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ let make_pool ~__context ~master ?(name_label = "") ?(name_description = "")
?(telemetry_uuid = Ref.null) ?(telemetry_frequency = `weekly)
?(telemetry_next_collection = API.Date.never)
?(last_update_sync = API.Date.epoch) ?(update_sync_frequency = `daily)
?(update_sync_day = 0L) ?(update_sync_enabled = false) () =
?(update_sync_day = 0L) ?(update_sync_enabled = false)
?(recommendations = []) () =
let pool_ref = Ref.make () in
Db.Pool.create ~__context ~ref:pool_ref ~uuid:(make_uuid ()) ~name_label
~name_description ~master ~default_SR ~suspend_image_SR ~crash_dump_SR
Expand All @@ -316,7 +317,7 @@ let make_pool ~__context ~master ?(name_label = "") ?(name_description = "")
~migration_compression ~coordinator_bias ~telemetry_uuid
~telemetry_frequency ~telemetry_next_collection ~last_update_sync
~local_auth_max_threads:8L ~ext_auth_max_threads:8L ~update_sync_frequency
~update_sync_day ~update_sync_enabled ;
~update_sync_day ~update_sync_enabled ~recommendations ;
pool_ref

let default_sm_features =
Expand Down
6 changes: 6 additions & 0 deletions ocaml/xapi-cli-server/records.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,12 @@ let pool_record rpc session_id pool =
; make_field ~name:"update-sync-enabled"
~get:(fun () -> (x ()).API.pool_update_sync_enabled |> string_of_bool)
()
; make_field ~name:"recommendations"
~get:(fun () ->
Record_util.s2sm_to_string "; " (x ()).API.pool_recommendations
)
~get_map:(fun () -> (x ()).API.pool_recommendations)
()
]
}

Expand Down
18 changes: 16 additions & 2 deletions ocaml/xapi/dbsync_master.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module D = Debug.Make (struct let name = "dbsync" end)

open D
open Client
open Recommendations

(* Synchronising code which is specific to the master *)

Expand Down Expand Up @@ -53,7 +54,7 @@ let create_pool_record ~__context =
~last_update_sync:Xapi_stdext_date.Date.epoch
~update_sync_frequency:`weekly ~update_sync_day:0L
~update_sync_enabled:false ~local_auth_max_threads:8L
~ext_auth_max_threads:1L
~ext_auth_max_threads:1L ~recommendations:[]

let set_master_ip ~__context =
let ip =
Expand Down Expand Up @@ -336,6 +337,18 @@ let setup_telemetry ~__context =
)
()

let update_pool_recommendations_noexn ~__context =
Helpers.log_exn_continue "update pool recommendations"
(fun () ->
let pool = Helpers.get_pool ~__context in
let recommendations =
Recommendations.load ~path:!Xapi_globs.pool_recommendations_dir
|> StringMap.bindings
in
Db.Pool.set_recommendations ~__context ~self:pool ~value:recommendations
)
()

(* Update the database to reflect current state. Called for both start of day and after
an agent restart. *)
let update_env __context =
Expand All @@ -360,4 +373,5 @@ let update_env __context =
Storage_access.on_xapi_start ~__context ;
if !Xapi_globs.create_tools_sr then
create_tools_sr_noexn __context ;
ensure_vm_metrics_records_exist_noexn __context
ensure_vm_metrics_records_exist_noexn __context ;
update_pool_recommendations_noexn ~__context
50 changes: 50 additions & 0 deletions ocaml/xapi/recommendations.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(*
* Copyright (c) Cloud Software Group, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*)

module Unixext = Xapi_stdext_unix.Unixext
module Config_file = Xcp_service.Config_file

module D = Debug.Make (struct let name = "recommendations" end)

open D
module StringMap = Map.Make (String)

let process_line map data =
match Config_file.parse_line data with
| Some (k, v) ->
debug "Parsing data, key: %s, value: %s" k v ;
StringMap.add k v map
| None ->
map

let parse map filename =
debug "Parsing recommendations file: %s" filename ;
Unixext.file_lines_fold process_line map filename

let load ~path =
(try Sys.readdir path with _ -> [||])
|> Array.to_list
|> List.filter (fun f -> Filename.check_suffix f ".conf")
|> List.stable_sort compare
|> List.map (Filename.concat path)
|> List.filter (fun f ->
match Unix.((stat f).st_kind) with
| Unix.S_REG ->
true
| _ ->
false
| exception _ ->
false
)
|> List.fold_left parse StringMap.empty
17 changes: 17 additions & 0 deletions ocaml/xapi/recommendations.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(*
* Copyright (c) Cloud Software Group, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*)

module StringMap : Map.S with type key = string

val load : path:string -> string StringMap.t
6 changes: 6 additions & 0 deletions ocaml/xapi/xapi_globs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,8 @@ let python3_path = ref "/usr/bin/python3"
let observer_experimental_components =
ref (StringSet.singleton Constants.observer_component_smapi)

let pool_recommendations_dir = ref "/etc/xapi.pool-recommendations.d"

let xapi_globs_spec =
[
( "master_connection_reset_timeout"
Expand Down Expand Up @@ -1803,6 +1805,10 @@ module Resources = struct
, trace_log_dir
, "Directory for storing traces exported to logs"
)
; ( "pool-recommendations-dir"
, pool_recommendations_dir
, "Directory containing files with recommendations in key=value format"
)
]

let xcp_resources =
Expand Down

0 comments on commit d03febe

Please sign in to comment.