forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
1086 lines (1016 loc) · 34.1 KB
/
lib.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
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).
#![deny(warnings)]
// Enable all clippy lints except for many of the pedantic ones. It's a shame this needs to be copied and pasted across crates, but there doesn't appear to be a way to include inner attributes from a common source.
#![deny(
clippy::all,
clippy::default_trait_access,
clippy::expl_impl_clone_on_copy,
clippy::if_not_else,
clippy::needless_continue,
clippy::unseparated_literal_suffix,
// TODO: Falsely triggers for async/await:
// see https://github.com/rust-lang/rust-clippy/issues/5360
// clippy::used_underscore_binding
)]
// It is often more clear to show that nothing is being moved.
#![allow(clippy::match_ref_pats)]
// Subjective style.
#![allow(
clippy::len_without_is_empty,
clippy::redundant_field_names,
clippy::too_many_arguments
)]
// Default isn't as big a deal as people seem to think it is.
#![allow(clippy::new_without_default, clippy::new_ret_no_self)]
// Arc<Mutex> can be more clear than needing to grok Orderings:
#![allow(clippy::mutex_atomic)]
#![type_length_limit = "8576838"]
mod snapshot;
pub use crate::snapshot::{OneOffStoreFileByDigest, Snapshot, StoreFileByDigest};
mod snapshot_ops;
#[cfg(test)]
mod snapshot_ops_tests;
#[cfg(test)]
mod snapshot_tests;
pub use crate::snapshot_ops::{SnapshotOps, StoreWrapper, SubsetParams};
use async_trait::async_trait;
use bazel_protos::remote_execution as remexec;
use boxfuture::{try_future, BoxFuture, Boxable};
use bytes::Bytes;
use concrete_time::TimeSpan;
use fs::{default_cache_path, FileContent};
use futures::compat::Future01CompatExt;
use futures::future::{self as future03, Either, FutureExt, TryFutureExt};
use futures01::{future, Future};
use hashing::Digest;
use protobuf::Message;
use serde_derive::Serialize;
pub use serverset::BackoffConfig;
use std::collections::{BTreeMap, HashMap};
use std::convert::TryInto;
use std::fs::OpenOptions;
use std::io::Write;
use std::os::unix::fs::OpenOptionsExt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime};
use parking_lot::Mutex;
const MEGABYTES: usize = 1024 * 1024;
const GIGABYTES: usize = 1024 * MEGABYTES;
// This is the target number of bytes which should be present in all combined LMDB store files
// after garbage collection. We almost certainly want to make this configurable.
pub const DEFAULT_LOCAL_STORE_GC_TARGET_BYTES: usize = 4 * GIGABYTES;
mod local;
#[cfg(test)]
pub mod local_tests;
mod remote;
#[cfg(test)]
mod remote_tests;
// Summary of the files and directories uploaded with an operation
// ingested_file_{count, bytes}: Number and combined size of processed files
// uploaded_file_{count, bytes}: Number and combined size of files uploaded to the remote
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize)]
pub struct UploadSummary {
pub ingested_file_count: usize,
pub ingested_file_bytes: usize,
pub uploaded_file_count: usize,
pub uploaded_file_bytes: usize,
#[serde(skip)]
pub upload_wall_time: Duration,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub enum LoadMetadata {
Local,
Remote(TimeSpan),
}
#[derive(Debug, PartialEq, Serialize)]
pub struct DirectoryMaterializeMetadata {
pub metadata: LoadMetadata,
pub child_directories: BTreeMap<String, DirectoryMaterializeMetadata>,
pub child_files: BTreeMap<String, LoadMetadata>,
}
impl DirectoryMaterializeMetadata {
pub fn to_path_list(&self) -> Vec<String> {
fn recurse(
outputs: &mut Vec<String>,
path_so_far: PathBuf,
current: &DirectoryMaterializeMetadata,
) {
for (child, _) in current.child_files.iter() {
outputs.push(path_so_far.join(child).to_string_lossy().to_string())
}
for (dir, meta) in current.child_directories.iter() {
recurse(outputs, path_so_far.join(dir), &meta);
}
}
let mut output_paths: Vec<String> = vec![];
recurse(&mut output_paths, PathBuf::new(), self);
output_paths
}
}
#[derive(Debug)]
struct DirectoryMaterializeMetadataBuilder {
pub metadata: LoadMetadata,
pub child_directories: Arc<Mutex<BTreeMap<String, DirectoryMaterializeMetadataBuilder>>>,
pub child_files: Arc<Mutex<BTreeMap<String, LoadMetadata>>>,
}
impl DirectoryMaterializeMetadataBuilder {
pub fn new(metadata: LoadMetadata) -> Self {
DirectoryMaterializeMetadataBuilder {
metadata,
child_directories: Arc::new(Mutex::new(BTreeMap::new())),
child_files: Arc::new(Mutex::new(BTreeMap::new())),
}
}
}
impl DirectoryMaterializeMetadataBuilder {
pub fn build(self) -> DirectoryMaterializeMetadata {
let child_directories = Arc::try_unwrap(self.child_directories)
.unwrap()
.into_inner();
let child_files = Arc::try_unwrap(self.child_files).unwrap().into_inner();
DirectoryMaterializeMetadata {
metadata: self.metadata,
child_directories: child_directories
.into_iter()
.map(|(dir, builder)| (dir, builder.build()))
.collect(),
child_files,
}
}
}
#[allow(clippy::type_complexity)]
#[derive(Debug)]
enum RootOrParentMetadataBuilder {
Root(Arc<Mutex<Option<DirectoryMaterializeMetadataBuilder>>>),
Parent(
(
String,
Arc<Mutex<BTreeMap<String, DirectoryMaterializeMetadataBuilder>>>,
Arc<Mutex<BTreeMap<String, LoadMetadata>>>,
),
),
}
///
/// A content-addressed store of file contents, and Directories.
///
/// Store keeps content on disk, and can optionally delegate to backfill its on-disk storage by
/// fetching files from a remote server which implements the gRPC bytestream interface
/// (see https://github.com/googleapis/googleapis/blob/master/google/bytestream/bytestream.proto)
/// as specified by the gRPC remote execution interface (see
/// https://github.com/googleapis/googleapis/blob/master/google/devtools/remoteexecution/v1test/)
///
/// It can also write back to a remote gRPC server, but will only do so when explicitly instructed
/// to do so.
///
#[derive(Debug, Clone)]
pub struct Store {
local: local::ByteStore,
remote: Option<remote::ByteStore>,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ShrinkBehavior {
///
/// Free up space in the store for future writes (marking pages as dirty), but don't proactively
/// free up the disk space that was used. This is fast and safe, but won't free up disk space.
///
Fast,
///
/// As with Fast, but also free up disk space from no-longer-used data. This may use extra disk
/// space temporarily while compaction is happening.
///
/// Note that any processes which have the Store open may need to re-open the Store after this
/// operation, as the underlying files may have been re-written.
///
Compact,
}
// Note that Store doesn't implement ByteStore because it operates at a higher level of abstraction,
// considering Directories as a standalone concept, rather than a buffer of bytes.
// This has the nice property that Directories can be trusted to be valid and canonical.
// We may want to re-visit this if we end up wanting to handle local/remote/merged interchangably.
impl Store {
///
/// Make a store which only uses its local storage.
///
pub fn local_only<P: AsRef<Path>>(
executor: task_executor::Executor,
path: P,
) -> Result<Store, String> {
Ok(Store {
local: local::ByteStore::new(executor, path)?,
remote: None,
})
}
///
/// Converts this (copy of) a Store to local only by dropping the remote half.
///
/// Because both underlying stores are reference counted, this is cheap, and has no effect on
/// other clones of the Store.
///
fn into_local_only(self) -> Store {
Store {
local: self.local,
remote: None,
}
}
///
/// Make a store which uses local storage, and if it is missing a value which it tries to load,
/// will attempt to back-fill its local storage from a remote CAS.
///
pub fn with_remote<P: AsRef<Path>>(
executor: task_executor::Executor,
path: P,
cas_addresses: Vec<String>,
instance_name: Option<String>,
root_ca_certs: Option<Vec<u8>>,
oauth_bearer_token: Option<String>,
thread_count: usize,
chunk_size_bytes: usize,
upload_timeout: Duration,
backoff_config: BackoffConfig,
rpc_retries: usize,
connection_limit: usize,
) -> Result<Store, String> {
Ok(Store {
local: local::ByteStore::new(executor, path)?,
remote: Some(remote::ByteStore::new(
cas_addresses,
instance_name,
root_ca_certs,
oauth_bearer_token,
thread_count,
chunk_size_bytes,
upload_timeout,
backoff_config,
rpc_retries,
connection_limit,
)?),
})
}
// This default suffix is also hard-coded into the Python options code in global_options.py
pub fn default_path() -> PathBuf {
default_cache_path().join("lmdb_store")
}
///
/// Store a file locally.
///
pub async fn store_file_bytes(
&self,
bytes: Bytes,
initial_lease: bool,
) -> Result<Digest, String> {
self
.local
.store_bytes(EntryType::File, bytes, initial_lease)
.await
}
/// Store a digest under a given file path, returning a Snapshot
pub async fn snapshot_of_one_file(
&self,
name: PathBuf,
digest: hashing::Digest,
is_executable: bool,
) -> Result<Snapshot, String> {
#[derive(Clone)]
struct Digester {
digest: hashing::Digest,
}
impl StoreFileByDigest<String> for Digester {
fn store_by_digest(&self, _: fs::File) -> BoxFuture<hashing::Digest, String> {
future::ok(self.digest).to_boxed()
}
}
Snapshot::from_path_stats(
self.clone(),
Digester { digest },
vec![fs::PathStat::File {
path: name.clone(),
stat: fs::File {
path: name,
is_executable: is_executable,
},
}],
)
.await
}
///
/// Loads the bytes of the file with the passed fingerprint from the local store and back-fill
/// from remote when necessary and possible (i.e. when remote is configured), and returns the
/// result of applying f to that value.
///
pub async fn load_file_bytes_with<
T: Send + 'static,
F: Fn(&[u8]) -> T + Send + Sync + 'static,
>(
&self,
digest: Digest,
f: F,
) -> Result<Option<(T, LoadMetadata)>, String> {
// No transformation or verification is needed for files, so we pass in a pair of functions
// which always succeed, whether the underlying bytes are coming from a local or remote store.
// Unfortunately, we need to be a little verbose to do this.
let f_local = Arc::new(f);
let f_remote = f_local.clone();
self
.load_bytes_with(
EntryType::File,
digest,
move |v: &[u8]| Ok(f_local(v)),
move |v: Bytes| Ok(f_remote(&v)),
)
.await
}
///
/// Save the bytes of the Directory proto locally, without regard for any of the
/// contents of any FileNodes or DirectoryNodes therein (i.e. does not require that its
/// children are already stored).
///
pub async fn record_directory(
&self,
directory: &bazel_protos::remote_execution::Directory,
initial_lease: bool,
) -> Result<Digest, String> {
let local = self.local.clone();
let bytes = directory
.write_to_bytes()
.map_err(|e| format!("Error serializing directory proto {:?}: {:?}", directory, e))?;
local
.store_bytes(EntryType::Directory, Bytes::from(bytes), initial_lease)
.await
}
///
/// Loads a directory proto from the local store, back-filling from remote if necessary.
///
/// Guarantees that if an Ok Some value is returned, it is valid, and canonical, and its
/// fingerprint exactly matches that which is requested. Will return an Err if it would return a
/// non-canonical Directory.
///
pub async fn load_directory(
&self,
digest: Digest,
) -> Result<Option<(bazel_protos::remote_execution::Directory, LoadMetadata)>, String> {
self
.load_bytes_with(
EntryType::Directory,
digest,
// Trust that locally stored values were canonical when they were written into the CAS,
// don't bother to check this, as it's slightly expensive.
move |bytes: &[u8]| {
let mut directory = bazel_protos::remote_execution::Directory::new();
directory.merge_from_bytes(&bytes).map_err(|e| {
format!(
"LMDB corruption: Directory bytes for {:?} were not valid: {:?}",
digest, e
)
})?;
Ok(directory)
},
// Eagerly verify that CAS-returned Directories are canonical, so that we don't write them
// into our local store.
move |bytes: Bytes| {
let mut directory = bazel_protos::remote_execution::Directory::new();
directory.merge_from_bytes(&bytes).map_err(|e| {
format!(
"CAS returned Directory proto for {:?} which was not valid: {:?}",
digest, e
)
})?;
bazel_protos::verify_directory_canonical(&directory)?;
Ok(directory)
},
)
.await
}
///
/// Loads bytes from remote cas if required and possible (i.e. if remote is configured). Takes
/// two functions f_local and f_remote. These functions are any validation or transformations you
/// want to perform on the bytes received from the local and remote cas (if remote is configured).
///
async fn load_bytes_with<
T: Send + 'static,
FLocal: Fn(&[u8]) -> Result<T, String> + Send + Sync + 'static,
FRemote: Fn(Bytes) -> Result<T, String> + Send + Sync + 'static,
>(
&self,
entry_type: EntryType,
digest: Digest,
f_local: FLocal,
f_remote: FRemote,
) -> Result<Option<(T, LoadMetadata)>, String> {
let local = self.local.clone();
let maybe_remote = self.remote.clone();
let start = SystemTime::now();
let maybe_local_value = self
.local
.load_bytes_with(entry_type, digest, f_local)
.await?;
match (maybe_local_value, maybe_remote) {
(Some(value_result), _) => value_result.map(|res| Some((res, LoadMetadata::Local))),
(None, None) => Ok(None),
(None, Some(remote)) => {
let maybe_bytes = remote
.load_bytes_with(entry_type, digest, move |bytes: Bytes| bytes)
.await?;
match maybe_bytes {
Some(bytes) => {
let value = f_remote(bytes.clone())?;
let stored_digest = local.store_bytes(entry_type, bytes, true).await?;
if digest == stored_digest {
let time_span = TimeSpan::since(&start);
Ok(Some((value, LoadMetadata::Remote(time_span))))
} else {
Err(format!(
"CAS gave wrong digest: expected {:?}, got {:?}",
digest, stored_digest
))
}
}
None => Ok(None),
}
}
}
}
///
/// Ensures that the remote ByteStore has a copy of each passed Fingerprint, including any files
/// contained in any Directories in the list.
///
/// Returns a structure with the summary of operations.
///
pub fn ensure_remote_has_recursive(
&self,
digests: Vec<Digest>,
) -> BoxFuture<UploadSummary, String> {
let start_time = Instant::now();
let remote = if let Some(ref remote) = self.remote {
remote
} else {
return future::err("Cannot ensure remote has blobs without a remote".to_owned()).to_boxed();
};
let store = self.clone();
let remote = remote.clone();
async move {
let ingested_digests = store.expand_local_digests(digests.iter(), false).await?;
let digests_to_upload =
if Store::upload_is_faster_than_checking_whether_to_upload(&ingested_digests) {
ingested_digests.keys().cloned().collect()
} else {
let request = remote.find_missing_blobs_request(ingested_digests.keys());
remote.list_missing_digests(request).compat().await?
};
let uploaded_digests = future03::try_join_all(
digests_to_upload
.into_iter()
.map(|digest| {
let entry_type = ingested_digests[&digest];
let local = store.local.clone();
let remote = remote.clone();
async move {
let executor = local.executor().clone();
let maybe_upload = local
.load_bytes_with(entry_type, digest, move |bytes| {
// NB: `load_bytes_with` runs on a spawned thread which we can safely block.
executor.block_on(remote.store_bytes(bytes))
})
.await?;
match maybe_upload {
Some(res) => res,
None => Err(format!("Failed to upload digest {:?}: Not found", digest)),
}
}
})
.collect::<Vec<_>>(),
)
.await?;
let ingested_file_sizes = ingested_digests.iter().map(|(digest, _)| digest.1);
let uploaded_file_sizes = uploaded_digests.iter().map(|digest| digest.1);
Ok(UploadSummary {
ingested_file_count: ingested_file_sizes.len(),
ingested_file_bytes: ingested_file_sizes.sum(),
uploaded_file_count: uploaded_file_sizes.len(),
uploaded_file_bytes: uploaded_file_sizes.sum(),
upload_wall_time: start_time.elapsed(),
})
}
.boxed()
.compat()
.to_boxed()
}
///
/// Download a directory from Remote ByteStore recursively to the local one. Called only with the
/// Digest of a Directory.
///
pub fn ensure_local_has_recursive_directory(&self, dir_digest: Digest) -> BoxFuture<(), String> {
let loaded_directory = {
let store = self.clone();
let res = async move { store.load_directory(dir_digest).await };
res.boxed().compat()
};
let store = self.clone();
loaded_directory
.and_then(move |directory_opt| {
directory_opt
.map(|(dir, _metadata)| dir)
.ok_or_else(|| format!("Could not read dir with digest {:?}", dir_digest))
})
.and_then(move |directory| {
// Traverse the files within directory
let file_futures = directory
.get_files()
.iter()
.map(|file_node| {
let file_digest = try_future!(file_node.get_digest().try_into());
let store = store.clone();
Box::pin(async move {
store
.load_bytes_with(EntryType::File, file_digest, |_| Ok(()), |_| Ok(()))
.await
})
.compat()
.to_boxed()
})
.collect::<Vec<_>>();
// Recursively call with sub-directories
let directory_futures = directory
.get_directories()
.iter()
.map(move |child_dir| {
let child_digest = try_future!(child_dir.get_digest().try_into());
store.ensure_local_has_recursive_directory(child_digest)
})
.collect::<Vec<_>>();
future::join_all(file_futures)
.join(future::join_all(directory_futures))
.map(|_| ())
})
.to_boxed()
}
pub async fn lease_all_recursively<'a, Ds: Iterator<Item = &'a Digest>>(
&self,
digests: Ds,
) -> Result<(), String> {
let reachable_digests_and_types = self.expand_local_digests(digests, true).await?;
self
.local
.lease_all(reachable_digests_and_types.into_iter())
.await
}
pub fn garbage_collect(
&self,
target_size_bytes: usize,
shrink_behavior: ShrinkBehavior,
) -> Result<(), String> {
match self.local.shrink(target_size_bytes, shrink_behavior) {
Ok(size) => {
if size > target_size_bytes {
Err(format!(
"Garbage collection attempted to target {} bytes but could only shrink to {} bytes",
target_size_bytes, size
))
} else {
Ok(())
}
}
Err(err) => Err(format!("Garbage collection failed: {:?}", err)),
}
}
///
/// To check if it might be faster to upload the digests recursively
/// vs checking if the files are present first.
///
/// The values are guesses, feel free to tweak them.
///
fn upload_is_faster_than_checking_whether_to_upload(
digests: &HashMap<Digest, EntryType>,
) -> bool {
if digests.len() < 3 {
let mut num_bytes = 0;
for digest in digests.keys() {
num_bytes += digest.1;
}
num_bytes < 1024 * 1024
} else {
false
}
}
///
/// Return all Digests reachable locally from the given root Digests (which may represent either
/// Files or Directories).
///
/// If `allow_missing`, root digests which do not exist locally will be ignored.
///
pub async fn expand_local_digests<'a, Ds: Iterator<Item = &'a Digest>>(
&self,
digests: Ds,
allow_missing: bool,
) -> Result<HashMap<Digest, EntryType>, String> {
// Expand each digest into either a single file digest, or a collection of recursive digests
// below a directory.
let expanded_digests = future03::try_join_all(
digests
.map(|digest| {
let store = self.clone();
async move {
match store.local.entry_type(digest.0).await {
Ok(Some(EntryType::File)) => Ok(Either::Left(*digest)),
Ok(Some(EntryType::Directory)) => {
// Locally expand the directory.
let reachable = store
.into_local_only()
.expand_directory(*digest)
.compat()
.await?;
Ok(Either::Right(reachable))
}
Ok(None) => {
if allow_missing {
Ok(Either::Right(HashMap::new()))
} else {
Err(format!("Failed to expand digest {:?}: Not found", digest))
}
}
Err(err) => Err(format!("Failed to expand digest {:?}: {:?}", digest, err)),
}
}
})
.collect::<Vec<_>>(),
)
.await?;
let mut result: HashMap<Digest, EntryType> = HashMap::new();
for e in expanded_digests {
match e {
Either::Left(digest) => {
result.insert(digest, EntryType::File);
}
Either::Right(reachable_digests) => {
result.extend(reachable_digests);
}
}
}
Ok(result)
}
pub fn expand_directory(&self, digest: Digest) -> BoxFuture<HashMap<Digest, EntryType>, String> {
self
.walk(digest, |_, _, digest, directory| {
let mut digest_types = Vec::new();
digest_types.push((digest, EntryType::Directory));
for file in directory.get_files() {
digest_types.push((try_future!(file.get_digest().try_into()), EntryType::File));
}
future::ok(digest_types).to_boxed()
})
.map(|digest_pairs_per_directory| {
Iterator::flatten(digest_pairs_per_directory.into_iter().map(Vec::into_iter)).collect()
})
.to_boxed()
}
///
/// Lays out the directory and all of its contents (files and directories) on disk so that a
/// process which uses the directory structure can run.
///
pub fn materialize_directory(
&self,
destination: PathBuf,
digest: Digest,
) -> BoxFuture<DirectoryMaterializeMetadata, String> {
let root = Arc::new(Mutex::new(None));
let executor = self.local.executor().clone();
self
.materialize_directory_helper(
destination.clone(),
RootOrParentMetadataBuilder::Root(root.clone()),
digest,
)
.and_then(move |()| {
let materialize_metadata = Arc::try_unwrap(root).unwrap().into_inner().unwrap().build();
// We fundamentally materialize files for other processes to read; as such, we must ensure
// data is flushed to disk and visible to them as opposed to just our process. Even though
// we need to re-open all written files, executing all fsyncs at the end of the
// materialize call is significantly faster than doing it as we go.
future::join_all(
materialize_metadata
.to_path_list()
.into_iter()
.map(|path| {
let path = destination.join(path);
executor
.spawn_blocking(move || {
OpenOptions::new()
.write(true)
.create(false)
.open(path)?
.sync_all()
})
.compat()
})
.collect::<Vec<_>>(),
)
.map_err(|e| format!("Failed to fsync directory contents: {}", e))
.map(move |_| materialize_metadata)
})
.to_boxed()
}
fn materialize_directory_helper(
&self,
destination: PathBuf,
root_or_parent_metadata: RootOrParentMetadataBuilder,
digest: Digest,
) -> BoxFuture<(), String> {
let store = self.clone();
async move {
let directory_creation =
if let RootOrParentMetadataBuilder::Root(..) = root_or_parent_metadata {
let destination = destination.clone();
store
.local
.executor()
.spawn_blocking(move || fs::safe_create_dir_all(&destination))
.await
} else {
let destination = destination.clone();
store
.local
.executor()
.spawn_blocking(move || fs::safe_create_dir(&destination))
.await
};
directory_creation.map_err(|e| {
format!(
"Failed to create directory {}: {}",
destination.display(),
e
)
})?;
let (directory, metadata) = store
.load_directory(digest)
.await?
.ok_or_else(|| format!("Directory with digest {:?} not found", digest))?;
let (child_directories, child_files) = match root_or_parent_metadata {
RootOrParentMetadataBuilder::Root(root) => {
let builder = DirectoryMaterializeMetadataBuilder::new(metadata);
let child_directories = builder.child_directories.clone();
let child_files = builder.child_files.clone();
*root.lock() = Some(builder);
(child_directories, child_files)
}
RootOrParentMetadataBuilder::Parent((
dir_name,
parent_child_directories,
_parent_files,
)) => {
let builder = DirectoryMaterializeMetadataBuilder::new(metadata);
let child_directories = builder.child_directories.clone();
let child_files = builder.child_files.clone();
parent_child_directories.lock().insert(dir_name, builder);
(child_directories, child_files)
}
};
let file_futures = directory
.get_files()
.iter()
.map(|file_node| {
let store = store.clone();
let path = destination.join(file_node.get_name());
let digest = try_future!(file_node.get_digest().try_into());
let child_files = child_files.clone();
let name = file_node.get_name().to_owned();
store
.materialize_file(path, digest, file_node.is_executable, false)
.map(move |metadata| child_files.lock().insert(name, metadata))
.to_boxed()
})
.collect::<Vec<_>>();
let directory_futures = directory
.get_directories()
.iter()
.map(|directory_node| {
let store = store.clone();
let path = destination.join(directory_node.get_name());
let digest = try_future!(directory_node.get_digest().try_into());
let builder = RootOrParentMetadataBuilder::Parent((
directory_node.get_name().to_owned(),
child_directories.clone(),
child_files.clone(),
));
store.materialize_directory_helper(path, builder, digest)
})
.collect::<Vec<_>>();
let _ = future::join_all(file_futures)
.join(future::join_all(directory_futures))
.compat()
.await?;
Ok(())
}
.boxed()
.compat()
.to_boxed()
}
///
/// Materializes a single file. This method is private because generally files should be
/// materialized together via `materialize_directory`, which handles batch fsync'ing.
///
fn materialize_file(
&self,
destination: PathBuf,
digest: Digest,
is_executable: bool,
fsync: bool,
) -> BoxFuture<LoadMetadata, String> {
let store = self.clone();
let res = async move {
let write_result = store
.load_file_bytes_with(digest, move |bytes| {
if destination.exists() {
std::fs::remove_file(&destination)
} else {
Ok(())
}
.and_then(|_| {
OpenOptions::new()
.create(true)
.write(true)
.mode(if is_executable { 0o755 } else { 0o644 })
.open(&destination)
})
.and_then(|mut f| {
f.write_all(&bytes)?;
if fsync {
f.sync_all()
} else {
Ok(())
}
})
.map_err(|e| format!("Error writing file {:?}: {:?}", destination, e))
})
.await?;
match write_result {
Some((Ok(()), metadata)) => Ok(metadata),
Some((Err(e), _metadata)) => Err(e),
None => Err(format!("File with digest {:?} not found", digest)),
}
};
res.boxed().compat().to_boxed()
}
///
/// Returns files sorted by their path.
///
pub fn contents_for_directory(&self, digest: Digest) -> BoxFuture<Vec<FileContent>, String> {
self
.walk(digest, move |store, path_so_far, _, directory| {
future::join_all(
directory
.get_files()
.iter()
.map(|file_node| {
let path = path_so_far.join(file_node.get_name());
let is_executable = file_node.is_executable;
let file_node_digest: Result<_, _> = file_node.get_digest().try_into();
let store = store.clone();
let res = async move {
let maybe_bytes = store
.load_file_bytes_with(file_node_digest?, |b| b.into())
.await?;
maybe_bytes
.ok_or_else(|| format!("Couldn't find file contents for {:?}", path))
.map(|(content, _metadata)| FileContent {
path,
content,
is_executable,
})
};
res.boxed().compat().to_boxed()
})
.collect::<Vec<_>>(),
)
.to_boxed()
})
.map(|file_contents_per_directory| {
let mut vec =
Iterator::flatten(file_contents_per_directory.into_iter().map(Vec::into_iter))
.collect::<Vec<_>>();
vec.sort_by(|l, r| l.path.cmp(&r.path));
vec
})
.to_boxed()
}
///
/// Given the Digest for a Directory, recursively walk the Directory, calling the given function
/// with the path so far, and the new Directory.
///
/// The recursive walk will proceed concurrently, so if order matters, a caller should sort the
/// output after the call.
///
pub fn walk<
T: Send + 'static,
F: Fn(
&Store,
&PathBuf,
Digest,
&bazel_protos::remote_execution::Directory,
) -> BoxFuture<T, String>
+ Send
+ Sync
+ 'static,
>(
&self,
digest: Digest,
f: F,
) -> BoxFuture<Vec<T>, String> {
let f = Arc::new(f);
let accumulator = Arc::new(Mutex::new(Vec::new()));
self
.walk_helper(digest, PathBuf::new(), f, accumulator.clone())
.map(|()| {
Arc::try_unwrap(accumulator)
.unwrap_or_else(|_| panic!("walk_helper violated its contract."))
.into_inner()
})
.to_boxed()
}
fn walk_helper<
T: Send + 'static,
F: Fn(
&Store,
&PathBuf,
Digest,
&bazel_protos::remote_execution::Directory,
) -> BoxFuture<T, String>