vstorage/sync/
execute.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Copyright 2023-2024 Hugo Osvaldo Barrera
//
// SPDX-License-Identifier: EUPL-1.2

//! See [`Executor`] as the main type in this module.

use log::{debug, error, info, warn};

use crate::{
    base::{Item, ItemRef, Property, Storage},
    disco::DiscoveredCollection,
    CollectionId, Href,
};

use super::{
    error::SyncError,
    plan::{
        CollectionAction, CollectionPlan, ItemAction, Plan, PropertyAction, PropertyPlan,
        ResolvedMapping,
    },
    status::{ItemState, MappingUid, Side, StatusDatabase, StatusError},
};

/// Executes a plan or individual actions.
///
/// At this time, an executor can only execute a plan, but in future it may be able to execute a
/// stream of actions for keeping collections continuously in sync.
pub struct Executor<I: Item> {
    on_error: fn(SyncError<I>),
}

impl<I: Item> Executor<I> {
    /// Create a new instance.
    ///
    /// Use the given `on_error` function to handle non-fatal errors. See [`Executor::plan`] for
    /// further details on error handling.
    pub fn new(on_error: fn(SyncError<I>)) -> Executor<I> {
        Executor { on_error }
    }

    /// Executes a whole plan.
    ///
    /// # Errors
    ///
    /// Returns `Err(_)` if a fatal error occurred when interacting with the status database. Fatal
    /// errors are errors that occur when interacting with the status database, which tracks the
    /// current state of multiple storages. When a fatal error occurs, neither the status database
    /// nor the `Executor` instance should be re-used until the underlying issue is resolved.
    ///
    /// When a non-fatal error occurs, the `on_error` shall be invoked. E.g.: when the plan
    /// requires creating many items, if a single item fails, the error for this operation is
    /// passed to the `on_error` function, while the overall operation continues. This allows
    /// handling individual errors (e.g.: displaying them to a user) without interrupting the
    /// operation or having to wait for the completion of the entire operation.
    pub async fn plan(&self, plan: Plan<I>, status: &StatusDatabase) -> Result<(), StatusError> {
        let storage_a = plan.storage_a.as_ref();
        let storage_b = plan.storage_b.as_ref();

        // Clear these first, since they might conflict with a new mapping.
        if !plan.stale_collections.is_empty() {
            info!("Flushing stale collections: {:?}", plan.stale_collections);
            status.flush_stale_mappings(plan.stale_collections)?;
        }

        for plan in plan.collection_plans {
            let CollectionPlan {
                action,
                items,
                properties,
                mapping,
            } = plan;

            // Only executes creation or update actions. Deletions are done after item actions.
            let (mapping_uid, side_to_delete) = match self
                .collection(&action, status, &mapping, storage_a, storage_b)
                .await?
            {
                Ok((m, s)) => (m, s),
                Err(err) => {
                    (self.on_error)(SyncError::collection(action, mapping, err));
                    continue;
                }
            };

            for item in items {
                self.item(item, storage_a, storage_b, &mapping, status, mapping_uid)
                    .await?;
            }

            for prop in properties {
                self.property(prop, storage_a, storage_b, status, mapping_uid, &mapping)
                    .await?;
            }

            if let Some(side) = side_to_delete {
                // Presumably, item operations deleted all items. If another client created new items,
                // then the collection won't be empty and deletion will (rightfully) fail.
                let (storage, href) = match side {
                    Side::A => (storage_a, mapping.a().href()),
                    Side::B => (storage_b, mapping.b().href()),
                };
                if let Err(err) = delete_collection(href, status, storage, mapping_uid).await? {
                    let action = CollectionAction::Delete(mapping_uid, side);
                    (self.on_error)(SyncError::collection(action, mapping, err));
                };
            };
        }

        Ok(())
    }

