-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathdb.rs
More file actions
685 lines (614 loc) · 22.2 KB
/
Copy pathdb.rs
File metadata and controls
685 lines (614 loc) · 22.2 KB
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
use crate::crates::Crate;
use crate::db::{Database, QueryUtils};
use crate::experiments::{Experiment, Status};
use crate::prelude::*;
use crate::results::{
DeleteResults, EncodedLog, EncodingType, ReadResults, TestResult, WriteResults,
};
use crate::timings::TimingInfo;
use crate::toolchain::Toolchain;
use base64::Engine;
use rustwide::logging::{self, LogStorage};
#[derive(Deserialize)]
pub struct TaskResult {
#[serde(rename = "crate")]
pub krate: Crate,
pub toolchain: Toolchain,
pub result: TestResult,
pub log: String,
}
#[derive(Deserialize)]
pub struct ProgressData {
pub result: TaskResult,
pub version: Option<(Crate, Crate)>,
}
pub struct DatabaseDB<'a> {
db: &'a Database,
}
impl<'a> DatabaseDB<'a> {
pub fn new(db: &'a Database) -> Self {
DatabaseDB { db }
}
pub fn clear_stale_records(&self) -> Fallible<()> {
// We limit ourselves to a small number of records at a time. This means this query
// needs to run tends of thousands of times to purge records from a
// single crater run, but it also means that each individual execution
// is quite fast. That lets us run it often, without blocking other I/O
// on the database for long.
//
// Currently this is run as we add new results into the database, which
// does add some overhead to progress collection, but also gives us a
// natural point to run this housekeeping.
//
// In practice so long as the limit here is >1 that also means we're
// definitely going to keep up and not have more than (approximately)
// one crater run in storage at any time, as old ones get purged
// quite quickly after they finish.
//
// We also only purge from results here rather than cleaning up other
// tables as this is just simpler and the results dominate the storage
// size anyway. In the future we might expand this to other tables, but
// for now that wouldn't really add enough value to be worth it.
//
// The query here would be simpler if rusqlite came with delete .. limit
// support compiled in, but that's not likely to happen (see
// https://github.com/rusqlite/rusqlite/issues/1111).
self.db.execute(
"delete from results where rowid in (
select rowid from results where \
experiment in (select name from experiments where status = 'completed') \
limit 100
)",
&[],
)?;
self.db.execute(
"delete from experiment_crates where rowid in (
select rowid from experiment_crates where \
experiment in (select name from experiments where status = 'completed') \
limit 100
)",
&[],
)?;
Ok(())
}
pub fn store(
&self,
ex: &Experiment,
data: &ProgressData,
encoding_type: EncodingType,
) -> Fallible<()> {
let krate = if let Some((old, new)) = &data.version {
// If we're updating the name of the crate (typically changing the hash we found on
// github) then we ought to also use that new name for marking the crate as complete.
// Otherwise, we leave behind the old (unversioned) name and end up running this crate
// many times, effectively never actually completing it.
self.update_crate_version(ex, old, new)?;
// sanity check that the previous name of the crate is the one we intended to run.
if old.id() != data.result.krate.id() {
log::warn!(
"Storing result under {} despite job intended for {} (with wrong name old={})",
new.id(),
data.result.krate.id(),
old.id(),
);
}
new
} else {
&data.result.krate
};
self.store_result(
ex,
krate,
&data.result.toolchain,
&data.result.result,
&base64::engine::general_purpose::STANDARD
.decode(&data.result.log)
.with_context(|| "invalid base64 log provided")?,
encoding_type,
)?;
self.mark_crate_as_completed(ex, krate)?;
Ok(())
}
fn mark_crate_as_completed(&self, ex: &Experiment, krate: &Crate) -> Fallible<usize> {
self.db.execute(
"UPDATE experiment_crates SET status = ?1 WHERE experiment = ?2 AND crate = ?3 \
AND ( (SELECT COUNT(*) FROM results WHERE experiment = ?2 AND crate = ?3) > 1 )",
&[&Status::Completed.to_string(), &ex.name, &krate.id()],
)
}
fn store_result(
&self,
ex: &Experiment,
krate: &Crate,
toolchain: &Toolchain,
res: &TestResult,
log: &[u8],
desired_encoding_type: EncodingType,
) -> Fallible<()> {
let encoded_log = EncodedLog::from_plain_slice(log, desired_encoding_type)?;
self.insert_into_results(ex, krate, toolchain, res, encoded_log)?;
Ok(())
}
pub fn store_timings(
&self,
ex: &Experiment,
krate: &Crate,
toolchain: &Toolchain,
timings: &[TimingInfo],
) -> Fallible<()> {
for timing in timings {
let target_kind = timing.target.kind.join(",");
self.db.execute_cached(
"INSERT INTO build_timings \
(experiment, crate, toolchain, package_id, target_name, target_kind, mode, duration, rmeta_time) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9);",
&[
&ex.name as &dyn rusqlite::types::ToSql,
&krate.id(),
&toolchain.to_string(),
&timing.package_id,
&timing.target.name,
&target_kind,
&timing.mode,
&timing.duration,
&timing.rmeta_time,
],
)?;
}
Ok(())
}
pub fn load_timings(
&self,
ex: &Experiment,
krate: &Crate,
toolchain: &Toolchain,
) -> Fallible<Vec<TimingInfo>> {
self.db.query(
"SELECT package_id, target_name, target_kind, mode, duration, rmeta_time \
FROM build_timings \
WHERE experiment = ?1 AND crate = ?2 AND toolchain = ?3;",
[&ex.name, &krate.id(), &toolchain.to_string()],
|row| {
let target_kind: String = row.get("target_kind")?;
let kinds: Vec<String> = target_kind.split(',').map(|s| s.to_string()).collect();
Ok(TimingInfo {
package_id: row.get("package_id")?,
target: crate::timings::TimingTarget {
kind: kinds,
crate_types: Vec::new(),
name: row.get("target_name")?,
src_path: String::new(),
edition: String::new(),
doc: false,
doctest: false,
test: false,
},
mode: row.get("mode")?,
duration: row.get("duration")?,
rmeta_time: row.get("rmeta_time")?,
})
},
)
}
fn insert_into_results(
&self,
ex: &Experiment,
krate: &Crate,
toolchain: &Toolchain,
res: &TestResult,
log: EncodedLog,
) -> Fallible<usize> {
log::info!(
"insert {krate} for ex={ex:?} with tc={toolchain}; result={res:?}",
krate = krate.id(),
ex = &ex.name
);
self.db.execute(
"INSERT INTO results (experiment, crate, toolchain, result, log, encoding) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6);",
&[
&ex.name,
&krate.id(),
&toolchain.to_string(),
&res.to_string(),
&log.as_slice(),
&log.get_encoding_type().to_str(),
],
)
}
}
impl ReadResults for DatabaseDB<'_> {
fn load_log(
&self,
ex: &Experiment,
toolchain: &Toolchain,
krate: &Crate,
) -> Fallible<Option<EncodedLog>> {
self.db.get_row(
"SELECT log, encoding FROM results \
WHERE experiment = ?1 AND toolchain = ?2 AND crate = ?3 \
LIMIT 1;",
[&ex.name, &toolchain.to_string(), &krate.id()],
|row| {
let log: Vec<u8> = row.get("log")?;
let encoding: String = row.get("encoding")?;
let encoding = encoding.parse().unwrap();
Ok(match encoding {
EncodingType::Plain => EncodedLog::Plain(log),
EncodingType::Gzip => EncodedLog::Gzip(log),
})
},
)
}
fn load_test_result(
&self,
ex: &Experiment,
toolchain: &Toolchain,
krate: &Crate,
) -> Fallible<Option<TestResult>> {
Ok(self.db.query_row(
"SELECT result FROM results \
WHERE experiment = ?1 AND toolchain = ?2 AND crate = ?3 \
LIMIT 1;",
[&ex.name, &toolchain.to_string(), &krate.id()],
|row| Ok(row.get_ref("result")?.as_str()?.parse::<TestResult>()?),
)?)
}
}
impl WriteResults for DatabaseDB<'_> {
fn get_result(
&self,
ex: &Experiment,
toolchain: &Toolchain,
krate: &Crate,
) -> Fallible<Option<TestResult>> {
self.load_test_result(ex, toolchain, krate)
}
fn update_crate_version(&self, ex: &Experiment, old: &Crate, new: &Crate) -> Fallible<()> {
self.db.execute(
"UPDATE experiment_crates SET crate = ?1 WHERE experiment = ?2 AND crate = ?3;",
&[&new.id(), &ex.name, &old.id()],
)?;
Ok(())
}
fn record_result<F>(
&self,
ex: &Experiment,
toolchain: &Toolchain,
krate: &Crate,
storage: &LogStorage,
encoding_type: EncodingType,
f: F,
) -> Fallible<TestResult>
where
F: FnOnce() -> Fallible<TestResult>,
{
let result = logging::capture(storage, f)?;
let output = storage.to_string();
self.store_result(
ex,
krate,
toolchain,
&result,
output.as_bytes(),
encoding_type,
)?;
Ok(result)
}
}
impl crate::runner::RecordProgress for DatabaseDB<'_> {
fn record_progress(
&self,
ex: &Experiment,
krate: &Crate,
toolchain: &Toolchain,
log: &[u8],
result: &TestResult,
version: Option<(&Crate, &Crate)>,
) -> Fallible<()> {
self.store_result(ex, krate, toolchain, result, log, EncodingType::Plain)?;
if let Some((old, new)) = version {
self.update_crate_version(ex, old, new)?;
}
Ok(())
}
fn record_timings(
&self,
ex: &Experiment,
krate: &Crate,
toolchain: &Toolchain,
timings: &[TimingInfo],
) -> Fallible<()> {
self.store_timings(ex, krate, toolchain, timings)
}
}
impl DeleteResults for DatabaseDB<'_> {
fn delete_all_results(&self, ex: &Experiment) -> Fallible<()> {
self.db
.execute("DELETE FROM results WHERE experiment = ?1;", &[&ex.name])?;
Ok(())
}
fn delete_result(&self, ex: &Experiment, tc: &Toolchain, krate: &Crate) -> Fallible<()> {
self.db.execute(
"DELETE FROM results WHERE experiment = ?1 AND toolchain = ?2 AND crate = ?3;",
&[&ex.name, &tc.to_string(), &krate.id()],
)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use base64::Engine;
use rustwide::logging::LogStorage;
use super::{DatabaseDB, ProgressData, TaskResult};
use crate::actions::{Action, ActionsCtx, CreateExperiment};
use crate::config::Config;
use crate::crates::{Crate, RegistryCrate};
use crate::db::Database;
use crate::experiments::Experiment;
use crate::prelude::*;
use crate::results::{
DeleteResults, EncodedLog, EncodingType, FailureReason, ReadResults, TestResult,
WriteResults,
};
use crate::toolchain::{MAIN_TOOLCHAIN, TEST_TOOLCHAIN};
use std::collections::BTreeSet;
#[test]
fn test_versions() {
let db = Database::temp().unwrap();
let results = DatabaseDB::new(&db);
let config = Config::default();
let ctx = ActionsCtx::new(&db, &config);
crate::crates::lists::setup_test_lists(&db, &config).unwrap();
// Create a dummy experiment to attach the results to
CreateExperiment::dummy("dummy").apply(&ctx).unwrap();
let ex = Experiment::get(&db, "dummy").unwrap().unwrap();
let crates = ex
.get_crates(&db)
.unwrap()
.into_iter()
.collect::<BTreeSet<_>>();
let build_fail = Crate::Local("build-fail".to_string());
let updated = Crate::Local("updated".to_string());
assert!(crates.contains(&build_fail));
// update crate version
results
.update_crate_version(&ex, &build_fail, &updated)
.unwrap();
let crates = ex
.get_crates(&db)
.unwrap()
.into_iter()
.collect::<BTreeSet<_>>();
assert!(!crates.contains(&build_fail));
assert!(crates.contains(&updated));
let updated_again = Crate::Local("updated, again".to_string());
results
.update_crate_version(&ex, &updated, &updated_again)
.unwrap();
let crates = ex
.get_crates(&db)
.unwrap()
.into_iter()
.collect::<BTreeSet<_>>();
assert!(!crates.contains(&build_fail));
assert!(!crates.contains(&updated));
assert!(crates.contains(&updated_again));
}
#[test]
fn test_results() {
rustwide::logging::init();
let db = Database::temp().unwrap();
let results = DatabaseDB::new(&db);
let config = Config::default();
let ctx = ActionsCtx::new(&db, &config);
crate::crates::lists::setup_test_lists(&db, &config).unwrap();
// Create a dummy experiment to attach the results to
CreateExperiment::dummy("dummy").apply(&ctx).unwrap();
let ex = Experiment::get(&db, "dummy").unwrap().unwrap();
let krate = Crate::Registry(RegistryCrate {
name: "lazy_static".into(),
version: "1".into(),
});
// Record a result with a message in it
results
.record_result(
&ex,
&MAIN_TOOLCHAIN,
&krate,
&LogStorage::from(&config),
EncodingType::Plain,
|| {
info!("hello world");
Ok(TestResult::TestPass)
},
)
.unwrap();
// Ensure the data is recorded correctly
assert_eq!(
results
.load_test_result(&ex, &MAIN_TOOLCHAIN, &krate)
.unwrap(),
Some(TestResult::TestPass)
);
let result_var = results
.load_log(&ex, &MAIN_TOOLCHAIN, &krate)
.unwrap()
.unwrap();
assert!(String::from_utf8_lossy(match result_var {
EncodedLog::Plain(ref data) => data,
EncodedLog::Gzip(_) => panic!("The encoded log should not be Gzipped."),
})
.contains("hello world"));
// Ensure no data is returned for missing results
assert!(results
.load_test_result(&ex, &TEST_TOOLCHAIN, &krate)
.unwrap()
.is_none());
assert!(results
.get_result(&ex, &TEST_TOOLCHAIN, &krate)
.unwrap()
.is_none());
assert!(results
.load_log(&ex, &TEST_TOOLCHAIN, &krate)
.unwrap()
.is_none());
// Add another result
results
.record_result(
&ex,
&TEST_TOOLCHAIN,
&krate,
&LogStorage::from(&config),
EncodingType::Plain,
|| {
info!("Another log message!");
Ok(TestResult::TestFail(FailureReason::Unknown))
},
)
.unwrap();
assert_eq!(
results.get_result(&ex, &TEST_TOOLCHAIN, &krate).unwrap(),
Some(TestResult::TestFail(FailureReason::Unknown))
);
// Test deleting the newly-added result
results.delete_result(&ex, &TEST_TOOLCHAIN, &krate).unwrap();
assert!(results
.get_result(&ex, &TEST_TOOLCHAIN, &krate)
.unwrap()
.is_none());
assert_eq!(
results.get_result(&ex, &MAIN_TOOLCHAIN, &krate).unwrap(),
Some(TestResult::TestPass)
);
// Test deleting all the remaining results
results.delete_all_results(&ex).unwrap();
assert!(results
.get_result(&ex, &MAIN_TOOLCHAIN, &krate)
.unwrap()
.is_none());
}
#[test]
fn test_store_and_load_timings() {
let db = Database::temp().unwrap();
let results = DatabaseDB::new(&db);
let config = Config::default();
let ctx = ActionsCtx::new(&db, &config);
crate::crates::lists::setup_test_lists(&db, &config).unwrap();
CreateExperiment::dummy("dummy").apply(&ctx).unwrap();
let ex = Experiment::get(&db, "dummy").unwrap().unwrap();
let krate = Crate::Registry(RegistryCrate {
name: "lazy_static".into(),
version: "1".into(),
});
let timings = vec![
crate::timings::TimingInfo {
package_id: "serde 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)"
.to_string(),
target: crate::timings::TimingTarget {
kind: vec!["lib".to_string()],
crate_types: vec!["lib".to_string()],
name: "serde".to_string(),
src_path: "/path/to/src/lib.rs".to_string(),
edition: "2021".to_string(),
doc: true,
doctest: true,
test: true,
},
mode: "build".to_string(),
duration: 12.5,
rmeta_time: Some(8.3),
},
crate::timings::TimingInfo {
package_id: "foo 0.1.0".to_string(),
target: crate::timings::TimingTarget {
kind: vec!["bin".to_string()],
crate_types: vec!["bin".to_string()],
name: "foo".to_string(),
src_path: "/path/to/main.rs".to_string(),
edition: "2021".to_string(),
doc: false,
doctest: false,
test: false,
},
mode: "build".to_string(),
duration: 1.0,
rmeta_time: None,
},
];
results
.store_timings(&ex, &krate, &MAIN_TOOLCHAIN, &timings)
.unwrap();
let loaded = results.load_timings(&ex, &krate, &MAIN_TOOLCHAIN).unwrap();
assert_eq!(loaded.len(), 2);
assert_eq!(loaded[0].package_id, timings[0].package_id);
assert_eq!(loaded[0].target.name, "serde");
assert_eq!(loaded[0].target.kind, vec!["lib"]);
assert_eq!(loaded[0].mode, "build");
assert!((loaded[0].duration - 12.5).abs() < f64::EPSILON);
assert!((loaded[0].rmeta_time.unwrap() - 8.3).abs() < f64::EPSILON);
assert_eq!(loaded[1].package_id, "foo 0.1.0");
assert_eq!(loaded[1].target.name, "foo");
assert_eq!(loaded[1].target.kind, vec!["bin"]);
assert!((loaded[1].duration - 1.0).abs() < f64::EPSILON);
assert!(loaded[1].rmeta_time.is_none());
// Different toolchain should return empty
let empty = results.load_timings(&ex, &krate, &TEST_TOOLCHAIN).unwrap();
assert!(empty.is_empty());
}
#[test]
fn test_store() {
let db = Database::temp().unwrap();
let results = DatabaseDB::new(&db);
let config = Config::default();
let ctx = ActionsCtx::new(&db, &config);
crate::crates::lists::setup_test_lists(&db, &config).unwrap();
// Create a dummy experiment to attach the results to
CreateExperiment::dummy("dummy").apply(&ctx).unwrap();
let ex = Experiment::get(&db, "dummy").unwrap().unwrap();
let krate = Crate::Registry(RegistryCrate {
name: "lazy_static".into(),
version: "1".into(),
});
let updated = Crate::Registry(RegistryCrate {
name: "lazy_static".into(),
version: "1.2".into(),
});
// Store a result and versions
results
.store(
&ex,
&ProgressData {
result: TaskResult {
krate: updated.clone(),
toolchain: MAIN_TOOLCHAIN.clone(),
result: TestResult::TestPass,
log: base64::engine::general_purpose::STANDARD.encode("foo"),
},
version: Some((krate.clone(), updated.clone())),
},
EncodingType::Plain,
)
.unwrap();
assert_eq!(
results.load_log(&ex, &MAIN_TOOLCHAIN, &updated).unwrap(),
Some(EncodedLog::Plain(b"foo".to_vec()))
);
assert_eq!(
results
.load_test_result(&ex, &MAIN_TOOLCHAIN, &updated)
.unwrap(),
Some(TestResult::TestPass)
);
assert_eq!(
results.load_log(&ex, &MAIN_TOOLCHAIN, &krate).unwrap(),
None
);
assert_eq!(
results
.load_test_result(&ex, &MAIN_TOOLCHAIN, &krate)
.unwrap(),
None
);
}
}