Skip to content

Commit 62ff51f

Browse files
author
Gabriel Buica
committed
CP-53843: Move session handling logic out of sm_exec
Signed-off-by: Gabriel Buica <danutgabriel.buica@citrix.com>
1 parent 6e1c8a6 commit 62ff51f

3 files changed

Lines changed: 84 additions & 78 deletions

File tree

ocaml/xapi/sm_exec.ml

Lines changed: 3 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ module E = Debug.Make (struct let name = "mscgen" end)
3030

3131
let cmd_name driver = sprintf "%s/%sSR" !Xapi_globs.sm_dir driver
3232

33-
let sm_username = "__sm__backend"
34-
3533
let with_dbg ~name ~dbg f =
3634
Debug_info.with_dbg ~module_name:"Sm_exec" ~name ~dbg f
3735

@@ -317,80 +315,6 @@ let methodResponse xml =
317315
| xml ->
318316
XMLRPC.From.methodResponse xml
319317

320-
let reusable_sessions : (string option, API.ref_session) Hashtbl.t =
321-
Hashtbl.create 5
322-
323-
let sm_sessions_m = Mutex.create ()
324-
325-
let with_sm_sessions_lock f =
326-
Xapi_stdext_threads.Threadext.Mutex.execute sm_sessions_m f
327-
328-
let is_valid_session ~__context session_id =
329-
try
330-
(* Call an API function to check the session is still valid *)
331-
let rpc = Helpers.make_rpc ~__context in
332-
ignore (Client.Client.Pool.get_all ~rpc ~session_id) ;
333-
true
334-
with Api_errors.Server_error (err, _) ->
335-
debug "%s: Invalid session: %s" __FUNCTION__ err ;
336-
false
337-
338-
let session_access ~__context session sr =
339-
(* Give this session access to this particular SR *)
340-
Option.iter
341-
(fun sr ->
342-
Db.Session.add_to_other_config ~__context ~self:session
343-
~key:Xapi_globs._sm_session ~value:(Ref.string_of sr)
344-
)
345-
sr
346-
347-
let create_session ~__context sr =
348-
let host = !Xapi_globs.localhost_ref in
349-
let session =
350-
Xapi_session.login_no_password ~__context ~uname:None ~host ~pool:false
351-
~is_local_superuser:true ~subject:Ref.null ~auth_user_sid:""
352-
~auth_user_name:sm_username ~rbac_permissions:[]
353-
in
354-
session_access ~__context session sr ;
355-
session
356-
357-
let rec get_session ~__context sr =
358-
let sr_key = Option.map Ref.string_of sr in
359-
let session = Hashtbl.find_opt reusable_sessions sr_key in
360-
match session with
361-
| Some session ->
362-
if is_valid_session ~__context session then
363-
session
364-
else (
365-
with_sm_sessions_lock (fun () -> Hashtbl.remove reusable_sessions sr_key) ;
366-
(get_session [@tailcall]) ~__context sr
367-
)
368-
| None ->
369-
let new_session = create_session ~__context sr in
370-
with_sm_sessions_lock (fun () ->
371-
match Hashtbl.find_opt reusable_sessions sr_key with
372-
| None ->
373-
Hashtbl.add reusable_sessions sr_key new_session ;
374-
new_session
375-
| Some session ->
376-
Xapi_session.destroy_db_session ~__context ~self:new_session ;
377-
session
378-
)
379-
380-
let with_session ~traceparent sr f =
381-
Server_helpers.exec_with_new_task "sm_exec"
382-
~origin:(Internal_Traced traceparent) (fun __context ->
383-
if !Xapi_globs.reuse_pool_sessions then
384-
let session_id = get_session ~__context sr in
385-
f session_id
386-
else
387-
let session_id = create_session ~__context sr in
388-
let destroy_session () =
389-
Xapi_session.destroy_db_session ~__context ~self:session_id
390-
in
391-
finally (fun () -> f session_id) destroy_session
392-
)
393-
394318
(****************************************************************************************)
395319
(* Functions that actually execute the python backends *)
396320

