arcnix 0.3.5

A friendly desktop workspace for managing servers over SSH
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
//! Real SFTP transfers verify the pool's global file limit and cancellation.
use std::collections::HashSet;
use std::fs;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::{Duration, Instant};

use anyhow::{Context, Result, bail, ensure};
use arcnix_core::remote::{PendingConnection, RemoteSession};
use arcnix_core::transfer::{ConflictAction, FileProgress, FileStatus};
use arcnix_core::{ServerProfile, TransferDirection, TransferJob, TransferStatus, new_id};
use zeroize::Zeroizing;

use crate::transfer_worker::Pool;
use crate::worker::{ConflictDecision, ConflictScope, Response, Update};

struct Temp(PathBuf);
impl Temp {
    fn new() -> Result<Self> {
        let path = std::env::temp_dir().join(format!("arcnix-transfer-pool-{}", new_id()));
        fs::create_dir(&path)?;
        Ok(Self(path))
    }
}
impl Drop for Temp {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.0);
    }
}

fn receive_until(
    events: &async_channel::Receiver<Response>,
    deadline: Instant,
) -> Result<Option<Response>> {
    loop {
        match events.try_recv() {
            Ok(response) => return Ok(Some(response)),
            Err(async_channel::TryRecvError::Closed) => bail!("transfer event channel closed"),
            Err(async_channel::TryRecvError::Empty) if Instant::now() >= deadline => {
                return Ok(None);
            }
            Err(async_channel::TryRecvError::Empty) => std::thread::sleep(Duration::from_millis(5)),
        }
    }
}

fn receive(events: &async_channel::Receiver<Response>) -> Result<Response> {
    receive_until(events, Instant::now() + Duration::from_secs(15))?
        .context("transfer event timed out")
}

fn job(profile: &ServerProfile, local: &Path, remote: &str) -> TransferJob {
    TransferJob {
        id: new_id(),
        server_id: profile.id.clone(),
        direction: TransferDirection::Upload,
        local_path: local.to_owned(),
        remote_path: remote.to_owned(),
        total_bytes: 0,
        transferred_bytes: 0,
        status: TransferStatus::Queued,
    }
}

fn track(
    progress: FileProgress,
    active: &mut HashSet<usize>,
    completed: &mut HashSet<usize>,
    limit: usize,
) -> Result<()> {
    ensure!(
        progress.transferred_bytes <= progress.total_bytes,
        "file progress exceeded its total"
    );
    match progress.status {
        FileStatus::Running | FileStatus::Waiting => {
            active.insert(progress.index);
        }
        FileStatus::Completed => {
            active.remove(&progress.index);
            completed.insert(progress.index);
        }
        FileStatus::Failed(error) => bail!("fixture file failed: {error}"),
        FileStatus::Cancelled => bail!("fixture file was unexpectedly cancelled"),
        _ => {}
    }
    ensure!(
        active.len() <= limit,
        "more than {limit} file transfers are active"
    );
    Ok(())
}

