Skip to content

Commit caff014

Browse files
authored
Merge pull request #5751 from last-genius/private/asultanov/opt-refactoring
IH-633: Transition away from exception-raising Hashtbl.find and Unix.getenv
2 parents af4860b + 76f232d commit caff014

File tree

85 files changed

+914
-771
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+914
-771
lines changed

configure.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ let () =
8484
in
8585
List.iter print_endline lines ;
8686
(* Expand @LIBEXEC@ in udev rules *)
87-
try
88-
let xenopsd_libexecdir = Hashtbl.find config "XENOPSD_LIBEXECDIR" in
89-
expand "@LIBEXEC@" xenopsd_libexecdir "ocaml/xenopsd/scripts/vif.in"
90-
"ocaml/xenopsd/scripts/vif" ;
91-
expand "@LIBEXEC@" xenopsd_libexecdir
92-
"ocaml/xenopsd/scripts/xen-backend.rules.in"
93-
"ocaml/xenopsd/scripts/xen-backend.rules"
94-
with Not_found -> failwith "xenopsd_libexecdir not set"
87+
match Hashtbl.find_opt config "XENOPSD_LIBEXECDIR" with
88+
| Some xenopsd_libexecdir ->
89+
expand "@LIBEXEC@" xenopsd_libexecdir "ocaml/xenopsd/scripts/vif.in"
90+
"ocaml/xenopsd/scripts/vif" ;
91+
expand "@LIBEXEC@" xenopsd_libexecdir
92+
"ocaml/xenopsd/scripts/xen-backend.rules.in"
93+
"ocaml/xenopsd/scripts/xen-backend.rules"
94+
| None ->
95+
failwith "xenopsd_libexecdir not set"

ocaml/database/db_backend.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,5 @@ let is_session_registered session =
104104

105105
let get_registered_database session =
106106
with_lock db_registration_mutex (fun () ->
107-
if Hashtbl.mem foreign_databases session then
108-
Some (Hashtbl.find foreign_databases session)
109-
else
110-
None
107+
Hashtbl.find_opt foreign_databases session
111108
)