    /// Execute this collection's action.
    ///
    /// Returns the [`MappingUid`] for this collection and the side that needs to be deleted, if
    /// any.
    ///
    /// # Errors
    ///
    /// - Returns `Err(_)` if a fatal error occurred when interacting with the status database.
    /// - Returns `Ok(Err(_))` in case of non-fatal error.
    async fn collection(
        &self,
        action: &CollectionAction,
        status: &StatusDatabase,
        mapping: &ResolvedMapping,
        storage_a: &dyn Storage<I>,
        storage_b: &dyn Storage<I>,
    ) -> Result<Result<(MappingUid, Option<Side>), ExecutionError>, StatusError> {
        let a = mapping.a();
        let b = mapping.b();
        match action {
            CollectionAction::NoAction(mapping_uid) => Ok(Ok((*mapping_uid, None))),
            CollectionAction::SaveToStatus => status
                .get_or_add_collection(a.href(), b.href())
                .map(|uid| Ok((uid, None))),
            CollectionAction::CreateInOne(side) => {
                let storage = match side {
                    Side::A => storage_a,
                    Side::B => storage_b,
                };
                create_collection(storage, status, mapping, *side)
                    .await
                    .map(|r| r.map(|uid| (uid, None)))
            }
            CollectionAction::CreateInBoth => {
                create_both_collections(storage_a, storage_b, mapping, status)
                    .await
                    .map(|r| r.map(|uid| (uid, None)))
            }
            CollectionAction::Delete(mapping, side) => Ok(Ok((*mapping, Some(*side)))),
        }
    }

    /// Execute action for a single item.
    ///
    /// # Errors
    ///
    /// - Returns `Err(_)` if a fatal error occurred when interacting with the status database.
    /// - Returns `Ok(Err(_))` in case of non-fatal error.
    #[inline]
    async fn item(
        &self,
        item: ItemAction<I>,
        a: &dyn Storage<I>,
        b: &dyn Storage<I>,
        mapping: &ResolvedMapping,
        status: &StatusDatabase,
        mapping_uid: MappingUid,
    ) -> Result<(), StatusError> {
        debug!("Executing item action: {item}");
        match &item {
            ItemAction::SaveToStatus { a, b, uid, hash } => {
                status.insert_item(mapping_uid, uid, hash, a, b)
            }
            ItemAction::UpdateStatus { hash, old, new } => {
                status.update_item(hash, &old.0, &old.1, &new.0, &new.1)
            }
            ItemAction::ClearStatus { uid } => status.delete_item(mapping_uid, uid),
            ItemAction::Create { side, source } => {
                match create_item(source, status, mapping, a, b, mapping_uid, *side).await {
                    Ok(Ok(())) => Ok(()),
                    Ok(Err(err)) => {
                        (self.on_error)(SyncError::item(item, err));
                        Ok(())
                    }
                    Err(err) => Err(err),
                }
            }
            ItemAction::Update {
                side,
                source,
                target,
                old,
            } => match update_item(a, b, source, target, old, status, *side).await {
                Ok(Ok(())) => Ok(()),
                Ok(Err(err)) => {
                    (self.on_error)(SyncError::item(item, err));
                    Ok(())
                }
                Err(err) => Err(err),
            },
            ItemAction::Delete { side, target, uid } => {
                let storage = if *side == Side::A { a } else { b };
                match delete_item(target, status, storage, mapping_uid, uid).await {
                    Ok(Ok(())) => Ok(()),
                    Ok(Err(err)) => {
                        (self.on_error)(SyncError::item(item, err));
                        Ok(())
                    }
                    Err(err) => Err(err),
                }
            }
            ItemAction::Conflict { a, .. } => {
                // TODO: should call on_error
                error!("Conflict for items {}. Skipping.", a.uid);
                Ok(())
            }
        }
    }

    /// Execute action for a single property
    ///
    /// # Errors
    ///
    /// Returns an error in case of a fatal error. See: [`Executor::plan`]
    async fn property(
        &self,
        plan: PropertyPlan<I>,
        a: &dyn Storage<I>,
        b: &dyn Storage<I>,
        status: &StatusDatabase,
        mapping_uid: MappingUid,
        mapping: &ResolvedMapping,
    ) -> Result<(), StatusError> {
        let href_a = mapping.a().href();
        let href_b = mapping.b().href();
        match &plan.action {
            PropertyAction::Write { value, side } => {
                let (storage, href) = match side {
                    Side::A => (a, href_a),
                    Side::B => (b, href_b),
                };
                if let Err(err) = storage
                    .set_property(href, plan.property.clone(), value)
                    .await
                {
                    (self.on_error)(SyncError::property(plan.action, err.into()));
                    Ok(())
                } else {
                    status.set_property(mapping_uid, href_a, href_b, &plan.property.name(), value)
                }
            }
            PropertyAction::Delete(side) => {
                let (storage, href) = match side {
                    Side::A => (a, href_a),
                    Side::B => (b, href_b),
                };
                if let Err(err) = storage.unset_property(href, plan.property.clone()).await {
                    (self.on_error)(SyncError::property(plan.action, err.into()));
                    Ok(())
                } else {
                    status.delete_property(mapping_uid, href_a, href_b, &plan.property.name())
                }
            }
            PropertyAction::ClearStatus => {
                status.delete_property(mapping_uid, href_a, href_b, &plan.property.name())
            }
            PropertyAction::UpdateStatus { value } => {
                status.set_property(mapping_uid, href_a, href_b, &plan.property.name(), value)
            }
            PropertyAction::Conflict => {
                // TODO: call on_error instead.
                error!("Conflict for property {}. Skipping.", plan.property.name());
                Ok(())
            }
        }
    }
}

