subspace_farmer/cluster/controller/
caches.rs

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//! This module exposed implementation of caches maintenance.
//!
//! The goal is to observe caches in a particular cache group and keep controller's data structures
//! about which pieces are stored where up to date. Implementation automatically handles dynamic
//! cache addition and removal, tries to reduce number of reinitializations that result in potential
//! piece cache sync, etc.

use crate::cluster::cache::{
    ClusterCacheDetailsRequest, ClusterCacheId, ClusterCacheIdentifyBroadcast, ClusterPieceCache,
    ClusterPieceCacheDetails,
};
use crate::cluster::controller::ClusterControllerCacheIdentifyBroadcast;
use crate::cluster::nats_client::NatsClient;
use crate::farm::PieceCache;
use crate::farmer_cache::FarmerCache;
use anyhow::anyhow;
use futures::channel::oneshot;
use futures::future::FusedFuture;
use futures::{select, FutureExt, StreamExt};
use parking_lot::Mutex;
use std::future::{ready, Future};
use std::pin::{pin, Pin};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::time::MissedTickBehavior;
use tokio_stream::StreamMap;
use tracing::{debug, info, trace, warn};

const SCHEDULE_REINITIALIZATION_DELAY: Duration = Duration::from_secs(3);

#[derive(Debug)]
struct KnownCache {
    cluster_cache_id: ClusterCacheId,
    last_identification: Instant,
    piece_caches: Vec<Arc<ClusterPieceCache>>,
}

#[derive(Debug)]
struct KnownCaches {
    identification_broadcast_interval: Duration,
    known_caches: Vec<KnownCache>,
}

impl KnownCaches {
    fn new(identification_broadcast_interval: Duration) -> Self {
        Self {
            identification_broadcast_interval,
            known_caches: Vec::new(),
        }
    }

    fn get_all(&self) -> Vec<Arc<dyn PieceCache>> {
        self.known_caches
            .iter()
            .flat_map(|known_cache| {
                known_cache
                    .piece_caches
                    .iter()
                    .map(|piece_cache| Arc::clone(piece_cache) as Arc<_>)
            })
            .collect()
    }

    /// Return `false` if cluster cache is unknown and reinitialization is required
    #[must_use]
    fn refresh(&mut self, cluster_cache_id: ClusterCacheId) -> bool {
        self.known_caches.iter_mut().any(|known_cache| {
            if known_cache.cluster_cache_id == cluster_cache_id {
                trace!(%cluster_cache_id, "Updating last identification for cache");
                known_cache.last_identification = Instant::now();
                true
            } else {
                false
            }
        })
    }

    /// Return `true` if cluster cache reinitialization is required
    fn add_cache(
        &mut self,
        cluster_cache_id: ClusterCacheId,
        piece_caches: Vec<Arc<ClusterPieceCache>>,
    ) {
        self.known_caches.push(KnownCache {
            cluster_cache_id,
            last_identification: Instant::now(),
            piece_caches,
        });
    }

    fn remove_expired(&mut self) -> impl Iterator<Item = KnownCache> + '_ {
        self.known_caches.extract_if(.., |known_cache| {
            known_cache.last_identification.elapsed() > self.identification_broadcast_interval * 2
        })
    }
}

