Skip to content

Commit 0327da7

Browse files
author
David Scott
committed
Refactor the volume plugin watching code
This creates the foundation for watching the datapath plugins. Signed-off-by: David Scott <[email protected]>
1 parent 9c10c00 commit 0327da7

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

main.ml

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -625,49 +625,43 @@ let get_ok = function
625625
Format.pp_print_flush fmt ();
626626
failwith (Buffer.contents b)
627627

628-
let create switch_path root_dir name =
629-
if Hashtbl.mem servers name
630-
then return ()
631-
else begin
632-
info "Adding %s" name
633-
>>= fun () ->
634-
Protocol_async.Server.listen ~process:(process root_dir name) ~switch:switch_path ~queue:(Filename.basename name) ()
635-
>>= fun result ->
636-
let server = get_ok result in
637-
Hashtbl.add_exn servers name server;
638-
return ()
639-
end
640-
641-
let destroy switch_path name =
642-
info "Removing %s" name
643-
>>= fun () ->
644-
if Hashtbl.mem servers name then begin
645-
let t = Hashtbl.find_exn servers name in
646-
Protocol_async.Server.shutdown ~t () >>= fun () ->
647-
Hashtbl.remove servers name;
648-
return ()
649-
end else return ()
650628

651629
let rec diff a b = match a with
652630
| [] -> []
653631
| a :: aa ->
654632
if List.mem b a then diff aa b else a :: (diff aa b)
655633

656-
(* Ensure the right servers are started *)
657-
let sync ~root_dir ~switch_path =
658-
Sys.readdir root_dir
659-
>>= fun names ->
660-
let needed : string list = Array.to_list names in
661-
let got_already : string list = Hashtbl.keys servers in
662-
Deferred.all_ignore (List.map ~f:(create switch_path root_dir) (diff needed got_already))
663-
>>= fun () ->
664-
Deferred.all_ignore (List.map ~f:(destroy switch_path) (diff got_already needed))
665-
666-
let main ~root_dir ~state_path ~switch_path =
667-
Attached_SRs.reload state_path
668-
>>= fun () ->
669-
(* We watch and create queues for the Volume plugins only *)
634+
let watch_volume_plugins ~root_dir ~switch_path =
670635
let root_dir = Filename.concat root_dir "volume" in
636+
let create switch_path root_dir name =
637+
if Hashtbl.mem servers name
638+
then return ()
639+
else begin
640+
info "Adding %s" name
641+
>>= fun () ->
642+
Protocol_async.Server.listen ~process:(process root_dir name) ~switch:switch_path ~queue:(Filename.basename name) ()
643+
>>= fun result ->
644+
let server = get_ok result in
645+
Hashtbl.add_exn servers name server;
646+
return ()
647+
end in
648+
let destroy switch_path name =
649+
info "Removing %s" name
650+
>>= fun () ->
651+
if Hashtbl.mem servers name then begin
652+
let t = Hashtbl.find_exn servers name in
653+
Protocol_async.Server.shutdown ~t () >>= fun () ->
654+
Hashtbl.remove servers name;
655+
return ()
656+
end else return () in
657+
let sync ~root_dir ~switch_path =
658+
Sys.readdir root_dir
659+
>>= fun names ->
660+
let needed : string list = Array.to_list names in
661+
let got_already : string list = Hashtbl.keys servers in
662+
Deferred.all_ignore (List.map ~f:(create switch_path root_dir) (diff needed got_already))
663+
>>= fun () ->
664+
Deferred.all_ignore (List.map ~f:(destroy switch_path) (diff got_already needed)) in
671665
Async_inotify.create ~recursive:false ~watch_new_dirs:false root_dir
672666
>>= fun (watch, _) ->
673667
sync ~root_dir ~switch_path
@@ -698,6 +692,11 @@ let main ~root_dir ~state_path ~switch_path =
698692
loop () in
699693
loop ()
700694

695+
let main ~root_dir ~state_path ~switch_path =
696+
Attached_SRs.reload state_path
697+
>>= fun () ->
698+
watch_volume_plugins ~root_dir ~switch_path
699+
701700
let main ~root_dir ~state_path ~switch_path =
702701
let (_: unit Deferred.t) = main ~root_dir ~state_path ~switch_path in
703702
never_returns (Scheduler.go ())

0 commit comments

Comments
 (0)