Skip to content

Commit

Permalink
Merge pull request #4961 from psafont/private/paus/local-vm-rrd
Browse files Browse the repository at this point in the history
  • Loading branch information
psafont authored Apr 14, 2023
2 parents e9e0dc3 + d920df4 commit f354373
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 51 deletions.
2 changes: 1 addition & 1 deletion ocaml/xapi/rrdd_proxy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ let get_vm_rrd_forwarder (req : Http.Request.t) (s : Unix.file_descr) _ =
let localhost_uuid = Helpers.get_localhost_uuid () in
let vm_ref = Db.VM.get_by_uuid ~__context ~uuid:vm_uuid in
let owner = Db.VM.get_resident_on ~__context ~self:vm_ref in
let owner_uuid = Ref.string_of owner in
let owner_uuid = Db.Host.get_uuid ~__context ~self:owner in
let is_owner_localhost = owner_uuid = localhost_uuid in
if is_owner_localhost then
if is_master then
Expand Down
48 changes: 29 additions & 19 deletions ocaml/xcp-rrdd/bin/rrdd/rrdd_http_handler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,39 @@ let with_lock = Xapi_stdext_threads.Threadext.Mutex.execute
(* A handler for unarchiving RRDs. Only called on pool master. *)
let unarchive_rrd_handler (req : Http.Request.t) (s : Unix.file_descr) _ =
debug "unarchive_rrd_handler: start" ;
let query = req.Http.Request.query in
let uuid = List.assoc "uuid" query in
let path = Rrdd_libs.Constants.rrd_location ^ "/" ^ uuid in
let rrd = rrd_of_gzip path in
let header_content =
Http.http_200_ok ~version:"1.0" ~keep_alive:false ()
@ ["Access-Control-Allow-Origin: *"]
let unarchive () =
let ( let* ) = Option.bind in
let* uuid = List.assoc_opt "uuid" req.Http.Request.query in
let path = Rrdd_libs.Constants.rrd_location ^ "/" ^ uuid in
rrd_of_gzip path
in
Http_svr.headers s header_content ;
Rrd_unix.to_fd rrd s
match unarchive () with
| None ->
Http_svr.headers s (Http.http_404_missing ())
| Some rrd ->
let header_content =
Http.http_200_ok ~version:"1.0" ~keep_alive:false ()
@ ["Access-Control-Allow-Origin: *"]
in
Http_svr.headers s header_content ;
Rrd_unix.to_fd rrd s

