Skip to content

Commit

Permalink
delete target file before rename_file
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorgan committed Sep 3, 2024
1 parent 5c0bfc6 commit ae8a6aa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
26 changes: 14 additions & 12 deletions core/src/services/hdfs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,6 @@ impl Access for HdfsBackend {
if !target_exists {
let parent = get_parent(&target_path);
self.client.create_dir(parent).map_err(new_std_io_error)?;
} else if tmp_path.is_some() {
// we must delete the target_path, otherwise the rename_file operation will fail
self.client
.remove_file(&target_path)
.map_err(new_std_io_error)?;
}

let mut open_options = self.client.open_file();
Expand All @@ -335,7 +330,13 @@ impl Access for HdfsBackend {

Ok((
RpWrite::new(),
HdfsWriter::new(target_path, tmp_path, f, Arc::clone(&self.client)),
HdfsWriter::new(
target_path,
tmp_path,
f,
Arc::clone(&self.client),
target_exists,
),
))
}

Expand Down Expand Up @@ -507,11 +508,6 @@ impl Access for HdfsBackend {
if !target_exists {
let parent = get_parent(&target_path);
self.client.create_dir(parent).map_err(new_std_io_error)?;
} else if tmp_path.is_some() {
// we must delete the target_path, otherwise the rename_file operation will fail
self.client
.remove_file(&target_path)
.map_err(new_std_io_error)?;
}

let mut open_options = self.client.open_file();
Expand All @@ -528,7 +524,13 @@ impl Access for HdfsBackend {

Ok((
RpWrite::new(),
HdfsWriter::new(target_path, tmp_path, f, Arc::clone(&self.client)),
HdfsWriter::new(
target_path,
tmp_path,
f,
Arc::clone(&self.client),
target_exists,
),
))
}

Expand Down
15 changes: 15 additions & 0 deletions core/src/services/hdfs/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct HdfsWriter<F> {
tmp_path: Option<String>,
f: Option<F>,
client: Arc<hdrs::Client>,
target_path_exists: bool,
}

/// # Safety
Expand All @@ -42,12 +43,14 @@ impl<F> HdfsWriter<F> {
tmp_path: Option<String>,
f: F,
client: Arc<hdrs::Client>,
target_path_exists: bool,
) -> Self {
Self {
target_path,
tmp_path,
f: Some(f),
client,
target_path_exists,
}
}
}
Expand All @@ -70,6 +73,12 @@ impl oio::Write for HdfsWriter<hdrs::AsyncFile> {

// TODO: we need to make rename async.
if let Some(tmp_path) = &self.tmp_path {
// we must delete the target_path, otherwise the rename_file operation will fail
if self.target_path_exists {
self.client
.remove_file(&self.target_path)
.map_err(new_std_io_error)?;
}
self.client
.rename_file(tmp_path, &self.target_path)
.map_err(new_std_io_error)?
Expand Down Expand Up @@ -102,6 +111,12 @@ impl oio::BlockingWrite for HdfsWriter<hdrs::File> {
f.flush().map_err(new_std_io_error)?;

if let Some(tmp_path) = &self.tmp_path {
// we must delete the target_path, otherwise the rename_file operation will fail
if self.target_path_exists {
self.client
.remove_file(&self.target_path)
.map_err(new_std_io_error)?;
}
self.client
.rename_file(tmp_path, &self.target_path)
.map_err(new_std_io_error)?;
Expand Down

0 comments on commit ae8a6aa

Please sign in to comment.