summaryrefslogtreecommitdiffstats
path: root/src/compaction/local.rs
blob: adf87649b83b73cd33265f14ef632b7ba4b4a58f (plain)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! Per-node, in-memory compaction state: snapshots built ahead of the switch, switch
//! progress for reports, and archived documents of previous epochs.
//!
//! Nothing here is replicated or durable. After a restart the node simply has no
//! archive and rebuilds snapshots when needed.

use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::Duration;

use automerge::AutoCommit;
use serde::Serialize;
use tokio::sync::oneshot;
use tokio::time::Instant;

use super::records::{unix_millis, Retention};
use super::snapshot::Snapshot;
use crate::replica::Epoch;
use crate::transport::SnapshotReply;

#[derive(Default)]
pub(crate) struct Compaction {
    // A std mutex: every access is short and never spans an await.
    inner: Mutex<Inner>,
    /// Set while this node saves its document for a peer; one at a time.
    serving: AtomicBool,
}

#[derive(Default)]
struct Inner {
    /// Built by the coordinator at commit time; consumed by the local switch.
    prebuilt: HashMap<String, Snapshot>,
    /// When this node first saw a compaction committed.
    first_seen: HashMap<String, Instant>,
    status: HashMap<String, LocalStatus>,
    archive: Vec<Archived>,
    /// The chunk this node is waiting for: (peer, wanted epoch, offset).
    awaiting: Option<(String, Epoch, u64, oneshot::Sender<SnapshotReply>)>,
    catch_up: Option<CatchUp>,
    /// The saved document chunks are served from (one download source at a time).
    served: Option<Arc<Served>>,
}

/// A saved copy of this node's document, served to downloading peers.
pub(crate) struct Served {
    pub(crate) epoch: Epoch,
    pub(crate) bytes: Vec<u8>,
    pub(crate) digest: [u8; 32],
    pub(crate) saved_at: Instant,
}

/// A served copy is dropped this long after it was saved.
const SERVED_KEPT_FOR: Duration = Duration::from_secs(15 * 60);

/// Fetching a newer epoch from a peer.
#[derive(Clone, Debug)]
pub(crate) struct CatchUp {
    pub(crate) target: Epoch,
    pub(crate) attempts: u32,
    pub(crate) last_attempt: Instant,
    pub(crate) error: Option<String>,
}

#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CatchUpReport {
    pub(crate) target: String,
    pub(crate) attempts: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) error: Option<String>,
}

/// What happened to a compaction on this node.
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct LocalStatus {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) switched_at: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) replayed_paths: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) failed_paths: Option<usize>,
    /// Why the switch did not happen (yet).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) error: Option<String>,
}

struct Archived {
    epoch: Epoch,
    // Kept for later steps (serving or auditing old epochs); not read yet.
    #[allow(dead_code)]
    doc: AutoCommit,
    archived_at: u64,
    retention: Retention,
}

/// Summary of an archived epoch.
#[derive(Clone, Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ArchivedEpoch {
    pub(crate) generation: u64,
    pub(crate) id: String,
    pub(crate) archived_at: u64,
}

impl Compaction {
    fn lock(&self) -> MutexGuard<'_, Inner> {
        self.inner
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    pub(crate) fn store_prebuilt(&self, id: &str, snapshot: Snapshot) {
        self.lock().prebuilt.insert(id.to_owned(), snapshot);
    }

    pub(crate) fn take_prebuilt(&self, id: &str) -> Option<Snapshot> {
        self.lock().prebuilt.remove(id)
    }

    /// When this node first saw the compaction committed (now, on the first call).
    pub(crate) fn first_seen(&self, id: &str) -> Instant {
        *self
            .lock()
            .first_seen
            .entry(id.to_owned())
            .or_insert_with(Instant::now)
    }

    pub(crate) fn status(&self, id: &str) -> Option<LocalStatus> {
        self.lock().status.get(id).cloned()
    }

    pub(crate) fn set_status(&self, id: &str, status: LocalStatus) {
        self.lock().status.insert(id.to_owned(), status);
    }

    /// Keeps the previous epoch's document, then drops what has outlived its retention.
    pub(crate) fn archive(
        &self,
        epoch: Epoch,
        doc: AutoCommit,
        retention: Retention,
        current_generation: u64,
    ) {
        let mut inner = self.lock();
        inner.archive.push(Archived {
            epoch,
            doc,
            archived_at: unix_millis(),
            retention,
        });
        prune(&mut inner.archive, current_generation);
    }

