Source file command_rpc.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
open Core
open Poly
open Async
open Command_rpc_intf

module Default_timeouts = struct
  (* Here we greatly increase the default timeouts that we inherit from Async_rpc.
     The reason it makes sense to have larger defaults here are:
     - it's common to use [Command_rpc] to offload slow computations to the child process,
       so long async cycles in the child are more likely, which makes spurious timeout
       more likely;
     - command_rpc timeouts are almost always fatal for the application, while it's
       somewhat-common to have retries or fallbacks for network services;
     - command_rpc commands are often started from NFS, or compiled on the first start,
       and we've seen that take some tens of second occasionally.

     The reason the parent timeouts are shorter than child timeouts is that errors
     detected in the parent have a simpler error handling story.
  *)
  let default_handshake_timeout ~side =
    match side with
    | `parent -> Time.Span.of_min 10.
    | `child -> Time.Span.of_hr 1.
  ;;

  let default_heartbeat_config ~side =
    Rpc.Connection.Heartbeat_config.create
      ~timeout:
        (Time_ns.Span.of_span_float_round_nearest_microsecond
           (default_handshake_timeout ~side))
      ~send_every:(Time_ns.Span.of_sec 10.)
      ()
  ;;
end

open Default_timeouts

module Command = struct
  module Invocation = struct
    type t =
      | Sexp
      | Bin_io of Rpc.Connection.t
  end

  module Stateful = struct
    module type T = T
    module type T_conv = T_conv
    module type T_pipe = T_pipe
    module type T_pipe_conv = T_pipe_conv

    type 'state t =
      [ `Plain of (module T with type state = 'state)
      | `Plain_conv of (module T_conv with type state = 'state)
      | `Pipe of (module T_pipe with type state = 'state)
      | `Pipe_conv of (module T_pipe_conv with type state = 'state)
      | `Implementations of 'state Rpc.Implementation.t list
      ]

    let lift (type a b) (t : a t) ~(f : b -> a) : b t =
      match t with
      | `Plain (module M) ->
        `Plain
          (module struct
            include (M : T with type state := a)

            type state = b

            let implementation state query = implementation (f state) query
          end)
      | `Plain_conv (module M) ->
        `Plain_conv
          (module struct
            include (M : T_conv with type state := a)

            type state = b

            let implementation state ~version query =
              implementation (f state) ~version query
            ;;
          end)
      | `Pipe (module M) ->
        `Pipe
          (module struct
            include (M : T_pipe with type state := a)

            type state = b

            let implementation state query = implementation (f state) query
          end)
      | `Pipe_conv (module M) ->
        `Pipe_conv
          (module struct
            include (M : T_pipe_conv with type state := a)

            type state = b

            let implementation state ~version query =
              implementation (f state) ~version query
            ;;
          end)
      | `Implementations impls ->
        `Implementations (List.map impls ~f:(Rpc.Implementation.lift ~f))
    ;;
  end

  module type T = Stateful.T with type state := Invocation.t
  module type T_conv = Stateful.T_conv with type state := Invocation.t
  module type T_pipe = Stateful.T_pipe with type state := Invocation.t
  module type T_pipe_conv = Stateful.T_pipe_conv with type state := Invocation.t

  type t =
    [ `Plain of (module T)
    | `Plain_conv of (module T_conv)
    | `Pipe of (module T_pipe)
    | `Pipe_conv of (module T_pipe_conv)
    | `Implementations of Invocation.t Rpc.Implementation.t list
    ]

  let stateful (rpcs : Invocation.t Stateful.t list) = (rpcs :> t list)

  let menu impls =
    match
      Map.Poly.of_alist
        (List.concat_map impls ~f:(fun impl ->
           match impl with
           | `Plain plain ->
             let module T = (val plain : T) in
             [ (Rpc.Rpc.name T.rpc, Rpc.Rpc.version T.rpc), impl ]
           | `Plain_conv x ->
             let module T = (val x : T_conv) in
             let versions = Set.to_list @@ T.versions () in
             List.map versions ~f:(fun version -> (T.name, version), impl)
           | `Pipe pipe ->
             let module T = (val pipe : T_pipe) in
             [ (Rpc.Pipe_rpc.name T.rpc, Rpc.Pipe_rpc.version T.rpc), impl ]
           | `Pipe_conv pipe ->
             let module T = (val pipe : T_pipe_conv) in
             let versions = Set.to_list @@ T.versions () in
             List.map versions ~f:(fun version -> (T.name, version), impl)
           | `Implementations impls ->
             List.map impls ~f:(fun impl ->
               let desc = Rpc.Implementation.description impl in
               (desc.name, desc.version), `Implementations impls)))
    with
    | `Ok map -> map
    | `Duplicate_key (name, version) ->
      failwithf "multiple implementations of rpc (%s %d)" name version ()
  ;;

  let implementations ?log_not_previously_seen_version
    : t -> Invocation.t Rpc.Implementation.t list
    = function
      | `Plain (module T) -> [ Rpc.Rpc.implement T.rpc T.implementation ]
      | `Plain_conv (module T) ->
        T.implement_multi ?log_not_previously_seen_version (fun s ~version q ->
          T.implementation s ~version q)
      | `Pipe (module T) -> [ Rpc.Pipe_rpc.implement T.rpc T.implementation ]
      | `Pipe_conv (module T) ->
        T.implement_multi ?log_not_previously_seen_version (fun s ~version q ->
          T.implementation s ~version q)
      | `Implementations impls -> impls
  ;;

  type call =
    { rpc_name : string
    ; version : int
    ; query : Sexp.t
    }
  [@@deriving sexp]

  let write_sexp w sexp =
    Writer.write_sexp w sexp;
    Writer.newline w
  ;;

  (** This function returns [stdin] and [stdout] that are connected to the same kernel
      objects (files/pipes/etc) as [Reader.stdin] and [Writer.stdout] before the
      call, except they will have new file descriptor numbers (greater than 2) to avoid
      the other parts of the program writing there by accident.

      It also changes file descriptors such that after this function [Reader.stdin] is
      reading from /dev/null and [Writer.stdout] is writing to stderr.
  *)
  let claim_stdin_and_stdout_for_exclusive_use ?buffer_age_limit () =
    let same_fd fd1 fd2 =
      Int.( = ) (Core_unix.File_descr.to_int fd1) (Core_unix.File_descr.to_int fd2)
    in
    let equivalent_fd fd1 fd2 =
      (* this is the same check [Writer] does when sharing the writer between stderr
         and stdout *)
      let dev_and_ino fd =
        let stats = Core_unix.fstat fd in
        stats.st_dev, stats.st_ino
      in
      same_fd fd1 fd2 || dev_and_ino fd1 = dev_and_ino fd2
    in
    let stdin = Lazy.force Reader.stdin in
    let stdout = Lazy.force Writer.stdout in
    let stderr = Lazy.force Writer.stderr in
    assert (same_fd (Fd.file_descr_exn (Reader.fd stdin)) Core_unix.stdin);
    (* Async has a special hack where if file descriptors 1 and 2 happen to point to the
       same file/device (for example when running in a tty) then it only creates one
       writer (ignoring file descriptor 2) that's used for both [Writer.stderr] and
       [Writer.stdout]. In this situation [same_fd] will be false, but [equivalent_fd]
       will be true and that should be enough to keep sanity below. *)
    assert (equivalent_fd (Fd.file_descr_exn (Writer.fd stdout)) Core_unix.stdout);
    assert (equivalent_fd (Fd.file_descr_exn (Writer.fd stderr)) Core_unix.stderr);
    let make_a_copy_of_stdin_and_stdout () =
      let dupped_stdin = Core_unix.dup Core_unix.stdin in
      Core_unix.set_close_on_exec dupped_stdin;
      let dupped_stdout = Core_unix.dup Core_unix.stdout in
      Core_unix.set_close_on_exec dupped_stdout;
      assert (Core_unix.File_descr.to_int dupped_stdin > 2);
      assert (Core_unix.File_descr.to_int dupped_stdout > 2);
      let create_fd ~similar_to fd =
        Fd.create (Fd.kind similar_to) fd (Fd.info similar_to)
      in
      let stdin = Reader.create (create_fd ~similar_to:(Reader.fd stdin) dupped_stdin) in
      let stdout =
        Writer.create
          ?buffer_age_limit
          (create_fd ~similar_to:(Writer.fd stdout) dupped_stdout)
      in
      stdin, stdout
    in
    let make_sure_stdin_and_stdout_are_not_used () =
      (* After this, anyone attempting to read from stdin gets an empty result
         and anything written to stdout goes to stderr instead. *)
      let dev_null = Core_unix.openfile ~mode:[ O_RDONLY ] "/dev/null" in
      Async.Fd.expect_file_descr_redirection Core_unix.stdin ~f:(fun () ->
        Core_unix.dup2 ~src:dev_null ~dst:Core_unix.stdin ());
      Async.Fd.expect_file_descr_redirection Core_unix.stdout ~f:(fun () ->
        Core_unix.dup2 ~src:Core_unix.stderr ~dst:Core_unix.stdout ());
      Core_unix.close dev_null
    in
    let res = make_a_copy_of_stdin_and_stdout () in
    make_sure_stdin_and_stdout_are_not_used ();
    res
  ;;

  let main
        ?connection_description
        ?(handshake_timeout = default_handshake_timeout ~side:`child)
        ?(heartbeat_config = default_heartbeat_config ~side:`child)
        ?max_message_size
        ?log_not_previously_seen_version
        ?buffer_age_limit
        impls
        ~show_menu
        mode
    =
    if show_menu
    then (
      let menu_sexp = [%sexp_of: (string * int) list] (Map.keys (menu impls)) in
      write_sexp (Lazy.force Writer.stdout) menu_sexp;
      return `Success)
    else (
      let stdin, stdout = claim_stdin_and_stdout_for_exclusive_use ?buffer_age_limit () in
      match mode with
      | `Bin_prot ->
        (match
           Rpc.Implementations.create
             ~on_unknown_rpc:`Raise
             ~implementations:
               (Versioned_rpc.Menu.add
                  (List.concat_map
                     ~f:(implementations ?log_not_previously_seen_version)
                     impls))
         with
         | Error (`Duplicate_implementations descriptions) ->
           raise_s
             [%message "duplicate implementations" (descriptions : Rpc.Description.t list)]
         | Ok implementations ->
           Rpc.Connection.server_with_close
             stdin
             stdout
             ?description:connection_description
             ~handshake_timeout
             ~heartbeat_config
             ?max_message_size
             ~implementations
             ~connection_state:(fun conn -> Bin_io conn)
             ~on_handshake_error:`Raise
           >>| fun () -> `Success)
      | `Sexp ->
        Reader.read_sexp stdin
        >>= (function
          | `Eof -> failwith "unexpected EOF on stdin"
          | `Ok sexp ->
            let call = call_of_sexp sexp in
            (match Map.find (menu impls) (call.rpc_name, call.version) with
             | None -> failwithf "unimplemented rpc: (%s, %d)" call.rpc_name call.version ()
             | Some impl ->
               (match impl with
                | `Plain (module T) ->
                  let query = T.query_of_sexp call.query in
                  T.implementation Sexp query
                  >>| fun response ->
                  write_sexp stdout (T.sexp_of_response response);
                  `Success
                | `Plain_conv (module T) ->
                  let query = T.query_of_sexp call.query in
                  T.implementation Sexp ~version:call.version query
                  >>| fun response ->
                  write_sexp stdout (T.sexp_of_response response);
                  `Success
                | `Pipe (module T) ->
                  let query = T.query_of_sexp call.query in
                  T.implementation Sexp query
                  >>= (function
                    | Error e ->
                      write_sexp stdout (T.sexp_of_error e);
                      return `Failure
                    | Ok pipe ->
                      Pipe.iter pipe ~f:(fun r ->
                        write_sexp stdout (T.sexp_of_response r);
                        Deferred.unit)
                      >>| fun () -> `Success)
                | `Pipe_conv (module T) ->
                  let query = T.query_of_sexp call.query in
                  T.implementation Sexp ~version:call.version query
                  >>= (function
                    | Error e ->
                      write_sexp stdout (T.sexp_of_error e);
                      return `Failure
                    | Ok pipe ->
                      Pipe.iter pipe ~f:(fun r ->
                        write_sexp stdout (T.sexp_of_response r);
                        Deferred.unit)
                      >>| fun () -> `Success)
                | `Implementations _ ->
                  failwithf
                    "This RPC is not supported in [-sexp] mode: (%s, %d)"
                    call.rpc_name
                    call.version
                    ()))))
  ;;

  let async_main status_deferred =
    upon status_deferred (fun status ->
      Shutdown.shutdown
        (match status with
         | `Success -> 0
         | `Failure -> 1));
    never_returns (Scheduler.go ())
  ;;

  let menu_doc = " dump a sexp representation of the rpc menu"
  let sexp_doc = " speak sexp instead of bin-prot"

  module Expert = struct
    let param_exit_status () =
      let open Command.Let_syntax in
      [%map_open
        let show_menu = flag "-menu" no_arg ~doc:menu_doc
        and sexp = flag "-sexp" no_arg ~doc:sexp_doc in
        fun ?connection_description
          ?handshake_timeout
          ?heartbeat_config
          ?max_message_size
          ?log_not_previously_seen_version
          ?buffer_age_limit
          impls ->
          main
            ?connection_description
            ?handshake_timeout
            ?heartbeat_config
            ?max_message_size
            ?log_not_previously_seen_version
            ?buffer_age_limit
            impls
            ~show_menu
            (if sexp then `Sexp else `Bin_prot)]
    ;;

    let param () =
      Command.Param.map
        (param_exit_status ())
        ~f:(fun
             main
             ?connection_description
             ?handshake_timeout
             ?heartbeat_config
             ?max_message_size
             ?log_not_previously_seen_version
             ?buffer_age_limit
             rpcs
             ->
               (* If you want to detect success or failure and do something appropriate,
                  you can just do that from your RPC implementation. But we still need
                  [param_exit_status] separately because [create] below doesn't have
                  access to the RPC implementations. *)
               main
                 ?connection_description
                 ?handshake_timeout
                 ?heartbeat_config
                 ?max_message_size
                 ?log_not_previously_seen_version
                 ?buffer_age_limit
                 rpcs
               >>| function
               | `Success | `Failure -> ())
    ;;
  end

  let create
        ?connection_description
        ?handshake_timeout
        ?heartbeat_config
        ?max_message_size
        ?log_not_previously_seen_version
        ?buffer_age_limit
        ~summary
        impls
    =
    let open Command.Let_syntax in
    Command.basic
      ~summary
      [%map_open
        let main = Expert.param_exit_status () in
        fun () ->
          async_main
            (main
               ?connection_description
               ?handshake_timeout
               ?heartbeat_config
               ?max_message_size
               ?log_not_previously_seen_version
               ?buffer_age_limit
               impls)]
  ;;
end

module Connection = struct
  (* We explicitly [Process.wait] as soon as we start our connection. This guarantees the
     subprocess gets cleaned up even if the client does not explicitly wait. *)
  type t =
    { process : Process.t
    ; wait : Unix.Exit_or_signal.t Deferred.t
    ; rpc_connection : Rpc.Connection.t
    }

  let rpc_connection t = t.rpc_connection
  let wait t = t.wait
  let kill t signal = Process.send_signal t.process signal

  type 'a with_connection_args =
    ?wait_for_stderr_transfer:bool
    -> ?connection_description:Info.t
    -> ?handshake_timeout:Time.Span.t
    -> ?heartbeat_config:Rpc.Connection.Heartbeat_config.t
    -> ?max_message_size:int
    -> ?implementations:unit Rpc.Implementations.t
    -> ?propagate_stderr:bool (* defaults to true *)
    -> ?env:Process.env (* defaults to [`Extend []] *)
    -> ?process_create:
         (prog:string
          -> args:string list
          -> ?env:Process.env
          -> ?working_dir:string
          -> unit
          -> Process.t Deferred.Or_error.t)
    -> ?working_dir:string
    -> prog:string
    -> args:string list
    -> 'a

  let transfer_stderr child_stderr =
    Reader.transfer child_stderr (Writer.pipe (Lazy.force Writer.stderr))
    >>= fun () -> Reader.close child_stderr
  ;;

  let connect_gen
        ?(wait_for_stderr_transfer = false)
        ?(process_create =
          fun ~prog ~args ?env ?working_dir () ->
            Process.create ~prog ~args ?env ?working_dir ())
        ~propagate_stderr
        ~env
        ~prog
        ~args
        ?working_dir
        f
    =
    process_create ~prog ~args ~env ?working_dir ()
    >>=? fun process ->
    let stdin = Process.stdin process in
    let stdout = Process.stdout process in
    let stderr = Process.stderr process in
    let stderr_flushed =
      if propagate_stderr then transfer_stderr stderr else Reader.drain stderr
    in
    if not wait_for_stderr_transfer
    then
      (* This is mainly so that when a user closes the connection (which closes stdin and
         stdout) we will also close stderr. *)
      don't_wait_for
        (Writer.close_finished stdin
         >>= fun () -> Reader.close_finished stdout >>= fun () -> Reader.close stderr);
    let wait = Process.wait process in
    let wait =
      match wait_for_stderr_transfer with
      | false -> wait
      | true ->
        let%map wait = wait
        and () = stderr_flushed in
        wait
    in
    f ~process ~wait
  ;;

  let with_close
        ?wait_for_stderr_transfer
        ?connection_description
        ?handshake_timeout
        ?heartbeat_config
        ?max_message_size
        ?implementations
        ?(propagate_stderr = true)
        ?(env = `Extend [])
        ?process_create
        ?working_dir
        ~prog
        ~args
        dispatch_queries
    =
    connect_gen
      ?wait_for_stderr_transfer
      ?process_create
      ~propagate_stderr
      ~env
      ~prog
      ~args
      ?working_dir
      (fun ~process ~wait ->
         let%bind result =
           Rpc.Connection.with_close
             (Process.stdout process)
             (Process.stdin process)
             ?description:connection_description
             ?handshake_timeout
             ?heartbeat_config
             ?max_message_size
             ?implementations
             ~connection_state:(fun _ -> ())
             ~on_handshake_error:(`Call (fun exn -> return (Or_error.of_exn exn)))
             ~dispatch_queries:(fun rpc_connection ->
               dispatch_queries { process; wait; rpc_connection })
         in
         let%bind exit_or_signal = wait in
         ignore (exit_or_signal : Unix.Exit_or_signal.t);
         return result)
  ;;

  let create
        ?wait_for_stderr_transfer
        ?connection_description
        ?(handshake_timeout = default_handshake_timeout ~side:`parent)
        ?(heartbeat_config = default_heartbeat_config ~side:`parent)
        ?max_message_size
        ?implementations
        ?(propagate_stderr = true)
        ?(env = `Extend [])
        ?process_create
        ?working_dir
        ~prog
        ~args
        ()
    =
    connect_gen
      ?wait_for_stderr_transfer
      ?process_create
      ~propagate_stderr
      ~env
      ~prog
      ~args
      ?working_dir
      (fun ~process ~wait ->
         Rpc.Connection.create
           (Process.stdout process)
           (Process.stdin process)
           ?description:connection_description
           ~handshake_timeout
           ~heartbeat_config
           ?max_message_size
           ?implementations
           ~connection_state:(fun _ -> ())
         >>| Or_error.of_exn_result
         >>| Or_error.map ~f:(fun rpc_connection -> { process; wait; rpc_connection }))
  ;;

  module Expert = struct
    let wait = wait
    let kill = kill
  end
end