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
//! TOML-based runtime configuration with hot reload.
//!
//! Why hot reload? The original code baked IPs, chunk size and link selection
//! into Rust constants or one-shot bitcmd packets. Re-tuning meant rebuilding
//! and restarting. parachuter watches its config file with `notify` so changes
//! apply within ~1 second without a restart, and the same values can also be
//! poked over the control socket – the file is the durable record, the socket
//! is the live override.
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use std::time::Duration;
use serde::{Deserialize, Serialize};
use crate::error::{Error, Result};
use crate::proto::DEFAULT_CHUNK_SIZE;
/// One named link, e.g. `pilot`, `los`, `tdrss`, `starlink`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Link {
/// Destination IP address packets are sent to.
pub ip: String,
/// UDP port packets are sent to.
pub port: u16,
/// Maximum sustained throughput in kilobits per second.
pub max_kbps: u64,
/// Optional comment describing the link.
#[serde(default)]
pub note: Option<String>,
}
/// Top-level configuration shared by every parachuter binary.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Config {
/// Sender-specific configuration.
pub sender: SenderConfig,
/// Receiver-specific configuration.
pub receiver: ReceiverConfig,
/// Cleaner-specific configuration.
pub cleaner: CleanerConfig,
/// Named links indexed by short name.
pub links: BTreeMap<String, Link>,
}
/// Configuration consumed by `parachuter sender`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SenderConfig {
/// IP the sender binds for outbound packets.
pub bind_ip: String,
/// Source port for outbound datagrams.
pub bind_port: u16,
/// Datagram size including the 32-byte header.
pub chunk_size: usize,
/// Active link key (must exist in `[links]`).
pub active_link: String,
/// Where the SQLite ledger lives.
pub ledger_path: PathBuf,
/// File-priority hierarchy used in `Auto` (and as a fallback elsewhere).
pub priority_dirs: Vec<PathBuf>,
/// File-priority hierarchy walked when the sender is in `Debug` state.
/// If empty, no auto-discovery happens in debug mode (manual `submit` /
/// `enqueue` still work). Defaults to empty.
#[serde(default)]
pub priority_dirs_debug: Vec<PathBuf>,
/// Optional whitelist of file extensions the auto-scanner is allowed to
/// pick up (matched case-insensitively against the file's tail; entries
/// may include or omit a leading dot, and may be compound, e.g.
/// `"fits.bz2"`). Defaults to empty (no restriction).
#[serde(default)]
pub include_extensions: Vec<String>,
/// Optional blacklist of file extensions the auto-scanner must skip.
/// Same matching rules as [`Self::include_extensions`]. Defaults to
/// empty. On clashes between this list and `include_extensions`, this
/// list wins (the file is skipped). The filter applies only to the
/// auto-scanner; explicit `parachuter ctl submit` always honours the
/// caller's path.
#[serde(default)]
pub skip_extensions: Vec<String>,
/// Path to the Unix domain socket the sender exposes for control.
pub control_socket: PathBuf,
/// Initial state.
pub initial_state: SenderState,
/// Calibration counters (mirrored from the SuperBIT downlinker).
pub images_per_dark: u32,
/// As above.
pub images_per_flat: u32,
/// As above.
pub images_per_bias: u32,
/// How often to poll the priority directories when the queue is empty.
pub recheck_period_secs: u64,
/// How often to dump the ledger as a CSV to ground.
pub status_dump_period_secs: u64,
}
/// Sender control states.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SenderState {
/// Sender pulls work from the priority directories and ships continuously.
Auto,
/// Sender only sends what is explicitly requested.
Manual,
/// Sender drops new work; useful while the link is down.
Paused,
/// Sender services in-flight retransmits but ignores fresh files.
Debug,
}
/// Configuration consumed by `parachuter receiver`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ReceiverConfig {
/// IP the receiver binds to.
pub bind_ip: String,
/// UDP port to bind.
pub bind_port: u16,
/// Holding directory for in-flight assemblies.
pub holding_dir: PathBuf,
/// Final destination for completed files. Each file lands at this
/// directory plus its full payload path, e.g. `/data/qsc/m31.fits.bz2`
/// becomes `<final_dir>/data/qsc/m31.fits.bz2`.
pub final_dir: PathBuf,
/// Path to the Unix domain socket the receiver exposes for control.
pub control_socket: PathBuf,
}
/// Configuration consumed by `parachuter cleaner`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CleanerConfig {
/// Holding directory for in-flight assemblies (must match the receiver).
pub holding_dir: PathBuf,
/// Final directory (must match the receiver). The reconciler checks it
/// for every file in the sender's ledger snapshot.
pub final_dir: PathBuf,
/// Path to the sender's control socket; cleaner queries it for queue
/// state to dedup requests.
pub sender_control_socket: PathBuf,
/// Per-link concurrency limits.
pub links: BTreeMap<String, LinkBudget>,
/// Active link key.
pub active_link: String,
/// Where to write a JSON state file with everything the cleaner has
/// requested (TTL dedup backstop).
pub state_path: PathBuf,
/// How often the cleaner main loop runs.
pub run_period_secs: u64,
/// How often the reconciler compares the newest downlinked ledger
/// snapshot against `final_dir` and re-requests files lost whole.
/// Default 6 hours. (`checker_period_secs` is accepted as an old name.)
#[serde(default = "default_reconcile_period", alias = "checker_period_secs")]
pub reconcile_period_secs: u64,
/// How long after a file was queued before the reconciler may call it
/// lost. Default 3 hours.
#[serde(default = "default_reconcile_grace")]
pub reconcile_grace_secs: u64,
/// How long an in-flight request stays in the dedup cache, in seconds.
pub dedup_ttl_secs: u64,
/// Path to the cleaner's own control socket.
pub control_socket: PathBuf,
}
fn default_reconcile_period() -> u64 {
6 * 60 * 60
}
fn default_reconcile_grace() -> u64 {
3 * 60 * 60
}
/// Bandwidth budget for one uplink.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct LinkBudget {
/// Max unacknowledged requests at any time.
pub max_in_flight: u32,
/// Minimum spacing between requests (milliseconds).
pub min_period_ms: u64,
}
impl Default for Config {
fn default() -> Self {
let mut links = BTreeMap::new();
links.insert(
"pilot".to_string(),
Link {
ip: "192.168.98.24".into(),
port: 41410,
max_kbps: 1000,
note: Some("Pilot link".into()),
},
);
links.insert(
"los".to_string(),
Link {
ip: "239.255.0.1".into(),
port: 41410,
max_kbps: 250,
note: Some("LOS multicast".into()),
},
);
links.insert(
"tdrss".to_string(),
Link {
ip: "239.255.0.2".into(),
port: 41410,
max_kbps: 30,
note: Some("TDRSS multicast".into()),
},
);
let mut cleaner_links = BTreeMap::new();
for k in ["pilot", "los", "tdrss"] {
cleaner_links.insert(
k.into(),
LinkBudget {
max_in_flight: 8,
min_period_ms: 500,
},
);
}
Self {
sender: SenderConfig {
bind_ip: "0.0.0.0".into(),
bind_port: 34646,
chunk_size: DEFAULT_CHUNK_SIZE,
active_link: "pilot".into(),
ledger_path: PathBuf::from("/var/lib/parachuter/ledger.sqlite"),
priority_dirs: vec![PathBuf::from("/data/parachuter/queue")],
priority_dirs_debug: Vec::new(),
include_extensions: Vec::new(),
skip_extensions: Vec::new(),
control_socket: PathBuf::from("/run/parachuter/sender.sock"),
initial_state: SenderState::Auto,
images_per_dark: 0,
images_per_flat: 0,
images_per_bias: 0,
recheck_period_secs: 100,
status_dump_period_secs: 6 * 60 * 60,
},
receiver: ReceiverConfig {
bind_ip: "0.0.0.0".into(),
bind_port: 41410,
holding_dir: PathBuf::from("/var/lib/parachuter/holding"),
final_dir: PathBuf::from("/var/lib/parachuter/downloads"),
control_socket: PathBuf::from("/run/parachuter/receiver.sock"),
},
cleaner: CleanerConfig {
holding_dir: PathBuf::from("/var/lib/parachuter/holding"),
final_dir: PathBuf::from("/var/lib/parachuter/downloads"),
sender_control_socket: PathBuf::from("/run/parachuter/sender.sock"),
links: cleaner_links,
active_link: "pilot".into(),
state_path: PathBuf::from("/var/lib/parachuter/cleaner-state.json"),
run_period_secs: 60,
reconcile_period_secs: default_reconcile_period(),
reconcile_grace_secs: default_reconcile_grace(),
dedup_ttl_secs: 300,
control_socket: PathBuf::from("/run/parachuter/cleaner.sock"),
},
links,
}
}
}
impl Config {
/// Load a config from disk, returning a [`Config::default`] augmented
/// with whatever the user has set. Missing fields use the defaults.
pub fn load(path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref();
let s = std::fs::read_to_string(path).map_err(|e| match e.kind() {
std::io::ErrorKind::NotFound => Error::NotFound(path.to_path_buf()),
_ => Error::Io(e),
})?;
let cfg: Self = toml::from_str(&s)?;
cfg.validate()?;
Ok(cfg)
}
/// Serialise the config back out as TOML.
pub fn to_toml(&self) -> String {
toml::to_string_pretty(self).expect("config serialises")
}
/// Validate cross-field invariants the type system can't express.
pub fn validate(&self) -> Result<()> {
if !self.links.contains_key(&self.sender.active_link) {
return Err(Error::BadConfig(format!(
"sender.active_link `{}` not declared in [links]",
self.sender.active_link
)));
}
if !self.cleaner.links.contains_key(&self.cleaner.active_link) {
return Err(Error::BadConfig(format!(
"cleaner.active_link `{}` has no budget in [cleaner.links]",
self.cleaner.active_link
)));
}
if !(crate::proto::MIN_CHUNK_SIZE..=crate::proto::MAX_CHUNK_SIZE)
.contains(&self.sender.chunk_size)
{
return Err(Error::BadChunkSize(self.sender.chunk_size));
}
Ok(())
}
}
/// Thread-safe handle that holds the *currently active* config, atomically
/// swapped whenever the file changes on disk.
#[derive(Clone)]
pub struct LiveConfig {
inner: Arc<RwLock<Arc<Config>>>,
path: PathBuf,
}
impl LiveConfig {
/// Load the file once and return a handle whose [`Self::current`] returns
/// the latest version. Spawn [`Self::watch`] in a background thread (or
/// tokio task) to enable hot reload.
pub fn load(path: impl Into<PathBuf>) -> Result<Self> {
let path = path.into();
let cfg = Config::load(&path)?;
Ok(Self {
inner: Arc::new(RwLock::new(Arc::new(cfg))),
path,
})
}
/// Cheap snapshot of the current configuration. The returned `Arc` is
/// stable even if the file is reloaded mid-call.
pub fn current(&self) -> Arc<Config> {
self.inner.read().unwrap().clone()
}
/// Replace the live config in memory (used by the control socket to apply
/// transient overrides without rewriting the TOML file).
pub fn override_with(&self, new: Config) -> Result<()> {
new.validate()?;
*self.inner.write().unwrap() = Arc::new(new);
Ok(())
}
/// Path of the file backing this config.
pub fn path(&self) -> &Path {
&self.path
}
/// Spawn a background thread that polls the file for changes and
/// hot-reloads it. Errors are logged via `tracing` and don't tear down
/// the process.
pub fn watch(self) -> Result<()> {
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
let (tx, rx) = std::sync::mpsc::channel();
let mut watcher: RecommendedWatcher = notify::recommended_watcher(tx)
.map_err(|e| Error::BadConfig(format!("watcher: {e}")))?;
watcher
.watch(&self.path, RecursiveMode::NonRecursive)
.map_err(|e| Error::BadConfig(format!("watch: {e}")))?;
std::thread::Builder::new()
.name("parachuter-config-watch".into())
.spawn(move || {
let _watcher = watcher;
while let Ok(_event) = rx.recv_timeout(Duration::from_secs(5)) {
match Config::load(&self.path) {
Ok(cfg) => {
tracing::info!(
path = %self.path.display(),
"reloaded config"
);
*self.inner.write().unwrap() = Arc::new(cfg);
}
Err(e) => tracing::warn!(?e, "failed to reload config"),
}
}
})?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn old_cleaner_keys_still_load() {
// A config written before reconciliation: `checker_period_secs` and
// `csv_dir` must not break it.
let mut c = Config::default();
c.cleaner.reconcile_period_secs = 1234;
let mut toml = c.to_toml().replace("reconcile_period_secs", "checker_period_secs");
toml = toml.replace("reconcile_grace_secs = 10800\n", "");
toml = toml.replace("[cleaner]\n", "[cleaner]\ncsv_dir = \"/var/lib/parachuter/csv\"\n");
let back: Config = toml::from_str(&toml).unwrap();
assert_eq!(back.cleaner.reconcile_period_secs, 1234);
assert_eq!(back.cleaner.reconcile_grace_secs, 3 * 60 * 60);
}
#[test]
fn default_is_valid() {
Config::default().validate().unwrap();
}
#[test]
fn rejects_unknown_active_link() {
let mut c = Config::default();
c.sender.active_link = "missing".into();
assert!(matches!(c.validate(), Err(Error::BadConfig(_))));
}
#[test]
fn rejects_silly_chunk_size() {
let mut c = Config::default();
c.sender.chunk_size = 4;
assert!(matches!(c.validate(), Err(Error::BadChunkSize(_))));
}
}