Skip to content

Commit

Permalink
Write to temp file (#24)
Browse files Browse the repository at this point in the history
Write out the index to a temp file when writing too ensure we don't partially write a corrupt copy.
  • Loading branch information
ianoc authored Nov 3, 2020
1 parent 0721d50 commit a27b682
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 40 deletions.
14 changes: 10 additions & 4 deletions bazelfe-core/src/bazel_runner/bazel_runner_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
if index_table.is_mutated() {
debug!("Writing out index file...");

if let Some(e) = &opt.index_input_location {
if let Some(parent) = e.parent() {
if let Some(target_path) = &opt.index_input_location {
if let Some(parent) = target_path.parent() {
std::fs::create_dir_all(parent).unwrap();
}
let mut file = std::fs::File::create(&e).unwrap();
index_table.write(&mut file).await
let mut temp_path = target_path.clone();
temp_path.set_extension("tmp");

let mut file = std::fs::File::create(&temp_path).unwrap();
index_table.write(&mut file).await;
drop(file);
std::fs::rename(temp_path, target_path)
.expect("Expected to be able to rename our temp path into the final location.");
}
debug!("Index write complete.");
}
Expand Down
11 changes: 9 additions & 2 deletions bazelfe-core/src/build_events/build_event_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,22 @@ pub mod bazel_event {
.and_then(|e| match e {
build_event_stream::build_event_id::Id::TargetCompleted(
target_completed_id,
) => Some(target_completed_id.label.clone()),
) => Some((
target_completed_id.label.clone(),
Some(target_completed_id.aspect.clone())
.filter(|e| !e.is_empty()),
)),
_ => None,
});

target_label_opt.and_then(|label| {
target_label_opt.and_then(|(label, aspect)| {
v.payload.as_ref().and_then(|e| match e {
build_event_stream::build_event::Payload::Completed(
target_completed,
) => Some(Evt::TargetCompleted(TargetCompletedEvt {
success: target_completed.success,
label: label,
aspect: aspect,
output_groups: target_completed.output_group.clone(),
})),
_ => None,
Expand Down Expand Up @@ -286,6 +291,7 @@ pub mod bazel_event {
#[derive(Clone, PartialEq, Debug)]
pub struct TargetCompletedEvt {
pub label: String,
pub aspect: Option<String>,
pub success: bool,
pub output_groups: Vec<build_event_stream::OutputGroup>,
}
Expand Down Expand Up @@ -605,6 +611,7 @@ mod tests {
bazel_event::BazelBuildEvent {
event: bazel_event::Evt::TargetCompleted(bazel_event::TargetCompletedEvt {
label: label_name,
aspect: None,
success: true,
output_groups: vec![OutputGroup {
name: String::from("default"),
Expand Down
2 changes: 2 additions & 0 deletions bazelfe-core/src/build_events/hydrated_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct ActionSuccessInfo {
#[derive(Clone, PartialEq, Debug)]
pub struct TargetCompleteInfo {
pub label: String,
pub aspect: Option<String>,
pub success: bool,
pub target_kind: Option<String>,
pub output_files: Vec<build_event_stream::file::File>,
Expand Down Expand Up @@ -109,6 +110,7 @@ async fn tce_event(
let target_complete_info = TargetCompleteInfo {
output_files: output_files,
target_kind: rule_kind_lookup.get(&tce.label).map(|e| e.clone()),
aspect: tce.aspect,
label: tce.label,
success: tce.success,
};
Expand Down
44 changes: 11 additions & 33 deletions bazelfe-core/src/index_table/index_table_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,47 +193,25 @@ impl IndexTableValue {
return false;
}

let insert_position =
match write_vec.binary_search_by_key(&Priority(priority), |e| e.priority) {
Ok(current_position) => current_position,
Err(should_be_at_position) => should_be_at_position,
};
if insert_position == position {
write_vec[position].priority = Priority(priority);
return true;
}
write_vec.remove(position);
let insert_position = if insert_position < position {
insert_position
} else {
insert_position - 1
};
write_vec.insert(
insert_position,
IndexTableValueEntry {
target: target_v,
priority: Priority(priority),
},
);

write_vec.push(IndexTableValueEntry {
target: target_v,
priority: Priority(priority),
});
write_vec.sort();
if write_vec.len() > 10 {
write_vec.truncate(10);
}
}
None => {
let mut write_vec = self.0.write().await;

let insert_position =
match write_vec.binary_search_by_key(&Priority(priority), |e| e.priority) {
Ok(current_position) => current_position,
Err(should_be_at_position) => should_be_at_position,
};
write_vec.insert(
insert_position,
IndexTableValueEntry {
target: target_v,
priority: Priority(priority),
},
);
write_vec.push(IndexTableValueEntry {
target: target_v,
priority: Priority(priority),
});
write_vec.sort();
if write_vec.len() > 10 {
write_vec.truncate(10);
}
Expand Down
6 changes: 5 additions & 1 deletion bazelfe-core/src/jvm_indexer/jvm_indexer_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
String::from("bazel_tools"),
String::from("remote_java_tools_linux"),
String::from("jdk-default"),
String::from("remote_java_tools_linux"),
String::from("remote_java_tools_darwin"),
String::from("remote_java_tools_windows"),
String::from("remote_coverage_tools"),
String::from("io_bazel_stardoc"),
String::from("io_bazel"),
];

if let Some(r) = parse_current_repo_name() {
Expand Down

0 comments on commit a27b682

Please sign in to comment.