Skip to content

Commit fbe6f12

Browse files
authored
A builder for the Downstairs Downstairs struct. (#1152)
Updated the downstairs Downstairs struct to have a builder. Replaced build_downstairs_for_region() and build_downstairs_for_region_with_backend() with the new builder pattern.
1 parent f55f2eb commit fbe6f12

File tree

4 files changed

+170
-232
lines changed

4 files changed

+170
-232
lines changed

downstairs/src/admin.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ pub async fn run_downstairs_for_region(
6262
));
6363
}
6464

65-
let d = build_downstairs_for_region(
66-
&run_params.data,
67-
run_params.lossy,
68-
run_params.read_errors,
69-
run_params.write_errors,
70-
run_params.flush_errors,
71-
run_params.read_only,
72-
None,
73-
)
74-
.await
75-
.map_err(|e| HttpError::for_internal_error(e.to_string()))?;
65+
let mut d = Downstairs::new_builder(&run_params.data, run_params.read_only);
66+
let d = d
67+
.set_lossy(run_params.lossy)
68+
.set_test_errors(
69+
run_params.read_errors,
70+
run_params.write_errors,
71+
run_params.flush_errors,
72+
)
73+
.build()
74+
.await
75+
.map_err(|e| HttpError::for_internal_error(e.to_string()))?;
7676