/// Utility function for maintaining caches by controller in a cluster environment
pub async fn maintain_caches(
    cache_group: &str,
    nats_client: &NatsClient,
    farmer_cache: &FarmerCache,
    identification_broadcast_interval: Duration,
) -> anyhow::Result<()> {
    let mut known_caches = KnownCaches::new(identification_broadcast_interval);

    let mut piece_caches_to_add = StreamMap::<ClusterCacheId, _>::new().fuse();

    let mut scheduled_reinitialization_for = None;
    let mut cache_reinitialization =
        (Box::pin(ready(())) as Pin<Box<dyn Future<Output = ()>>>).fuse();

    let cache_identify_subscription = pin!(nats_client
        .subscribe_to_broadcasts::<ClusterCacheIdentifyBroadcast>(Some(cache_group), None)
        .await
        .map_err(|error| anyhow!("Failed to subscribe to cache identify broadcast: {error}"))?);

    // Request cache to identify themselves
    if let Err(error) = nats_client
        .broadcast(&ClusterControllerCacheIdentifyBroadcast, cache_group)
        .await
    {
        warn!(%error, "Failed to send cache identification broadcast");
    }

    let mut cache_identify_subscription = cache_identify_subscription.fuse();
    let mut cache_pruning_interval = tokio::time::interval_at(
        (Instant::now() + identification_broadcast_interval * 2).into(),
        identification_broadcast_interval * 2,
    );
    cache_pruning_interval.set_missed_tick_behavior(MissedTickBehavior::Delay);

    loop {
        if cache_reinitialization.is_terminated()
            && let Some(time) = scheduled_reinitialization_for
            && time <= Instant::now()
        {
            scheduled_reinitialization_for.take();

            let new_piece_caches = known_caches.get_all();
            let new_cache_reinitialization = async {
                let (sync_finish_sender, sync_finish_receiver) = oneshot::channel::<()>();
                let sync_finish_sender = Mutex::new(Some(sync_finish_sender));

                let _handler_id = farmer_cache.on_sync_progress(Arc::new(move |&progress| {
                    if progress == 100.0 {
                        if let Some(sync_finish_sender) = sync_finish_sender.lock().take() {
                            // Result doesn't matter
                            let _ = sync_finish_sender.send(());
                        }
                    }
                }));

                farmer_cache
                    .replace_backing_caches(new_piece_caches, Vec::new())
                    .await;

                // Wait for piece cache sync to finish before potentially staring a new one, result
                // doesn't matter
                let _ = sync_finish_receiver.await;
            };

            cache_reinitialization =
                (Box::pin(new_cache_reinitialization) as Pin<Box<dyn Future<Output = ()>>>).fuse();
        }

        select! {
            maybe_identify_message = cache_identify_subscription.next() => {
                let Some(identify_message) = maybe_identify_message else {
                    return Err(anyhow!("Cache identify stream ended"));
                };

                let ClusterCacheIdentifyBroadcast { cluster_cache_id } = identify_message;

                if known_caches.refresh(cluster_cache_id) {
                    trace!(
                        %cluster_cache_id,
                        "Received identification for already known cache"
                    );
                } else if piece_caches_to_add.get_ref().contains_key(&cluster_cache_id) {
                    debug!(
                        %cluster_cache_id,
                        "Received identification for new cache, which is already in progress"
                    );
                } else {
                    debug!(
                        %cluster_cache_id,
                        "Received identification for new cache, collecting piece caches"
                    );
                    piece_caches_to_add.get_mut().insert(
                        cluster_cache_id,
                        Box::pin(collect_piece_caches(cluster_cache_id, nats_client).into_stream()),
                    );
                }
            }
            (cluster_cache_id, maybe_piece_caches) = piece_caches_to_add.select_next_some() => {
                let Ok(piece_caches) = maybe_piece_caches.inspect_err(|error| {
                    warn!(
                        %cluster_cache_id,
                        %error,
                        "Failed to collect piece caches to add, may retry later"
                    )
                }) else {
                    continue;
                };

                info!(
                    %cluster_cache_id,
                    "New cache discovered, scheduling reinitialization"
                );
                scheduled_reinitialization_for.replace(Instant::now() + SCHEDULE_REINITIALIZATION_DELAY);

                known_caches.add_cache(cluster_cache_id, piece_caches);
            }
            _ = cache_pruning_interval.tick().fuse() => {
                let mut reinit = false;
                for removed_cache in known_caches.remove_expired() {
                    reinit = true;

                    warn!(
                        cluster_cache_id = %removed_cache.cluster_cache_id,
                        "Cache expired and removed, scheduling reinitialization"
                    );
                }

                if reinit {
                    scheduled_reinitialization_for.replace(
                        Instant::now() + SCHEDULE_REINITIALIZATION_DELAY,
                    );
                }
            }
            _ = cache_reinitialization => {
                // Nothing left to do
            }
        }
    }
}

/// Collect piece caches from the cache and convert them to `ClusterPieceCache` by sending a stream
/// request, then construct a `KnownCache` instance.
async fn collect_piece_caches(
    cluster_cache_id: ClusterCacheId,
    nats_client: &NatsClient,
) -> anyhow::Result<Vec<Arc<ClusterPieceCache>>> {
    let piece_caches = nats_client
        .stream_request(
            &ClusterCacheDetailsRequest,
            Some(&cluster_cache_id.to_string()),
        )
        .await
        .inspect_err(|error| {
            warn!(
                %error,
                %cluster_cache_id,
                "Failed to request farmer farm details"
            )
        })?
        .map(
            |ClusterPieceCacheDetails {
                 piece_cache_id,
                 max_num_elements,
             }| {
                debug!(
                    %cluster_cache_id,
                    %piece_cache_id,
                    %max_num_elements,
                    "Discovered new piece cache"
                );
                Arc::new(ClusterPieceCache::new(
                    piece_cache_id,
                    max_num_elements,
                    nats_client.clone(),
                ))
            },
        )
        .collect()
        .await;

    Ok(piece_caches)
}