    /// Drops archives past their retention and a served copy nobody needs any more.
    pub(crate) fn prune_archive(&self, current_generation: u64) {
        let mut inner = self.lock();
        prune(&mut inner.archive, current_generation);
        if inner
            .served
            .as_ref()
            .is_some_and(|served| served.saved_at.elapsed() > SERVED_KEPT_FOR)
        {
            inner.served = None;
        }
    }

    /// Registers interest in one chunk from `peer`, replacing any earlier request.
    pub(crate) fn await_reply(
        &self,
        peer: &str,
        wanted: Epoch,
        offset: u64,
    ) -> oneshot::Receiver<SnapshotReply> {
        let (sender, receiver) = oneshot::channel();
        self.lock().awaiting = Some((peer.to_owned(), wanted, offset, sender));
        receiver
    }

    /// Hands a reply to the waiting download. False unless it answers exactly the
    /// pending request (late replies to earlier attempts are dropped).
    pub(crate) fn deliver_reply(&self, peer: &str, reply: SnapshotReply) -> bool {
        let mut inner = self.lock();
        match inner.awaiting.take() {
            Some((expected, wanted, offset, sender))
                if expected == peer && wanted == reply.wanted && offset == reply.offset =>
            {
                sender.send(reply).is_ok()
            }
            other => {
                inner.awaiting = other;
                false
            }
        }
    }

    /// The copy served for `epoch`, if any.
    pub(crate) fn served(&self, epoch: Epoch) -> Option<Arc<Served>> {
        self.lock()
            .served
            .as_ref()
            .filter(|served| served.epoch == epoch)
            .cloned()
    }

    pub(crate) fn set_served(&self, served: Arc<Served>) {
        self.lock().served = Some(served);
    }

    pub(crate) fn stop_awaiting(&self) {
        self.lock().awaiting = None;
    }

    /// Claims the right to serve one snapshot; release with [`Compaction::done_serving`].
    pub(crate) fn try_start_serving(&self) -> bool {
        !self.serving.swap(true, Ordering::AcqRel)
    }

    pub(crate) fn done_serving(&self) {
        self.serving.store(false, Ordering::Release);
    }

    pub(crate) fn catch_up(&self) -> Option<CatchUp> {
        self.lock().catch_up.clone()
    }

    pub(crate) fn set_catch_up(&self, catch_up: Option<CatchUp>) {
        self.lock().catch_up = catch_up;
    }

    pub(crate) fn catch_up_report(&self) -> Option<CatchUpReport> {
        self.lock().catch_up.as_ref().map(|catch_up| CatchUpReport {
            target: catch_up.target.to_string(),
            attempts: catch_up.attempts,
            error: catch_up.error.clone(),
        })
    }

    pub(crate) fn archived_epochs(&self) -> Vec<ArchivedEpoch> {
        self.lock()
            .archive
            .iter()
            .map(|archived| ArchivedEpoch {
                generation: archived.epoch.generation,
                id: archived.epoch.id_hex(),
                archived_at: archived.archived_at,
            })
            .collect()
    }
}

/// An archive is kept until every limit of its retention has passed.
fn prune(archive: &mut Vec<Archived>, current_generation: u64) {
    let now = unix_millis();
    archive.retain(|archived| {
        let within_epochs = archived.retention.epochs.is_some_and(|epochs| {
            current_generation.saturating_sub(archived.epoch.generation) <= epochs
        });
        let within_time = archived.retention.seconds.is_some_and(|seconds| {
            now.saturating_sub(archived.archived_at) <= seconds.saturating_mul(1000)
        });
        within_epochs || within_time
    });
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn archive_retention() {
        let compaction = Compaction::default();
        let epoch = |generation| Epoch {
            generation,
            id: [generation as u8; 32],
        };

        // Kept by epoch count only.
        compaction.archive(
            epoch(0),
            AutoCommit::new(),
            Retention {
                epochs: Some(1),
                seconds: None,
            },
            1,
        );
        assert_eq!(compaction.archived_epochs().len(), 1);
        compaction.prune_archive(2);
        assert!(compaction.archived_epochs().is_empty());

        // Kept by time while the epoch count has passed.
        compaction.archive(
            epoch(1),
            AutoCommit::new(),
            Retention {
                epochs: Some(0),
                seconds: Some(3600),
            },
            5,
        );
        assert_eq!(compaction.archived_epochs().len(), 1);

        // No limits at all: not kept.
        compaction.archive(
            epoch(2),
            AutoCommit::new(),
            Retention {
                epochs: None,
                seconds: None,
            },
            3,
        );
        assert_eq!(compaction.archived_epochs().len(), 1);
    }
}