10
1
mirror of https://gitlab.com/scemama/qmcchem.git synced 2024-06-17 18:55:18 +02:00
qmcchem/ocaml/Qmcchem_forwarder.ml

550 lines
15 KiB
OCaml
Raw Normal View History

2017-10-10 09:39:58 +02:00
open Core
2015-12-19 02:35:13 +01:00
let bind_socket ~socket_type ~socket ~address =
2016-03-05 00:25:39 +01:00
let rec loop = function
| 0 -> failwith @@ Printf.sprintf
"Unable to bind the forwarder's %s socket : %s\n"
socket_type address
| -1 -> ()
| i ->
try
2018-06-04 10:26:49 +02:00
Zmq.Socket.bind socket address;
2016-03-05 00:25:39 +01:00
loop (-1)
with
2017-10-10 09:39:58 +02:00
| Unix.Unix_error _ -> (Time.pause @@ Time.Span.of_sec 1. ; loop (i-1) )
2016-03-05 00:25:39 +01:00
| other_exception -> raise other_exception
in loop 10
2015-12-19 02:35:13 +01:00
let run ezfio_filename dataserver =
let dataserver_address, dataserver_port =
2018-03-14 17:02:52 +01:00
Substring.create ~pos:6 (Bytes.of_string dataserver)
2015-12-19 02:35:13 +01:00
|> Substring.to_string
|> String.lsplit2_exn ~on:':'
and qmc =
Lazy.force Qmcchem_config.qmc
in
(* Go into /dev/shm *)
Unix.chdir Qmcchem_config.dev_shm;
let tmpdir =
ezfio_filename ^ "_" ^ dataserver_port
in
(* Port of the data server *)
let port =
(Int.of_string dataserver_port)+10
in
(* Build qmc executable command *)
2017-10-10 09:39:58 +02:00
let prog, argv =
2015-12-19 02:35:13 +01:00
qmc,
[ qmc ; ezfio_filename ;
Printf.sprintf "ipc://%s:%d" Qmcchem_config.dev_shm port ];
in
(* Create the temporary directory. If it is possible, then the process is a
* master and the forwarder will start. Otherwise, only start a qmc process.
*)
let () =
try
2016-03-05 00:25:39 +01:00
Unix.mkdir tmpdir;
Unix.chdir tmpdir
2015-12-19 02:35:13 +01:00
with
| Unix.Unix_error _ ->
begin
Unix.chdir tmpdir;
2017-10-10 09:39:58 +02:00
Time.pause @@ Time.Span.of_sec 0.1;
2016-03-05 00:25:39 +01:00
match (Sys.file_exists "PID") with
| `No
| `Unknown -> ()
| `Yes ->
let pid =
In_channel.with_file "PID" ~f:(fun ic ->
match (In_channel.input_line ic) with
| Some x -> x
| None -> "-1" )
|> Int.of_string
in
match pid with
| -1 -> ()
| pid ->
begin
match Signal.send (Signal.of_system_int 0) (`Pid (Pid.of_int pid)) with
| `No_such_process -> ()
2017-10-10 09:39:58 +02:00
| _ -> ignore @@ Unix.exec ~prog ~argv ()
2016-03-05 00:25:39 +01:00
end
2015-12-19 02:35:13 +01:00
end
in
(* Now, only one forwarder will execute the following code *)
2016-03-05 00:25:39 +01:00
Out_channel.with_file "PID" ~f:(fun oc ->
Unix.getpid ()
|> Pid.to_int
|> Printf.sprintf "%d\n"
|> Out_channel.output_string oc);
2015-12-19 02:35:13 +01:00
(* Fork a qmc *)
ignore @@
2017-10-10 09:39:58 +02:00
Watchdog.fork_exec ~prog ~argv ();
2015-12-19 02:35:13 +01:00
(* If there are MICs, use them here (TODO) *)
(* Fetch input *)
let zmq_context =
2018-06-04 10:26:49 +02:00
Zmq.Context.create ()
2015-12-19 02:35:13 +01:00
in
let terminate () =
(* Clean up the temp directory *)
Unix.chdir Qmcchem_config.dev_shm;
let command =
Printf.sprintf "rm -rf -- \"%s\" " tmpdir
in
match Unix.system command with
| Ok _ -> ()
| _ -> print_endline "Unable to remove temporary directory"
;
2018-06-04 10:26:49 +02:00
Zmq.Context.terminate zmq_context ;
for i=port to port+4
do
let filename =
Filename.concat Qmcchem_config.dev_shm (Printf.sprintf ":%d" i)
in
try
Unix.unlink filename
with
| _ -> ()
;
2016-03-05 00:25:39 +01:00
done;
Watchdog.kill ()
2015-12-19 02:35:13 +01:00
in
(* Signal handler to Kill properly all the processes *)
let handler s =
2016-03-03 13:57:33 +01:00
Printf.printf "Forwarder received the %s signal... killing\n%!" (Signal.to_string s);
2015-12-19 02:35:13 +01:00
terminate ();
in
List.iter [
2016-03-03 13:57:33 +01:00
Signal.int ;
2015-12-19 02:35:13 +01:00
Signal.term ;
Signal.quit ;
]
~f:(fun x -> Signal.Expert.handle x handler)
;
(* Fetch walkers *)
let walk_num =
ref 0
and walkers =
ref []
in
(* Status thread *)
let status =
ref Status.Running
in
let start_status_thread =
let f () =
let pub_socket =
2018-06-04 10:26:49 +02:00
Zmq.Socket.create zmq_context Zmq.Socket.pub
2015-12-19 02:35:13 +01:00
and address =
Printf.sprintf "ipc://%s:%d" Qmcchem_config.dev_shm (port+1);
in
bind_socket "PUB" pub_socket address;
let sub_socket =
2018-06-04 10:26:49 +02:00
Zmq.Socket.create zmq_context Zmq.Socket.sub
2015-12-19 02:35:13 +01:00
and address =
Printf.sprintf "tcp://%s:%d" dataserver_address (port+1-10)
in
2018-06-04 10:26:49 +02:00
Zmq.Socket.connect sub_socket address;
Zmq.Socket.subscribe sub_socket "";
2015-12-19 02:35:13 +01:00
let pollitem =
2018-06-04 10:26:49 +02:00
Zmq.Poll.mask_of
[| (sub_socket, Zmq.Poll.In) ;
2015-12-19 02:35:13 +01:00
|]
in
while (!status <> Status.Stopped)
do
let polling =
2018-06-04 10:26:49 +02:00
Zmq.Poll.poll ~timeout:1000 pollitem
2015-12-19 02:35:13 +01:00
in
2018-06-04 10:26:49 +02:00
if (polling.(0) = Some Zmq.Poll.In) then
2015-12-19 02:35:13 +01:00
begin
let msg =
2018-06-04 10:26:49 +02:00
Zmq.Socket.recv ~block:false sub_socket
2015-12-19 02:35:13 +01:00
in
2018-06-04 10:26:49 +02:00
Zmq.Socket.send pub_socket msg;
2015-12-19 02:35:13 +01:00
status := Status.of_string msg;
end;
done;
List.iter ~f:(fun socket ->
2018-06-04 10:26:49 +02:00
Zmq.Socket.set_linger_period socket 1000 ;
Zmq.Socket.close socket)
2015-12-19 02:35:13 +01:00
[ sub_socket ; pub_socket ]
in
Thread.create f
in
let start_log_thread =
let f () =
let sub_socket =
2018-06-04 10:26:49 +02:00
Zmq.Socket.create zmq_context Zmq.Socket.xsub
2015-12-19 02:35:13 +01:00
and address =
Printf.sprintf "ipc://%s:%d" Qmcchem_config.dev_shm (port+3);
in
bind_socket "XSUB" sub_socket address;
let pub_socket =
2018-06-04 10:26:49 +02:00
Zmq.Socket.create zmq_context Zmq.Socket.xpub
2015-12-19 02:35:13 +01:00
and address =
Printf.sprintf "tcp://%s:%d" dataserver_address (port+3-10)
in
2018-06-04 10:26:49 +02:00
Zmq.Socket.connect pub_socket address;
2015-12-19 02:35:13 +01:00
let pollitem =
2018-06-04 10:26:49 +02:00
Zmq.Poll.mask_of
[| (sub_socket, Zmq.Poll.In) ;
(pub_socket, Zmq.Poll.In) ;
2015-12-19 02:35:13 +01:00
|]
in
(* Main loop *)
while (!status <> Status.Stopped)
do
let polling =
2018-06-04 10:26:49 +02:00
Zmq.Poll.poll ~timeout:1000 pollitem
2015-12-19 02:35:13 +01:00
in
2018-06-04 10:26:49 +02:00
if (polling.(0) = Some Zmq.Poll.In) then
2015-12-19 02:35:13 +01:00
begin
2018-06-04 10:26:49 +02:00
Zmq.Socket.recv ~block:false sub_socket
|> Zmq.Socket.send pub_socket ;
2015-12-19 02:35:13 +01:00
end
2018-06-04 10:26:49 +02:00
else if (polling.(1) = Some Zmq.Poll.In) then
2015-12-19 02:35:13 +01:00
begin
2016-04-05 00:48:37 +02:00
Printf.eprintf "Forwarder subscribe\n%!";
2018-06-04 10:26:49 +02:00
Zmq.Socket.recv ~block:false pub_socket
|> Zmq.Socket.send sub_socket ;
2015-12-19 02:35:13 +01:00
end
done;
List.iter ~f:(fun socket ->
2018-06-04 10:26:49 +02:00
Zmq.Socket.set_linger_period socket 1000 ;
Zmq.Socket.close socket)
2015-12-19 02:35:13 +01:00
[ sub_socket ; pub_socket ]
in
Thread.create f
in
(* Proxy thread *)
let start_proxy_thread =
let f () =
let req_socket =
2018-06-04 10:26:49 +02:00
Zmq.Socket.create zmq_context Zmq.Socket.req
2015-12-19 02:35:13 +01:00
in
2018-06-04 10:26:49 +02:00
Zmq.Socket.connect req_socket dataserver;
Zmq.Socket.set_receive_timeout req_socket 600_000;
2015-12-19 02:35:13 +01:00
let dealer_socket =
2018-06-04 10:26:49 +02:00
Zmq.Socket.create zmq_context Zmq.Socket.dealer
2015-12-19 02:35:13 +01:00
in
bind_socket "PROXY" dealer_socket "inproc://dealer";
2018-06-04 10:26:49 +02:00
Zmq.Socket.set_receive_high_water_mark dealer_socket 100_000;
Zmq.Socket.set_send_high_water_mark dealer_socket 100_000;
Zmq.Socket.set_immediate dealer_socket true;
Zmq.Socket.set_linger_period dealer_socket 600_000;
2015-12-19 02:35:13 +01:00
let fetch_walkers () =
2018-06-04 10:26:49 +02:00
Zmq.Socket.send_all req_socket ["get_walkers" ; Int.to_string !walk_num ];
Zmq.Socket.recv_all req_socket
2015-12-19 02:35:13 +01:00
in
let pollitem =
2018-06-04 10:26:49 +02:00
Zmq.Poll.mask_of
[| (dealer_socket, Zmq.Poll.In) ;
2015-12-19 02:35:13 +01:00
|]
in
(* EZFIO Cache *)
let ezfio_cache =
String.Table.create ()
in
let handle_ezfio msg =
match Hashtbl.find ezfio_cache msg with
| Some result -> result
| None ->
begin
2018-06-04 10:26:49 +02:00
Zmq.Socket.send_all req_socket ["Ezfio" ; msg];
2015-12-19 02:35:13 +01:00
let result =
2018-06-04 10:26:49 +02:00
Zmq.Socket.recv_all req_socket
2015-12-19 02:35:13 +01:00
in
match (Hashtbl.add ezfio_cache ~key:msg ~data:result) with
| `Ok -> result
| `Duplicate -> result
end
in
(* Main loop *)
while (!status <> Status.Stopped)
do
let polling =
2018-06-04 10:26:49 +02:00
Zmq.Poll.poll ~timeout:1000 pollitem
2015-12-19 02:35:13 +01:00
in
2018-06-04 10:26:49 +02:00
if (polling.(0) = Some Zmq.Poll.In) then
2015-12-19 02:35:13 +01:00
begin
let raw_msg =
2018-06-04 10:26:49 +02:00
Zmq.Socket.recv_all ~block:false dealer_socket
2015-12-19 02:35:13 +01:00
in
let header, msg =
let rec aux header = function
| "" :: msg -> List.rev ("" :: header), Message.create msg
| head :: tail -> aux (head::header) tail
| _ -> failwith "Too many routers in the middle"
in
aux [] (List.map ~f:String.strip raw_msg)
in
let handle message =
match message with
| Message.Ezfio ezfio_msg ->
let result =
handle_ezfio ezfio_msg
in
2018-06-04 10:26:49 +02:00
Zmq.Socket.send_all dealer_socket (header @ result)
2015-12-19 02:35:13 +01:00
| Message.GetWalkers n_walks ->
begin
if (!walk_num = 0) then
begin
walk_num := Qptypes.Strictly_positive_int.to_int n_walks;
walkers := fetch_walkers ();
end;
2018-06-04 10:26:49 +02:00
Zmq.Socket.send_all dealer_socket (header @ !walkers);
2015-12-19 02:35:13 +01:00
walkers := fetch_walkers ();
end
| Message.Test ->
2018-06-04 10:26:49 +02:00
Zmq.Socket.send_all dealer_socket (header @ [ "OK" ])
| Message.Error _ -> ()
2015-12-19 02:35:13 +01:00
| Message.Register _
| Message.Unregister _
| Message.Walkers _
| Message.Property _ ->
failwith "Bad message"
in handle msg
end;
done;
2018-06-04 10:26:49 +02:00
Zmq.Socket.set_linger_period dealer_socket 1000 ;
Zmq.Socket.set_linger_period req_socket 1000 ;
Zmq.Socket.close dealer_socket;
Zmq.Socket.close req_socket;
2015-12-19 02:35:13 +01:00
in
Thread.create f
in
(* Main thread *)
let start_main_thread =
let f () =
let dealer_socket =
2018-06-04 10:26:49 +02:00
Zmq.Socket.create zmq_context Zmq.Socket.dealer
2015-12-19 02:35:13 +01:00
in
2018-06-04 10:26:49 +02:00
Zmq.Socket.connect dealer_socket dataserver;
Zmq.Socket.set_linger_period dealer_socket 600_000;
2015-12-19 02:35:13 +01:00
let proxy_socket =
2018-06-04 10:26:49 +02:00
Zmq.Socket.create zmq_context Zmq.Socket.dealer
2015-12-19 02:35:13 +01:00
in
2018-06-04 10:26:49 +02:00
Zmq.Socket.connect proxy_socket "inproc://dealer";
2015-12-19 02:35:13 +01:00
let router_socket =
2018-06-04 10:26:49 +02:00
Zmq.Socket.create zmq_context Zmq.Socket.router
2015-12-19 02:35:13 +01:00
and address =
Printf.sprintf "ipc://%s:%d" Qmcchem_config.dev_shm (port);
in
bind_socket "ROUTER" router_socket address;
2018-06-04 10:26:49 +02:00
Zmq.Socket.set_receive_high_water_mark router_socket 100000;
Zmq.Socket.set_send_high_water_mark router_socket 100000;
Zmq.Socket.set_immediate router_socket true;
Zmq.Socket.set_linger_period router_socket 600_000;
2015-12-19 02:35:13 +01:00
(* Pull socket for computed data *)
let push_socket =
2018-06-04 10:26:49 +02:00
Zmq.Socket.create zmq_context Zmq.Socket.push
2015-12-19 02:35:13 +01:00
and address =
Printf.sprintf "tcp://%s:%d" dataserver_address (port+2-10)
in
2018-06-04 10:26:49 +02:00
Zmq.Socket.connect push_socket address;
Zmq.Socket.set_linger_period push_socket 600_000;
2015-12-19 02:35:13 +01:00
let pull_socket =
2018-06-04 10:26:49 +02:00
Zmq.Socket.create zmq_context Zmq.Socket.pull
2015-12-19 02:35:13 +01:00
and address =
Printf.sprintf "ipc://%s:%d" Qmcchem_config.dev_shm (port+2);
in
bind_socket "PULL" pull_socket address;
(* Handles messages coming into the ROUTER socket. *)
let handle_router () =
let raw_msg =
2018-06-04 10:26:49 +02:00
Zmq.Socket.recv_all ~block:false router_socket
2015-12-19 02:35:13 +01:00
in
let header, msg =
let rec aux header = function
| "" :: msg -> List.rev ("" :: header), Message.create msg
| head :: tail -> aux (head::header) tail
| _ -> failwith "Too many routers in the middle"
in
aux [] (List.map ~f:String.strip raw_msg)
in
let handle message =
match message with
| Message.GetWalkers _
| Message.Ezfio _
| Message.Test ->
2018-06-04 10:26:49 +02:00
Zmq.Socket.send_all proxy_socket raw_msg
2015-12-19 02:35:13 +01:00
| Message.Register _
| Message.Unregister _ ->
2018-06-04 10:26:49 +02:00
Zmq.Socket.send_all dealer_socket raw_msg
2015-12-19 02:35:13 +01:00
| Message.Walkers (_, _, _)
| Message.Property _ ->
failwith "Bad message"
| Message.Error _ -> ()
2015-12-19 02:35:13 +01:00
in handle msg
in
let handle_dealer () =
2018-06-04 10:26:49 +02:00
Zmq.Socket.recv_all ~block:false dealer_socket
|> Zmq.Socket.send_all router_socket
2015-12-19 02:35:13 +01:00
in
let handle_proxy () =
2018-06-04 10:26:49 +02:00
Zmq.Socket.recv_all ~block:false proxy_socket
|> Zmq.Socket.send_all router_socket
2015-12-19 02:35:13 +01:00
in
2016-04-05 00:48:37 +02:00
let select_n_of ~n ~len l =
let a =
Array.of_list l
in
let s =
(Array.length a)/ len
in
let fetch i =
let rec loop accu = function
| -1 -> accu
2016-04-05 11:52:04 +02:00
| k -> loop ((Array.get a (i*len+k)) :: accu) (k-1)
2016-04-05 00:48:37 +02:00
in
loop [] (len-1)
in
let rec select accu = function
| 0 -> accu
| i -> let new_accu =
(fetch @@ Random.int s) :: accu
in
select new_accu (i-1)
in
select [] n
|> List.concat
in
2015-12-19 02:35:13 +01:00
(* Handles messages coming into the PULL socket. *)
let handle_pull () =
2016-04-05 00:48:37 +02:00
let message =
2018-06-04 10:26:49 +02:00
Zmq.Socket.recv_all ~block:false pull_socket
2016-04-05 00:48:37 +02:00
in
let new_message =
match message with
| "elec_coord":: hostname :: pid :: id :: n_str :: rest ->
let n =
Int.of_string n_str
in
let len =
n / !walk_num
in
if (n < 5*len) then
message
else
List.concat [ [ "elec_coord" ; hostname ; pid ; id ;
Int.to_string (5*len)] ; ( select_n_of ~n:5 ~len rest ) ]
| _ -> message
in
2018-06-04 10:26:49 +02:00
Zmq.Socket.send_all push_socket new_message
2015-12-19 02:35:13 +01:00
in
(* Polling item to poll ROUTER and PULL sockets. *)
let pollitem =
2018-06-04 10:26:49 +02:00
Zmq.Poll.mask_of
[| (router_socket , Zmq.Poll.In) ;
(pull_socket , Zmq.Poll.In) ;
(dealer_socket, Zmq.Poll.In) ;
(proxy_socket , Zmq.Poll.In)
2015-12-19 02:35:13 +01:00
|]
in
(* Main loop *)
while (!status <> Status.Stopped)
do
let polling =
2018-06-04 10:26:49 +02:00
Zmq.Poll.poll ~timeout:1000 pollitem
2015-12-19 02:35:13 +01:00
in
2018-06-04 10:26:49 +02:00
if (polling.(0) = Some Zmq.Poll.In) then
2015-12-19 02:35:13 +01:00
handle_router ();
2018-06-04 10:26:49 +02:00
if (polling.(1) = Some Zmq.Poll.In) then
2015-12-19 02:35:13 +01:00
handle_pull ();
2018-06-04 10:26:49 +02:00
if (polling.(2) = Some Zmq.Poll.In) then
2015-12-19 02:35:13 +01:00
handle_dealer ();
2018-06-04 10:26:49 +02:00
if (polling.(3) = Some Zmq.Poll.In) then
2015-12-19 02:35:13 +01:00
handle_proxy ();
done;
List.iter ~f:(fun socket ->
2018-06-04 10:26:49 +02:00
Zmq.Socket.set_linger_period socket 1000 ;
Zmq.Socket.close socket)
2015-12-19 02:35:13 +01:00
[ router_socket ; dealer_socket ; push_socket ; pull_socket ; proxy_socket ]
in
Thread.create f
in
(* Start the status thread and the main thread *)
begin
try
(List.iter ~f:Thread.join
[ start_status_thread ();
start_log_thread ();
start_proxy_thread ();
start_main_thread ();
])
with
| err ->
begin
print_endline "Trapped error. Waiting 10 seconds...";
status := Status.Stopping;
Time.Span.of_sec 10. |> Time.pause;
raise err
end
end;
(* Wait for the qmc process to complete *)
2016-03-03 13:39:06 +01:00
try
ignore (Watchdog.join ());
terminate ()
with
| error ->
begin
terminate ();
raise error
end
2015-12-19 02:35:13 +01:00