@@ -515,8 +439,9 @@ let exec_xmlrpc ~dbg ?context:_ ?(needs_session = true) (driver : string)
515439
)
516440
in
517441
if needs_session then
518-
with_session ~traceparent:(Debug_info.span_of di) call.sr_ref
519-
(fun session_id -> do_call {call with session_ref= Some session_id}
442+
Xapi_session.SM.with_session ~traceparent:(Debug_info.span_of di)
443+
call.sr_ref (fun session_id ->
444+
do_call {call with session_ref= Some session_id}
520445
)
521446
else
522447
do_call call

ocaml/xapi/xapi_session.ml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,3 +1589,77 @@ let create_from_db_file ~__context ~filename =
15891589
in
15901590
let db_ref = Some (Xapi_database.Db_ref.in_memory (Atomic.make db)) in
15911591
create_readonly_session ~__context ~uname:"db-from-file" ~db_ref
1592+
1593+
module SM = struct
1594+
let reusable_sessions : (string option, API.ref_session) Hashtbl.t =
1595+
Hashtbl.create 5
1596+
1597+
let sm_sessions_m = Mutex.create ()
1598+
1599+
let with_sm_sessions_lock f =
1600+
Xapi_stdext_threads.Threadext.Mutex.execute sm_sessions_m f
1601+
1602+
let finally = Xapi_stdext_pervasives.Pervasiveext.finally
1603+
1604+
let sm_username = "__sm__backend"
1605+
1606+
let is_valid_session ~__context session_id =
1607+
try
1608+
(* Call an API function to check the session is still valid *)
1609+
let rpc = Helpers.make_rpc ~__context in
1610+
ignore (Client.Pool.get_all ~rpc ~session_id) ;
1611+
true
1612+
with Api_errors.Server_error (err, _) ->
1613+
debug "%s: Invalid session: %s" __FUNCTION__ err ;
1614+
false
1615+
1616+
let session_access ~__context session sr =
1617+
(* Give this session access to this particular SR *)
1618+
Option.iter
1619+
(fun sr ->
1620+
Db.Session.add_to_other_config ~__context ~self:session
1621+
~key:Xapi_globs._sm_session ~value:(Ref.string_of sr)
1622+
)
1623+
sr
1624+
1625+
let create_session ~__context sr =
1626+
let host = !Xapi_globs.localhost_ref in
1627+
let session =
1628+
login_no_password ~__context ~uname:None ~host ~pool:false
1629+
~is_local_superuser:true ~subject:Ref.null ~auth_user_sid:""
1630+
~auth_user_name:sm_username ~rbac_permissions:[]
1631+
in
1632+
session_access ~__context session sr ;
1633+
session
1634+
1635+
let get_session ~__context sr =
1636+
let sr_key = Option.map Ref.string_of sr in
1637+
with_sm_sessions_lock (fun () ->
1638+
match Hashtbl.find_opt reusable_sessions sr_key with
1639+
| Some session when is_valid_session ~__context session ->
1640+
session
1641+
| Some _ ->
1642+
Hashtbl.remove reusable_sessions sr_key ;
1643+
let new_session = create_session ~__context sr in
1644+
Hashtbl.add reusable_sessions sr_key new_session ;
1645+
new_session
1646+
| None ->
1647+
let new_session = create_session ~__context sr in
1648+
Hashtbl.add reusable_sessions sr_key new_session ;
1649+
new_session
1650+
)
1651+
1652+
let with_session ~traceparent sr f =
1653+
Server_helpers.exec_with_new_task "sm_exec"
1654+
~origin:(Internal_Traced traceparent) (fun __context ->
1655+
if !Xapi_globs.reuse_pool_sessions then
1656+
let session_id = get_session ~__context sr in
1657+
f session_id
1658+
else
1659+
let session_id = create_session ~__context sr in
1660+
let destroy_session () =
1661+
destroy_db_session ~__context ~self:session_id
1662+
in
1663+
finally (fun () -> f session_id) destroy_session
1664+
)
1665+
end

ocaml/xapi/xapi_session.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,10 @@ val set_local_auth_max_threads : int64 -> unit
112112
val set_ext_auth_max_threads : int64 -> unit
113113

114114
val clear_external_auth_cache : unit -> unit
115+
116+
module SM : sig
117+
val with_session :
118+
traceparent:Tracing.Span.t option
119+
-> [< Uuidx.all] Ref.t option
120+
-> (Uuidx.secret Ref.t -> 'a)
121+
-> 'a end

0 commit comments

Comments
 (0)