Allow the snapshot interval to be configurable in the environment variables
[rapid-gossip-sync-server] / src / snapshot.rs
index a27eafdfe0de702cb57db02048e1fd0d02a45532..28d0768d65ede059651d16bcf6dc3421a681ce58 100644 (file)
@@ -25,8 +25,10 @@ impl<L: Deref + Clone> Snapshotter<L> where L::Target: Logger {
        pub(crate) async fn snapshot_gossip(&self) {
                log_info!(self.logger, "Initiating snapshotting service");
 
+               let calc_interval = config::calculate_interval();
                let snapshot_sync_day_factors = [1, 2, 3, 4, 5, 6, 7, 14, 21, u64::MAX];
                const DAY_SECONDS: u64 = 60 * 60 * 24;
+               let round_day_seconds = calc_interval as u64;
 
                let pending_snapshot_directory = format!("{}/snapshots_pending", cache_path());
                let pending_symlink_directory = format!("{}/symlinks_pending", cache_path());
@@ -41,7 +43,7 @@ impl<L: Deref + Clone> Snapshotter<L> where L::Target: Logger {
                        let reference_timestamp = Self::round_down_to_nearest_multiple(snapshot_generation_timestamp, config::SNAPSHOT_CALCULATION_INTERVAL as u64);
                        log_info!(self.logger, "Capturing snapshots at {} for: {}", snapshot_generation_timestamp, reference_timestamp);
 
-                       // 2. sleep until the next round 24 hours
+                       // 2. sleep until the next round interval
                        // 3. refresh all snapshots
 
                        // the stored snapshots should adhere to the following format
@@ -115,11 +117,27 @@ impl<L: Deref + Clone> Snapshotter<L> where L::Target: Logger {
                                        // special-case 0 to always refer to a full/initial sync
                                        u64::MAX
                                } else {
+                                       /*
+                                       We have snapshots for 6-day- and 7-day-intervals, but the next interval is
+                                       14 days. So if somebody requests an update with a timestamp that is 10 days old,
+                                       there is no longer a snapshot for that specific interval.
+
+                                       The correct snapshot will be the next highest interval, i. e. for 14 days.
+
+                                       The `snapshot_sync_day_factors` array is sorted ascendingly, so find() will
+                                       return on the first iteration that is at least equal to the requested interval.
+
+                                       Note, however, that the last value in the array is u64::max, which means that
+                                       multiplying it with DAY_SECONDS will overflow. To avoid that, we use
+                                       saturating_mul.
+                                        */
+
                                        // find min(x) in snapshot_sync_day_factors where x >= i
                                        snapshot_sync_day_factors.iter().find(|x| {
-                                               *x * DAY_SECONDS >= i * config::SNAPSHOT_CALCULATION_INTERVAL as u64
+                                               DAY_SECONDS.saturating_mul(**x) >= i * config::SNAPSHOT_CALCULATION_INTERVAL as u64
                                        }).unwrap().clone()
                                };
+                               log_info!(self.logger, "i: {}, referenced day range: {}", i, referenced_day_range);
 
                                let snapshot_filename = snapshot_filenames_by_day_range.get(&referenced_day_range).unwrap();
                                let relative_snapshot_path = format!("{}/{}", relative_symlink_to_snapshot_path, snapshot_filename);