Skip to content

Commit

Permalink
feat: remove crc32 iso3309 from write piece func (#933)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Jan 7, 2025
1 parent 62047a9 commit 6d9cdbf
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 118 deletions.
17 changes: 8 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ members = [
]

[workspace.package]
version = "0.2.1"
version = "0.2.2"
authors = ["The Dragonfly Developers"]
homepage = "https://d7y.io/"
repository = "https://github.com/dragonflyoss/client.git"
Expand All @@ -22,13 +22,13 @@ readme = "README.md"
edition = "2021"

[workspace.dependencies]
dragonfly-client = { path = "dragonfly-client", version = "0.2.1" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.2.1" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.2.1" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.2.1" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.2.1" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.2.1" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.2.1" }
dragonfly-client = { path = "dragonfly-client", version = "0.2.2" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.2.2" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.2.2" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.2.2" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.2.2" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.2.2" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.2.2" }
thiserror = "1.0"
dragonfly-api = "=2.1.3"
reqwest = { version = "0.12.4", features = [
Expand Down Expand Up @@ -61,7 +61,6 @@ rustls-pki-types = "1.10.1"
rustls-pemfile = "2.2.0"
sha2 = "0.10"
blake3 = "1.5.5"
crc32fast = "1.4.2"
crc32c = "0.6.8"
uuid = { version = "1.11", features = ["v4"] }
hex = "0.4"
Expand Down
1 change: 0 additions & 1 deletion dragonfly-client-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ prost-wkt-types.workspace = true
tokio.workspace = true
tokio-util.workspace = true
sha2.workspace = true
crc32fast.workspace = true
crc32c.workspace = true
base16ct.workspace = true
num_cpus = "1.0"
Expand Down
51 changes: 0 additions & 51 deletions dragonfly-client-storage/src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use tokio::fs::{self, File, OpenOptions};
use tokio::io::{
self, AsyncRead, AsyncReadExt, AsyncSeekExt, AsyncWriteExt, BufReader, BufWriter, SeekFrom,
};
use tokio_util::io::InspectReader;
use tracing::{error, info, instrument, warn};

/// DEFAULT_CONTENT_DIR is the default directory for store content.
Expand Down Expand Up @@ -362,56 +361,6 @@ impl Content {
})
}

/// write_piece_with_crc32_iso3309 writes the piece to the content with crc32 iso3309, there is
/// no hardware acceleration.
#[instrument(skip_all)]
pub async fn write_piece_with_crc32_iso3309<R: AsyncRead + Unpin + ?Sized>(
&self,
task_id: &str,
offset: u64,
reader: &mut R,
) -> Result<WritePieceResponse> {
// Use a buffer to read the piece.
let reader = BufReader::with_capacity(self.config.storage.write_buffer_size, reader);

// Crc32 is used to calculate the hash of the piece.
let mut hasher = crc32fast::Hasher::new();

// InspectReader is used to calculate the hash of the piece.
let mut tee = InspectReader::new(reader, |bytes| {
hasher.update(bytes);
});

// Open the file and seek to the offset.
let task_path = self.create_or_get_task_path(task_id).await?;
let mut f = OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(task_path.as_path())
.await
.inspect_err(|err| {
error!("open {:?} failed: {}", task_path, err);
})?;

f.seek(SeekFrom::Start(offset)).await.inspect_err(|err| {
error!("seek {:?} failed: {}", task_path, err);
})?;

// Copy the piece to the file.
let mut writer = BufWriter::with_capacity(self.config.storage.write_buffer_size, f);
let length = io::copy(&mut tee, &mut writer).await.inspect_err(|err| {
error!("copy {:?} failed: {}", task_path, err);
})?;

// Calculate the hash of the piece.
let hash = hasher.finalize();
Ok(WritePieceResponse {
length,
hash: base16ct::lower::encode_string(&hash.to_be_bytes()),
})
}

/// get_task_path returns the task path by task id.
#[instrument(skip_all)]
fn get_task_path(&self, task_id: &str) -> PathBuf {
Expand Down
20 changes: 5 additions & 15 deletions dragonfly-client-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

use dragonfly_api::common::v2::Range;
use dragonfly_client_config::dfdaemon::Config;
use dragonfly_client_core::{
error::{ErrorType, OrErr},
Error, Result,
};
use dragonfly_client_core::{Error, Result};
use dragonfly_client_util::digest::{Algorithm, Digest};
use reqwest::header::HeaderMap;
use std::path::Path;
Expand Down Expand Up @@ -337,17 +334,10 @@ impl Storage {
parent_id: &str,
reader: &mut R,
) -> Result<metadata::Piece> {
let digest: Digest = expected_digest.parse().or_err(ErrorType::ParseError)?;
let response = if digest.is_crc32_iso3309() {
// Compatible with the old version.
self.content
.write_piece_with_crc32_iso3309(task_id, offset, reader)
.await?
} else {
self.content
.write_piece_with_crc32_castagnoli(task_id, offset, reader)
.await?
};
let response = self
.content
.write_piece_with_crc32_castagnoli(task_id, offset, reader)
.await?;

let length = response.length;
let digest = Digest::new(Algorithm::Crc32, response.hash);
Expand Down
2 changes: 1 addition & 1 deletion dragonfly-client-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ uuid.workspace = true
hex.workspace = true
openssl.workspace = true
blake3.workspace = true
crc32fast.workspace = true
crc32c.workspace = true
base16ct.workspace = true
base64 = "0.22.1"
crc32fast = "1.4.2"

[dev-dependencies]
tempfile.workspace = true
32 changes: 0 additions & 32 deletions dragonfly-client-util/src/digest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,6 @@ impl Digest {
pub fn encoded(&self) -> &str {
&self.encoded
}

// is_crc32_iso3309 checks if the crc32 encoded string uses the ISO 3309 polynomial.
pub fn is_crc32_iso3309(&self) -> bool {
self.algorithm == Algorithm::Crc32 && self.encoded.len() == 8
}

// is_crc32_castagnoli checks if the crc32 encoded string uses the Castagnoli polynomial.
pub fn is_crc32_castagnoli(&self) -> bool {
self.algorithm == Algorithm::Crc32 && self.encoded.len() == 10
}
}

/// Digest implements the Display.
Expand Down Expand Up @@ -235,26 +225,4 @@ mod tests {
calculate_file_hash(Algorithm::Crc32, path).expect("failed to calculate Sha512 hash");
assert_eq!(digest.encoded(), expected_crc32);
}

#[test]
fn test_is_crc32_iso3309() {
let mut hasher = crc32fast::Hasher::new();
hasher.update(b"test");
let hash = hasher.finalize();

let digest = Digest::new(
Algorithm::Crc32,
base16ct::lower::encode_string(&hash.to_be_bytes()),
);
assert!(digest.is_crc32_iso3309());
}

#[test]
fn test_is_crc32_castagnoli() {
let crc = crc32c::crc32c(b"test");
let encoded = crc.to_string();

let digest = Digest::new(Algorithm::Crc32, encoded);
assert!(digest.is_crc32_castagnoli());
}
}

0 comments on commit 6d9cdbf

Please sign in to comment.