/// Error during execution of a synchronisation [`Plan`]. See [`SyncError`].
#[derive(thiserror::Error, Debug)]
pub enum ExecutionError {
    #[error(transparent)]
    Storage(#[from] crate::Error),
    #[error("created collection {1} on side {0} does not have the expected id, it has: {2:?}")]
    IdMismatch(Side, Href, Option<CollectionId>),
}

async fn create_item<I: Item>(
    // TODO: Unused field: source.hash, source.uid
    source: &ItemState<I>,
    status: &StatusDatabase,
    mapping: &ResolvedMapping,
    storage_a: &dyn Storage<I>,
    storage_b: &dyn Storage<I>,
    mapping_uid: MappingUid,
    side: Side,
) -> Result<Result<(), ExecutionError>, StatusError> {
    debug!("Creating item from {}", source.href);

    let (target_collection, src_storage, dst_storage) = match side {
        Side::A => (mapping.a().href(), storage_b, storage_a),
        Side::B => (mapping.b().href(), storage_a, storage_b),
    };

    let (item_data, source_etag) = if let Some(data) = &source.data {
        (data.clone(), source.etag.clone())
    } else {
        warn!("Fetching item to create during execution");
        match src_storage.get_item(&source.href).await {
            Ok((i, e)) => (i, e),
            Err(err) => return Ok(Err(ExecutionError::Storage(err))),
        }
    };

    let uid = item_data.ident();
    let new_item = match dst_storage.add_item(target_collection, &item_data).await {
        Ok(i) => i,
        Err(err) => return Ok(Err(ExecutionError::Storage(err))),
    };

    // The original Etag MAY have changed.
    let source_ref = ItemRef {
        href: source.href.clone(),
        etag: source_etag,
    };

    match side {
        Side::A => status.insert_item(mapping_uid, &uid, &item_data.hash(), &new_item, &source_ref),
        Side::B => status.insert_item(mapping_uid, &uid, &item_data.hash(), &source_ref, &new_item),
    }?;

    Ok(Ok(()))
}

async fn update_item<I: Item>(
    storage_a: &dyn Storage<I>,
    storage_b: &dyn Storage<I>,
    // TODO: Unused field: source.hash, source.uid
    source: &ItemState<I>,
    target: &ItemRef,
    old: &(ItemRef, ItemRef),
    status: &StatusDatabase,
    side: Side,
) -> Result<Result<(), ExecutionError>, StatusError> {
    let (dst_storage, src_storage) = match side {
        Side::A => (storage_a, storage_b),
        Side::B => (storage_b, storage_a),
    };
    debug!("Updating from {}", source.href);
    let (source_item, source_etag) = if let Some(data) = &source.data {
        (data.clone(), source.etag.clone())
    } else {
        warn!("Fetching item to update during execution");
        match src_storage.get_item(&source.href).await {
            Ok((i, e)) => (i, e),
            Err(err) => return Ok(Err(ExecutionError::Storage(err))),
        }
    };

    let new_etag = match dst_storage
        .update_item(&target.href, &target.etag, &source_item)
        .await
    {
        Ok(i) => i,
        Err(err) => return Ok(Err(ExecutionError::Storage(err))),
    };

    let hash = source_item.hash();
    let target_ref = &ItemRef {
        href: target.href.clone(),
        etag: new_etag,
    };
    let source_ref = &ItemRef {
        href: source.href.clone(),
        etag: source_etag,
    };
    match side {
        Side::A => status.update_item(&hash, &old.0, &old.1, target_ref, source_ref),
        Side::B => status.update_item(&hash, &old.0, &old.1, source_ref, target_ref),
    }?;

    Ok(Ok(()))
}

async fn delete_item<I: Item>(
    target: &ItemRef,
    status: &StatusDatabase,
    storage: &dyn Storage<I>,
    mapping_uid: MappingUid,
    uid: &str,
) -> Result<Result<(), ExecutionError>, StatusError> {
    debug!("Deleting {}", target.href);
    match storage.delete_item(&target.href, &target.etag).await {
        Ok(()) => Ok(Ok(status.delete_item(mapping_uid, uid)?)),
        Err(err) => Ok(Err(ExecutionError::Storage(err))),
    }
}

async fn delete_collection<I: Item>(
    href: &Href,
    status: &StatusDatabase,
    storage: &dyn Storage<I>,
    mapping_uid: MappingUid,
) -> Result<Result<(), ExecutionError>, StatusError> {
    match storage.destroy_collection(href).await {
        Ok(()) => Ok(Ok(status.remove_collection(mapping_uid)?)),
        Err(err) => Ok(Err(ExecutionError::Storage(err))),
    }
}

/// Creates a collection and updates the state accordingly.
async fn create_collection<I: Item>(
    storage: &dyn Storage<I>,
    status: &StatusDatabase,
    mapping: &ResolvedMapping,
    side: Side,
) -> Result<Result<MappingUid, ExecutionError>, StatusError> {
    let (target, existing) = match side {
        Side::A => (mapping.a(), mapping.b()),
        Side::B => (mapping.b(), mapping.a()),
    };
    let new = match storage.create_collection(target.href()).await {
        Ok(c) => c,
        Err(err) => return Ok(Err(ExecutionError::Storage(err))),
    };
    if new.href() != target.href() {
        warn!(
            "Created collection has href {}, expected {}.",
            new.href(),
            target.href()
        );
    }

    if let Err(err) = check_id_matches_expected(target.id(), storage, new.href(), side).await {
        return Ok(Err(err));
    };
    let mapping_uid = match side {
        Side::A => status.add_collection(new.href(), existing.href()),
        Side::B => status.add_collection(existing.href(), new.href()),
    }?;
    Ok(Ok(mapping_uid))
}

async fn create_both_collections<I: Item>(
    storage_a: &dyn Storage<I>,
    storage_b: &dyn Storage<I>,
    mapping: &ResolvedMapping,
    status: &StatusDatabase,
) -> Result<Result<MappingUid, ExecutionError>, StatusError> {
    let href_a = mapping.a().href();
    let href_b = mapping.b().href();
    let id_a = mapping.a().id();
    let id_b = mapping.b().id();

    let new_a = match storage_a.create_collection(href_a).await {
        Ok(c) => c,
        Err(err) => return Ok(Err(ExecutionError::Storage(err))),
    };
    if let Err(err) = check_id_matches_expected(id_a, storage_a, new_a.href(), Side::A).await {
        return Ok(Err(err));
    };
    let new_b = match storage_b.create_collection(href_b).await {
        Ok(c) => c,
        Err(err) => return Ok(Err(ExecutionError::Storage(err))),
    };
    if let Err(err) = check_id_matches_expected(id_b, storage_b, new_b.href(), Side::B).await {
        return Ok(Err(err));
    };

    Ok(Ok(status.get_or_add_collection(href_a, href_b)?))
}

async fn check_id_matches_expected<I: Item>(
    expected_id: Option<&CollectionId>,
    storage: &dyn Storage<I>,
    collection: &Href,
    side: Side,
) -> Result<(), ExecutionError> {
    if let Some(expected_id) = expected_id {
        let disco = storage.discover_collections().await?;
        let created = disco.collections().iter().find(|c| c.href() == collection);
        // FIXME: returned error description is incorrect in case of None.
        let created_id = created.map(DiscoveredCollection::id);
        if created_id != Some(expected_id) {
            return Err(ExecutionError::IdMismatch(
                side,
                collection.to_string(),
                created_id.cloned(),
            ));
        }
    }
    Ok(())
}