Skip to content

Commit

Permalink
lib: remove Failure "hd" exceptions
Browse files Browse the repository at this point in the history
Instead raise the semantically correct No_RRA_available.

There's one last List.hd in the code, but it's correctly guarded so it
cannot ever be used with an empty list.

Signed-off-by: Pau Ruiz Safont <[email protected]>
  • Loading branch information
psafont committed Nov 16, 2023
1 parent c1e97c5 commit e85c2c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 5 additions & 1 deletion lib/rrd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,11 @@ let query_named_ds rrd now ds_name cf =
raise (Invalid_data_source ds_name)
else
let rras = find_best_rras rrd 0 (Some cf) (Int64.of_float now) in
Fring.peek (List.hd rras).rra_data.(n) 0
match rras with
| [] ->
raise No_RRA_Available
| rra :: _ ->
Fring.peek rra.rra_data.(n) 0

(******************************************************************************)
(* Marshalling/Unmarshalling functions *)
Expand Down
20 changes: 11 additions & 9 deletions lib/rrd_updates.ml
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,19 @@ let json_of_t t =
'xport' format. *)

let create_multi prefixandrrds start interval cfopt =
let first_rrd = snd (List.hd prefixandrrds) in
let timestep, last_updated =
match prefixandrrds with
| (_, r) :: _ ->
(r.timestep, r.last_updated)
| [] ->
raise No_RRA_Available
in

let pdp_interval = Int64.to_int (Int64.div interval first_rrd.timestep) in
let pdp_interval = Int64.to_int (Int64.div interval timestep) in

(* Sanity - make sure the RRDs are homogeneous *)
let prefixandrrds =
List.filter
(fun (_prefix, rrd) -> rrd.timestep = first_rrd.timestep)
prefixandrrds
List.filter (fun (_prefix, rrd) -> rrd.timestep = timestep) prefixandrrds
in

(* Treat -ve start values as relative to the latest update. *)
Expand Down Expand Up @@ -282,12 +286,10 @@ let create_multi prefixandrrds start interval cfopt =
let rras = List.flatten rras in

(* The following timestep is that of the archive *)
let rra_timestep =
Int64.mul first_rrd.timestep (Int64.of_int first_rra.rra_pdp_cnt)
in
let rra_timestep = Int64.mul timestep (Int64.of_int first_rra.rra_pdp_cnt) in

(* Get the last and first times of the CDPs to be returned *)
let last_cdp_time, _age = get_times first_rrd.last_updated rra_timestep in
let last_cdp_time, _age = get_times last_updated rra_timestep in
let first_cdp_time_minus_one, _age =
get_times (Int64.to_float start) rra_timestep
in
Expand Down

0 comments on commit e85c2c5

Please sign in to comment.