2017-02-24 20:17:53 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2017-02-24 20:17:53 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
module type DISTRIBUTED_DB = sig
|
2017-03-28 15:31:41 +04:00
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
type t
|
|
|
|
type key
|
|
|
|
type value
|
2017-03-28 15:31:41 +04:00
|
|
|
type param
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
val known: t -> key -> bool Lwt.t
|
2017-04-19 23:46:10 +04:00
|
|
|
|
|
|
|
type error += Missing_data of key
|
2017-11-08 15:42:09 +04:00
|
|
|
type error += Canceled of key
|
2017-11-08 15:52:10 +04:00
|
|
|
type error += Timeout of key
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
val read: t -> key -> value tzresult Lwt.t
|
|
|
|
val read_opt: t -> key -> value option Lwt.t
|
2017-02-24 20:17:53 +04:00
|
|
|
val read_exn: t -> key -> value Lwt.t
|
2017-03-28 15:31:41 +04:00
|
|
|
|
2017-11-10 23:30:29 +04:00
|
|
|
val prefetch:
|
|
|
|
t ->
|
2018-01-24 15:48:25 +04:00
|
|
|
?peer:P2p_peer.Id.t ->
|
2017-11-10 23:30:29 +04:00
|
|
|
?timeout:float ->
|
|
|
|
key -> param -> unit
|
|
|
|
|
2017-11-08 15:42:09 +04:00
|
|
|
val fetch:
|
2017-11-08 15:52:10 +04:00
|
|
|
t ->
|
2018-01-24 15:48:25 +04:00
|
|
|
?peer:P2p_peer.Id.t ->
|
2017-11-08 15:52:10 +04:00
|
|
|
?timeout:float ->
|
|
|
|
key -> param -> value tzresult Lwt.t
|
2017-03-28 15:31:41 +04:00
|
|
|
|
2017-11-06 18:23:06 +04:00
|
|
|
val clear_or_cancel: t -> key -> unit
|
2017-02-24 20:17:53 +04:00
|
|
|
val inject: t -> key -> value -> bool Lwt.t
|
2017-11-27 09:13:12 +04:00
|
|
|
val watch: t -> (key * value) Lwt_stream.t * Lwt_watcher.stopper
|
2017-03-28 15:31:41 +04:00
|
|
|
|
2017-11-14 05:41:37 +04:00
|
|
|
val pending: t -> key -> bool
|
|
|
|
|
2017-03-28 15:31:41 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
module type DISK_TABLE = sig
|
|
|
|
type store
|
|
|
|
type key
|
|
|
|
type value
|
|
|
|
val known: store -> key -> bool Lwt.t
|
|
|
|
val read: store -> key -> value tzresult Lwt.t
|
|
|
|
val read_opt: store -> key -> value option Lwt.t
|
|
|
|
val read_exn: store -> key -> value Lwt.t
|
|
|
|
end
|
|
|
|
|
|
|
|
module type MEMORY_TABLE = sig
|
|
|
|
type 'a t
|
|
|
|
type key
|
|
|
|
val create: int -> 'a t
|
|
|
|
val find: 'a t -> key -> 'a
|
|
|
|
val add: 'a t -> key -> 'a -> unit
|
|
|
|
val replace: 'a t -> key -> 'a -> unit
|
|
|
|
val remove: 'a t -> key -> unit
|
|
|
|
val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
module type SCHEDULER_EVENTS = sig
|
|
|
|
type t
|
|
|
|
type key
|
2018-01-24 15:48:25 +04:00
|
|
|
val request: t -> P2p_peer.Id.t option -> key -> unit
|
|
|
|
val notify: t -> P2p_peer.Id.t -> key -> unit
|
2017-11-06 18:23:06 +04:00
|
|
|
val notify_cancelation: t -> key -> unit
|
2018-01-24 15:48:25 +04:00
|
|
|
val notify_unrequested: t -> P2p_peer.Id.t -> key -> unit
|
|
|
|
val notify_duplicate: t -> P2p_peer.Id.t -> key -> unit
|
|
|
|
val notify_invalid: t -> P2p_peer.Id.t -> key -> unit
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
|
|
|
|
2017-03-28 15:31:41 +04:00
|
|
|
module type PRECHECK = sig
|
|
|
|
type key
|
|
|
|
type param
|
2017-04-19 23:46:10 +04:00
|
|
|
type notified_value
|
2017-03-28 15:31:41 +04:00
|
|
|
type value
|
2017-04-19 23:46:10 +04:00
|
|
|
val precheck: key -> param -> notified_value -> value option
|
2017-03-28 15:31:41 +04:00
|
|
|
end
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-03-28 15:31:41 +04:00
|
|
|
module Make_table
|
2017-07-21 18:16:39 +04:00
|
|
|
(Hash : sig
|
|
|
|
type t
|
|
|
|
val name : string
|
|
|
|
val encoding : t Data_encoding.t
|
|
|
|
val pp : Format.formatter -> t -> unit
|
|
|
|
end)
|
2017-03-28 15:31:41 +04:00
|
|
|
(Disk_table : DISK_TABLE with type key := Hash.t)
|
|
|
|
(Memory_table : MEMORY_TABLE with type key := Hash.t)
|
|
|
|
(Scheduler : SCHEDULER_EVENTS with type key := Hash.t)
|
|
|
|
(Precheck : PRECHECK with type key := Hash.t
|
|
|
|
and type value := Disk_table.value) : sig
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
include DISTRIBUTED_DB with type key = Hash.t
|
|
|
|
and type value = Disk_table.value
|
|
|
|
and type param = Precheck.param
|
2017-02-24 20:17:53 +04:00
|
|
|
val create:
|
2017-11-27 09:13:12 +04:00
|
|
|
?global_input:(key * value) Lwt_watcher.input ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Scheduler.t -> Disk_table.store -> t
|
2018-01-24 15:48:25 +04:00
|
|
|
val notify: t -> P2p_peer.Id.t -> key -> Precheck.notified_value -> unit Lwt.t
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
end = struct
|
|
|
|
|
|
|
|
type key = Hash.t
|
|
|
|
type value = Disk_table.value
|
2017-03-28 15:31:41 +04:00
|
|
|
type param = Precheck.param
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
type t = {
|
|
|
|
scheduler: Scheduler.t ;
|
|
|
|
disk: Disk_table.store ;
|
|
|
|
memory: status Memory_table.t ;
|
2017-11-27 09:13:12 +04:00
|
|
|
global_input: (key * value) Lwt_watcher.input option ;
|
|
|
|
input: (key * value) Lwt_watcher.input ;
|
2017-02-24 20:17:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
and status =
|
2017-12-29 18:29:29 +04:00
|
|
|
| Pending of { waiter : value tzresult Lwt.t ;
|
|
|
|
wakener : value tzresult Lwt.u ;
|
2017-11-08 14:59:05 +04:00
|
|
|
mutable waiters : int ;
|
2017-11-08 14:47:08 +04:00
|
|
|
param : param }
|
2017-02-24 20:17:53 +04:00
|
|
|
| Found of value
|
|
|
|
|
|
|
|
let known s k =
|
|
|
|
match Memory_table.find s.memory k with
|
|
|
|
| exception Not_found -> Disk_table.known s.disk k
|
|
|
|
| Pending _ -> Lwt.return_false
|
|
|
|
| Found _ -> Lwt.return_true
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
let read_opt s k =
|
2017-02-24 20:17:53 +04:00
|
|
|
match Memory_table.find s.memory k with
|
|
|
|
| exception Not_found -> Disk_table.read_opt s.disk k
|
|
|
|
| Found v -> Lwt.return (Some v)
|
|
|
|
| Pending _ -> Lwt.return_none
|
|
|
|
|
|
|
|
let read_exn s k =
|
|
|
|
match Memory_table.find s.memory k with
|
|
|
|
| exception Not_found -> Disk_table.read_exn s.disk k
|
|
|
|
| Found v -> Lwt.return v
|
|
|
|
| Pending _ -> Lwt.fail Not_found
|
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
type error += Missing_data of key
|
2017-11-08 15:42:09 +04:00
|
|
|
type error += Canceled of key
|
2017-11-08 15:52:10 +04:00
|
|
|
type error += Timeout of key
|
2017-04-19 23:46:10 +04:00
|
|
|
|
2017-07-21 18:16:39 +04:00
|
|
|
let () =
|
2018-02-13 17:12:09 +04:00
|
|
|
(* Missing data key *)
|
|
|
|
register_error_kind
|
|
|
|
`Permanent
|
2017-07-21 18:16:39 +04:00
|
|
|
~id: ("distributed_db." ^ Hash.name ^ ".missing")
|
|
|
|
~title: ("Missing " ^ Hash.name)
|
|
|
|
~description: ("Some " ^ Hash.name ^ " is missing from the distributed db")
|
|
|
|
~pp: (fun ppf key ->
|
|
|
|
Format.fprintf ppf "Missing %s %a" Hash.name Hash.pp key)
|
|
|
|
(Data_encoding.obj1 (Data_encoding.req "key" Hash.encoding))
|
|
|
|
(function Missing_data key -> Some key | _ -> None)
|
2017-11-10 12:55:25 +04:00
|
|
|
(fun key -> Missing_data key) ;
|
2018-02-13 17:12:09 +04:00
|
|
|
(* Canceled key *)
|
2017-11-10 12:55:25 +04:00
|
|
|
register_error_kind
|
|
|
|
`Permanent
|
|
|
|
~title: ("Canceled fetch of a " ^ Hash.name)
|
|
|
|
~description: ("The fetch of a " ^ Hash.name ^ " has been canceled")
|
|
|
|
~id: ("distributed_db." ^ Hash.name ^ ".fetch_canceled")
|
|
|
|
~pp: (fun ppf key ->
|
|
|
|
Format.fprintf ppf "Fetch of %s %a canceled" Hash.name Hash.pp key)
|
|
|
|
Data_encoding.(obj1 (req "key" Hash.encoding))
|
|
|
|
(function (Canceled key) -> Some key | _ -> None)
|
|
|
|
(fun key -> Canceled key) ;
|
2018-02-13 17:12:09 +04:00
|
|
|
(* Timeout key *)
|
2017-11-10 12:55:25 +04:00
|
|
|
register_error_kind
|
|
|
|
`Permanent
|
|
|
|
~title: ("Timed out fetch of a " ^ Hash.name)
|
|
|
|
~description: ("The fetch of a " ^ Hash.name ^ " has timed out")
|
|
|
|
~id: ("distributed_db." ^ Hash.name ^ ".fetch_timeout")
|
|
|
|
~pp: (fun ppf key ->
|
|
|
|
Format.fprintf ppf "Fetch of %s %a timed out" Hash.name Hash.pp key)
|
|
|
|
Data_encoding.(obj1 (req "key" Hash.encoding))
|
|
|
|
(function (Timeout key) -> Some key | _ -> None)
|
|
|
|
(fun key -> Timeout key)
|
2017-07-21 18:16:39 +04:00
|
|
|
|
2017-04-19 23:46:10 +04:00
|
|
|
let read s k =
|
|
|
|
match Memory_table.find s.memory k with
|
|
|
|
| exception Not_found ->
|
|
|
|
trace (Missing_data k) @@
|
|
|
|
Disk_table.read s.disk k
|
|
|
|
| Found v -> return v
|
|
|
|
| Pending _ -> fail (Missing_data k)
|
|
|
|
|
2017-11-08 15:52:10 +04:00
|
|
|
let wrap s k ?timeout t =
|
2017-11-08 14:59:05 +04:00
|
|
|
let t = Lwt.protected t in
|
|
|
|
Lwt.on_cancel t begin fun () ->
|
|
|
|
match Memory_table.find s.memory k with
|
|
|
|
| exception Not_found -> ()
|
|
|
|
| Found _ -> ()
|
|
|
|
| Pending data ->
|
|
|
|
data.waiters <- data.waiters - 1 ;
|
|
|
|
if data.waiters = 0 then begin
|
|
|
|
Memory_table.remove s.memory k ;
|
|
|
|
Scheduler.notify_cancelation s.scheduler k ;
|
|
|
|
end
|
|
|
|
end ;
|
2017-11-08 15:52:10 +04:00
|
|
|
match timeout with
|
|
|
|
| None -> t
|
|
|
|
| Some delay ->
|
|
|
|
let timeout =
|
|
|
|
Lwt_unix.sleep delay >>= fun () -> fail (Timeout k) in
|
|
|
|
Lwt.pick [ t ; timeout ]
|
|
|
|
|
|
|
|
let fetch s ?peer ?timeout k param =
|
2017-02-24 20:17:53 +04:00
|
|
|
match Memory_table.find s.memory k with
|
|
|
|
| exception Not_found -> begin
|
|
|
|
Disk_table.read_opt s.disk k >>= function
|
2017-11-08 15:42:09 +04:00
|
|
|
| Some v -> return v
|
2017-09-29 20:43:13 +04:00
|
|
|
| None ->
|
|
|
|
match Memory_table.find s.memory k with
|
|
|
|
| exception Not_found -> begin
|
|
|
|
let waiter, wakener = Lwt.wait () in
|
2017-11-08 14:59:05 +04:00
|
|
|
Memory_table.add s.memory k
|
2017-12-29 18:29:29 +04:00
|
|
|
(Pending { waiter ; wakener ; waiters = 1 ; param }) ;
|
2017-09-29 20:43:13 +04:00
|
|
|
Scheduler.request s.scheduler peer k ;
|
2017-11-08 15:52:10 +04:00
|
|
|
wrap s k ?timeout waiter
|
2017-09-29 20:43:13 +04:00
|
|
|
end
|
2017-11-08 14:59:05 +04:00
|
|
|
| Pending data ->
|
2017-09-29 20:43:13 +04:00
|
|
|
Scheduler.request s.scheduler peer k ;
|
2017-11-08 14:59:05 +04:00
|
|
|
data.waiters <- data.waiters + 1 ;
|
2017-12-29 18:29:29 +04:00
|
|
|
wrap s k ?timeout data.waiter
|
2017-11-08 15:42:09 +04:00
|
|
|
| Found v -> return v
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
2017-11-08 14:59:05 +04:00
|
|
|
| Pending data ->
|
2017-09-29 20:43:13 +04:00
|
|
|
Scheduler.request s.scheduler peer k ;
|
2017-11-08 14:59:05 +04:00
|
|
|
data.waiters <- data.waiters + 1 ;
|
2017-12-29 18:29:29 +04:00
|
|
|
wrap s k ?timeout data.waiter
|
2017-11-08 15:42:09 +04:00
|
|
|
| Found v -> return v
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-11-10 23:30:29 +04:00
|
|
|
let prefetch s ?peer ?timeout k param =
|
|
|
|
try ignore (fetch s ?peer ?timeout k param) with _ -> ()
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
let notify s p k v =
|
|
|
|
match Memory_table.find s.memory k with
|
|
|
|
| exception Not_found -> begin
|
|
|
|
Disk_table.known s.disk k >>= function
|
|
|
|
| true ->
|
|
|
|
Scheduler.notify_duplicate s.scheduler p k ;
|
|
|
|
Lwt.return_unit
|
|
|
|
| false ->
|
|
|
|
Scheduler.notify_unrequested s.scheduler p k ;
|
|
|
|
Lwt.return_unit
|
|
|
|
end
|
2017-11-08 14:47:08 +04:00
|
|
|
| Pending { wakener = w ; param } -> begin
|
2017-04-19 23:46:10 +04:00
|
|
|
match Precheck.precheck k param v with
|
|
|
|
| None ->
|
|
|
|
Scheduler.notify_invalid s.scheduler p k ;
|
|
|
|
Lwt.return_unit
|
|
|
|
| Some v ->
|
|
|
|
Scheduler.notify s.scheduler p k ;
|
|
|
|
Memory_table.replace s.memory k (Found v) ;
|
2017-11-08 15:42:09 +04:00
|
|
|
Lwt.wakeup_later w (Ok v) ;
|
2017-11-27 09:13:12 +04:00
|
|
|
Option.iter s.global_input
|
|
|
|
~f:(fun input -> Lwt_watcher.notify input (k, v)) ;
|
|
|
|
Lwt_watcher.notify s.input (k, v) ;
|
2017-04-19 23:46:10 +04:00
|
|
|
Lwt.return_unit
|
2017-11-13 19:34:00 +04:00
|
|
|
end
|
2017-02-24 20:17:53 +04:00
|
|
|
| Found _ ->
|
|
|
|
Scheduler.notify_duplicate s.scheduler p k ;
|
|
|
|
Lwt.return_unit
|
|
|
|
|
|
|
|
let inject s k v =
|
|
|
|
match Memory_table.find s.memory k with
|
|
|
|
| exception Not_found -> begin
|
|
|
|
Disk_table.known s.disk k >>= function
|
|
|
|
| true ->
|
|
|
|
Lwt.return_false
|
|
|
|
| false ->
|
|
|
|
Memory_table.add s.memory k (Found v) ;
|
|
|
|
Lwt.return_true
|
|
|
|
end
|
|
|
|
| Pending _
|
|
|
|
| Found _ ->
|
|
|
|
Lwt.return_false
|
|
|
|
|
2017-11-06 18:23:06 +04:00
|
|
|
let clear_or_cancel s k =
|
2017-02-24 20:17:53 +04:00
|
|
|
match Memory_table.find s.memory k with
|
2017-06-09 19:54:08 +04:00
|
|
|
| exception Not_found -> ()
|
2017-11-08 14:47:08 +04:00
|
|
|
| Pending { wakener = w ; _ } ->
|
2017-11-06 18:23:06 +04:00
|
|
|
Scheduler.notify_cancelation s.scheduler k ;
|
|
|
|
Memory_table.remove s.memory k ;
|
2017-11-08 15:42:09 +04:00
|
|
|
Lwt.wakeup_later w (Error [Canceled k])
|
2017-06-09 19:54:08 +04:00
|
|
|
| Found _ -> Memory_table.remove s.memory k
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
let watch s = Lwt_watcher.create_stream s.input
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
let create ?global_input scheduler disk =
|
|
|
|
let memory = Memory_table.create 17 in
|
2017-11-27 09:13:12 +04:00
|
|
|
let input = Lwt_watcher.create_input () in
|
2017-02-24 20:17:53 +04:00
|
|
|
{ scheduler ; disk ; memory ; input ; global_input }
|
|
|
|
|
2017-11-14 05:41:37 +04:00
|
|
|
let pending s k =
|
|
|
|
match Memory_table.find s.memory k with
|
|
|
|
| exception Not_found -> false
|
|
|
|
| Found _ -> false
|
|
|
|
| Pending _ -> true
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
module type REQUEST = sig
|
|
|
|
type key
|
|
|
|
type param
|
2018-01-24 15:48:25 +04:00
|
|
|
val active : param -> P2p_peer.Set.t
|
|
|
|
val send : param -> P2p_peer.Id.t -> key list -> unit
|
2017-02-24 20:17:53 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
module Make_request_scheduler
|
2017-09-29 20:43:13 +04:00
|
|
|
(Hash : sig
|
|
|
|
type t
|
|
|
|
val name : string
|
|
|
|
val encoding : t Data_encoding.t
|
|
|
|
val pp : Format.formatter -> t -> unit
|
|
|
|
end)
|
2017-03-28 15:31:41 +04:00
|
|
|
(Table : MEMORY_TABLE with type key := Hash.t)
|
2017-02-24 20:17:53 +04:00
|
|
|
(Request : REQUEST with type key := Hash.t) : sig
|
|
|
|
|
|
|
|
type t
|
|
|
|
val create: Request.param -> t
|
|
|
|
val shutdown: t -> unit Lwt.t
|
|
|
|
include SCHEDULER_EVENTS with type t := t and type key := Hash.t
|
|
|
|
|
|
|
|
end = struct
|
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
include Logging.Make(struct let name = "node.distributed_db.scheduler." ^ Hash.name end)
|
|
|
|
|
2017-02-24 20:17:53 +04:00
|
|
|
type key = Hash.t
|
|
|
|
|
|
|
|
type t = {
|
2017-11-08 15:06:20 +04:00
|
|
|
param: Request.param ;
|
|
|
|
pending: status Table.t ;
|
|
|
|
|
|
|
|
queue: event Lwt_pipe.t ;
|
|
|
|
mutable events: event list Lwt.t ;
|
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
canceler: Lwt_canceler.t ;
|
2017-11-08 15:06:20 +04:00
|
|
|
mutable worker: unit Lwt.t ;
|
|
|
|
}
|
|
|
|
|
|
|
|
and status = {
|
2018-01-24 15:48:25 +04:00
|
|
|
peers: P2p_peer.Set.t ;
|
2017-11-08 15:06:20 +04:00
|
|
|
next_request: float ;
|
|
|
|
delay: float ;
|
2017-02-24 20:17:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
and event =
|
2018-01-24 15:48:25 +04:00
|
|
|
| Request of P2p_peer.Id.t option * key
|
|
|
|
| Notify of P2p_peer.Id.t * key
|
2017-11-06 18:23:06 +04:00
|
|
|
| Notify_cancelation of key
|
2018-01-24 15:48:25 +04:00
|
|
|
| Notify_invalid of P2p_peer.Id.t * key
|
|
|
|
| Notify_duplicate of P2p_peer.Id.t * key
|
|
|
|
| Notify_unrequested of P2p_peer.Id.t * key
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
let request t p k =
|
2017-11-08 15:06:20 +04:00
|
|
|
assert (Lwt_pipe.push_now t.queue (Request (p, k)))
|
2017-02-24 20:17:53 +04:00
|
|
|
let notify t p k =
|
2017-09-29 20:43:13 +04:00
|
|
|
debug "push received %a from %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
Hash.pp k P2p_peer.Id.pp_short p ;
|
2017-11-08 15:06:20 +04:00
|
|
|
assert (Lwt_pipe.push_now t.queue (Notify (p, k)))
|
2017-11-06 18:23:06 +04:00
|
|
|
let notify_cancelation t k =
|
|
|
|
debug "push cancelation %a"
|
|
|
|
Hash.pp k ;
|
2017-11-08 15:06:20 +04:00
|
|
|
assert (Lwt_pipe.push_now t.queue (Notify_cancelation k))
|
2017-03-28 15:31:41 +04:00
|
|
|
let notify_invalid t p k =
|
2017-09-29 20:43:13 +04:00
|
|
|
debug "push received invalid %a from %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
Hash.pp k P2p_peer.Id.pp_short p ;
|
2017-11-08 15:06:20 +04:00
|
|
|
assert (Lwt_pipe.push_now t.queue (Notify_invalid (p, k)))
|
2017-02-24 20:17:53 +04:00
|
|
|
let notify_duplicate t p k =
|
2017-09-29 20:43:13 +04:00
|
|
|
debug "push received duplicate %a from %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
Hash.pp k P2p_peer.Id.pp_short p ;
|
2017-11-08 15:06:20 +04:00
|
|
|
assert (Lwt_pipe.push_now t.queue (Notify_duplicate (p, k)))
|
2017-02-24 20:17:53 +04:00
|
|
|
let notify_unrequested t p k =
|
2017-09-29 20:43:13 +04:00
|
|
|
debug "push received unrequested %a from %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
Hash.pp k P2p_peer.Id.pp_short p ;
|
2017-11-08 15:06:20 +04:00
|
|
|
assert (Lwt_pipe.push_now t.queue (Notify_unrequested (p, k)))
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
let compute_timeout state =
|
|
|
|
let next =
|
|
|
|
Table.fold
|
2017-11-30 17:50:36 +04:00
|
|
|
(fun _ { next_request } acc ->
|
|
|
|
match acc with
|
|
|
|
| None -> Some next_request
|
|
|
|
| Some x -> Some (min x next_request))
|
|
|
|
state.pending None in
|
|
|
|
match next with
|
|
|
|
| None -> fst @@ Lwt.task ()
|
|
|
|
| Some next ->
|
|
|
|
let now = Unix.gettimeofday () in
|
|
|
|
let delay = next -. now in
|
|
|
|
if delay <= 0. then Lwt.return_unit else begin
|
|
|
|
(* lwt_debug "waiting at least %.2fs" delay >>= fun () -> *)
|
|
|
|
Lwt_unix.sleep delay
|
|
|
|
end
|
2017-09-29 20:43:13 +04:00
|
|
|
|
|
|
|
let may_pp_peer ppf = function
|
|
|
|
| None -> ()
|
2018-01-24 15:48:25 +04:00
|
|
|
| Some peer -> P2p_peer.Id.pp_short ppf peer
|
2017-02-24 20:17:53 +04:00
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
(* TODO should depend on the ressource kind... *)
|
|
|
|
let initial_delay = 0.1
|
|
|
|
|
|
|
|
let process_event state now = function
|
2017-02-24 20:17:53 +04:00
|
|
|
| Request (peer, key) -> begin
|
2017-09-29 20:43:13 +04:00
|
|
|
lwt_debug "registering request %a from %a"
|
|
|
|
Hash.pp key may_pp_peer peer >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
try
|
|
|
|
let data = Table.find state.pending key in
|
|
|
|
let peers =
|
|
|
|
match peer with
|
|
|
|
| None -> data.peers
|
2018-01-24 15:48:25 +04:00
|
|
|
| Some peer -> P2p_peer.Set.add peer data.peers in
|
2017-09-29 20:43:13 +04:00
|
|
|
Table.replace state.pending key {
|
|
|
|
delay = initial_delay ;
|
|
|
|
next_request = min data.next_request (now +. initial_delay) ;
|
|
|
|
peers ;
|
|
|
|
} ;
|
2017-09-29 20:43:13 +04:00
|
|
|
lwt_debug "registering request %a from %a -> replaced"
|
|
|
|
Hash.pp key may_pp_peer peer >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Lwt.return_unit
|
|
|
|
with Not_found ->
|
|
|
|
let peers =
|
|
|
|
match peer with
|
2018-01-24 15:48:25 +04:00
|
|
|
| None -> P2p_peer.Set.empty
|
|
|
|
| Some peer -> P2p_peer.Set.singleton peer in
|
2017-02-24 20:17:53 +04:00
|
|
|
Table.add state.pending key {
|
|
|
|
peers ;
|
2017-09-29 20:43:13 +04:00
|
|
|
next_request = now ;
|
|
|
|
delay = initial_delay ;
|
2017-02-24 20:17:53 +04:00
|
|
|
} ;
|
2017-09-29 20:43:13 +04:00
|
|
|
lwt_debug "registering request %a from %a -> added"
|
|
|
|
Hash.pp key may_pp_peer peer >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Lwt.return_unit
|
|
|
|
end
|
2017-09-29 20:43:13 +04:00
|
|
|
| Notify (peer, key) ->
|
2017-02-24 20:17:53 +04:00
|
|
|
Table.remove state.pending key ;
|
2017-09-29 20:43:13 +04:00
|
|
|
lwt_debug "received %a from %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
Hash.pp key P2p_peer.Id.pp_short peer >>= fun () ->
|
2017-09-29 20:43:13 +04:00
|
|
|
Lwt.return_unit
|
2017-11-06 18:23:06 +04:00
|
|
|
| Notify_cancelation key ->
|
|
|
|
Table.remove state.pending key ;
|
|
|
|
lwt_debug "canceled %a"
|
|
|
|
Hash.pp key >>= fun () ->
|
|
|
|
Lwt.return_unit
|
2017-09-29 20:43:13 +04:00
|
|
|
| Notify_invalid (peer, key) ->
|
|
|
|
lwt_debug "received invalid %a from %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
Hash.pp key P2p_peer.Id.pp_short peer >>= fun () ->
|
2017-09-29 20:43:13 +04:00
|
|
|
(* TODO *)
|
|
|
|
Lwt.return_unit
|
|
|
|
| Notify_unrequested (peer, key) ->
|
|
|
|
lwt_debug "received unrequested %a from %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
Hash.pp key P2p_peer.Id.pp_short peer >>= fun () ->
|
2017-09-29 20:43:13 +04:00
|
|
|
(* TODO *)
|
2017-02-24 20:17:53 +04:00
|
|
|
Lwt.return_unit
|
2017-09-29 20:43:13 +04:00
|
|
|
| Notify_duplicate (peer, key) ->
|
|
|
|
lwt_debug "received duplicate %a from %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
Hash.pp key P2p_peer.Id.pp_short peer >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
(* TODO *)
|
|
|
|
Lwt.return_unit
|
|
|
|
|
2017-09-29 20:43:13 +04:00
|
|
|
let rec worker_loop state =
|
2017-11-27 09:13:12 +04:00
|
|
|
let shutdown = Lwt_canceler.cancelation state.canceler
|
2017-09-29 20:43:13 +04:00
|
|
|
and timeout = compute_timeout state in
|
2017-11-08 15:06:20 +04:00
|
|
|
Lwt.choose
|
|
|
|
[ (state.events >|= fun _ -> ()) ; timeout ; shutdown ] >>= fun () ->
|
2017-09-29 20:43:13 +04:00
|
|
|
if Lwt.state shutdown <> Lwt.Sleep then
|
2017-09-29 20:43:13 +04:00
|
|
|
lwt_debug "terminating" >>= fun () ->
|
2017-09-29 20:43:13 +04:00
|
|
|
Lwt.return_unit
|
|
|
|
else if Lwt.state state.events <> Lwt.Sleep then
|
2017-09-29 20:43:13 +04:00
|
|
|
let now = Unix.gettimeofday () in
|
2017-09-29 20:43:13 +04:00
|
|
|
state.events >>= fun events ->
|
2017-11-08 15:06:20 +04:00
|
|
|
state.events <- Lwt_pipe.pop_all state.queue ;
|
2017-09-29 20:43:13 +04:00
|
|
|
Lwt_list.iter_s (process_event state now) events >>= fun () ->
|
2017-09-29 20:43:13 +04:00
|
|
|
worker_loop state
|
|
|
|
else
|
2017-09-29 20:43:13 +04:00
|
|
|
lwt_debug "timeout" >>= fun () ->
|
2017-09-29 20:43:13 +04:00
|
|
|
let now = Unix.gettimeofday () in
|
|
|
|
let active_peers = Request.active state.param in
|
|
|
|
let requests =
|
|
|
|
Table.fold
|
|
|
|
(fun key { peers ; next_request ; delay } acc ->
|
|
|
|
if next_request > now +. 0.2 then
|
|
|
|
acc
|
|
|
|
else
|
|
|
|
let remaining_peers =
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Set.inter peers active_peers in
|
|
|
|
if P2p_peer.Set.is_empty remaining_peers &&
|
|
|
|
not (P2p_peer.Set.is_empty peers) then
|
2017-09-29 20:43:13 +04:00
|
|
|
( Table.remove state.pending key ; acc )
|
|
|
|
else
|
2017-09-29 20:43:13 +04:00
|
|
|
let requested_peer =
|
2018-02-08 13:51:01 +04:00
|
|
|
P2p_peer.Id.Set.random_elt
|
2018-01-24 15:48:25 +04:00
|
|
|
(if P2p_peer.Set.is_empty remaining_peers
|
2017-09-29 20:43:13 +04:00
|
|
|
then active_peers
|
|
|
|
else remaining_peers) in
|
2017-09-29 20:43:13 +04:00
|
|
|
let next = { peers = remaining_peers ;
|
|
|
|
next_request = now +. delay ;
|
|
|
|
delay = delay *. 1.2 } in
|
|
|
|
Table.replace state.pending key next ;
|
2017-09-29 20:43:13 +04:00
|
|
|
let requests =
|
2018-01-24 15:48:25 +04:00
|
|
|
try key :: P2p_peer.Map.find requested_peer acc
|
2017-09-29 20:43:13 +04:00
|
|
|
with Not_found -> [key] in
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_peer.Map.add requested_peer requests acc)
|
|
|
|
state.pending P2p_peer.Map.empty in
|
|
|
|
P2p_peer.Map.iter (Request.send state.param) requests ;
|
|
|
|
P2p_peer.Map.fold begin fun peer request acc ->
|
2017-09-29 20:43:13 +04:00
|
|
|
acc >>= fun () ->
|
|
|
|
Lwt_list.iter_s (fun key ->
|
|
|
|
lwt_debug "requested %a from %a"
|
2018-01-24 15:48:25 +04:00
|
|
|
Hash.pp key P2p_peer.Id.pp_short peer)
|
2017-09-29 20:43:13 +04:00
|
|
|
request
|
|
|
|
end requests Lwt.return_unit >>= fun () ->
|
2017-09-29 20:43:13 +04:00
|
|
|
worker_loop state
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
let create param =
|
2017-11-08 15:06:20 +04:00
|
|
|
let state = {
|
|
|
|
param ;
|
|
|
|
queue = Lwt_pipe.create () ;
|
|
|
|
pending = Table.create 17 ;
|
|
|
|
events = Lwt.return [] ;
|
2017-11-27 09:13:12 +04:00
|
|
|
canceler = Lwt_canceler.create () ;
|
2017-11-08 15:06:20 +04:00
|
|
|
worker = Lwt.return_unit ;
|
|
|
|
} in
|
|
|
|
state.worker <-
|
2017-02-24 20:17:53 +04:00
|
|
|
Lwt_utils.worker "db_request_scheduler"
|
2017-11-08 15:06:20 +04:00
|
|
|
~run:(fun () -> worker_loop state)
|
2017-11-27 09:13:12 +04:00
|
|
|
~cancel:(fun () -> Lwt_canceler.cancel state.canceler) ;
|
2017-11-08 15:06:20 +04:00
|
|
|
state
|
2017-02-24 20:17:53 +04:00
|
|
|
|
|
|
|
let shutdown s =
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_canceler.cancel s.canceler >>= fun () ->
|
2017-02-24 20:17:53 +04:00
|
|
|
s.worker
|
|
|
|
|
|
|
|
end
|