ocaml/database/db_conn_store.ml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ let read_db_connections () = !db_connections
4141
let with_db_conn_lock db_conn f =
4242
let db_conn_m =
4343
with_lock db_conn_locks_m (fun () ->
44-
try Hashtbl.find db_conn_locks db_conn
45-
with _ ->
46-
(* If we don't have a lock already for this connection then go make one dynamically and use that from then on *)
47-
let new_dbconn_mutex = Mutex.create () in
48-
Hashtbl.replace db_conn_locks db_conn new_dbconn_mutex ;
49-
new_dbconn_mutex
44+
match Hashtbl.find_opt db_conn_locks db_conn with
45+
| Some x ->
46+
x
47+
| None ->
48+
(* If we don't have a lock already for this connection then go make one dynamically and use that from then on *)
49+
let new_dbconn_mutex = Mutex.create () in
50+
Hashtbl.replace db_conn_locks db_conn new_dbconn_mutex ;
51+
new_dbconn_mutex
5052
)
5153
in
5254
with_lock db_conn_m (fun () -> f ())

ocaml/database/stats.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ let sample (name : string) (x : float) : unit =
7777
let x' = log x in
7878
with_lock timings_m (fun () ->
7979
let p =
80-
if Hashtbl.mem timings name then
81-
Hashtbl.find timings name
82-
else
83-
Normal_population.empty
80+
Option.value
81+
(Hashtbl.find_opt timings name)
82+
~default:Normal_population.empty
8483
in
8584
let p' = Normal_population.sample p x' in
8685
Hashtbl.replace timings name p'

ocaml/idl/dtd_backend.ml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,13 @@ let rec strings_of_dtd_element known_els = function
110110

111111
let element known_els name children atts =
112112
let existing_children =
113-
if Hashtbl.mem known_els name then
114-
match Hashtbl.find known_els name with
115-
| Element (_, c, att) ->
116-
(c, att)
117-
| _ ->
118-
assert false
119-
else
120-
([], [])
113+
match Hashtbl.find_opt known_els name with
114+
| Some (Element (_, c, att)) ->
115+
(c, att)
116+
| None ->
117+
([], [])
118+
| _ ->
119+
assert false
121120
in
122121
let open Xapi_stdext_std.Listext in
123122
let el =

ocaml/libs/http-lib/http_svr.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,11 @@ exception Socket_not_found
660660
(* Stop an HTTP server running on a socket *)
661661
let stop (socket, _name) =
662662
let server =
663-
try Hashtbl.find socket_table socket
664-
with Not_found -> raise Socket_not_found
663+
match Hashtbl.find_opt socket_table socket with
664+
| Some x ->
665+
x
666+
| None ->
667+
raise Socket_not_found
665668
in
666669
Hashtbl.remove socket_table socket ;
667670
server.Server_io.shutdown ()

ocaml/libs/http-lib/mime.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let default_mime = "text/plain"
4242

4343
(** Map a file extension to a MIME type *)
4444
let mime_of_ext mime ext =
45-
try Hashtbl.find mime (lowercase ext) with Not_found -> default_mime
45+
Option.value (Hashtbl.find_opt mime (lowercase ext)) ~default:default_mime
4646

4747
(** Figure out a mime type from a full filename *)
4848
let mime_of_file_name mime fname =

ocaml/libs/resources/table.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct
4141
Hashtbl.remove t k
4242
)
4343

44-
let find (t, m) k = with_lock m (fun () -> Hashtbl.find t k)
44+
let find (t, m) k = with_lock m (fun () -> Hashtbl.find_opt t k)
4545

4646
let with_find_moved_exn (t, m) k =
4747
let v =

ocaml/libs/stunnel/stunnel.ml

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,38 @@ let stunnel_logger = ref ignore
3535
let timeoutidle = ref None
3636

3737
let init_stunnel_path () =
38-
try cached_stunnel_path := Some (Unix.getenv "XE_STUNNEL")
39-
with Not_found ->
40-
let choices =
41-
[
42-
"/opt/xensource/libexec/stunnel/stunnel"
43-
; "/usr/sbin/stunnel4"
44-
; "/usr/sbin/stunnel"
45-
; "/usr/bin/stunnel4"
46-
; "/usr/bin/stunnel"
47-
]
48-
in
49-
let rec choose l =
50-
match l with
51-
| [] ->
52-
raise Stunnel_binary_missing
53-
| p :: ps -> (
54-
try Unix.access p [Unix.X_OK] ; p with _ -> choose ps
38+
cached_stunnel_path :=
39+
Some
40+
( match Sys.getenv_opt "XE_STUNNEL" with
41+
| Some x ->
42+
x
43+
| None ->
44+
let choices =
45+
[
46+
"/opt/xensource/libexec/stunnel/stunnel"
47+
; "/usr/sbin/stunnel4"
48+
; "/usr/sbin/stunnel"
49+
; "/usr/bin/stunnel4"
50+
; "/usr/bin/stunnel"
51+
]
52+
in
53+
54+
let choose l =
55+
match
56+
List.find_opt
57+
(fun el ->
58+
try Unix.access el [Unix.X_OK] ; true with _ -> false
59+
)
60+
l
61+
with
62+
| Some p ->
63+
p
64+
| None ->
65+
raise Stunnel_binary_missing
66+
in
67+
let path = choose choices in
68+
path
5569
)
56-
in
57-
let path = choose choices in
58-
cached_stunnel_path := Some path
5970

6071
let stunnel_path () =
6172
if Option.is_none !cached_stunnel_path then
@@ -150,7 +161,8 @@ let debug_conf_of_bool verbose : string =
150161
if verbose then "debug=authpriv.7" else "debug=authpriv.5"
151162
152163
let debug_conf_of_env () : string =
153-
(try Unix.getenv "debug_stunnel" with _ -> "") |> String.lowercase_ascii
164+
Option.value (Sys.getenv_opt "debug_stunnel") ~default:""
165+
|> String.lowercase_ascii
154166
|> fun x -> List.mem x ["yes"; "true"; "1"] |> debug_conf_of_bool
155167
156168
let config_file ?(accept = None) config host port =

ocaml/libs/stunnel/stunnel_cache.ml

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@ let unlocked_gc () =
7474
( if debug_enabled then
7575
let now = Unix.gettimeofday () in
7676
let string_of_id id =
77-
let stunnel = Tbl.find !stunnels id in
78-
Printf.sprintf "(id %s / idle %.2f age %.2f)" (id_of_stunnel stunnel)
79-
(now -. Hashtbl.find !times id)
80-
(now -. stunnel.Stunnel.connected_time)
77+
match (Tbl.find !stunnels id, Hashtbl.find_opt !times id) with
78+
| Some stunnel, Some stunnel_id ->
79+
Printf.sprintf "(id %s / idle %.2f age %.2f)"
80+
(id_of_stunnel stunnel) (now -. stunnel_id)
81+
(now -. stunnel.Stunnel.connected_time)
82+
| _ ->
83+
Printf.sprintf "%s: found no entry for id=%d" __FUNCTION__ id
8184
in
8285
let string_of_endpoint ep = Printf.sprintf "%s:%d" ep.host ep.port in
8386
let string_of_index ep xs =
@@ -134,20 +137,24 @@ let unlocked_gc () =
134137
let oldest_ids = List.map fst oldest in
135138
List.iter
136139
(fun x ->
137-
let stunnel = Tbl.find !stunnels x in
138-
debug
139-
"Expiring stunnel id %s since we have too many cached tunnels (limit \
140-
is %d)"
141-
(id_of_stunnel stunnel) max_stunnel
140+
match Tbl.find !stunnels x with
141+
| Some stunnel ->
142+
debug
143+
"Expiring stunnel id %s since we have too many cached tunnels \
144+
(limit is %d)"
145+
(id_of_stunnel stunnel) max_stunnel
146+
| None ->
147+
debug "%s: Couldn't find an expiring stunnel (id=%d) in the table"
148+
__FUNCTION__ x
142149
)
143150
oldest_ids ;
144151
to_gc := !to_gc @ oldest_ids
145152
) ;
146153
(* Disconnect all stunnels we wish to GC *)
147154
List.iter
148155
(fun id ->
149-
let s = Tbl.find !stunnels id in
150-
Stunnel.disconnect s
156+
(* Only remove stunnel if we find it in the table *)
157+
Option.iter (fun s -> Stunnel.disconnect s) (Tbl.find !stunnels id)
151158
)
152159
!to_gc ;
153160
(* Remove all reference to them from our cache hashtables *)
@@ -187,12 +194,7 @@ let add (x : Stunnel.t) =
187194
; verified= x.Stunnel.verified
188195
}
189196
in
190-
let existing =
191-
if Hashtbl.mem !index ep then
192-
Hashtbl.find !index ep
193-
else
194-
[]
195-
in
197+
let existing = Option.value (Hashtbl.find_opt !index ep) ~default:[] in
196198
Hashtbl.replace !index ep (idx :: existing) ;
197199
debug "Adding stunnel id %s (idle %.2f) to the cache" (id_of_stunnel x) 0. ;
198200
unlocked_gc ()
@@ -206,23 +208,33 @@ let with_remove ~host ~port verified f =
206208
let get_id () =
207209
with_lock m (fun () ->
208210
unlocked_gc () ;
209-
let ids = Hashtbl.find !index ep in
210-
let table = List.map (fun id -> (id, Hashtbl.find !times id)) ids in
211+
let ( let* ) = Option.bind in
212+
let* ids = Hashtbl.find_opt !index ep in
213+
let table =
214+
List.filter_map
215+
(fun id ->
216+
Option.map (fun time -> (id, time)) (Hashtbl.find_opt !times id)
217+
)
218+
ids
219+
in
211220
let sorted = List.sort (fun a b -> compare (snd a) (snd b)) table in
212221
match sorted with
213222
| (id, time) :: _ ->
214-
let stunnel = Tbl.find !stunnels id in
215-
debug "Removing stunnel id %s (idle %.2f) from the cache"
216-
(id_of_stunnel stunnel)
217-
(Unix.gettimeofday () -. time) ;
223+
Option.iter
224+
(fun stunnel ->
225+
debug "Removing stunnel id %s (idle %.2f) from the cache"
226+
(id_of_stunnel stunnel)
227+
(Unix.gettimeofday () -. time)
228+
)
229+
(Tbl.find !stunnels id) ;
218230
Hashtbl.remove !times id ;
219231
Hashtbl.replace !index ep (List.filter (fun x -> x <> id) ids) ;
220-
id
232+
Some id
221233
| _ ->
222-
raise Not_found
234+
None
223235
)
224236
in
225-
let id_opt = try Some (get_id ()) with Not_found -> None in
237+
let id_opt = get_id () in
226238
id_opt
227239
|> Option.map @@ fun id ->
228240
(* cannot call while holding above mutex or we deadlock *)

0 commit comments

Comments
 (0)