fn roundtrip_with_limit(
    pool: &Pool,
    events: &async_channel::Receiver<Response>,
    session: &RemoteSession,
    profile: &ServerProfile,
    root: &Path,
    limit: usize,
    reduce_to: Option<usize>,
) -> Result<()> {
    const COUNT: usize = 12;
    let round_limit = limit;
    let source = root.join(format!("source-{limit}"));
    fs::create_dir(&source)?;
    let remote = format!("/pool-limit-{limit}-{}", new_id());
    session.mkdir(&remote)?;
    let seed = root.join("existing-target");
    fs::write(&seed, b"existing bytes")?;
    for index in 0..COUNT {
        let name = format!("file-{index:02}.bin");
        fs::write(source.join(&name), vec![index as u8; 96 * 1024])?;
        session.upload(
            &seed,
            &format!("{remote}/{name}"),
            &AtomicBool::new(false),
            |_, _| {},
        )?;
    }
    let transfer = job(profile, &source, &remote);
    let id = transfer.id.clone();
    pool.submit(
        91,
        limit as u64,
        transfer,
        Arc::new(AtomicBool::new(false)),
        Arc::default(),
        None,
    );
    let mut active = HashSet::new();
    let mut completed = HashSet::new();
    let mut replies = Vec::new();
    let mut saw_plan = false;
    // Existing target conflicts keep every admitted file active. A serial
    // implementation cannot reach the barrier, and an unbounded one exceeds it.
    while replies.len() < limit {
        match receive(events)?.update {
            Update::TransferPlan(actual, plan) => {
                ensure!(
                    actual == id && plan.is_directory && plan.files.len() == COUNT,
                    "wrong file manifest"
                );
                saw_plan = true;
            }
            Update::TransferFile(actual, progress) => {
                ensure!(actual == id, "file event belongs to another job");
                track(progress, &mut active, &mut completed, limit)?;
            }
            Update::TransferConflict(actual, _, reply) => {
                ensure!(actual == id, "conflict belongs to another job");
                replies.push(reply);
            }
            Update::Progress(..) => {}
            Update::Finished(_, result) => {
                bail!("transfer finished before concurrency barrier: {result:?}")
            }
            _ => bail!("unexpected event before concurrency barrier"),
        }
    }
    ensure!(
        saw_plan && active.len() == limit,
        "the configured number of files did not run concurrently"
    );
    let barrier = Instant::now() + Duration::from_millis(200);
    while let Some(response) = receive_until(events, barrier)? {
        match response.update {
            Update::Progress(..) => {}
            Update::TransferFile(actual, progress) => {
                ensure!(actual == id, "unexpected file event");
                track(progress, &mut active, &mut completed, limit)?;
            }
            Update::TransferConflict(..) => {
                bail!("another file bypassed the configured concurrency limit")
            }
            _ => bail!("unexpected event while all file slots were held"),
        }
    }
    if let Some(reduced) = reduce_to {
        pool.set_limit(reduced as u8);
    }
    for reply in replies {
        reply.send(ConflictDecision {
            action: ConflictAction::Overwrite,
            scope: ConflictScope::ThisFile,
        })?;
    }
    let limit = if let Some(reduced) = reduce_to {
        let original = limit;
        let mut replies = Vec::new();
        while replies.len() < reduced {
            match receive(events)?.update {
                Update::TransferFile(actual, progress) => {
                    ensure!(actual == id, "unexpected file event during limit reduction");
                    track(progress, &mut active, &mut completed, original)?;
                }
                Update::TransferConflict(actual, _, reply) => {
                    ensure!(actual == id, "unexpected conflict during limit reduction");
                    replies.push(reply);
                }
                Update::Progress(..) => {}
                _ => bail!("unexpected event while reducing the live file limit"),
            }
        }
        ensure!(
            completed.len() == original && active.len() == reduced,
            "lowering the live limit admitted extra files before active transfers finished"
        );
        let barrier = Instant::now() + Duration::from_millis(200);
        while let Some(response) = receive_until(events, barrier)? {
            match response.update {
                Update::TransferFile(actual, progress) => {
                    ensure!(actual == id, "unexpected file event after limit reduction");
                    track(progress, &mut active, &mut completed, reduced)?;
                }
                Update::Progress(..) => {}
                Update::TransferConflict(..) => {
                    bail!("a third file bypassed the reduced live limit")
                }
                _ => bail!("unexpected event while holding the reduced file slots"),
            }
        }
        for reply in replies {
            reply.send(ConflictDecision {
                action: ConflictAction::Overwrite,
                scope: ConflictScope::ThisFile,
            })?;
        }
        reduced
    } else {
        limit
    };
    let mut report = None;
    let mut last = 0;
    loop {
        match receive(events)?.update {
            Update::TransferFile(actual, progress) => {
                ensure!(actual == id, "unexpected file event");
                track(progress, &mut active, &mut completed, limit)?;
            }
            Update::TransferConflict(actual, _, reply) => {
                ensure!(actual == id, "unexpected conflict");
                reply.send(ConflictDecision {
                    action: ConflictAction::Overwrite,
                    scope: ConflictScope::ThisFile,
                })?;
            }
            Update::Progress(actual, done, total, _) => {
                ensure!(
                    actual == id && done >= last && done <= total,
                    "aggregate progress went backwards"
                );
                last = done;
            }
            Update::TransferReport(actual, summary) => {
                ensure!(
                    actual == id && summary.transferred_files == COUNT as u64,
                    "wrong transfer report"
                );
                report = Some(summary);
            }
            Update::Finished(actual, result) => {
                ensure!(actual == id, "unexpected completion");
                result.map_err(anyhow::Error::msg)?;
                break;
            }
            _ => bail!("unexpected event while completing the file transfer"),
        }
    }
    ensure!(
        report.is_some() && completed.len() == COUNT && active.is_empty(),
        "not all files completed"
    );
    for index in 0..COUNT {
        let name = format!("file-{index:02}.bin");
        let saved = root.join(format!("verify-{round_limit}-{index}"));
        session.download(
            &format!("{remote}/{name}"),
            &saved,
            &AtomicBool::new(false),
            |_, _| {},
        )?;
        ensure!(
            fs::read(saved)? == fs::read(source.join(name))?,
            "transferred file contents differ"
        );
    }
    Ok(())
}

