2020 self ,
2121 solvers:: dto:: { settle, solve} ,
2222 } ,
23+ leader_lock_tracker:: LeaderLockTracker ,
2324 maintenance:: Maintenance ,
2425 run:: Liveness ,
2526 shutdown_controller:: ShutdownController ,
@@ -68,6 +69,11 @@ pub struct Config {
6869 pub enable_leader_lock : bool ,
6970}
7071
72+ pub struct Probes {
73+ pub liveness : Arc < Liveness > ,
74+ pub startup : Arc < Option < AtomicBool > > ,
75+ }
76+
7177pub struct RunLoop {
7278 config : Config ,
7379 eth : infra:: Ethereum ,
@@ -77,8 +83,7 @@ pub struct RunLoop {
7783 solvable_orders_cache : Arc < SolvableOrdersCache > ,
7884 trusted_tokens : AutoUpdatingTokenList ,
7985 in_flight_orders : Arc < Mutex < HashSet < OrderUid > > > ,
80- liveness : Arc < Liveness > ,
81- readiness : Arc < Option < AtomicBool > > ,
86+ probes : Probes ,
8287 /// Maintenance tasks that should run before every runloop to have
8388 /// the most recent data available.
8489 maintenance : Arc < Maintenance > ,
@@ -96,8 +101,7 @@ impl RunLoop {
96101 solver_participation_guard : SolverParticipationGuard ,
97102 solvable_orders_cache : Arc < SolvableOrdersCache > ,
98103 trusted_tokens : AutoUpdatingTokenList ,
99- liveness : Arc < Liveness > ,
100- readiness : Arc < Option < AtomicBool > > ,
104+ probes : Probes ,
101105 maintenance : Arc < Maintenance > ,
102106 competition_updates_sender : tokio:: sync:: mpsc:: UnboundedSender < ( ) > ,
103107 ) -> Self {
@@ -113,8 +117,7 @@ impl RunLoop {
113117 solvable_orders_cache,
114118 trusted_tokens,
115119 in_flight_orders : Default :: default ( ) ,
116- liveness,
117- readiness,
120+ probes,
118121 maintenance,
119122 competition_updates_sender,
120123 winner_selection : winner_selection:: Arbitrator { max_winners, weth } ,
@@ -131,7 +134,7 @@ impl RunLoop {
131134 let mut last_block = None ;
132135
133136 let self_arc = Arc :: new ( self ) ;
134- let mut leader = if self_arc. config . enable_leader_lock {
137+ let leader = if self_arc. config . enable_leader_lock {
135138 Some (
136139 self_arc
137140 . persistence
@@ -141,37 +144,26 @@ impl RunLoop {
141144 } else {
142145 None
143146 } ;
144- let mut was_leader = false ;
147+ let mut leader_lock_tracker = LeaderLockTracker :: new ( leader ) ;
145148
146149 while !control. should_shutdown ( ) {
147- let is_leader = if let Some ( ref mut leader) = leader {
148- leader. try_acquire ( ) . await . unwrap_or_else ( |err| {
149- tracing:: error!( ?err, "failed to become leader" ) ;
150- Metrics :: leader_lock_error ( ) ;
151- false
152- } )
153- } else {
154- true
155- } ;
150+ leader_lock_tracker. try_acquire ( ) . await ;
156151
157- if leader. is_some ( ) && is_leader && !was_leader {
158- tracing:: info!( "Stepped up as a leader" ) ;
159- Metrics :: leader_step_up ( ) ;
152+ let start_block = self_arc
153+ . update_caches ( & mut last_block, leader_lock_tracker. is_leader ( ) )
154+ . await ;
155+
156+ // caches are warmed up, we're ready to do leader work
157+ if let Some ( startup) = self_arc. probes . startup . as_ref ( ) {
158+ startup. store ( true , Ordering :: Release ) ;
160159 }
161- was_leader = is_leader;
162160
163- let start_block = self_arc. update_caches ( & mut last_block, is_leader) . await ;
164- if !is_leader {
161+ if !leader_lock_tracker. is_leader ( ) {
165162 // only the leader is supposed to run the auctions
166163 tokio:: time:: sleep ( Duration :: from_millis ( 200 ) ) . await ;
167164 continue ;
168165 }
169166
170- // caches are warmed up, we're ready to do leader work
171- if let Some ( readiness) = self_arc. readiness . as_ref ( ) {
172- readiness. store ( true , Ordering :: Release ) ;
173- }
174-
175167 if let Some ( auction) = self_arc
176168 . next_auction ( start_block, & mut last_auction, & mut last_block)
177169 . await
@@ -183,12 +175,7 @@ impl RunLoop {
183175 . await
184176 }
185177 }
186-
187- if let Some ( mut leader) = leader {
188- tracing:: info!( "Shutdown received, stepping down as the leader" ) ;
189- leader. release ( ) . await ;
190- Metrics :: leader_step_down ( ) ;
191- }
178+ leader_lock_tracker. release ( ) . await ;
192179 }
193180
194181 async fn update_caches ( & self , prev_block : & mut Option < H256 > , is_leader : bool ) -> BlockInfo {
@@ -248,7 +235,7 @@ impl RunLoop {
248235 }
249236
250237 observe:: log_auction_delta ( & previous, & auction) ;
251- self . liveness . auction ( ) ;
238+ self . probes . liveness . auction ( ) ;
252239 Metrics :: auction_ready ( start_block. observed_at ) ;
253240 Some ( auction)
254241 }
@@ -284,7 +271,7 @@ impl RunLoop {
284271
285272 if auction. orders . is_empty ( ) {
286273 // Updating liveness probe to not report unhealthy due to this optimization
287- self . liveness . auction ( ) ;
274+ self . probes . liveness . auction ( ) ;
288275 tracing:: debug!( "skipping empty auction" ) ;
289276 return None ;
290277 }
@@ -1003,14 +990,6 @@ struct Metrics {
1003990 /// function is started.
1004991 #[ metric( buckets( 0 , 0.25 , 0.5 , 0.75 , 1 , 1.5 , 2 , 2.5 , 3 , 4 , 5 , 6 ) ) ]
1005992 current_block_delay : prometheus:: Histogram ,
1006-
1007- /// Tracks the current leader status
1008- /// 1 - is currently autopilot leader
1009- /// 0 - is currently autopilot follower
1010- is_leader : prometheus:: IntGauge ,
1011-
1012- /// Trackes the count of errors acquiring leader lock (should never happen)
1013- leader_lock_error : prometheus:: IntCounter ,
1014993}
1015994
1016995impl Metrics {
@@ -1111,18 +1090,6 @@ impl Metrics {
11111090 . current_block_delay
11121091 . observe ( init_block_timestamp. elapsed ( ) . as_secs_f64 ( ) )
11131092 }
1114-
1115- fn leader_step_up ( ) {
1116- Self :: get ( ) . is_leader . set ( 1 )
1117- }
1118-
1119- fn leader_step_down ( ) {
1120- Self :: get ( ) . is_leader . set ( 0 )
1121- }
1122-
1123- fn leader_lock_error ( ) {
1124- Self :: get ( ) . leader_lock_error . inc ( )
1125- }
11261093}
11271094
11281095pub mod observe {
0 commit comments