7777
let _join_handle = start_downstairs(
7878
d.clone(),

downstairs/src/lib.rs

+126-159
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,99 @@ pub struct ActiveUpstairs {
13511351
pub terminate_sender: oneshot::Sender<UpstairsConnection>,
13521352
}
13531353

1354+
#[derive(Debug)]
1355+
pub struct DownstairsBuilder<'a> {
1356+
data: &'a Path,
1357+
read_only: bool,
1358+
lossy: Option<bool>,
1359+
read_errors: Option<bool>, // Test flag
1360+
write_errors: Option<bool>, // Test flag
1361+
flush_errors: Option<bool>, // Test flag
1362+
backend: Option<Backend>,
1363+
log: Option<Logger>,
1364+
}
1365+
1366+
impl DownstairsBuilder<'_> {
1367+
pub fn set_lossy(&mut self, lossy: bool) -> &mut Self {
1368+
self.lossy = Some(lossy);
1369+
self
1370+
}
1371+
pub fn set_test_errors(
1372+
&mut self,
1373+
read_errors: bool,
1374+
write_errors: bool,
1375+
flush_errors: bool,
1376+
) -> &mut Self {
1377+
self.read_errors = Some(read_errors);
1378+
self.write_errors = Some(write_errors);
1379+
self.flush_errors = Some(flush_errors);
1380+
self
1381+
}
1382+
pub fn set_backend(&mut self, backend: Backend) -> &mut Self {
1383+
self.backend = Some(backend);
1384+
self
1385+
}
1386+
pub fn set_logger(&mut self, log: Logger) -> &mut Self {
1387+
self.log = Some(log);
1388+
self
1389+
}
1390+
1391+
pub async fn build(&mut self) -> Result<Arc<Mutex<Downstairs>>> {
1392+
let lossy = self.lossy.unwrap_or(false);
1393+
let read_errors = self.read_errors.unwrap_or(false);
1394+
let write_errors = self.write_errors.unwrap_or(false);
1395+
let flush_errors = self.flush_errors.unwrap_or(false);
1396+
let backend = self.backend.unwrap_or(Backend::RawFile);
1397+
1398+
let log = match &self.log {
1399+
Some(log) => log.clone(),
1400+
None => build_logger(),
1401+
};
1402+
1403+
// Open the region at the provided location.
1404+
let region = Region::open_with_backend(
1405+
self.data,
1406+
Default::default(),
1407+
true,
1408+
self.read_only,
1409+
backend,
1410+
&log,
1411+
)
1412+
.await?;
1413+
1414+
let encrypted = region.encrypted();
1415+
1416+
let dss = DsStatOuter {
1417+
ds_stat_wrap: Arc::new(std::sync::Mutex::new(DsCountStat::new(
1418+
region.def().uuid(),
1419+
))),
1420+
};
1421+
1422+
info!(log, "UUID: {:?}", region.def().uuid());
1423+
info!(
1424+
log,
1425+
"Blocks per extent:{} Total Extents: {}",
1426+
region.def().extent_size().value,
1427+
region.def().extent_count(),
1428+
);
1429+
1430+
Ok(Arc::new(Mutex::new(Downstairs {
1431+
region,
1432+
lossy,
1433+
read_errors,
1434+
write_errors,
1435+
flush_errors,
1436+
active_upstairs: HashMap::new(),
1437+
dss,
1438+
read_only: self.read_only,
1439+
encrypted,
1440+
address: None,
1441+
repair_address: None,
1442+
log,
1443+
})))
1444+
}
1445+
}
1446+
13541447
/*
13551448
* Overall structure for things the downstairs is tracking.
13561449
* This includes the extents and their status as well as the
@@ -1374,34 +1467,16 @@ pub struct Downstairs {
13741467

13751468
#[allow(clippy::too_many_arguments)]
13761469
impl Downstairs {
1377-
fn new(
1378-
region: Region,
1379-
lossy: bool,
1380-
read_errors: bool,
1381-
write_errors: bool,
1382-
flush_errors: bool,
1383-
read_only: bool,
1384-
encrypted: bool,
1385-
log: Logger,
1386-
) -> Self {
1387-
let dss = DsStatOuter {
1388-
ds_stat_wrap: Arc::new(std::sync::Mutex::new(DsCountStat::new(
1389-
region.def().uuid(),
1390-
))),
1391-
};
1392-
Downstairs {
1393-
region,
1394-
lossy,
1395-
read_errors,
1396-
write_errors,
1397-
flush_errors,
1398-
active_upstairs: HashMap::new(),
1399-
dss,
1470+
pub fn new_builder(data: &Path, read_only: bool) -> DownstairsBuilder {
1471+
DownstairsBuilder {
1472+
data,
14001473
read_only,
1401-
encrypted,
1402-
address: None,
1403-
repair_address: None,
1404-
log,
1474+
lossy: Some(false),
1475+
read_errors: Some(false),
1476+
write_errors: Some(false),
1477+
flush_errors: Some(false),
1478+
backend: Some(Backend::RawFile),
1479+
log: None,
14051480
}
14061481
}
14071482

@@ -3069,78 +3144,6 @@ pub async fn create_region_with_backend(
30693144
Ok(region)
30703145
}
30713146

3072-
pub async fn build_downstairs_for_region(
3073-
data: &Path,
3074-
lossy: bool,
3075-
read_errors: bool,
3076-
write_errors: bool,
3077-
flush_errors: bool,
3078-
read_only: bool,
3079-
log_request: Option<Logger>,
3080-
) -> Result<Arc<Mutex<Downstairs>>> {
3081-
build_downstairs_for_region_with_backend(
3082-
data,
3083-
lossy,
3084-
read_errors,
3085-
write_errors,
3086-
flush_errors,
3087-
read_only,
3088-
Backend::RawFile,
3089-
log_request,
3090-
)
3091-
.await
3092-
}
3093-
3094-
// Build the downstairs struct given a region directory and some additional
3095-
// needed information. If a logger is passed in, we will use that, otherwise
3096-
// a logger will be created.
3097-
#[allow(clippy::too_many_arguments)]
3098-
pub async fn build_downstairs_for_region_with_backend(
3099-
data: &Path,
3100-
lossy: bool,
3101-
read_errors: bool,
3102-
write_errors: bool,
3103-
flush_errors: bool,
3104-
read_only: bool,
3105-
backend: Backend,
3106-
log_request: Option<Logger>,
3107-
) -> Result<Arc<Mutex<Downstairs>>> {
3108-
let log = match log_request {
3109-
Some(log) => log,
3110-
None => build_logger(),
3111-
};
3112-
let region = Region::open_with_backend(
3113-
data,
3114-
Default::default(),
3115-
true,
3116-
read_only,
3117-
backend,
3118-
&log,
3119-
)
3120-
.await?;
3121-
3122-
info!(log, "UUID: {:?}", region.def().uuid());
3123-
info!(
3124-
log,
3125-
"Blocks per extent:{} Total Extents: {}",
3126-
region.def().extent_size().value,
3127-
region.def().extent_count(),
3128-
);
3129-
3130-
let encrypted = region.encrypted();
3131-
3132-
Ok(Arc::new(Mutex::new(Downstairs::new(
3133-
region,
3134-
lossy,
3135-
read_errors,
3136-
write_errors,
3137-
flush_errors,
3138-
read_only,
3139-
encrypted,
3140-
log,
3141-
))))
3142-
}
3143-
31443147
/// Returns Ok if everything spawned ok, Err otherwise
31453148
///
31463149
/// Return Ok(main task join handle) if all the necessary tasks spawned
@@ -3568,16 +3571,10 @@ mod test {
35683571
region.extend(2).await?;
35693572

35703573
let path_dir = dir.as_ref().to_path_buf();
3571-
let ads = build_downstairs_for_region(
3572-
&path_dir,
3573-
false,
3574-
false,
3575-
false,
3576-
false,
3577-
false,
3578-
Some(csl()),
3579-
)
3580-
.await?;
3574+
let ads = Downstairs::new_builder(&path_dir, false)
3575+
.set_logger(csl())
3576+
.build()
3577+
.await?;
35813578

35823579
// This happens in proc() function.
35833580
let upstairs_connection = UpstairsConnection {
@@ -3653,16 +3650,10 @@ mod test {
36533650
region.extend(extent_count).await?;
36543651

36553652
let path_dir = dir.as_ref().to_path_buf();
3656-
let ads = build_downstairs_for_region(
3657-
&path_dir,
3658-
false,
3659-
false,
3660-
false,
3661-
false,
3662-
false,
3663-
Some(csl()),
3664-
)
3665-
.await?;
3653+
let ads = Downstairs::new_builder(&path_dir, false)
3654+
.set_logger(csl())
3655+
.build()
3656+
.await?;
36663657

36673658
Ok(ads)
36683659
}
@@ -5506,16 +5497,10 @@ mod test {
55065497

55075498
let path_dir = dir.as_ref().to_path_buf();
55085499

5509-
build_downstairs_for_region(
5510-
&path_dir,
5511-
false, // lossy
5512-
false, // read errors
5513-
false, // write errors
5514-
false, // flush errors
5515-
read_only,
5516-
Some(csl()),
5517-
)
5518-
.await
5500+
Downstairs::new_builder(&path_dir, read_only)
5501+
.set_logger(csl())
5502+
.build()
5503+
.await
55195504
}
55205505

55215506
#[tokio::test]
@@ -5915,16 +5900,10 @@ mod test {
59155900
region.extend(2).await?;
59165901

59175902
let path_dir = dir.as_ref().to_path_buf();
5918-
let ads = build_downstairs_for_region(
5919-
&path_dir,
5920-
false,
5921-
false,
5922-
false,
5923-
false,
5924-
false,
5925-
Some(csl()),
5926-
)
5927-
.await?;
5903+
let ads = Downstairs::new_builder(&path_dir, false)
5904+
.set_logger(csl())
5905+
.build()
5906+
.await?;
59285907

59295908
// This happens in proc() function.
59305909
let upstairs_connection_1 = UpstairsConnection {
@@ -6011,16 +5990,10 @@ mod test {
60115990
region.extend(2).await?;
60125991

60135992
let path_dir = dir.as_ref().to_path_buf();
6014-
let ads = build_downstairs_for_region(
6015-
&path_dir,
6016-
false,
6017-
false,
6018-
false,
6019-
false,
6020-
false,
6021-
Some(csl()),
6022-
)
6023-
.await?;
5993+
let ads = Downstairs::new_builder(&path_dir, false)
5994+
.set_logger(csl())
5995+
.build()
5996+
.await?;
60245997

60255998
// This happens in proc() function.
60265999
let upstairs_connection_1 = UpstairsConnection {
@@ -6107,16 +6080,10 @@ mod test {
61076080
region.extend(2).await?;
61086081

61096082
let path_dir = dir.as_ref().to_path_buf();
6110-
let ads = build_downstairs_for_region(
6111-
&path_dir,
6112-
false,
6113-
false,
6114-
false,
6115-
false,
6116-
false,
6117-
Some(csl()),
6118-
)
6119-
.await?;
6083+
let ads = Downstairs::new_builder(&path_dir, false)
6084+
.set_logger(csl())
6085+
.build()
6086+
.await?;
61206087

61216088
// This happens in proc() function.
61226089
let upstairs_connection_1 = UpstairsConnection {

0 commit comments

Comments
 (0)