Skip to content

Commit deab0ce

Browse files
committed
xapi: remove some usages of common, exception-raising functions
The Not_found and hd exceptions keep popping up, and it's difficult to find them when there are no backtraces logged. Remove usages if them, even if they are not problematic so the actual problematic ones can be flushed out over time. Signed-off-by: Pau Ruiz Safont <pau.safont@vates.tech>
1 parent 125cf6e commit deab0ce

File tree

6 files changed

+55
-125
lines changed

6 files changed

+55
-125
lines changed

ocaml/xapi/create_storage.ml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,16 @@ let maybe_create_pbd rpc session_id sr device_config me =
8181
in
8282
(* Check not more than 1 pbd in the database *)
8383
let pbds =
84-
if List.length pbds > 1 then (
85-
(* shouldn't happen... delete all but first pbd to make db consistent again *)
86-
List.iter
87-
(fun pbd -> Client.PBD.destroy ~rpc ~session_id ~self:pbd)
88-
(List.tl pbds) ;
89-
[List.hd pbds]
90-
) else
91-
pbds
84+
match pbds with
85+
| [] | [_] ->
86+
pbds
87+
| pbd :: pbds ->
88+
(* shouldn't happen... delete all but first pbd to make db consistent
89+
again *)
90+
List.iter
91+
(fun pbd -> Client.PBD.destroy ~rpc ~session_id ~self:pbd)
92+
pbds ;
93+
[pbd]
9294
in
9395
if pbds = [] (* If there's no PBD, create it *) then
9496
Client.PBD.create ~rpc ~session_id ~host:me ~sR:sr ~device_config
@@ -153,17 +155,16 @@ let initialise_storage (me : API.ref_host) rpc session_id __context : unit =
153155
List.filter (fun (_, pbd_rec) -> pbd_rec.API.pBD_host = master) pbds
154156
in
155157
let maybe_create_pbd_for_shared_sr s =
156-
let _, mpbd_rec =
157-
List.find (fun (_, pbdr) -> pbdr.API.pBD_SR = s) master_pbds
158-
in
159-
let master_devconf = mpbd_rec.API.pBD_device_config in
160-
let my_devconf = List.remove_assoc "SRmaster" master_devconf in
161-
(* this should never be used *)
162-
maybe_create_pbd rpc session_id s my_devconf me
158+
List.find_opt (fun (_, pbdr) -> pbdr.API.pBD_SR = s) master_pbds
159+
|> Option.iter @@ fun (_, mpbd_rec) ->
160+
let master_devconf = mpbd_rec.API.pBD_device_config in
161+
let my_devconf = List.remove_assoc "SRmaster" master_devconf in
162+
try
163+
(maybe_create_pbd rpc session_id s my_devconf me : [`PBD] Ref.t)
164+
|> ignore
165+
with _ -> ()
163166
in
164-
List.iter
165-
(fun s -> try ignore (maybe_create_pbd_for_shared_sr s) with _ -> ())
166-
shared_sr_refs
167+
List.iter maybe_create_pbd_for_shared_sr shared_sr_refs
167168
in
168169
let other_config =
169170
try
@@ -173,9 +174,8 @@ let initialise_storage (me : API.ref_host) rpc session_id __context : unit =
173174
in
174175
if
175176
not
176-
(List.mem_assoc Xapi_globs.sync_create_pbds other_config
177-
&& List.assoc Xapi_globs.sync_create_pbds other_config
178-
= Xapi_globs.sync_switch_off
177+
(List.assoc_opt Xapi_globs.sync_create_pbds other_config
178+
= Some Xapi_globs.sync_switch_off
179179
)
180180
then (
181181
debug "Creating PBDs for shared SRs" ;

ocaml/xapi/system_domains.ml

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -148,36 +148,6 @@ let is_in_use ~__context ~self =
148148
else
149149
false
150150

151-
(* [wait_for ?timeout f] returns true if [f()] (called at 1Hz) returns true within
152-
the [timeout] period and false otherwise *)
153-
let wait_for ?(timeout = 120.) f =
154-
let start = Unix.gettimeofday () in
155-
let finished = ref false in
156-
let success = ref false in
157-
while not !finished do
158-
let remaining = timeout -. (Unix.gettimeofday () -. start) in
159-
if remaining < 0. then
160-
finished := true
161-
else
162-
try
163-
if f () then (
164-
success := true ;
165-
finished := true
166-
) else
167-
Thread.delay 1.
168-
with _ -> Thread.delay 1.
169-
done ;
170-
!success
171-
172-
let pingable ip () =
173-
try
174-
let (_ : string * string) =
175-
Forkhelpers.execute_command_get_output "/bin/ping"
176-
["-c"; "1"; "-w"; "1"; ip]
177-
in
178-
true
179-
with _ -> false
180-
181151
let queryable ~__context transport () =
182152
let open Xmlrpc_client in
183153
let tracing = Context.set_client_span __context in
@@ -197,47 +167,6 @@ let queryable ~__context transport () =
197167
(Printexc.to_string e) ;
198168
false
199169

200-
let ip_of ~__context driver =
201-
(* Find the VIF on the Host internal management network *)
202-
let vifs = Db.VM.get_VIFs ~__context ~self:driver in
203-
let hin = Helpers.get_host_internal_management_network ~__context in
204-
let ip =
205-
let vif =
206-
try
207-
List.find
208-
(fun vif -> Db.VIF.get_network ~__context ~self:vif = hin)
209-
vifs
210-
with Not_found ->
211-
failwith
212-
(Printf.sprintf
213-
"driver domain %s has no VIF on host internal management network"
214-
(Ref.string_of driver)
215-
)
216-
in
217-
match Xapi_udhcpd.get_ip ~__context vif with
218-
| Some (a, b, c, d) ->
219-
Printf.sprintf "%d.%d.%d.%d" a b c d
220-
| None ->
221-
failwith
222-
(Printf.sprintf
223-
"driver domain %s has no IP on the host internal management \
224-
network"
225-
(Ref.string_of driver)
226-
)
227-
in
228-
info "driver domain uuid:%s ip:%s" (Db.VM.get_uuid ~__context ~self:driver) ip ;
229-
if not (wait_for (pingable ip)) then
230-
failwith
231-
(Printf.sprintf "driver domain %s is not responding to IP ping"
232-
(Ref.string_of driver)
233-
) ;
234-
if not (wait_for (queryable ~__context (Xmlrpc_client.TCP (ip, 80)))) then
235-
failwith
236-
(Printf.sprintf "driver domain %s is not responding to XMLRPC query"
237-
(Ref.string_of driver)
238-
) ;
239-
ip
240-
241170
type service = {uuid: string; ty: string; instance: string; url: string}
242171
[@@deriving rpc]
243172

ocaml/xapi/system_domains.mli

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ val is_in_use : __context:Context.t -> self:API.ref_VM -> bool
5151
val queryable : __context:Context.t -> Xmlrpc_client.transport -> unit -> bool
5252
(** [queryable ip port ()] returns true if [ip]:[port] responsds to an XMLRPC query *)
5353

54-
val ip_of : __context:Context.t -> API.ref_VM -> string
55-
(** [ip_of __context vm] returns the IP of the given VM on the internal management network *)
56-
5754
(** One of many service running in a driver domain *)
5855
type service = {uuid: string; ty: string; instance: string; url: string}
5956

ocaml/xapi/xapi_ha.ml

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,10 +1869,7 @@ let enable __context heartbeat_srs configuration =
18691869
with _ -> false
18701870
in
18711871
if not alive then
1872-
raise
1873-
(Api_errors.Server_error
1874-
(Api_errors.host_offline, [Ref.string_of host])
1875-
)
1872+
raise Api_errors.(Server_error (host_offline, [Ref.string_of host]))
18761873
)
18771874
(Db.Host.get_all ~__context) ;
18781875
let pool = Helpers.get_pool ~__context in
@@ -1897,20 +1894,23 @@ let enable __context heartbeat_srs configuration =
18971894
else
18981895
heartbeat_srs
18991896
in
1900-
if possible_srs = [] then
1901-
raise (Api_errors.Server_error (Api_errors.cannot_create_state_file, [])) ;
1902-
(* For the moment we'll create a state file in one compatible SR since the xHA component only handles one *)
1903-
let srs = [List.hd possible_srs] in
1897+
(* For the moment we'll create a state file in one compatible SR since the
1898+
xHA component only handles one *)
1899+
let sr =
1900+
match possible_srs with
1901+
| [] ->
1902+
raise Api_errors.(Server_error (cannot_create_state_file, []))
1903+
| sr :: _ ->
1904+
sr
1905+
in
19041906
List.iter
19051907
(fun sr ->
19061908
let vdi = Xha_statefile.find_or_create ~__context ~sr ~cluster_stack in
19071909
statefile_vdis := vdi :: !statefile_vdis
19081910
)
1909-
srs ;
1911+
[sr] ;
19101912
(* For storing the database, assume there is only one SR *)
1911-
let database_vdi =
1912-
Xha_metadata_vdi.find_or_create ~__context ~sr:(List.hd srs)
1913-
in
1913+
let database_vdi = Xha_metadata_vdi.find_or_create ~__context ~sr in
19141914
database_vdis := database_vdi :: !database_vdis ;
19151915
(* Record the statefile UUIDs in the Pool.ha_statefile set *)
19161916
Db.Pool.set_ha_statefiles ~__context ~self:pool
@@ -1991,14 +1991,16 @@ let enable __context heartbeat_srs configuration =
19911991
(ExnHelper.string_of_exn e)
19921992
)
19931993
errors ;
1994-
if errors <> [] then (
1995-
(* Perform a disable since the pool HA state isn't consistent *)
1996-
error "Attempting to disable HA pool-wide" ;
1997-
Helpers.log_exn_continue
1998-
"Disabling HA after a failure joining all hosts to the liveset"
1999-
disable_internal __context ;
2000-
raise (snd (List.hd errors))
2001-
) ;
1994+
List.iter
1995+
(fun (_, exn) ->
1996+
(* Perform a disable since the pool HA state isn't consistent *)
1997+
error "Attempting to disable HA pool-wide" ;
1998+
Helpers.log_exn_continue
1999+
"Disabling HA after a failure joining all hosts to the liveset"
2000+
disable_internal __context ;
2001+
raise exn
2002+
)
2003+
errors ;
20022004
(* We have to set the HA enabled flag before forcing a database resynchronisation *)
20032005
Db.Pool.set_ha_enabled ~__context ~self:pool ~value:true ;
20042006
debug "HA enabled" ;
@@ -2036,13 +2038,16 @@ let enable __context heartbeat_srs configuration =
20362038
(ExnHelper.string_of_exn e)
20372039
)
20382040
errors ;
2039-
if errors <> [] then (
2040-
(* Perform a disable since the pool HA state isn't consistent *)
2041-
error "Attempting to disable HA pool-wide" ;
2042-
Helpers.log_exn_continue "Disabling HA after a failure during enable"
2043-
disable_internal __context ;
2044-
raise (snd (List.hd errors))
2045-
) ;
2041+
List.iter
2042+
(fun (_, exn) ->
2043+
(* Perform a disable since the pool HA state isn't consistent *)
2044+
error "Attempting to disable HA pool-wide" ;
2045+
Helpers.log_exn_continue
2046+
"Disabling HA after a failure during enable" disable_internal
2047+
__context ;
2048+
raise exn
2049+
)
2050+
errors ;
20462051
(* Update the allowed_operations on the HA volumes to prevent people thinking they can mess with them *)
20472052
List.iter
20482053
(fun vdi -> Xapi_vdi.update_allowed_operations ~__context ~self:vdi)

ocaml/xapi/xha_interface.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ let xml_element_has_name name element =
3232
(** Returns the first element with the specified name from
3333
the given element list. *)
3434
let first_xml_element_with_name elements name =
35-
try Some (List.find (xml_element_has_name name) elements)
36-
with Not_found -> None
35+
List.find_opt (xml_element_has_name name) elements
3736

3837
(** Parses an XML element of the form "<name>value</value>".
3938
Returns a (name, value) string pair, where the arguments

quality-gate.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -e
44

55
list-hd () {
6-
N=274
6+
N=269
77
LIST_HD=$(git grep -r --count 'List.hd' -- **/*.ml | cut -d ':' -f 2 | paste -sd+ - | bc)
88
if [ "$LIST_HD" -eq "$N" ]; then
99
echo "OK counted $LIST_HD List.hd usages"

0 commit comments

Comments
 (0)