Skip to content

Commit

Permalink
Add a cache system
Browse files Browse the repository at this point in the history
The response of a request is cached and it is invalidated every time one
of then packages of the new request is involve between the new opam
commit and the old one (cached opam commit).

Anytime the cached response is the same as the new response, we keep the
old one for the oldest opam commit.
  • Loading branch information
moyodiallo committed Dec 1, 2023
1 parent 48a157e commit b109239
Show file tree
Hide file tree
Showing 17 changed files with 599 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "scache"]
path = scache
url = https://github.com/moyodiallo/scache.git
branch = master
19 changes: 14 additions & 5 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,25 @@ let start_server ~service vat_config =
let+ vat = Capnp_rpc_unix.serve vat_config ~restore in
Capnp_rpc_unix.Vat.sturdy_uri vat service_id

let main_service () solver cap_file vat_config =
let uri = start_server ~service:(Solver_service.Service.v solver) vat_config in
let main_service () solver cacheable cap_file vat_config =
let uri = start_server ~service:(Solver_service.Service.v ~cacheable solver) vat_config in
Capnp_rpc_unix.Cap_file.save_uri uri cap_file |> or_die;
Fmt.pr "Wrote solver service's address to %S@." cap_file;
Fiber.await_cancel ()

let main_service_pipe () solver =
let main_service_pipe () solver cacheable =
let socket = Lwt_unix.stdin in
(* Run locally reading from socket *)
export (Solver_service.Service.v solver) ~on:socket
export (Solver_service.Service.v ~cacheable solver) ~on:socket

let main_cluster () solver name capacity register_addr =
let main_cluster () solver cacheable name capacity register_addr =
let vat = Capnp_rpc_unix.client_only_vat () in
let sr = Capnp_rpc_unix.Vat.import_exn vat register_addr in
let `Cancelled =
Solver_worker.run solver sr
~name
~capacity
~cacheable
in
()

Expand Down Expand Up @@ -128,6 +129,11 @@ let capacity =
@@ Arg.info ~doc:"The number of cluster jobs that can run in parallel" ~docv:"N"
[ "capacity" ]

let cacheable =
Arg.value
@@ Arg.flag
@@ Arg.info ~doc:"Activate the cache system" [ "activate-cache"; "cache" ]

let cap_file =
Arg.required
@@ Arg.opt Arg.(some string) None
Expand Down Expand Up @@ -168,6 +174,7 @@ let () =
const main_service
$ setup_log
$ solver
$ cacheable
$ cap_file
$ Capnp_rpc_unix.Vat_config.cmd
)
Expand All @@ -179,6 +186,7 @@ let () =
const main_service_pipe
$ setup_log
$ solver
$ cacheable
)
in
let run_agent =
Expand All @@ -188,6 +196,7 @@ let () =
const main_cluster
$ setup_log
$ solver
$ cacheable
$ worker_name
$ capacity
$ register_addr
Expand Down
1 change: 1 addition & 0 deletions scache
Submodule scache added at 265c82
5 changes: 4 additions & 1 deletion service/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
solver-service-api
opam-0install
capnp-rpc-net
opam-file-format
git-unix
ocaml-version))
scache
ocaml-version)
(preprocess (pps ppx_deriving_yojson)))
4 changes: 2 additions & 2 deletions service/service.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Worker = Solver_service_api.Worker

let v t =
let v ?cacheable t =
let open Capnp_rpc_lwt in
let module X = Solver_service_api.Raw.Service.Solver in
X.local
Expand All @@ -27,7 +27,7 @@ let v t =
(Capnp_rpc.Error.exn "Bad JSON in request: %s" msg))
| Ok request ->
Lwt_eio.run_eio @@ fun () ->
let selections = Solver.solve t ~log request in
let selections = Solver.solve ?cacheable t ~log request in
let json =
Yojson.Safe.to_string
(Worker.Solve_response.to_yojson selections)
Expand Down
2 changes: 1 addition & 1 deletion service/service.mli
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
val v : Solver.t -> Solver_service_api.Solver.t
val v : ?cacheable:bool -> Solver.t -> Solver_service_api.Solver.t
(** [capnp_service t] is a Cap'n Proto service that handles requests using [t]. *)
Loading

0 comments on commit b109239

Please sign in to comment.