- // 1. get the current timestamp
- let snapshot_generation_timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
- let reference_timestamp = Self::round_down_to_nearest_multiple(snapshot_generation_timestamp, round_day_seconds);
- println!("Capturing snapshots at {} for: {}", snapshot_generation_timestamp, reference_timestamp);
-
- // 2. sleep until the next round 24 hours
- // 3. refresh all snapshots
-
- // the stored snapshots should adhere to the following format
- // from one day ago
- // from two days ago
- // …
- // from a week ago
- // from two weeks ago
- // from three weeks ago
- // full
- // That means that at any given moment, there should only ever be
- // 6 (daily) + 3 (weekly) + 1 (total) = 10 cached snapshots
- // The snapshots, unlike dynamic updates, should account for all intermediate
- // channel updates
-
- // purge and recreate the pending directories
- if fs::metadata(&pending_snapshot_directory).is_ok(){
- fs::remove_dir_all(&pending_snapshot_directory).expect("Failed to remove pending snapshot directory.");
- }
- if fs::metadata(&pending_symlink_directory).is_ok(){
- fs::remove_dir_all(&pending_symlink_directory).expect("Failed to remove pending symlink directory.");
+ self.generate_snapshots(config::SYMLINK_GRANULARITY_INTERVAL as u64, snapshot_interval, &snapshot_scopes, &cache_path(), None).await;
+
+ // constructing the snapshots may have taken a while
+ let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
+
+ // NOTE: we're waiting until the next multiple of snapshot_interval
+ // however, if the symlink granularity is lower, then during that time, no intermediate
+ // symlinks will be generated. That should be ok, because any timestamps previously
+ // returned would already have generated symlinks, but this does have bug potential
+ let remainder = current_time % snapshot_interval;
+ let time_until_next_generation = snapshot_interval - remainder;
+
+ log_info!(self.logger, "Sleeping until next snapshot capture: {}s", time_until_next_generation);
+ // add in an extra five seconds to assure the rounding down works correctly
+ let sleep = tokio::time::sleep(Duration::from_secs(time_until_next_generation + 5));
+ sleep.await;
+ }
+ }
+
+ pub(crate) async fn generate_snapshots(&self, granularity_interval: u64, snapshot_interval: u64, snapshot_scopes: &[u64], cache_path: &str, max_symlink_count: Option<u64>) {
+ let pending_snapshot_directory = format!("{}/snapshots_pending", cache_path);
+ let pending_symlink_directory = format!("{}/symlinks_pending", cache_path);
+ let finalized_snapshot_directory = format!("{}/snapshots", cache_path);
+ let finalized_symlink_directory = format!("{}/symlinks", cache_path);
+ let relative_symlink_to_snapshot_path = "../snapshots";
+
+ // 1. get the current timestamp
+ let snapshot_generation_timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
+ let reference_timestamp = Self::round_down_to_nearest_multiple(snapshot_generation_timestamp, snapshot_interval as u64);
+ log_info!(self.logger, "Capturing snapshots at {} for: {}", snapshot_generation_timestamp, reference_timestamp);
+
+ // 2. sleep until the next round interval
+ // 3. refresh all snapshots
+
+ // the stored snapshots should adhere to the following format
+ // from one day ago
+ // from two days ago
+ // …
+ // from a week ago
+ // from two weeks ago
+ // from three weeks ago
+ // full
+ // That means that at any given moment, there should only ever be
+ // 6 (daily) + 3 (weekly) + 1 (total) = 10 cached snapshots
+ // The snapshots, unlike dynamic updates, should account for all intermediate
+ // channel updates
+
+ // purge and recreate the pending directories
+ if fs::metadata(&pending_snapshot_directory).is_ok() {
+ fs::remove_dir_all(&pending_snapshot_directory).expect("Failed to remove pending snapshot directory.");
+ }
+ if fs::metadata(&pending_symlink_directory).is_ok() {
+ fs::remove_dir_all(&pending_symlink_directory).expect("Failed to remove pending symlink directory.");
+ }
+ fs::create_dir_all(&pending_snapshot_directory).expect("Failed to create pending snapshot directory");
+ fs::create_dir_all(&pending_symlink_directory).expect("Failed to create pending symlink directory");
+
+ let mut snapshot_sync_timestamps: Vec<(u64, u64)> = Vec::new();
+ for current_scope in snapshot_scopes {
+ let timestamp = reference_timestamp.saturating_sub(current_scope.clone());
+ snapshot_sync_timestamps.push((current_scope.clone(), timestamp));
+ };
+
+ let mut snapshot_filenames_by_scope: HashMap<u64, String> = HashMap::with_capacity(10);
+
+ for (current_scope, current_last_sync_timestamp) in &snapshot_sync_timestamps {
+ let network_graph_clone = self.network_graph.clone();
+ {
+ log_info!(self.logger, "Calculating {}-second snapshot", current_scope);
+ // calculate the snapshot
+ let snapshot = super::serialize_delta(network_graph_clone, current_last_sync_timestamp.clone() as u32, self.logger.clone()).await;
+
+ // persist the snapshot and update the symlink
+ let snapshot_filename = format!("snapshot__calculated-at:{}__range:{}-scope__previous-sync:{}.lngossip", reference_timestamp, current_scope, current_last_sync_timestamp);
+ let snapshot_path = format!("{}/{}", pending_snapshot_directory, snapshot_filename);
+ log_info!(self.logger, "Persisting {}-second snapshot: {} ({} messages, {} announcements, {} updates ({} full, {} incremental))", current_scope, snapshot_filename, snapshot.message_count, snapshot.announcement_count, snapshot.update_count, snapshot.update_count_full, snapshot.update_count_incremental);
+ fs::write(&snapshot_path, snapshot.data).unwrap();
+ snapshot_filenames_by_scope.insert(current_scope.clone(), snapshot_filename);