Line data Source code
1 : #include "fd_policy.h"
2 : #include "../../disco/metrics/fd_metrics.h"
3 :
4 : #define NONCE_NULL (UINT_MAX)
5 0 : #define DEFER_REPAIR_MS (200UL)
6 0 : #define TARGET_TICK_PER_SLOT (64.0)
7 0 : #define MS_PER_TICK (400.0 / TARGET_TICK_PER_SLOT)
8 :
9 : void *
10 0 : fd_policy_new( void * shmem, ulong peer_max, ulong seed, fd_rnonce_ss_t const * rnonce_ss ) {
11 :
12 0 : if( FD_UNLIKELY( !shmem ) ) {
13 0 : FD_LOG_WARNING(( "NULL mem" ));
14 0 : return NULL;
15 0 : }
16 :
17 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_policy_align() ) ) ) {
18 0 : FD_LOG_WARNING(( "misaligned mem" ));
19 0 : return NULL;
20 0 : }
21 :
22 0 : ulong footprint = fd_policy_footprint( peer_max );
23 0 : fd_memset( shmem, 0, footprint );
24 :
25 0 : ulong peer_chain_cnt = fd_policy_peer_map_chain_cnt_est( peer_max );
26 0 : FD_SCRATCH_ALLOC_INIT( l, shmem );
27 0 : fd_policy_t * policy = FD_SCRATCH_ALLOC_APPEND( l, fd_policy_align(), sizeof(fd_policy_t) );
28 0 : void * peers = FD_SCRATCH_ALLOC_APPEND( l, fd_policy_peer_map_align(), fd_policy_peer_map_footprint( peer_chain_cnt ) );
29 0 : void * peers_pool = FD_SCRATCH_ALLOC_APPEND( l, fd_policy_peer_pool_align(), fd_policy_peer_pool_footprint( peer_max ) );
30 0 : void * peers_fast = FD_SCRATCH_ALLOC_APPEND( l, fd_policy_peer_dlist_align(), fd_policy_peer_dlist_footprint() );
31 0 : void * peers_slow = FD_SCRATCH_ALLOC_APPEND( l, fd_policy_peer_dlist_align(), fd_policy_peer_dlist_footprint() );
32 0 : FD_TEST( FD_SCRATCH_ALLOC_FINI( l, fd_policy_align() ) == (ulong)shmem + footprint );
33 :
34 0 : policy->peers.map = fd_policy_peer_map_new ( peers, peer_chain_cnt, seed );
35 0 : policy->peers.pool = fd_policy_peer_pool_new ( peers_pool, peer_max );
36 0 : policy->peers.fast = fd_policy_peer_dlist_new( peers_fast );
37 0 : policy->peers.slow = fd_policy_peer_dlist_new( peers_slow );
38 0 : policy->turbine_slot0 = ULONG_MAX;
39 0 : policy->rnonce_ss[0] = *rnonce_ss;
40 :
41 0 : return shmem;
42 0 : }
43 :
44 : fd_policy_t *
45 0 : fd_policy_join( void * shpolicy ) {
46 0 : fd_policy_t * policy = (fd_policy_t *)shpolicy;
47 :
48 0 : if( FD_UNLIKELY( !policy ) ) {
49 0 : FD_LOG_WARNING(( "NULL policy" ));
50 0 : return NULL;
51 0 : }
52 :
53 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned((ulong)policy, fd_policy_align() ) ) ) {
54 0 : FD_LOG_WARNING(( "misaligned policy" ));
55 0 : return NULL;
56 0 : }
57 :
58 0 : fd_wksp_t * wksp = fd_wksp_containing( policy );
59 0 : if( FD_UNLIKELY( !wksp ) ) {
60 0 : FD_LOG_WARNING(( "policy must be part of a workspace" ));
61 0 : return NULL;
62 0 : }
63 :
64 0 : policy->peers.map = fd_policy_peer_map_join ( policy->peers.map );
65 0 : policy->peers.pool = fd_policy_peer_pool_join ( policy->peers.pool );
66 0 : policy->peers.fast = fd_policy_peer_dlist_join( policy->peers.fast );
67 0 : policy->peers.slow = fd_policy_peer_dlist_join( policy->peers.slow );
68 :
69 0 : policy->peers.select.fast_iter = fd_policy_peer_dlist_iter_fwd_init( policy->peers.fast, policy->peers.pool );
70 0 : policy->peers.select.slow_iter = fd_policy_peer_dlist_iter_fwd_init( policy->peers.slow, policy->peers.pool );
71 0 : policy->peers.select.cnt = 0;
72 :
73 0 : return policy;
74 0 : }
75 :
76 : void *
77 0 : fd_policy_leave( fd_policy_t const * policy ) {
78 :
79 0 : if( FD_UNLIKELY( !policy ) ) {
80 0 : FD_LOG_WARNING(( "NULL policy" ));
81 0 : return NULL;
82 0 : }
83 :
84 0 : return (void *)policy;
85 0 : }
86 :
87 : void *
88 0 : fd_policy_delete( void * policy ) {
89 :
90 0 : if( FD_UNLIKELY( !policy ) ) {
91 0 : FD_LOG_WARNING(( "NULL policy" ));
92 0 : return NULL;
93 0 : }
94 :
95 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned((ulong)policy, fd_policy_align() ) ) ) {
96 0 : FD_LOG_WARNING(( "misaligned policy" ));
97 0 : return NULL;
98 0 : }
99 :
100 0 : return policy;
101 0 : }
102 :
103 0 : static ulong ts_ms( long wallclock ) {
104 0 : return (ulong)wallclock / (ulong)1e6;
105 0 : }
106 :
107 : static int
108 0 : passes_throttle_threshold( fd_policy_t * policy, fd_forest_blk_t * ele ) {
109 0 : if( FD_UNLIKELY( ele->slot < policy->turbine_slot0 ) ) return 1;
110 : /* Essentially is checking if current duration of block ( from the
111 : first shred received until now ) is greater than the highest tick
112 : received + 200ms. */
113 0 : double current_duration = (double)(fd_tickcount() - ele->first_shred_ts) / fd_tempo_tick_per_ns(NULL);
114 0 : double tick_plus_buffer = (ele->est_buffered_tick_recv * MS_PER_TICK + DEFER_REPAIR_MS) * 1e6; // change to 400e6 for a slot duration policy
115 :
116 0 : if( current_duration >= tick_plus_buffer ){
117 0 : FD_MCNT_INC( REPAIR, EAGER_THRESHOLD_EXCEEDED, 1 );
118 0 : return 1;
119 0 : }
120 0 : return 0;
121 0 : }
122 :
123 : static inline fd_policy_peer_dlist_iter_t
124 : peer_iter_advance( fd_policy_peer_dlist_iter_t iter,
125 : fd_policy_peer_dlist_t * dlist,
126 0 : fd_policy_peer_t * pool ) {
127 0 : iter = fd_policy_peer_dlist_iter_fwd_next( iter, dlist, pool );
128 0 : if( FD_UNLIKELY( fd_policy_peer_dlist_iter_done( iter, dlist, pool ) ) ) {
129 0 : iter = fd_policy_peer_dlist_iter_fwd_init( dlist, pool );
130 0 : }
131 0 : return iter;
132 0 : }
133 :
134 : fd_pubkey_t const *
135 0 : fd_policy_peer_select( fd_policy_t * policy ) {
136 0 : fd_policy_peer_dlist_t * fast = policy->peers.fast;
137 0 : fd_policy_peer_dlist_t * slow = policy->peers.slow;
138 0 : fd_policy_peer_t * pool = policy->peers.pool;
139 :
140 0 : if( FD_UNLIKELY( fd_policy_peer_pool_used( pool ) == 0 ) ) return NULL;
141 :
142 : /* reinit stale iterators. happens when peers are inserted into a
143 : previously-empty list after the iterator was initialized. */
144 0 : int fast_empty = fd_policy_peer_dlist_iter_done( fd_policy_peer_dlist_iter_fwd_init( fast, pool ), fast, pool );
145 0 : int slow_empty = fd_policy_peer_dlist_iter_done( fd_policy_peer_dlist_iter_fwd_init( slow, pool ), slow, pool );
146 :
147 0 : if( FD_UNLIKELY( !fast_empty && fd_policy_peer_dlist_iter_done( policy->peers.select.fast_iter, fast, pool ) ) ) {
148 0 : policy->peers.select.fast_iter = fd_policy_peer_dlist_iter_fwd_init( fast, pool );
149 0 : }
150 0 : if( FD_UNLIKELY( !slow_empty && fd_policy_peer_dlist_iter_done( policy->peers.select.slow_iter, slow, pool ) ) ) {
151 0 : policy->peers.select.slow_iter = fd_policy_peer_dlist_iter_fwd_init( slow, pool );
152 0 : }
153 :
154 0 : fd_policy_peer_t * select;
155 :
156 : /* select will be set to current iterator status. Then iterator should
157 : be advanced for the following peer_select call. */
158 :
159 0 : if( FD_UNLIKELY( fast_empty ) ) {
160 0 : select = fd_policy_peer_dlist_iter_ele( policy->peers.select.slow_iter, slow, pool );
161 0 : policy->peers.select.slow_iter = peer_iter_advance( policy->peers.select.slow_iter, slow, pool );
162 0 : return &select->key;
163 0 : }
164 :
165 0 : if( FD_UNLIKELY( slow_empty ) ) {
166 0 : select = fd_policy_peer_dlist_iter_ele( policy->peers.select.fast_iter, fast, pool );
167 0 : policy->peers.select.fast_iter = peer_iter_advance( policy->peers.select.fast_iter, fast, pool );
168 0 : return &select->key;
169 0 : }
170 :
171 : /* interleave FD_POLICY_FAST_PER_SLOW fast, 1 slow. */
172 0 : if( FD_LIKELY( policy->peers.select.cnt < FD_POLICY_FAST_PER_SLOW ) ) {
173 0 : select = fd_policy_peer_dlist_iter_ele( policy->peers.select.fast_iter, fast, pool );
174 0 : policy->peers.select.fast_iter = peer_iter_advance( policy->peers.select.fast_iter, fast, pool );
175 0 : policy->peers.select.cnt++;
176 0 : return &select->key;
177 0 : }
178 :
179 0 : select = fd_policy_peer_dlist_iter_ele( policy->peers.select.slow_iter, slow, pool );
180 0 : policy->peers.select.slow_iter = peer_iter_advance( policy->peers.select.slow_iter, slow, pool );
181 0 : policy->peers.select.cnt = 0;
182 0 : return &select->key;
183 0 : }
184 :
185 : fd_repair_msg_t const *
186 0 : fd_policy_next( fd_policy_t * policy, fd_reqlim_t * dedup, fd_forest_t * forest, fd_repair_t * repair, long now, ulong highest_known_slot, int * charge_busy ) {
187 0 : fd_forest_blk_t * pool = fd_forest_pool( forest );
188 0 : fd_forest_subtlist_t * subtlist = fd_forest_subtlist( forest );
189 0 : *charge_busy = 0;
190 :
191 0 : if( FD_UNLIKELY( forest->root == ULONG_MAX ) ) return NULL;
192 0 : if( FD_UNLIKELY( fd_policy_peer_pool_used( policy->peers.pool ) == 0 ) ) return NULL;
193 :
194 0 : fd_repair_msg_t * out = NULL;
195 0 : ulong now_ms = ts_ms( now );
196 :
197 0 : for( fd_forest_subtlist_iter_t iter = fd_forest_subtlist_iter_fwd_init( subtlist, pool );
198 0 : !fd_forest_subtlist_iter_done ( iter, subtlist, pool );
199 0 : iter = fd_forest_subtlist_iter_fwd_next( iter, subtlist, pool ) ) {
200 0 : *charge_busy = 1;
201 0 : fd_forest_blk_t * orphan = fd_forest_subtlist_iter_ele( iter, subtlist, pool );
202 0 : ulong key = fd_reqlim_key( FD_REPAIR_KIND_ORPHAN, orphan->slot, UINT_MAX );
203 0 : if( FD_UNLIKELY( !fd_reqlim_next( dedup, key, now ) ) ) {
204 0 : uint nonce = fd_rnonce_ss_compute( policy->rnonce_ss, 0, orphan->slot, 0U, now );
205 0 : out = fd_repair_orphan( repair, fd_policy_peer_select( policy ), now_ms, nonce, orphan->slot );
206 0 : return out;
207 0 : }
208 0 : }
209 :
210 : /* Select a slot to operate on 🔪. Advance either the orphan iter or
211 : regular iter. */
212 0 : fd_forest_iter_t * iter = NULL;
213 0 : if( FD_UNLIKELY( fd_forest_reqslist_is_empty( fd_forest_reqslist( forest ), fd_forest_reqspool( forest ) ) ) ) {
214 : /* If the main tree has nothing to iterate at the moment, we can
215 : request down the ORPHAN trees on slots we know about. */
216 0 : iter = &forest->orphiter;
217 0 : } else {
218 0 : iter = &forest->iter;
219 0 : }
220 :
221 0 : fd_forest_iter_next( iter, forest );
222 0 : if( FD_UNLIKELY( fd_forest_iter_done( iter, forest ) ) ) {
223 : // This happens when we have already requested all the shreds we know about.
224 0 : return NULL;
225 0 : }
226 :
227 0 : fd_forest_blk_t * ele = fd_forest_pool_ele( pool, iter->ele_idx );
228 0 : if( FD_UNLIKELY( !passes_throttle_threshold( policy, ele ) ) ) {
229 : /* When we are at the head of the turbine, we should give turbine the
230 : chance to complete the shreds. Agave waits 200ms from the
231 : estimated "correct time" of the highest shred received to repair.
232 : i.e. if we've received the first 200 shreds, the 200th has a tick
233 : of x. Translate that to millis, and we should wait to request shred
234 : 201 until x + 200ms. If we have a hole, i.e. first 200 shreds
235 : receive except shred 100, and the 101th shred has a tick of y, we
236 : should wait until y + 200ms to request shred 100.
237 :
238 : Here we did not pass the timeout threshold, so we are not ready
239 : to repair this slot yet. But it's possible we have another fork
240 : that we need to repair... so we just should skip to the next SLOT
241 : in the main tree iterator. The likelihood that this ele is the
242 : head of turbine is high, which means that the shred_idx of the
243 : iterf is likely to be UINT_MAX, which means calling
244 : fd_forest_iter_next will advance the iterf to the next slot. */
245 0 : iter->shred_idx = UINT_MAX;
246 : /* TODO: Heinous... but the easiest way to ensure this slot gets
247 : added back to the requests deque is if we set the shred_idx to
248 : UINT_MAX, but maybe there should be an explicit API for it. */
249 :
250 0 : return NULL;
251 0 : }
252 :
253 0 : *charge_busy = 1;
254 :
255 0 : if( FD_UNLIKELY( iter->shred_idx == UINT_MAX ) ) {
256 : // We'll never know the the highest shred for the current turbine slot, so there's no point in requesting it.
257 0 : if( FD_UNLIKELY( ele->slot < highest_known_slot && !fd_reqlim_next( dedup, fd_reqlim_key( FD_REPAIR_KIND_HIGHEST_SHRED, ele->slot, UINT_MAX ), now ) ) ) {
258 0 : uint nonce = fd_rnonce_ss_compute( policy->rnonce_ss, 0, ele->slot, 0U, now );
259 0 : out = fd_repair_highest_shred( repair, fd_policy_peer_select( policy ), now_ms, nonce, ele->slot, 0 );
260 0 : }
261 0 : } else {
262 : /* Regular repair requests are not deduped. Any potential regular
263 : shred request that will be made needs to be handled at the repair
264 : tile level to allow repair tile to re-request the same shred if
265 : it gets deduped. */
266 0 : uint nonce = fd_rnonce_ss_compute( policy->rnonce_ss, 1, ele->slot, iter->shred_idx, now );
267 0 : out = fd_repair_shred( repair, fd_policy_peer_select( policy ), now_ms, nonce, ele->slot, iter->shred_idx );
268 0 : if( FD_UNLIKELY( ele->first_req_ts == 0 ) ) ele->first_req_ts = fd_tickcount();
269 0 : }
270 0 : return out;
271 0 : }
272 :
273 : fd_policy_peer_t const *
274 0 : fd_policy_peer_upsert( fd_policy_t * policy, fd_pubkey_t const * key, fd_ip4_port_t const * addr ) {
275 0 : fd_policy_peer_map_t * peer_map = policy->peers.map;
276 0 : fd_policy_peer_t * pool = policy->peers.pool;
277 0 : fd_policy_peer_t * peer = fd_policy_peer_map_ele_query( peer_map, key, NULL, pool );
278 0 : if( FD_UNLIKELY( !peer && fd_policy_peer_pool_free( pool ) ) ) {
279 0 : peer = fd_policy_peer_pool_ele_acquire( pool );
280 0 : peer->key = *key;
281 0 : peer->ip4 = addr->addr;
282 0 : peer->port = addr->port;
283 0 : peer->req_cnt = 0;
284 0 : peer->res_cnt = 0;
285 0 : peer->first_req_ts = 0;
286 0 : peer->last_req_ts = 0;
287 0 : peer->first_resp_ts = 0;
288 0 : peer->last_resp_ts = 0;
289 0 : peer->total_lat = 0;
290 0 : peer->ewma_lat = 0;
291 0 : peer->stake = 0;
292 0 : peer->unanswered = 0;
293 0 : peer->ping = 0;
294 :
295 0 : fd_policy_peer_map_ele_insert( peer_map, peer, pool );
296 0 : fd_policy_peer_dlist_ele_push_tail( policy->peers.slow, peer, pool );
297 0 : return peer;
298 0 : }
299 0 : if( FD_LIKELY( peer ) ) {
300 0 : peer->ip4 = addr->addr;
301 0 : peer->port = addr->port;
302 0 : }
303 0 : return NULL;
304 0 : }
305 :
306 : fd_policy_peer_t *
307 0 : fd_policy_peer_query( fd_policy_t * policy, fd_pubkey_t const * key ) {
308 0 : if( FD_UNLIKELY( memcmp( key->key, null_pubkey.key, 32UL ) == 0 ) ) return NULL;
309 0 : fd_policy_peer_t * pool = policy->peers.pool;
310 0 : return fd_policy_peer_map_ele_query( policy->peers.map, key, NULL, pool );
311 0 : }
312 :
313 : int
314 0 : fd_policy_peer_remove( fd_policy_t * policy, fd_pubkey_t const * key ) {
315 0 : fd_policy_peer_t * pool = policy->peers.pool;
316 0 : fd_policy_peer_t * peer = fd_policy_peer_map_ele_query( policy->peers.map, key, NULL, pool );
317 0 : if( FD_UNLIKELY( !peer ) ) return 0;
318 :
319 0 : ulong peer_idx = fd_policy_peer_pool_idx( pool, peer );
320 0 : fd_policy_peer_dlist_t * bucket = fd_policy_peer_latency_bucket( policy, peer->ewma_lat, peer->res_cnt );
321 :
322 : /* Advance iterators past the peer being removed while the dlist links
323 : are still intact, so iter_fwd_next can follow the forward pointer. */
324 0 : if( FD_UNLIKELY( policy->peers.select.fast_iter == peer_idx ) ) {
325 0 : policy->peers.select.fast_iter = fd_policy_peer_dlist_iter_fwd_next( policy->peers.select.fast_iter, bucket, pool );
326 0 : }
327 0 : if( FD_UNLIKELY( policy->peers.select.slow_iter == peer_idx ) ) {
328 0 : policy->peers.select.slow_iter = fd_policy_peer_dlist_iter_fwd_next( policy->peers.select.slow_iter, bucket, pool );
329 0 : }
330 :
331 0 : fd_policy_peer_dlist_ele_remove( bucket, peer, pool );
332 0 : fd_policy_peer_map_ele_remove ( policy->peers.map, key, NULL, pool );
333 0 : fd_policy_peer_pool_ele_release( pool, peer );
334 0 : return 1;
335 0 : }
336 :
337 : void
338 0 : fd_policy_peer_request_update( fd_policy_t * policy, fd_pubkey_t const * to ) {
339 0 : fd_policy_peer_t * active = fd_policy_peer_query( policy, to );
340 0 : if( FD_LIKELY( active ) ) {
341 0 : active->req_cnt++;
342 0 : active->unanswered++;
343 0 : active->last_req_ts = fd_tickcount();
344 0 : if( FD_UNLIKELY( active->first_req_ts == 0 ) ) active->first_req_ts = active->last_req_ts;
345 0 : }
346 0 : }
347 :
348 : void
349 0 : fd_policy_peer_response_update( fd_policy_t * policy, fd_pubkey_t const * to, long rtt /* ns */ ) {
350 0 : fd_policy_peer_t * peer = fd_policy_peer_query( policy, to );
351 0 : if( FD_LIKELY( peer ) ) {
352 0 : long now = fd_tickcount();
353 0 : fd_policy_peer_dlist_t * prev_bucket = fd_policy_peer_latency_bucket( policy, peer->ewma_lat, peer->res_cnt );
354 0 : peer->res_cnt++;
355 0 : peer->unanswered = 0;
356 0 : if( FD_UNLIKELY( peer->first_resp_ts == 0 ) ) peer->first_resp_ts = now;
357 0 : peer->last_resp_ts = now;
358 0 : peer->total_lat += rtt;
359 :
360 0 : if( FD_UNLIKELY( peer->res_cnt == 1 ) ) {
361 0 : peer->ewma_lat = rtt;
362 0 : } else {
363 0 : peer->ewma_lat = peer->ewma_lat - peer->ewma_lat / (long)FD_POLICY_EWMA_ALPHA_DENOM
364 0 : + rtt / (long)FD_POLICY_EWMA_ALPHA_DENOM;
365 0 : }
366 0 : fd_policy_peer_dlist_t * new_bucket = fd_policy_peer_latency_bucket( policy, peer->ewma_lat, peer->res_cnt );
367 0 : if( prev_bucket != new_bucket ) {
368 : /* Advance stale iterators */
369 0 : ulong peer_idx = fd_policy_peer_pool_idx( policy->peers.pool, peer );
370 0 : if( FD_UNLIKELY( policy->peers.select.fast_iter == peer_idx ) ) policy->peers.select.fast_iter = fd_policy_peer_dlist_iter_fwd_next( policy->peers.select.fast_iter, policy->peers.fast, policy->peers.pool );
371 0 : if( FD_UNLIKELY( policy->peers.select.slow_iter == peer_idx ) ) policy->peers.select.slow_iter = fd_policy_peer_dlist_iter_fwd_next( policy->peers.select.slow_iter, policy->peers.slow, policy->peers.pool );
372 :
373 0 : fd_policy_peer_dlist_ele_remove ( prev_bucket, peer, policy->peers.pool );
374 0 : fd_policy_peer_dlist_ele_push_tail( new_bucket, peer, policy->peers.pool );
375 0 : }
376 0 : }
377 0 : }
378 :
379 : void
380 0 : fd_policy_set_turbine_slot0( fd_policy_t * policy, ulong slot ) {
381 0 : policy->turbine_slot0 = slot;
382 0 : }
383 :
|