From 03a35880ed28904bbe35f401871d2ca0e01e417f Mon Sep 17 00:00:00 2001 From: Pau Ruiz Safont Date: Thu, 16 Nov 2023 14:21:41 +0000 Subject: [PATCH] lib: remove Failure "hd" exceptions 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 --- ChangeLog | 3 +++ lib/rrd.ml | 6 +++++- lib/rrd_updates.ml | 20 +++++++++++--------- lib_test/unit_tests.ml | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5e3685c..2f23638 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +## v1.11.0 (16-Nov-2023) +* lib: remove Failure "hd" exceptions + ## v1.10.0 (02-Aug-2023) * rrd_updates: use yojson instead of ad-hoc json serialization diff --git a/lib/rrd.ml b/lib/rrd.ml index 4ffce3f..642a875 100644 --- a/lib/rrd.ml +++ b/lib/rrd.ml @@ -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 *) diff --git a/lib/rrd_updates.ml b/lib/rrd_updates.ml index daae0c5..e1e3a98 100644 --- a/lib/rrd_updates.ml +++ b/lib/rrd_updates.ml @@ -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. *) @@ -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 diff --git a/lib_test/unit_tests.ml b/lib_test/unit_tests.ml index 1ad4f5f..aa15b94 100644 --- a/lib_test/unit_tests.ml +++ b/lib_test/unit_tests.ml @@ -313,7 +313,7 @@ let suite_create_multi = ) in let test_no_rrds () = - Alcotest.check_raises "should raise error" (Failure "hd") (fun () -> + Alcotest.check_raises "should raise error" (No_RRA_Available) (fun () -> let _ = RU.create_multi [] 0L 1L None in () )