package dune-rpc

  1. Overview
  2. Docs

Source file versioned.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
open Import
open Types

module Version_error = struct
  type t =
    { payload : Csexp.t option
    ; message : string
    }

  let payload t = t.payload

  let message t = t.message

  let create ?payload ~message () = { payload; message }

  exception E of t

  let to_response_error { payload; message } =
    Response.Error.create ~kind:Invalid_request ?payload ~message ()
end

module Staged = struct
  type ('req, 'resp) request =
    { encode_req : 'req -> Call.t
    ; decode_resp : Csexp.t -> ('resp, Response.Error.t) result
    }

  type 'payload notification = { encode : 'payload -> Call.t }
end

module Make (Fiber : Fiber_intf.S) = struct
  module Handler = struct
    type 'state t =
      { menu : Menu.t
      ; handle_request :
          Menu.t -> 'state -> Types.Request.t -> Response.t Fiber.t
      ; handle_notification :
          Menu.t -> 'state -> Call.t -> (unit, Response.Error.t) result Fiber.t
      ; prepare_request :
          'req 'resp.
             Menu.t
          -> ('req, 'resp) Decl.Request.witness
          -> (('req, 'resp) Staged.request, Version_error.t) result
      ; prepare_notification :
          'a.
             Menu.t
          -> 'a Decl.Notification.witness
          -> ('a Staged.notification, Version_error.t) result
      }

    let handle_request t = t.handle_request t.menu

    let handle_notification t = t.handle_notification t.menu

    let prepare_request t = t.prepare_request t.menu

    let prepare_notification t = t.prepare_notification t.menu
  end

  (* TODO: This module involves some convoluted and difficult-to-understand
     types, with multiple levels of GADTs and type packing, in the
     (possibly-misguided) twin aims of ensuring type safety and maximizing reuse
     of the actual generation management code. *)
  module Builder = struct
    open Decl

    (* A [('req, 'resp) Decl.Generation.t] contains the information necessary to
       convert from a [Csexp.t] to a ['req]. The [_handler] packings are to
       enable storing the callbacks in a homogeneous data structure (namely, the
       [Method_name.Table.t]. It's alright to erase these types, because these
       callbacks are intended to be used by the receiving endpoint, which only
       sees a [Csexp.t], and we only discover the correct type to deserialize to
       at runtime. *)
    type 's r_handler =
      | R :
          ('s -> 'req -> 'resp Fiber.t) * ('req, 'resp) Decl.Generation.t
          -> 's r_handler

    type 's n_handler =
      | N :
          ('s -> 'payload -> unit Fiber.t) * ('payload, unit) Decl.Generation.t
          -> 's n_handler

    (* Pack a universal map key. See below. We can afford to erase the type of
       the key, because we only care about the keyset of the stored generation
       listing. *)
    type packed = T : 'a Method_version.Map.t Univ_map.Key.t -> packed

    (* The declarations and implementations serve dual purposes with dual
       requirements.

       When storing implementations, we erase the type of the callback, because
       we cannot know what type to deserialize to until runtime, and so all that
       matters is whether some handler with the correct type exists.

       On the other hand, declarations must keep some type information in an
       externally-retrievable way. This is because when invoking an RPC of type
       [('req, 'resp)], we are *given* a value of type ['req], so the object
       being stored in the map cannot have its type erased. Instead, we use a
       [Univ_map] (with the key being stored in the [Decl.t]) so we can retrieve
       a correctly-typed [Generation.t] mapping later.

       However, unlike a string table, the use of a [Univ_map.t] means that we
       cannot examine the map alone to get a list of all declared procedures and
       versions. This is bad, because we need that information to perform
       version negotiation for the session. To resolve this, we also keep a
       mapping of all known keys and their associated method names, which we use
       to construct the initial version menu, then discard. *)
    type 'state t =
      { mutable declared_requests : packed list Method_name.Map.t * Univ_map.t
      ; mutable declared_notifications :
          packed list Method_name.Map.t * Univ_map.t
      ; implemented_requests :
          'state r_handler Method_version.Map.t Method_name.Table.t
      ; implemented_notifications :
          'state n_handler Method_version.Map.t Method_name.Table.t
      }

    (* A [('state, 'key, 'output) field_witness] is a first-class representation
       of a field of a ['state t]. Each field is morally a mutable table holding
       ['output Method_version.Map.t]s (mapping generation numbers to
       ['output]s), indexed by ['key]s.

       The mental model isn't strictly correct (mostly due to needing the "all
       known registered keys" hack described above), but is accurate enough that
       the types of [get] and [set] below should become readable.

       By doing things this way, we can abstract away the logic of

       - Checking the corresponding registry (the declarations table when
       implementing, and vice versa) for duplicate entries

       - Checking the provided generation listings for overlap

       and

       - Looking up a method name and generation number

       from the type-erasure implementation shenanigans described above, letting
       all related operations (declaring, implementing, dispatching) share
       uniform implementations as much as possible. *)
    type (_, _, _) field_witness =
      | Declared_requests
          : ( _
            , Method_name.t
              * ('req, 'resp) Decl.Generation.t Method_version.Map.t
                Univ_map.Key.t
            , ('req, 'resp) Decl.Generation.t )
            field_witness
      | Declared_notifs
          : ( _
            , Method_name.t
              * ('a, unit) Decl.Generation.t Method_version.Map.t Univ_map.Key.t
            , ('a, unit) Decl.Generation.t )
            field_witness
      | Impl_requests : ('state, string, 'state r_handler) field_witness
      | Impl_notifs : ('state, string, 'state n_handler) field_witness

    let get (type st a b) (t : st t) (witness : (st, a, b) field_witness)
        (key : a) : b Method_version.Map.t option =
      match witness with
      | Declared_requests ->
        let _, key = key in
        let _, table = t.declared_requests in
        Univ_map.find table key
      | Declared_notifs ->
        let _, key = key in
        let _, table = t.declared_notifications in
        Univ_map.find table key
      | Impl_requests -> Method_name.Table.find t.implemented_requests key
      | Impl_notifs -> Method_name.Table.find t.implemented_notifications key

    let set (type st a b) (t : st t) (witness : (st, a, b) field_witness)
        (key : a) (value : b Method_version.Map.t) =
      match witness with
      | Declared_requests ->
        let name, key = key in
        let known_keys, table = t.declared_requests in
        t.declared_requests <-
          ( Method_name.Map.add_multi known_keys name (T key)
          , Univ_map.set table key value )
      | Declared_notifs ->
        let name, key = key in
        let known_keys, table = t.declared_notifications in
        t.declared_notifications <-
          ( Method_name.Map.add_multi known_keys name (T key)
          , Univ_map.set table key value )
      | Impl_requests -> Method_name.Table.set t.implemented_requests key value
      | Impl_notifs ->
        Method_name.Table.set t.implemented_notifications key value

    let registered_procedures
        { declared_requests = declared_request_keys, declared_request_table
        ; declared_notifications =
            declared_notification_keys, declared_notification_table
        ; implemented_requests
        ; implemented_notifications
        } =
      let batch_declarations which declared_keys declaration_table =
        Method_name.Map.foldi declared_keys ~init:[] ~f:(fun name keys acc ->
            let generations =
              List.fold_left keys ~init:[] ~f:(fun acc (T key) ->
                  match Univ_map.find declaration_table key with
                  | None ->
                    Code_error.raise
                      "versioning: method found in versioning table without \
                       actually being declared"
                      [ ("method_", Dyn.String name)
                      ; ("table", Dyn.String ("known_" ^ which ^ "_table"))
                      ]
                  | Some listing -> Method_version.Map.keys listing @ acc)
            in
            (name, generations) :: acc)
      in
      let declared_requests =
        batch_declarations "request" declared_request_keys
          declared_request_table
      in
      let declared_notifications =
        batch_declarations "notification" declared_notification_keys
          declared_notification_table
      in
      let batch_implementations table =
        Method_name.Table.foldi table ~init:[] ~f:(fun name listing acc ->
            (name, Method_version.Map.keys listing) :: acc)
      in
      let implemented_requests = batch_implementations implemented_requests in
      let implemented_notifications =
        batch_implementations implemented_notifications
      in
      List.concat
        [ declared_requests
        ; declared_notifications
        ; implemented_requests
        ; implemented_notifications
        ]

    let create () =
      let declared_requests = (Method_name.Map.empty, Univ_map.empty) in
      let declared_notifications = (Method_name.Map.empty, Univ_map.empty) in
      let implemented_requests = Method_name.Table.create 16 in
      let implemented_notifications = Method_name.Table.create 16 in
      { declared_requests
      ; declared_notifications
      ; implemented_requests
      ; implemented_notifications
      }

    let register_generic t ~method_ ~generations ~registry ~registry_key ~other
        ~other_key ~pack =
      let () =
        match get t other other_key with
        | None -> ()
        | Some _ ->
          Code_error.raise "attempted to implement and declare method"
            [ ("method", Dyn.String method_) ]
      in
      let prior_registered_generations =
        match get t registry registry_key with
        | None -> Method_version.Map.empty
        | Some s -> s
      in
      let all_generations, duplicate_generations =
        List.fold_left generations
          ~init:(prior_registered_generations, Method_version.Set.empty)
          ~f:(fun (acc, dups) (n, gen) ->
            match Method_version.Map.add acc n (pack gen) with
            | Error _ -> (acc, Method_version.Set.add dups n)
            | Ok acc' -> (acc', dups))
      in
      if Method_version.Set.is_empty duplicate_generations then
        set t registry registry_key all_generations
      else
        Code_error.raise
          "attempted to register duplicate generations for RPC method"
          [ ("method", Dyn.String method_)
          ; ("duplicated", Method_version.Set.to_dyn duplicate_generations)
          ]

    let declare_request t proc =
      register_generic t ~method_:proc.Request.decl.method_
        ~generations:proc.Request.generations ~registry:Declared_requests
        ~other:Impl_requests
        ~registry_key:(proc.Request.decl.method_, proc.Request.decl.key)
        ~other_key:proc.Request.decl.method_ ~pack:(fun rc -> rc)

    let declare_notification t (proc : _ notification) =
      register_generic t ~method_:proc.decl.method_
        ~generations:proc.generations ~registry:Declared_notifs
        ~other:Impl_notifs ~registry_key:(proc.decl.method_, proc.decl.key)
        ~other_key:proc.decl.method_ ~pack:(fun nc -> nc)

    let implement_request t (proc : _ request) f =
      register_generic t ~method_:proc.decl.method_
        ~generations:proc.generations ~registry:Impl_requests
        ~other:Declared_requests ~registry_key:proc.decl.method_
        ~other_key:(proc.decl.method_, proc.decl.key) ~pack:(fun r -> R (f, r))

    let implement_notification t (proc : _ notification) f =
      register_generic t ~method_:proc.decl.method_
        ~generations:proc.generations ~registry:Impl_notifs
        ~other:Declared_notifs ~registry_key:proc.decl.method_
        ~other_key:(proc.decl.method_, proc.decl.key) ~pack:(fun n -> N (f, n))

    let lookup_method_generic t ~menu ~table ~key ~method_ k s =
      match (get t table key, Method_name.Map.find menu method_) with
      | None, _ ->
        let payload = Sexp.record [ ("method", Atom method_) ] in
        k (Version_error.create ~message:"invalid method" ~payload ())
      | _, None ->
        let payload = Sexp.record [ ("method", Atom method_) ] in
        k
          (Version_error.create
             ~message:"remote and local have no common version for method"
             ~payload ())
      | Some subtable, Some version -> s (subtable, version)

    let raise_version_bug ~method_ ~selected ~verb ~known =
      Code_error.raise
        "bug with version negotiation; selected bad method version"
        [ ("message", Dyn.String ("version is " ^ verb))
        ; ("method", Dyn.String method_)
        ; ( "implemented versions"
          , Dyn.List (List.map ~f:(fun i -> Dyn.Int i) known) )
        ; ("selected version", Dyn.Int selected)
        ]

    let to_handler t ~session_version =
      let open Fiber.O in
      let handle_request menu state (_id, (n : Call.t)) =
        lookup_method_generic t ~menu ~table:Impl_requests ~key:n.method_
          ~method_:n.method_
          (fun e -> Fiber.return (Error (Version_error.to_response_error e)))
          (fun (handlers, version) ->
            match Method_version.Map.find handlers version with
            | None ->
              raise_version_bug ~method_:n.method_ ~selected:version
                ~verb:"unimplemented"
                ~known:(Method_version.Map.keys handlers)
            | Some (R (f, T gen)) -> (
              match
                Conv.of_sexp gen.req ~version:(session_version state) n.params
              with
              | Error e -> Fiber.return (Error (Response.Error.of_conv e))
              | Ok req ->
                let+ resp = f state (gen.upgrade_req req) in
                Ok (Conv.to_sexp gen.resp (gen.downgrade_resp resp))))
      in
      let handle_notification menu state (n : Call.t) =
        lookup_method_generic t ~menu ~table:Impl_notifs ~key:n.method_
          ~method_:n.method_
          (fun e -> Fiber.return (Error (Version_error.to_response_error e)))
          (fun (handlers, version) ->
            match Method_version.Map.find handlers version with
            | None ->
              raise_version_bug ~method_:n.method_ ~selected:version
                ~verb:"unimplemented"
                ~known:(Method_version.Map.keys handlers)
            | Some (N (f, T gen)) -> (
              match
                Conv.of_sexp gen.req ~version:(session_version state) n.params
              with
              | Error e -> Fiber.return (Error (Response.Error.of_conv e))
              | Ok req ->
                let+ () = f state (gen.upgrade_req req) in
                Ok ()))
      in
      let prepare_request (type a b) menu (decl : (a, b) Decl.Request.witness) :
          ((a, b) Staged.request, Version_error.t) result =
        let method_ = decl.Decl.method_ in
        lookup_method_generic t ~menu ~table:Declared_requests
          ~key:(method_, decl.key) ~method_
          (fun e -> Error e)
          (fun (decls, version) ->
            match Method_version.Map.find decls version with
            | None ->
              raise_version_bug ~method_ ~selected:version ~verb:"undeclared"
                ~known:(Method_version.Map.keys decls)
            | Some (T gen) ->
              let encode_req (req : a) =
                { Call.method_
                ; params = Conv.to_sexp gen.req (gen.downgrade_req req)
                }
              in
              let decode_resp sexp =
                match Conv.of_sexp gen.resp ~version:(3, 0) sexp with
                | Ok resp -> Ok (gen.upgrade_resp resp)
                | Error e -> Error (Response.Error.of_conv e)
              in
              Ok { Staged.encode_req; decode_resp })
      in
      let prepare_notification (type a) menu
          (decl : a Decl.Notification.witness) :
          (a Staged.notification, Version_error.t) result =
        let method_ = decl.Decl.method_ in
        lookup_method_generic t ~menu ~table:Declared_notifs
          ~key:(method_, decl.key) ~method_
          (fun e -> Error e)
          (fun (decls, version) ->
            match Method_version.Map.find decls version with
            | None ->
              raise_version_bug ~method_ ~selected:version ~verb:"undeclared"
                ~known:(Method_version.Map.keys decls)
            | Some (T gen) ->
              let encode (req : a) =
                { Call.method_
                ; params = Conv.to_sexp gen.req (gen.downgrade_req req)
                }
              in
              Ok { Staged.encode })
      in
      fun ~menu ->
        { Handler.menu
        ; handle_request
        ; handle_notification
        ; prepare_request
        ; prepare_notification
        }
  end
end
OCaml

Innovation. Community. Security.