From 509b95d2f57475bb878777b440cd85ad3e3333fe Mon Sep 17 00:00:00 2001 From: Sebastien Mondet Date: Mon, 25 Feb 2019 17:58:09 -0500 Subject: [PATCH 1/2] Make 2-min sleep configurable (`P2p_maintenance`) --- src/bin_node/node_config_file.ml | 17 ++++++++++++----- src/lib_p2p/p2p.ml | 2 ++ src/lib_p2p/p2p.mli | 3 +++ src/lib_p2p/p2p_maintenance.ml | 3 ++- src/lib_p2p/p2p_pool.ml | 1 + src/lib_p2p/p2p_pool.mli | 3 +++ src/lib_p2p/test/test_p2p_pool.ml | 1 + 7 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/bin_node/node_config_file.ml b/src/bin_node/node_config_file.ml index f3a1f4d13c8b..7f034590bb93 100644 --- a/src/bin_node/node_config_file.ml +++ b/src/bin_node/node_config_file.ml @@ -76,6 +76,7 @@ let default_p2p_limits : P2p.limits = { connection_timeout = 10. ; authentication_timeout = 5. ; greylist_timeout = 86400 ; (* one day *) + maintenance_idle_time = 120. ; (* two minutes *) min_connections = 10 ; expected_connections = 50 ; max_connections = 100 ; @@ -133,6 +134,7 @@ let limit : P2p.limits Data_encoding.t = let open Data_encoding in conv (fun { P2p.connection_timeout ; authentication_timeout ; greylist_timeout ; + maintenance_idle_time ; min_connections ; expected_connections ; max_connections ; backlog ; max_incoming_connections ; max_download_speed ; max_upload_speed ; @@ -152,7 +154,7 @@ let limit : P2p.limits Data_encoding.t = incoming_message_queue_size, outgoing_message_queue_size, known_points_history_size, known_peer_ids_history_size, max_known_points)), - ( max_known_peer_ids, greylist_timeout)))) + ( max_known_peer_ids, greylist_timeout, maintenance_idle_time)))) (fun (((( connection_timeout, authentication_timeout, min_connections, expected_connections, max_connections, backlog, max_incoming_connections, @@ -162,8 +164,9 @@ let limit : P2p.limits Data_encoding.t = incoming_message_queue_size, outgoing_message_queue_size, known_points_history_size, known_peer_ids_history_size, max_known_points)), - ( max_known_peer_ids, greylist_timeout))) -> + ( max_known_peer_ids, greylist_timeout, maintenance_idle_time))) -> { connection_timeout ; authentication_timeout ; greylist_timeout ; + maintenance_idle_time ; min_connections ; expected_connections ; max_connections ; backlog ; max_incoming_connections ; max_download_speed ; max_upload_speed ; @@ -234,13 +237,17 @@ let limit : P2p.limits Data_encoding.t = default_p2p_limits.known_points_history_size) (opt "max_known_points" (tup2 uint16 uint16)) )) - (obj2 + (obj3 (opt "max_known_peer_ids" (tup2 uint16 uint16)) (dft "greylist-timeout" ~description: "GC delay for the greylists tables, in seconds." int31 default_p2p_limits.greylist_timeout) - - )) + (dft "maintenance-idle-time" + ~description: "How long to wait at most, in seconds, \ + before running a maintenance loop." + float default_p2p_limits.maintenance_idle_time) + ) + ) let p2p = let open Data_encoding in diff --git a/src/lib_p2p/p2p.ml b/src/lib_p2p/p2p.ml index 506bdaeb3bcd..25d338a063b9 100644 --- a/src/lib_p2p/p2p.ml +++ b/src/lib_p2p/p2p.ml @@ -72,6 +72,7 @@ type limits = { connection_timeout : float ; authentication_timeout : float ; greylist_timeout : int ; + maintenance_idle_time: float ; min_connections : int ; expected_connections : int ; @@ -127,6 +128,7 @@ let create_connection_pool config limits meta_cfg conn_meta_cfg msg_cfg io_sched connection_timeout = limits.connection_timeout ; authentication_timeout = limits.authentication_timeout ; greylist_timeout = limits.greylist_timeout ; + maintenance_idle_time = limits.maintenance_idle_time ; incoming_app_message_queue_size = limits.incoming_app_message_queue_size ; incoming_message_queue_size = limits.incoming_message_queue_size ; outgoing_message_queue_size = limits.outgoing_message_queue_size ; diff --git a/src/lib_p2p/p2p.mli b/src/lib_p2p/p2p.mli index c2cc68ca0699..b719a854e739 100644 --- a/src/lib_p2p/p2p.mli +++ b/src/lib_p2p/p2p.mli @@ -116,6 +116,9 @@ type limits = { greylist_timeout : int ; (** GC delay for the grelists tables, in seconds. *) + maintenance_idle_time: float ; + (** How long to wait at most, in seconds, before running a maintenance loop. *) + min_connections : int ; (** Strict minimum number of connections (triggers an urgent maintenance) *) diff --git a/src/lib_p2p/p2p_maintenance.ml b/src/lib_p2p/p2p_maintenance.ml index cd8f8df0930a..f6b8b95fa4a6 100644 --- a/src/lib_p2p/p2p_maintenance.ml +++ b/src/lib_p2p/p2p_maintenance.ml @@ -190,10 +190,11 @@ and too_many_connections st n_connected = let rec worker_loop st = let Pool pool = st.pool in + let config = P2p_pool.config pool in begin protect ~canceler:st.canceler begin fun () -> Lwt.pick [ - Lwt_unix.sleep 120. ; (* every two minutes *) + Lwt_unix.sleep config.P2p_pool.maintenance_idle_time ; (* default: every two minutes *) Lwt_condition.wait st.please_maintain ; (* when asked *) P2p_pool.Pool_event.wait_too_few_connections pool ; (* limits *) P2p_pool.Pool_event.wait_too_many_connections pool ; diff --git a/src/lib_p2p/p2p_pool.ml b/src/lib_p2p/p2p_pool.ml index eb3587d89948..87424efbc211 100644 --- a/src/lib_p2p/p2p_pool.ml +++ b/src/lib_p2p/p2p_pool.ml @@ -201,6 +201,7 @@ type config = { connection_timeout : float ; authentication_timeout : float ; greylist_timeout : int ; + maintenance_idle_time: float ; incoming_app_message_queue_size : int option ; incoming_message_queue_size : int option ; diff --git a/src/lib_p2p/p2p_pool.mli b/src/lib_p2p/p2p_pool.mli index dddf716fd39f..6ff77f449cf1 100644 --- a/src/lib_p2p/p2p_pool.mli +++ b/src/lib_p2p/p2p_pool.mli @@ -106,6 +106,9 @@ type config = { greylist_timeout : int ; (** GC delay for the grelists tables, in seconds. *) + maintenance_idle_time: float ; + (** How long to wait at most, in seconds, before running a maintenance loop. *) + incoming_app_message_queue_size : int option ; (** Size of the message queue for user messages (messages returned by this module's [read] function. *) diff --git a/src/lib_p2p/test/test_p2p_pool.ml b/src/lib_p2p/test/test_p2p_pool.ml index 8265221d3464..aa453e7f7db0 100644 --- a/src/lib_p2p/test/test_p2p_pool.ml +++ b/src/lib_p2p/test/test_p2p_pool.ml @@ -95,6 +95,7 @@ let detach_node f points n = connection_timeout = 10. ; authentication_timeout = 2. ; greylist_timeout = 2 ; + maintenance_idle_time = 120. ; incoming_app_message_queue_size = None ; incoming_message_queue_size = None ; outgoing_message_queue_size = None ; -- GitLab From 6df51349a443b79c7c90685b4ed20437e4452280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Tue, 26 Feb 2019 17:44:38 +0100 Subject: [PATCH 2/2] P2p: change configuration of `P2p_maintenance` It feels more coherent not to configure `P2p_pool` with parameters only for `P2p_maintenance`. --- src/lib_p2p/p2p.ml | 18 ++++++++++-------- src/lib_p2p/p2p_maintenance.ml | 19 ++++++++++++------- src/lib_p2p/p2p_maintenance.mli | 21 +++++++++++++++++++-- src/lib_p2p/p2p_pool.ml | 2 -- src/lib_p2p/p2p_pool.mli | 6 ------ src/lib_p2p/test/test_p2p_pool.ml | 2 -- 6 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/lib_p2p/p2p.ml b/src/lib_p2p/p2p.ml index 25d338a063b9..86bfd06c84d0 100644 --- a/src/lib_p2p/p2p.ml +++ b/src/lib_p2p/p2p.ml @@ -72,7 +72,7 @@ type limits = { connection_timeout : float ; authentication_timeout : float ; greylist_timeout : int ; - maintenance_idle_time: float ; + maintenance_idle_time : float ; min_connections : int ; expected_connections : int ; @@ -127,8 +127,6 @@ let create_connection_pool config limits meta_cfg conn_meta_cfg msg_cfg io_sched max_incoming_connections = limits.max_incoming_connections ; connection_timeout = limits.connection_timeout ; authentication_timeout = limits.authentication_timeout ; - greylist_timeout = limits.greylist_timeout ; - maintenance_idle_time = limits.maintenance_idle_time ; incoming_app_message_queue_size = limits.incoming_app_message_queue_size ; incoming_message_queue_size = limits.incoming_message_queue_size ; outgoing_message_queue_size = limits.outgoing_message_queue_size ; @@ -173,11 +171,15 @@ let create_maintenance_worker limits pool config = bounds ~min:limits.min_connections ~expected:limits.expected_connections - ~max:limits.max_connections - in - let discovery = - may_create_discovery_worker limits config pool in - P2p_maintenance.create ?discovery bounds pool + ~max:limits.max_connections in + let maintenance_config = { + P2p_maintenance. + maintenance_idle_time = limits.maintenance_idle_time ; + greylist_timeout = limits.greylist_timeout ; + private_mode = config.private_mode ; + } in + let discovery = may_create_discovery_worker limits config pool in + P2p_maintenance.create ?discovery maintenance_config bounds pool let may_create_welcome_worker config limits pool = match config.listening_port with diff --git a/src/lib_p2p/p2p_maintenance.ml b/src/lib_p2p/p2p_maintenance.ml index f6b8b95fa4a6..d6695e507bc6 100644 --- a/src/lib_p2p/p2p_maintenance.ml +++ b/src/lib_p2p/p2p_maintenance.ml @@ -33,10 +33,17 @@ type bounds = { max_threshold: int ; } +type config = { + maintenance_idle_time: float ; + greylist_timeout: int ; + private_mode: bool ; +} + type 'meta pool = Pool : ('msg, 'meta, 'meta_conn) P2p_pool.t -> 'meta pool type 'meta t = { canceler: Lwt_canceler.t ; + config: config ; bounds: bounds ; pool: 'meta pool ; discovery: P2p_discovery.t option ; @@ -64,7 +71,6 @@ let connectable st start_time expected seen_points = | Some t1, Some t2 -> Time.compare t2 t1 end) in let acc = Bounded_point_info.create expected in - let private_mode = (P2p_pool.config pool).P2p_pool.private_mode in let seen_points = P2p_pool.Points.fold_known pool ~init:seen_points ~f:begin fun point pi seen_points -> @@ -75,7 +81,7 @@ let connectable st start_time expected seen_points = *) if P2p_point.Set.mem point seen_points || P2p_pool.Points.banned pool point || - (private_mode && not (P2p_point_state.Info.trusted pi)) + (st.config.private_mode && not (P2p_point_state.Info.trusted pi)) then seen_points else @@ -131,9 +137,8 @@ let rec try_to_contact let rec maintain st = let Pool pool = st.pool in let n_connected = P2p_pool.active_connections pool in - let pool_cfg = P2p_pool.config pool in let older_than = - Time.(add (now ()) (Int64.of_int (- pool_cfg.greylist_timeout))) + Time.(add (now ()) (Int64.of_int (- st.config.greylist_timeout))) in P2p_pool.gc_greylist pool ~older_than ; if n_connected < st.bounds.min_threshold then @@ -190,11 +195,10 @@ and too_many_connections st n_connected = let rec worker_loop st = let Pool pool = st.pool in - let config = P2p_pool.config pool in begin protect ~canceler:st.canceler begin fun () -> Lwt.pick [ - Lwt_unix.sleep config.P2p_pool.maintenance_idle_time ; (* default: every two minutes *) + Lwt_unix.sleep st.config.maintenance_idle_time ; (* default: every two minutes *) Lwt_condition.wait st.please_maintain ; (* when asked *) P2p_pool.Pool_event.wait_too_few_connections pool ; (* limits *) P2p_pool.Pool_event.wait_too_many_connections pool ; @@ -214,8 +218,9 @@ let rec worker_loop st = | Error [ Canceled ] -> Lwt.return_unit | Error _ -> Lwt.return_unit -let create ?discovery bounds pool = { +let create ?discovery config bounds pool = { canceler = Lwt_canceler.create () ; + config ; bounds ; discovery ; pool = Pool pool ; diff --git a/src/lib_p2p/p2p_maintenance.mli b/src/lib_p2p/p2p_maintenance.mli index 0c93e15e5f1a..675d1c7ff7a8 100644 --- a/src/lib_p2p/p2p_maintenance.mli +++ b/src/lib_p2p/p2p_maintenance.mli @@ -50,15 +50,32 @@ type bounds = { max_threshold: int ; } +type config = { + + maintenance_idle_time: float ; + (** How long to wait at most, in seconds, before running a maintenance loop. *) + + greylist_timeout: int ; + (** GC delay for the greylists tables, in seconds. *) + + private_mode: bool ; + (** If [true], only open outgoing/accept incoming connections + to/from peers whose addresses are in [trusted_peers], and inform + these peers that the identity of this node should be revealed to + the rest of the network. *) + +} + + type 'meta t (** Type of a maintenance worker. *) val create: ?discovery:P2p_discovery.t -> - bounds -> + config -> bounds -> ('msg, 'meta, 'meta_conn) P2p_pool.t -> 'meta t -(** [run ?discovery bounds pool] returns a maintenance worker, with +(** [run ?discovery config bounds pool] returns a maintenance worker, with the [discovery] worker if present, for [pool] with connection targets specified in [bounds]. *) diff --git a/src/lib_p2p/p2p_pool.ml b/src/lib_p2p/p2p_pool.ml index 87424efbc211..11a6341532c0 100644 --- a/src/lib_p2p/p2p_pool.ml +++ b/src/lib_p2p/p2p_pool.ml @@ -200,8 +200,6 @@ type config = { max_incoming_connections : int ; connection_timeout : float ; authentication_timeout : float ; - greylist_timeout : int ; - maintenance_idle_time: float ; incoming_app_message_queue_size : int option ; incoming_message_queue_size : int option ; diff --git a/src/lib_p2p/p2p_pool.mli b/src/lib_p2p/p2p_pool.mli index 6ff77f449cf1..7a14912e4703 100644 --- a/src/lib_p2p/p2p_pool.mli +++ b/src/lib_p2p/p2p_pool.mli @@ -103,12 +103,6 @@ type config = { authentication_timeout : float ; (** Delay granted to a peer to perform authentication, in seconds. *) - greylist_timeout : int ; - (** GC delay for the grelists tables, in seconds. *) - - maintenance_idle_time: float ; - (** How long to wait at most, in seconds, before running a maintenance loop. *) - incoming_app_message_queue_size : int option ; (** Size of the message queue for user messages (messages returned by this module's [read] function. *) diff --git a/src/lib_p2p/test/test_p2p_pool.ml b/src/lib_p2p/test/test_p2p_pool.ml index aa453e7f7db0..f4c26453d946 100644 --- a/src/lib_p2p/test/test_p2p_pool.ml +++ b/src/lib_p2p/test/test_p2p_pool.ml @@ -94,8 +94,6 @@ let detach_node f points n = max_incoming_connections = nb_points ; connection_timeout = 10. ; authentication_timeout = 2. ; - greylist_timeout = 2 ; - maintenance_idle_time = 120. ; incoming_app_message_queue_size = None ; incoming_message_queue_size = None ; outgoing_message_queue_size = None ; -- GitLab