Skip to content

Commit 8789d0c

Browse files
authored
[LCM to feature] Merge LCM to feature branch (#6962)
2 parents 7b7a701 + eaf7841 commit 8789d0c

15 files changed

Lines changed: 202 additions & 178 deletions

File tree

ocaml/idl/ocaml_backend/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
(libraries
55
astring
66
cmdliner
7+
fmt
8+
ptime.clock
79
uuidm
810
xapi-consts
911
xapi-datamodel

ocaml/idl/ocaml_backend/gen_rbac.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ let internal_role_local_root = "_local_root_"
3434

3535
(* the output of this function is used as input by the automatic tests *)
3636
let writer_csv static_permissions_roles =
37-
Printf.sprintf "%s,PERMISSION/ROLE,%s\n"
38-
(let t = Debug.gettimestring () in
39-
String.sub t 0 (String.length t - 1)
40-
)
37+
let now =
38+
let now = Ptime_clock.now () in
39+
let str = Fmt.str "%a" Ptime.(pp_rfc3339 ~frac_s:3 ~tz_offset_s:0 ()) now in
40+
(* remove separators between Year, Month, and Day; to keep old logging format *)
41+
Astring.String.filter (function '-' -> false | _ -> true) str
42+
in
43+
Printf.sprintf "%s,PERMISSION/ROLE,%s\n" now
4144
(* role titles are ordered by roles in roles_all *)
4245
(List.fold_left (fun rr r -> rr ^ r ^ ",") "" Datamodel_roles.roles_all)
4346
^ List.fold_left

ocaml/libs/log/debug.ml

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,8 @@ let tasks : task ThreadLocalTable.t = ThreadLocalTable.make ()
7474
let names : string ThreadLocalTable.t = ThreadLocalTable.make ()
7575

7676
let gettimestring () =
77-
let time = Unix.gettimeofday () in
78-
let tm = Unix.gmtime time in
79-
let msec = time -. floor time in
80-
Printf.sprintf "%d%.2d%.2dT%.2d:%.2d:%.2d.%.3dZ|" (1900 + tm.Unix.tm_year)
81-
(tm.Unix.tm_mon + 1) tm.Unix.tm_mday tm.Unix.tm_hour tm.Unix.tm_min
82-
tm.Unix.tm_sec
83-
(int_of_float (1000.0 *. msec))
77+
let now = Ptime_clock.now () in
78+
Fmt.str "%a|" Ptime.(pp_rfc3339 ~frac_s:3 ~tz_offset_s:0 ()) now
8479

8580
(** [escape str] efficiently escapes non-printable characters and in addition
8681
the backslash character. The function is efficient in the sense that it will
@@ -216,20 +211,14 @@ let init_logs () =
216211
calling [output_log] too often. *)
217212
Logs.set_level (Some Logs.Warning)
218213

219-
let rec split_c c str =
220-
try
221-
let i = String.index str c in
222-
String.sub str 0 i
223-
:: split_c c (String.sub str (i + 1) (String.length str - i - 1))
224-
with Not_found -> [str]
225-
226214
let log_backtrace_exn ?(level = Syslog.Err) ?(msg = "error") exn bt =
227-
(* We already got the backtrace in the `bt` argument when called from with_thread_associated.
228-
Log that, and remove `exn` from the backtraces table.
229-
If with_backtraces was not nested then looking at `bt` is the only way to get
230-
a proper backtrace, otherwise exiting from `with_backtraces` would've removed the backtrace
231-
from the thread-local backtraces table, and we'd always just log a message complaining about
232-
with_backtraces not being called, which is not true because it was.
215+
(* We already got the backtrace in the `bt` argument when called from
216+
with_thread_associated. Log that, and remove `exn` from the backtraces
217+
table. If with_backtraces was not nested then looking at `bt` is the only
218+
way to get a proper backtrace, otherwise exiting from `with_backtraces`
219+
would've removed the backtrace from the thread-local backtraces table, and
220+
we'd always just log a message complaining about with_backtraces not being
221+
called, which is not true because it was.
233222
*)
234223
let bt' = Backtrace.remove exn in
235224
(* bt could be empty, but bt' would contain a non-empty warning, so compare 'bt' here *)
@@ -239,7 +228,7 @@ let log_backtrace_exn ?(level = Syslog.Err) ?(msg = "error") exn bt =
239228
else
240229
bt
241230
in
242-
let all = split_c '\n' Backtrace.(to_string_hum bt) in
231+
let all = String.split_on_char '\n' Backtrace.(to_string_hum bt) in
243232
(* Write to the log line at a time *)
244233
output_log "backtrace" level msg
245234
(Printf.sprintf "Raised %s" (Printexc.to_string exn)) ;

ocaml/libs/log/debug.mli

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ val with_thread_named : string -> ('a -> 'b) -> 'a -> 'b
3131

3232
module type BRAND = sig val name : string end
3333

34-
val gettimestring : unit -> string
35-
(** The current time of day in a format suitable for logging *)
36-
3734
val set_facility : Syslog.facility -> unit
3835
(** Set the syslog facility that will be used by this program. *)
3936

ocaml/libs/log/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
fmt
1010
mtime
1111
logs
12+
ptime
13+
ptime.clock
1214
threads.posix
1315
xapi-backtrace
1416
unix

ocaml/libs/xapi-stdext/lib/xapi-stdext-std/listext.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ module List = struct
8686
in
8787
loop [] l
8888

89+
let try_map_collect f l =
90+
let rec loop acc = function
91+
| [] ->
92+
Ok (List.rev acc)
93+
| x :: xs -> (
94+
match f x with
95+
| Ok r ->
96+
loop (r :: acc) xs
97+
| Error e ->
98+
Error (List.rev acc, e)
99+
)
100+
in
101+
loop [] l
102+
89103
let take n list =
90104
let rec loop i acc = function
91105
| x :: xs when i < n ->

ocaml/libs/xapi-stdext/lib/xapi-stdext-std/listext.mli

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ module List : sig
4040
(** [last l] returns the last element of [l] or None if [l] is empty *)
4141

4242
val try_map : ('a -> ('b, 'c) result) -> 'a list -> ('b list, 'c) result
43-
(** [try_map f l] applies [f] to all elements of [l] in turn. Returns the
44-
first [Error] result encountered or, if no errors were produced, returns
45-
all the [Ok] results. *)
43+
(** [try_map f l] applies [f] to elements of [l] in turn. Returns the first
44+
[Error] result encountered or, if no errors were produced, returns all
45+
the [Ok] results. *)
46+
47+
val try_map_collect :
48+
('a -> ('b, 'c) result) -> 'a list -> ('b list, 'b list * 'c) result
49+
(** [try_map_collect f l] applies [f] to elements of [l] in turn. Returns all
50+
the [Ok] results, and the first [Error] result encountered, if it is
51+
encountered. *)
4652

4753
val rev_mapi : (int -> 'a -> 'b) -> 'a list -> 'b list
4854
(** [rev_map f l] gives the same result as {!Stdlib.List.rev}[ (]

ocaml/xapi-cli-server/cli_operations.ml

Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4839,56 +4839,73 @@ let vm_migrate printer rpc session_id params =
48394839
(read_map_params "vgpu" params)
48404840
in
48414841
let preferred_sr =
4842-
(* The preferred SR is determined to be as the SR that the destine host has a PDB attached to it,
4843-
and among the choices of that the shared is preferred first(as it is recommended to have shared storage
4844-
in pool to host VMs), and then the one with the maximum available space *)
4842+
(* The preferred SR is determined to be as the SR that the
4843+
destination host has a PDB attached to it, and among the choices
4844+
of that the shared is preferred first (as it is recommended to
4845+
have shared storage in pool to host VMs), and then the one with
4846+
the maximum available space *)
48454847
try
4846-
let expr =
4847-
Printf.sprintf
4848-
{|(field "host"="%s") and (field "currently_attached"="true")|}
4849-
(Ref.string_of host)
4848+
let host_attached_pbds =
4849+
let expr =
4850+
Printf.sprintf
4851+
{|(field "host"="%s") and (field "currently_attached"="true")|}
4852+
(Ref.string_of host)
4853+
in
4854+
remote Client.PBD.get_all_records_where ~expr
48504855
in
4851-
let srs =
4852-
remote Client.PBD.get_all_where ~expr
4853-
|> List.map (fun pbd ->
4854-
let sr = remote Client.PBD.get_SR ~self:pbd in
4855-
(sr, remote Client.SR.get_record ~self:sr)
4856-
)
4856+
let shared_non_iso_srs () =
4857+
let expr =
4858+
{|(not (field "content_type"="iso")) and (field "shared"="true")|}
4859+
in
4860+
remote Client.SR.get_all_where ~expr
48574861
in
4858-
(* In the following loop, the current SR:sr' will be compared with previous checked ones,
4859-
first if it is an ISO type, then pass this one for selection, then the only shared one from this and
4860-
previous one will be valued, and if not that case (both shared or none shared), choose the one with
4861-
more space available *)
4862-
let sr, _ =
4863-
List.fold_left
4864-
(fun (sr, free_space) ((_, sr_rec') as sr') ->
4865-
if sr_rec'.API.sR_content_type = "iso" then
4866-
(sr, free_space)
4867-
else
4868-
let free_space' =
4869-
Int64.sub sr_rec'.API.sR_physical_size
4870-
sr_rec'.API.sR_physical_utilisation
4862+
let local_non_iso_srs () =
4863+
let expr =
4864+
{|(not (field "content_type"="iso")) and (field "shared"="false")|}
4865+
in
4866+
remote Client.SR.get_all_where ~expr
4867+
in
4868+
let get_free_space_of non_iso_srs =
4869+
host_attached_pbds
4870+
|> List.filter_map (fun (_, pbd_rec) ->
4871+
let sr = pbd_rec.API.pBD_SR in
4872+
if List.mem sr non_iso_srs then
4873+
let size = remote Client.SR.get_physical_size ~self:sr in
4874+
let used =
4875+
remote Client.SR.get_physical_utilisation ~self:sr
48714876
in
4872-
match sr with
4873-
| None ->
4874-
(Some sr', free_space')
4875-
| Some ((_, sr_rec) as sr) -> (
4876-
match (sr_rec.API.sR_shared, sr_rec'.API.sR_shared) with
4877-
| true, false ->
4878-
(Some sr, free_space)
4879-
| false, true ->
4880-
(Some sr', free_space')
4881-
| _ ->
4882-
if free_space' > free_space then
4883-
(Some sr', free_space')
4884-
else
4885-
(Some sr, free_space)
4886-
)
4887-
)
4888-
(None, Int64.zero) srs
4877+
Some (sr, Int64.sub size used)
4878+
else
4879+
None
4880+
)
48894881
in
4890-
match sr with Some (sr_ref, _) -> Some sr_ref | _ -> None
4891-
with _ -> None
4882+
let find_most_free_space srs =
4883+
match
4884+
List.fast_sort
4885+
(fun (_, a) (_, b) -> Int64.compare b a)
4886+
(get_free_space_of srs)
4887+
with
4888+
| (sr, _) :: _ ->
4889+
Some sr
4890+
| [] ->
4891+
None
4892+
in
4893+
match find_most_free_space (shared_non_iso_srs ()) with
4894+
| Some sr ->
4895+
Some sr
4896+
| None ->
4897+
find_most_free_space (local_non_iso_srs ())
4898+
with exn ->
4899+
printer
4900+
(Cli_printer.PMsg
4901+
(Printf.sprintf
4902+
"Couldn't compute preferred SR, continuing with the \
4903+
user-provided VDI mapping. The reason is: %s"
4904+
(Printexc.to_string exn)
4905+
)
4906+
) ;
4907+
4908+
None
48924909
in
48934910
let vdi_map =
48944911
match preferred_sr with

ocaml/xapi/dbsync_slave.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,21 @@ let create_localhost ~__context info =
5252
in
5353
(* me = None on firstboot only *)
5454
if me = None then
55+
(* Restore the host.last_update_hash when this is an ejected host. *)
56+
let last_update_hash =
57+
let k = "last_update_hash" in
58+
let x = Localdb.get k |> Option.value ~default:"" in
59+
(try Localdb.del k with _ -> ()) ;
60+
x
61+
in
5562
let (_ : API.ref_host) =
5663
Xapi_host.create ~__context ~uuid:info.uuid ~name_label:info.hostname
5764
~name_description:"" ~hostname:info.hostname ~address:ip
5865
~external_auth_type:"" ~external_auth_service_name:""
5966
~external_auth_configuration:[] ~license_params:[] ~edition:""
6067
~license_server:[("address", "localhost"); ("port", "27000")]
6168
~local_cache_sr:Ref.null ~chipset_info:[] ~ssl_legacy:false
62-
~last_software_update:Date.epoch ~last_update_hash:""
69+
~last_software_update:Date.epoch ~last_update_hash
6370
~ssh_enabled:Constants.default_ssh_enabled
6471
~ssh_enabled_timeout:Constants.default_ssh_enabled_timeout
6572
~ssh_expiry:Date.epoch

ocaml/xapi/helpers.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ let call_script ?(log_output = Always) ?env ?stdin ?timeout script args =
126126
(String.concat " " (filter_args args))
127127
message stdout stderr ;
128128
raise e
129+
| e ->
130+
debug "%s: unexpected exception raised: %s" __FUNCTION__
131+
(ExnHelper.string_of_exn e) ;
132+
raise e
129133

130134
(** Construct a descriptive network name (used as name_label) for a give network interface. *)
131135
let choose_network_name_for_pif device = function

0 commit comments

Comments
 (0)