2017-01-14 16:14:11 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2017-01-14 16:14:11 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
include Logging.Make (struct let name = "p2p.maintenance" end)
|
|
|
|
|
|
|
|
type bounds = {
|
|
|
|
min_threshold: int ;
|
|
|
|
min_target: int ;
|
|
|
|
max_target: int ;
|
|
|
|
max_threshold: int ;
|
|
|
|
}
|
|
|
|
|
2018-01-24 15:48:25 +04:00
|
|
|
type 'meta pool = Pool : ('msg, 'meta) P2p_pool.t -> 'meta pool
|
2017-01-14 16:14:11 +04:00
|
|
|
|
|
|
|
type 'meta t = {
|
2017-11-27 09:13:12 +04:00
|
|
|
canceler: Lwt_canceler.t ;
|
2017-01-14 16:14:11 +04:00
|
|
|
bounds: bounds ;
|
|
|
|
pool: 'meta pool ;
|
|
|
|
just_maintained: unit Lwt_condition.t ;
|
|
|
|
please_maintain: unit Lwt_condition.t ;
|
2017-03-14 13:51:44 +04:00
|
|
|
mutable maintain_worker : unit Lwt.t ;
|
2017-01-14 16:14:11 +04:00
|
|
|
}
|
|
|
|
|
2018-05-03 20:20:28 +04:00
|
|
|
(** Select [expected] points among the disconnected known points.
|
2017-01-14 16:14:11 +04:00
|
|
|
It ignores points which are greylisted, or for which a connection
|
2018-02-22 18:35:50 +04:00
|
|
|
failed after [start_time] and the pointes that are banned. It
|
2018-05-03 20:20:28 +04:00
|
|
|
first selects points with the oldest last tentative.
|
|
|
|
Non-trusted points are also ignored if option --closed is set. *)
|
2018-05-15 20:44:16 +04:00
|
|
|
let connectable st start_time expected seen_points =
|
2017-03-14 13:51:44 +04:00
|
|
|
let Pool pool = st.pool in
|
2017-01-14 16:14:11 +04:00
|
|
|
let now = Time.now () in
|
|
|
|
let module Bounded_point_info =
|
2017-11-27 09:13:12 +04:00
|
|
|
List.Bounded(struct
|
2018-01-24 15:48:25 +04:00
|
|
|
type t = (Time.t option * P2p_point.Id.t)
|
2017-01-14 16:14:11 +04:00
|
|
|
let compare (t1, _) (t2, _) =
|
|
|
|
match t1, t2 with
|
|
|
|
| None, None -> 0
|
|
|
|
| None, Some _ -> 1
|
|
|
|
| Some _, None -> -1
|
|
|
|
| Some t1, Some t2 -> Time.compare t2 t1
|
|
|
|
end) in
|
|
|
|
let acc = Bounded_point_info.create expected in
|
2018-05-03 20:20:28 +04:00
|
|
|
let closed = (P2p_pool.config pool).P2p_pool.closed_network in
|
2018-05-15 20:44:16 +04:00
|
|
|
let seen_points =
|
|
|
|
P2p_pool.Points.fold_known pool ~init:seen_points
|
|
|
|
~f:begin fun point pi seen_points ->
|
|
|
|
(* consider the point only if:
|
|
|
|
- it is not in seen_points and
|
|
|
|
- it is not banned, and
|
|
|
|
- it is trusted if we are in `closed` mode
|
|
|
|
*)
|
|
|
|
if P2p_point.Set.mem point seen_points ||
|
|
|
|
P2p_pool.Points.banned pool point ||
|
|
|
|
(closed && not (P2p_point_state.Info.trusted pi))
|
|
|
|
then
|
|
|
|
seen_points
|
|
|
|
else
|
|
|
|
let seen_points = P2p_point.Set.add point seen_points in
|
|
|
|
match P2p_point_state.get pi with
|
|
|
|
| Disconnected -> begin
|
|
|
|
match P2p_point_state.Info.last_miss pi with
|
|
|
|
| Some last when Time.(start_time < last)
|
|
|
|
|| P2p_point_state.Info.greylisted ~now pi ->
|
|
|
|
seen_points
|
|
|
|
| last ->
|
|
|
|
Bounded_point_info.insert (last, point) acc ;
|
|
|
|
seen_points
|
|
|
|
end
|
|
|
|
| _ -> seen_points
|
|
|
|
end
|
|
|
|
in
|
|
|
|
List.map snd (Bounded_point_info.get acc), seen_points
|
2017-01-14 16:14:11 +04:00
|
|
|
|
|
|
|
(** Try to create connections to new peers. It tries to create at
|
|
|
|
least [min_to_contact] connections, and will never creates more
|
|
|
|
than [max_to_contact]. But, if after trying once all disconnected
|
|
|
|
peers, it returns [false]. *)
|
|
|
|
let rec try_to_contact
|
2018-05-15 20:44:16 +04:00
|
|
|
st ?(start_time = Time.now ()) ~seen_points
|
2017-01-14 16:14:11 +04:00
|
|
|
min_to_contact max_to_contact =
|
|
|
|
let Pool pool = st.pool in
|
|
|
|
if min_to_contact <= 0 then
|
|
|
|
Lwt.return_true
|
|
|
|
else
|
2018-05-15 20:44:16 +04:00
|
|
|
let contactable, seen_points =
|
|
|
|
connectable st start_time max_to_contact seen_points in
|
2017-01-14 16:14:11 +04:00
|
|
|
if contactable = [] then
|
2017-01-23 14:09:59 +04:00
|
|
|
Lwt_unix.yield () >>= fun () ->
|
2017-01-14 16:14:11 +04:00
|
|
|
Lwt.return_false
|
|
|
|
else
|
|
|
|
List.fold_left
|
|
|
|
(fun acc point ->
|
2018-02-20 21:28:05 +04:00
|
|
|
P2p_pool.connect pool point >>= function
|
2017-01-14 16:14:11 +04:00
|
|
|
| Ok _ -> acc >|= succ
|
|
|
|
| Error _ -> acc)
|
|
|
|
(Lwt.return 0)
|
|
|
|
contactable >>= fun established ->
|
2018-05-15 20:44:16 +04:00
|
|
|
try_to_contact st ~start_time ~seen_points
|
2017-01-14 16:14:11 +04:00
|
|
|
(min_to_contact - established) (max_to_contact - established)
|
|
|
|
|
|
|
|
(** Do a maintenance step. It will terminate only when the number
|
2018-02-22 18:35:50 +04:00
|
|
|
of connections is between `min_threshold` and `max_threshold`.
|
|
|
|
Do a pass in the list of banned peers and remove all peers that
|
|
|
|
have been banned for more then xxx seconds *)
|
2017-01-14 16:14:11 +04:00
|
|
|
let rec maintain st =
|
|
|
|
let Pool pool = st.pool in
|
2018-01-24 15:48:25 +04:00
|
|
|
let n_connected = P2p_pool.active_connections pool in
|
2018-03-11 18:02:59 +04:00
|
|
|
let pool_cfg = P2p_pool.config pool in
|
2018-04-25 16:37:33 +04:00
|
|
|
let older_than =
|
|
|
|
Time.(add (now ()) (Int64.of_int (- pool_cfg.greylist_timeout)))
|
|
|
|
in
|
2018-04-26 15:57:11 +04:00
|
|
|
P2p_pool.gc_greylist pool ~older_than ;
|
2017-01-14 16:14:11 +04:00
|
|
|
if n_connected < st.bounds.min_threshold then
|
|
|
|
too_few_connections st n_connected
|
|
|
|
else if st.bounds.max_threshold < n_connected then
|
|
|
|
too_many_connections st n_connected
|
|
|
|
else begin
|
|
|
|
(* end of maintenance when enough users have been reached *)
|
|
|
|
Lwt_condition.broadcast st.just_maintained () ;
|
|
|
|
lwt_debug "Maintenance step ended" >>= fun () ->
|
|
|
|
return ()
|
|
|
|
end
|
|
|
|
|
|
|
|
and too_few_connections st n_connected =
|
|
|
|
let Pool pool = st.pool in
|
|
|
|
(* too few connections, try and contact many peers *)
|
2017-01-23 14:09:59 +04:00
|
|
|
lwt_log_notice "Too few connections (%d)" n_connected >>= fun () ->
|
2017-01-14 16:14:11 +04:00
|
|
|
let min_to_contact = st.bounds.min_target - n_connected in
|
|
|
|
let max_to_contact = st.bounds.max_target - n_connected in
|
2018-05-15 20:44:16 +04:00
|
|
|
try_to_contact
|
|
|
|
st min_to_contact max_to_contact ~seen_points:P2p_point.Set.empty >>=
|
|
|
|
fun success ->
|
2017-01-23 14:09:59 +04:00
|
|
|
if success then begin
|
2017-01-14 16:14:11 +04:00
|
|
|
maintain st
|
|
|
|
end else begin
|
2018-02-20 21:28:05 +04:00
|
|
|
(* not enough contacts, ask the pals of our pals, and then wait *)
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_pool.broadcast_bootstrap_msg pool ;
|
2018-02-08 13:51:01 +04:00
|
|
|
protect ~canceler:st.canceler begin fun () ->
|
2017-01-14 16:14:11 +04:00
|
|
|
Lwt.pick [
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_pool.Pool_event.wait_new_peer pool ;
|
2017-01-14 16:14:11 +04:00
|
|
|
Lwt_unix.sleep 5.0 (* TODO exponential back-off ??
|
|
|
|
or wait for the existence of a
|
|
|
|
non grey-listed peer ?? *)
|
|
|
|
] >>= return
|
|
|
|
end >>=? fun () ->
|
|
|
|
maintain st
|
|
|
|
end
|
|
|
|
|
|
|
|
and too_many_connections st n_connected =
|
|
|
|
let Pool pool = st.pool in
|
|
|
|
(* too many connections, start the russian roulette *)
|
|
|
|
let to_kill = n_connected - st.bounds.max_target in
|
|
|
|
lwt_debug "Too many connections, will kill %d" to_kill >>= fun () ->
|
2018-01-24 15:48:25 +04:00
|
|
|
snd @@ P2p_pool.Connection.fold pool
|
2017-01-14 16:14:11 +04:00
|
|
|
~init:(to_kill, Lwt.return_unit)
|
|
|
|
~f:(fun _ conn (i, t) ->
|
|
|
|
if i = 0 then (0, t)
|
2018-01-24 15:48:25 +04:00
|
|
|
else (i - 1, t >>= fun () -> P2p_pool.disconnect conn))
|
2017-01-14 16:14:11 +04:00
|
|
|
>>= fun () ->
|
|
|
|
maintain st
|
|
|
|
|
|
|
|
let rec worker_loop st =
|
2017-03-14 13:51:44 +04:00
|
|
|
let Pool pool = st.pool in
|
2017-01-14 16:14:11 +04:00
|
|
|
begin
|
2018-02-08 13:51:01 +04:00
|
|
|
protect ~canceler:st.canceler begin fun () ->
|
2017-01-14 16:14:11 +04:00
|
|
|
Lwt.pick [
|
|
|
|
Lwt_unix.sleep 120. ; (* every two minutes *)
|
|
|
|
Lwt_condition.wait st.please_maintain ; (* when asked *)
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_pool.Pool_event.wait_too_few_connections pool ; (* limits *)
|
|
|
|
P2p_pool.Pool_event.wait_too_many_connections pool
|
2017-01-14 16:14:11 +04:00
|
|
|
] >>= fun () ->
|
|
|
|
return ()
|
|
|
|
end >>=? fun () ->
|
2018-01-24 15:48:25 +04:00
|
|
|
let n_connected = P2p_pool.active_connections pool in
|
2017-03-14 13:51:44 +04:00
|
|
|
if n_connected < st.bounds.min_threshold
|
2017-11-13 19:34:00 +04:00
|
|
|
|| st.bounds.max_threshold < n_connected then
|
2017-03-14 13:51:44 +04:00
|
|
|
maintain st
|
|
|
|
else begin
|
2018-01-24 15:48:25 +04:00
|
|
|
P2p_pool.send_swap_request pool ;
|
2017-03-14 13:51:44 +04:00
|
|
|
return ()
|
|
|
|
end
|
2017-01-14 16:14:11 +04:00
|
|
|
end >>= function
|
|
|
|
| Ok () -> worker_loop st
|
2018-02-08 13:51:01 +04:00
|
|
|
| Error [ Canceled ] -> Lwt.return_unit
|
2017-01-14 16:14:11 +04:00
|
|
|
| Error _ -> Lwt.return_unit
|
|
|
|
|
2018-03-11 18:02:59 +04:00
|
|
|
let run bounds pool =
|
2017-11-27 09:13:12 +04:00
|
|
|
let canceler = Lwt_canceler.create () in
|
2017-01-14 16:14:11 +04:00
|
|
|
let st = {
|
2017-03-14 13:51:44 +04:00
|
|
|
canceler ;
|
|
|
|
bounds ;
|
|
|
|
pool = Pool pool ;
|
2017-01-14 16:14:11 +04:00
|
|
|
just_maintained = Lwt_condition.create () ;
|
|
|
|
please_maintain = Lwt_condition.create () ;
|
2017-03-14 13:51:44 +04:00
|
|
|
maintain_worker = Lwt.return_unit ;
|
2017-01-14 16:14:11 +04:00
|
|
|
} in
|
2017-03-14 13:51:44 +04:00
|
|
|
st.maintain_worker <-
|
2017-01-14 16:14:11 +04:00
|
|
|
Lwt_utils.worker "maintenance"
|
2017-11-13 17:29:28 +04:00
|
|
|
~run:(fun () -> worker_loop st)
|
2017-11-27 09:13:12 +04:00
|
|
|
~cancel:(fun () -> Lwt_canceler.cancel canceler) ;
|
2017-01-14 16:14:11 +04:00
|
|
|
st
|
|
|
|
|
|
|
|
let maintain { just_maintained ; please_maintain } =
|
|
|
|
let wait = Lwt_condition.wait just_maintained in
|
|
|
|
Lwt_condition.broadcast please_maintain () ;
|
|
|
|
wait
|
|
|
|
|
2017-03-14 13:51:44 +04:00
|
|
|
let shutdown {
|
|
|
|
canceler ;
|
|
|
|
maintain_worker ;
|
|
|
|
just_maintained } =
|
2017-11-27 09:13:12 +04:00
|
|
|
Lwt_canceler.cancel canceler >>= fun () ->
|
2017-03-14 13:51:44 +04:00
|
|
|
maintain_worker >>= fun () ->
|
2017-01-14 16:14:11 +04:00
|
|
|
Lwt_condition.broadcast just_maintained () ;
|
|
|
|
Lwt.return_unit
|
2017-03-14 13:51:44 +04:00
|
|
|
|