#[test]
#[ignore = "requires tools/sftp_fixture.py and ARCNIX_TEST_SSH_ADDR"]
fn directory_file_pool_obeys_default_and_custom_limits_and_cancels_queued_files() -> Result<()> {
    let address: SocketAddr = std::env::var("ARCNIX_TEST_SSH_ADDR")?.parse()?;
    ensure!(address.ip().is_loopback(), "fixture must be loopback");
    let profile = ServerProfile {
        id: new_id(),
        host: address.ip().to_string(),
        port: address.port(),
        username: "arcnix".into(),
        ..Default::default()
    };
    let pending = PendingConnection::open(&profile)?;
    let fingerprint = pending.fingerprint().to_owned();
    let session = pending.authenticate("arcnix-test", &fingerprint)?;
    let (send, events) = async_channel::bounded(256);
    let generation = Arc::new(AtomicU64::new(91));
    let mut pool = Pool::new(send, generation);
    pool.attach(
        91,
        profile.clone(),
        fingerprint,
        Zeroizing::new("arcnix-test".into()),
    );
    let root = Temp::new()?;
    roundtrip_with_limit(&pool, &events, &session, &profile, &root.0, 5, Some(2))?;
    pool.set_limit(2);
    roundtrip_with_limit(&pool, &events, &session, &profile, &root.0, 2, None)?;

    // More files than active workers plus queue capacity exercise cancellation
    // while the producer is still submitting work, not just a small running job.
    let source = root.0.join("cancel-source");
    fs::create_dir(&source)?;
    for index in 0..96 {
        fs::write(
            source.join(format!("file-{index:03}")),
            b"fixture-only payload",
        )?;
    }
    let remote = format!("/pool-cancel-{}", new_id());
    session.mkdir(&remote)?;
    for index in 0..96 {
        session.upload(
            &source.join(format!("file-{index:03}")),
            &format!("{remote}/file-{index:03}"),
            &AtomicBool::new(false),
            |_, _| {},
        )?;
    }
    let transfer = job(&profile, &source, &remote);
    let id = transfer.id.clone();
    let cancel = Arc::new(AtomicBool::new(false));
    pool.submit(91, 30, transfer, cancel.clone(), Arc::default(), None);
    let mut prompts = Vec::new();
    loop {
        match receive(&events)?.update {
            Update::TransferPlan(actual, plan) => {
                ensure!(
                    actual == id && plan.files.len() == 96,
                    "wrong cancellation manifest"
                );
            }
            Update::TransferConflict(actual, _, reply) => {
                ensure!(actual == id, "wrong cancellation conflict");
                prompts.push(reply);
                if prompts.len() == 2 {
                    cancel.store(true, Ordering::Release);
                }
            }
            Update::Finished(actual, result) => {
                ensure!(
                    actual == id && result.is_err(),
                    "cancelled transfer did not terminate"
                );
                break;
            }
            Update::TransferFile(..) | Update::Progress(..) => {}
            _ => bail!("unexpected cancellation event"),
        }
    }
    ensure!(
        prompts.len() == 2,
        "expected both available file slots to be held during cancellation"
    );
    for reply in prompts {
        ensure!(
            reply
                .send(ConflictDecision {
                    action: ConflictAction::Overwrite,
                    scope: ConflictScope::ThisFile
                })
                .is_err(),
            "cancelled prompt still accepted a reply"
        );
    }
    let fresh_source = root.0.join("after-cancel");
    fs::write(&fresh_source, b"pool still available")?;
    let fresh_remote = format!("/pool-after-cancel-{}", new_id());
    let fresh = job(&profile, &fresh_source, &fresh_remote);
    let fresh_id = fresh.id.clone();
    pool.submit(
        91,
        31,
        fresh,
        Arc::new(AtomicBool::new(false)),
        Arc::default(),
        Some(ConflictAction::Overwrite),
    );
    loop {
        match receive(&events)?.update {
            Update::Finished(actual, result) => {
                ensure!(actual == fresh_id, "unexpected transfer completion");
                result.map_err(anyhow::Error::msg)?;
                break;
            }
            Update::TransferPlan(..)
            | Update::TransferFile(..)
            | Update::Progress(..)
            | Update::TransferReport(..) => {}
            _ => bail!("unexpected event after cancellation"),
        }
    }
    let verify = root.0.join("after-cancel-result");
    session.download(&fresh_remote, &verify, &AtomicBool::new(false), |_, _| {})?;
    ensure!(
        fs::read(verify)? == b"pool still available",
        "post-cancellation transfer differs"
    );
    Ok(())
}