1mod metrics;
7mod piece_cache_state;
8#[cfg(test)]
9mod tests;
10
11use crate::farm::{MaybePieceStoredResult, PieceCache, PieceCacheId, PieceCacheOffset, PlotCache};
12use crate::farmer_cache::metrics::FarmerCacheMetrics;
13use crate::farmer_cache::piece_cache_state::PieceCachesState;
14use crate::node_client::NodeClient;
15use async_lock::RwLock as AsyncRwLock;
16use event_listener_primitives::{Bag, HandlerId};
17use futures::channel::mpsc;
18use futures::future::{Either, FusedFuture};
19use futures::stream::{FuturesOrdered, FuturesUnordered};
20use futures::{FutureExt, SinkExt, Stream, StreamExt, select, stream};
21use parking_lot::{Mutex, RwLock};
22use prometheus_client::registry::Registry;
23use rand::prelude::*;
24use rayon::prelude::*;
25use std::collections::hash_map::Entry;
26use std::collections::{HashMap, HashSet};
27use std::future::join;
28use std::sync::Arc;
29use std::sync::atomic::{AtomicUsize, Ordering};
30use std::task::Poll;
31use std::time::Duration;
32use std::{fmt, mem};
33use subspace_core_primitives::pieces::{Piece, PieceIndex};
34use subspace_core_primitives::segments::{SegmentHeader, SegmentIndex};
35use subspace_data_retrieval::piece_getter::PieceGetter;
36use subspace_networking::KeyWithDistance;
37use subspace_networking::libp2p::PeerId;
38use subspace_networking::libp2p::kad::RecordKey;
39use subspace_networking::utils::multihash::ToMultihash;
40use subspace_process::run_future_in_dedicated_thread;
41use tokio::sync::Semaphore;
42use tokio::task::yield_now;
43use tracing::{Instrument, debug, error, info, info_span, trace, warn};
44
45const WORKER_CHANNEL_CAPACITY: usize = 100;
46const SYNC_BATCH_SIZE: usize = 256;
47const SYNC_CONCURRENT_BATCHES: usize = 4;
48const INTERMEDIATE_CACHE_UPDATE_INTERVAL: usize = 100;
51const INITIAL_SYNC_FARM_INFO_CHECK_INTERVAL: Duration = Duration::from_secs(1);
52
53type HandlerFn<A> = Arc<dyn Fn(&A) + Send + Sync + 'static>;
54type Handler<A> = Bag<HandlerFn<A>, A>;
55type CacheIndex = u8;
56
57#[derive(Default, Debug)]
58struct Handlers {
59 progress: Handler<f32>,
60}
61
62#[derive(Debug, Clone, Copy)]
63struct FarmerCacheOffset {
64 cache_index: CacheIndex,
65 piece_offset: PieceCacheOffset,
66}
67
68impl FarmerCacheOffset {
69 fn new(cache_index: CacheIndex, piece_offset: PieceCacheOffset) -> Self {
70 Self {
71 cache_index,
72 piece_offset,
73 }
74 }
75}
76
77#[derive(Debug, Clone)]
78struct CacheBackend {
79 backend: Arc<dyn PieceCache>,
80 used_capacity: u32,
81 total_capacity: u32,
82}
83
84impl std::ops::Deref for CacheBackend {
85 type Target = Arc<dyn PieceCache>;
86
87 fn deref(&self) -> &Self::Target {
88 &self.backend
89 }
90}
91
92impl CacheBackend {
93 fn new(backend: Arc<dyn PieceCache>, total_capacity: u32) -> Self {
94 Self {
95 backend,
96 used_capacity: 0,
97 total_capacity,
98 }
99 }
100
101 fn next_free(&mut self) -> Option<PieceCacheOffset> {
102 let offset = self.used_capacity;
103 if offset < self.total_capacity {
104 self.used_capacity += 1;
105 Some(PieceCacheOffset(offset))
106 } else {
107 debug!(?offset, total_capacity = ?self.total_capacity, "No free space in cache backend");
108 None
109 }
110 }
111
112 fn free_size(&self) -> u32 {
113 self.total_capacity - self.used_capacity
114 }
115}
116
117#[derive(Debug)]
118struct CacheState {
119 cache_stored_pieces: HashMap<KeyWithDistance, FarmerCacheOffset>,
120 cache_free_offsets: Vec<FarmerCacheOffset>,
121 backend: CacheBackend,
122}
123
124#[derive(Debug)]
125enum WorkerCommand {
126 ReplaceBackingCaches {
127 new_piece_caches: Vec<Arc<dyn PieceCache>>,
128 },
129 ForgetKey {
130 key: RecordKey,
131 },
132}
133
134#[derive(Debug)]
136#[must_use = "Farmer cache will not work unless its worker is running"]
137pub struct FarmerCacheWorker<NC>
138where
139 NC: fmt::Debug,
140{
141 peer_id: PeerId,
142 node_client: NC,
143 piece_caches: Arc<AsyncRwLock<PieceCachesState>>,
144 plot_caches: Arc<PlotCaches>,
145 handlers: Arc<Handlers>,
146 worker_receiver: Option<mpsc::Receiver<WorkerCommand>>,
147 metrics: Option<Arc<FarmerCacheMetrics>>,
148}
149
150impl<NC> FarmerCacheWorker<NC>
151where
152 NC: NodeClient,
153{
154 pub async fn run<PG>(mut self, piece_getter: PG)
158 where
159 PG: PieceGetter,
160 {
161 let mut last_segment_index_internal = SegmentIndex::ZERO;
163
164 let mut worker_receiver = self
165 .worker_receiver
166 .take()
167 .expect("Always set during worker instantiation");
168
169 if let Some(WorkerCommand::ReplaceBackingCaches { new_piece_caches }) =
170 worker_receiver.next().await
171 {
172 self.initialize(
173 &piece_getter,
174 &mut last_segment_index_internal,
175 new_piece_caches,
176 )
177 .await;
178 } else {
179 return;
181 }
182
183 let mut segment_headers_notifications =
184 match self.node_client.subscribe_archived_segment_headers().await {
185 Ok(segment_headers_notifications) => segment_headers_notifications,
186 Err(error) => {
187 error!(%error, "Failed to subscribe to archived segments notifications");
188 return;
189 }
190 };
191
192 self.keep_up_after_initial_sync(&piece_getter, &mut last_segment_index_internal)
196 .await;
197
198 loop {
199 select! {
200 maybe_command = worker_receiver.next() => {
201 let Some(command) = maybe_command else {
202 return;
204 };
205
206 self.handle_command(command, &piece_getter, &mut last_segment_index_internal).await;
207 }
208 maybe_segment_header = segment_headers_notifications.next().fuse() => {
209 if let Some(segment_header) = maybe_segment_header {
210 self.process_segment_header(&piece_getter, segment_header, &mut last_segment_index_internal).await;
211 } else {
212 return;
215 }
216 }
217 }
218 }
219 }
220
221 async fn handle_command<PG>(
222 &self,
223 command: WorkerCommand,
224 piece_getter: &PG,
225 last_segment_index_internal: &mut SegmentIndex,
226 ) where
227 PG: PieceGetter,
228 {
229 match command {
230 WorkerCommand::ReplaceBackingCaches { new_piece_caches } => {
231 self.initialize(piece_getter, last_segment_index_internal, new_piece_caches)
232 .await;
233 }
234 WorkerCommand::ForgetKey { key } => {
236 let mut caches = self.piece_caches.write().await;
237 let key = KeyWithDistance::new_with_record_key(self.peer_id, key);
238 let Some(offset) = caches.remove_stored_piece(&key) else {
239 return;
241 };
242
243 let cache_index = offset.cache_index;
244 let piece_offset = offset.piece_offset;
245 let Some(backend) = caches.get_backend(cache_index).cloned() else {
246 return;
248 };
249
250 caches.push_dangling_free_offset(offset);
251 match backend.read_piece_index(piece_offset).await {
252 Ok(Some(piece_index)) => {
253 trace!(%piece_index, %cache_index, %piece_offset, "Forget piece");
254 }
255 Ok(None) => {
256 warn!(
257 %cache_index,
258 %piece_offset,
259 "Piece index out of range, this is likely an implementation bug, \
260 not freeing heap element"
261 );
262 }
263 Err(error) => {
264 error!(
265 %error,
266 %cache_index,
267 ?key,
268 %piece_offset,
269 "Error while reading piece from cache"
270 );
271 }
272 }
273 }
274 }
275 }
276
277 async fn initialize<PG>(
278 &self,
279 piece_getter: &PG,
280 last_segment_index_internal: &mut SegmentIndex,
281 new_piece_caches: Vec<Arc<dyn PieceCache>>,
282 ) where
283 PG: PieceGetter,
284 {
285 info!("Initializing piece cache");
286
287 let (mut stored_pieces, mut dangling_free_offsets) =
289 mem::take(&mut *self.piece_caches.write().await).reuse();
290
291 debug!("Collecting pieces that were in the cache before");
292
293 if let Some(metrics) = &self.metrics {
294 metrics.piece_cache_capacity_total.set(0);
295 metrics.piece_cache_capacity_used.set(0);
296 }
297
298 let peer_id = self.peer_id;
299
300 let piece_caches_number = new_piece_caches.len();
302 let maybe_caches_futures = new_piece_caches
303 .into_iter()
304 .enumerate()
305 .filter_map(|(cache_index, new_cache)| {
306 let total_capacity = new_cache.max_num_elements();
307 let mut backend = CacheBackend::new(new_cache, total_capacity);
308 let Ok(cache_index) = CacheIndex::try_from(cache_index) else {
309 warn!(
310 ?piece_caches_number,
311 "Too many piece caches provided, {cache_index} cache will be ignored",
312 );
313 return None;
314 };
315
316 if let Some(metrics) = &self.metrics {
317 metrics
318 .piece_cache_capacity_total
319 .inc_by(total_capacity as i64);
320 }
321
322 let init_fut = async move {
323 let used_capacity = &mut backend.used_capacity;
324
325 let mut maybe_contents = match backend.backend.contents().await {
329 Ok(contents) => Some(contents),
330 Err(error) => {
331 warn!(%error, "Failed to get cache contents");
332
333 None
334 }
335 };
336
337 #[allow(clippy::mutable_key_type)]
338 let mut cache_stored_pieces = HashMap::new();
339 let mut cache_free_offsets = Vec::new();
340
341 let Some(mut contents) = maybe_contents.take() else {
342 drop(maybe_contents);
343
344 return CacheState {
345 cache_stored_pieces,
346 cache_free_offsets,
347 backend,
348 };
349 };
350
351 while let Some(maybe_element_details) = contents.next().await {
352 let (piece_offset, maybe_piece_index) = match maybe_element_details {
353 Ok(element_details) => element_details,
354 Err(error) => {
355 warn!(%error, "Failed to get cache contents element details");
356 break;
357 }
358 };
359 let offset = FarmerCacheOffset::new(cache_index, piece_offset);
360 match maybe_piece_index {
361 Some(piece_index) => {
362 *used_capacity = piece_offset.0 + 1;
363 let record_key = RecordKey::from(piece_index.to_multihash());
364 let key = KeyWithDistance::new_with_record_key(peer_id, record_key);
365 cache_stored_pieces.insert(key, offset);
366 }
367 None => {
368 cache_free_offsets.push(offset);
371 }
372 }
373
374 yield_now().await;
376 }
377
378 drop(maybe_contents);
379 drop(contents);
380
381 CacheState {
382 cache_stored_pieces,
383 cache_free_offsets,
384 backend,
385 }
386 };
387
388 Some(run_future_in_dedicated_thread(
389 move || init_fut.instrument(info_span!("", %cache_index)),
390 format!("piece-cache.{cache_index:02}"),
391 ))
392 })
393 .collect::<Result<Vec<_>, _>>();
394
395 let caches_futures = match maybe_caches_futures {
396 Ok(caches_futures) => caches_futures,
397 Err(error) => {
398 error!(%error, "Failed to spawn piece cache reading thread");
399
400 return;
401 }
402 };
403
404 let mut backends = Vec::with_capacity(caches_futures.len());
405 let mut caches_futures = caches_futures.into_iter().collect::<FuturesOrdered<_>>();
406
407 while let Some(maybe_cache) = caches_futures.next().await {
408 match maybe_cache {
409 Ok(cache) => {
410 let backend = cache.backend;
411 for (key, cache_offset) in cache.cache_stored_pieces {
412 if let Some(old_cache_offset) = stored_pieces.insert(key, cache_offset) {
413 dangling_free_offsets.push_front(old_cache_offset);
414 }
415 }
416 dangling_free_offsets.extend(
417 cache.cache_free_offsets.into_iter().filter(|free_offset| {
418 free_offset.piece_offset.0 < backend.used_capacity
419 }),
420 );
421 backends.push(backend);
422 }
423 Err(_cancelled) => {
424 error!("Piece cache reading thread panicked");
425
426 return;
427 }
428 };
429 }
430
431 let mut caches = PieceCachesState::new(stored_pieces, dangling_free_offsets, backends);
432
433 info!("Synchronizing piece cache");
434
435 let last_segment_index = loop {
436 match self.node_client.farmer_app_info().await {
437 Ok(farmer_app_info) => {
438 let last_segment_index =
439 farmer_app_info.protocol_info.history_size.segment_index();
440 if !farmer_app_info.syncing || last_segment_index > SegmentIndex::ZERO {
448 break last_segment_index;
449 }
450 }
451 Err(error) => {
452 error!(
453 %error,
454 "Failed to get farmer app info from node, keeping old cache state without \
455 updates"
456 );
457
458 *self.piece_caches.write().await = caches;
460 return;
461 }
462 }
463
464 tokio::time::sleep(INITIAL_SYNC_FARM_INFO_CHECK_INTERVAL).await;
465 };
466
467 debug!(%last_segment_index, "Identified last segment index");
468
469 let segment_indices = Vec::from_iter(SegmentIndex::ZERO..=last_segment_index);
471 let mut piece_indices_to_store = segment_indices
474 .into_par_iter()
475 .flat_map(|segment_index| {
476 segment_index
477 .segment_piece_indexes()
478 .into_par_iter()
479 .map(|piece_index| {
480 (
481 KeyWithDistance::new(self.peer_id, piece_index.to_multihash()),
482 piece_index,
483 )
484 })
485 })
486 .collect::<Vec<_>>();
487
488 piece_indices_to_store.par_sort_unstable_by(|(a_key, _), (b_key, _)| a_key.cmp(b_key));
491
492 let mut piece_indices_to_store = piece_indices_to_store
494 .into_iter()
495 .take(caches.total_capacity())
496 .collect::<HashMap<_, _>>();
497
498 let mut piece_caches_capacity_used = vec![0u32; caches.backends().len()];
499 caches.free_unneeded_stored_pieces(&mut piece_indices_to_store);
503
504 if let Some(metrics) = &self.metrics {
505 for offset in caches.stored_pieces_offsets() {
506 piece_caches_capacity_used[usize::from(offset.cache_index)] += 1;
507 }
508
509 for cache_used in piece_caches_capacity_used {
510 metrics
511 .piece_cache_capacity_used
512 .inc_by(i64::from(cache_used));
513 }
514 }
515
516 self.piece_caches.write().await.clone_from(&caches);
518 let stored_count = caches.stored_pieces_offsets().len();
519
520 debug!(
521 %stored_count,
522 count = %piece_indices_to_store.len(),
523 "Identified piece indices that should be cached",
524 );
525
526 let pieces_to_download_total = piece_indices_to_store.len() + stored_count;
527 let piece_indices_to_store = piece_indices_to_store
528 .into_values()
529 .collect::<Vec<_>>()
530 .chunks(SYNC_BATCH_SIZE)
534 .map(|chunk| chunk.to_vec())
535 .collect::<Vec<_>>();
536
537 let downloaded_pieces_count = AtomicUsize::new(stored_count);
538 let caches = Mutex::new(caches);
539 self.handlers.progress.call_simple(&0.0);
540 let piece_indices_to_store = piece_indices_to_store.into_iter().enumerate();
541
542 let downloading_semaphore = &Semaphore::new(SYNC_BATCH_SIZE * SYNC_CONCURRENT_BATCHES);
543 let ignored_cache_indices = &RwLock::new(HashSet::new());
544
545 let downloading_pieces_stream =
546 stream::iter(piece_indices_to_store.map(|(batch, piece_indices)| {
547 let downloaded_pieces_count = &downloaded_pieces_count;
548 let caches = &caches;
549
550 async move {
551 let mut permit = downloading_semaphore
552 .acquire_many(SYNC_BATCH_SIZE as u32)
553 .await
554 .expect("Semaphore is never closed; qed");
555 debug!(%batch, num_pieces = %piece_indices.len(), "Downloading pieces");
556
557 let pieces_stream = match piece_getter.get_pieces(piece_indices).await {
558 Ok(pieces_stream) => pieces_stream,
559 Err(error) => {
560 error!(
561 %error,
562 "Failed to get pieces from piece getter"
563 );
564 return;
565 }
566 };
567 let mut pieces_stream = pieces_stream.enumerate();
568
569 while let Some((index, (piece_index, result))) = pieces_stream.next().await {
570 debug!(%batch, %index, %piece_index, "Downloaded piece");
571
572 let piece = match result {
573 Ok(Some(piece)) => {
574 trace!(%batch, %piece_index, "Downloaded piece successfully");
575 piece
576 }
577 Ok(None) => {
578 debug!(%batch, %piece_index, "Couldn't find piece");
579 continue;
580 }
581 Err(error) => {
582 debug!(
583 %batch,
584 %error,
585 %piece_index,
586 "Failed to get piece for piece cache"
587 );
588 continue;
589 }
590 };
591 permit.split(1);
593
594 let (offset, maybe_backend) = {
595 let mut caches = caches.lock();
596
597 let Some(offset) = caches.pop_free_offset() else {
599 error!(
600 %batch,
601 %piece_index,
602 "Failed to store piece in cache, there was no space"
603 );
604 break;
605 };
606
607 (offset, caches.get_backend(offset.cache_index).cloned())
608 };
609
610 let cache_index = offset.cache_index;
611 let piece_offset = offset.piece_offset;
612
613 let skip_write = ignored_cache_indices.read().contains(&cache_index);
614 if skip_write {
615 trace!(
616 %batch,
617 %cache_index,
618 %piece_index,
619 %piece_offset,
620 "Skipping known problematic cache index"
621 );
622 } else {
623 if let Some(backend) = maybe_backend
624 && let Err(error) =
625 backend.write_piece(piece_offset, piece_index, &piece).await
626 {
627 error!(
628 %error,
629 %batch,
630 %cache_index,
631 %piece_index,
632 %piece_offset,
633 "Failed to write piece into cache, ignoring this cache going \
634 forward"
635 );
636 ignored_cache_indices.write().insert(cache_index);
637 continue;
638 }
639
640 let key =
641 KeyWithDistance::new(self.peer_id, piece_index.to_multihash());
642 caches.lock().push_stored_piece(key, offset);
643 }
644
645 let prev_downloaded_pieces_count =
646 downloaded_pieces_count.fetch_add(1, Ordering::Relaxed);
647 if prev_downloaded_pieces_count != pieces_to_download_total {
650 let progress = prev_downloaded_pieces_count as f32
651 / pieces_to_download_total as f32
652 * 100.0;
653 if prev_downloaded_pieces_count % INTERMEDIATE_CACHE_UPDATE_INTERVAL
654 == 0
655 {
656 let mut piece_caches = self.piece_caches.write().await;
657 piece_caches.clone_from(&caches.lock());
658
659 info!(
660 "Piece cache sync {progress:.2}% complete ({} / {})",
661 bytesize::to_string(
662 (prev_downloaded_pieces_count * Piece::SIZE) as u64,
663 true,
664 ),
665 bytesize::to_string(
666 (pieces_to_download_total * Piece::SIZE) as u64,
667 true,
668 ),
669 );
670 }
671
672 self.handlers.progress.call_simple(&progress);
673 }
674 }
675 }
676 }));
677
678 downloading_pieces_stream
681 .buffer_unordered(SYNC_CONCURRENT_BATCHES * 10)
684 .for_each(|()| async {})
686 .await;
687
688 *self.piece_caches.write().await = caches.into_inner();
689 self.handlers.progress.call_simple(&100.0);
690 *last_segment_index_internal = last_segment_index;
691
692 info!("Finished piece cache synchronization");
693 }
694
695 async fn process_segment_header<PG>(
696 &self,
697 piece_getter: &PG,
698 segment_header: SegmentHeader,
699 last_segment_index_internal: &mut SegmentIndex,
700 ) where
701 PG: PieceGetter,
702 {
703 let segment_index = segment_header.segment_index();
704 debug!(%segment_index, "Starting to process newly archived segment");
705
706 if *last_segment_index_internal < segment_index {
707 debug!(%segment_index, "Downloading potentially useful pieces");
708
709 let pieces_to_maybe_include = segment_index
713 .segment_piece_indexes()
714 .into_iter()
715 .map(|piece_index| async move {
716 let should_store_in_piece_cache = self
717 .piece_caches
718 .read()
719 .await
720 .should_include_key(self.peer_id, piece_index);
721
722 let key = RecordKey::from(piece_index.to_multihash());
723 let should_store_in_plot_cache =
724 self.plot_caches.should_store(piece_index, &key).await;
725
726 if !(should_store_in_piece_cache || should_store_in_plot_cache) {
727 trace!(%piece_index, "Piece doesn't need to be cached #1");
728
729 return None;
730 }
731
732 let maybe_piece_result =
733 self.node_client
734 .piece(piece_index)
735 .await
736 .inspect_err(|error| {
737 debug!(
738 %error,
739 %segment_index,
740 %piece_index,
741 "Failed to retrieve piece from node right after archiving"
742 );
743 });
744
745 if let Ok(Some(piece)) = maybe_piece_result {
746 return Some((piece_index, piece));
747 }
748
749 match piece_getter.get_piece(piece_index).await {
750 Ok(Some(piece)) => Some((piece_index, piece)),
751 Ok(None) => {
752 warn!(
753 %segment_index,
754 %piece_index,
755 "Failed to retrieve piece right after archiving"
756 );
757
758 None
759 }
760 Err(error) => {
761 warn!(
762 %error,
763 %segment_index,
764 %piece_index,
765 "Failed to retrieve piece right after archiving"
766 );
767
768 None
769 }
770 }
771 })
772 .collect::<FuturesUnordered<_>>()
773 .filter_map(|maybe_piece| async move { maybe_piece })
774 .collect::<Vec<_>>()
775 .await;
776
777 debug!(%segment_index, "Downloaded potentially useful pieces");
778
779 self.acknowledge_archived_segment_processing(segment_index)
780 .await;
781
782 for (piece_index, piece) in pieces_to_maybe_include {
785 if !self
786 .plot_caches
787 .store_additional_piece(piece_index, &piece)
788 .await
789 {
790 trace!(%piece_index, "Piece doesn't need to be cached in plot cache");
791 }
792
793 if !self
794 .piece_caches
795 .read()
796 .await
797 .should_include_key(self.peer_id, piece_index)
798 {
799 trace!(%piece_index, "Piece doesn't need to be cached #2");
800
801 continue;
802 }
803
804 trace!(%piece_index, "Piece needs to be cached #1");
805
806 self.persist_piece_in_cache(piece_index, piece).await;
807 }
808
809 *last_segment_index_internal = segment_index;
810 } else {
811 self.acknowledge_archived_segment_processing(segment_index)
812 .await;
813 }
814
815 debug!(%segment_index, "Finished processing newly archived segment");
816 }
817
818 async fn acknowledge_archived_segment_processing(&self, segment_index: SegmentIndex) {
819 match self
820 .node_client
821 .acknowledge_archived_segment_header(segment_index)
822 .await
823 {
824 Ok(()) => {
825 debug!(%segment_index, "Acknowledged archived segment");
826 }
827 Err(error) => {
828 error!(%segment_index, ?error, "Failed to acknowledge archived segment");
829 }
830 };
831 }
832
833 async fn keep_up_after_initial_sync<PG>(
834 &self,
835 piece_getter: &PG,
836 last_segment_index_internal: &mut SegmentIndex,
837 ) where
838 PG: PieceGetter,
839 {
840 let last_segment_index = match self.node_client.farmer_app_info().await {
841 Ok(farmer_app_info) => farmer_app_info.protocol_info.history_size.segment_index(),
842 Err(error) => {
843 error!(
844 %error,
845 "Failed to get farmer app info from node, keeping old cache state without \
846 updates"
847 );
848 return;
849 }
850 };
851
852 if last_segment_index <= *last_segment_index_internal {
853 return;
854 }
855
856 info!(
857 "Syncing piece cache to the latest history size, this may pause block production if \
858 takes too long"
859 );
860
861 let piece_indices = (*last_segment_index_internal..=last_segment_index)
863 .flat_map(|segment_index| segment_index.segment_piece_indexes());
864
865 for piece_index in piece_indices {
867 if !self
868 .piece_caches
869 .read()
870 .await
871 .should_include_key(self.peer_id, piece_index)
872 {
873 trace!(%piece_index, "Piece doesn't need to be cached #3");
874
875 continue;
876 }
877
878 trace!(%piece_index, "Piece needs to be cached #2");
879
880 let result = piece_getter.get_piece(piece_index).await;
881
882 let piece = match result {
883 Ok(Some(piece)) => piece,
884 Ok(None) => {
885 debug!(%piece_index, "Couldn't find piece");
886 continue;
887 }
888 Err(error) => {
889 debug!(
890 %error,
891 %piece_index,
892 "Failed to get piece for piece cache"
893 );
894 continue;
895 }
896 };
897
898 self.persist_piece_in_cache(piece_index, piece).await;
899 }
900
901 info!("Finished syncing piece cache to the latest history size");
902
903 *last_segment_index_internal = last_segment_index;
904 }
905
906 async fn persist_piece_in_cache(&self, piece_index: PieceIndex, piece: Piece) {
909 let key = KeyWithDistance::new(self.peer_id, piece_index.to_multihash());
910 let mut caches = self.piece_caches.write().await;
911 match caches.should_replace(&key) {
912 Some((old_key, offset)) => {
914 let cache_index = offset.cache_index;
915 let piece_offset = offset.piece_offset;
916 let Some(backend) = caches.get_backend(cache_index) else {
917 warn!(
919 %cache_index,
920 %piece_index,
921 "Should have a cached backend, but it didn't exist, this is an \
922 implementation bug"
923 );
924 return;
925 };
926 if let Err(error) = backend.write_piece(piece_offset, piece_index, &piece).await {
927 error!(
928 %error,
929 %cache_index,
930 %piece_index,
931 %piece_offset,
932 "Failed to write piece into cache"
933 );
934 } else {
935 let old_piece_index = decode_piece_index_from_record_key(old_key.record_key());
936 trace!(
937 %cache_index,
938 %old_piece_index,
939 %piece_index,
940 %piece_offset,
941 "Successfully replaced old cached piece"
942 );
943 caches.push_stored_piece(key, offset);
944 }
945 }
946 None => {
948 let Some(offset) = caches.pop_free_offset() else {
949 warn!(
950 %piece_index,
951 "Should have inserted piece into cache, but it didn't happen, this is an \
952 implementation bug"
953 );
954 return;
955 };
956 let cache_index = offset.cache_index;
957 let piece_offset = offset.piece_offset;
958 let Some(backend) = caches.get_backend(cache_index) else {
959 warn!(
961 %cache_index,
962 %piece_index,
963 "Should have a cached backend, but it didn't exist, this is an \
964 implementation bug"
965 );
966 return;
967 };
968
969 if let Err(error) = backend.write_piece(piece_offset, piece_index, &piece).await {
970 error!(
971 %error,
972 %cache_index,
973 %piece_index,
974 %piece_offset,
975 "Failed to write piece into cache"
976 );
977 } else {
978 trace!(
979 %cache_index,
980 %piece_index,
981 %piece_offset,
982 "Successfully stored piece in cache"
983 );
984 if let Some(metrics) = &self.metrics {
985 metrics.piece_cache_capacity_used.inc();
986 }
987 caches.push_stored_piece(key, offset);
988 }
989 }
990 };
991 }
992}
993
994#[derive(Debug)]
995struct PlotCaches {
996 caches: AsyncRwLock<Vec<Arc<dyn PlotCache>>>,
998 next_plot_cache: AtomicUsize,
1000}
1001
1002impl PlotCaches {
1003 async fn should_store(&self, piece_index: PieceIndex, key: &RecordKey) -> bool {
1004 for (cache_index, cache) in self.caches.read().await.iter().enumerate() {
1005 match cache.is_piece_maybe_stored(key).await {
1006 Ok(MaybePieceStoredResult::No) => {
1007 }
1009 Ok(MaybePieceStoredResult::Vacant) => {
1010 return true;
1011 }
1012 Ok(MaybePieceStoredResult::Yes) => {
1013 return false;
1015 }
1016 Err(error) => {
1017 warn!(
1018 %cache_index,
1019 %piece_index,
1020 %error,
1021 "Failed to check piece stored in cache"
1022 );
1023 }
1024 }
1025 }
1026
1027 false
1028 }
1029
1030 async fn store_additional_piece(&self, piece_index: PieceIndex, piece: &Piece) -> bool {
1032 let plot_caches = self.caches.read().await;
1033 let plot_caches_len = plot_caches.len();
1034
1035 for _ in 0..plot_caches_len {
1037 let plot_cache_index =
1038 self.next_plot_cache.fetch_add(1, Ordering::Relaxed) % plot_caches_len;
1039
1040 match plot_caches[plot_cache_index]
1041 .try_store_piece(piece_index, piece)
1042 .await
1043 {
1044 Ok(true) => {
1045 return false;
1046 }
1047 Ok(false) => {
1048 continue;
1049 }
1050 Err(error) => {
1051 error!(
1052 %error,
1053 %piece_index,
1054 %plot_cache_index,
1055 "Failed to store additional piece in cache"
1056 );
1057 continue;
1058 }
1059 }
1060 }
1061
1062 false
1063 }
1064}
1065
1066#[derive(Debug, Clone)]
1077pub struct FarmerCache {
1078 peer_id: PeerId,
1079 piece_caches: Arc<AsyncRwLock<PieceCachesState>>,
1081 plot_caches: Arc<PlotCaches>,
1083 handlers: Arc<Handlers>,
1084 worker_sender: mpsc::Sender<WorkerCommand>,
1086 metrics: Option<Arc<FarmerCacheMetrics>>,
1087}
1088
1089impl FarmerCache {
1090 pub fn new<NC>(
1095 node_client: NC,
1096 peer_id: PeerId,
1097 registry: Option<&mut Registry>,
1098 ) -> (Self, FarmerCacheWorker<NC>)
1099 where
1100 NC: NodeClient,
1101 {
1102 let caches = Arc::default();
1103 let (worker_sender, worker_receiver) = mpsc::channel(WORKER_CHANNEL_CAPACITY);
1104 let handlers = Arc::new(Handlers::default());
1105
1106 let plot_caches = Arc::new(PlotCaches {
1107 caches: AsyncRwLock::default(),
1108 next_plot_cache: AtomicUsize::new(0),
1109 });
1110 let metrics = registry.map(|registry| Arc::new(FarmerCacheMetrics::new(registry)));
1111
1112 let instance = Self {
1113 peer_id,
1114 piece_caches: Arc::clone(&caches),
1115 plot_caches: Arc::clone(&plot_caches),
1116 handlers: Arc::clone(&handlers),
1117 worker_sender,
1118 metrics: metrics.clone(),
1119 };
1120 let worker = FarmerCacheWorker {
1121 peer_id,
1122 node_client,
1123 piece_caches: caches,
1124 plot_caches,
1125 handlers,
1126 worker_receiver: Some(worker_receiver),
1127 metrics,
1128 };
1129
1130 (instance, worker)
1131 }
1132
1133 pub async fn get_piece<Key>(&self, key: Key) -> Option<Piece>
1135 where
1136 RecordKey: From<Key>,
1137 {
1138 let key = RecordKey::from(key);
1139 let maybe_piece_found = {
1140 let key = KeyWithDistance::new_with_record_key(self.peer_id, key.clone());
1141 let caches = self.piece_caches.read().await;
1142
1143 caches.get_stored_piece(&key).and_then(|offset| {
1144 let cache_index = offset.cache_index;
1145 let piece_offset = offset.piece_offset;
1146 Some((
1147 piece_offset,
1148 cache_index,
1149 caches.get_backend(cache_index)?.clone(),
1150 ))
1151 })
1152 };
1153
1154 if let Some((piece_offset, cache_index, backend)) = maybe_piece_found {
1155 match backend.read_piece(piece_offset).await {
1156 Ok(maybe_piece) => {
1157 return match maybe_piece {
1158 Some((_piece_index, piece)) => {
1159 if let Some(metrics) = &self.metrics {
1160 metrics.cache_get_hit.inc();
1161 }
1162 Some(piece)
1163 }
1164 None => {
1165 error!(
1166 %cache_index,
1167 %piece_offset,
1168 ?key,
1169 "Piece was expected to be in cache, but wasn't found there"
1170 );
1171 if let Some(metrics) = &self.metrics {
1172 metrics.cache_get_error.inc();
1173 }
1174 None
1175 }
1176 };
1177 }
1178 Err(error) => {
1179 error!(
1180 %error,
1181 %cache_index,
1182 ?key,
1183 %piece_offset,
1184 "Error while reading piece from cache"
1185 );
1186
1187 if let Err(error) = self
1188 .worker_sender
1189 .clone()
1190 .send(WorkerCommand::ForgetKey { key })
1191 .await
1192 {
1193 trace!(%error, "Failed to send ForgetKey command to worker");
1194 }
1195
1196 if let Some(metrics) = &self.metrics {
1197 metrics.cache_get_error.inc();
1198 }
1199 return None;
1200 }
1201 }
1202 }
1203
1204 for cache in self.plot_caches.caches.read().await.iter() {
1205 if let Ok(Some(piece)) = cache.read_piece(&key).await {
1206 if let Some(metrics) = &self.metrics {
1207 metrics.cache_get_hit.inc();
1208 }
1209 return Some(piece);
1210 }
1211 }
1212
1213 if let Some(metrics) = &self.metrics {
1214 metrics.cache_get_miss.inc();
1215 }
1216 None
1217 }
1218
1219 pub async fn get_pieces<'a, PieceIndices>(
1223 &'a self,
1224 piece_indices: PieceIndices,
1225 ) -> impl Stream<Item = (PieceIndex, Option<Piece>)> + Send + Unpin + 'a
1226 where
1227 PieceIndices: IntoIterator<Item = PieceIndex, IntoIter: Send + 'a> + Send + 'a,
1228 {
1229 let mut pieces_to_get_from_plot_cache = Vec::new();
1230
1231 let pieces_to_read_from_piece_cache = {
1232 let caches = self.piece_caches.read().await;
1233 let mut pieces_to_read_from_piece_cache =
1235 HashMap::<CacheIndex, (CacheBackend, HashMap<_, _>)>::new();
1236
1237 for piece_index in piece_indices {
1238 let key = RecordKey::from(piece_index.to_multihash());
1239
1240 let offset = match caches.get_stored_piece(&KeyWithDistance::new_with_record_key(
1241 self.peer_id,
1242 key.clone(),
1243 )) {
1244 Some(offset) => offset,
1245 None => {
1246 pieces_to_get_from_plot_cache.push((piece_index, key));
1247 continue;
1248 }
1249 };
1250
1251 let cache_index = offset.cache_index;
1252 let piece_offset = offset.piece_offset;
1253
1254 match pieces_to_read_from_piece_cache.entry(cache_index) {
1255 Entry::Occupied(mut entry) => {
1256 let (_backend, pieces) = entry.get_mut();
1257 pieces.insert(piece_offset, (piece_index, key));
1258 }
1259 Entry::Vacant(entry) => {
1260 let backend = match caches.get_backend(cache_index) {
1261 Some(backend) => backend.clone(),
1262 None => {
1263 pieces_to_get_from_plot_cache.push((piece_index, key));
1264 continue;
1265 }
1266 };
1267 entry
1268 .insert((backend, HashMap::from([(piece_offset, (piece_index, key))])));
1269 }
1270 }
1271 }
1272
1273 pieces_to_read_from_piece_cache
1274 };
1275
1276 let (tx, mut rx) = mpsc::unbounded();
1277
1278 let fut = async move {
1279 let tx = &tx;
1280
1281 let mut reading_from_piece_cache = pieces_to_read_from_piece_cache
1282 .into_iter()
1283 .map(|(cache_index, (backend, mut pieces_to_get))| async move {
1284 let mut pieces_stream = match backend
1285 .read_pieces(Box::new(
1286 pieces_to_get
1287 .keys()
1288 .copied()
1289 .collect::<Vec<_>>()
1290 .into_iter(),
1291 ))
1292 .await
1293 {
1294 Ok(pieces_stream) => pieces_stream,
1295 Err(error) => {
1296 error!(
1297 %error,
1298 %cache_index,
1299 "Error while reading pieces from cache"
1300 );
1301
1302 if let Some(metrics) = &self.metrics {
1303 metrics.cache_get_error.inc_by(pieces_to_get.len() as u64);
1304 }
1305 for (piece_index, _key) in pieces_to_get.into_values() {
1306 tx.unbounded_send((piece_index, None)).expect(
1307 "This future isn't polled after receiver is dropped; qed",
1308 );
1309 }
1310 return;
1311 }
1312 };
1313
1314 while let Some(maybe_piece) = pieces_stream.next().await {
1315 let result = match maybe_piece {
1316 Ok((piece_offset, Some((piece_index, piece)))) => {
1317 pieces_to_get.remove(&piece_offset);
1318
1319 if let Some(metrics) = &self.metrics {
1320 metrics.cache_get_hit.inc();
1321 }
1322 (piece_index, Some(piece))
1323 }
1324 Ok((piece_offset, None)) => {
1325 let Some((piece_index, key)) = pieces_to_get.remove(&piece_offset)
1326 else {
1327 debug!(
1328 %cache_index,
1329 %piece_offset,
1330 "Received piece offset that was not expected"
1331 );
1332 continue;
1333 };
1334
1335 error!(
1336 %cache_index,
1337 %piece_index,
1338 %piece_offset,
1339 ?key,
1340 "Piece was expected to be in cache, but wasn't found there"
1341 );
1342 if let Some(metrics) = &self.metrics {
1343 metrics.cache_get_error.inc();
1344 }
1345 (piece_index, None)
1346 }
1347 Err(error) => {
1348 error!(
1349 %error,
1350 %cache_index,
1351 "Error while reading piece from cache"
1352 );
1353
1354 if let Some(metrics) = &self.metrics {
1355 metrics.cache_get_error.inc();
1356 }
1357 continue;
1358 }
1359 };
1360
1361 tx.unbounded_send(result)
1362 .expect("This future isn't polled after receiver is dropped; qed");
1363 }
1364
1365 if pieces_to_get.is_empty() {
1366 return;
1367 }
1368
1369 if let Some(metrics) = &self.metrics {
1370 metrics.cache_get_error.inc_by(pieces_to_get.len() as u64);
1371 }
1372 for (piece_offset, (piece_index, key)) in pieces_to_get {
1373 error!(
1374 %cache_index,
1375 %piece_index,
1376 %piece_offset,
1377 ?key,
1378 "Piece cache didn't return an entry for offset"
1379 );
1380
1381 tx.unbounded_send((piece_index, None))
1384 .expect("This future isn't polled after receiver is dropped; qed");
1385 }
1386 })
1387 .collect::<FuturesUnordered<_>>();
1388 let reading_from_piece_cache_fut = async move {
1394 while let Some(()) = reading_from_piece_cache.next().await {
1395 }
1397 };
1398
1399 let reading_from_plot_cache_fut = async {
1400 if pieces_to_get_from_plot_cache.is_empty() {
1401 return;
1402 }
1403
1404 for cache in self.plot_caches.caches.read().await.iter() {
1405 for offset in (0..pieces_to_get_from_plot_cache.len()).rev() {
1408 let (piece_index, key) = &pieces_to_get_from_plot_cache[offset];
1409
1410 if let Ok(Some(piece)) = cache.read_piece(key).await {
1411 if let Some(metrics) = &self.metrics {
1412 metrics.cache_get_hit.inc();
1413 }
1414 tx.unbounded_send((*piece_index, Some(piece)))
1415 .expect("This future isn't polled after receiver is dropped; qed");
1416
1417 pieces_to_get_from_plot_cache.swap_remove(offset);
1420 }
1421 }
1422
1423 if pieces_to_get_from_plot_cache.is_empty() {
1424 return;
1425 }
1426 }
1427
1428 if let Some(metrics) = &self.metrics {
1429 metrics
1430 .cache_get_miss
1431 .inc_by(pieces_to_get_from_plot_cache.len() as u64);
1432 }
1433
1434 for (piece_index, _key) in pieces_to_get_from_plot_cache {
1435 tx.unbounded_send((piece_index, None))
1436 .expect("This future isn't polled after receiver is dropped; qed");
1437 }
1438 };
1439
1440 join!(reading_from_piece_cache_fut, reading_from_plot_cache_fut).await
1441 };
1442 let mut fut = Box::pin(fut.fuse());
1443
1444 stream::poll_fn(move |cx| {
1446 if !fut.is_terminated() {
1447 let _ = fut.poll_unpin(cx);
1449 }
1450
1451 if let Poll::Ready(maybe_result) = rx.poll_next_unpin(cx) {
1452 return Poll::Ready(maybe_result);
1453 }
1454
1455 Poll::Pending
1457 })
1458 }
1459
1460 pub async fn has_pieces(&self, mut piece_indices: Vec<PieceIndex>) -> Vec<PieceIndex> {
1462 let mut pieces_to_find = HashMap::<PieceIndex, RecordKey>::from_iter(
1463 piece_indices
1464 .iter()
1465 .map(|piece_index| (*piece_index, RecordKey::from(piece_index.to_multihash()))),
1466 );
1467
1468 {
1470 let piece_caches = self.piece_caches.read().await;
1471 pieces_to_find.retain(|_piece_index, key| {
1472 let distance_key = KeyWithDistance::new(self.peer_id, key.clone());
1473 !piece_caches.contains_stored_piece(&distance_key)
1474 });
1475 }
1476
1477 if pieces_to_find.is_empty() {
1479 return piece_indices;
1480 }
1481
1482 if let Some(plot_caches) = self.plot_caches.caches.try_read() {
1484 let plot_caches = &plot_caches;
1485 let not_found = pieces_to_find
1486 .into_iter()
1487 .map(|(piece_index, key)| async move {
1488 let key = &key;
1489
1490 let found = plot_caches
1491 .iter()
1492 .map(|plot_cache| async {
1493 matches!(
1494 plot_cache.is_piece_maybe_stored(key).await,
1495 Ok(MaybePieceStoredResult::Yes)
1496 )
1497 })
1498 .collect::<FuturesUnordered<_>>()
1499 .any(|found| async move { found })
1500 .await;
1501
1502 if found { None } else { Some(piece_index) }
1503 })
1504 .collect::<FuturesUnordered<_>>()
1505 .filter_map(|maybe_piece_index| async move { maybe_piece_index })
1506 .collect::<HashSet<_>>()
1507 .await;
1508 piece_indices.retain(|piece_index| !not_found.contains(piece_index));
1509 }
1510 piece_indices
1511 }
1512
1513 pub async fn find_piece(
1515 &self,
1516 piece_index: PieceIndex,
1517 ) -> Option<(PieceCacheId, PieceCacheOffset)> {
1518 let caches = self.piece_caches.read().await;
1519
1520 self.find_piece_internal(&caches, piece_index)
1521 }
1522
1523 pub async fn find_pieces<PieceIndices>(
1525 &self,
1526 piece_indices: PieceIndices,
1527 ) -> Vec<(PieceIndex, PieceCacheId, PieceCacheOffset)>
1528 where
1529 PieceIndices: IntoIterator<Item = PieceIndex>,
1530 {
1531 let caches = self.piece_caches.read().await;
1532
1533 piece_indices
1534 .into_iter()
1535 .filter_map(|piece_index| {
1536 self.find_piece_internal(&caches, piece_index)
1537 .map(|(cache_id, piece_offset)| (piece_index, cache_id, piece_offset))
1538 })
1539 .collect()
1540 }
1541
1542 fn find_piece_internal(
1543 &self,
1544 caches: &PieceCachesState,
1545 piece_index: PieceIndex,
1546 ) -> Option<(PieceCacheId, PieceCacheOffset)> {
1547 let key = KeyWithDistance::new(self.peer_id, piece_index.to_multihash());
1548
1549 let Some(offset) = caches.get_stored_piece(&key) else {
1550 if let Some(metrics) = &self.metrics {
1551 metrics.cache_find_miss.inc();
1552 }
1553
1554 return None;
1555 };
1556 let piece_offset = offset.piece_offset;
1557
1558 if let Some(backend) = caches.get_backend(offset.cache_index) {
1559 if let Some(metrics) = &self.metrics {
1560 metrics.cache_find_hit.inc();
1561 }
1562 return Some((*backend.id(), piece_offset));
1563 }
1564
1565 if let Some(metrics) = &self.metrics {
1566 metrics.cache_find_miss.inc();
1567 }
1568 None
1569 }
1570
1571 pub async fn maybe_store_additional_piece(&self, piece_index: PieceIndex, piece: &Piece) {
1573 let key = RecordKey::from(piece_index.to_multihash());
1574
1575 let should_store = self.plot_caches.should_store(piece_index, &key).await;
1576
1577 if !should_store {
1578 return;
1579 }
1580
1581 self.plot_caches
1582 .store_additional_piece(piece_index, piece)
1583 .await;
1584 }
1585
1586 pub async fn replace_backing_caches(
1588 &self,
1589 new_piece_caches: Vec<Arc<dyn PieceCache>>,
1590 new_plot_caches: Vec<Arc<dyn PlotCache>>,
1591 ) {
1592 if let Err(error) = self
1593 .worker_sender
1594 .clone()
1595 .send(WorkerCommand::ReplaceBackingCaches { new_piece_caches })
1596 .await
1597 {
1598 warn!(%error, "Failed to replace backing caches, worker exited");
1599 }
1600
1601 *self.plot_caches.caches.write().await = new_plot_caches;
1602 }
1603
1604 pub fn on_sync_progress(&self, callback: HandlerFn<f32>) -> HandlerId {
1606 self.handlers.progress.add(callback)
1607 }
1608}
1609
1610#[derive(Debug, Clone)]
1612pub struct FarmerCaches {
1613 caches: Arc<[FarmerCache]>,
1614}
1615
1616impl From<Arc<[FarmerCache]>> for FarmerCaches {
1617 fn from(caches: Arc<[FarmerCache]>) -> Self {
1618 Self { caches }
1619 }
1620}
1621
1622impl From<FarmerCache> for FarmerCaches {
1623 fn from(cache: FarmerCache) -> Self {
1624 Self {
1625 caches: Arc::new([cache]),
1626 }
1627 }
1628}
1629
1630impl FarmerCaches {
1631 pub async fn get_piece<Key>(&self, key: Key) -> Option<Piece>
1633 where
1634 RecordKey: From<Key>,
1635 {
1636 let farmer_cache = self.caches.choose(&mut thread_rng())?;
1637 farmer_cache.get_piece(key).await
1638 }
1639
1640 pub async fn get_pieces<'a, PieceIndices>(
1644 &'a self,
1645 piece_indices: PieceIndices,
1646 ) -> impl Stream<Item = (PieceIndex, Option<Piece>)> + Send + Unpin + 'a
1647 where
1648 PieceIndices: IntoIterator<Item = PieceIndex, IntoIter: Send + 'a> + Send + 'a,
1649 {
1650 let Some(farmer_cache) = self.caches.choose(&mut thread_rng()) else {
1651 return Either::Left(stream::iter(
1652 piece_indices
1653 .into_iter()
1654 .map(|piece_index| (piece_index, None)),
1655 ));
1656 };
1657
1658 Either::Right(farmer_cache.get_pieces(piece_indices).await)
1659 }
1660
1661 pub async fn has_pieces(&self, piece_indices: Vec<PieceIndex>) -> Vec<PieceIndex> {
1663 let Some(farmer_cache) = self.caches.choose(&mut thread_rng()) else {
1664 return Vec::new();
1665 };
1666
1667 farmer_cache.has_pieces(piece_indices).await
1668 }
1669
1670 pub async fn maybe_store_additional_piece(&self, piece_index: PieceIndex, piece: &Piece) {
1672 self.caches
1673 .iter()
1674 .map(|farmer_cache| farmer_cache.maybe_store_additional_piece(piece_index, piece))
1675 .collect::<FuturesUnordered<_>>()
1676 .for_each(|()| async {})
1677 .await;
1678 }
1679}
1680
1681fn decode_piece_index_from_record_key(key: &RecordKey) -> PieceIndex {
1683 let len = key.as_ref().len();
1684 let s = len - PieceIndex::SIZE;
1685
1686 let mut piece_index_bytes = [0u8; PieceIndex::SIZE];
1687 piece_index_bytes.copy_from_slice(&key.as_ref()[s..]);
1688
1689 PieceIndex::from_bytes(piece_index_bytes)
1690}