1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//! Pool plotter

use crate::plotter::{Plotter, SectorPlottingProgress};
use async_trait::async_trait;
use futures::channel::mpsc;
use std::any::type_name_of_val;
use std::time::Duration;
use subspace_core_primitives::{PublicKey, SectorIndex};
use subspace_farmer_components::FarmerProtocolInfo;
use tracing::{error, trace};

/// Pool plotter.
///
/// This plotter implementation relies on retries and should only be used with local plotter
/// implementations (like CPU and GPU).
#[derive(Debug)]
pub struct PoolPlotter {
    plotters: Vec<Box<dyn Plotter + Send + Sync>>,
    retry_interval: Duration,
}

#[async_trait]
impl Plotter for PoolPlotter {
    async fn has_free_capacity(&self) -> Result<bool, String> {
        for (index, plotter) in self.plotters.iter().enumerate() {
            match plotter.has_free_capacity().await {
                Ok(result) => {
                    if result {
                        return Ok(true);
                    }
                }
                Err(error) => {
                    error!(
                        %error,
                        %index,
                        r#type = type_name_of_val(plotter),
                        "Failed to check free capacity for plotter"
                    );
                }
            }
        }

        Ok(false)
    }

    async fn plot_sector(
        &self,
        public_key: PublicKey,
        sector_index: SectorIndex,
        farmer_protocol_info: FarmerProtocolInfo,
        pieces_in_sector: u16,
        replotting: bool,
        progress_sender: mpsc::Sender<SectorPlottingProgress>,
    ) {
        loop {
            for plotter in &self.plotters {
                if plotter
                    .try_plot_sector(
                        public_key,
                        sector_index,
                        farmer_protocol_info,
                        pieces_in_sector,
                        replotting,
                        progress_sender.clone(),
                    )
                    .await
                {
                    return;
                }
            }

            trace!(
                retry_interval = ?self.retry_interval,
                "All plotters are busy, will wait and try again later"
            );
            tokio::time::sleep(self.retry_interval).await;
        }
    }

    async fn try_plot_sector(
        &self,
        public_key: PublicKey,
        sector_index: SectorIndex,
        farmer_protocol_info: FarmerProtocolInfo,
        pieces_in_sector: u16,
        replotting: bool,
        progress_sender: mpsc::Sender<SectorPlottingProgress>,
    ) -> bool {
        for plotter in &self.plotters {
            if plotter
                .try_plot_sector(
                    public_key,
                    sector_index,
                    farmer_protocol_info,
                    pieces_in_sector,
                    replotting,
                    progress_sender.clone(),
                )
                .await
            {
                return true;
            }
        }

        false
    }
}

impl PoolPlotter {
    /// Create new instance
    pub fn new(plotters: Vec<Box<dyn Plotter + Send + Sync>>, retry_interval: Duration) -> Self {
        Self {
            plotters,
            retry_interval,
        }
    }
}