(* A handler for putting a VM's RRD data into the Http response. The rrdd
assumes that it has RRD for the vm_uuid, since xapi confirmed this with rrdd
over XMLRPC before forwarding the HTTP request --- see rrdd_proxy in xapi. *)
(* A handler for putting a VM's RRD data into the Http response. *)
let get_vm_rrd_handler (req : Http.Request.t) (s : Unix.file_descr) _ =
debug "get_vm_rrd_handler: start" ;
let query = req.Http.Request.query in
let vm_uuid = List.assoc "uuid" query in
let rrd =
with_lock mutex (fun () -> Rrd.copy_rrd (Hashtbl.find vm_rrds vm_uuid).rrd)
let get () =
let ( let* ) = Option.bind in
let* vm_uuid = List.assoc_opt "uuid" req.Http.Request.query in
let* old_rrd =
with_lock mutex (fun () -> Hashtbl.find_opt vm_rrds vm_uuid)
in
Some (Rrd.copy_rrd old_rrd.rrd)
in
Http_svr.headers s (Http.http_200_ok ~version:"1.0" ~keep_alive:false ()) ;
Rrd_unix.to_fd rrd s
match get () with
| None ->
Http_svr.headers s (Http.http_404_missing ())
| Some rrd ->
Http_svr.headers s (Http.http_200_ok ~version:"1.0" ~keep_alive:false ()) ;
Rrd_unix.to_fd rrd s

(* A handler for putting the host's RRD data into the Http response. *)
let get_host_rrd_handler (req : Http.Request.t) (s : Unix.file_descr) _ =
Expand Down
50 changes: 24 additions & 26 deletions ocaml/xcp-rrdd/bin/rrdd/rrdd_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ let push_sr_rrd (sr_uuid : string) (path : string) : unit =
else
path
in
let rrd = rrd_of_gzip path in
debug "Pushing RRD for SR uuid=%s locally" sr_uuid ;
with_lock mutex (fun _ ->
Hashtbl.replace sr_rrds sr_uuid {rrd; dss= []; domid= 0}
)
match rrd_of_gzip path with
| Some rrd ->
debug "Pushing RRD for SR uuid=%s locally" sr_uuid ;
with_lock mutex (fun _ ->
Hashtbl.replace sr_rrds sr_uuid {rrd; dss= []; domid= 0}
)
| None ->
()
with _ -> ()

let has_vm_rrd (vm_uuid : string) =
Expand Down Expand Up @@ -162,12 +165,14 @@ let backup_rrds (remote_address : string option) () : unit =
)
done

(* Load an RRD from the local filesystem. Will return an RRD or throw an
exception. *)
let load_rrd_from_local_filesystem uuid =
let get_rrd ~uuid =
debug "Loading RRD from local filesystem for object uuid=%s" uuid ;
let path = Rrdd_libs.Constants.rrd_location ^ "/" ^ uuid in
rrd_of_gzip path
let path = Filename.concat Rrdd_libs.Constants.rrd_location uuid in
match rrd_of_gzip path with
| Some rrd ->
rrd
| None ->
failwith "File not present in the filesystem"

module Deprecated = struct
(* DEPRECATED *)
Expand Down Expand Up @@ -215,7 +220,7 @@ module Deprecated = struct
try
let rrd =
try
let rrd = load_rrd_from_local_filesystem uuid in
let rrd = get_rrd ~uuid in
debug
"RRD loaded from local filesystem for object uuid=%s (deprecation \
warning: timescale %d is ignored)."
Expand Down Expand Up @@ -251,22 +256,16 @@ module Deprecated = struct
with _ -> ()
end

let get_rrd ~vm_uuid =
let path = Filename.concat Rrdd_libs.Constants.rrd_location vm_uuid in
rrd_of_gzip path

let push_rrd_local vm_uuid domid : unit =
let push_rrd_local uuid domid : unit =
try
let rrd = get_rrd ~vm_uuid in
debug "Pushing RRD for VM uuid=%s locally" vm_uuid ;
with_lock mutex (fun _ ->
Hashtbl.replace vm_rrds vm_uuid {rrd; dss= []; domid}
)
let rrd = get_rrd ~uuid in
debug "Pushing RRD for VM uuid=%s locally" uuid ;
with_lock mutex (fun _ -> Hashtbl.replace vm_rrds uuid {rrd; dss= []; domid})
with _ -> ()

let push_rrd_remote vm_uuid member_address : unit =
let push_rrd_remote uuid member_address : unit =
try
let rrd = get_rrd ~vm_uuid in
let rrd = get_rrd ~uuid in
let transport =
let open Xmlrpc_client in
SSL
Expand All @@ -275,10 +274,9 @@ let push_rrd_remote vm_uuid member_address : unit =
, !Rrdd_shared.https_port
)
in
debug "Pushing RRD for VM uuid=%s to another pool member with %s" vm_uuid
debug "Pushing RRD for VM uuid=%s to another pool member with %s" uuid
(Xmlrpc_client.string_of_transport transport) ;
send_rrd ~transport ~to_archive:false ~uuid:vm_uuid ~rrd:(Rrd.copy_rrd rrd)
()
send_rrd ~transport ~to_archive:false ~uuid ~rrd:(Rrd.copy_rrd rrd) ()
with _ -> ()

(** Remove an RRD from the local filesystem, if it exists. *)
Expand Down
12 changes: 7 additions & 5 deletions ocaml/xcp-rrdd/bin/rrdd/rrdd_shared.ml
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ let rrd_of_gzip path =
with _ -> false
in
if gz_exists then
Xapi_stdext_unix.Unixext.with_file gz_path [Unix.O_RDONLY] 0o0 (fun fd ->
Gzip.Default.decompress_passive fd rrd_of_fd
)
else (* If this fails, let the exception propagate *)
Xapi_stdext_unix.Unixext.with_file path [Unix.O_RDONLY] 0 rrd_of_fd
Some
(Xapi_stdext_unix.Unixext.with_file gz_path [Unix.O_RDONLY] 0o0 (fun fd ->
Gzip.Default.decompress_passive fd rrd_of_fd
)
)
else
None

(* Send rrds to a remote host. If the host is on another pool, you must pass the
session_id parameter, and optionally the __context. *)
Expand Down

0 comments on commit f354373

Please sign in to comment.