-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrepair.rs
422 lines (367 loc) · 11.9 KB
/
repair.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
// Copyright 2022 Oxide Computer Company
use std::path::PathBuf;
use std::sync::Arc;
use dropshot::ApiDescription;
use dropshot::ConfigDropshot;
use dropshot::HandlerTaskMode;
use dropshot::HttpError;
use dropshot::HttpResponseOk;
use dropshot::HttpServerStarter;
use dropshot::RequestContext;
use dropshot::{endpoint, Path};
use http::{Response, StatusCode};
use hyper::Body;
use schemars::JsonSchema;
use serde::Deserialize;
use super::*;
use crate::extent::{extent_dir, extent_file_name, extent_path, ExtentType};
/**
* Our context is the root of the region we want to serve.
*/
pub struct FileServerContext {
region_dir: PathBuf,
read_only: bool,
region_definition: RegionDefinition,
downstairs: Arc<Mutex<Downstairs>>,
}
pub fn write_openapi<W: Write>(f: &mut W) -> Result<()> {
let api = build_api();
api.openapi("Downstairs Repair", "0.0.0").write(f)?;
Ok(())
}
fn build_api() -> ApiDescription<Arc<FileServerContext>> {
let mut api = ApiDescription::new();
api.register(get_extent_file).unwrap();
api.register(get_files_for_extent).unwrap();
api.register(get_region_info).unwrap();
api.register(get_region_mode).unwrap();
api.register(extent_repair_ready).unwrap();
api
}
/// Returns Ok(listen address) if everything launched ok, Err otherwise
pub async fn repair_main(
downstairs: Arc<Mutex<Downstairs>>,
addr: SocketAddr,
log: &Logger,
) -> Result<SocketAddr, String> {
/*
* We must specify a configuration with a bind address.
*/
let config_dropshot = ConfigDropshot {
bind_address: addr,
request_body_max_bytes: 1024,
default_handler_task_mode: HandlerTaskMode::Detached,
};
/*
* Build a description of the API
*/
let api = build_api();
/*
* Record the region directory where all the extents and metadata
* files live.
*/
let ds = downstairs.lock().await;
let region_dir = ds.region.dir.clone();
let read_only = ds.read_only;
let region_definition = ds.region.def();
drop(ds);
info!(log, "Repair listens on {} for path:{:?}", addr, region_dir);
let context = FileServerContext {
region_dir,
read_only,
region_definition,
downstairs,
};
/*
* Set up the server.
*/
let server =
HttpServerStarter::new(&config_dropshot, api, context.into(), log)
.map_err(|error| format!("failed to create server: {}", error))?
.start();
let local_addr = server.local_addr();
tokio::spawn(server);
Ok(local_addr)
}
#[derive(Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct Eid {
eid: u32,
}
#[derive(Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum FileType {
#[serde(rename = "data")]
Data,
#[serde(rename = "db")]
Database,
#[serde(rename = "db_shm")]
DatabaseSharedMemory,
#[serde(rename = "db_wal")]
DatabaseLog,
}
#[derive(Deserialize, JsonSchema)]
pub struct FileSpec {
eid: u32,
file_type: FileType,
}
#[endpoint {
method = GET,
path = "/newextent/{eid}/{file_type}",
}]
async fn get_extent_file(
rqctx: RequestContext<Arc<FileServerContext>>,
path: Path<FileSpec>,
) -> Result<Response<Body>, HttpError> {
let fs = path.into_inner();
let eid = fs.eid;
let mut extent_path = extent_path(rqctx.context().region_dir.clone(), eid);
match fs.file_type {
FileType::Database => {
extent_path.set_extension("db");
}
FileType::DatabaseSharedMemory => {
extent_path.set_extension("db-shm");
}
FileType::DatabaseLog => {
extent_path.set_extension("db-wal");
}
FileType::Data => (),
};
get_a_file(extent_path).await
}
async fn get_a_file(path: PathBuf) -> Result<Response<Body>, HttpError> {
/*
* Make sure our file is neither a link nor a directory.
*/
let m = path.symlink_metadata().map_err(|e| {
HttpError::for_bad_request(
None,
format!("Failed to get {:?} metadata: {:#}", path, e),
)
})?;
if m.file_type().is_symlink() {
Err(HttpError::for_bad_request(
None,
"File is symlink".to_string(),
))
} else if path.is_dir() {
Err(HttpError::for_bad_request(
None,
"Expected a file, found a directory".to_string(),
))
} else {
let file = tokio::fs::File::open(&path).await.map_err(|e| {
HttpError::for_bad_request(
None,
format!("file {:?}: {:#}", path, e),
)
})?;
let file_stream = hyper_staticfile::FileBytesStream::new(file);
let content_type = "application/octet-stream".to_string();
Ok(Response::builder()
.status(StatusCode::OK)
.header(http::header::CONTENT_TYPE, content_type)
.body(file_stream.into_body())?)
}
}
/// Return true if the provided extent is closed or the region is read only
#[endpoint {
method = GET,
path = "/extent/{eid}/repair-ready",
}]
async fn extent_repair_ready(
rqctx: RequestContext<Arc<FileServerContext>>,
path: Path<Eid>,
) -> Result<HttpResponseOk<bool>, HttpError> {
let eid: usize = path.into_inner().eid as usize;
let downstairs = &rqctx.context().downstairs;
// If the region is read only, the extent is always ready.
if rqctx.context().read_only {
return Ok(HttpResponseOk(true));
}
let res = {
let ds = downstairs.lock().await;
matches!(ds.region.extents[eid], ExtentState::Closed)
};
Ok(HttpResponseOk(res))
}
/**
* Get the list of files related to an extent.
*
* For a given extent, return a vec of strings representing the names of
* the files that exist for that extent.
*/
#[endpoint {
method = GET,
path = "/extent/{eid}/files",
}]
async fn get_files_for_extent(
rqctx: RequestContext<Arc<FileServerContext>>,
path: Path<Eid>,
) -> Result<HttpResponseOk<Vec<String>>, HttpError> {
let eid = path.into_inner().eid;
let extent_dir = extent_dir(rqctx.context().region_dir.clone(), eid);
// Some sanity checking on the extent path
let m = extent_dir.symlink_metadata().map_err(|e| {
HttpError::for_bad_request(
None,
format!("Failed to get {:?} metadata: {:#}", extent_dir, e),
)
})?;
if m.file_type().is_symlink() {
Err(HttpError::for_bad_request(
None,
format!("File {:?} is a symlink", extent_dir),
))
} else if !extent_dir.is_dir() {
Err(HttpError::for_bad_request(
None,
format!("Expected {:?} to be a directory", extent_dir),
))
} else {
let files = extent_file_list(extent_dir, eid)?;
Ok(HttpResponseOk(files))
}
}
/**
* Return the list of extent files we have in our region directory
* that correspond to the given extent. Return an error if any
* of the required files are missing.
*/
fn extent_file_list(
extent_dir: PathBuf,
eid: u32,
) -> Result<Vec<String>, HttpError> {
let mut files = Vec::new();
let possible_files = [
(ExtentType::Data, true),
(ExtentType::Db, false),
(ExtentType::DbShm, false),
(ExtentType::DbWal, false),
];
for (file, required) in possible_files.into_iter() {
let mut fullname = extent_dir.clone();
let file_name = extent_file_name(eid, file);
fullname.push(file_name.clone());
if fullname.exists() {
files.push(file_name);
} else if required {
return Err(HttpError::for_bad_request(None, "EBADF".to_string()));
}
}
Ok(files)
}
/// Return the RegionDefinition describing our region.
#[endpoint {
method = GET,
path = "/region-info",
}]
async fn get_region_info(
rqctx: RequestContext<Arc<FileServerContext>>,
) -> Result<HttpResponseOk<crucible_common::RegionDefinition>, HttpError> {
let region_definition = rqctx.context().region_definition;
Ok(HttpResponseOk(region_definition))
}
/// Return the region-mode describing our region.
#[endpoint {
method = GET,
path = "/region-mode",
}]
async fn get_region_mode(
rqctx: RequestContext<Arc<FileServerContext>>,
) -> Result<HttpResponseOk<bool>, HttpError> {
let read_only = rqctx.context().read_only;
Ok(HttpResponseOk(read_only))
}
#[cfg(test)]
mod test {
use super::*;
use openapiv3::OpenAPI;
use tempfile::tempdir;
fn new_region_options() -> crucible_common::RegionOptions {
let mut region_options: crucible_common::RegionOptions =
Default::default();
let block_size = 512;
region_options.set_block_size(block_size);
region_options
.set_extent_size(Block::new(10, block_size.trailing_zeros()));
region_options
}
// Create a simple logger
fn csl() -> Logger {
build_logger()
}
#[tokio::test]
async fn extent_expected_files() -> Result<()> {
// Verify that the list of files returned for an extent matches
// what we expect. This is a hack of sorts as we are hard coding
// the expected names of files here in that test, rather than
// determine them through some programmatic means.
let dir = tempdir()?;
let mut region =
Region::create(&dir, new_region_options(), csl()).await?;
region.extend(3).await?;
// Determine the directory and name for expected extent files.
let ed = extent_dir(&dir, 1);
let mut ex_files = extent_file_list(ed, 1).unwrap();
ex_files.sort();
let expected = vec!["001"];
println!("files: {:?}", ex_files);
assert_eq!(ex_files, expected);
Ok(())
}
#[tokio::test]
async fn extent_expected_files_with_close() -> Result<()> {
// Verify that the list of files returned for an extent matches
// what we expect. In this case we expect the extent data file and
// nothing else. We close the extent here first, and on illumos that
// behaves a little different than elsewhere.
let dir = tempdir()?;
let mut region =
Region::create(&dir, new_region_options(), csl()).await?;
region.extend(3).await?;
region.close_extent(1).await.unwrap();
// Determine the directory and name for expected extent files.
let extent_dir = extent_dir(&dir, 1);
let mut ex_files = extent_file_list(extent_dir, 1).unwrap();
ex_files.sort();
let expected = vec!["001"];
println!("files: {:?}", ex_files);
assert_eq!(ex_files, expected);
Ok(())
}
#[tokio::test]
async fn extent_expected_files_fail() -> Result<()> {
// Verify that we get an error if the expected extent file
// is missing.
let dir = tempdir()?;
let mut region =
Region::create(&dir, new_region_options(), csl()).await?;
region.extend(3).await?;
// Determine the directory and name for expected extent files.
let extent_dir = extent_dir(&dir, 1);
// Delete the extent file
let mut rm_file = extent_dir.clone();
rm_file.push(extent_file_name(1, ExtentType::Data));
std::fs::remove_file(&rm_file).unwrap();
assert!(extent_file_list(extent_dir, 1).is_err());
Ok(())
}
#[test]
fn test_crucible_repair_openapi() {
let mut raw = Vec::new();
write_openapi(&mut raw).unwrap();
let actual = String::from_utf8(raw).unwrap();
// Make sure the result parses as a valid OpenAPI spec.
let spec = serde_json::from_str::<OpenAPI>(&actual)
.expect("output was not valid OpenAPI");
// Check for lint errors.
let errors = openapi_lint::validate(&spec);
assert!(errors.is_empty(), "{}", errors.join("\n\n"));
expectorate::assert_contents(
"../openapi/downstairs-repair.json",
&actual,
);
}
}