-
Notifications
You must be signed in to change notification settings - Fork 524
/
mod.rs
648 lines (599 loc) · 21.2 KB
/
mod.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#[cfg(any(feature = "dist-client", feature = "dist-server"))]
use sccache::config::HTTPUrl;
use sccache::dist::{self, SchedulerStatusResult, ServerId};
use sccache::server::ServerInfo;
use std::env;
use std::fs;
use std::io::Write;
use std::net::{self, IpAddr, SocketAddr};
use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};
use std::str::{self, FromStr};
use std::thread;
use std::time::{Duration, Instant};
use assert_cmd::prelude::*;
#[cfg(feature = "dist-server")]
use nix::{
sys::{
signal::Signal,
wait::{WaitPidFlag, WaitStatus},
},
unistd::{ForkResult, Pid},
};
use predicates::prelude::*;
use serde::Serialize;
use uuid::Uuid;
const CONTAINER_NAME_PREFIX: &str = "sccache_dist_test";
const DIST_IMAGE: &str = "sccache_dist_test_image";
const DIST_DOCKERFILE: &str = include_str!("Dockerfile.sccache-dist");
const DIST_IMAGE_BWRAP_PATH: &str = "/usr/bin/bwrap";
const MAX_STARTUP_WAIT: Duration = Duration::from_secs(5);
const DIST_SERVER_TOKEN: &str = "THIS IS THE TEST TOKEN";
const CONFIGS_CONTAINER_PATH: &str = "/sccache-bits";
const BUILD_DIR_CONTAINER_PATH: &str = "/sccache-bits/build-dir";
const SCHEDULER_PORT: u16 = 10500;
const SERVER_PORT: u16 = 12345; // arbitrary
const TC_CACHE_SIZE: u64 = 1024 * 1024 * 1024; // 1 gig
pub fn start_local_daemon(cfg_path: &Path, cached_cfg_path: &Path) {
// Don't run this with run() because on Windows `wait_with_output`
// will hang because the internal server process is not detached.
let _ = sccache_command()
.arg("--start-server")
// Uncomment following lines to debug locally.
// .env("SCCACHE_LOG", "debug")
// .env("SCCACHE_ERROR_LOG", "/tmp/sccache_log.txt")
.env("SCCACHE_CONF", cfg_path)
.env("SCCACHE_CACHED_CONF", cached_cfg_path)
.status()
.unwrap()
.success();
}
pub fn stop_local_daemon() {
trace!("sccache --stop-server");
drop(
sccache_command()
.arg("--stop-server")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status(),
);
}
pub fn get_stats<F: 'static + Fn(ServerInfo)>(f: F) {
sccache_command()
.args(&["--show-stats", "--stats-format=json"])
.assert()
.success()
.stdout(predicate::function(move |output: &[u8]| {
let s = str::from_utf8(output).expect("Output not UTF-8");
let stats = serde_json::from_str(s).expect("Failed to parse JSON stats");
eprintln!("get server stats: {stats:?}");
f(stats);
true
}));
}
#[allow(unused)]
pub fn zero_stats() {
trace!("sccache --zero-stats");
drop(
sccache_command()
.arg("--zero-stats")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status(),
);
}
pub fn write_json_cfg<T: Serialize>(path: &Path, filename: &str, contents: &T) {
let p = path.join(filename);
let mut f = fs::File::create(&p).unwrap();
f.write_all(&serde_json::to_vec(contents).unwrap()).unwrap();
}
pub fn write_source(path: &Path, filename: &str, contents: &str) {
let p = path.join(filename);
let mut f = fs::File::create(&p).unwrap();
f.write_all(contents.as_bytes()).unwrap();
}
// Prune any environment variables that could adversely affect test execution.
pub fn sccache_command() -> Command {
use sccache::util::OsStrExt;
let mut cmd = Command::new(assert_cmd::cargo::cargo_bin("sccache"));
for (var, _) in env::vars_os() {
if var.starts_with("SCCACHE_") {
cmd.env_remove(var);
}
}
cmd
}
#[cfg(feature = "dist-server")]
pub fn sccache_dist_path() -> PathBuf {
assert_cmd::cargo::cargo_bin("sccache-dist")
}
pub fn sccache_client_cfg(tmpdir: &Path) -> sccache::config::FileConfig {
let cache_relpath = "client-cache";
let dist_cache_relpath = "client-dist-cache";
fs::create_dir(tmpdir.join(cache_relpath)).unwrap();
fs::create_dir(tmpdir.join(dist_cache_relpath)).unwrap();
let disk_cache = sccache::config::DiskCacheConfig {
dir: tmpdir.join(cache_relpath),
..Default::default()
};
sccache::config::FileConfig {
cache: sccache::config::CacheConfigs {
azure: None,
disk: Some(disk_cache),
gcs: None,
gha: None,
memcached: None,
redis: None,
s3: None,
},
dist: sccache::config::DistConfig {
auth: Default::default(), // dangerously_insecure
scheduler_url: None,
cache_dir: tmpdir.join(dist_cache_relpath),
toolchains: vec![],
toolchain_cache_size: TC_CACHE_SIZE,
rewrite_includes_only: false, // TODO
},
server_startup_timeout_ms: None,
}
}
#[cfg(feature = "dist-server")]
fn sccache_scheduler_cfg() -> sccache::config::scheduler::Config {
sccache::config::scheduler::Config {
public_addr: SocketAddr::from(([0, 0, 0, 0], SCHEDULER_PORT)),
client_auth: sccache::config::scheduler::ClientAuth::Insecure,
server_auth: sccache::config::scheduler::ServerAuth::Token {
token: DIST_SERVER_TOKEN.to_owned(),
},
}
}
#[cfg(feature = "dist-server")]
fn sccache_server_cfg(
tmpdir: &Path,
scheduler_url: HTTPUrl,
server_ip: IpAddr,
) -> sccache::config::server::Config {
let relpath = "server-cache";
fs::create_dir(tmpdir.join(relpath)).unwrap();
sccache::config::server::Config {
builder: sccache::config::server::BuilderType::Overlay {
build_dir: BUILD_DIR_CONTAINER_PATH.into(),
bwrap_path: DIST_IMAGE_BWRAP_PATH.into(),
},
cache_dir: Path::new(CONFIGS_CONTAINER_PATH).join(relpath),
public_addr: SocketAddr::new(server_ip, SERVER_PORT),
scheduler_url,
scheduler_auth: sccache::config::server::SchedulerAuth::Token {
token: DIST_SERVER_TOKEN.to_owned(),
},
toolchain_cache_size: TC_CACHE_SIZE,
}
}
// TODO: this is copied from the sccache-dist binary - it's not clear where would be a better place to put the
// code so that it can be included here
#[cfg(feature = "dist-server")]
fn create_server_token(server_id: ServerId, auth_token: &str) -> String {
format!("{} {}", server_id.addr(), auth_token)
}
#[cfg(feature = "dist-server")]
pub enum ServerHandle {
Container { cid: String, url: HTTPUrl },
Process { pid: Pid, url: HTTPUrl },
}
#[cfg(feature = "dist-server")]
pub struct DistSystem {
sccache_dist: PathBuf,
tmpdir: PathBuf,
scheduler_name: Option<String>,
server_names: Vec<String>,
server_pids: Vec<Pid>,
}
#[cfg(feature = "dist-server")]
impl DistSystem {
pub fn new(sccache_dist: &Path, tmpdir: &Path) -> Self {
// Make sure the docker image is available, building it if necessary
let mut child = Command::new("docker")
.args(&["build", "-q", "-t", DIST_IMAGE, "-"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
child
.stdin
.as_mut()
.unwrap()
.write_all(DIST_DOCKERFILE.as_bytes())
.unwrap();
let output = child.wait_with_output().unwrap();
check_output(&output);
let tmpdir = tmpdir.join("distsystem");
fs::create_dir(&tmpdir).unwrap();
Self {
sccache_dist: sccache_dist.to_owned(),
tmpdir,
scheduler_name: None,
server_names: vec![],
server_pids: vec![],
}
}
pub fn add_scheduler(&mut self) {
let scheduler_cfg_relpath = "scheduler-cfg.json";
let scheduler_cfg_path = self.tmpdir.join(scheduler_cfg_relpath);
let scheduler_cfg_container_path =
Path::new(CONFIGS_CONTAINER_PATH).join(scheduler_cfg_relpath);
let scheduler_cfg = sccache_scheduler_cfg();
fs::File::create(&scheduler_cfg_path)
.unwrap()
.write_all(&serde_json::to_vec(&scheduler_cfg).unwrap())
.unwrap();
// Create the scheduler
let scheduler_name = make_container_name("scheduler");
let output = Command::new("docker")
.args(&[
"run",
"--name",
&scheduler_name,
"-e",
"SCCACHE_NO_DAEMON=1",
"-e",
"SCCACHE_LOG=sccache=trace",
"-e",
"RUST_BACKTRACE=1",
"--network",
"host",
"-v",
&format!("{}:/sccache-dist", self.sccache_dist.to_str().unwrap()),
"-v",
&format!(
"{}:{}",
self.tmpdir.to_str().unwrap(),
CONFIGS_CONTAINER_PATH
),
"-d",
DIST_IMAGE,
"bash",
"-c",
&format!(
r#"
set -o errexit &&
exec /sccache-dist scheduler --config {cfg}
"#,
cfg = scheduler_cfg_container_path.to_str().unwrap()
),
])
.output()
.unwrap();
self.scheduler_name = Some(scheduler_name);
check_output(&output);
let scheduler_url = self.scheduler_url();
wait_for_http(scheduler_url, Duration::from_millis(100), MAX_STARTUP_WAIT);
wait_for(
|| {
let status = self.scheduler_status();
if matches!(
status,
SchedulerStatusResult {
num_servers: 0,
num_cpus: _,
in_progress: 0
}
) {
Ok(())
} else {
Err(format!("{:?}", status))
}
},
Duration::from_millis(100),
MAX_STARTUP_WAIT,
);
}
pub fn add_server(&mut self) -> ServerHandle {
let server_cfg_relpath = format!("server-cfg-{}.json", self.server_names.len());
let server_cfg_path = self.tmpdir.join(&server_cfg_relpath);
let server_cfg_container_path = Path::new(CONFIGS_CONTAINER_PATH).join(server_cfg_relpath);
let server_name = make_container_name("server");
let output = Command::new("docker")
.args(&[
"run",
// Important for the bubblewrap builder
"--privileged",
"--name",
&server_name,
"-e",
"SCCACHE_LOG=sccache=trace",
"-e",
"RUST_BACKTRACE=1",
"--network",
"host",
"-v",
&format!("{}:/sccache-dist", self.sccache_dist.to_str().unwrap()),
"-v",
&format!(
"{}:{}",
self.tmpdir.to_str().unwrap(),
CONFIGS_CONTAINER_PATH
),
"-d",
DIST_IMAGE,
"bash",
"-c",
&format!(
r#"
set -o errexit &&
while [ ! -f {cfg}.ready ]; do sleep 0.1; done &&
exec /sccache-dist server --config {cfg}
"#,
cfg = server_cfg_container_path.to_str().unwrap()
),
])
.output()
.unwrap();
self.server_names.push(server_name.clone());
check_output(&output);
let server_ip = IpAddr::from_str("127.0.0.1").unwrap();
let server_cfg = sccache_server_cfg(&self.tmpdir, self.scheduler_url(), server_ip);
fs::File::create(&server_cfg_path)
.unwrap()
.write_all(&serde_json::to_vec(&server_cfg).unwrap())
.unwrap();
fs::File::create(format!("{}.ready", server_cfg_path.to_str().unwrap())).unwrap();
let url = HTTPUrl::from_url(
reqwest::Url::parse(&format!("https://{}:{}", server_ip, SERVER_PORT)).unwrap(),
);
let handle = ServerHandle::Container {
cid: server_name,
url,
};
self.wait_server_ready(&handle);
handle
}
pub fn add_custom_server<S: dist::ServerIncoming + 'static>(
&mut self,
handler: S,
) -> ServerHandle {
let server_addr = {
let ip = IpAddr::from_str("127.0.0.1").unwrap();
let listener = net::TcpListener::bind(SocketAddr::from((ip, 0))).unwrap();
listener.local_addr().unwrap()
};
let token = create_server_token(ServerId::new(server_addr), DIST_SERVER_TOKEN);
let server =
dist::http::Server::new(server_addr, self.scheduler_url().to_url(), token, handler)
.unwrap();
let pid = match unsafe { nix::unistd::fork() }.unwrap() {
ForkResult::Parent { child } => {
self.server_pids.push(child);
child
}
ForkResult::Child => {
env::set_var("SCCACHE_LOG", "sccache=trace");
env_logger::try_init().unwrap();
void::unreachable(server.start().unwrap())
}
};
let url =
HTTPUrl::from_url(reqwest::Url::parse(&format!("https://{}", server_addr)).unwrap());
let handle = ServerHandle::Process { pid, url };
self.wait_server_ready(&handle);
handle
}
pub fn restart_server(&mut self, handle: &ServerHandle) {
match handle {
ServerHandle::Container { cid, url: _ } => {
let output = Command::new("docker")
.args(&["restart", cid])
.output()
.unwrap();
check_output(&output);
}
ServerHandle::Process { pid: _, url: _ } => {
// TODO: pretty easy, just no need yet
panic!("restart not yet implemented for pids")
}
}
self.wait_server_ready(handle)
}
pub fn wait_server_ready(&mut self, handle: &ServerHandle) {
let url = match handle {
ServerHandle::Container { cid: _, url } | ServerHandle::Process { pid: _, url } => {
url.clone()
}
};
wait_for_http(url, Duration::from_millis(100), MAX_STARTUP_WAIT);
wait_for(
|| {
let status = self.scheduler_status();
if matches!(
status,
SchedulerStatusResult {
num_servers: 1,
num_cpus: _,
in_progress: 0
}
) {
Ok(())
} else {
Err(format!("{:?}", status))
}
},
Duration::from_millis(100),
MAX_STARTUP_WAIT,
);
}
pub fn scheduler_url(&self) -> HTTPUrl {
let url = format!("http://127.0.0.1:{}", SCHEDULER_PORT);
HTTPUrl::from_url(reqwest::Url::parse(&url).unwrap())
}
fn scheduler_status(&self) -> SchedulerStatusResult {
let res = reqwest::blocking::get(dist::http::urls::scheduler_status(
&self.scheduler_url().to_url(),
))
.unwrap();
assert!(res.status().is_success());
bincode::deserialize_from(res).unwrap()
}
}
// If you want containers to hang around (e.g. for debugging), comment out the "rm -f" lines
#[cfg(feature = "dist-server")]
impl Drop for DistSystem {
fn drop(&mut self) {
let mut did_err = false;
// Panicking halfway through drop would either abort (if it's a double panic) or leave us with
// resources that aren't yet cleaned up. Instead, do as much as possible then decide what to do
// at the end - panic (if not already doing so) or let the panic continue
macro_rules! droperr {
($e:expr) => {
match $e {
Ok(()) => (),
Err(e) => {
did_err = true;
eprintln!("Error with {}: {}", stringify!($e), e)
}
}
};
}
let mut logs = vec![];
let mut outputs = vec![];
let mut exits = vec![];
if let Some(scheduler_name) = self.scheduler_name.as_ref() {
droperr!(Command::new("docker")
.args(&["logs", scheduler_name])
.output()
.map(|o| logs.push((scheduler_name, o))));
droperr!(Command::new("docker")
.args(&["kill", scheduler_name])
.output()
.map(|o| outputs.push((scheduler_name, o))));
droperr!(Command::new("docker")
.args(&["rm", "-f", scheduler_name])
.output()
.map(|o| outputs.push((scheduler_name, o))));
}
for server_name in self.server_names.iter() {
droperr!(Command::new("docker")
.args(&["logs", server_name])
.output()
.map(|o| logs.push((server_name, o))));
droperr!(Command::new("docker")
.args(&["kill", server_name])
.output()
.map(|o| outputs.push((server_name, o))));
droperr!(Command::new("docker")
.args(&["rm", "-f", server_name])
.output()
.map(|o| outputs.push((server_name, o))));
}
for &pid in self.server_pids.iter() {
droperr!(nix::sys::signal::kill(pid, Signal::SIGINT));
thread::sleep(Duration::from_millis(100));
let mut killagain = true; // Default to trying to kill again, e.g. if there was an error waiting on the pid
droperr!(
nix::sys::wait::waitpid(pid, Some(WaitPidFlag::WNOHANG)).map(|ws| {
if ws != WaitStatus::StillAlive {
killagain = false;
exits.push(ws)
}
})
);
if killagain {
eprintln!("SIGINT didn't kill process, trying SIGKILL");
droperr!(nix::sys::signal::kill(pid, Signal::SIGKILL));
droperr!(nix::sys::wait::waitpid(pid, Some(WaitPidFlag::WNOHANG))
.map_err(|e| e.to_string())
.and_then(|ws| if ws == WaitStatus::StillAlive {
Err("process alive after sigkill".to_owned())
} else {
exits.push(ws);
Ok(())
}));
}
}
for (
container,
Output {
status,
stdout,
stderr,
},
) in logs
{
println!(
"LOGS == ({}) ==\n> {} <:\n## STDOUT\n{}\n\n## STDERR\n{}\n====",
status,
container,
String::from_utf8_lossy(&stdout),
String::from_utf8_lossy(&stderr)
);
}
for (
container,
Output {
status,
stdout,
stderr,
},
) in outputs
{
println!(
"OUTPUTS == ({}) ==\n> {} <:\n## STDOUT\n{}\n\n## STDERR\n{}\n====",
status,
container,
String::from_utf8_lossy(&stdout),
String::from_utf8_lossy(&stderr)
);
}
for exit in exits {
println!("EXIT: {:?}", exit)
}
if did_err && !thread::panicking() {
panic!("Encountered failures during dist system teardown")
}
}
}
fn make_container_name(tag: &str) -> String {
format!(
"{}_{}_{}",
CONTAINER_NAME_PREFIX,
tag,
Uuid::new_v4().hyphenated()
)
}
fn check_output(output: &Output) {
if !output.status.success() {
println!("{}\n\n[BEGIN STDOUT]\n===========\n{}\n===========\n[FIN STDOUT]\n\n[BEGIN STDERR]\n===========\n{}\n===========\n[FIN STDERR]\n\n",
output.status, String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr));
panic!()
}
}
#[cfg(feature = "dist-server")]
fn wait_for_http(url: HTTPUrl, interval: Duration, max_wait: Duration) {
// TODO: after upgrading to reqwest >= 0.9, use 'danger_accept_invalid_certs' and stick with that rather than tcp
wait_for(
|| {
let url = url.to_url();
let url = url.socket_addrs(|| None).unwrap();
match net::TcpStream::connect(url.as_slice()) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
},
interval,
max_wait,
)
}
fn wait_for<F: Fn() -> Result<(), String>>(f: F, interval: Duration, max_wait: Duration) {
let start = Instant::now();
let mut lasterr;
loop {
match f() {
Ok(()) => return,
Err(e) => lasterr = e,
}
if start.elapsed() > max_wait {
break;
}
thread::sleep(interval)
}
panic!("wait timed out, last error result: {}", lasterr)
}