Skip to content
This repository was archived by the owner on Apr 9, 2020. It is now read-only.

Commit d28b99d

Browse files
wangbjfacebook-github-bot
authored andcommitted
walker: migrate to new futures
Summary: walker: migrate to new futures (0.3.1). * walk.rs is not fully migrated, due to function call to `bounded_traversal_stream`, which uses old futures. * `scrub::scrub_objects`, `sizing::compression_benefit` and `validate::validate` returns `BoxFuture` instead of being a regular `async` function, due to limitation of rust issue [#63303](rust-lang/rust#63033). Reviewed By: farnz Differential Revision: D19536696 fbshipit-source-id: a0df337b86d7b067a44bf3b18834193d3f63f5dc
1 parent 58f0f6b commit d28b99d

File tree

9 files changed

+326
-264
lines changed

9 files changed

+326
-264
lines changed

mononoke/walker/src/graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use anyhow::{format_err, Error};
1010
use bookmarks::BookmarkName;
1111
use filenodes::FilenodeInfo;
1212
use filestore::Alias;
13-
use futures_ext::BoxStream;
13+
use futures_preview::stream::BoxStream;
1414
use mercurial_types::{
1515
blobs::HgBlobChangeset, FileBytes, HgChangesetId, HgFileEnvelope, HgFileNodeId, HgManifest,
1616
HgManifestId,
@@ -204,7 +204,7 @@ impl fmt::Display for EdgeType {
204204

205205
/// File content gets a special two-state content so we can chose when to read the data
206206
pub enum FileContentData {
207-
ContentStream(BoxStream<FileBytes, Error>),
207+
ContentStream(BoxStream<'static, Result<FileBytes, Error>>),
208208
Consumed(usize),
209209
}
210210

mononoke/walker/src/main.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88

99
#![deny(warnings)]
1010
#![feature(process_exitcode_placeholder)]
11-
11+
#![feature(async_closure)]
1212
use anyhow::Error;
1313
use fbinit::FacebookInit;
14-
use futures::IntoFuture;
15-
use futures_ext::FutureExt;
16-
use futures_preview::compat::Future01CompatExt;
14+
use futures_preview::future::{self, FutureExt};
1715

1816
use cmdlib::{args, helpers::block_execute};
1917

@@ -41,13 +39,13 @@ fn main(fb: FacebookInit) -> Result<(), Error> {
4139
sizing::compression_benefit(fb, logger.clone(), &matches, sub_m)
4240
}
4341
(setup::VALIDATE, Some(sub_m)) => validate::validate(fb, logger.clone(), &matches, sub_m),
44-
_ => Err(Error::msg("Invalid Arguments, pass --help for usage."))
45-
.into_future()
46-
.boxify(),
42+
_ => {
43+
future::err::<_, Error>(Error::msg("Invalid Arguments, pass --help for usage.")).boxed()
44+
}
4745
};
4846

4947
block_execute(
50-
future.compat(),
48+
future,
5149
fb,
5250
app_name,
5351
&logger,

mononoke/walker/src/progress.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use crate::state::StepStats;
1111
use anyhow::Error;
1212
use cloned::cloned;
1313
use context::CoreContext;
14-
use futures::{
15-
future::{self},
16-
Future, Stream,
14+
use futures_preview::{
15+
future::FutureExt,
16+
stream::{Stream, StreamExt, TryStreamExt},
1717
};
1818
use slog::{info, Logger};
1919
use stats::prelude::*;
@@ -328,50 +328,58 @@ impl<Inner> Clone for ProgressStateMutex<Inner> {
328328
// Print some status update, passing on all data unchanged
329329
pub fn progress_stream<InStream, PS, ND, SS>(
330330
quiet: bool,
331-
progress_state: PS,
331+
progress_state: &PS,
332332
s: InStream,
333-
) -> impl Stream<Item = (Node, Option<ND>, Option<SS>), Error = Error>
333+
) -> impl Stream<Item = Result<(Node, Option<ND>, Option<SS>), Error>>
334334
where
335-
InStream: 'static + Stream<Item = (Node, Option<ND>, Option<SS>), Error = Error> + Send,
335+
InStream: Stream<Item = Result<(Node, Option<ND>, Option<SS>), Error>> + 'static + Send,
336336
PS: 'static + Send + Clone + ProgressRecorder<SS> + ProgressReporter,
337337
{
338-
s.map(move |(n, data_opt, stats_opt)| {
339-
progress_state.record_step(&n, stats_opt.as_ref());
340-
if !quiet {
341-
progress_state.report_throttled();
338+
s.map({
339+
let progress_state = progress_state.clone();
340+
move |r| {
341+
r.and_then(|(n, data_opt, stats_opt)| {
342+
progress_state.record_step(&n, stats_opt.as_ref());
343+
if !quiet {
344+
progress_state.report_throttled();
345+
}
346+
Ok((n, data_opt, stats_opt))
347+
})
342348
}
343-
(n, data_opt, stats_opt)
344349
})
345350
}
346351

347352
// Final status summary, plus count of seen nodes
348-
pub fn report_state<InStream, PS, ND, SS>(
353+
pub async fn report_state<InStream, PS, ND, SS>(
349354
ctx: CoreContext,
350355
progress_state: PS,
351356
s: InStream,
352-
) -> impl Future<Item = (), Error = Error>
357+
) -> Result<(), Error>
353358
where
354-
InStream: Stream<Item = (Node, Option<ND>, Option<SS>), Error = Error>,
359+
InStream: Stream<Item = Result<(Node, Option<ND>, Option<SS>), Error>> + 'static + Send,
355360
PS: 'static + Send + Clone + ProgressReporter,
356361
{
357362
let init_stats: (usize, usize) = (0, 0);
358-
s.fold(init_stats, {
359-
move |(mut seen, mut loaded), (_n, nd, _ss)| {
363+
s.try_fold(init_stats, {
364+
async move |(mut seen, mut loaded), (_n, nd, _ss)| {
360365
let data_count = match nd {
361366
None => 0,
362367
_ => 1,
363368
};
364369
seen += 1;
365370
loaded += data_count;
366-
future::ok::<_, Error>((seen, loaded))
371+
Ok((seen, loaded))
367372
}
368373
})
369374
.map({
370375
cloned!(ctx);
371376
move |stats| {
372-
info!(ctx.logger(), "Final count: {:?}", stats);
373-
progress_state.report_progress();
374-
()
377+
stats.and_then(|stats| {
378+
info!(ctx.logger(), "Final count: {:?}", stats);
379+
progress_state.report_progress();
380+
Ok(())
381+
})
375382
}
376383
})
384+
.await
377385
}

mononoke/walker/src/scrub.rs

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,43 @@ use crate::tail::walk_exact_tail;
1414

1515
use anyhow::Error;
1616
use clap::ArgMatches;
17-
use cloned::cloned;
1817
use context::CoreContext;
1918
use fbinit::FacebookInit;
20-
use futures::{
21-
future::{self},
22-
Future, Stream,
19+
use futures_preview::{
20+
future::{self, BoxFuture, FutureExt},
21+
stream::{Stream, StreamExt, TryStreamExt},
2322
};
24-
use futures_ext::{try_boxfuture, BoxFuture, FutureExt};
2523
use slog::Logger;
2624

2725
// Force load of leaf data like file contents that graph traversal did not need
2826
pub fn loading_stream<InStream, SS>(
2927
limit_data_fetch: bool,
3028
scheduled_max: usize,
3129
s: InStream,
32-
) -> impl Stream<Item = (Node, Option<NodeData>, Option<SS>), Error = Error>
30+
) -> impl Stream<Item = Result<(Node, Option<NodeData>, Option<SS>), Error>>
3331
where
34-
InStream: Stream<Item = (Node, Option<NodeData>, Option<SS>), Error = Error>,
32+
InStream: Stream<Item = Result<(Node, Option<NodeData>, Option<SS>), Error>> + 'static + Send,
3533
{
36-
s.map(move |(n, nd, ss)| match nd {
37-
Some(NodeData::FileContent(FileContentData::ContentStream(file_bytes_stream)))
38-
if !limit_data_fetch =>
39-
{
40-
file_bytes_stream
41-
.fold(0, |acc, file_bytes| {
42-
future::ok::<_, Error>(acc + file_bytes.size())
43-
})
44-
.map(|num_bytes| {
45-
(
34+
s.then(async move |r| match r {
35+
Err(e) => future::err(e),
36+
Ok((n, nd, ss)) => match nd {
37+
Some(NodeData::FileContent(FileContentData::ContentStream(file_bytes_stream)))
38+
if !limit_data_fetch =>
39+
{
40+
let res = file_bytes_stream
41+
.try_fold(0, async move |acc, file_bytes| Ok(acc + file_bytes.size()))
42+
.await;
43+
match res {
44+
Err(e) => future::err(e),
45+
Ok(bytes) => future::ok((
4646
n,
47-
Some(NodeData::FileContent(FileContentData::Consumed(num_bytes))),
47+
Some(NodeData::FileContent(FileContentData::Consumed(bytes))),
4848
ss,
49-
)
50-
})
51-
.left_future()
52-
}
53-
_ => future::ok((n, nd, ss)).right_future(),
49+
)),
50+
}
51+
}
52+
_ => future::ok((n, nd, ss)),
53+
},
5454
})
5555
.buffer_unordered(scheduled_max)
5656
}
@@ -61,35 +61,30 @@ pub fn scrub_objects(
6161
logger: Logger,
6262
matches: &ArgMatches<'_>,
6363
sub_m: &ArgMatches<'_>,
64-
) -> BoxFuture<(), Error> {
65-
let (datasources, walk_params) =
66-
try_boxfuture!(setup_common(SCRUB, fb, &logger, matches, sub_m));
67-
let ctx = CoreContext::new_with_logger(fb, logger.clone());
64+
) -> BoxFuture<'static, Result<(), Error>> {
65+
match setup_common(SCRUB, fb, &logger, matches, sub_m) {
66+
Err(e) => future::err::<_, Error>(e).boxed(),
67+
Ok((datasources, walk_params)) => {
68+
let ctx = CoreContext::new_with_logger(fb, logger.clone());
69+
let limit_data_fetch = sub_m.is_present(LIMIT_DATA_FETCH_ARG);
6870

69-
let limit_data_fetch = sub_m.is_present(LIMIT_DATA_FETCH_ARG);
71+
let make_sink = {
72+
let scheduled_max = walk_params.scheduled_max;
73+
let quiet = walk_params.quiet;
74+
let progress_state = walk_params.progress_state.clone();
75+
let ctx = ctx.clone();
76+
async move |walk_output| {
77+
let loading = loading_stream(limit_data_fetch, scheduled_max, walk_output);
78+
let show_progress = progress_stream(quiet, &progress_state, loading);
79+
report_state(ctx, progress_state, show_progress).await
80+
}
81+
};
7082

71-
let make_sink = {
72-
cloned!(
73-
ctx,
74-
walk_params.progress_state,
75-
walk_params.quiet,
76-
walk_params.scheduled_max
77-
);
78-
move |walk_output| {
79-
cloned!(ctx, progress_state);
80-
let loading = loading_stream(limit_data_fetch, scheduled_max, walk_output);
81-
let show_progress = progress_stream(quiet, progress_state.clone(), loading);
82-
let one_fut = report_state(ctx, progress_state.clone(), show_progress);
83-
one_fut
83+
let walk_state = WalkState::new(WalkStateCHashMap::new(
84+
walk_params.include_node_types.clone(),
85+
walk_params.include_edge_types.clone(),
86+
));
87+
walk_exact_tail(ctx, datasources, walk_params, walk_state, make_sink).boxed()
8488
}
85-
};
86-
cloned!(
87-
walk_params.include_node_types,
88-
walk_params.include_edge_types
89-
);
90-
let walk_state = WalkState::new(WalkStateCHashMap::new(
91-
include_node_types,
92-
include_edge_types,
93-
));
94-
walk_exact_tail(ctx, datasources, walk_params, walk_state, make_sink).boxify()
89+
}
9590
}

mononoke/walker/src/setup.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@ use bookmarks::BookmarkName;
2020
use clap::{App, Arg, ArgMatches, SubCommand, Values};
2121
use cmdlib::args;
2222
use fbinit::FacebookInit;
23-
use futures_ext::{BoxFuture, FutureExt};
23+
use futures_preview::{
24+
compat::Future01CompatExt,
25+
future::{BoxFuture, FutureExt},
26+
};
2427
use lazy_static::lazy_static;
2528
use metaconfig_types::{Redaction, ScrubAction};
2629
use scuba_ext::{ScubaSampleBuilder, ScubaSampleBuilderExt};
2730
use slog::{info, warn, Logger};
2831
use std::{collections::HashSet, iter::FromIterator, str::FromStr, time::Duration};
2932

3033
pub struct RepoWalkDatasources {
31-
pub blobrepo: BoxFuture<BlobRepo, Error>,
34+
pub blobrepo: BoxFuture<'static, Result<BlobRepo, Error>>,
3235
pub scuba_builder: ScubaSampleBuilder,
3336
}
3437

@@ -696,7 +699,8 @@ pub fn setup_common(
696699
config.filestore,
697700
readonly_storage,
698701
)
699-
.boxify();
702+
.compat()
703+
.boxed();
700704

701705
let mut progress_node_types = include_node_types.clone();
702706
for e in &walk_roots {

0 commit comments

Comments
 (0)