agde/sync.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
//! Module for handling the sync between piers.
//!
//! This occurs when the hashes don't line up or when fast forwarding on a new connection.
use std::cmp;
use std::collections::{hash_map, HashMap};
use std::time::{Duration, SystemTime};
use serde::{Deserialize, Serialize};
use crate::{event, log, Manager, Uuid};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Copy)]
pub(crate) enum RevertTo {
/// Don't revert.
Latest,
/// Revert all the events in our [`crate::log`].
Origin,
/// Revert so our data is at the message with the contained UUID.
To(Uuid),
}
/// Request to sync selected resources.
///
/// Obtained from [`RequestBuilder`], which you get from
/// [`crate::Manager::apply_hash_check_reply`]. This is the last request of the series of assuring
/// data is the same across all piers.
// `TODO`: send Signature of event log.
// ↑ We won't have to send as much data.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[must_use]
pub struct Request {
pier: Uuid,
signatures: HashMap<String, dach::Signature>,
log_settings: (Duration, u32),
revert: RevertTo,
}
impl Request {
pub(crate) fn recipient(&self) -> Uuid {
self.pier
}
}
/// A builder struct for a [`Request`].
///
/// See [`crate::Manager::apply_hash_check_reply`] for usage details.
#[derive(Debug)]
#[must_use]
pub struct RequestBuilder {
req: Request,
}
impl RequestBuilder {
pub(crate) fn new(
pier: Uuid,
log_lifetime: Duration,
log_limit: u32,
revert: RevertTo,
) -> Self {
Self {
req: Request {
pier,
signatures: HashMap::new(),
log_settings: (log_lifetime, log_limit),
revert,
},
}
}
/// Insert the `resource`'s `signature` to this response.
///
/// The [`dach::Signature`] allows the pier to get the diff for us.
pub fn insert(&mut self, resource: String, signature: dach::Signature) -> &mut Self {
self.req.signatures.insert(resource, signature);
self
}
/// Make a [`Request`] from this builder and a signature of all the resources inserted.
///
/// Call [`crate::Manager::process_sync_reply`] to get a [`crate::Message`].
#[inline]
pub fn finish(self) -> Request {
self.req
}
}
/// The diffs to make the pier's data the same as ours.
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[must_use]
pub struct Response {
pier: Uuid,
log: Vec<log::ReceivedEvent>,
diff: Vec<(String, dach::Difference)>,
delete: Vec<String>,
revert: RevertTo,
}
impl Response {
pub(crate) fn recipient(&self) -> Uuid {
self.pier
}
/// Extract the event log. After this function, it's reset to an empty list.
pub(crate) fn take_event_log(&mut self) -> Vec<log::ReceivedEvent> {
std::mem::take(&mut self.log)
}
pub(crate) fn revert(&self) -> RevertTo {
self.revert
}
/// Returns a list with `(resource, diff)`,
/// where you should call [`dach::Difference::apply`] on the data
/// `resource` holds.
pub fn diff(&self) -> &[(impl AsRef<str>, dach::Difference)] {
&self.diff
}
/// Returns a list with `resource`,
/// where you should delete `resource`.
#[must_use]
pub fn delete(&self) -> &[impl AsRef<str>] {
&self.delete
}
}
/// Builder for a [`Response`].
///
/// Follow the instructions from how you got this builder and [insert](`Self::add_diff`) the appropriate
/// resources.
/// Execute the action returned by the aforementioned function.
#[derive(Debug)]
pub struct ResponseBuilder<'a> {
request: &'a Request,
signature_iter: hash_map::Iter<'a, String, dach::Signature>,
pier: Uuid,
/// Binary sorted by String
diff: Vec<(String, dach::Difference)>,
unwinder: Option<event::Unwinder<'a>>,
}
impl<'a> ResponseBuilder<'a> {
pub(crate) fn new(request: &'a Request, pier: Uuid, manager: &'a Manager) -> Self {
Self {
request,
signature_iter: request.signatures.iter(),
pier,
diff: Vec::new(),
unwinder: match request.revert {
RevertTo::Latest => None,
RevertTo::Origin => Some(manager.unwinder_to(SystemTime::UNIX_EPOCH)),
RevertTo::To(uuid) => Some({
let events_start = manager.event_log.cutoff_from_uuid(uuid).unwrap_or(0);
let events = &manager.event_log.list[events_start + 1..];
event::Unwinder::new(events, Some(manager))
}),
},
}
}
/// Use this in a `while let Some((resource, signature)) = response_builder.next_signature()`
/// loop to add all the returned values to [`Self::add_diff`].
///
/// If you don't have a resource, just don't call [`Self::add_diff`]. Agde will then automatically
/// send a delete event then.
pub fn next_signature(
&mut self,
) -> Option<(&str, &dach::Signature, Option<&mut event::Unwinder<'a>>)> {
self.unwinder();
let unwinder = self.unwinder.as_mut();
self.signature_iter.next().map(|(k, v)| (&**k, v, unwinder))
}
/// Returns the unwinder (if any) to use for the `resource` before [adding](Self::add_diff) it.
pub fn unwinder(&mut self) -> Option<&mut event::Unwinder<'a>> {
self.unwinder.as_mut().map(|unwinder| {
unwinder.clear_unwound();
unwinder
})
}
/// Tell the requester their `resource` needs to apply `diff` to get our data.
///
/// You have to unwind `resource` before getting the `diff` if [`Self::unwinder`] returns
/// [`Some`].
///
/// # Panics
///
/// Panics if you've called this with the same `resource` before.
pub fn add_diff(&mut self, resource: String, diff: dach::Difference) -> &mut Self {
if let Err(idx) = self.diff.binary_search_by(|item| item.0.cmp(&resource)) {
self.diff.insert(idx, (resource, diff));
} else {
panic!("you cannot add two diffs with the same resource name");
}
self
}
/// Finish the response.
pub fn finish(mut self, manager: &Manager) -> Response {
let mut delete = Vec::new();
for resource in self.request.signatures.keys() {
if self
.diff
.binary_search_by(|item| item.0.cmp(resource))
.is_err()
{
delete.push(resource.clone());
}
}
let max = cmp::min(
manager
.event_log
.cutoff_from_time(self.request.log_settings.0)
.unwrap_or(manager.event_log.limit() as usize),
self.request.log_settings.1 as usize,
);
let event_log = manager.event_log.get_max(max).to_vec();
let last = event_log.last().map(|ev| ev.message_uuid);
self.diff.retain(|diff| !diff.1.is_empty());
Response {
pier: self.pier,
log: event_log,
diff: self.diff,
delete,
revert: last.map_or(RevertTo::Origin, RevertTo::To),
}
}
}