Skip to content

Commit 4d275d6

Browse files
committed
CP-52708: Avoid making Unix read/write calls for internal API calls: forward the API call directly like we do with the CLI for calls to the coordinator when we are the coordinator
Signed-off-by: Edwin Török <[email protected]>
1 parent 4d3e411 commit 4d275d6

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

ocaml/xapi/helpers.ml

+21-12
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,11 @@ module TraceHelper = struct
402402
Tracing_propagator.Propagator.Http.inject_into trace_context
403403
end
404404

405+
(** Once the server functor has been instantiated, xapi sets this reference to the appropriate
406+
"fake_rpc" (loopback non-HTTP) rpc function.
407+
This way, internally the coordinator can short-circuit API calls without having to go over the network. *)
408+
let rpc_fun : (Http.Request.t -> Rpc.call -> Rpc.response) option ref = ref None
409+
405410
(* Note that both this and `make_timeboxed_rpc` are almost always
406411
* partially applied, returning a function of type 'Rpc.request -> Rpc.response'.
407412
* The body is therefore not evaluated until the RPC call is actually being
@@ -418,18 +423,22 @@ let make_rpc ~__context rpc : Rpc.response =
418423
in
419424
let http = xmlrpc ~subtask_of ~version:"1.1" path in
420425
let http = TraceHelper.inject_span_into_req tracing http in
421-
let transport =
422-
if Pool_role.is_master () then
423-
Unix Xapi_globs.unix_domain_socket
424-
else
425-
SSL
426-
( SSL.make ~use_stunnel_cache:true ~verify_cert:(Stunnel_client.pool ())
427-
()
428-
, Pool_role.get_master_address ()
429-
, !Constants.https_port
430-
)
431-
in
432-
dorpc ~srcstr:"xapi" ~dststr:"xapi" ~transport ~http rpc
426+
match !rpc_fun with
427+
| Some rpcfun when Pool_role.is_master () ->
428+
rpcfun http rpc
429+
| _ ->
430+
let transport =
431+
if Pool_role.is_master () then
432+
Unix Xapi_globs.unix_domain_socket
433+
else
434+
SSL
435+
( SSL.make ~use_stunnel_cache:true
436+
~verify_cert:(Stunnel_client.pool ()) ()
437+
, Pool_role.get_master_address ()
438+
, !Constants.https_port
439+
)
440+
in
441+
dorpc ~srcstr:"xapi" ~dststr:"xapi" ~transport ~http rpc
433442

434443
let make_timeboxed_rpc ~__context timeout rpc : Rpc.response =
435444
let subtask_of = Ref.string_of (Context.get_task_id __context) in

ocaml/xapi/xapi.ml

+3
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,14 @@ let random_setup () =
159159
finally (fun () -> really_input chan s 0 n) (fun () -> close_in chan) ;
160160
Random.full_init (Array.init n (fun i -> Char.code (Bytes.get s i)))
161161

162+
let fake_rpc2 req rpc = Api_server.Server.dispatch_call req None rpc
163+
162164
let register_callback_fns () =
163165
let fake_rpc req sock xml : Rpc.response =
164166
Api_server.callback1 false req sock xml
165167
in
166168
Xapi_cli.rpc_fun := Some fake_rpc ;
169+
Helpers.rpc_fun := Some fake_rpc2 ;
167170
Message_forwarding.register_callback_fns ()
168171

169172
let noevents = ref false

0 commit comments

Comments
 (0)