Skip to content

Commit dfaec8f

Browse files
committed
xapi: Add new message-destroy-all API call
We've seen issues where a client will try to dismiss all their alerts and fail due to the message-destroy call hitting the maximum XAPI request size at ~5000 messages. A new message-destroy-all API avoids this issue while saving the bandwidth required to send a ref for each message. Users of the new API call can choose to dismiss messages that arrived before or after a certain date, or only those with a given priority. Signed-off-by: Christian Pardillo Laursen <christian.pardillolaursen@citrix.com>
1 parent bba5d0e commit dfaec8f

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

ocaml/idl/datamodel.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8915,6 +8915,24 @@ module Message = struct
89158915
~params:[(Set (Ref _message), "messages", "Messages to destroy")]
89168916
~allowed_roles:_R_POOL_OP ()
89178917

8918+
let destroy_all =
8919+
call ~name:"destroy_all" ~lifecycle:[]
8920+
~params:
8921+
[
8922+
( Option DateTime
8923+
, "before"
8924+
, "Cutoff time for destroyed messages - any messages dated after \
8925+
this will not be destroyed"
8926+
)
8927+
; ( Option DateTime
8928+
, "after"
8929+
, "Cutoff time for destroyed messages - any messages dated before \
8930+
this timestamp will not be destroyed"
8931+
)
8932+
; (Option Int, "priority", "Priority of messages to destroy")
8933+
]
8934+
~allowed_roles:_R_POOL_OP ()
8935+
89188936
let get_all =
89198937
call ~name:"get_all"
89208938
~lifecycle:[(Published, rel_orlando, "")]
@@ -9002,6 +9020,7 @@ module Message = struct
90029020
create
90039021
; destroy
90049022
; destroy_many
9023+
; destroy_all
90059024
; get
90069025
; get_all
90079026
; get_since

ocaml/xapi-cli-server/cli_frontend.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ let rec cmdtable_data : (string * cmd_spec) list =
128128
; flags= []
129129
}
130130
)
131+
; ( "message-destroy-all"
132+
, {
133+
reqd= []
134+
; optn= ["before"; "after"; "priority"]
135+
; help= "Destroy all existing messages matching the given conditions."
136+
; implementation= No_fd Cli_operations.message_destroy_all
137+
; flags= []
138+
}
139+
)
131140
; ( "pool-enable-binary-storage"
132141
, {
133142
reqd= []

ocaml/xapi-cli-server/cli_operations.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,25 @@ let message_destroy (_ : printer) rpc session_id params =
14181418
in
14191419
Client.Message.destroy_many ~rpc ~session_id ~messages
14201420

1421+
let message_destroy_all (_ : printer) rpc session_id params =
1422+
let fail msg = raise (Cli_util.Cli_failure msg) in
1423+
let before_str = List.assoc_opt "before" params in
1424+
let after_str = List.assoc_opt "after" params in
1425+
let priority_str = List.assoc_opt "priority" params in
1426+
let before =
1427+
try Option.map Date.of_iso8601 before_str
1428+
with _ -> fail "invalid timestamp format for 'before' (expected RFC3339)"
1429+
in
1430+
let after =
1431+
try Option.map Date.of_iso8601 after_str
1432+
with _ -> fail "Invalid timestamp format for 'after' (expected RFC3339)"
1433+
in
1434+
let priority =
1435+
try Option.map Int64.of_string priority_str
1436+
with _ -> fail "Invalid priority format (expected integer)"
1437+
in
1438+
Client.Message.destroy_all ~rpc ~session_id ~before ~after ~priority
1439+
14211440
(* Pool operations *)
14221441

14231442
let get_pool_with_default rpc session_id params key =

ocaml/xapi/xapi_message.ml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,41 @@ let get_record ~__context ~self =
730730

731731
let get_all_records ~__context = get_real message_dir (fun _ -> true) 0.0
732732

733+
let destroy_all ~__context ~before ~after ~priority =
734+
let filter_before_timestamp =
735+
match before with
736+
| None ->
737+
fun _ -> true
738+
| Some timestamp ->
739+
Date.is_later ~than:timestamp
740+
in
741+
let filter_after_timestamp =
742+
match after with
743+
| None ->
744+
fun _ -> true
745+
| Some timestamp ->
746+
Date.is_earlier ~than:timestamp
747+
in
748+
let filter_timestamp ts =
749+
filter_before_timestamp ts && filter_after_timestamp ts
750+
in
751+
let priority_filter =
752+
match priority with
753+
| None ->
754+
fun _ -> true
755+
| Some p ->
756+
fun prio -> prio == p
757+
in
758+
let message_filter msg =
759+
filter_timestamp msg.API.message_timestamp
760+
&& priority_filter msg.API.message_priority
761+
in
762+
let messages =
763+
get_real_inner message_dir message_filter (fun _ -> true)
764+
|> List.map (fun (_, msg, _) -> msg)
765+
in
766+
destroy_many ~__context ~messages
767+
733768
let get_all_records_where ~__context ~expr =
734769
let open Xapi_database in
735770
let expr = Db_filter.expr_of_string expr in

0 commit comments

Comments
 (0)