Compare commits

...

249 Commits

Author SHA1 Message Date
dependabot[bot] d4aaa03301
chore(deps): Bump github/codeql-action from 3.29.7 to 3.29.8 (#1282)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.29.7 to 3.29.8.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](51f77329af...76621b61de)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.29.8
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-08-13 11:39:56 +08:00
dependabot[bot] d68dba20c1
chore(deps): Bump clap from 4.5.41 to 4.5.43 (#1283)
Bumps [clap](https://github.com/clap-rs/clap) from 4.5.41 to 4.5.43.
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/clap_complete-v4.5.41...clap_complete-v4.5.43)

---
updated-dependencies:
- dependency-name: clap
  dependency-version: 4.5.43
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-08-13 11:39:31 +08:00
dependabot[bot] 8116538556
chore(deps): Bump glob from 0.3.2 to 0.3.3 (#1285)
Bumps [glob](https://github.com/rust-lang/glob) from 0.3.2 to 0.3.3.
- [Release notes](https://github.com/rust-lang/glob/releases)
- [Changelog](https://github.com/rust-lang/glob/blob/master/CHANGELOG.md)
- [Commits](https://github.com/rust-lang/glob/compare/v0.3.2...v0.3.3)

---
updated-dependencies:
- dependency-name: glob
  dependency-version: 0.3.3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-08-13 11:39:00 +08:00
dependabot[bot] 3822048e6b
chore(deps): Bump actions/download-artifact from 4 to 5 (#1286)
Bumps [actions/download-artifact](https://github.com/actions/download-artifact) from 4 to 5.
- [Release notes](https://github.com/actions/download-artifact/releases)
- [Commits](https://github.com/actions/download-artifact/compare/v4...v5)

---
updated-dependencies:
- dependency-name: actions/download-artifact
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-08-13 11:38:35 +08:00
Gaius e05270e598
feat: get network speed for scheduler (#1279)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-08-11 22:24:16 +08:00
Chlins Zhang 2cca5c7b9b
refactor: remove the seed peer announcement to manager (#1261)
Signed-off-by: chlins <chlins.zhang@gmail.com>
2025-08-08 12:25:08 +08:00
this is my name 1f8a323665
docs:Modify Cache storage comments (#1277)
Modify Cache storage comments to clarify its usage scenarios.

Signed-off-by: fu220 <2863318196@qq.com>
2025-08-06 17:04:20 +08:00
Gaius e1ae65a48d
chore(ci/Dockerfile): add grpcurl for dfdaemon container (#1276)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-08-06 08:26:15 +00:00
Gaius e415df936d
feat: enable console subscriber layer for tracing spawn tasks on `127.0.0.1:6669` when log level is TRACE (#1275)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-08-06 06:58:47 +00:00
this is my name 848737e327
feat:removes the load_to_cache field (#1264)
This pull request removes the load_to_cache field and adds trait method definitions to dfdaemon_download.rs and dfdaemon_upload.rs.

- Removed cache-related processing from Task handling.
- Added trait method definitions such as download_cache_task to dfdaemon_download.rs and dfdaemon_upload.rs to comply with the API format of version 2.1.55.

- Aim to allow Task to focus on disk interactions while delegating memory cache operations to CacheTask.

Signed-off-by: fu220 <2863318196@qq.com>
2025-08-06 14:40:39 +08:00
Gaius 7796ee7342
chore(deps): remove unused dependencies (#1274)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-08-06 11:11:15 +08:00
dependabot[bot] 60ebb33b50
chore(deps): Bump hyper-util from 0.1.15 to 0.1.16 (#1267)
Bumps [hyper-util](https://github.com/hyperium/hyper-util) from 0.1.15 to 0.1.16.
- [Release notes](https://github.com/hyperium/hyper-util/releases)
- [Changelog](https://github.com/hyperium/hyper-util/blob/master/CHANGELOG.md)
- [Commits](https://github.com/hyperium/hyper-util/compare/v0.1.15...v0.1.16)

---
updated-dependencies:
- dependency-name: hyper-util
  dependency-version: 0.1.16
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-08-05 17:12:52 +08:00
dependabot[bot] b2ac70f5f6
chore(deps): Bump tokio-util from 0.7.15 to 0.7.16 (#1270)
Bumps [tokio-util](https://github.com/tokio-rs/tokio) from 0.7.15 to 0.7.16.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-util-0.7.15...tokio-util-0.7.16)

---
updated-dependencies:
- dependency-name: tokio-util
  dependency-version: 0.7.16
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-08-05 17:11:55 +08:00
Gaius 52b263ac66
feat: Disable compression in HTTP client configuration (#1273)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-08-05 16:58:43 +08:00
dependabot[bot] 15dea31154
chore(deps): Bump tokio from 1.46.1 to 1.47.1 (#1266)
Bumps [tokio](https://github.com/tokio-rs/tokio) from 1.46.1 to 1.47.1.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-1.46.1...tokio-1.47.1)

---
updated-dependencies:
- dependency-name: tokio
  dependency-version: 1.47.1
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-08-05 11:37:48 +08:00
dependabot[bot] ab616f9498
chore(deps): Bump github/codeql-action from 3.29.3 to 3.29.5 (#1271)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.29.3 to 3.29.5.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](d6bbdef45e...51f77329af)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.29.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-08-05 03:26:36 +00:00
dependabot[bot] f0b406c37a
chore(deps): Bump kentaro-m/auto-assign-action from 586b61c136c65d09c1775da39cc4a80e026834f4 to 9f6dbe84a80c6e7639d1b9698048b201052a2a94 (#1272)
chore(deps): Bump kentaro-m/auto-assign-action

Bumps [kentaro-m/auto-assign-action](https://github.com/kentaro-m/auto-assign-action) from 586b61c136c65d09c1775da39cc4a80e026834f4 to 9f6dbe84a80c6e7639d1b9698048b201052a2a94.
- [Release notes](https://github.com/kentaro-m/auto-assign-action/releases)
- [Commits](586b61c136...9f6dbe84a8)

---
updated-dependencies:
- dependency-name: kentaro-m/auto-assign-action
  dependency-version: 9f6dbe84a80c6e7639d1b9698048b201052a2a94
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-08-05 11:23:13 +08:00
dependabot[bot] 3d4b07e86e
chore(deps): Bump taiki-e/cache-cargo-install-action from 2.2.0 to 2.3.0 (#1269)
Bumps [taiki-e/cache-cargo-install-action](https://github.com/taiki-e/cache-cargo-install-action) from 2.2.0 to 2.3.0.
- [Release notes](https://github.com/taiki-e/cache-cargo-install-action/releases)
- [Changelog](https://github.com/taiki-e/cache-cargo-install-action/blob/main/CHANGELOG.md)
- [Commits](1bb5728d79...b33c63d3b3)

---
updated-dependencies:
- dependency-name: taiki-e/cache-cargo-install-action
  dependency-version: 2.3.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-08-05 11:22:51 +08:00
dependabot[bot] 02690b8365
chore(deps): Bump serde_json from 1.0.141 to 1.0.142 (#1268)
Bumps [serde_json](https://github.com/serde-rs/json) from 1.0.141 to 1.0.142.
- [Release notes](https://github.com/serde-rs/json/releases)
- [Commits](https://github.com/serde-rs/json/compare/v1.0.141...v1.0.142)

---
updated-dependencies:
- dependency-name: serde_json
  dependency-version: 1.0.142
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-08-05 11:22:26 +08:00
Gaius f0c983093a
feat: add Range header to ensure Content-Length is returned in response headers (#1263)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-08-04 06:25:58 +00:00
Gaius cd6ca368d5
feat: rename tracing field from `uri` to `url` in proxy handler functions (#1260)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-30 16:55:56 +08:00
Cyrus 1aefde8ed4
refactor: improve filter_entries test function (#1259)
* refactor filter_entries test

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

* fix a variable name

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

* merge all filter_entries test

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

* add a assert

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

---------

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>
2025-07-29 11:18:49 +08:00
Gaius b30993eef0
refactor(dfget): Improve logic and error handling in `filter_entries` (#1258)
- Updated function signature to use references (`&Url`, `&[String]`) for efficiency.
- Improved error handling with detailed `ValidationError` messages instead of generic `UnexpectedResponse`.
- Renamed `rel_path_to_entry` to `entries_by_relative_path` for better clarity.
- Replaced `Vec` with `HashSet` for filtered entries to avoid duplicates.
- Simplified parent directory path construction using `join("")`.
- Enhanced doc comments to clearly describe functionality and behavior.
- Streamlined pattern compilation and iteration using `iter()`.

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-25 14:09:34 +00:00
Cyrus cca88b3eea
feat: add include-files argrument in the downloaded directory (#1247)
* add include-files arg

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

* fix regular expression matching

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

* fix lint

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

---------

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>
2025-07-25 17:13:57 +08:00
Gaius bf6f49e0e9
feat: add task ID response header in Dragonfly client proxy (#1256)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-24 18:16:34 +08:00
Gaius 777c131fbe
feat: use piece_timeout for list task entries (#1255)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-24 09:33:47 +00:00
Gaius 45f86226cf
feat: use piece_timeout for list task entries (#1254)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-24 07:47:25 +00:00
Gaius a340f0c2f1
chore: update crate version to 1.0.6 (#1253)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-24 14:01:55 +08:00
Gaius 5c87849f67
feat: update gRPC server to use non-cloned reflection service and fix task piece filtering logic (#1252)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-24 03:29:11 +00:00
Gaius 74ab386d87
feat: Enhance stat_task error handling in dfdaemon_download and dfdaemon_upload (#1251)
feat: Bump dragonfly-api to 2.1.49 and add local_only support for task stat

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-23 10:06:11 +00:00
Gaius 10c73119cb
feat: Bump dragonfly-api to 2.1.49 and add local_only support for task stat (#1250)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-23 09:43:21 +00:00
Gaius 7190529693
feat(dragonfly-client): change permissions of download grpc uds (#1249)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-23 02:58:01 +00:00
Gaius e249089ec8
feat: support remote-ip for tracing (#1248)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-22 08:55:49 +00:00
dependabot[bot] 5660c73d9a
chore(deps): Bump github/codeql-action from 3.29.2 to 3.29.3 (#1244)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.29.2 to 3.29.3.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](181d5eefc2...d6bbdef45e)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.29.3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-22 08:55:05 +00:00
dependabot[bot] 52307b64cc
chore(deps): Bump rustix from 1.0.5 to 1.0.8 (#1243)
Bumps [rustix](https://github.com/bytecodealliance/rustix) from 1.0.5 to 1.0.8.
- [Release notes](https://github.com/bytecodealliance/rustix/releases)
- [Changelog](https://github.com/bytecodealliance/rustix/blob/main/CHANGES.md)
- [Commits](https://github.com/bytecodealliance/rustix/compare/v1.0.5...v1.0.8)

---
updated-dependencies:
- dependency-name: rustix
  dependency-version: 1.0.8
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-22 16:45:02 +08:00
dependabot[bot] 64e607db74
chore(deps): Bump serde_json from 1.0.140 to 1.0.141 (#1245)
Bumps [serde_json](https://github.com/serde-rs/json) from 1.0.140 to 1.0.141.
- [Release notes](https://github.com/serde-rs/json/releases)
- [Commits](https://github.com/serde-rs/json/compare/v1.0.140...v1.0.141)

---
updated-dependencies:
- dependency-name: serde_json
  dependency-version: 1.0.141
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-22 16:44:38 +08:00
Gaius a926e627d3
feat(dragonfly-client): change interface optional in DfdaemonUploadServerHandler (#1246)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-22 12:18:19 +08:00
dependabot[bot] 5cad620894
chore(deps): Bump kentaro-m/auto-assign-action from db77befe126df3163e8ee988af621569f7f2b82f to 586b61c136c65d09c1775da39cc4a80e026834f4 (#1242)
chore(deps): Bump kentaro-m/auto-assign-action

Bumps [kentaro-m/auto-assign-action](https://github.com/kentaro-m/auto-assign-action) from db77befe126df3163e8ee988af621569f7f2b82f to 586b61c136c65d09c1775da39cc4a80e026834f4.
- [Release notes](https://github.com/kentaro-m/auto-assign-action/releases)
- [Commits](db77befe12...586b61c136)

---
updated-dependencies:
- dependency-name: kentaro-m/auto-assign-action
  dependency-version: 586b61c136c65d09c1775da39cc4a80e026834f4
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-22 10:32:43 +08:00
Chlins Zhang d054d8ac2f
fix: rename scheduler_cluster_id in the host config (#1241)
Signed-off-by: chlins <chlins.zhang@gmail.com>
2025-07-21 17:37:40 +08:00
Chlins Zhang e53ed3411c
feat: support specify the schedulerClusterID in host config and carry… (#1240)
feat: support specify the schedulerClusterID in host config and carry it when listing schedulers

Signed-off-by: chlins <chlins.zhang@gmail.com>
2025-07-21 13:31:49 +08:00
Gaius aedbc2ceb0
fix: logging message in BackendFactory to reference correct backend_plugin_dir variable (#1237)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-18 11:25:41 +08:00
Gaius cafb074620
feat: update default plugin directory to /usr/local/lib/dragonfly/plugins/ on Linux (#1236)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-17 05:58:20 +00:00
Gaius 0bf6f5d1a4
refactor: list_task_entries in DfdaemonDownloadServerHandler (#1235)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-17 12:32:00 +08:00
Cyrus 7c9d691a04
feat: modify the execution logic of dfget to list directories (#1225)
* add list_task_entries grpc function

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

* change version

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

* fix code format

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

* fix clippy error

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

* move list_task_entries to grpc server

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

* fix code format

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

* merge list_task_entries function

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>

---------

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>
2025-07-17 11:31:55 +08:00
Gaius 4d6ad26d87
feat: move metadata update after content task creation in Storage::download_task_started (#1234)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-16 17:52:09 +08:00
Gaius 18a15df503
feat: add panic log for tracing (#1233)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-16 17:17:09 +08:00
dependabot[bot] b4bcae43b0
chore(deps): Bump hyper-util from 0.1.14 to 0.1.15 (#1229)
Bumps [hyper-util](https://github.com/hyperium/hyper-util) from 0.1.14 to 0.1.15.
- [Release notes](https://github.com/hyperium/hyper-util/releases)
- [Changelog](https://github.com/hyperium/hyper-util/blob/master/CHANGELOG.md)
- [Commits](https://github.com/hyperium/hyper-util/compare/v0.1.14...v0.1.15)

---
updated-dependencies:
- dependency-name: hyper-util
  dependency-version: 0.1.15
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-16 11:21:32 +08:00
dependabot[bot] 0c8073eedd
chore(deps): Bump crc32fast from 1.4.2 to 1.5.0 (#1230)
Bumps [crc32fast](https://github.com/srijs/rust-crc32fast) from 1.4.2 to 1.5.0.
- [Commits](https://github.com/srijs/rust-crc32fast/compare/v1.4.2...v1.5.0)

---
updated-dependencies:
- dependency-name: crc32fast
  dependency-version: 1.5.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-16 11:17:26 +08:00
dependabot[bot] 5a81dbe90b
chore(deps): Bump clap from 4.5.40 to 4.5.41 (#1231)
Bumps [clap](https://github.com/clap-rs/clap) from 4.5.40 to 4.5.41.
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/clap_complete-v4.5.40...clap_complete-v4.5.41)

---
updated-dependencies:
- dependency-name: clap
  dependency-version: 4.5.41
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-15 22:06:15 +08:00
Cyrus 9c3c0930af
feat: Create a blacklist of backends that do not support directory downloads (#1224)
Create a blacklist of backends that do not support path downloads

Signed-off-by: LunaWhispers <yangmuyucs@gmail.com>
2025-07-08 15:09:20 +08:00
dependabot[bot] 731b5fb370
chore(deps): Bump github/codeql-action from 3.29.1 to 3.29.2 (#1219)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.29.1 to 3.29.2.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](39edc492db...181d5eefc2)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.29.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-08 14:04:45 +08:00
dependabot[bot] 7e337bfe13
chore(deps): Bump kentaro-m/auto-assign-action from a558e3a7a389eae6d782d4f5ad0aaa5e36ec2e39 to db77befe126df3163e8ee988af621569f7f2b82f (#1220)
chore(deps): Bump kentaro-m/auto-assign-action

Bumps [kentaro-m/auto-assign-action](https://github.com/kentaro-m/auto-assign-action) from a558e3a7a389eae6d782d4f5ad0aaa5e36ec2e39 to db77befe126df3163e8ee988af621569f7f2b82f.
- [Release notes](https://github.com/kentaro-m/auto-assign-action/releases)
- [Commits](a558e3a7a3...db77befe12)

---
updated-dependencies:
- dependency-name: kentaro-m/auto-assign-action
  dependency-version: db77befe126df3163e8ee988af621569f7f2b82f
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-08 14:04:21 +08:00
dependabot[bot] 511117fe4e
chore(deps): Bump indicatif from 0.17.11 to 0.18.0 (#1221)
Bumps [indicatif](https://github.com/console-rs/indicatif) from 0.17.11 to 0.18.0.
- [Release notes](https://github.com/console-rs/indicatif/releases)
- [Commits](https://github.com/console-rs/indicatif/compare/0.17.11...0.18.0)

---
updated-dependencies:
- dependency-name: indicatif
  dependency-version: 0.18.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-08 14:03:57 +08:00
dependabot[bot] 997e14318d
chore(deps): Bump tokio from 1.45.1 to 1.46.1 (#1222)
Bumps [tokio](https://github.com/tokio-rs/tokio) from 1.45.1 to 1.46.1.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-1.45.1...tokio-1.46.1)

---
updated-dependencies:
- dependency-name: tokio
  dependency-version: 1.46.1
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-08 14:03:29 +08:00
dependabot[bot] 4db4a5b1ce
chore(deps): Bump aquasecurity/trivy-action from 0.31.0 to 0.32.0 (#1223)
Bumps [aquasecurity/trivy-action](https://github.com/aquasecurity/trivy-action) from 0.31.0 to 0.32.0.
- [Release notes](https://github.com/aquasecurity/trivy-action/releases)
- [Commits](76071ef0d7...dc5a429b52)

---
updated-dependencies:
- dependency-name: aquasecurity/trivy-action
  dependency-version: 0.32.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-08 14:03:02 +08:00
Gaius 3c5abded83
chore: update cargo version to 1.0.2 (#1218)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-07 13:01:49 +00:00
Gaius 5fbc681ee5
chore(Dockerfile): Change TARGETARCH to TARGETPLATFORM in Dockerfiles for multi-platform builds (#1217)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-07 10:48:06 +00:00
Gaius 612fa07845
chore(Dockerfile): support jemalloc for 64K page size (#1216)
chore(DOckerfile): support jemalloc for 64K page size

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-07 11:16:55 +08:00
Gaius 38abfaf4f3
feat: Update error message in task.rs to remove 'persistent cache' reference (#1215)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-03 19:04:30 +08:00
Gaius cad36b3a19
feat: remove write buffer and check piece length when write piece finished
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-03 02:03:59 +08:00
Gaius fb3be39b50
docs: Remove license section README.md (#1213)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-01 15:45:18 +08:00
Gaius 7cf69832a8
chore: update cargo version to 1.0.0 (#1212)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-07-01 11:54:51 +08:00
dependabot[bot] 99fdab86bb
chore(deps): Bump github/codeql-action from 3.29.0 to 3.29.1 (#1211)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.29.0 to 3.29.1.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](ce28f5bb42...39edc492db)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.29.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-01 11:43:28 +08:00
dependabot[bot] afd9e9fe3c
chore(deps): Bump taiki-e/cache-cargo-install-action from 2.1.2 to 2.2.0 (#1210)
Bumps [taiki-e/cache-cargo-install-action](https://github.com/taiki-e/cache-cargo-install-action) from 2.1.2 to 2.2.0.
- [Release notes](https://github.com/taiki-e/cache-cargo-install-action/releases)
- [Changelog](https://github.com/taiki-e/cache-cargo-install-action/blob/main/CHANGELOG.md)
- [Commits](5c9abe9a3f...1bb5728d79)

---
updated-dependencies:
- dependency-name: taiki-e/cache-cargo-install-action
  dependency-version: 2.2.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-07-01 11:43:10 +08:00
Gaius cec3407126
fix: OTLP endpoint URL parsing to correctly include protocol in tracing module (#1209)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-30 18:53:10 +08:00
Gaius 23fa1ba3b7
feat: verify digest when file is downloaded (#1208)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-30 14:32:07 +08:00
Gaius 4711bd86af
feat: bump version to 0.2.41 and optimize vector initialization in dfdaemon_upload (#1207)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-25 12:46:31 +00:00
Gaius a81a67a7bc
feat: support HTTPS protocol for otel (#1206)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-25 15:54:01 +08:00
dependabot[bot] b31732332c
chore(deps): Bump kentaro-m/auto-assign-action from e6c4932f995626505d2a5d85401b0319753caa11 to a558e3a7a389eae6d782d4f5ad0aaa5e36ec2e39 (#1204)
chore(deps): Bump kentaro-m/auto-assign-action

Bumps [kentaro-m/auto-assign-action](https://github.com/kentaro-m/auto-assign-action) from e6c4932f995626505d2a5d85401b0319753caa11 to a558e3a7a389eae6d782d4f5ad0aaa5e36ec2e39.
- [Release notes](https://github.com/kentaro-m/auto-assign-action/releases)
- [Commits](e6c4932f99...a558e3a7a3)

---
updated-dependencies:
- dependency-name: kentaro-m/auto-assign-action
  dependency-version: a558e3a7a389eae6d782d4f5ad0aaa5e36ec2e39
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-24 11:00:13 +08:00
dependabot[bot] d321adf22c
chore(deps): Bump mheap/github-action-required-labels from 5.5.0 to 5.5.1 (#1203)
chore(deps): Bump mheap/github-action-required-labels

Bumps [mheap/github-action-required-labels](https://github.com/mheap/github-action-required-labels) from 5.5.0 to 5.5.1.
- [Release notes](https://github.com/mheap/github-action-required-labels/releases)
- [Commits](388fd6af37...8afbe8ae6a)

---
updated-dependencies:
- dependency-name: mheap/github-action-required-labels
  dependency-version: 5.5.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-24 10:59:58 +08:00
dependabot[bot] b4ec6d533e
chore(deps): Bump taiki-e/cache-cargo-install-action from 2.1.1 to 2.1.2 (#1202)
Bumps [taiki-e/cache-cargo-install-action](https://github.com/taiki-e/cache-cargo-install-action) from 2.1.1 to 2.1.2.
- [Release notes](https://github.com/taiki-e/cache-cargo-install-action/releases)
- [Changelog](https://github.com/taiki-e/cache-cargo-install-action/blob/main/CHANGELOG.md)
- [Commits](4d586f211d...5c9abe9a3f)

---
updated-dependencies:
- dependency-name: taiki-e/cache-cargo-install-action
  dependency-version: 2.1.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-24 10:59:44 +08:00
dependabot[bot] a0a347eda4
chore(deps): Bump wiremock from 0.6.3 to 0.6.4 (#1201)
Bumps [wiremock](https://github.com/LukeMathWalker/wiremock-rs) from 0.6.3 to 0.6.4.
- [Changelog](https://github.com/LukeMathWalker/wiremock-rs/blob/main/CHANGELOG.md)
- [Commits](https://github.com/LukeMathWalker/wiremock-rs/compare/v0.6.3...v0.6.4)

---
updated-dependencies:
- dependency-name: wiremock
  dependency-version: 0.6.4
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-24 10:59:28 +08:00
dependabot[bot] 158569dd24
chore(deps): Bump reqwest-tracing from 0.5.7 to 0.5.8 (#1200)
Bumps [reqwest-tracing](https://github.com/TrueLayer/reqwest-middleware) from 0.5.7 to 0.5.8.
- [Release notes](https://github.com/TrueLayer/reqwest-middleware/releases)
- [Commits](https://github.com/TrueLayer/reqwest-middleware/commits)

---
updated-dependencies:
- dependency-name: reqwest-tracing
  dependency-version: 0.5.8
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-24 10:59:13 +08:00
dependabot[bot] 4337246d09
chore(deps): Bump toml from 0.8.22 to 0.8.23 (#1199)
Bumps [toml](https://github.com/toml-rs/toml) from 0.8.22 to 0.8.23.
- [Commits](https://github.com/toml-rs/toml/compare/toml-v0.8.22...toml-v0.8.23)

---
updated-dependencies:
- dependency-name: toml
  dependency-version: 0.8.23
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-24 10:58:51 +08:00
dependabot[bot] cb6d583225
chore(deps): Bump tabled from 0.19.0 to 0.20.0 (#1198)
Bumps [tabled](https://github.com/zhiburt/tabled) from 0.19.0 to 0.20.0.
- [Changelog](https://github.com/zhiburt/tabled/blob/master/CHANGELOG.md)
- [Commits](https://github.com/zhiburt/tabled/commits)

---
updated-dependencies:
- dependency-name: tabled
  dependency-version: 0.20.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-24 10:58:32 +08:00
Gaius cdc6e30425
feat: add per-piece collection timeout for sync_pieces and sync_persistent_cache_pieces (#1205)
* feat: add per-piece collection timeout for sync_pieces and sync_persistent_cache_pieces

Signed-off-by: Gaius <gaius.qi@gmail.com>

* feat: update

Signed-off-by: Gaius <gaius.qi@gmail.com>

---------

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-24 00:03:24 +08:00
Gaius feceeacfb7
chore(ci/Dockerfile): add tools for base image (#1197)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-23 09:14:33 +00:00
Gaius 2a9ae049af
feat: if the task has started downloading, wait for the first piece to begin downloading (#1196)
For the first sync, if the task has started downloading, wait for the
first piece to begin downloading. This prevents the child from receiving
an empty piece, which would cause disconnection from the parent and
rescheduling. Waiting ensures the child avoids unnecessary rescheduling
and maximizes the chance to download pieces from the parent.

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-20 18:31:47 +08:00
Gaius 40c9e62ebd
feat(dragonfly-client): support http protocol for otel (#1195)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-19 08:54:19 +00:00
Gaius 78505d46fc
feat: remove DISK_WRITTEN_BYTES and DISK_READ_BYTES metrics (#1194)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-18 23:10:23 +08:00
dependabot[bot] 04da438d12
chore(deps): Bump clap from 4.5.39 to 4.5.40 (#1188)
Bumps [clap](https://github.com/clap-rs/clap) from 4.5.39 to 4.5.40.
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/clap_complete-v4.5.39...clap_complete-v4.5.40)

---
updated-dependencies:
- dependency-name: clap
  dependency-version: 4.5.40
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-18 12:35:43 +08:00
dependabot[bot] 60c9717760
chore(deps): Bump github/codeql-action from 3.28.19 to 3.29.0 (#1190)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.19 to 3.29.0.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](fca7ace96b...ce28f5bb42)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.29.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-18 12:35:07 +08:00
Gaius 19e233cc46
feat: add url, content_length and piece_length for tracing span (#1192)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-17 12:58:34 +08:00
dependabot[bot] 8bc771e619
chore(deps): Bump num_cpus from 1.16.0 to 1.17.0 (#1187)
Bumps [num_cpus](https://github.com/seanmonstar/num_cpus) from 1.16.0 to 1.17.0.
- [Release notes](https://github.com/seanmonstar/num_cpus/releases)
- [Changelog](https://github.com/seanmonstar/num_cpus/blob/master/CHANGELOG.md)
- [Commits](https://github.com/seanmonstar/num_cpus/compare/v1.16.0...v1.17.0)

---
updated-dependencies:
- dependency-name: num_cpus
  dependency-version: 1.17.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-16 22:10:57 +08:00
Gaius 8b6758e79e
feat(dragonfly-client-storage): add dist_threshold for disk usage calculation in GC policy (#1191)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-16 14:08:16 +00:00
Gaius 4bee58a863
feat(dragonfly-client): add tracing header for exporter (#1183)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-13 18:42:13 +08:00
dependabot[bot] ee5cf8f642
chore(deps): Bump libloading from 0.8.7 to 0.8.8 (#1174)
Bumps [libloading](https://github.com/nagisa/rust_libloading) from 0.8.7 to 0.8.8.
- [Commits](https://github.com/nagisa/rust_libloading/commits)

---
updated-dependencies:
- dependency-name: libloading
  dependency-version: 0.8.8
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-11 10:46:26 +08:00
dependabot[bot] 317b9e0557
chore(deps): Bump headers from 0.4.0 to 0.4.1 (#1176)
Bumps [headers](https://github.com/hyperium/headers) from 0.4.0 to 0.4.1.
- [Release notes](https://github.com/hyperium/headers/releases)
- [Commits](https://github.com/hyperium/headers/compare/headers-v0.4.0...headers-v0.4.1)

---
updated-dependencies:
- dependency-name: headers
  dependency-version: 0.4.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-11 10:45:59 +08:00
dependabot[bot] d1bfcb4ce9
chore(deps): Bump hyper-util from 0.1.12 to 0.1.14 (#1178)
Bumps [hyper-util](https://github.com/hyperium/hyper-util) from 0.1.12 to 0.1.14.
- [Release notes](https://github.com/hyperium/hyper-util/releases)
- [Changelog](https://github.com/hyperium/hyper-util/blob/master/CHANGELOG.md)
- [Commits](https://github.com/hyperium/hyper-util/compare/v0.1.12...v0.1.14)

---
updated-dependencies:
- dependency-name: hyper-util
  dependency-version: 0.1.14
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-11 10:45:30 +08:00
dependabot[bot] 5823457de9
chore(deps): Bump aquasecurity/trivy-action from 0.30.0 to 0.31.0 (#1179)
Bumps [aquasecurity/trivy-action](https://github.com/aquasecurity/trivy-action) from 0.30.0 to 0.31.0.
- [Release notes](https://github.com/aquasecurity/trivy-action/releases)
- [Commits](6c175e9c40...76071ef0d7)

---
updated-dependencies:
- dependency-name: aquasecurity/trivy-action
  dependency-version: 0.31.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-11 10:45:00 +08:00
dependabot[bot] 9c828e963e
chore(deps): Bump github/codeql-action from 3.28.18 to 3.28.19 (#1180)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.18 to 3.28.19.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](ff0a06e83c...fca7ace96b)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.28.19
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-11 10:44:38 +08:00
dependabot[bot] 2fa7c40f6b
chore(deps): Bump kentaro-m/auto-assign-action from 7ae38e468e64dec0af17820972bc4915aa511ec2 to e6c4932f995626505d2a5d85401b0319753caa11 (#1181)
chore(deps): Bump kentaro-m/auto-assign-action

Bumps [kentaro-m/auto-assign-action](https://github.com/kentaro-m/auto-assign-action) from 7ae38e468e64dec0af17820972bc4915aa511ec2 to e6c4932f995626505d2a5d85401b0319753caa11.
- [Release notes](https://github.com/kentaro-m/auto-assign-action/releases)
- [Commits](7ae38e468e...e6c4932f99)

---
updated-dependencies:
- dependency-name: kentaro-m/auto-assign-action
  dependency-version: e6c4932f995626505d2a5d85401b0319753caa11
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-11 10:44:10 +08:00
Gaius d899d9982f
feat: collects pieces from multiple parents with load balancing strategy (#1173)
* feat: collects pieces from multiple parents with load balancing strategy

Signed-off-by: Gaius <gaius.qi@gmail.com>

* feat: update

Signed-off-by: Gaius <gaius.qi@gmail.com>

* feat: update

Signed-off-by: Gaius <gaius.qi@gmail.com>

* feat: update

Signed-off-by: Gaius <gaius.qi@gmail.com>

---------

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-05 11:19:23 +00:00
Gaius 7f5b517f37
feat: add piece_length for download piece span (#1172)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-04 13:02:58 +00:00
Gaius fe178726e8
feat: remove span in get piece (#1171)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-06-04 20:03:04 +08:00
dependabot[bot] 976fe3ab11
chore(deps): Bump clap from 4.5.38 to 4.5.39 (#1170)
Bumps [clap](https://github.com/clap-rs/clap) from 4.5.38 to 4.5.39.
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/clap_complete-v4.5.38...clap_complete-v4.5.39)

---
updated-dependencies:
- dependency-name: clap
  dependency-version: 4.5.39
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-04 11:20:02 +08:00
dependabot[bot] 4952a60c10
chore(deps): Bump opentelemetry-semantic-conventions from 0.29.0 to 0.30.0 (#1169)
chore(deps): Bump opentelemetry-semantic-conventions

Bumps [opentelemetry-semantic-conventions](https://github.com/open-telemetry/opentelemetry-rust) from 0.29.0 to 0.30.0.
- [Release notes](https://github.com/open-telemetry/opentelemetry-rust/releases)
- [Changelog](https://github.com/open-telemetry/opentelemetry-rust/blob/main/docs/release_0.30.md)
- [Commits](https://github.com/open-telemetry/opentelemetry-rust/compare/opentelemetry-semantic-conventions-0.29.0...opentelemetry-semantic-conventions-0.30.0)

---
updated-dependencies:
- dependency-name: opentelemetry-semantic-conventions
  dependency-version: 0.30.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-04 11:14:43 +08:00
dependabot[bot] eda6ba65cb
chore(deps): Bump pprof from 0.14.0 to 0.15.0 (#1166)
Bumps [pprof](https://github.com/tikv/pprof-rs) from 0.14.0 to 0.15.0.
- [Changelog](https://github.com/tikv/pprof-rs/blob/master/CHANGELOG.md)
- [Commits](https://github.com/tikv/pprof-rs/commits)

---
updated-dependencies:
- dependency-name: pprof
  dependency-version: 0.15.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-06-04 11:13:55 +08:00
Gaius 44d58fee37
feat: rename option log_to_stdout console for dfinit (#1165)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-29 03:30:26 +00:00
Gaius 7819702b67
feat(tracing): align resource attributes with OpenTelemetry semantic conventions (#1164)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-29 00:00:12 +08:00
Gaius 3959bb9330
feat: rename --log-to-stdout to --console (#1163)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-27 15:31:14 +08:00
Gaius 7b1cdbe1f1
feat: add --log-to-stdout for command (#1162)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-27 14:15:56 +08:00
dependabot[bot] de812d2f18
chore(deps): Bump tokio from 1.45.0 to 1.45.1 (#1159)
Bumps [tokio](https://github.com/tokio-rs/tokio) from 1.45.0 to 1.45.1.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-1.45.0...tokio-1.45.1)

---
updated-dependencies:
- dependency-name: tokio
  dependency-version: 1.45.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-26 22:14:29 +08:00
dependabot[bot] b06c210dda
chore(deps): Bump hyper-util from 0.1.10 to 0.1.12 (#1158)
Bumps [hyper-util](https://github.com/hyperium/hyper-util) from 0.1.10 to 0.1.12.
- [Release notes](https://github.com/hyperium/hyper-util/releases)
- [Changelog](https://github.com/hyperium/hyper-util/blob/master/CHANGELOG.md)
- [Commits](https://github.com/hyperium/hyper-util/compare/v0.1.10...v0.1.12)

---
updated-dependencies:
- dependency-name: hyper-util
  dependency-version: 0.1.12
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-26 22:14:01 +08:00
Gaius 57caa35900
feat(tracing): migrate to OpenTelemetry OTLP and enrich trace metadata (#1160)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-26 22:00:31 +08:00
Gaius 365f099b16
chore(README.md): add maintainer google groups for communication channels and remove discussion group (#1157)
* chore(README.md): add maintainer google groups for communication channels and remove discussion group

Signed-off-by: Gaius <gaius.qi@gmail.com>

* Update README.md

---------

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-23 16:04:43 +08:00
dependabot[bot] 1d63a078f0
chore(deps): Bump github/codeql-action from 3.28.17 to 3.28.18 (#1152)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.17 to 3.28.18.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](60168efe1c...ff0a06e83c)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.28.18
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-20 10:51:00 +08:00
dependabot[bot] e963bd411f
chore(deps): Bump rustls-pki-types from 1.11.0 to 1.12.0 (#1153)
Bumps [rustls-pki-types](https://github.com/rustls/pki-types) from 1.11.0 to 1.12.0.
- [Release notes](https://github.com/rustls/pki-types/releases)
- [Commits](https://github.com/rustls/pki-types/compare/v/1.11.0...v/1.12.0)

---
updated-dependencies:
- dependency-name: rustls-pki-types
  dependency-version: 1.12.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-20 10:50:40 +08:00
dependabot[bot] 683930fbcc
chore(deps): Bump tokio from 1.44.2 to 1.45.0 (#1154)
Bumps [tokio](https://github.com/tokio-rs/tokio) from 1.44.2 to 1.45.0.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-1.44.2...tokio-1.45.0)

---
updated-dependencies:
- dependency-name: tokio
  dependency-version: 1.45.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-20 10:50:20 +08:00
dependabot[bot] b8f69fbffa
chore(deps): Bump tempfile from 3.19.1 to 3.20.0 (#1156)
Bumps [tempfile](https://github.com/Stebalien/tempfile) from 3.19.1 to 3.20.0.
- [Changelog](https://github.com/Stebalien/tempfile/blob/master/CHANGELOG.md)
- [Commits](https://github.com/Stebalien/tempfile/compare/v3.19.1...v3.20.0)

---
updated-dependencies:
- dependency-name: tempfile
  dependency-version: 3.20.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-20 10:49:59 +08:00
Gaius 3811569f29
feat: add hardlink when task is downloaded (#1151)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-19 18:33:59 +08:00
Gaius 44362c6a00
feat: increase GRPC REQUEST_TIMEOUT to 15 seconds and add comment (#1150)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-19 11:49:36 +08:00
Gaius 604a9451da
feat: add task type to ID generation for Standard and PersistentCache tasks (#1149)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-14 09:38:31 +00:00
Gaius 086bc6d226
feat: move hard link creation after download_task_started in task.rs
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-13 23:43:51 +08:00
Gaius f8ae582fa3
feat(dragonfly-client): replace inspect_err with unwrap_or_else for error handling in piece download requests (#1148)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-13 23:24:14 +08:00
Gaius c11f533637
feat(dragonfly-client): add logs for finished piece from local (#1147)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-12 23:47:02 +08:00
dependabot[bot] 5bc3a0a6dd
chore(deps): Bump chrono from 0.4.40 to 0.4.41 (#1144)
Bumps [chrono](https://github.com/chronotope/chrono) from 0.4.40 to 0.4.41.
- [Release notes](https://github.com/chronotope/chrono/releases)
- [Changelog](https://github.com/chronotope/chrono/blob/main/CHANGELOG.md)
- [Commits](https://github.com/chronotope/chrono/compare/v0.4.40...v0.4.41)

---
updated-dependencies:
- dependency-name: chrono
  dependency-version: 0.4.41
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-12 20:54:46 +08:00
dependabot[bot] 3ce1e2ca42
chore(deps): Bump libloading from 0.8.6 to 0.8.7 (#1143)
Bumps [libloading](https://github.com/nagisa/rust_libloading) from 0.8.6 to 0.8.7.
- [Commits](https://github.com/nagisa/rust_libloading/compare/0.8.6...0.8.7)

---
updated-dependencies:
- dependency-name: libloading
  dependency-version: 0.8.7
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-12 20:54:27 +08:00
dependabot[bot] 88b27ea0bc
chore(deps): Bump thiserror from 1.0.69 to 2.0.12 (#1142)
Bumps [thiserror](https://github.com/dtolnay/thiserror) from 1.0.69 to 2.0.12.
- [Release notes](https://github.com/dtolnay/thiserror/releases)
- [Commits](https://github.com/dtolnay/thiserror/compare/1.0.69...2.0.12)

---
updated-dependencies:
- dependency-name: thiserror
  dependency-version: 2.0.12
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-12 20:54:06 +08:00
dependabot[bot] 414cdc6336
chore(deps): Bump clap from 4.5.37 to 4.5.38 (#1140)
Bumps [clap](https://github.com/clap-rs/clap) from 4.5.37 to 4.5.38.
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/clap_complete-v4.5.37...clap_complete-v4.5.38)

---
updated-dependencies:
- dependency-name: clap
  dependency-version: 4.5.38
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-12 20:53:38 +08:00
Gaius f2315f2804
feat(dragonfly-client-storage): add write_piece_timeout to storage config and enhance piece download timeout handling (#1146)
feat(dragonfly-client-storage): add write_piece_timeout to storage config and enhance piece download timeout handling

This change introduces a `write_piece_timeout` configuration to the storage module, allowing customizable timeouts for writing pieces to storage (e.g., disk or cache). The default timeout is set to 90 seconds. The total timeout for piece operations now combines `download.piece was timeout` and `storage.write_piece_timeout`. Key changes include:

- Added `write_piece_timeout` field to `Storage` config with default and serialization support.
- Updated `wait_for_piece_finished` and `wait_for_piece_finished_by_task_id` to use combined timeout (`download.piece_timeout + storage.write_piece_timeout`).
- Introduced `DownloadPieceFinished` error for piece download timeouts and refactored `download_piece_from_source_finished` and `download_piece_from_parent_finished` to enforce timeouts using `tokio::select!`.
- Increased default `download.piece_timeout` from 60s to 120s for robustness.
- Removed `wait_for_piece_count` debug logging to simplify code.
- Updated tests to validate `write_piece_timeout` parsing.

These changes improve timeout granularity and reliability for piece operations.

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-12 20:52:21 +08:00
Gaius 8c6f9771c9
fix: scheduling and piece collection logic for task abortion (#1139)
Modify scheduling logic in persistent_cache_task.rs and task.rs to use strict inequality (>) for max_schedule_count comparison,
ensuring tasks abort only after exceeding the configured limit. Correct piece collection logic in piece_collector.rs to abort
tasks when collected_pieces is empty, fixing the condition for both PieceCollector and PersistentCachePieceCollector to align with intended behavior.

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-09 19:23:42 +08:00
Gaius 23efe2cb04
feat(dragonfly-client-config): change the default value oft the schedule_timeout (#1138)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-05-08 22:49:50 +08:00
dependabot[bot] 7e46bff143
chore(deps): Bump prost-wkt-types from 0.6.0 to 0.6.1 (#1135)
Bumps [prost-wkt-types](https://github.com/fdeantoni/prost-wkt) from 0.6.0 to 0.6.1.
- [Release notes](https://github.com/fdeantoni/prost-wkt/releases)
- [Changelog](https://github.com/fdeantoni/prost-wkt/blob/master/CHANGELOG.md)
- [Commits](https://github.com/fdeantoni/prost-wkt/compare/v0.6.0...v0.6.1)

---
updated-dependencies:
- dependency-name: prost-wkt-types
  dependency-version: 0.6.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-07 10:30:20 +08:00
dependabot[bot] b2c1f26ce9
chore(deps): Bump local-ip-address from 0.6.3 to 0.6.5 (#1134)
Bumps [local-ip-address](https://github.com/LeoBorai/local-ip-address) from 0.6.3 to 0.6.5.
- [Release notes](https://github.com/LeoBorai/local-ip-address/releases)
- [Changelog](https://github.com/LeoBorai/local-ip-address/blob/main/CHANGELOG.md)
- [Commits](https://github.com/LeoBorai/local-ip-address/commits/v0.6.5)

---
updated-dependencies:
- dependency-name: local-ip-address
  dependency-version: 0.6.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-07 10:29:29 +08:00
dependabot[bot] 17e403c3dc
chore(deps): Bump toml from 0.8.21 to 0.8.22 (#1133)
Bumps [toml](https://github.com/toml-rs/toml) from 0.8.21 to 0.8.22.
- [Commits](https://github.com/toml-rs/toml/compare/toml-v0.8.21...toml-v0.8.22)

---
updated-dependencies:
- dependency-name: toml
  dependency-version: 0.8.22
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-07 10:28:37 +08:00
dependabot[bot] e96000b379
chore(deps): Bump github/codeql-action from 3.28.16 to 3.28.17 (#1137)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.16 to 3.28.17.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](28deaeda66...60168efe1c)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.28.17
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-05-06 10:47:28 +08:00
Gaius 233fcdf3a1
chore(.github/workflows): change rules of the stale actions (#1131)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-30 12:34:15 +08:00
yxxhero 5d3b05372b
comments(container_runtime): remove unused containerd config override (#1130)
Signed-off-by: yxxhero <aiopsclub@163.com>
2025-04-30 12:03:08 +08:00
Gaius f3fd5f46c4
feat(dragonfly-client-backend): change retry times for backend (#1129)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-29 16:39:28 +08:00
Gaius a15d556f95
feat(dragonfly-client): change MAX_PIECE_LENGTH to 64MiB (#1128)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-29 04:02:16 +00:00
Gaius a2ba39026e
feat: support fallocate for creating task (#1119)
* feat: support fallocate and filling zero for creating task

* feat: support fallocate and filling zero for creating task

---------

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-29 11:43:42 +08:00
dependabot[bot] f60bad399b
chore(deps): Bump toml_edit from 0.22.24 to 0.22.25 (#1127)
Bumps [toml_edit](https://github.com/toml-rs/toml) from 0.22.24 to 0.22.25.
- [Commits](https://github.com/toml-rs/toml/compare/v0.22.24...v0.22.25)

---
updated-dependencies:
- dependency-name: toml_edit
  dependency-version: 0.22.25
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-29 11:42:08 +08:00
dependabot[bot] 1600f0d591
chore(deps): Bump tokio-util from 0.7.14 to 0.7.15 (#1125)
Bumps [tokio-util](https://github.com/tokio-rs/tokio) from 0.7.14 to 0.7.15.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-util-0.7.14...tokio-util-0.7.15)

---
updated-dependencies:
- dependency-name: tokio-util
  dependency-version: 0.7.15
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-29 11:37:34 +08:00
dependabot[bot] ff0cce2fc8
chore(deps): Bump tabled from 0.18.0 to 0.19.0 (#1123)
Bumps [tabled](https://github.com/zhiburt/tabled) from 0.18.0 to 0.19.0.
- [Changelog](https://github.com/zhiburt/tabled/blob/master/CHANGELOG.md)
- [Commits](https://github.com/zhiburt/tabled/commits)

---
updated-dependencies:
- dependency-name: tabled
  dependency-version: 0.19.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-29 11:37:15 +08:00
dependabot[bot] 87da08ab39
chore(deps): Bump actions/stale from 9.0.0 to 9.1.0 (#1122)
Bumps [actions/stale](https://github.com/actions/stale) from 9.0.0 to 9.1.0.
- [Release notes](https://github.com/actions/stale/releases)
- [Changelog](https://github.com/actions/stale/blob/main/CHANGELOG.md)
- [Commits](28ca103628...5bef64f19d)

---
updated-dependencies:
- dependency-name: actions/stale
  dependency-version: 9.1.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-29 11:36:47 +08:00
dependabot[bot] 494b8cd95a
chore(deps): Bump github/codeql-action from 3.28.15 to 3.28.16 (#1121)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.15 to 3.28.16.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](45775bd823...28deaeda66)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.28.16
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-29 11:36:27 +08:00
dependabot[bot] 620c8fa6a8
chore(deps): Bump kentaro-m/auto-assign-action from 248761c4feb3917c1b0444e33fad1a50093b9847 to 7ae38e468e64dec0af17820972bc4915aa511ec2 (#1120)
chore(deps): Bump kentaro-m/auto-assign-action

Bumps [kentaro-m/auto-assign-action](https://github.com/kentaro-m/auto-assign-action) from 248761c4feb3917c1b0444e33fad1a50093b9847 to 7ae38e468e64dec0af17820972bc4915aa511ec2.
- [Release notes](https://github.com/kentaro-m/auto-assign-action/releases)
- [Commits](248761c4fe...7ae38e468e)

---
updated-dependencies:
- dependency-name: kentaro-m/auto-assign-action
  dependency-version: 7ae38e468e64dec0af17820972bc4915aa511ec2
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-29 11:36:04 +08:00
dependabot[bot] 1c059171db
chore(deps): Bump toml from 0.8.20 to 0.8.21 (#1124)
Bumps [toml](https://github.com/toml-rs/toml) from 0.8.20 to 0.8.21.
- [Commits](https://github.com/toml-rs/toml/compare/toml-v0.8.20...toml-v0.8.21)

---
updated-dependencies:
- dependency-name: toml
  dependency-version: 0.8.21
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-29 11:34:34 +08:00
yxxhero cfa7dba465
refactor(ci/Dockerfile.dfinit, dragonfly-client/src/dynconfig/mod.rs): format and optimize code (#1117)
Signed-off-by: yxxhero <aiopsclub@163.com>
2025-04-27 10:13:17 +08:00
Gaius 9f6cecacd4
chore(.github/workflows): add stale action (#1113)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-25 18:41:10 +08:00
Gaius 8aec90c152
chore(.github/workflows): add auto assign action (#1114)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-25 18:41:02 +08:00
Gaius 900ab7abcc
chore(.github/workflows): add pr labels action (#1115)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-25 18:40:44 +08:00
Gaius 7950aa5ab3
feat: handle Interrupted error for io (#1112)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-25 10:48:30 +08:00
Gaius c21159037a
feat(dragonfly-client): add support for content-based task ID generation in Dragonfly client (#1111)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-24 20:54:46 +08:00
Gaius e2c7d9000a
feat(dragonfly-client): remove dfstore command (#1109)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-24 10:33:49 +08:00
Gaius 63ceb47d82
feat(dragonfly-client-storage): Change error log to info for cache task deletion failure (#1108)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-22 13:29:10 +00:00
KennyMcCormick 75c0c1f5a0
chroe: fix typo in ut (#1106)
Signed-off-by: cormick <cormick1080@gmail.com>
2025-04-22 04:34:21 +00:00
dependabot[bot] 37b93913cc
chore(deps): Bump clap from 4.5.35 to 4.5.37 (#1104)
Bumps [clap](https://github.com/clap-rs/clap) from 4.5.35 to 4.5.37.
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/clap_complete-v4.5.35...clap_complete-v4.5.37)

---
updated-dependencies:
- dependency-name: clap
  dependency-version: 4.5.37
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-22 04:28:21 +00:00
dependabot[bot] 7b1a1f197e
chore(deps): Bump taiki-e/cache-cargo-install-action from 2.1.0 to 2.1.1 (#1103)
Bumps [taiki-e/cache-cargo-install-action](https://github.com/taiki-e/cache-cargo-install-action) from 2.1.0 to 2.1.1.
- [Release notes](https://github.com/taiki-e/cache-cargo-install-action/releases)
- [Changelog](https://github.com/taiki-e/cache-cargo-install-action/blob/main/CHANGELOG.md)
- [Commits](44857e0ff6...4d586f211d)

---
updated-dependencies:
- dependency-name: taiki-e/cache-cargo-install-action
  dependency-version: 2.1.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-22 12:25:03 +08:00
KennyMcCormick 657d8867bf
test: add ut for resource.task (#1101)
Signed-off-by: cormick <cormick1080@gmail.com>
2025-04-18 10:25:16 +00:00
Gaius 2a0ef8ec19
fix: solve the memory leak caused by rayon (#1102)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-18 09:49:05 +00:00
KennyMcCormick f3b1b67607
test: add ut for shutdown (#1100)
Signed-off-by: cormick <cormick1080@gmail.com>
2025-04-18 09:55:00 +08:00
KennyMcCormick 938d17c0cf
chore: opt code under persistent_cache_task (#1098)
chroe: opt code under persistent_cache_task

Signed-off-by: cormick <cormick1080@gmail.com>
2025-04-16 13:02:47 +00:00
KennyMcCormick ad335784fe
fix: modify ut logic which is incorrect under macOS (#1099)
Signed-off-by: cormick <cormick1080@gmail.com>
2025-04-16 20:53:27 +08:00
Gaius cfab09f4c0 chore: pin cargo-deb dependency version 2025-04-15 05:11:19 -04:00
Gaius 3b3fbf7abb
fix: cargo deb building
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-15 15:24:38 +08:00
Gaius 5d2b719626
chore(dragonfly-client): fix building for deb package
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-15 15:03:06 +08:00
dependabot[bot] 57078c5d03
chore(deps): Bump reqwest-middleware from 0.4.1 to 0.4.2 (#1092)
Bumps [reqwest-middleware](https://github.com/TrueLayer/reqwest-middleware) from 0.4.1 to 0.4.2.
- [Release notes](https://github.com/TrueLayer/reqwest-middleware/releases)
- [Commits](https://github.com/TrueLayer/reqwest-middleware/commits/reqwest-middleware-v0.4.2)

---
updated-dependencies:
- dependency-name: reqwest-middleware
  dependency-version: 0.4.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-14 21:05:15 +08:00
dependabot[bot] ba7940dc6f
chore(deps): Bump tokio-util from 0.7.13 to 0.7.14 (#1096)
Bumps [tokio-util](https://github.com/tokio-rs/tokio) from 0.7.13 to 0.7.14.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-util-0.7.13...tokio-util-0.7.14)

---
updated-dependencies:
- dependency-name: tokio-util
  dependency-version: 0.7.14
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-14 21:04:51 +08:00
dependabot[bot] c7bbc522c0
chore(deps): Bump reqwest-tracing from 0.5.6 to 0.5.7 (#1095)
Bumps [reqwest-tracing](https://github.com/TrueLayer/reqwest-middleware) from 0.5.6 to 0.5.7.
- [Release notes](https://github.com/TrueLayer/reqwest-middleware/releases)
- [Commits](https://github.com/TrueLayer/reqwest-middleware/commits/reqwest-tracing-v0.5.7)

---
updated-dependencies:
- dependency-name: reqwest-tracing
  dependency-version: 0.5.7
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-14 21:04:30 +08:00
dependabot[bot] 7cf5a17a90
chore(deps): Bump github/codeql-action from 3.28.14 to 3.28.15 (#1097)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.14 to 3.28.15.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](fc7e4a0fa0...45775bd823)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.28.15
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-14 21:04:12 +08:00
dependabot[bot] aa96b9801b
chore(deps): Bump anyhow from 1.0.97 to 1.0.98 (#1093)
Bumps [anyhow](https://github.com/dtolnay/anyhow) from 1.0.97 to 1.0.98.
- [Release notes](https://github.com/dtolnay/anyhow/releases)
- [Commits](https://github.com/dtolnay/anyhow/compare/1.0.97...1.0.98)

---
updated-dependencies:
- dependency-name: anyhow
  dependency-version: 1.0.98
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-14 20:59:01 +08:00
Gaius 3299a2aa20
refactor: add network interface utilities and enhance upload rate limiting (#1091)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-14 16:57:45 +08:00
baowj a0c3494d7a
feat: add DfdaemonUpload.sync_host. (#929)
Signed-off-by: baowj <bwj_678@qq.com>
2025-04-14 13:27:18 +08:00
Zhaoxinxin 4f6417c221
Feat client config add test (#1086)
* feat: client config add test

Signed-off-by: zhaoxinxin <1186037180@qq.com>

* feat: client config add test

Signed-off-by: zhaoxinxin <1186037180@qq.com>

* feat: client config add test

Signed-off-by: zhaoxinxin <1186037180@qq.com>

* feat: client config add test

Signed-off-by: zhaoxinxin <1186037180@qq.com>

* feat: client config add test

Signed-off-by: zhaoxinxin <1186037180@qq.com>

* feat: client config add test

Signed-off-by: zhaoxinxin <1186037180@qq.com>

---------

Signed-off-by: zhaoxinxin <1186037180@qq.com>
2025-04-12 15:55:50 +08:00
Gaius 3abe3654ef
chore: update verison to 0.2.23 (#1090)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-11 17:22:41 +08:00
Gaius 8631030c97
test(dragonfly-client-storage/benches): remove 1u64 when create cache (#1089)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-11 16:27:04 +08:00
dependabot[bot] fc47cb3855
chore(deps): Bump crossbeam-channel from 0.5.11 to 0.5.15 (#1088)
Bumps [crossbeam-channel](https://github.com/crossbeam-rs/crossbeam) from 0.5.11 to 0.5.15.
- [Release notes](https://github.com/crossbeam-rs/crossbeam/releases)
- [Changelog](https://github.com/crossbeam-rs/crossbeam/blob/master/CHANGELOG.md)
- [Commits](https://github.com/crossbeam-rs/crossbeam/compare/crossbeam-channel-0.5.11...crossbeam-channel-0.5.15)

---
updated-dependencies:
- dependency-name: crossbeam-channel
  dependency-version: 0.5.15
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-11 14:51:28 +08:00
Gaius f1a9f0af55
feat: remove unnecessary dependencies (#1085)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-10 17:03:04 +08:00
Gaius 2e08f6622a
refactor(dragonfly-client-storage): lru cache support for piece download/upload (#1084)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-09 23:19:46 +08:00
SouthWest7 9c3019f88b
feat: add cache support for piece download/upload. (#1074)
Signed-off-by: southwest miao <1403572259@qq.com>
2025-04-09 17:16:19 +08:00
dependabot[bot] 2b414711a4
chore(deps): Bump clap from 4.5.34 to 4.5.35 (#1078)
Bumps [clap](https://github.com/clap-rs/clap) from 4.5.34 to 4.5.35.
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/clap_complete-v4.5.34...clap_complete-v4.5.35)

---
updated-dependencies:
- dependency-name: clap
  dependency-version: 4.5.35
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-08 14:10:22 +08:00
dependabot[bot] 11a426ef2f
chore(deps): Bump tokio from 1.44.1 to 1.44.2 (#1082)
Bumps [tokio](https://github.com/tokio-rs/tokio) from 1.44.1 to 1.44.2.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-1.44.1...tokio-1.44.2)

---
updated-dependencies:
- dependency-name: tokio
  dependency-version: 1.44.2
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-08 10:59:25 +08:00
dependabot[bot] 3351dd3362
chore(deps): Bump github/codeql-action from 3.28.13 to 3.28.14 (#1081)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.13 to 3.28.14.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](1b549b9259...fc7e4a0fa0)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-version: 3.28.14
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-08 10:59:12 +08:00
dependabot[bot] bfb18e8997
chore(deps): Bump blake3 from 1.8.0 to 1.8.1 (#1080)
Bumps [blake3](https://github.com/BLAKE3-team/BLAKE3) from 1.8.0 to 1.8.1.
- [Release notes](https://github.com/BLAKE3-team/BLAKE3/releases)
- [Commits](https://github.com/BLAKE3-team/BLAKE3/compare/1.8.0...1.8.1)

---
updated-dependencies:
- dependency-name: blake3
  dependency-version: 1.8.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-08 10:58:56 +08:00
dependabot[bot] 64369470f4
chore(deps): Bump termion from 4.0.4 to 4.0.5 (#1079)
Bumps termion from 4.0.4 to 4.0.5.

---
updated-dependencies:
- dependency-name: termion
  dependency-version: 4.0.5
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-08 10:57:52 +08:00
dependabot[bot] 74fe905dc3
chore(deps): Bump openssl from 0.10.71 to 0.10.72 (#1075) 2025-04-05 07:18:37 +08:00
Gaius ccddebc7a6
feat(dragonfly-client/src/proxy): add X-Dragonfly-Force-Hard-Link header for proxy (#1073)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-04-03 12:05:24 +08:00
dependabot[bot] d1b3b9d791
chore(deps): Bump github/codeql-action from 3.28.12 to 3.28.13 (#1072)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.12 to 3.28.13.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](5f8171a638...1b549b9259)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-31 21:20:10 +08:00
dependabot[bot] 7d23837943
chore(deps): Bump clap from 4.5.31 to 4.5.34 (#1070)
Bumps [clap](https://github.com/clap-rs/clap) from 4.5.31 to 4.5.34.
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/v4.5.31...clap_complete-v4.5.34)

---
updated-dependencies:
- dependency-name: clap
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-31 21:18:58 +08:00
dependabot[bot] 531ade4a3c
chore(deps): Bump bytesize from 1.3.2 to 1.3.3 (#1069)
Bumps [bytesize](https://github.com/bytesize-rs/bytesize) from 1.3.2 to 1.3.3.
- [Release notes](https://github.com/bytesize-rs/bytesize/releases)
- [Changelog](https://github.com/bytesize-rs/bytesize/blob/master/CHANGELOG.md)
- [Commits](https://github.com/bytesize-rs/bytesize/compare/v1.3.2...v1.3.3)

---
updated-dependencies:
- dependency-name: bytesize
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-31 21:18:37 +08:00
dependabot[bot] 6d5515de81
chore(deps): Bump blake3 from 1.7.0 to 1.8.0 (#1067)
Bumps [blake3](https://github.com/BLAKE3-team/BLAKE3) from 1.7.0 to 1.8.0.
- [Release notes](https://github.com/BLAKE3-team/BLAKE3/releases)
- [Commits](https://github.com/BLAKE3-team/BLAKE3/compare/1.7.0...1.8.0)

---
updated-dependencies:
- dependency-name: blake3
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-31 21:18:09 +08:00
Gaius ddde4da033
feat(dragonfly-client/src/proxy): add port parameter to HTTPS proxy handler (#1066)
- Modified `https_handler` to extract port from URI, defaulting to 443 if not present
- Updated `upgraded_tunnel` and `upgraded_handler` to accept port parameter
- Adjusted URI construction in `upgraded_handler` to include port in HTTPS format

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-31 18:33:57 +08:00
Gaius 83bbf1973a
test(dragonfly-client-storage): add unit tests for content and fix doctest syntax (#1065)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-31 11:52:03 +08:00
Gaius 250f083980
feat: optimize hard link and copy logic (#1064)
* feat: optimize hard link and copy logic

Refer to https://github.com/dragonflyoss/design/blob/main/systems-analysis/file-download-workflow-with-hard-link/README.md

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-28 18:29:20 +08:00
Gaius 501a4cdf66
chore(ci/Dockerfile): add benches for dragonfly-client-storage
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-27 22:48:54 +08:00
Gaius ce4b748c19
feat: remove repeat piece from piece collector (#1062)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-27 22:37:43 +08:00
Gaius 4f3e2f6e4f
feat: optimize benches for cache and lru cache (#1063)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-27 22:16:22 +08:00
SouthWest7 47d2222900
test: add benchmarks for dragonfly-client-storage/cache (#1057)
Signed-off-by: southwest miao <1403572259@qq.com>
2025-03-27 18:24:49 +08:00
Gaius 216ac0937a
feat(dragonfly-client): reducing lock contention and improving concurrency (#1061)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-27 14:27:44 +08:00
Gaius ac7f2d7388
feat(dragonfly-client/src/resource): reuse grpc client in piece downloader (#1060)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-27 10:26:09 +08:00
Gaius 991855ef21
feat(dragonfly-client/src/grpc): support exchange_ib_verbs_queue_pair_endpoint api (#1059)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-25 21:03:32 +08:00
dependabot[bot] d5ddb0eee0
chore(deps): Bump blake3 from 1.6.0 to 1.7.0 (#1056)
Bumps [blake3](https://github.com/BLAKE3-team/BLAKE3) from 1.6.0 to 1.7.0.
- [Release notes](https://github.com/BLAKE3-team/BLAKE3/releases)
- [Commits](https://github.com/BLAKE3-team/BLAKE3/compare/1.6.0...1.7.0)

---
updated-dependencies:
- dependency-name: blake3
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-25 12:19:02 +08:00
dependabot[bot] 139d106716
chore(deps): Bump bytes from 1.10.0 to 1.10.1 (#1055)
Bumps [bytes](https://github.com/tokio-rs/bytes) from 1.10.0 to 1.10.1.
- [Release notes](https://github.com/tokio-rs/bytes/releases)
- [Changelog](https://github.com/tokio-rs/bytes/blob/master/CHANGELOG.md)
- [Commits](https://github.com/tokio-rs/bytes/compare/v1.10.0...v1.10.1)

---
updated-dependencies:
- dependency-name: bytes
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-25 12:18:48 +08:00
dependabot[bot] 3f5def40c3
chore(deps): Bump http-body-util from 0.1.2 to 0.1.3 (#1054)
Bumps [http-body-util](https://github.com/hyperium/http-body) from 0.1.2 to 0.1.3.
- [Release notes](https://github.com/hyperium/http-body/releases)
- [Commits](https://github.com/hyperium/http-body/compare/http-body-util-v0.1.2...http-body-util-v0.1.3)

---
updated-dependencies:
- dependency-name: http-body-util
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-25 12:18:35 +08:00
dependabot[bot] 94ce2f7334
chore(deps): Bump tempfile from 3.18.0 to 3.19.1 (#1053)
Bumps [tempfile](https://github.com/Stebalien/tempfile) from 3.18.0 to 3.19.1.
- [Changelog](https://github.com/Stebalien/tempfile/blob/master/CHANGELOG.md)
- [Commits](https://github.com/Stebalien/tempfile/compare/v3.18.0...v3.19.1)

---
updated-dependencies:
- dependency-name: tempfile
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-25 12:18:20 +08:00
dependabot[bot] 44e4ad63cd
chore(deps): Bump github/codeql-action from 3.28.11 to 3.28.12 (#1051)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.11 to 3.28.12.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](6bb031afdd...5f8171a638)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-25 12:17:29 +08:00
dependabot[bot] a7f006fb00
chore(deps): Bump http from 1.2.0 to 1.3.1 (#1052)
Bumps [http](https://github.com/hyperium/http) from 1.2.0 to 1.3.1.
- [Release notes](https://github.com/hyperium/http/releases)
- [Changelog](https://github.com/hyperium/http/blob/master/CHANGELOG.md)
- [Commits](https://github.com/hyperium/http/compare/v1.2.0...v1.3.1)

---
updated-dependencies:
- dependency-name: http
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-24 20:58:04 +08:00
Gaius 2e46546bb6
feat(dragonfly-client-storage): optimize lru cache
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-24 13:56:49 +08:00
SouthWest7 3111106f4a
test(dragonfly-client-storage): add unit tests for `lru_cache` and `cache` module. (#1050)
Signed-off-by: Gaius <gaius.qi@gmail.com>
Signed-off-by: southwest miao <1403572259@qq.com>
Co-authored-by: Gaius <gaius.qi@gmail.com>
2025-03-24 13:40:06 +08:00
dependabot[bot] b9b784289b
chore(deps): Bump wiremock from 0.6.2 to 0.6.3 (#1044)
Bumps [wiremock](https://github.com/LukeMathWalker/wiremock-rs) from 0.6.2 to 0.6.3.
- [Changelog](https://github.com/LukeMathWalker/wiremock-rs/blob/main/CHANGELOG.md)
- [Commits](https://github.com/LukeMathWalker/wiremock-rs/compare/v0.6.2...v0.6.3)

---
updated-dependencies:
- dependency-name: wiremock
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-18 11:47:37 +08:00
dependabot[bot] 31a7afeb41
chore(deps): Bump serde from 1.0.218 to 1.0.219 (#1045)
Bumps [serde](https://github.com/serde-rs/serde) from 1.0.218 to 1.0.219.
- [Release notes](https://github.com/serde-rs/serde/releases)
- [Commits](https://github.com/serde-rs/serde/compare/v1.0.218...v1.0.219)

---
updated-dependencies:
- dependency-name: serde
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-18 11:47:11 +08:00
dependabot[bot] de676ee5bf
chore(deps): Bump uuid from 1.15.1 to 1.16.0 (#1046)
Bumps [uuid](https://github.com/uuid-rs/uuid) from 1.15.1 to 1.16.0.
- [Release notes](https://github.com/uuid-rs/uuid/releases)
- [Commits](https://github.com/uuid-rs/uuid/compare/v1.15.1...v1.16.0)

---
updated-dependencies:
- dependency-name: uuid
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-18 11:46:51 +08:00
dependabot[bot] 667d6667c6
chore(deps): Bump aquasecurity/trivy-action from 0.29.0 to 0.30.0 (#1049)
Bumps [aquasecurity/trivy-action](https://github.com/aquasecurity/trivy-action) from 0.29.0 to 0.30.0.
- [Release notes](https://github.com/aquasecurity/trivy-action/releases)
- [Commits](18f2510ee3...6c175e9c40)

---
updated-dependencies:
- dependency-name: aquasecurity/trivy-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-18 11:44:09 +08:00
Gaius 8e79b1b72f
chore(ci/Dockerfile): add infiniband-diags and libibverbs-utils for container (#1043)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-17 14:10:59 +08:00
Gaius fcea789ec8
feat: remove proxy cache (#1042)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-14 19:07:51 +08:00
Gaius e5b8dcb383
refactor: optimize storage cache (#1041)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-14 18:49:37 +08:00
Gaius 70eca028cd
refactor(dragonfly-client-storage): optimize lru_cache for storage (#1040)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-14 15:49:56 +08:00
SouthWest7 9ca89909f3
feat: add a LRU cache module for preheat jobs (#945)
Signed-off-by: southwest miao <1403572259@qq.com>
2025-03-14 13:47:53 +08:00
Gaius beff3f87b3
chore(ci/Dockerfile): add tools for image (#1039)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-13 19:43:47 +08:00
Gaius 6bc3fbbd48
fix(dragonfly-client-storage): improve hard link handling in content storage (#1038)
- Bumped version from 0.2.18 to 0.2.19 across all packages
- Modified hard link logic in content.rs to:
  - Move existence check after initial hard link attempt
  - Handle AlreadyExists error case more gracefully
  - Apply consistent behavior for both regular and persistent cache tasks
- Maintains original functionality while improving error handling flow

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-13 17:51:15 +08:00
Gaius 316cb541dd
chore: update version to 0.2.18
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-13 11:45:18 +08:00
Gaius 21a2065d7b
chore(ci/Dockerfile): add bottom for client image (#1037)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-13 11:41:32 +08:00
Gaius a75988717c
test(dragonfly-client-storage): optimize engine tests (#1036)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-11 22:37:21 +08:00
Chongzhi Deng e34d07f419
test: add unit tests for RocksdbStorageEngine (#1035)
Signed-off-by: BruceAko <chongzhi@hust.edu.cn>
2025-03-11 20:31:43 +08:00
Gaius 79a0aa25c8
chore: change the registry origin from dockerhub to ecr (#1034)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-11 16:37:32 +08:00
dependabot[bot] 604a0d079b
chore(deps): Bump github/codeql-action from 3.28.10 to 3.28.11 (#1033)
Bumps [github/codeql-action](https://github.com/github/codeql-action) from 3.28.10 to 3.28.11.
- [Release notes](https://github.com/github/codeql-action/releases)
- [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
- [Commits](b56ba49b26...6bb031afdd)

---
updated-dependencies:
- dependency-name: github/codeql-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-11 11:05:37 +08:00
dependabot[bot] 0db01708fe
chore(deps): Bump tempfile from 3.17.1 to 3.18.0 (#1032)
Bumps [tempfile](https://github.com/Stebalien/tempfile) from 3.17.1 to 3.18.0.
- [Changelog](https://github.com/Stebalien/tempfile/blob/master/CHANGELOG.md)
- [Commits](https://github.com/Stebalien/tempfile/compare/v3.17.1...v3.18.0)

---
updated-dependencies:
- dependency-name: tempfile
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-11 11:05:09 +08:00
dependabot[bot] c3784719dc
chore(deps): Bump uuid from 1.14.0 to 1.15.1 (#1031)
Bumps [uuid](https://github.com/uuid-rs/uuid) from 1.14.0 to 1.15.1.
- [Release notes](https://github.com/uuid-rs/uuid/releases)
- [Commits](https://github.com/uuid-rs/uuid/compare/v1.14.0...v1.15.1)

---
updated-dependencies:
- dependency-name: uuid
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-11 11:04:25 +08:00
dependabot[bot] b469ce9a74
chore(deps): Bump tokio from 1.43.0 to 1.44.0 (#1029)
Bumps [tokio](https://github.com/tokio-rs/tokio) from 1.43.0 to 1.44.0.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](https://github.com/tokio-rs/tokio/compare/tokio-1.43.0...tokio-1.44.0)

---
updated-dependencies:
- dependency-name: tokio
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-11 11:04:06 +08:00
dependabot[bot] a9f85fa5e7
chore(deps): Bump clap from 4.5.30 to 4.5.31 (#1028)
Bumps [clap](https://github.com/clap-rs/clap) from 4.5.30 to 4.5.31.
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](https://github.com/clap-rs/clap/compare/clap_complete-v4.5.30...v4.5.31)

---
updated-dependencies:
- dependency-name: clap
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-11 11:03:29 +08:00
dependabot[bot] 1d821bf636
chore(deps): Bump reqwest-middleware from 0.4.0 to 0.4.1 (#1030)
Bumps [reqwest-middleware](https://github.com/TrueLayer/reqwest-middleware) from 0.4.0 to 0.4.1.
- [Release notes](https://github.com/TrueLayer/reqwest-middleware/releases)
- [Commits](https://github.com/TrueLayer/reqwest-middleware/commits)

---
updated-dependencies:
- dependency-name: reqwest-middleware
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-11 10:59:23 +08:00
Gaius c91138d2eb
feat(dragonfly-client): optimize piece download performance (#1027)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-07 16:55:19 +08:00
Gaius 32a102da38
feat: add static SUPPORTED_HTTP_PROTOCOLS for proxy (#1026)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-07 14:50:33 +08:00
Gaius 0f04e07797
feat: add certs cache for generating self-signed certs (#1025)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-07 14:25:19 +08:00
Gaius 85ad3f81d0
feat: optimize profile.release and remove tracing-flame (#1024)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-06 20:27:07 +08:00
Gaius 68bc6df1b2
feat: change crc to crc32fast (#1023)
* feat: change crc to crc32fast

Signed-off-by: Gaius <gaius.qi@gmail.com>

* feat: update

Signed-off-by: Gaius <gaius.qi@gmail.com>

* feat: update

Signed-off-by: Gaius <gaius.qi@gmail.com>

---------

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-06 13:58:03 +08:00
Gaius 00ef5fdfbd
refactor(dragonfly-client-storage): optimize buffer creation sequence for better readability (#1022)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-05 22:39:59 +08:00
Gaius 22c37712f0
feat(dragonfly-client/proxy): improved data transfer efficiency in the proxy (#1021)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-05 19:15:15 +08:00
Gaius 4c6afc52a8
chore(ci/Dockerfile.debug): add flamegraph for debug image (#1020)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-05 12:37:48 +08:00
Gaius 033e67a82f
fix(workflows): rename debug image cache
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-04 23:36:03 +08:00
Gaius 232ba0a5e7
fix(dragonfly-client/resource/piece_collector.rs): remove download piece number (#1019)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-04 22:57:35 +08:00
Gaius 8b7645df2a
feat(dragonfly-client/proxy): eliminate potential memory leak in piece reader handling (#1018)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-03 22:47:15 +08:00
dependabot[bot] 48c996375a
chore(deps): Bump serde_json from 1.0.138 to 1.0.140 (#1013)
Bumps [serde_json](https://github.com/serde-rs/json) from 1.0.138 to 1.0.140.
- [Release notes](https://github.com/serde-rs/json/releases)
- [Commits](https://github.com/serde-rs/json/compare/v1.0.138...v1.0.140)

---
updated-dependencies:
- dependency-name: serde_json
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-03 21:07:32 +08:00
dependabot[bot] b0045ffc14
chore(deps): Bump termion from 4.0.3 to 4.0.4 (#1015)
Bumps termion from 4.0.3 to 4.0.4.

---
updated-dependencies:
- dependency-name: termion
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-03 21:06:59 +08:00
dependabot[bot] 75fadcf7a0
chore(deps): Bump chrono from 0.4.39 to 0.4.40 (#1016)
Bumps [chrono](https://github.com/chronotope/chrono) from 0.4.39 to 0.4.40.
- [Release notes](https://github.com/chronotope/chrono/releases)
- [Changelog](https://github.com/chronotope/chrono/blob/main/CHANGELOG.md)
- [Commits](https://github.com/chronotope/chrono/compare/v0.4.39...v0.4.40)

---
updated-dependencies:
- dependency-name: chrono
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-03 21:06:49 +08:00
dependabot[bot] 905a727535
chore(deps): Bump reqwest-tracing from 0.5.5 to 0.5.6 (#1014)
Bumps [reqwest-tracing](https://github.com/TrueLayer/reqwest-middleware) from 0.5.5 to 0.5.6.
- [Release notes](https://github.com/TrueLayer/reqwest-middleware/releases)
- [Commits](https://github.com/TrueLayer/reqwest-middleware/commits)

---
updated-dependencies:
- dependency-name: reqwest-tracing
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-03 21:06:44 +08:00
Gaius a3dfaa6711
chore(workflows/docker.yaml): add debug suffix for trivy (#1017)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-03 21:04:55 +08:00
dependabot[bot] 38b83967e9
chore(deps): Bump anyhow from 1.0.95 to 1.0.97 (#1012)
Bumps [anyhow](https://github.com/dtolnay/anyhow) from 1.0.95 to 1.0.97.
- [Release notes](https://github.com/dtolnay/anyhow/releases)
- [Commits](https://github.com/dtolnay/anyhow/compare/1.0.95...1.0.97)

---
updated-dependencies:
- dependency-name: anyhow
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-03-03 21:03:09 +08:00
Gaius eadbc46e51
feat(dragonfly-client/proxy): remove finished_piece_readers when copy finished (#1011)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-03 17:54:46 +08:00
Gaius cd9f5ca356
feat(dragonfly-client/proxy): add switch for cache in proxy (#1010)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-03 17:45:10 +08:00
Gaius 545bbe5902
chore(ci/Dockerfile.debug): remove --debug from cargo build
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-03 12:16:58 +08:00
Gaius 6440b91fee
chore(workflows/docker.yml): build debug image for client (#1009)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-03-03 11:33:41 +08:00
Gaius 1af3cccb64
feat(dragonfly-client/proxy): optimize map_or in proxy (#1007)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-02-27 09:23:25 +08:00
Gaius 31e4d136d0
feat(dragonfly-client-storage): optimize hard link handling and cache logic (#1005)
- Bump all crate versions from 0.2.15 to 0.2.16
- Add cross-platform device/inode check for hard link operations
- Refactor hard link logic to remove redundant helper methods
- Implement proper file existence checks with same-device/inode validation

Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-02-27 00:23:03 +08:00
Gaius 300bb7b786
chore(release): set toolchain to 1.82.0 (#1006)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-02-27 00:22:25 +08:00
Gaius 746adece0a
fix(dragonfly-client/proxy): resolve OOM issues caused by LRU cache memory leak (#1004)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-02-26 18:50:50 +08:00
Gaius 237d3d7173
feat: cleaning up Unix Domain Sockets to avoid crashes (#1003)
Signed-off-by: Gaius <gaius.qi@gmail.com>
2025-02-26 18:11:31 +08:00
84 changed files with 9389 additions and 2879 deletions

2
.cargo/config.toml Normal file
View File

@ -0,0 +1,2 @@
[build]
rustflags = ["--cfg", "tokio_unstable"]

16
.github/auto_assign.yml vendored Normal file
View File

@ -0,0 +1,16 @@
# Set to true to add reviewers to pull requests
addReviewers: true
# Set to true to add assignees to pull requests
addAssignees: author
# A list of reviewers to be added to pull requests (GitHub user name)
reviewers:
- gaius-qi
- yxxhero
- chlins
- CormickKneey
- xujihui1985
# A number of reviewers added to the pull request
numberOfReviewers: 3

11
.github/workflows/auto-assign.yml vendored Normal file
View File

@ -0,0 +1,11 @@
name: "Auto Assign"
on:
pull_request_target:
types: [opened, reopened, ready_for_review]
jobs:
add-assignee:
runs-on: ubuntu-latest
steps:
- uses: kentaro-m/auto-assign-action@9f6dbe84a80c6e7639d1b9698048b201052a2a94

View File

@ -26,6 +26,8 @@ jobs:
- name: Install Protoc
uses: arduino/setup-protoc@v2
with:
repo-token: ${{ secrets.GH_TOKEN }}
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
@ -55,6 +57,8 @@ jobs:
- name: Install Protoc
uses: arduino/setup-protoc@v2
with:
repo-token: ${{ secrets.GH_TOKEN }}
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable

View File

@ -86,7 +86,7 @@ jobs:
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: Run Trivy vulnerability scanner in tarball mode
uses: aquasecurity/trivy-action@18f2510ee396bbf400402947b394f2dd8c87dbb0
uses: aquasecurity/trivy-action@dc5a429b52fcf669ce959baa2c2dd26090d2a6c4
with:
image-ref: dragonflyoss/client:${{ steps.get_version.outputs.VERSION }}
severity: 'CRITICAL,HIGH'
@ -94,7 +94,102 @@ jobs:
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d
uses: github/codeql-action/upload-sarif@76621b61decf072c1cee8dd1ce2d2a82d33c17ed
with:
sarif_file: 'trivy-results.sarif'
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
push_client_debug_image_to_registry:
name: Push Client Debug Image
runs-on: [self-hosted, Linux, X64]
timeout-minutes: 600
steps:
- name: Check out code
uses: actions/checkout@v4
with:
submodules: recursive
- name: Get Version
id: get_version
run: |
VERSION=${GITHUB_REF#refs/tags/}
if [[ ${GITHUB_REF} == "refs/heads/main" || ${GITHUB_REF} =~ refs/pull/([0-9]+)/merge ]]; then
VERSION=latest
fi
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
- name: Get Git Revision
id: vars
shell: bash
run: |
echo "git_revision=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
- name: PrepareReg Names
run: |
echo IMAGE_REPOSITORY=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
- name: Setup QEMU
uses: docker/setup-qemu-action@v3
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Cache Docker layers
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-debug-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-debug-
- name: Login Docker Hub
uses: docker/login-action@v3
with:
registry: docker.io
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push to Registry
uses: docker/build-push-action@v6
with:
context: .
file: ci/Dockerfile.debug
platforms: linux/amd64,linux/arm64
labels: |-
org.opencontainers.image.source=https://github.com/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
build-args: |
GITVERSION=git-${{ steps.vars.outputs.git_revision }}
VERSION=${{ steps.get_version.outputs.VERSION }}-debug
tags: |
dragonflyoss/client:${{ steps.get_version.outputs.VERSION }}-debug
ghcr.io/${{ env.IMAGE_REPOSITORY }}:${{ steps.get_version.outputs.VERSION }}-debug
push: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: Run Trivy vulnerability scanner in tarball mode
uses: aquasecurity/trivy-action@dc5a429b52fcf669ce959baa2c2dd26090d2a6c4
with:
image-ref: dragonflyoss/client:${{ steps.get_version.outputs.VERSION }}-debug
severity: 'CRITICAL,HIGH'
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@76621b61decf072c1cee8dd1ce2d2a82d33c17ed
with:
sarif_file: 'trivy-results.sarif'
@ -181,7 +276,7 @@ jobs:
cache-to: type=local,dest=/tmp/.buildx-cache-new
- name: Run Trivy vulnerability scanner in tarball mode
uses: aquasecurity/trivy-action@18f2510ee396bbf400402947b394f2dd8c87dbb0
uses: aquasecurity/trivy-action@dc5a429b52fcf669ce959baa2c2dd26090d2a6c4
with:
image-ref: dragonflyoss/dfinit:${{ steps.get_version.outputs.VERSION }}
severity: 'CRITICAL,HIGH'
@ -189,7 +284,7 @@ jobs:
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@b56ba49b26e50535fa1e7f7db0f4f7b4bf65d80d
uses: github/codeql-action/upload-sarif@76621b61decf072c1cee8dd1ce2d2a82d33c17ed
with:
sarif_file: 'trivy-results.sarif'

View File

@ -15,18 +15,21 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
- name: Rust cache
- name: Rust cache
uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- name: Install Protoc
uses: arduino/setup-protoc@v2
with:
repo-token: ${{ secrets.GH_TOKEN }}
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
toolchain: 1.85.0
- name: Set up Clang
uses: egor-tensin/setup-clang@v1

20
.github/workflows/pr-labels.yml vendored Normal file
View File

@ -0,0 +1,20 @@
name: PR Label
on:
pull_request:
types: [opened, labeled, unlabeled, synchronize]
permissions:
contents: read
jobs:
classify:
name: Classify PR
runs-on: ubuntu-latest
steps:
- name: PR impact specified
uses: mheap/github-action-required-labels@8afbe8ae6ab7647d0c9f0cfa7c2f939650d22509 # v5.5
with:
mode: exactly
count: 1
labels: 'bug, enhancement, documentation, dependencies'

View File

@ -52,12 +52,13 @@ jobs:
target: ${{ matrix.target }}
- name: Install cargo-deb
uses: taiki-e/cache-cargo-install-action@v2
uses: taiki-e/cache-cargo-install-action@b33c63d3b3c85540f4eba8a4f71a5cc0ce030855
with:
tool: cargo-deb
# Don't upgrade cargo-deb, refer to https://github.com/kornelski/cargo-deb/issues/169.
tool: cargo-deb@2.10.0
- name: Install cargo-generate-rpm
uses: taiki-e/install-action@v2
uses: taiki-e/install-action@daa3c1f1f9a9d46f686d9fc2f65773d0c293688b
with:
tool: cargo-generate-rpm
@ -95,7 +96,6 @@ jobs:
mkdir -p "$dirname"
mv "target/${{ matrix.target }}/release/dfget" "$dirname"
mv "target/${{ matrix.target }}/release/dfdaemon" "$dirname"
mv "target/${{ matrix.target }}/release/dfstore" "$dirname"
mv "target/${{ matrix.target }}/release/dfcache" "$dirname"
mv "target/${{ matrix.target }}/release/dfinit" "$dirname"
mv CONTRIBUTING.md LICENSE README.md "$dirname"
@ -119,7 +119,7 @@ jobs:
contents: write
steps:
- name: Download Release Artifacts
uses: actions/download-artifact@v4
uses: actions/download-artifact@v5
with:
path: releases
pattern: release-*
@ -153,6 +153,8 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: 1.85.0
- name: Install dependencies
run: |
@ -163,3 +165,4 @@ jobs:
with:
registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
ignore-unpublished-changes: true
args: --locked

31
.github/workflows/stale.yml vendored Normal file
View File

@ -0,0 +1,31 @@
name: Close stale issues and PRs
on:
workflow_dispatch:
schedule:
- cron: "0 0 * * *"
permissions:
issues: write
pull-requests: write
jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@5bef64f19d7facfb25b37b414482c7164d639639 # v9.1.0
id: stale
with:
delete-branch: true
days-before-close: 7
days-before-stale: 90
days-before-pr-close: 7
days-before-pr-stale: 120
stale-issue-label: "stale"
exempt-issue-labels: bug,wip,on-hold
exempt-pr-labels: bug,wip,on-hold
exempt-all-milestones: true
stale-issue-message: 'This issue is stale because it has been open 90 days with no activity.'
close-issue-message: 'This issue was closed because it has been stalled for 7 days with no activity.'
stale-pr-message: 'This PR is stale because it has been open 120 days with no activity.'
close-pr-message: 'This PR was closed because it has been stalled for 7 days with no activity.'

1407
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -12,7 +12,7 @@ members = [
]
[workspace.package]
version = "0.2.14"
version = "1.0.10"
authors = ["The Dragonfly Developers"]
homepage = "https://d7y.io/"
repository = "https://github.com/dragonflyoss/client.git"
@ -22,15 +22,16 @@ readme = "README.md"
edition = "2021"
[workspace.dependencies]
dragonfly-client = { path = "dragonfly-client", version = "0.2.14" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.2.14" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.2.14" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.2.14" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.2.14" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.2.14" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.2.14" }
thiserror = "1.0"
dragonfly-api = "=2.1.30"
dragonfly-client = { path = "dragonfly-client", version = "1.0.10" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "1.0.10" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "1.0.10" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "1.0.10" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "1.0.10" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "1.0.10" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "1.0.10" }
dragonfly-api = "2.1.57"
thiserror = "2.0"
futures = "0.3.31"
reqwest = { version = "0.12.4", features = [
"stream",
"native-tls",
@ -40,11 +41,12 @@ reqwest = { version = "0.12.4", features = [
"brotli",
"zstd",
"deflate",
"blocking",
] }
reqwest-middleware = "0.4"
rcgen = { version = "0.12.1", features = ["x509-parser"] }
hyper = { version = "1.6", features = ["full"] }
hyper-util = { version = "0.1.10", features = [
hyper-util = { version = "0.1.16", features = [
"client",
"client-legacy",
"tokio",
@ -57,12 +59,11 @@ http-range-header = "0.4.2"
tracing = "0.1"
url = "2.5.4"
rustls = { version = "0.22.4", features = ["tls12"] }
rustls-pki-types = "1.11.0"
rustls-pki-types = "1.12.0"
rustls-pemfile = "2.2.0"
sha2 = "0.10"
blake3 = "1.6.0"
crc = "3.2.1"
uuid = { version = "1.14", features = ["v4"] }
crc32fast = "1.5.0"
uuid = { version = "1.16", features = ["v4"] }
hex = "0.4"
rocksdb = "0.22.0"
serde = { version = "1.0", features = ["derive"] }
@ -70,16 +71,16 @@ serde_yaml = "0.9"
http = "1"
tonic = { version = "0.12.2", features = ["tls"] }
tonic-reflection = "0.12.3"
tokio = { version = "1.43.0", features = ["full"] }
tokio-util = { version = "0.7.13", features = ["full"] }
tokio = { version = "1.47.1", features = ["full", "tracing"] }
tokio-util = { version = "0.7.16", features = ["full"] }
tokio-stream = "0.1.17"
validator = { version = "0.16", features = ["derive"] }
warp = "0.3.5"
headers = "0.4.0"
headers = "0.4.1"
regex = "1.11.1"
humantime = "2.1.0"
prost-wkt-types = "0.6"
chrono = { version = "0.4.39", features = ["serde", "clock"] }
chrono = { version = "0.4.41", features = ["serde", "clock"] }
openssl = { version = "0.10", features = ["vendored"] }
opendal = { version = "0.48.0", features = [
"services-s3",
@ -90,25 +91,35 @@ opendal = { version = "0.48.0", features = [
"services-cos",
"services-webhdfs",
] }
clap = { version = "4.5.30", features = ["derive"] }
anyhow = "1.0.95"
toml_edit = "0.22.24"
toml = "0.8.20"
base16ct = { version = "0.2", features = ["alloc"] }
bytesize = { version = "1.3.2", features = ["serde"] }
clap = { version = "4.5.45", features = ["derive"] }
anyhow = "1.0.98"
toml_edit = "0.22.26"
toml = "0.8.23"
bytesize = { version = "1.3.3", features = ["serde"] }
bytesize-serde = "0.2.1"
percent-encoding = "2.3.1"
tempfile = "3.17.1"
tempfile = "3.20.0"
tokio-rustls = "0.25.0-alpha.4"
serde_json = "1.0.138"
serde_json = "1.0.142"
lru = "0.12.5"
fs2 = "0.4.3"
lazy_static = "1.5"
bytes = "1.10"
local-ip-address = "0.6.5"
sysinfo = { version = "0.32.1", default-features = false, features = ["component", "disk", "network", "system", "user"] }
[profile.release]
opt-level = "z"
lto = true
opt-level = 3
lto = "thin"
codegen-units = 1
panic = "abort"
strip = "symbols"
[profile.dev]
opt-level = 0
debug = true
incremental = true
strip = false
[profile.bench]
debug = true

View File

@ -20,9 +20,9 @@ You can find the full documentation on the [d7y.io](https://d7y.io).
Join the conversation and help the community.
- **Slack Channel**: [#dragonfly](https://cloud-native.slack.com/messages/dragonfly/) on [CNCF Slack](https://slack.cncf.io/)
- **Discussion Group**: <dragonfly-discuss@googlegroups.com>
- **Developer Group**: <dragonfly-developers@googlegroups.com>
- **Github Discussions**: [Dragonfly Discussion Forum](https://github.com/dragonflyoss/dragonfly/discussions)
- **Developer Group**: <dragonfly-developers@googlegroups.com>
- **Maintainer Group**: <dragonfly-maintainers@googlegroups.com>
- **Twitter**: [@dragonfly_oss](https://twitter.com/dragonfly_oss)
- **DingTalk**: [22880028764](https://qr.dingtalk.com/action/joingroup?code=v1,k1,pkV9IbsSyDusFQdByPSK3HfCG61ZCLeb8b/lpQ3uUqI=&_dt_no_comment=1&origin=11)
@ -30,7 +30,3 @@ Join the conversation and help the community.
You should check out our
[CONTRIBUTING](./CONTRIBUTING.md) and develop the project together.
## License
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fdragonflyoss%2Fclient.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fdragonflyoss%2Fclient?ref=badge_large)

View File

@ -1,4 +1,4 @@
FROM rust:1.82.0 AS builder
FROM public.ecr.aws/docker/library/rust:1.85.0 AS builder
WORKDIR /app/client
@ -7,6 +7,7 @@ RUN apt-get update && apt-get install -y \
&& rm -rf /var/lib/apt/lists/*
COPY Cargo.toml Cargo.lock ./
COPY .cargo ./cargo
COPY dragonfly-client/Cargo.toml ./dragonfly-client/Cargo.toml
COPY dragonfly-client/src ./dragonfly-client/src
@ -20,6 +21,7 @@ COPY dragonfly-client-config/build.rs ./dragonfly-client-config/build.rs
COPY dragonfly-client-storage/Cargo.toml ./dragonfly-client-storage/Cargo.toml
COPY dragonfly-client-storage/src ./dragonfly-client-storage/src
COPY dragonfly-client-storage/benches ./dragonfly-client-storage/benches
COPY dragonfly-client-backend/Cargo.toml ./dragonfly-client-backend/Cargo.toml
COPY dragonfly-client-backend/src ./dragonfly-client-backend/src
@ -33,9 +35,15 @@ COPY dragonfly-client-util/src ./dragonfly-client-util/src
COPY dragonfly-client-init/Cargo.toml ./dragonfly-client-init/Cargo.toml
COPY dragonfly-client-init/src ./dragonfly-client-init/src
RUN cargo build --release --verbose --bin dfget --bin dfdaemon --bin dfstore --bin dfcache
ARG TARGETPLATFORM
RUN case "${TARGETPLATFORM}" in \
"linux/arm64") export JEMALLOC_SYS_WITH_LG_PAGE=16;; \
esac && \
cargo build --release --verbose --bin dfget --bin dfdaemon --bin dfcache
FROM alpine:3.20 AS health
RUN cargo install tokio-console --locked --root /usr/local
FROM public.ecr.aws/docker/library/alpine:3.20 AS health
ENV GRPC_HEALTH_PROBE_VERSION=v0.4.24
@ -48,22 +56,24 @@ RUN if [ "$(uname -m)" = "ppc64le" ]; then \
fi && \
chmod +x /bin/grpc_health_probe
FROM golang:1.23.0-alpine3.20 AS pprof
FROM public.ecr.aws/docker/library/golang:1.23.0-alpine3.20 AS pprof
RUN go install github.com/google/pprof@latest
RUN go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
FROM debian:bookworm-slim
FROM public.ecr.aws/debian/debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends iperf3 fio wget curl \
iotop bash-completion procps apache2-utils ca-certificates binutils bpfcc-tools \
dnsutils iputils-ping vim linux-perf llvm graphviz \
RUN apt-get update && apt-get install -y --no-install-recommends iperf3 fio curl \
iotop sysstat bash-completion procps apache2-utils ca-certificates binutils \
dnsutils iputils-ping llvm graphviz lsof strace dstat net-tools \
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/client/target/release/dfget /usr/local/bin/dfget
COPY --from=builder /app/client/target/release/dfdaemon /usr/local/bin/dfdaemon
COPY --from=builder /app/client/target/release/dfstore /usr/local/bin/dfstore
COPY --from=builder /app/client/target/release/dfcache /usr/local/bin/dfcache
COPY --from=builder /usr/local/bin/tokio-console /usr/local/bin/
COPY --from=pprof /go/bin/pprof /bin/pprof
COPY --from=pprof /go/bin/grpcurl /bin/grpcurl
COPY --from=health /bin/grpc_health_probe /bin/grpc_health_probe
ENTRYPOINT ["/usr/local/bin/dfdaemon"]

83
ci/Dockerfile.debug Normal file
View File

@ -0,0 +1,83 @@
FROM public.ecr.aws/docker/library/rust:1.85.0 AS builder
WORKDIR /app/client
RUN apt-get update && apt-get install -y \
openssl libclang-dev pkg-config protobuf-compiler git \
&& rm -rf /var/lib/apt/lists/*
COPY Cargo.toml Cargo.lock ./
COPY .cargo ./cargo
COPY dragonfly-client/Cargo.toml ./dragonfly-client/Cargo.toml
COPY dragonfly-client/src ./dragonfly-client/src
COPY dragonfly-client-core/Cargo.toml ./dragonfly-client-core/Cargo.toml
COPY dragonfly-client-core/src ./dragonfly-client-core/src
COPY dragonfly-client-config/Cargo.toml ./dragonfly-client-config/Cargo.toml
COPY dragonfly-client-config/src ./dragonfly-client-config/src
COPY dragonfly-client-config/build.rs ./dragonfly-client-config/build.rs
COPY dragonfly-client-storage/Cargo.toml ./dragonfly-client-storage/Cargo.toml
COPY dragonfly-client-storage/src ./dragonfly-client-storage/src
COPY dragonfly-client-storage/benches ./dragonfly-client-storage/benches
COPY dragonfly-client-backend/Cargo.toml ./dragonfly-client-backend/Cargo.toml
COPY dragonfly-client-backend/src ./dragonfly-client-backend/src
COPY dragonfly-client-backend/examples/plugin/Cargo.toml ./dragonfly-client-backend/examples/plugin/Cargo.toml
COPY dragonfly-client-backend/examples/plugin/src ./dragonfly-client-backend/examples/plugin/src
COPY dragonfly-client-util/Cargo.toml ./dragonfly-client-util/Cargo.toml
COPY dragonfly-client-util/src ./dragonfly-client-util/src
COPY dragonfly-client-init/Cargo.toml ./dragonfly-client-init/Cargo.toml
COPY dragonfly-client-init/src ./dragonfly-client-init/src
ARG TARGETPLATFORM
RUN case "${TARGETPLATFORM}" in \
"linux/arm64") export JEMALLOC_SYS_WITH_LG_PAGE=16;; \
esac && \
cargo build --verbose --bin dfget --bin dfdaemon --bin dfcache
RUN cargo install flamegraph --root /usr/local
RUN cargo install bottom --locked --root /usr/local
RUN cargo install tokio-console --locked --root /usr/local
FROM public.ecr.aws/docker/library/alpine:3.20 AS health
ENV GRPC_HEALTH_PROBE_VERSION=v0.4.24
RUN if [ "$(uname -m)" = "ppc64le" ]; then \
wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-ppc64le; \
elif [ "$(uname -m)" = "aarch64" ]; then \
wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-arm64; \
else \
wget -qO/bin/grpc_health_probe https://github.com/grpc-ecosystem/grpc-health-probe/releases/download/${GRPC_HEALTH_PROBE_VERSION}/grpc_health_probe-linux-amd64; \
fi && \
chmod +x /bin/grpc_health_probe
FROM public.ecr.aws/docker/library/golang:1.23.0-alpine3.20 AS pprof
RUN go install github.com/google/pprof@latest
RUN go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest
FROM public.ecr.aws/debian/debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends iperf3 fio curl infiniband-diags ibverbs-utils \
iotop sysstat bash-completion procps apache2-utils ca-certificates binutils bpfcc-tools \
dnsutils iputils-ping vim linux-perf llvm lsof socat strace dstat net-tools \
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/client/target/debug/dfget /usr/local/bin/dfget
COPY --from=builder /app/client/target/debug/dfdaemon /usr/local/bin/dfdaemon
COPY --from=builder /app/client/target/debug/dfcache /usr/local/bin/dfcache
COPY --from=builder /usr/local/bin/flamegraph /usr/local/bin/
COPY --from=builder /usr/local/bin/btm /usr/local/bin/
COPY --from=builder /usr/local/bin/tokio-console /usr/local/bin/
COPY --from=pprof /go/bin/pprof /bin/pprof
COPY --from=pprof /go/bin/grpcurl /bin/grpcurl
COPY --from=health /bin/grpc_health_probe /bin/grpc_health_probe
ENTRYPOINT ["/usr/local/bin/dfdaemon"]

View File

@ -1,12 +1,13 @@
FROM rust:1.82.0 AS builder
FROM public.ecr.aws/docker/library/rust:1.85.0 AS builder
RUN apt-get update && apt-get install -y \
openssl libclang-dev pkg-config protobuf-compiler \
&& rm -rf /var/lib/apt/lists/*
openssl libclang-dev pkg-config protobuf-compiler \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app/client
COPY Cargo.toml Cargo.lock ./
COPY .cargo ./cargo
COPY dragonfly-client/Cargo.toml ./dragonfly-client/Cargo.toml
COPY dragonfly-client/src ./dragonfly-client/src
@ -20,6 +21,7 @@ COPY dragonfly-client-config/build.rs ./dragonfly-client-config/build.rs
COPY dragonfly-client-storage/Cargo.toml ./dragonfly-client-storage/Cargo.toml
COPY dragonfly-client-storage/src ./dragonfly-client-storage/src
COPY dragonfly-client-storage/benches ./dragonfly-client-storage/benches
COPY dragonfly-client-backend/Cargo.toml ./dragonfly-client-backend/Cargo.toml
COPY dragonfly-client-backend/src ./dragonfly-client-backend/src
@ -33,9 +35,13 @@ COPY dragonfly-client-util/src ./dragonfly-client-util/src
COPY dragonfly-client-init/Cargo.toml ./dragonfly-client-init/Cargo.toml
COPY dragonfly-client-init/src ./dragonfly-client-init/src
RUN cargo build --release --verbose --bin dfinit
ARG TARGETPLATFORM
RUN case "${TARGETPLATFORM}" in \
"linux/arm64") export JEMALLOC_SYS_WITH_LG_PAGE=16;; \
esac && \
cargo build --release --verbose --bin dfinit
FROM debian:bookworm-slim
FROM public.ecr.aws/debian/debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends wget \
&& rm -rf /var/lib/apt/lists/*

View File

@ -5,7 +5,7 @@ After=network-online.target
After=network.target
[Service]
ExecStart=/usr/bin/dfdaemon --config /etc/dragonfly/dfdaemon.yaml --verbose
ExecStart=/usr/bin/dfdaemon --config /etc/dragonfly/dfdaemon.yaml --console
Type=simple
Environment=HOME=/root

View File

@ -69,7 +69,7 @@ cargo build --release --bin dfdaemon
```bash
# prepare client.yaml by yourself.
./target/release/dfdaemon --config client.yaml -l info --verbose
./target/release/dfdaemon --config client.yaml -l info --console
```
## FlameGraph

View File

@ -15,8 +15,6 @@ dragonfly-client-util.workspace = true
dragonfly-api.workspace = true
reqwest.workspace = true
reqwest-middleware.workspace = true
reqwest-retry = "0.7"
reqwest-tracing = "0.5"
tokio.workspace = true
tokio-util.workspace = true
rustls.workspace = true
@ -26,12 +24,14 @@ url.workspace = true
tracing.workspace = true
opendal.workspace = true
percent-encoding.workspace = true
futures = "0.3.31"
libloading = "0.8.6"
futures.workspace = true
reqwest-retry = "0.7"
reqwest-tracing = "0.5"
libloading = "0.8.8"
[dev-dependencies]
tempfile.workspace = true
wiremock = "0.6.2"
wiremock = "0.6.4"
rustls-pki-types.workspace = true
rustls-pemfile.workspace = true
hyper.workspace = true

View File

@ -14,7 +14,7 @@ cargo build --all && mv target/debug/libhdfs.so {plugin_dir}/backend/libhdfs.so
## Run Client with Plugin
```shell
$ cargo run --bin dfdaemon -- --config {config_dir}/config.yaml -l info --verbose
$ cargo run --bin dfdaemon -- --config {config_dir}/config.yaml -l info --console
INFO load [http] builtin backend
INFO load [https] builtin backend
INFO load [hdfs] plugin backend

View File

@ -31,6 +31,7 @@ pub const HDFS_SCHEME: &str = "hdfs";
const DEFAULT_NAMENODE_PORT: u16 = 9870;
/// Hdfs is a struct that implements the Backend trait.
#[derive(Default)]
pub struct Hdfs {
/// scheme is the scheme of the HDFS.
scheme: String,
@ -39,7 +40,6 @@ pub struct Hdfs {
/// Hdfs implements the Backend trait.
impl Hdfs {
/// new returns a new HDFS backend.
#[instrument(skip_all)]
pub fn new() -> Self {
Self {
scheme: HDFS_SCHEME.to_string(),
@ -47,7 +47,6 @@ impl Hdfs {
}
/// operator initializes the operator with the parsed URL and HDFS config.
#[instrument(skip_all)]
pub fn operator(
&self,
url: Url,
@ -84,7 +83,6 @@ impl Hdfs {
#[tonic::async_trait]
impl super::Backend for Hdfs {
/// scheme returns the scheme of the HDFS backend.
#[instrument(skip_all)]
fn scheme(&self) -> String {
self.scheme.clone()
}

View File

@ -43,7 +43,6 @@ pub struct HTTP {
/// HTTP implements the http interface.
impl HTTP {
/// new returns a new HTTP.
#[instrument(skip_all)]
pub fn new(scheme: &str) -> Result<HTTP> {
// Default TLS client config with no validation.
let client_config_builder = rustls::ClientConfig::builder()
@ -51,11 +50,22 @@ impl HTTP {
.with_custom_certificate_verifier(NoVerifier::new())
.with_no_client_auth();
// Disable automatic compression to prevent double-decompression issues.
//
// Problem scenario:
// 1. Origin server supports gzip and returns "content-encoding: gzip" header.
// 2. Backend decompresses the response and stores uncompressed content to disk.
// 3. When user's client downloads via dfdaemon proxy, the original "content-encoding: gzip".
// header is forwarded to it.
// 4. User's client attempts to decompress the already-decompressed content, causing errors.
//
// Solution: Disable all compression formats (gzip, brotli, zstd, deflate) to ensure
// we receive and store uncompressed content, eliminating the double-decompression issue.
let client = reqwest::Client::builder()
.gzip(true)
.brotli(true)
.zstd(true)
.deflate(true)
.no_gzip()
.no_brotli()
.no_zstd()
.no_deflate()
.use_preconfigured_tls(client_config_builder)
.pool_max_idle_per_host(super::POOL_MAX_IDLE_PER_HOST)
.tcp_keepalive(super::KEEP_ALIVE_INTERVAL)
@ -75,7 +85,6 @@ impl HTTP {
}
/// client returns a new reqwest client.
#[instrument(skip_all)]
fn client(
&self,
client_cert: Option<Vec<CertificateDer<'static>>>,
@ -90,11 +99,22 @@ impl HTTP {
.with_root_certificates(root_cert_store)
.with_no_client_auth();
// Disable automatic compression to prevent double-decompression issues.
//
// Problem scenario:
// 1. Origin server supports gzip and returns "content-encoding: gzip" header.
// 2. Backend decompresses the response and stores uncompressed content to disk.
// 3. When user's client downloads via dfdaemon proxy, the original "content-encoding: gzip".
// header is forwarded to it.
// 4. User's client attempts to decompress the already-decompressed content, causing errors.
//
// Solution: Disable all compression formats (gzip, brotli, zstd, deflate) to ensure
// we receive and store uncompressed content, eliminating the double-decompression issue.
let client = reqwest::Client::builder()
.gzip(true)
.brotli(true)
.zstd(true)
.deflate(true)
.no_gzip()
.no_brotli()
.no_zstd()
.no_deflate()
.use_preconfigured_tls(client_config_builder)
.build()?;
@ -117,7 +137,6 @@ impl HTTP {
#[tonic::async_trait]
impl super::Backend for HTTP {
/// scheme returns the scheme of the HTTP backend.
#[instrument(skip_all)]
fn scheme(&self) -> String {
self.scheme.clone()
}
@ -141,6 +160,13 @@ impl super::Backend for HTTP {
.client(request.client_cert)?
.get(&request.url)
.headers(header)
// Add Range header to ensure Content-Length is returned in response headers.
// Some servers (especially when using Transfer-Encoding: chunked,
// refer to https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Transfer-Encoding.) may not
// include Content-Length in HEAD requests. Using "bytes=0-" requests the
// entire file starting from byte 0, forcing the server to include file size
// information in the response headers.
.header(reqwest::header::RANGE, "bytes=0-")
.timeout(request.timeout)
.send()
.await

View File

@ -23,11 +23,10 @@ use libloading::Library;
use reqwest::header::HeaderMap;
use rustls_pki_types::CertificateDer;
use std::path::Path;
use std::str::FromStr;
use std::{collections::HashMap, pin::Pin, time::Duration};
use std::{fmt::Debug, fs};
use tokio::io::{AsyncRead, AsyncReadExt};
use tracing::{error, info, instrument, warn};
use tracing::{error, info, warn};
use url::Url;
pub mod hdfs;
@ -47,7 +46,7 @@ const HTTP2_KEEP_ALIVE_INTERVAL: Duration = Duration::from_secs(300);
const HTTP2_KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(20);
/// MAX_RETRY_TIMES is the max retry times for the request.
const MAX_RETRY_TIMES: u32 = 3;
const MAX_RETRY_TIMES: u32 = 1;
/// NAME is the name of the package.
pub const NAME: &str = "backend";
@ -167,7 +166,7 @@ where
}
/// The File Entry of a directory, including some relevant file metadata.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct DirEntry {
/// url is the url of the entry.
pub url: String,
@ -227,7 +226,6 @@ pub struct BackendFactory {
/// https://github.com/dragonflyoss/client/tree/main/dragonfly-client-backend/examples/plugin/.
impl BackendFactory {
/// new returns a new BackendFactory.
#[instrument(skip_all)]
pub fn new(plugin_dir: Option<&Path>) -> Result<Self> {
let mut backend_factory = Self::default();
backend_factory.load_builtin_backends()?;
@ -242,14 +240,12 @@ impl BackendFactory {
Ok(backend_factory)
}
/// supported_download_directory returns whether the scheme supports directory download.
#[instrument(skip_all)]
pub fn supported_download_directory(scheme: &str) -> bool {
object_storage::Scheme::from_str(scheme).is_ok() || scheme == hdfs::HDFS_SCHEME
/// unsupported_download_directory returns whether the scheme does not support directory download.
pub fn unsupported_download_directory(scheme: &str) -> bool {
scheme == http::HTTP_SCHEME || scheme == http::HTTPS_SCHEME
}
/// build returns the backend by the scheme of the url.
#[instrument(skip_all)]
pub fn build(&self, url: &str) -> Result<&(dyn Backend + Send + Sync)> {
let url = Url::parse(url).or_err(ErrorType::ParseError)?;
let scheme = url.scheme();
@ -260,7 +256,6 @@ impl BackendFactory {
}
/// load_builtin_backends loads the builtin backends.
#[instrument(skip_all)]
fn load_builtin_backends(&mut self) -> Result<()> {
self.backends.insert(
"http".to_string(),
@ -330,13 +325,12 @@ impl BackendFactory {
}
/// load_plugin_backends loads the plugin backends.
#[instrument(skip_all)]
fn load_plugin_backends(&mut self, plugin_dir: &Path) -> Result<()> {
let backend_plugin_dir = plugin_dir.join(NAME);
if !backend_plugin_dir.exists() {
warn!(
"skip loading plugin backends, because the plugin directory {} does not exist",
plugin_dir.display()
backend_plugin_dir.display()
);
return Ok(());
}
@ -436,9 +430,15 @@ mod tests {
let result = BackendFactory::new(Some(&plugin_dir));
assert!(result.is_err());
assert_eq!(
format!("{}", result.err().unwrap()),
format!("PluginError cause: {}: file too short", lib_path.display()),
let err_msg = format!("{}", result.err().unwrap());
assert!(
err_msg.starts_with("PluginError cause:"),
"error message should start with 'PluginError cause:'"
);
assert!(
err_msg.contains(&lib_path.display().to_string()),
"error message should contain library path"
);
}

View File

@ -177,7 +177,6 @@ pub struct ObjectStorage {
/// ObjectStorage implements the ObjectStorage trait.
impl ObjectStorage {
/// Returns ObjectStorage that implements the Backend trait.
#[instrument(skip_all)]
pub fn new(scheme: Scheme) -> ClientResult<ObjectStorage> {
// Initialize the reqwest client.
let client = reqwest::Client::builder()
@ -196,7 +195,6 @@ impl ObjectStorage {
}
/// operator initializes the operator with the parsed URL and object storage.
#[instrument(skip_all)]
pub fn operator(
&self,
parsed_url: &super::object_storage::ParsedURL,
@ -223,7 +221,6 @@ impl ObjectStorage {
}
/// s3_operator initializes the S3 operator with the parsed URL and object storage.
#[instrument(skip_all)]
pub fn s3_operator(
&self,
parsed_url: &super::object_storage::ParsedURL,
@ -276,7 +273,6 @@ impl ObjectStorage {
}
/// gcs_operator initializes the GCS operator with the parsed URL and object storage.
#[instrument(skip_all)]
pub fn gcs_operator(
&self,
parsed_url: &super::object_storage::ParsedURL,
@ -311,7 +307,6 @@ impl ObjectStorage {
}
/// abs_operator initializes the ABS operator with the parsed URL and object storage.
#[instrument(skip_all)]
pub fn abs_operator(
&self,
parsed_url: &super::object_storage::ParsedURL,
@ -354,7 +349,6 @@ impl ObjectStorage {
}
/// oss_operator initializes the OSS operator with the parsed URL and object storage.
#[instrument(skip_all)]
pub fn oss_operator(
&self,
parsed_url: &super::object_storage::ParsedURL,
@ -398,7 +392,6 @@ impl ObjectStorage {
}
/// obs_operator initializes the OBS operator with the parsed URL and object storage.
#[instrument(skip_all)]
pub fn obs_operator(
&self,
parsed_url: &super::object_storage::ParsedURL,
@ -487,7 +480,6 @@ impl ObjectStorage {
#[tonic::async_trait]
impl crate::Backend for ObjectStorage {
/// scheme returns the scheme of the object storage.
#[instrument(skip_all)]
fn scheme(&self) -> String {
self.scheme.to_string()
}

View File

@ -13,6 +13,7 @@ build = "build.rs"
[dependencies]
dragonfly-client-core.workspace = true
dragonfly-client-util.workspace = true
local-ip-address.workspace = true
clap.workspace = true
regex.workspace = true
serde.workspace = true
@ -21,13 +22,16 @@ validator.workspace = true
humantime.workspace = true
serde_yaml.workspace = true
tokio.workspace = true
tempfile.workspace = true
serde_json.workspace = true
bytesize.workspace = true
bytesize-serde.workspace = true
tonic.workspace = true
rustls-pki-types.workspace = true
rcgen.workspace = true
reqwest.workspace = true
home = "0.5.11"
local-ip-address = "0.6.3"
hostname = "^0.4"
humantime-serde = "1.1.1"
serde_regex = "1.1.0"
http-serde = "2.1.1"

File diff suppressed because it is too large Load Diff

View File

@ -173,7 +173,7 @@ pub struct CRIO {
pub registries: Vec<CRIORegistry>,
}
/// CRIORegistry is the registry configuration for cri-o.
/// PodmanRegistry is the registry configuration for podman.
#[derive(Debug, Clone, Default, Validate, Deserialize, Serialize, PartialEq, Eq)]
#[serde(default, rename_all = "camelCase")]
pub struct PodmanRegistry {
@ -352,6 +352,62 @@ impl Config {
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
#[test]
fn test_default_dfinit_config_path() {
let expected = crate::default_config_dir().join("dfinit.yaml");
assert_eq!(default_dfinit_config_path(), expected);
}
#[test]
fn test_default_dfinit_log_dir() {
let expected = crate::default_log_dir().join(NAME);
assert_eq!(default_dfinit_log_dir(), expected);
}
#[test]
fn test_container_runtime_default_paths() {
assert_eq!(
default_container_runtime_containerd_config_path(),
Path::new("/etc/containerd/config.toml")
);
assert_eq!(
default_container_runtime_docker_config_path(),
Path::new("/etc/docker/daemon.json")
);
assert_eq!(
default_container_runtime_crio_config_path(),
Path::new("/etc/containers/registries.conf")
);
assert_eq!(
default_container_runtime_podman_config_path(),
Path::new("/etc/containers/registries.conf")
);
}
#[test]
fn test_default_unqualified_search_registries() {
let crio_registries = default_container_runtime_crio_unqualified_search_registries();
assert_eq!(
crio_registries,
vec![
"registry.fedoraproject.org",
"registry.access.redhat.com",
"docker.io"
]
);
let podman_registries = default_container_runtime_podman_unqualified_search_registries();
assert_eq!(
podman_registries,
vec![
"registry.fedoraproject.org",
"registry.access.redhat.com",
"docker.io"
]
);
}
#[test]
fn serialize_container_runtime() {
@ -473,4 +529,43 @@ containerRuntime:
panic!("failed to deserialize");
}
}
#[test]
fn deserialize_container_runtime_podman_correctly() {
let raw_data = r#"
proxy:
addr: "hello"
containerRuntime:
podman:
configPath: "test_path"
unqualifiedSearchRegistries:
- "reg1"
- "reg2"
registries:
- prefix: "prefix1"
location: "location1"
- prefix: "prefix2"
location: "location2"
"#;
let cfg: Config = serde_yaml::from_str(raw_data).expect("failed to deserialize");
if let Some(ContainerRuntimeConfig::Podman(c)) = cfg.container_runtime.config {
assert_eq!(PathBuf::from("test_path"), c.config_path);
assert_eq!(vec!["reg1", "reg2"], c.unqualified_search_registries);
assert_eq!(
vec![
PodmanRegistry {
location: "location1".to_string(),
prefix: "prefix1".to_string()
},
PodmanRegistry {
location: "location2".to_string(),
prefix: "prefix2".to_string()
},
],
c.registries
);
} else {
panic!("failed to deserialize");
}
}
}

View File

@ -1,25 +0,0 @@
/*
* Copyright 2024 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::path::PathBuf;
/// NAME is the name of dfstore.
pub const NAME: &str = "dfstore";
/// default_dfstore_log_dir is the default log directory for dfstore.
pub fn default_dfstore_log_dir() -> PathBuf {
crate::default_log_dir().join(NAME)
}

View File

@ -21,7 +21,6 @@ pub mod dfcache;
pub mod dfdaemon;
pub mod dfget;
pub mod dfinit;
pub mod dfstore;
/// SERVICE_NAME is the name of the service.
pub const SERVICE_NAME: &str = "dragonfly";
@ -105,7 +104,7 @@ pub fn default_lock_dir() -> PathBuf {
/// default_plugin_dir is the default plugin directory for client.
pub fn default_plugin_dir() -> PathBuf {
#[cfg(target_os = "linux")]
return PathBuf::from("/var/lib/dragonfly/plugins/");
return PathBuf::from("/usr/local/lib/dragonfly/plugins/");
#[cfg(target_os = "macos")]
return home::home_dir().unwrap().join(".dragonfly").join("plugins");
@ -149,3 +148,20 @@ impl clap::builder::TypedValueParser for VersionValueParser {
Ok(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
use clap::{builder::TypedValueParser, Command};
use std::ffi::OsStr;
#[test]
fn version_value_parser_references_non_real_values() {
let parser = VersionValueParser;
let cmd = Command::new("test_app");
let value = OsStr::new("false");
let result = parser.parse_ref(&cmd, None, value);
assert!(result.is_ok());
assert!(!result.unwrap());
}
}

View File

@ -22,4 +22,3 @@ hyper-util.workspace = true
opendal.workspace = true
url.workspace = true
headers.workspace = true
libloading = "0.8.6"

View File

@ -62,6 +62,10 @@ pub enum DFError {
#[error{"piece {0} state is failed"}]
PieceStateIsFailed(String),
/// DownloadPieceFinished is the error when the download piece finished timeout.
#[error{"download piece {0} finished timeout"}]
DownloadPieceFinished(String),
/// WaitForPieceFinishedTimeout is the error when the wait for piece finished timeout.
#[error{"wait for piece {0} finished timeout"}]
WaitForPieceFinishedTimeout(String),

View File

@ -23,7 +23,6 @@ tokio.workspace = true
anyhow.workspace = true
tracing.workspace = true
toml_edit.workspace = true
toml.workspace = true
url.workspace = true
tempfile.workspace = true
serde_json.workspace = true

View File

@ -64,12 +64,8 @@ struct Args {
)]
log_max_files: usize,
#[arg(
long = "verbose",
default_value_t = false,
help = "Specify whether to print log"
)]
verbose: bool,
#[arg(long, default_value_t = false, help = "Specify whether to print log")]
console: bool,
#[arg(
short = 'V',
@ -94,8 +90,12 @@ async fn main() -> Result<(), anyhow::Error> {
args.log_level,
args.log_max_files,
None,
None,
None,
None,
None,
false,
args.verbose,
args.console,
);
// Load config.

View File

@ -50,8 +50,6 @@ impl ContainerRuntime {
/// run runs the container runtime to initialize runtime environment for the dfdaemon.
#[instrument(skip_all)]
pub async fn run(&self) -> Result<()> {
// If containerd is enabled, override the default containerd
// configuration.
match &self.engine {
None => Ok(()),
Some(Engine::Containerd(containerd)) => containerd.run().await,

View File

@ -22,13 +22,22 @@ tracing.workspace = true
prost-wkt-types.workspace = true
tokio.workspace = true
tokio-util.workspace = true
sha2.workspace = true
crc.workspace = true
base16ct.workspace = true
crc32fast.workspace = true
fs2.workspace = true
num_cpus = "1.0"
bytes.workspace = true
bytesize.workspace = true
num_cpus = "1.17"
bincode = "1.3.3"
rayon = "1.10.0"
walkdir = "2.5.0"
[dev-dependencies]
tempdir = "0.3"
tempfile.workspace = true
criterion = "0.5"
[[bench]]
name = "cache"
harness = false
[[bench]]
name = "lru_cache"
harness = false

View File

@ -0,0 +1,468 @@
/*
* Copyright 2025 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use bytes::Bytes;
use bytesize::ByteSize;
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use dragonfly_client_config::dfdaemon::{Config, Storage};
use dragonfly_client_storage::{cache::Cache, metadata::Piece};
use std::sync::Arc;
use tokio::io::AsyncReadExt;
use tokio::runtime::Runtime;
// Number of pieces to write/read in each benchmark.
const PIECE_COUNT: usize = 100;
fn create_config(capacity: ByteSize) -> Config {
Config {
storage: Storage {
cache_capacity: capacity,
..Default::default()
},
..Default::default()
}
}
fn create_piece(length: u64) -> Piece {
Piece {
number: 0,
offset: 0,
length,
digest: String::new(),
parent_id: None,
uploading_count: 0,
uploaded_count: 0,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
finished_at: None,
}
}
pub fn put_task(c: &mut Criterion) {
let rt: Runtime = Runtime::new().unwrap();
let mut group = c.benchmark_group("Put Task");
group.bench_with_input(
BenchmarkId::new("Put Task", "10MB"),
&ByteSize::mb(10),
|b, size| {
b.iter_batched(
|| rt.block_on(async { Cache::new(Arc::new(create_config(ByteSize::gb(2)))) }),
|mut cache| {
rt.block_on(async {
cache.put_task("task", black_box(size.as_u64())).await;
});
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Put Task", "100MB"),
&ByteSize::mb(100),
|b, size| {
b.iter_batched(
|| rt.block_on(async { Cache::new(Arc::new(create_config(ByteSize::gb(2)))) }),
|mut cache| {
rt.block_on(async {
cache.put_task("task", black_box(size.as_u64())).await;
});
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Put Task", "1GB"),
&ByteSize::gb(1),
|b, size| {
b.iter_batched(
|| rt.block_on(async { Cache::new(Arc::new(create_config(ByteSize::gb(2)))) }),
|mut cache| {
rt.block_on(async {
cache.put_task("task", black_box(size.as_u64())).await;
});
},
criterion::BatchSize::SmallInput,
);
},
);
group.finish();
}
pub fn delete_task(c: &mut Criterion) {
let rt: Runtime = Runtime::new().unwrap();
let mut group = c.benchmark_group("Delete Task");
group.bench_with_input(
BenchmarkId::new("Delete Task", "10MB"),
&ByteSize::mb(10),
|b, size| {
b.iter_batched(
|| {
let mut cache =
rt.block_on(async { Cache::new(Arc::new(create_config(ByteSize::gb(2)))) });
rt.block_on(async {
cache.put_task("task", black_box(size.as_u64())).await;
});
cache
},
|mut cache| {
rt.block_on(async {
cache.delete_task("task").await.unwrap();
});
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Delete Task", "100MB"),
&ByteSize::mb(100),
|b, size| {
b.iter_batched(
|| {
let mut cache =
rt.block_on(async { Cache::new(Arc::new(create_config(ByteSize::gb(2)))) });
rt.block_on(async {
cache.put_task("task", black_box(size.as_u64())).await;
});
cache
},
|mut cache| {
rt.block_on(async {
cache.delete_task("task").await.unwrap();
});
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Delete Task", "1GB"),
&ByteSize::gb(1),
|b, size| {
b.iter_batched(
|| {
let mut cache =
rt.block_on(async { Cache::new(Arc::new(create_config(ByteSize::gb(2)))) });
rt.block_on(async {
cache.put_task("task", black_box(size.as_u64())).await;
});
cache
},
|mut cache| {
rt.block_on(async {
cache.delete_task("task").await.unwrap();
});
},
criterion::BatchSize::SmallInput,
);
},
);
group.finish();
}
pub fn write_piece(c: &mut Criterion) {
let rt = Runtime::new().unwrap();
let mut group = c.benchmark_group("Write Piece");
group.bench_with_input(
BenchmarkId::new("Write Piece", "4MB"),
&vec![1u8; ByteSize::mb(4).as_u64() as usize],
|b, data| {
b.iter_batched(
|| {
let mut cache = rt.block_on(async {
Cache::new(Arc::new(create_config(
ByteSize::mb(4) * PIECE_COUNT as u64,
)))
});
rt.block_on(async {
cache
.put_task("task", (ByteSize::mb(4) * PIECE_COUNT as u64).as_u64())
.await;
});
cache
},
|cache| {
rt.block_on(async {
for i in 0..PIECE_COUNT {
cache
.write_piece(
"task",
&format!("piece{}", i),
Bytes::copy_from_slice(data),
)
.await
.unwrap();
}
});
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Write Piece", "10MB"),
&vec![1u8; ByteSize::mb(10).as_u64() as usize],
|b, data| {
b.iter_batched(
|| {
let mut cache = rt.block_on(async {
Cache::new(Arc::new(create_config(
ByteSize::mb(10) * PIECE_COUNT as u64,
)))
});
rt.block_on(async {
cache
.put_task("task", (ByteSize::mb(10) * PIECE_COUNT as u64).as_u64())
.await;
});
cache
},
|cache| {
rt.block_on(async {
for i in 0..PIECE_COUNT {
cache
.write_piece(
"task",
&format!("piece{}", i),
Bytes::copy_from_slice(data),
)
.await
.unwrap();
}
});
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Write Piece", "16MB"),
&vec![1u8; ByteSize::mb(16).as_u64() as usize],
|b, data| {
b.iter_batched(
|| {
let mut cache = rt.block_on(async {
Cache::new(Arc::new(create_config(
ByteSize::mb(16) * PIECE_COUNT as u64,
)))
});
rt.block_on(async {
cache
.put_task("task", (ByteSize::mb(16) * PIECE_COUNT as u64).as_u64())
.await;
});
cache
},
|cache| {
rt.block_on(async {
for i in 0..PIECE_COUNT {
cache
.write_piece(
"task",
&format!("piece{}", i),
Bytes::copy_from_slice(data),
)
.await
.unwrap();
}
});
},
criterion::BatchSize::SmallInput,
);
},
);
group.finish();
}
pub fn read_piece(c: &mut Criterion) {
let rt = Runtime::new().unwrap();
let mut group = c.benchmark_group("Read Piece");
group.bench_with_input(
BenchmarkId::new("Read Piece", "4MB"),
&vec![1u8; ByteSize::mb(4).as_u64() as usize],
|b, data| {
b.iter_batched(
|| {
let mut cache = rt.block_on(async {
Cache::new(Arc::new(create_config(
ByteSize::mb(4) * PIECE_COUNT as u64,
)))
});
rt.block_on(async {
cache
.put_task("task", (ByteSize::mb(4) * PIECE_COUNT as u64).as_u64())
.await;
for i in 0..PIECE_COUNT {
cache
.write_piece(
"task",
&format!("piece{}", i),
Bytes::copy_from_slice(data),
)
.await
.unwrap();
}
});
cache
},
|cache| {
rt.block_on(async {
for i in 0..PIECE_COUNT {
let mut reader = cache
.read_piece(
"task",
&format!("piece{}", i),
create_piece(data.len() as u64),
None,
)
.await
.unwrap();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
}
});
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Read Piece", "10MB"),
&vec![1u8; ByteSize::mb(10).as_u64() as usize],
|b, data| {
b.iter_batched(
|| {
let mut cache = rt.block_on(async {
Cache::new(Arc::new(create_config(
ByteSize::mb(10) * PIECE_COUNT as u64,
)))
});
rt.block_on(async {
cache
.put_task("task", (ByteSize::mb(10) * PIECE_COUNT as u64).as_u64())
.await;
for i in 0..PIECE_COUNT {
cache
.write_piece(
"task",
&format!("piece{}", i),
Bytes::copy_from_slice(data),
)
.await
.unwrap();
}
});
cache
},
|cache| {
rt.block_on(async {
for i in 0..PIECE_COUNT {
let mut reader = cache
.read_piece(
"task",
&format!("piece{}", i),
create_piece(data.len() as u64),
None,
)
.await
.unwrap();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
}
});
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Read Piece", "16MB"),
&vec![1u8; ByteSize::mb(16).as_u64() as usize],
|b, data| {
b.iter_batched(
|| {
let mut cache = rt.block_on(async {
Cache::new(Arc::new(create_config(
ByteSize::mb(16) * PIECE_COUNT as u64,
)))
});
rt.block_on(async {
cache
.put_task("task", (ByteSize::mb(16) * PIECE_COUNT as u64).as_u64())
.await;
for i in 0..PIECE_COUNT {
cache
.write_piece(
"task",
&format!("piece{}", i),
Bytes::copy_from_slice(data),
)
.await
.unwrap();
}
});
cache
},
|cache| {
rt.block_on(async {
for i in 0..PIECE_COUNT {
let mut reader = cache
.read_piece(
"task",
&format!("piece{}", i),
create_piece(data.len() as u64),
None,
)
.await
.unwrap();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
}
});
},
criterion::BatchSize::SmallInput,
);
},
);
group.finish();
}
criterion_group!(benches, put_task, delete_task, write_piece, read_piece,);
criterion_main!(benches);

View File

@ -0,0 +1,448 @@
/*
* Copyright 2025 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use bytesize::ByteSize;
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use dragonfly_client_storage::cache::lru_cache::LruCache;
// Number of operations to perform in each benchmark
const OPERATION_COUNT: usize = 1000;
pub fn lru_cache_put(c: &mut Criterion) {
let mut group = c.benchmark_group("Lru Cache Put");
group.bench_with_input(
BenchmarkId::new("Lru Cache Put", "4MB"),
&ByteSize::mb(4),
|b, size| {
b.iter_batched(
|| LruCache::new(OPERATION_COUNT),
|mut cache| {
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Lru Cache Put", "10MB"),
&ByteSize::mb(10),
|b, size| {
b.iter_batched(
|| LruCache::new(OPERATION_COUNT),
|mut cache| {
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Lru Cache Put", "16MB"),
&ByteSize::mb(16),
|b, size| {
b.iter_batched(
|| LruCache::new(OPERATION_COUNT),
|mut cache| {
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.finish();
}
pub fn lru_cache_get(c: &mut Criterion) {
let mut group = c.benchmark_group("Lru Cache Get");
group.bench_with_input(
BenchmarkId::new("Lru Cache Get", "4MB"),
&ByteSize::mb(4),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|mut cache| {
for i in 0..OPERATION_COUNT {
black_box(cache.get(&format!("key{}", i)));
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Lru Cache Get", "10MB"),
&ByteSize::mb(10),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|mut cache| {
for i in 0..OPERATION_COUNT {
black_box(cache.get(&format!("key{}", i)));
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Lru Cache Get", "16MB"),
&ByteSize::mb(16),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|mut cache| {
for i in 0..OPERATION_COUNT {
black_box(cache.get(&format!("key{}", i)));
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.finish();
}
pub fn lru_cache_peek(c: &mut Criterion) {
let mut group = c.benchmark_group("Lru Cache Peek");
group.bench_with_input(
BenchmarkId::new("Lru Cache Peek", "4MB"),
&ByteSize::mb(4),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|cache| {
for i in 0..OPERATION_COUNT {
black_box(cache.peek(&format!("key{}", i)));
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Lru Cache Peek", "10MB"),
&ByteSize::mb(10),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|cache| {
for i in 0..OPERATION_COUNT {
black_box(cache.peek(&format!("key{}", i)));
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Lru Cache Peek", "16MB"),
&ByteSize::mb(16),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|cache| {
for i in 0..OPERATION_COUNT {
black_box(cache.peek(&format!("key{}", i)));
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.finish();
}
pub fn lru_cache_contains(c: &mut Criterion) {
let mut group = c.benchmark_group("Lru Cache Contains");
group.bench_with_input(
BenchmarkId::new("Lru Cache Contains", "4MB"),
&ByteSize::mb(4),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|cache| {
for i in 0..OPERATION_COUNT {
black_box(cache.contains(&format!("key{}", i)));
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Lru Cache Contains", "10MB"),
&ByteSize::mb(10),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|cache| {
for i in 0..OPERATION_COUNT {
black_box(cache.contains(&format!("key{}", i)));
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Lru Cache Contains", "16MB"),
&ByteSize::mb(16),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|cache| {
for i in 0..OPERATION_COUNT {
black_box(cache.contains(&format!("key{}", i)));
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.finish();
}
pub fn lru_cache_pop(c: &mut Criterion) {
let mut group = c.benchmark_group("Lru Cache Pop");
group.bench_with_input(
BenchmarkId::new("Lru Cache Pop", "4MB"),
&ByteSize::mb(4),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|mut cache| {
for i in 0..OPERATION_COUNT {
black_box(cache.pop(&format!("key{}", i)));
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Lru Cache Pop", "10MB"),
&ByteSize::mb(10),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|mut cache| {
for i in 0..OPERATION_COUNT {
black_box(cache.pop(&format!("key{}", i)));
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Lru Cache Pop", "16MB"),
&ByteSize::mb(16),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|mut cache| {
for i in 0..OPERATION_COUNT {
black_box(cache.pop(&format!("key{}", i)));
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.finish();
}
pub fn lru_cache_pop_lru(c: &mut Criterion) {
let mut group = c.benchmark_group("Lru Cache Pop Lru");
group.bench_with_input(
BenchmarkId::new("Lru Cache Pop Lru", "4MB"),
&ByteSize::mb(4),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|mut cache| {
while !cache.is_empty() {
black_box(cache.pop_lru());
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Lru Cache Pop Lru", "10MB"),
&ByteSize::mb(10),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|mut cache| {
while !cache.is_empty() {
black_box(cache.pop_lru());
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.bench_with_input(
BenchmarkId::new("Lru Cache Pop Lru", "16MB"),
&ByteSize::mb(16),
|b, size| {
b.iter_batched(
|| {
let mut cache = LruCache::new(OPERATION_COUNT);
for i in 0..OPERATION_COUNT {
cache.put(format!("key{}", i), size.as_u64());
}
cache
},
|mut cache| {
while !cache.is_empty() {
black_box(cache.pop_lru());
}
},
criterion::BatchSize::SmallInput,
);
},
);
group.finish();
}
criterion_group!(
benches,
lru_cache_put,
lru_cache_get,
lru_cache_peek,
lru_cache_contains,
lru_cache_pop,
lru_cache_pop_lru,
);
criterion_main!(benches);

View File

@ -0,0 +1,509 @@
/*
* Copyright 2025 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::{borrow::Borrow, collections::HashMap, hash::Hash, hash::Hasher};
/// KeyRef is a reference to the key.
#[derive(Debug, Clone, Copy)]
struct KeyRef<K> {
k: *const K,
}
/// KeyRef implements Hash for KeyRef.
impl<K: Hash> Hash for KeyRef<K> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
unsafe {
let key = &*self.k;
key.hash(state)
}
}
}
/// KeyRef implements PartialEq for KeyRef.
impl<K: PartialEq> PartialEq for KeyRef<K> {
fn eq(&self, other: &Self) -> bool {
unsafe {
let key1 = &*self.k;
let key2 = &*other.k;
key1.eq(key2)
}
}
}
/// KeyRef implements Eq for KeyRef.
impl<K: Eq> Eq for KeyRef<K> {}
/// KeyWrapper is a wrapper for the key.
#[repr(transparent)]
struct KeyWrapper<K: ?Sized>(K);
/// KeyWrapper implements reference conversion.
impl<K: ?Sized> KeyWrapper<K> {
/// from_ref creates a new KeyWrapper from a reference to the key.
fn from_ref(key: &K) -> &Self {
unsafe { &*(key as *const K as *const KeyWrapper<K>) }
}
}
/// KeyWrapper implements Hash for KeyWrapper.
impl<K: ?Sized + Hash> Hash for KeyWrapper<K> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
}
/// KeyWrapper implements PartialEq for KeyWrapper.
impl<K: ?Sized + PartialEq> PartialEq for KeyWrapper<K> {
#![allow(unknown_lints)]
#[allow(clippy::unconditional_recursion)]
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}
/// KeyWrapper implements Eq for KeyWrapper.
impl<K: ?Sized + Eq> Eq for KeyWrapper<K> {}
/// KeyWrapper implements Borrow for KeyWrapper.
impl<K, Q> Borrow<KeyWrapper<Q>> for KeyRef<K>
where
K: Borrow<Q>,
Q: ?Sized,
{
/// borrow borrows the key.
fn borrow(&self) -> &KeyWrapper<Q> {
unsafe {
let key = &*self.k;
KeyWrapper::from_ref(key.borrow())
}
}
}
/// Entry is a cache entry.
struct Entry<K, V> {
key: K,
value: V,
prev: Option<*mut Entry<K, V>>,
next: Option<*mut Entry<K, V>>,
}
/// Entry implements Drop for Entry.
impl<K, V> Entry<K, V> {
/// new creates a new Entry.
fn new(key: K, value: V) -> Self {
Self {
key,
value,
prev: None,
next: None,
}
}
}
/// LruCache is a least recently used cache.
pub struct LruCache<K, V> {
capacity: usize,
map: HashMap<KeyRef<K>, Box<Entry<K, V>>>,
head: Option<*mut Entry<K, V>>,
tail: Option<*mut Entry<K, V>>,
_marker: std::marker::PhantomData<K>,
}
/// LruCache implements LruCache.
impl<K: Hash + Eq, V> LruCache<K, V> {
/// new creates a new LruCache.
pub fn new(capacity: usize) -> Self {
Self {
capacity,
map: HashMap::new(),
head: None,
tail: None,
_marker: std::marker::PhantomData,
}
}
/// get gets the value of the key.
pub fn get<'a, Q>(&'a mut self, k: &Q) -> Option<&'a V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
if let Some(entry) = self.map.get_mut(KeyWrapper::from_ref(k)) {
let entry_ptr: *mut Entry<K, V> = &mut **entry;
self.detach(entry_ptr);
self.attach(entry_ptr);
Some(&unsafe { &*entry_ptr }.value)
} else {
None
}
}
/// put puts the key and value into the cache.
pub fn put(&mut self, key: K, mut value: V) -> Option<V> {
if let Some(existing_entry) = self.map.get_mut(KeyWrapper::from_ref(&key)) {
let entry = existing_entry.as_mut();
std::mem::swap(&mut entry.value, &mut value);
let entry_ptr: *mut Entry<K, V> = entry;
self.detach(entry_ptr);
self.attach(entry_ptr);
return Some(value);
}
let mut evicted_value = None;
if self.map.len() >= self.capacity {
if let Some(tail) = self.tail {
self.detach(tail);
unsafe {
if let Some(entry) = self.map.remove(KeyWrapper::from_ref(&(*tail).key)) {
evicted_value = Some(entry.value);
}
}
}
}
let new_entry = Box::new(Entry::new(key, value));
let key_ptr: *const K = &new_entry.key;
let entry_ptr = Box::into_raw(new_entry);
unsafe {
self.attach(entry_ptr);
self.map
.insert(KeyRef { k: key_ptr }, Box::from_raw(entry_ptr));
}
evicted_value
}
/// detach detaches the entry from the cache.
fn detach(&mut self, entry: *mut Entry<K, V>) {
unsafe {
let prev = (*entry).prev;
let next = (*entry).next;
match prev {
Some(prev) => (*prev).next = next,
None => self.head = next,
}
match next {
Some(next) => (*next).prev = prev,
None => self.tail = prev,
}
(*entry).prev = None;
(*entry).next = None;
}
}
/// attach attaches the entry to the cache.
fn attach(&mut self, entry: *mut Entry<K, V>) {
match self.head {
Some(head) => {
unsafe {
(*entry).next = Some(head);
(*head).prev = Some(entry);
}
self.head = Some(entry);
}
None => {
self.head = Some(entry);
self.tail = Some(entry);
}
}
}
/// contains checks whether the key exists in the cache.
pub fn contains<Q>(&self, k: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.map.contains_key(KeyWrapper::from_ref(k))
}
/// peek peeks the value of the key. It does not move the key to the front of the cache.
pub fn peek<'a, Q>(&'a self, k: &Q) -> Option<&'a V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.map
.get(KeyWrapper::from_ref(k))
.map(|entry| &entry.value)
}
/// pop_lru pops the least recently used value from the cache.
pub fn pop_lru(&mut self) -> Option<(K, V)> {
if self.is_empty() {
return None;
}
let tail = self.tail?;
self.detach(tail);
unsafe {
self.map
.remove(KeyWrapper::from_ref(&(*tail).key))
.map(|entry| (entry.key, entry.value))
}
}
/// pop removes and returns the value for a given key, if it does not exist, it returns None.
pub fn pop<Q>(&mut self, k: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
match self.map.remove(KeyWrapper::from_ref(k)) {
None => None,
Some(entry) => {
let entry_ptr = Box::into_raw(entry);
self.detach(entry_ptr);
unsafe {
let entry = Box::from_raw(entry_ptr);
Some((entry.key, entry.value))
}
}
}
}
/// is_empty checks whether the cache is empty.
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
}
unsafe impl<K: Send, V: Send> Send for LruCache<K, V> {}
unsafe impl<K: Sync, V: Sync> Sync for LruCache<K, V> {}
impl<K, V> Drop for LruCache<K, V> {
fn drop(&mut self) {
self.map.clear();
self.head = None;
self.tail = None;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let test_cases = vec![
// Normal capacity.
(5, 5),
// Minimum meaningful capacity.
(1, 1),
// Zero capacity.
(0, 0),
// Maximum capacity.
(usize::MAX, usize::MAX),
];
for (capacity, expected_capacity) in test_cases {
let cache: LruCache<String, i32> = LruCache::new(capacity);
assert!(cache.is_empty());
assert_eq!(cache.capacity, expected_capacity);
}
}
#[test]
fn test_get() {
let mut cache: LruCache<String, i32> = LruCache::new(3);
let test_cases = vec![
// Initial insertions.
("key1", 1, None),
("key2", 2, None),
("key3", 3, None),
// Update existing key.
("key2", 22, Some(2)),
// Eviction of oldest key.
("key4", 4, Some(1)),
];
for (key, value, expected_result) in test_cases {
let result = cache.put(key.to_string(), value);
assert_eq!(result, expected_result);
}
// Verify final cache state.
assert_eq!(cache.get(&"key1".to_string()), None);
assert_eq!(cache.get(&"key2".to_string()).copied(), Some(22));
assert_eq!(cache.get(&"key3".to_string()).copied(), Some(3));
assert_eq!(cache.get(&"key4".to_string()).copied(), Some(4));
}
#[test]
fn test_get_after_evction() {
let mut cache = LruCache::new(3);
assert_eq!(cache.get(&"nonexistent".to_string()), None);
// Prepare cache with initial values.
for (key, value) in [("key1", 1), ("key2", 2), ("key3", 3)] {
cache.put(key.to_string(), value);
}
let test_cases = vec![
("key1", Some(1)),
("nonexistent", None),
("key1", Some(1)),
("key3", Some(3)),
];
for (key, expected_value) in test_cases {
assert_eq!(cache.get(&key.to_string()).copied(), expected_value);
}
// Test eviction after getting.
cache.put("key4".to_string(), 4);
assert_eq!(cache.get(&"key1".to_string()).copied(), Some(1));
assert_eq!(cache.get(&"key2".to_string()), None);
assert_eq!(cache.get(&"key3".to_string()).copied(), Some(3));
assert_eq!(cache.get(&"key4".to_string()).copied(), Some(4));
}
#[test]
fn test_put() {
let mut cache = LruCache::new(3);
let test_cases = vec![
// Initial insertions within capacity.
("key1", 1, None),
("key2", 2, None),
("key3", 3, None),
// Overflow capacity, should evict oldest.
("key4", 4, Some(1)),
("key5", 5, Some(2)),
// Update existing key.
("key4", 44, Some(4)),
];
for (key, value, expected_result) in test_cases {
let result = cache.put(key.to_string(), value);
assert_eq!(result, expected_result);
}
// Verify final cache state.
assert_eq!(cache.get(&"key1".to_string()), None);
assert_eq!(cache.get(&"key2".to_string()), None);
assert_eq!(cache.get(&"key3".to_string()).copied(), Some(3));
assert_eq!(cache.get(&"key4".to_string()).copied(), Some(44));
assert_eq!(cache.get(&"key5".to_string()).copied(), Some(5));
}
#[test]
fn test_peek() {
let mut cache: LruCache<String, i32> = LruCache::new(3);
assert_eq!(cache.peek(&"nonexistent".to_string()), None);
// Prepare cache with initial values.
for (key, value) in [("key1", 1), ("key2", 2), ("key3", 3)] {
cache.put(key.to_string(), value);
}
let test_cases = vec![
("nonexistent", None),
("key1", Some(1)),
("key2", Some(2)),
("key3", Some(3)),
];
for (key, expected_value) in test_cases {
assert_eq!(cache.peek(&key.to_string()).copied(), expected_value);
}
// Test eviction after peeking.
cache.put("key4".to_string(), 4);
assert_eq!(cache.peek(&"key1".to_string()), None);
assert_eq!(cache.peek(&"key2".to_string()).copied(), Some(2));
assert_eq!(cache.peek(&"key3".to_string()).copied(), Some(3));
assert_eq!(cache.peek(&"key4".to_string()).copied(), Some(4));
}
#[test]
fn test_contains() {
let mut cache: LruCache<String, i32> = LruCache::new(3);
assert!(!cache.contains(&"nonexistent".to_string()));
// Prepare cache with initial values.
for (key, value) in [("key1", 1), ("key2", 2), ("key3", 3)] {
cache.put(key.to_string(), value);
}
let test_cases = vec![
("nonexistent", false),
("key1", true),
("key2", true),
("key3", true),
];
for (key, expected_result) in test_cases {
assert_eq!(cache.contains(&key.to_string()), expected_result);
}
// Test eviction after contains.
cache.put("key4".to_string(), 4);
assert!(!cache.contains(&"key1".to_string()));
assert!(cache.contains(&"key2".to_string()));
assert!(cache.contains(&"key3".to_string()));
assert!(cache.contains(&"key4".to_string()));
}
#[test]
fn test_pop_lru() {
let mut cache: LruCache<String, i32> = LruCache::new(3);
assert_eq!(cache.pop_lru(), None);
for (key, value) in [("key1", 1), ("key2", 2), ("key3", 3)] {
cache.put(key.to_string(), value);
}
assert_eq!(cache.pop_lru(), Some(("key1".to_string(), 1)));
assert_eq!(cache.pop_lru(), Some(("key2".to_string(), 2)));
assert_eq!(cache.pop_lru(), Some(("key3".to_string(), 3)));
assert_eq!(cache.pop_lru(), None);
assert!(cache.is_empty());
}
#[test]
fn test_pop() {
let mut cache: LruCache<String, i32> = LruCache::new(3);
let test_cases = vec![
("key1".to_string(), Some(("key1".to_string(), 1))),
("key2".to_string(), Some(("key2".to_string(), 2))),
("key3".to_string(), Some(("key3".to_string(), 3))),
("key1".to_string(), None),
("key2".to_string(), None),
("key3".to_string(), None),
];
cache.put("key1".to_string(), 1);
cache.put("key2".to_string(), 2);
cache.put("key3".to_string(), 3);
for (key, expected) in test_cases {
assert_eq!(cache.pop(&key), expected);
}
assert!(cache.is_empty());
}
}

View File

@ -0,0 +1,989 @@
/*
* Copyright 2025 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use bytes::Bytes;
use dragonfly_api::common::v2::Range;
use dragonfly_client_config::dfdaemon::Config;
use dragonfly_client_core::{Error, Result};
use lru_cache::LruCache;
use std::cmp::{max, min};
use std::collections::HashMap;
use std::io::Cursor;
use std::sync::Arc;
use tokio::io::{AsyncRead, BufReader};
use tokio::sync::RwLock;
use tracing::info;
pub mod lru_cache;
/// Task is the task content in the cache.
#[derive(Clone, Debug)]
struct Task {
/// content_length is the length of the task content.
content_length: u64,
/// pieces is the pieces content of the task.
pieces: Arc<RwLock<HashMap<String, Bytes>>>,
}
/// Task implements the task content in the cache.
impl Task {
/// new creates a new task.
fn new(content_length: u64) -> Self {
Self {
content_length,
pieces: Arc::new(RwLock::new(HashMap::new())),
}
}
/// write_piece writes the piece content to the task.
async fn write_piece(&self, id: &str, piece: Bytes) {
let mut pieces = self.pieces.write().await;
pieces.insert(id.to_string(), piece);
}
/// read_piece reads the piece content from the task.
async fn read_piece(&self, id: &str) -> Option<Bytes> {
let pieces = self.pieces.read().await;
pieces.get(id).cloned()
}
/// contains checks whether the piece exists in the task.
async fn contains(&self, id: &str) -> bool {
let pieces = self.pieces.read().await;
pieces.contains_key(id)
}
/// content_length returns the content length of the task.
fn content_length(&self) -> u64 {
self.content_length
}
}
/// Cache is the cache for storing piece content by LRU algorithm.
///
/// Cache storage:
/// 1. Users can preheat task by caching to memory (via CacheTask) or to disk (via Task).
/// For more details, refer to https://github.com/dragonflyoss/api/blob/main/proto/dfdaemon.proto#L174.
/// 2. If the download hits the memory cache, it will be faster than reading from the disk, because there is no
/// page cache for the first read.
///
///```text
/// +--------+
/// │ Source │
/// +--------+
/// ^ ^ Preheat
/// │ │ |
/// +-----------------+ │ │ +----------------------------+
/// │ Other Peers │ │ │ │ Peer | │
/// │ │ │ │ │ v │
/// │ +----------+ │ │ │ │ +----------+ │
/// │ │ Cache |<--|----------|<-Miss--| Cache |--Hit-->|<----Download CacheTask
/// │ +----------+ │ │ │ +----------+ │
/// │ │ │ │ │
/// │ +----------+ │ │ │ +----------+ │
/// │ │ Disk |<--|----------|<-Miss--| Disk |--Hit-->|<----Download Task
/// │ +----------+ │ │ +----------+ │
/// │ │ │ ^ │
/// │ │ │ | │
/// +-----------------+ +----------------------------+
/// |
/// Preheat
///```
/// Task is the metadata of the task.
#[derive(Clone)]
pub struct Cache {
/// config is the configuration of the dfdaemon.
config: Arc<Config>,
/// size is the size of the cache in bytes.
size: u64,
/// capacity is the maximum capacity of the cache in bytes.
capacity: u64,
/// tasks stores the tasks with their task id.
tasks: Arc<RwLock<LruCache<String, Task>>>,
}
/// Cache implements the cache for storing piece content by LRU algorithm.
impl Cache {
/// new creates a new cache with the specified capacity.
pub fn new(config: Arc<Config>) -> Self {
Cache {
config: config.clone(),
size: 0,
capacity: config.storage.cache_capacity.as_u64(),
// LRU cache capacity is set to usize::MAX to avoid evicting tasks. LRU cache will evict tasks
// by cache capacity(cache size) itself, and used pop_lru to evict the least recently
// used task.
tasks: Arc::new(RwLock::new(LruCache::new(usize::MAX))),
}
}
/// read_piece reads the piece from the cache.
pub async fn read_piece(
&self,
task_id: &str,
piece_id: &str,
piece: super::metadata::Piece,
range: Option<Range>,
) -> Result<impl AsyncRead> {
let mut tasks = self.tasks.write().await;
let Some(task) = tasks.get(task_id) else {
return Err(Error::TaskNotFound(task_id.to_string()));
};
let Some(piece_content) = task.read_piece(piece_id).await else {
return Err(Error::PieceNotFound(piece_id.to_string()));
};
drop(tasks);
// Calculate the range of bytes to return based on the range provided.
let (target_offset, target_length) = if let Some(range) = range {
let target_offset = max(piece.offset, range.start) - piece.offset;
let target_length = min(
piece.offset + piece.length - 1,
range.start + range.length - 1,
) - target_offset
- piece.offset
+ 1;
(target_offset as usize, target_length as usize)
} else {
(0, piece.length as usize)
};
// Check if the target range is valid.
let begin = target_offset;
let end = target_offset + target_length;
if begin >= piece_content.len() || end > piece_content.len() {
return Err(Error::InvalidParameter);
}
let content = piece_content.slice(begin..end);
let reader =
BufReader::with_capacity(self.config.storage.read_buffer_size, Cursor::new(content));
Ok(reader)
}
/// write_piece writes the piece content to the cache.
pub async fn write_piece(&self, task_id: &str, piece_id: &str, content: Bytes) -> Result<()> {
let mut tasks = self.tasks.write().await;
let Some(task) = tasks.get(task_id) else {
return Err(Error::TaskNotFound(task_id.to_string()));
};
if task.contains(piece_id).await {
return Ok(());
}
task.write_piece(piece_id, content).await;
Ok(())
}
/// put_task puts a new task into the cache, constrained by the capacity of the cache.
pub async fn put_task(&mut self, task_id: &str, content_length: u64) {
// If the content length is 0, we don't cache the task.
if content_length == 0 {
return;
}
// If the content length is larger than the cache capacity and the task cannot be cached.
if content_length > self.capacity {
info!(
"task {} is too large and cannot be cached: {}",
task_id, content_length
);
return;
}
let mut tasks = self.tasks.write().await;
while self.size + content_length > self.capacity {
match tasks.pop_lru() {
Some((_, task)) => {
self.size -= task.content_length();
}
None => {
break;
}
}
}
let task = Task::new(content_length);
tasks.put(task_id.to_string(), task);
self.size += content_length;
}
pub async fn delete_task(&mut self, task_id: &str) -> Result<()> {
let mut tasks = self.tasks.write().await;
let Some((_, task)) = tasks.pop(task_id) else {
return Err(Error::TaskNotFound(task_id.to_string()));
};
self.size -= task.content_length();
Ok(())
}
/// contains_task checks whether the task exists in the cache.
pub async fn contains_task(&self, id: &str) -> bool {
let tasks = self.tasks.read().await;
tasks.contains(id)
}
/// contains_piece checks whether the piece exists in the specified task.
pub async fn contains_piece(&self, task_id: &str, piece_id: &str) -> bool {
let tasks = self.tasks.read().await;
if let Some(task) = tasks.peek(task_id) {
task.contains(piece_id).await
} else {
false
}
}
}
#[cfg(test)]
mod tests {
use super::super::metadata::Piece;
use super::*;
use bytesize::ByteSize;
use dragonfly_api::common::v2::Range;
use dragonfly_client_config::dfdaemon::Storage;
use tokio::io::AsyncReadExt;
#[tokio::test]
async fn test_new() {
let test_cases = vec![
// Default configuration with 64MiB capacity.
(Config::default(), 0, ByteSize::mib(64).as_u64()),
// Custom configuration with 100MiB capacity.
(
Config {
storage: Storage {
cache_capacity: ByteSize::mib(100),
..Default::default()
},
..Default::default()
},
0,
ByteSize::mib(100).as_u64(),
),
// Zero capacity configuration.
(
Config {
storage: Storage {
cache_capacity: ByteSize::b(0),
..Default::default()
},
..Default::default()
},
0,
0,
),
];
for (config, expected_size, expected_capacity) in test_cases {
let cache = Cache::new(Arc::new(config));
assert_eq!(cache.size, expected_size);
assert_eq!(cache.capacity, expected_capacity);
}
}
#[tokio::test]
async fn test_contains_task() {
let config = Config {
storage: Storage {
cache_capacity: ByteSize::mib(10),
..Default::default()
},
..Default::default()
};
let cache = Cache::new(Arc::new(config));
let test_cases = vec![
// Test non-existent task.
("check", "non_existent", 0, false),
// Add and verify task.
("add", "task1", ByteSize::mib(1).as_u64(), true),
("check", "task1", 0, true),
// Remove and verify task.
("remove", "task1", 0, false),
("check", "task1", 0, false),
// Test multiple tasks.
("add", "task1", ByteSize::mib(1).as_u64(), true),
("add", "task2", ByteSize::mib(2).as_u64(), true),
("check", "task1", 0, true),
("check", "task2", 0, true),
("check", "task3", 0, false),
];
for (operation, task_id, content_length, expected_result) in test_cases {
match operation {
"check" => {
assert_eq!(cache.contains_task(task_id).await, expected_result);
}
"add" => {
let task = Task::new(content_length);
cache.tasks.write().await.put(task_id.to_string(), task);
assert_eq!(cache.contains_task(task_id).await, expected_result);
}
"remove" => {
cache.tasks.write().await.pop_lru();
assert_eq!(cache.contains_task(task_id).await, expected_result);
}
_ => panic!("Unknown operation."),
}
}
}
#[tokio::test]
async fn test_put_task() {
let config = Config {
storage: Storage {
cache_capacity: ByteSize::mib(10),
..Default::default()
},
..Default::default()
};
let mut cache = Cache::new(Arc::new(config));
let test_cases = vec![
// Empty task should not be cached.
("empty_task", 0, false),
// Task equal to capacity should not be cached.
("equal_capacity", ByteSize::mib(10).as_u64(), true),
// Task exceeding capacity should not be cached.
("exceed_capacity", ByteSize::mib(10).as_u64() + 1, false),
// Normal sized task should be cached.
("normal_task", ByteSize::mib(1).as_u64(), true),
];
for (task_id, size, should_exist) in test_cases {
if size > 0 {
cache.put_task(task_id, size).await;
}
assert_eq!(cache.contains_task(task_id).await, should_exist);
}
}
#[tokio::test]
async fn test_put_task_lru() {
let config = Config {
storage: Storage {
cache_capacity: ByteSize::mib(5),
..Default::default()
},
..Default::default()
};
let mut cache = Cache::new(Arc::new(config));
let test_cases = vec![
// Add tasks until eviction triggers.
("lru_task_1", ByteSize::mib(2).as_u64(), true),
("lru_task_2", ByteSize::mib(2).as_u64(), true),
// Third task triggers eviction.
("lru_task_3", ByteSize::mib(2).as_u64(), true),
// Verify eviction results.
("lru_task_1", 0, false),
("lru_task_2", 0, true),
("lru_task_3", 0, true),
];
for (task_id, size, should_exist) in test_cases {
if size > 0 {
cache.put_task(task_id, size).await;
}
assert_eq!(cache.contains_task(task_id).await, should_exist);
}
}
#[tokio::test]
async fn test_delete_task() {
let config = Config {
storage: Storage {
cache_capacity: ByteSize::mib(10),
..Default::default()
},
..Default::default()
};
let mut cache = Cache::new(Arc::new(config));
cache.put_task("task1", ByteSize::mib(1).as_u64()).await;
cache.put_task("task2", ByteSize::mib(1).as_u64()).await;
cache.put_task("task3", ByteSize::mib(1).as_u64()).await;
let test_cases = vec![
("task1", true),
("task2", true),
("task3", true),
("nonexistent", false),
("", false),
("large_task", false),
];
for (task_id, exists) in test_cases {
assert_eq!(cache.contains_task(task_id).await, exists);
let result = cache.delete_task(task_id).await;
if exists {
assert!(result.is_ok());
} else {
assert!(result.is_err());
}
assert!(!cache.contains_task(task_id).await);
}
assert!(!cache.contains_task("task1").await);
assert!(!cache.contains_task("task2").await);
assert!(!cache.contains_task("task3").await);
assert!(!cache.contains_task("nonexistent").await);
assert!(!cache.contains_task("").await);
assert!(!cache.contains_task("large_task").await);
}
#[tokio::test]
async fn test_contains_piece() {
let config = Config {
storage: Storage {
cache_capacity: ByteSize::mib(10),
..Default::default()
},
..Default::default()
};
let mut cache = Cache::new(Arc::new(config));
let test_cases = vec![
// Check non-existent task.
("check", "non_existent", "piece1", "", false),
// Check empty piece ID in non-existent task.
("check", "non_existent", "", "", false),
// Add task and verify empty task behavior.
("add_task", "task1", "", "", true),
("check", "task1", "piece1", "", false),
// Add piece and verify existence.
("add_piece", "task1", "piece1", "test data", true),
("check", "task1", "piece1", "", true),
// Check empty piece ID in existing task.
("check", "task1", "", "", false),
// Check non-existent piece in existing task.
("check", "task1", "non_existent_piece", "", false),
// Test piece ID with special characters.
("add_piece", "task1", "piece#$%^&*", "test data", true),
("check", "task1", "piece#$%^&*", "", true),
];
for (operation, task_id, piece_id, content, expected_result) in test_cases {
match operation {
"check" => {
assert_eq!(
cache.contains_piece(task_id, piece_id).await,
expected_result
);
}
"add_task" => {
cache.put_task(task_id, 1000).await;
assert!(cache.contains_task(task_id).await);
}
"add_piece" => {
cache
.write_piece(task_id, piece_id, Bytes::from(content))
.await
.unwrap();
assert_eq!(
cache.contains_piece(task_id, piece_id).await,
expected_result
);
}
_ => panic!("Unknown operation."),
}
}
}
#[tokio::test]
async fn test_write_piece() {
let config = Config {
storage: Storage {
cache_capacity: ByteSize::mib(10),
..Default::default()
},
..Default::default()
};
let mut cache = Cache::new(Arc::new(config));
// Test writing to non-existent task.
let test_data = b"test data".to_vec();
let result = cache
.write_piece("non_existent", "piece1", Bytes::from(test_data))
.await;
assert!(matches!(result, Err(Error::TaskNotFound(_))));
// Create a task for testing.
cache.put_task("task1", ByteSize::mib(1).as_u64()).await;
assert!(cache.contains_task("task1").await);
let test_cases = vec![
("piece1", b"hello world".to_vec()),
("piece2", b"rust programming".to_vec()),
("piece3", b"dragonfly cache".to_vec()),
("piece4", b"unit testing".to_vec()),
("piece5", b"async await".to_vec()),
("piece6", b"error handling".to_vec()),
("piece7", vec![0u8; 1024]),
("piece8", vec![1u8; 2048]),
];
for (piece_id, content) in &test_cases {
let result = cache
.write_piece("task1", piece_id, Bytes::copy_from_slice(content))
.await;
assert!(result.is_ok());
assert!(cache.contains_piece("task1", piece_id).await);
let piece = Piece {
number: 0,
offset: 0,
length: content.len() as u64,
digest: "".to_string(),
parent_id: None,
uploading_count: 0,
uploaded_count: 0,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
finished_at: None,
};
let mut reader = cache
.read_piece("task1", piece_id, piece, None)
.await
.unwrap();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
assert_eq!(buffer, *content);
}
// Test attempting to overwrite existing pieces.
// The write should succeed (return Ok) but content should not change.
for (piece_id, original_content) in &test_cases {
let new_content = format!("updated content for {}", piece_id);
let result = cache
.write_piece("task1", piece_id, Bytes::from(new_content))
.await;
assert!(result.is_ok());
// Verify content remains unchanged.
let piece = Piece {
number: 0,
offset: 0,
length: original_content.len() as u64,
digest: "".to_string(),
parent_id: None,
uploading_count: 0,
uploaded_count: 0,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
finished_at: None,
};
let mut reader = cache
.read_piece("task1", piece_id, piece, None)
.await
.unwrap();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
assert_eq!(buffer, *original_content);
}
}
#[tokio::test]
async fn test_read_piece() {
let config = Config {
storage: Storage {
cache_capacity: ByteSize::mib(100),
..Default::default()
},
..Default::default()
};
let mut cache = Cache::new(Arc::new(config));
let piece = Piece {
number: 0,
offset: 0,
length: 11,
digest: "".to_string(),
parent_id: None,
uploading_count: 0,
uploaded_count: 0,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
finished_at: None,
};
let result = cache
.read_piece("non_existent", "piece1", piece.clone(), None)
.await;
assert!(matches!(result, Err(Error::TaskNotFound(_))));
cache.put_task("task1", ByteSize::mib(50).as_u64()).await;
let result = cache
.read_piece("task1", "non_existent", piece.clone(), None)
.await;
assert!(matches!(result, Err(Error::PieceNotFound(_))));
let test_pieces = vec![
// Small pieces for basic functionality testing.
(
"piece1",
b"hello world".to_vec(),
Piece {
number: 0,
offset: 0,
length: 11,
digest: "".to_string(),
parent_id: None,
uploading_count: 0,
uploaded_count: 0,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
finished_at: None,
},
vec![
(None, b"hello world".to_vec()),
(
Some(Range {
start: 0,
length: 5,
}),
b"hello".to_vec(),
),
],
),
(
"piece2",
b"rust lang".to_vec(),
Piece {
number: 1,
offset: 11,
length: 9,
digest: "".to_string(),
parent_id: None,
uploading_count: 0,
uploaded_count: 0,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
finished_at: None,
},
vec![
(None, b"rust lang".to_vec()),
(
Some(Range {
start: 11,
length: 4,
}),
b"rust".to_vec(),
),
],
),
(
"piece3",
b"unit test".to_vec(),
Piece {
number: 2,
offset: 20,
length: 9,
digest: "".to_string(),
parent_id: None,
uploading_count: 0,
uploaded_count: 0,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
finished_at: None,
},
vec![
(None, b"unit test".to_vec()),
(
Some(Range {
start: 20,
length: 4,
}),
b"unit".to_vec(),
),
],
),
// Large piece for boundary testing.
(
"large_piece",
{
let size = ByteSize::mib(50).as_u64();
(0..size).map(|i| (i % 256) as u8).collect()
},
Piece {
number: 2,
offset: 0,
length: ByteSize::mib(50).as_u64(),
digest: "".to_string(),
parent_id: None,
uploading_count: 0,
uploaded_count: 0,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
finished_at: None,
},
vec![
// Full read.
(
None,
(0..ByteSize::mib(50).as_u64())
.map(|i| (i % 256) as u8)
.collect(),
),
// Read first 1MiB.
(
Some(Range {
start: 0,
length: ByteSize::mib(1).as_u64(),
}),
(0..ByteSize::mib(1).as_u64())
.map(|i| (i % 256) as u8)
.collect(),
),
// Read last 1MiB.
(
Some(Range {
start: ByteSize::mib(49).as_u64(),
length: ByteSize::mib(1).as_u64(),
}),
(ByteSize::mib(49).as_u64()..ByteSize::mib(50).as_u64())
.map(|i| (i % 256) as u8)
.collect(),
),
],
),
];
// Write all pieces.
for (id, content, _, _) in &test_pieces {
cache
.write_piece("task1", id, Bytes::copy_from_slice(content))
.await
.unwrap();
}
// Test all pieces with their read ranges.
for (id, _, piece, ranges) in &test_pieces {
for (range, expected_content) in ranges {
let mut reader = cache
.read_piece("task1", id, piece.clone(), *range)
.await
.unwrap();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
assert_eq!(&buffer, expected_content);
}
}
}
#[tokio::test]
async fn test_concurrent_read_same_piece() {
let config = Config {
storage: Storage {
cache_capacity: ByteSize::mib(10),
..Default::default()
},
..Default::default()
};
let mut cache = Cache::new(Arc::new(config));
cache.put_task("task1", ByteSize::mib(1).as_u64()).await;
let content = b"test data for concurrent read".to_vec();
cache
.write_piece("task1", "piece1", Bytes::from(content.clone()))
.await
.unwrap();
let cache_arc = Arc::new(cache);
let mut join_set = tokio::task::JoinSet::new();
// Spawn concurrent readers.
for i in 0..50 {
let cache_clone = cache_arc.clone();
let expected_content = content.clone();
join_set.spawn(async move {
let piece = Piece {
number: 0,
offset: 0,
length: expected_content.len() as u64,
digest: "".to_string(),
parent_id: None,
uploading_count: 0,
uploaded_count: 0,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
finished_at: None,
};
let range = if i % 2 == 0 {
None
} else {
Some(Range {
start: 0,
length: 5,
})
};
let mut reader = cache_clone
.read_piece("task1", "piece1", piece, range)
.await
.unwrap_or_else(|e| panic!("Reader {} failed: {:?}.", i, e));
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
if let Some(range) = range {
assert_eq!(buffer, &expected_content[..range.length as usize]);
} else {
assert_eq!(buffer, expected_content);
}
});
}
while let Some(result) = join_set.join_next().await {
assert!(result.is_ok());
}
}
#[tokio::test]
async fn test_concurrent_write_different_pieces() {
let config = Config {
storage: Storage {
cache_capacity: ByteSize::mib(10),
..Default::default()
},
..Default::default()
};
let mut cache = Cache::new(Arc::new(config));
cache.put_task("task1", ByteSize::mib(1).as_u64()).await;
let cache_arc = Arc::new(cache);
let mut join_set = tokio::task::JoinSet::new();
// Spawn concurrent writers.
for i in 0..50 {
let cache_clone = cache_arc.clone();
let content = format!("content for piece {}", i).into_bytes();
join_set.spawn(async move {
let piece_id = format!("piece{}", i);
let result = cache_clone
.write_piece("task1", &piece_id, Bytes::from(content.clone()))
.await;
assert!(result.is_ok());
let piece = Piece {
number: 0,
offset: 0,
length: content.len() as u64,
digest: "".to_string(),
parent_id: None,
uploading_count: 0,
uploaded_count: 0,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
finished_at: None,
};
let mut reader = cache_clone
.read_piece("task1", &piece_id, piece, None)
.await
.unwrap();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
assert_eq!(buffer, content);
});
}
while let Some(result) = join_set.join_next().await {
assert!(result.is_ok());
}
}
#[tokio::test]
async fn test_concurrent_write_same_piece() {
let config = Config {
storage: Storage {
cache_capacity: ByteSize::mib(10),
..Default::default()
},
..Default::default()
};
let mut cache = Cache::new(Arc::new(config));
cache.put_task("task1", ByteSize::mib(1).as_u64()).await;
let original_content = b"original content".to_vec();
cache
.write_piece("task1", "piece1", Bytes::from(original_content.clone()))
.await
.unwrap();
let cache_arc = Arc::new(cache);
let mut join_set = tokio::task::JoinSet::new();
// Spawn concurrent writers.
for i in 0..50 {
let cache_clone = cache_arc.clone();
let new_content = format!("new content from writer {}", i).into_bytes();
join_set.spawn(async move {
let result = cache_clone
.write_piece("task1", "piece1", Bytes::from(new_content))
.await;
assert!(result.is_ok());
});
}
while let Some(result) = join_set.join_next().await {
assert!(result.is_ok());
}
let piece = Piece {
number: 0,
offset: 0,
length: original_content.len() as u64,
digest: "".to_string(),
parent_id: None,
uploading_count: 0,
uploaded_count: 0,
updated_at: chrono::Utc::now().naive_utc(),
created_at: chrono::Utc::now().naive_utc(),
finished_at: None,
};
let mut reader = cache_arc
.read_piece("task1", "piece1", piece, None)
.await
.unwrap();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
assert_eq!(buffer, original_content);
}
}

View File

@ -14,10 +14,11 @@
* limitations under the License.
*/
use crc::*;
use bytesize::ByteSize;
use dragonfly_api::common::v2::Range;
use dragonfly_client_config::dfdaemon::Config;
use dragonfly_client_core::Result;
use dragonfly_client_core::{Error, Result};
use dragonfly_client_util::fs::fallocate;
use std::cmp::{max, min};
use std::path::{Path, PathBuf};
use std::sync::Arc;
@ -27,6 +28,7 @@ use tokio::io::{
};
use tokio_util::io::InspectReader;
use tracing::{error, info, instrument, warn};
use walkdir::WalkDir;
/// DEFAULT_CONTENT_DIR is the default directory for store content.
pub const DEFAULT_CONTENT_DIR: &str = "content";
@ -67,7 +69,6 @@ pub struct WritePersistentCacheTaskResponse {
/// Content implements the content storage.
impl Content {
/// new returns a new content.
#[instrument(skip_all)]
pub async fn new(config: Arc<Config>, dir: &Path) -> Result<Content> {
let dir = dir.join(DEFAULT_CONTENT_DIR);
@ -86,12 +87,39 @@ impl Content {
/// available_space returns the available space of the disk.
pub fn available_space(&self) -> Result<u64> {
let dist_threshold = self.config.gc.policy.dist_threshold;
if dist_threshold != ByteSize::default() {
let usage_space = WalkDir::new(&self.dir)
.into_iter()
.filter_map(|entry| entry.ok())
.filter_map(|entry| entry.metadata().ok())
.filter(|metadata| metadata.is_file())
.fold(0, |acc, m| acc + m.len());
if usage_space >= dist_threshold.as_u64() {
warn!(
"usage space {} is greater than dist threshold {}, no need to calculate available space",
usage_space, dist_threshold
);
return Ok(0);
}
return Ok(dist_threshold.as_u64() - usage_space);
}
let stat = fs2::statvfs(&self.dir)?;
Ok(stat.available_space())
}
/// total_space returns the total space of the disk.
pub fn total_space(&self) -> Result<u64> {
// If the dist_threshold is set, return it directly.
let dist_threshold = self.config.gc.policy.dist_threshold;
if dist_threshold != ByteSize::default() {
return Ok(dist_threshold.as_u64());
}
let stat = fs2::statvfs(&self.dir)?;
Ok(stat.total_space())
}
@ -111,68 +139,100 @@ impl Content {
Ok(true)
}
/// hard_link_or_copy_task hard links or copies the task content to the destination.
#[instrument(skip_all)]
pub async fn hard_link_or_copy_task(
/// is_same_dev_inode checks if the source and target are the same device and inode.
async fn is_same_dev_inode<P: AsRef<Path>, Q: AsRef<Path>>(
&self,
task: &crate::metadata::Task,
to: &Path,
) -> Result<()> {
let task_path = self.get_task_path(task.id.as_str());
source: P,
target: Q,
) -> Result<bool> {
let source_metadata = fs::metadata(source).await?;
let target_metadata = fs::metadata(target).await?;
// If the hard link fails, copy the task content to the destination.
fs::remove_file(to).await.unwrap_or_else(|err| {
info!("remove {:?} failed: {}", to, err);
});
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
Ok(source_metadata.dev() == target_metadata.dev()
&& source_metadata.ino() == target_metadata.ino())
}
if let Err(err) = self.hard_link_task(task.id.as_str(), to).await {
warn!("hard link {:?} to {:?} failed: {}", task_path, to, err);
#[cfg(not(unix))]
{
Err(Error::IO(io::Error::new(
io::ErrorKind::Unsupported,
"platform not supported",
)))
}
}
// If the task is empty, no need to copy. Need to open the file to
// ensure the file exists.
if task.is_empty() {
info!("task is empty, no need to copy");
File::create(to).await.inspect_err(|err| {
error!("create {:?} failed: {}", to, err);
})?;
/// is_same_dev_inode_as_task checks if the task and target are the same device and inode.
pub async fn is_same_dev_inode_as_task(&self, task_id: &str, to: &Path) -> Result<bool> {
let task_path = self.get_task_path(task_id);
self.is_same_dev_inode(&task_path, to).await
}
return Ok(());
/// create_task creates a new task content.
///
/// Behavior of `create_task`:
/// 1. If the task already exists, return the task path.
/// 2. If the task does not exist, create the task directory and file.
#[instrument(skip_all)]
pub async fn create_task(&self, task_id: &str, length: u64) -> Result<PathBuf> {
let task_path = self.get_task_path(task_id);
if task_path.exists() {
return Ok(task_path);
}
let task_dir = self.dir.join(DEFAULT_TASK_DIR).join(&task_id[..3]);
fs::create_dir_all(&task_dir).await.inspect_err(|err| {
error!("create {:?} failed: {}", task_dir, err);
})?;
let f = fs::File::create(task_dir.join(task_id))
.await
.inspect_err(|err| {
error!("create {:?} failed: {}", task_dir, err);
})?;
fallocate(&f, length).await.inspect_err(|err| {
error!("fallocate {:?} failed: {}", task_dir, err);
})?;
Ok(task_dir.join(task_id))
}
/// Hard links the task content to the destination.
///
/// Behavior of `hard_link_task`:
/// 1. If the destination exists:
/// 1.1. If the source and destination share the same device and inode, return immediately.
/// 1.2. Otherwise, return an error.
/// 2. If the destination does not exist:
/// 2.1. If the hard link succeeds, return immediately.
/// 2.2. If the hard link fails, copy the task content to the destination once the task is finished, then return immediately.
#[instrument(skip_all)]
pub async fn hard_link_task(&self, task_id: &str, to: &Path) -> Result<()> {
let task_path = self.get_task_path(task_id);
if let Err(err) = fs::hard_link(task_path.clone(), to).await {
if err.kind() == std::io::ErrorKind::AlreadyExists {
if let Ok(true) = self.is_same_dev_inode(&task_path, to).await {
info!("hard already exists, no need to operate");
return Ok(());
}
}
self.copy_task(task.id.as_str(), to)
.await
.inspect_err(|err| {
error!("copy {:?} to {:?} failed: {}", task_path, to, err);
})?;
info!("copy {:?} to {:?} success", task_path, to);
return Ok(());
warn!("hard link {:?} to {:?} failed: {}", task_path, to, err);
return Err(Error::IO(err));
}
info!("hard link {:?} to {:?} success", task_path, to);
Ok(())
}
/// hard_link_task hard links the task content.
#[instrument(skip_all)]
async fn hard_link_task(&self, task_id: &str, link: &Path) -> Result<()> {
fs::hard_link(self.get_task_path(task_id), link).await?;
Ok(())
}
/// copy_task copies the task content to the destination.
#[instrument(skip_all)]
async fn copy_task(&self, task_id: &str, to: &Path) -> Result<()> {
// Ensure the parent directory of the destination exists.
if let Some(parent) = to.parent() {
if !parent.exists() {
fs::create_dir_all(parent).await.inspect_err(|err| {
error!("failed to create directory {:?}: {}", parent, err);
})?;
}
}
pub async fn copy_task(&self, task_id: &str, to: &Path) -> Result<()> {
fs::copy(self.get_task_path(task_id), to).await?;
info!("copy to {:?} success", to);
Ok(())
}
@ -208,7 +268,6 @@ impl Content {
}
/// delete_task deletes the task content.
#[instrument(skip_all)]
pub async fn delete_task(&self, task_id: &str) -> Result<()> {
info!("delete task content: {}", task_id);
let task_path = self.get_task_path(task_id);
@ -294,19 +353,18 @@ impl Content {
Ok((range_reader, reader))
}
/// write_piece_with_crc32_castagnoli writes the piece to the content with crc32 castagnoli.
/// Calculate the hash of the piece by crc32 castagnoli with hardware acceleration.
/// write_piece writes the piece to the content and calculates the hash of the piece by crc32.
#[instrument(skip_all)]
pub async fn write_piece_with_crc32_castagnoli<R: AsyncRead + Unpin + ?Sized>(
pub async fn write_piece<R: AsyncRead + Unpin + ?Sized>(
&self,
task_id: &str,
offset: u64,
expected_length: u64,
reader: &mut R,
) -> Result<WritePieceResponse> {
// Open the file and seek to the offset.
let task_path = self.create_or_get_task_path(task_id).await?;
let task_path = self.get_task_path(task_id);
let mut f = OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(task_path.as_path())
@ -319,16 +377,15 @@ impl Content {
error!("seek {:?} failed: {}", task_path, err);
})?;
// Copy the piece to the file while updating the CRC32 value.
let reader = BufReader::with_capacity(self.config.storage.write_buffer_size, reader);
let crc = Crc::<u32, Table<16>>::new(&CRC_32_ISCSI);
let mut digest = crc.digest();
let mut writer = BufWriter::with_capacity(self.config.storage.write_buffer_size, f);
// Copy the piece to the file while updating the CRC32 value.
let mut hasher = crc32fast::Hasher::new();
let mut tee = InspectReader::new(reader, |bytes| {
digest.update(bytes);
hasher.update(bytes);
});
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);
})?;
@ -337,15 +394,21 @@ impl Content {
error!("flush {:?} failed: {}", task_path, err);
})?;
if length != expected_length {
return Err(Error::Unknown(format!(
"expected length {} but got {}",
expected_length, length
)));
}
// Calculate the hash of the piece.
Ok(WritePieceResponse {
length,
hash: digest.finalize().to_string(),
hash: hasher.finalize().to_string(),
})
}
/// get_task_path returns the task path by task id.
#[instrument(skip_all)]
fn get_task_path(&self, task_id: &str) -> PathBuf {
// The task needs split by the first 3 characters of task id(sha256) to
// avoid too many files in one directory.
@ -353,72 +416,90 @@ impl Content {
self.dir.join(DEFAULT_TASK_DIR).join(sub_dir).join(task_id)
}
/// create_or_get_task_path creates parent directories or returns the task path by task id.
/// is_same_dev_inode_as_persistent_cache_task checks if the persistent cache task and target
/// are the same device and inode.
pub async fn is_same_dev_inode_as_persistent_cache_task(
&self,
task_id: &str,
to: &Path,
) -> Result<bool> {
let task_path = self.get_persistent_cache_task_path(task_id);
self.is_same_dev_inode(&task_path, to).await
}
/// create_persistent_cache_task creates a new persistent cache task content.
///
/// Behavior of `create_persistent_cache_task`:
/// 1. If the persistent cache task already exists, return the persistent cache task path.
/// 2. If the persistent cache task does not exist, create the persistent cache task directory and file.
#[instrument(skip_all)]
async fn create_or_get_task_path(&self, task_id: &str) -> Result<PathBuf> {
let task_dir = self.dir.join(DEFAULT_TASK_DIR).join(&task_id[..3]);
pub async fn create_persistent_cache_task(
&self,
task_id: &str,
length: u64,
) -> Result<PathBuf> {
let task_path = self.get_persistent_cache_task_path(task_id);
if task_path.exists() {
return Ok(task_path);
}
let task_dir = self
.dir
.join(DEFAULT_PERSISTENT_CACHE_TASK_DIR)
.join(&task_id[..3]);
fs::create_dir_all(&task_dir).await.inspect_err(|err| {
error!("create {:?} failed: {}", task_dir, err);
})?;
let f = fs::File::create(task_dir.join(task_id))
.await
.inspect_err(|err| {
error!("create {:?} failed: {}", task_dir, err);
})?;
fallocate(&f, length).await.inspect_err(|err| {
error!("fallocate {:?} failed: {}", task_dir, err);
})?;
Ok(task_dir.join(task_id))
}
/// hard_link_or_copy_persistent_cache_task hard links or copies the task content to the destination.
/// Hard links the persistent cache task content to the destination.
///
/// Behavior of `hard_link_persistent_cache_task`:
/// 1. If the destination exists:
/// 1.1. If the source and destination share the same device and inode, return immediately.
/// 1.2. Otherwise, return an error.
/// 2. If the destination does not exist:
/// 2.1. If the hard link succeeds, return immediately.
/// 2.2. If the hard link fails, copy the persistent cache task content to the destination once the task is finished, then return immediately.
#[instrument(skip_all)]
pub async fn hard_link_or_copy_persistent_cache_task(
&self,
task: &crate::metadata::PersistentCacheTask,
to: &Path,
) -> Result<()> {
// Ensure the parent directory of the destination exists.
if let Some(parent) = to.parent() {
if !parent.exists() {
fs::create_dir_all(parent).await.inspect_err(|err| {
error!("failed to create directory {:?}: {}", parent, err);
})?;
pub async fn hard_link_persistent_cache_task(&self, task_id: &str, to: &Path) -> Result<()> {
let task_path = self.get_persistent_cache_task_path(task_id);
if let Err(err) = fs::hard_link(task_path.clone(), to).await {
if err.kind() == std::io::ErrorKind::AlreadyExists {
if let Ok(true) = self.is_same_dev_inode(&task_path, to).await {
info!("hard already exists, no need to operate");
return Ok(());
}
}
}
// Get the persistent cache task path.
let task_path = self.get_persistent_cache_task_path(task.id.as_str());
// If the hard link fails, copy the task content to the destination.
fs::remove_file(to).await.unwrap_or_else(|err| {
info!("remove {:?} failed: {}", to, err);
});
if let Err(err) = self
.hard_link_persistent_cache_task(task.id.as_str(), to)
.await
{
warn!("hard link {:?} to {:?} failed: {}", task_path, to, err);
// If the persistent cache task is empty, no need to copy. Need to open the file to
// ensure the file exists.
if task.is_empty() {
info!("persistent cache task is empty, no need to copy");
File::create(to).await.inspect_err(|err| {
error!("create {:?} failed: {}", to, err);
})?;
return Ok(());
}
self.copy_persistent_cache_task(task.id.as_str(), to)
.await
.inspect_err(|err| {
error!("copy {:?} to {:?} failed: {}", task_path, to, err);
})?;
info!("copy {:?} to {:?} success", task_path, to);
return Ok(());
return Err(Error::IO(err));
}
info!("hard link {:?} to {:?} success", task_path, to);
Ok(())
}
/// copy_persistent_cache_task copies the persistent cache task content to the destination.
#[instrument(skip_all)]
pub async fn copy_persistent_cache_task(&self, task_id: &str, to: &Path) -> Result<()> {
fs::copy(self.get_persistent_cache_task_path(task_id), to).await?;
info!("copy to {:?} success", to);
Ok(())
}
/// read_persistent_cache_piece reads the persistent cache piece from the content.
#[instrument(skip_all)]
pub async fn read_persistent_cache_piece(
@ -493,23 +574,19 @@ impl Content {
Ok((range_reader, reader))
}
/// write_persistent_cache_piece_with_crc32_castagnoli writes the persistent cache piece to the content with crc32 castagnoli.
/// Calculate the hash of the piece by crc32 castagnoli with hardware acceleration.
/// write_persistent_cache_piece writes the persistent cache piece to the content and
/// calculates the hash of the piece by crc32.
#[instrument(skip_all)]
pub async fn write_persistent_cache_piece_with_crc32_castagnoli<
R: AsyncRead + Unpin + ?Sized,
>(
pub async fn write_persistent_cache_piece<R: AsyncRead + Unpin + ?Sized>(
&self,
task_id: &str,
offset: u64,
expected_length: u64,
reader: &mut R,
) -> Result<WritePieceResponse> {
// Open the file and seek to the offset.
let task_path = self
.create_or_get_persistent_cache_task_path(task_id)
.await?;
let task_path = self.get_persistent_cache_task_path(task_id);
let mut f = OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(task_path.as_path())
@ -522,16 +599,15 @@ impl Content {
error!("seek {:?} failed: {}", task_path, err);
})?;
// Copy the piece to the file while updating the CRC32 value.
let reader = BufReader::with_capacity(self.config.storage.write_buffer_size, reader);
let crc = Crc::<u32, Table<16>>::new(&CRC_32_ISCSI);
let mut digest = crc.digest();
let mut writer = BufWriter::with_capacity(self.config.storage.write_buffer_size, f);
// Copy the piece to the file while updating the CRC32 value.
let mut hasher = crc32fast::Hasher::new();
let mut tee = InspectReader::new(reader, |bytes| {
digest.update(bytes);
hasher.update(bytes);
});
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);
})?;
@ -540,38 +616,21 @@ impl Content {
error!("flush {:?} failed: {}", task_path, err);
})?;
if length != expected_length {
return Err(Error::Unknown(format!(
"expected length {} but got {}",
expected_length, length
)));
}
// Calculate the hash of the piece.
Ok(WritePieceResponse {
length,
hash: digest.finalize().to_string(),
hash: hasher.finalize().to_string(),
})
}
/// hard_link_persistent_cache_task hard links the persistent cache task content.
#[instrument(skip_all)]
async fn hard_link_persistent_cache_task(&self, task_id: &str, link: &Path) -> Result<()> {
fs::hard_link(self.get_persistent_cache_task_path(task_id), link).await?;
Ok(())
}
/// copy_persistent_cache_task copies the persistent cache task content to the destination.
#[instrument(skip_all)]
async fn copy_persistent_cache_task(&self, task_id: &str, to: &Path) -> Result<()> {
// Ensure the parent directory of the destination exists.
if let Some(parent) = to.parent() {
if !parent.exists() {
fs::create_dir_all(parent).await.inspect_err(|err| {
error!("failed to create directory {:?}: {}", parent, err);
})?;
}
}
fs::copy(self.get_persistent_cache_task_path(task_id), to).await?;
Ok(())
}
/// delete_task deletes the persistent cache task content.
#[instrument(skip_all)]
pub async fn delete_persistent_cache_task(&self, task_id: &str) -> Result<()> {
info!("delete persistent cache task content: {}", task_id);
let persistent_cache_task_path = self.get_persistent_cache_task_path(task_id);
@ -584,7 +643,6 @@ impl Content {
}
/// get_persistent_cache_task_path returns the persistent cache task path by task id.
#[instrument(skip_all)]
fn get_persistent_cache_task_path(&self, task_id: &str) -> PathBuf {
// The persistent cache task needs split by the first 3 characters of task id(sha256) to
// avoid too many files in one directory.
@ -593,21 +651,6 @@ impl Content {
.join(&task_id[..3])
.join(task_id)
}
/// create_or_get_persistent_cache_task_path creates parent directories or returns the persistent cache task path by task id.
#[instrument(skip_all)]
async fn create_or_get_persistent_cache_task_path(&self, task_id: &str) -> Result<PathBuf> {
let task_dir = self
.dir
.join(DEFAULT_PERSISTENT_CACHE_TASK_DIR)
.join(&task_id[..3]);
fs::create_dir_all(&task_dir).await.inspect_err(|err| {
error!("create {:?} failed: {}", task_dir, err);
})?;
Ok(task_dir.join(task_id))
}
}
/// calculate_piece_range calculates the target offset and length based on the piece range and
@ -626,6 +669,316 @@ pub fn calculate_piece_range(offset: u64, length: u64, range: Option<Range>) ->
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
use tempfile::tempdir;
#[tokio::test]
async fn test_create_task() {
let temp_dir = tempdir().unwrap();
let config = Arc::new(Config::default());
let content = Content::new(config, temp_dir.path()).await.unwrap();
let task_id = "60409bd0ec44160f44c53c39b3fe1c5fdfb23faded0228c68bee83bc15a200e3";
let task_path = content.create_task(task_id, 0).await.unwrap();
assert!(task_path.exists());
assert_eq!(task_path, temp_dir.path().join("content/tasks/604/60409bd0ec44160f44c53c39b3fe1c5fdfb23faded0228c68bee83bc15a200e3"));
let task_path_exists = content.create_task(task_id, 0).await.unwrap();
assert_eq!(task_path, task_path_exists);
}
#[tokio::test]
async fn test_hard_link_task() {
let temp_dir = tempdir().unwrap();
let config = Arc::new(Config::default());
let content = Content::new(config, temp_dir.path()).await.unwrap();
let task_id = "c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4";
content.create_task(task_id, 0).await.unwrap();
let to = temp_dir
.path()
.join("c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4");
content.hard_link_task(task_id, &to).await.unwrap();
assert!(to.exists());
content.hard_link_task(task_id, &to).await.unwrap();
}
#[tokio::test]
async fn test_copy_task() {
let temp_dir = tempdir().unwrap();
let config = Arc::new(Config::default());
let content = Content::new(config, temp_dir.path()).await.unwrap();
let task_id = "bfd3c02fb31a7373e25b405fd5fd3082987ccfbaf210889153af9e65bbf13002";
content.create_task(task_id, 64).await.unwrap();
let to = temp_dir
.path()
.join("bfd3c02fb31a7373e25b405fd5fd3082987ccfbaf210889153af9e65bbf13002");
content.copy_task(task_id, &to).await.unwrap();
assert!(to.exists());
}
#[tokio::test]
async fn test_delete_task() {
let temp_dir = tempdir().unwrap();
let config = Arc::new(Config::default());
let content = Content::new(config, temp_dir.path()).await.unwrap();
let task_id = "4e19f03b0fceb38f23ff4f657681472a53ef335db3660ae5494912570b7a2bb7";
let task_path = content.create_task(task_id, 0).await.unwrap();
assert!(task_path.exists());
content.delete_task(task_id).await.unwrap();
assert!(!task_path.exists());
}
#[tokio::test]
async fn test_read_piece() {
let temp_dir = tempdir().unwrap();
let config = Arc::new(Config::default());
let content = Content::new(config, temp_dir.path()).await.unwrap();
let task_id = "c794a3bbae81e06d1c8d362509bdd42a7c105b0fb28d80ffe27f94b8f04fc845";
content.create_task(task_id, 13).await.unwrap();
let data = b"hello, world!";
let mut reader = Cursor::new(data);
content
.write_piece(task_id, 0, 13, &mut reader)
.await
.unwrap();
let mut reader = content.read_piece(task_id, 0, 13, None).await.unwrap();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
assert_eq!(buffer, data);
let mut reader = content
.read_piece(
task_id,
0,
13,
Some(Range {
start: 0,
length: 5,
}),
)
.await
.unwrap();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
assert_eq!(buffer, b"hello");
}
#[tokio::test]
async fn test_write_piece() {
let temp_dir = tempdir().unwrap();
let config = Arc::new(Config::default());
let content = Content::new(config, temp_dir.path()).await.unwrap();
let task_id = "60b48845606946cea72084f14ed5cce61ec96e69f80a30f891a6963dccfd5b4f";
content.create_task(task_id, 4).await.unwrap();
let data = b"test";
let mut reader = Cursor::new(data);
let response = content
.write_piece(task_id, 0, 4, &mut reader)
.await
.unwrap();
assert_eq!(response.length, 4);
assert!(!response.hash.is_empty());
}
#[tokio::test]
async fn test_create_persistent_task() {
let temp_dir = tempdir().unwrap();
let config = Arc::new(Config::default());
let content = Content::new(config, temp_dir.path()).await.unwrap();
let task_id = "c4f108ab1d2b8cfdffe89ea9676af35123fa02e3c25167d62538f630d5d44745";
let task_path = content
.create_persistent_cache_task(task_id, 0)
.await
.unwrap();
assert!(task_path.exists());
assert_eq!(task_path, temp_dir.path().join("content/persistent-cache-tasks/c4f/c4f108ab1d2b8cfdffe89ea9676af35123fa02e3c25167d62538f630d5d44745"));
let task_path_exists = content
.create_persistent_cache_task(task_id, 0)
.await
.unwrap();
assert_eq!(task_path, task_path_exists);
}
#[tokio::test]
async fn test_hard_link_persistent_cache_task() {
let temp_dir = tempdir().unwrap();
let config = Arc::new(Config::default());
let content = Content::new(config, temp_dir.path()).await.unwrap();
let task_id = "5e81970eb2b048910cc84cab026b951f2ceac0a09c72c0717193bb6e466e11cd";
content
.create_persistent_cache_task(task_id, 0)
.await
.unwrap();
let to = temp_dir
.path()
.join("5e81970eb2b048910cc84cab026b951f2ceac0a09c72c0717193bb6e466e11cd");
content
.hard_link_persistent_cache_task(task_id, &to)
.await
.unwrap();
assert!(to.exists());
content
.hard_link_persistent_cache_task(task_id, &to)
.await
.unwrap();
}
#[tokio::test]
async fn test_copy_persistent_cache_task() {
let temp_dir = tempdir().unwrap();
let config = Arc::new(Config::default());
let content = Content::new(config, temp_dir.path()).await.unwrap();
let task_id = "194b9c2018429689fb4e596a506c7e9db564c187b9709b55b33b96881dfb6dd5";
content
.create_persistent_cache_task(task_id, 64)
.await
.unwrap();
let to = temp_dir
.path()
.join("194b9c2018429689fb4e596a506c7e9db564c187b9709b55b33b96881dfb6dd5");
content
.copy_persistent_cache_task(task_id, &to)
.await
.unwrap();
assert!(to.exists());
}
#[tokio::test]
async fn test_delete_persistent_cache_task() {
let temp_dir = tempdir().unwrap();
let config = Arc::new(Config::default());
let content = Content::new(config, temp_dir.path()).await.unwrap();
let task_id = "17430ba545c3ce82790e9c9f77e64dca44bb6d6a0c9e18be175037c16c73713d";
let task_path = content
.create_persistent_cache_task(task_id, 0)
.await
.unwrap();
assert!(task_path.exists());
content.delete_persistent_cache_task(task_id).await.unwrap();
assert!(!task_path.exists());
}
#[tokio::test]
async fn test_read_persistent_cache_piece() {
let temp_dir = tempdir().unwrap();
let config = Arc::new(Config::default());
let content = Content::new(config, temp_dir.path()).await.unwrap();
let task_id = "9cb27a4af09aee4eb9f904170217659683f4a0ea7cd55e1a9fbcb99ddced659a";
content
.create_persistent_cache_task(task_id, 13)
.await
.unwrap();
let data = b"hello, world!";
let mut reader = Cursor::new(data);
content
.write_persistent_cache_piece(task_id, 0, 13, &mut reader)
.await
.unwrap();
let mut reader = content
.read_persistent_cache_piece(task_id, 0, 13, None)
.await
.unwrap();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
assert_eq!(buffer, data);
let mut reader = content
.read_persistent_cache_piece(
task_id,
0,
13,
Some(Range {
start: 0,
length: 5,
}),
)
.await
.unwrap();
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).await.unwrap();
assert_eq!(buffer, b"hello");
}
#[tokio::test]
async fn test_write_persistent_cache_piece() {
let temp_dir = tempdir().unwrap();
let config = Arc::new(Config::default());
let content = Content::new(config, temp_dir.path()).await.unwrap();
let task_id = "ca1afaf856e8a667fbd48093ca3ca1b8eeb4bf735912fbe551676bc5817a720a";
content
.create_persistent_cache_task(task_id, 4)
.await
.unwrap();
let data = b"test";
let mut reader = Cursor::new(data);
let response = content
.write_persistent_cache_piece(task_id, 0, 4, &mut reader)
.await
.unwrap();
assert_eq!(response.length, 4);
assert!(!response.hash.is_empty());
}
#[tokio::test]
async fn test_has_enough_space() {
let config = Arc::new(Config::default());
let temp_dir = tempdir().unwrap();
let content = Content::new(config, temp_dir.path()).await.unwrap();
let has_space = content.has_enough_space(1).unwrap();
assert!(has_space);
let has_space = content.has_enough_space(u64::MAX).unwrap();
assert!(!has_space);
let mut config = Config::default();
config.gc.policy.dist_threshold = ByteSize::mib(10);
let config = Arc::new(config);
let content = Content::new(config, temp_dir.path()).await.unwrap();
let file_path = Path::new(temp_dir.path())
.join(DEFAULT_CONTENT_DIR)
.join(DEFAULT_TASK_DIR)
.join("1mib");
let mut file = File::create(&file_path).await.unwrap();
let buffer = vec![0u8; ByteSize::mib(1).as_u64() as usize];
file.write_all(&buffer).await.unwrap();
file.flush().await.unwrap();
let has_space = content
.has_enough_space(ByteSize::mib(9).as_u64() + 1)
.unwrap();
assert!(!has_space);
let has_space = content.has_enough_space(ByteSize::mib(9).as_u64()).unwrap();
assert!(has_space);
}
#[tokio::test]
async fn test_calculate_piece_range() {
@ -700,17 +1053,4 @@ mod tests {
assert_eq!(target_length, expected_length);
}
}
#[tokio::test]
async fn test_has_enough_space() {
let config = Arc::new(Config::default());
let dir = PathBuf::from("/tmp/dragonfly_test");
let content = Content::new(config, &dir).await.unwrap();
let has_space = content.has_enough_space(1).unwrap();
assert!(has_space);
let has_space = content.has_enough_space(u64::MAX).unwrap();
assert!(!has_space);
}
}

View File

@ -25,8 +25,11 @@ use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use tokio::io::AsyncRead;
use tracing::{debug, error, instrument, warn};
use tokio::time::sleep;
use tokio_util::either::Either;
use tracing::{debug, error, info, instrument, warn};
pub mod cache;
pub mod content;
pub mod metadata;
pub mod storage_engine;
@ -44,19 +47,24 @@ pub struct Storage {
/// content implements the content storage.
content: content::Content,
/// cache implements the cache storage.
cache: cache::Cache,
}
/// Storage implements the storage.
impl Storage {
/// new returns a new storage.
#[instrument(skip_all)]
pub async fn new(config: Arc<Config>, dir: &Path, log_dir: PathBuf) -> Result<Self> {
let metadata = metadata::Metadata::new(config.clone(), dir, &log_dir)?;
let content = content::Content::new(config.clone(), dir).await?;
let cache = cache::Cache::new(config.clone());
Ok(Storage {
config,
metadata,
content,
cache,
})
}
@ -75,23 +83,48 @@ impl Storage {
self.content.has_enough_space(content_length)
}
/// hard_link_or_copy_task hard links or copies the task content to the destination.
/// hard_link_task hard links the task content to the destination.
#[instrument(skip_all)]
pub async fn hard_link_or_copy_task(&self, task: &metadata::Task, to: &Path) -> Result<()> {
self.content.hard_link_or_copy_task(task, to).await
pub async fn hard_link_task(&self, task_id: &str, to: &Path) -> Result<()> {
self.content.hard_link_task(task_id, to).await
}
/// download_task_started updates the metadata of the task when the task downloads started.
/// copy_task copies the task content to the destination.
#[instrument(skip_all)]
pub fn download_task_started(
pub async fn copy_task(&self, id: &str, to: &Path) -> Result<()> {
self.content.copy_task(id, to).await
}
/// is_same_dev_inode_as_task checks if the task content is on the same device inode as the
/// destination.
pub async fn is_same_dev_inode_as_task(&self, id: &str, to: &Path) -> Result<bool> {
self.content.is_same_dev_inode_as_task(id, to).await
}
/// prepare_download_task_started prepares the metadata of the task when the task downloads
/// started.
pub async fn prepare_download_task_started(&self, id: &str) -> Result<metadata::Task> {
self.metadata.download_task_started(id, None, None, None)
}
/// download_task_started updates the metadata of the task and create task content
/// when the task downloads started.
#[instrument(skip_all)]
pub async fn download_task_started(
&self,
id: &str,
piece_length: Option<u64>,
content_length: Option<u64>,
piece_length: u64,
content_length: u64,
response_header: Option<HeaderMap>,
) -> Result<metadata::Task> {
self.metadata
.download_task_started(id, piece_length, content_length, response_header)
self.content.create_task(id, content_length).await?;
self.metadata.download_task_started(
id,
Some(piece_length),
Some(content_length),
response_header,
)
}
/// download_task_finished updates the metadata of the task when the task downloads finished.
@ -156,17 +189,36 @@ impl Storage {
self.content.delete_task(id).await.unwrap_or_else(|err| {
error!("delete task content failed: {}", err);
});
let mut cache = self.cache.clone();
cache.delete_task(id).await.unwrap_or_else(|err| {
info!("delete task from cache failed: {}", err);
});
}
/// hard_link_or_copy_persistent_cache_task hard links or copies the persistent cache task content to the destination.
/// hard_link_persistent_cache_task hard links the persistent cache task content to the destination.
#[instrument(skip_all)]
pub async fn hard_link_or_copy_persistent_cache_task(
&self,
task: &metadata::PersistentCacheTask,
to: &Path,
) -> Result<()> {
pub async fn hard_link_persistent_cache_task(&self, task_id: &str, to: &Path) -> Result<()> {
self.content
.hard_link_or_copy_persistent_cache_task(task, to)
.hard_link_persistent_cache_task(task_id, to)
.await
}
/// copy_taskcopy_persistent_cache_taskcopies the persistent cache task content to the destination.
#[instrument(skip_all)]
pub async fn copy_persistent_cache_task(&self, id: &str, to: &Path) -> Result<()> {
self.content.copy_persistent_cache_task(id, to).await
}
/// is_same_dev_inode_as_persistent_cache_task checks if the persistent cache task content is on the same device inode as the
/// destination.
pub async fn is_same_dev_inode_as_persistent_cache_task(
&self,
id: &str,
to: &Path,
) -> Result<bool> {
self.content
.is_same_dev_inode_as_persistent_cache_task(id, to)
.await
}
@ -179,8 +231,17 @@ impl Storage {
piece_length: u64,
content_length: u64,
) -> Result<metadata::PersistentCacheTask> {
self.metadata
.create_persistent_cache_task_started(id, ttl, piece_length, content_length)
let metadata = self.metadata.create_persistent_cache_task_started(
id,
ttl,
piece_length,
content_length,
)?;
self.content
.create_persistent_cache_task(id, content_length)
.await?;
Ok(metadata)
}
/// create_persistent_cache_task_finished updates the metadata of the persistent cache task
@ -201,9 +262,9 @@ impl Storage {
}
/// download_persistent_cache_task_started updates the metadata of the persistent cache task
/// when the persistent cache task downloads started.
/// and creates the persistent cache task content when the persistent cache task downloads started.
#[instrument(skip_all)]
pub fn download_persistent_cache_task_started(
pub async fn download_persistent_cache_task_started(
&self,
id: &str,
ttl: Duration,
@ -212,14 +273,19 @@ impl Storage {
content_length: u64,
created_at: NaiveDateTime,
) -> Result<metadata::PersistentCacheTask> {
self.metadata.download_persistent_cache_task_started(
let metadata = self.metadata.download_persistent_cache_task_started(
id,
ttl,
persistent,
piece_length,
content_length,
created_at,
)
)?;
self.content
.create_persistent_cache_task(id, content_length)
.await?;
Ok(metadata)
}
/// download_persistent_cache_task_finished updates the metadata of the persistent cache task when the persistent cache task downloads finished.
@ -310,7 +376,7 @@ impl Storage {
) -> Result<metadata::Piece> {
let response = self
.content
.write_persistent_cache_piece_with_crc32_castagnoli(task_id, offset, reader)
.write_persistent_cache_piece(task_id, offset, length, reader)
.await?;
let digest = Digest::new(Algorithm::Crc32, response.hash);
@ -340,6 +406,7 @@ impl Storage {
}
/// download_piece_from_source_finished is used for downloading piece from source.
#[allow(clippy::too_many_arguments)]
#[instrument(skip_all)]
pub async fn download_piece_from_source_finished<R: AsyncRead + Unpin + ?Sized>(
&self,
@ -348,13 +415,34 @@ impl Storage {
offset: u64,
length: u64,
reader: &mut R,
timeout: Duration,
) -> Result<metadata::Piece> {
tokio::select! {
piece = self.handle_downloaded_from_source_finished(piece_id, task_id, offset, length, reader) => {
piece
}
_ = sleep(timeout) => {
Err(Error::DownloadPieceFinished(piece_id.to_string()))
}
}
}
// handle_downloaded_from_source_finished handles the downloaded piece from source.
#[instrument(skip_all)]
async fn handle_downloaded_from_source_finished<R: AsyncRead + Unpin + ?Sized>(
&self,
piece_id: &str,
task_id: &str,
offset: u64,
length: u64,
reader: &mut R,
) -> Result<metadata::Piece> {
let response = self
.content
.write_piece_with_crc32_castagnoli(task_id, offset, reader)
.write_piece(task_id, offset, length, reader)
.await?;
let digest = Digest::new(Algorithm::Crc32, response.hash);
let digest = Digest::new(Algorithm::Crc32, response.hash);
self.metadata.download_piece_finished(
piece_id,
offset,
@ -365,19 +453,45 @@ impl Storage {
}
/// download_piece_from_parent_finished is used for downloading piece from parent.
#[allow(clippy::too_many_arguments)]
#[instrument(skip_all)]
pub async fn download_piece_from_parent_finished<R: AsyncRead + Unpin + ?Sized>(
&self,
piece_id: &str,
task_id: &str,
offset: u64,
length: u64,
expected_digest: &str,
parent_id: &str,
reader: &mut R,
timeout: Duration,
) -> Result<metadata::Piece> {
tokio::select! {
piece = self.handle_downloaded_piece_from_parent_finished(piece_id, task_id, offset, length, expected_digest, parent_id, reader) => {
piece
}
_ = sleep(timeout) => {
Err(Error::DownloadPieceFinished(piece_id.to_string()))
}
}
}
// handle_downloaded_piece_from_parent_finished handles the downloaded piece from parent.
#[allow(clippy::too_many_arguments)]
#[instrument(skip_all)]
async fn handle_downloaded_piece_from_parent_finished<R: AsyncRead + Unpin + ?Sized>(
&self,
piece_id: &str,
task_id: &str,
offset: u64,
length: u64,
expected_digest: &str,
parent_id: &str,
reader: &mut R,
) -> Result<metadata::Piece> {
let response = self
.content
.write_piece_with_crc32_castagnoli(task_id, offset, reader)
.write_piece(task_id, offset, length, reader)
.await?;
let length = response.length;
@ -424,6 +538,24 @@ impl Storage {
// Get the piece metadata and return the content of the piece.
match self.metadata.get_piece(piece_id) {
Ok(Some(piece)) => {
if self.cache.contains_piece(task_id, piece_id).await {
match self
.cache
.read_piece(task_id, piece_id, piece.clone(), range)
.await
{
Ok(reader) => {
// Finish uploading the task.
self.metadata.upload_task_finished(task_id)?;
debug!("get piece from cache: {}", piece_id);
return Ok(Either::Left(reader));
}
Err(err) => {
return Err(err);
}
}
}
match self
.content
.read_piece(task_id, piece.offset, piece.length, range)
@ -432,55 +564,7 @@ impl Storage {
Ok(reader) => {
// Finish uploading the task.
self.metadata.upload_task_finished(task_id)?;
Ok(reader)
}
Err(err) => {
// Failed uploading the task.
self.metadata.upload_task_failed(task_id)?;
Err(err)
}
}
}
Ok(None) => {
// Failed uploading the task.
self.metadata.upload_task_failed(task_id)?;
Err(Error::PieceNotFound(piece_id.to_string()))
}
Err(err) => {
// Failed uploading the task.
self.metadata.upload_task_failed(task_id)?;
Err(err)
}
}
}
/// upload_piece_with_dual_read returns the dual reader of the piece, one is the range reader, and the other is the
/// full reader of the piece. It is used for cache the piece content to the proxy cache.
#[instrument(skip_all)]
pub async fn upload_piece_with_dual_read(
&self,
piece_id: &str,
task_id: &str,
range: Option<Range>,
) -> Result<(impl AsyncRead, impl AsyncRead)> {
// Wait for the piece to be finished.
self.wait_for_piece_finished(piece_id).await?;
// Start uploading the task.
self.metadata.upload_task_started(task_id)?;
// Get the piece metadata and return the content of the piece.
match self.metadata.get_piece(piece_id) {
Ok(Some(piece)) => {
match self
.content
.read_piece_with_dual_read(task_id, piece.offset, piece.length, range)
.await
{
Ok(dual_reader) => {
// Finish uploading the task.
self.metadata.upload_task_finished(task_id)?;
Ok(dual_reader)
Ok(Either::Right(reader))
}
Err(err) => {
// Failed uploading the task.
@ -503,7 +587,6 @@ impl Storage {
}
/// get_piece returns the piece metadata.
#[instrument(skip_all)]
pub fn get_piece(&self, piece_id: &str) -> Result<Option<metadata::Piece>> {
self.metadata.get_piece(piece_id)
}
@ -515,13 +598,13 @@ impl Storage {
}
/// get_pieces returns the piece metadatas.
#[instrument(skip_all)]
pub fn get_pieces(&self, task_id: &str) -> Result<Vec<metadata::Piece>> {
self.metadata.get_pieces(task_id)
}
/// piece_id returns the piece id.
#[inline]
#[instrument(skip_all)]
pub fn piece_id(&self, task_id: &str, number: u32) -> String {
self.metadata.piece_id(task_id, number)
}
@ -546,6 +629,7 @@ impl Storage {
}
/// download_persistent_cache_piece_from_parent_finished is used for downloading persistent cache piece from parent.
#[allow(clippy::too_many_arguments)]
#[instrument(skip_all)]
pub async fn download_persistent_cache_piece_from_parent_finished<
R: AsyncRead + Unpin + ?Sized,
@ -554,13 +638,14 @@ impl Storage {
piece_id: &str,
task_id: &str,
offset: u64,
length: u64,
expected_digest: &str,
parent_id: &str,
reader: &mut R,
) -> Result<metadata::Piece> {
let response = self
.content
.write_persistent_cache_piece_with_crc32_castagnoli(task_id, offset, reader)
.write_persistent_cache_piece(task_id, offset, length, reader)
.await?;
let length = response.length;
@ -659,7 +744,6 @@ impl Storage {
/// persistent_cache_piece_id returns the persistent cache piece id.
#[inline]
#[instrument(skip_all)]
pub fn persistent_cache_piece_id(&self, task_id: &str, number: u32) -> String {
self.metadata.piece_id(task_id, number)
}
@ -667,12 +751,12 @@ impl Storage {
/// wait_for_piece_finished waits for the piece to be finished.
#[instrument(skip_all)]
async fn wait_for_piece_finished(&self, piece_id: &str) -> Result<metadata::Piece> {
// Initialize the timeout of piece.
let piece_timeout = tokio::time::sleep(self.config.download.piece_timeout);
tokio::pin!(piece_timeout);
// Total timeout for downloading a piece, combining the download time and the time to write to storage.
let wait_timeout = tokio::time::sleep(
self.config.download.piece_timeout + self.config.storage.write_piece_timeout,
);
tokio::pin!(wait_timeout);
// Initialize the interval of piece.
let mut wait_for_piece_count = 0;
let mut interval = tokio::time::interval(DEFAULT_WAIT_FOR_PIECE_FINISHED_INTERVAL);
loop {
tokio::select! {
@ -686,13 +770,8 @@ impl Storage {
debug!("wait piece finished success");
return Ok(piece);
}
if wait_for_piece_count > 0 {
debug!("wait piece finished");
}
wait_for_piece_count += 1;
}
_ = &mut piece_timeout => {
_ = &mut wait_timeout => {
self.metadata.wait_for_piece_finished_failed(piece_id).unwrap_or_else(|err| error!("delete piece metadata failed: {}", err));
return Err(Error::WaitForPieceFinishedTimeout(piece_id.to_string()));
}
@ -706,12 +785,12 @@ impl Storage {
&self,
piece_id: &str,
) -> Result<metadata::Piece> {
// Initialize the timeout of piece.
let piece_timeout = tokio::time::sleep(self.config.download.piece_timeout);
tokio::pin!(piece_timeout);
// Total timeout for downloading a piece, combining the download time and the time to write to storage.
let wait_timeout = tokio::time::sleep(
self.config.download.piece_timeout + self.config.storage.write_piece_timeout,
);
tokio::pin!(wait_timeout);
// Initialize the interval of piece.
let mut wait_for_piece_count = 0;
let mut interval = tokio::time::interval(DEFAULT_WAIT_FOR_PIECE_FINISHED_INTERVAL);
loop {
tokio::select! {
@ -725,13 +804,8 @@ impl Storage {
debug!("wait piece finished success");
return Ok(piece);
}
if wait_for_piece_count > 0 {
debug!("wait piece finished");
}
wait_for_piece_count += 1;
}
_ = &mut piece_timeout => {
_ = &mut wait_timeout => {
self.metadata.wait_for_piece_finished_failed(piece_id).unwrap_or_else(|err| error!("delete piece metadata failed: {}", err));
return Err(Error::WaitForPieceFinishedTimeout(piece_id.to_string()));
}

View File

@ -15,11 +15,9 @@
*/
use chrono::{NaiveDateTime, Utc};
use crc::*;
use dragonfly_client_config::dfdaemon::Config;
use dragonfly_client_core::{Error, Result};
use dragonfly_client_util::{digest, http::headermap_to_hashmap};
use rayon::prelude::*;
use reqwest::header::HeaderMap;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
@ -307,14 +305,13 @@ impl Piece {
/// offset, length and content digest. The digest is used to check the integrity of the
/// piece metadata.
pub fn calculate_digest(&self) -> String {
let crc = Crc::<u32, Table<16>>::new(&CRC_32_ISCSI);
let mut digest = crc.digest();
digest.update(&self.number.to_be_bytes());
digest.update(&self.offset.to_be_bytes());
digest.update(&self.length.to_be_bytes());
digest.update(self.digest.as_bytes());
let mut hasher = crc32fast::Hasher::new();
hasher.update(&self.number.to_be_bytes());
hasher.update(&self.offset.to_be_bytes());
hasher.update(&self.length.to_be_bytes());
hasher.update(self.digest.as_bytes());
let encoded = digest.finalize().to_string();
let encoded = hasher.finalize().to_string();
digest::Digest::new(digest::Algorithm::Crc32, encoded).to_string()
}
}
@ -529,7 +526,7 @@ impl<E: StorageEngineOwned> Metadata<E> {
.collect::<Result<Vec<Box<[u8]>>>>()?;
tasks
.par_iter()
.iter()
.map(|task| Task::deserialize_from(task))
.collect()
}
@ -843,7 +840,6 @@ impl<E: StorageEngineOwned> Metadata<E> {
}
/// get_piece gets the piece metadata.
#[instrument(skip_all)]
pub fn get_piece(&self, piece_id: &str) -> Result<Option<Piece>> {
self.db.get(piece_id.as_bytes())
}
@ -855,6 +851,7 @@ impl<E: StorageEngineOwned> Metadata<E> {
}
/// get_pieces gets the piece metadatas.
#[instrument(skip_all)]
pub fn get_pieces(&self, task_id: &str) -> Result<Vec<Piece>> {
let pieces = self
.db
@ -866,7 +863,7 @@ impl<E: StorageEngineOwned> Metadata<E> {
.collect::<Result<Vec<Box<[u8]>>>>()?;
pieces
.par_iter()
.iter()
.map(|piece| Piece::deserialize_from(piece))
.collect()
}
@ -891,7 +888,7 @@ impl<E: StorageEngineOwned> Metadata<E> {
.collect::<Result<Vec<Box<[u8]>>>>()?;
let piece_ids_refs = piece_ids
.par_iter()
.iter()
.map(|id| {
let id_ref = id.as_ref();
info!(
@ -909,7 +906,6 @@ impl<E: StorageEngineOwned> Metadata<E> {
/// piece_id returns the piece id.
#[inline]
#[instrument(skip_all)]
pub fn piece_id(&self, task_id: &str, number: u32) -> String {
format!("{}-{}", task_id, number)
}
@ -942,7 +938,7 @@ impl Metadata<RocksdbStorageEngine> {
#[cfg(test)]
mod tests {
use super::*;
use tempdir::TempDir;
use tempfile::tempdir;
#[test]
fn test_calculate_digest() {
@ -950,17 +946,17 @@ mod tests {
number: 1,
offset: 0,
length: 1024,
digest: "crc32:3810626145".to_string(),
digest: "crc32:1929153120".to_string(),
..Default::default()
};
let digest = piece.calculate_digest();
assert_eq!(digest, "crc32:3874114958");
assert_eq!(digest, "crc32:3299754941");
}
#[test]
fn should_create_metadata() {
let dir = TempDir::new("metadata").unwrap();
let dir = tempdir().unwrap();
let log_dir = dir.path().join("log");
let metadata = Metadata::new(Arc::new(Config::default()), dir.path(), &log_dir).unwrap();
assert!(metadata.get_tasks().unwrap().is_empty());
@ -972,7 +968,7 @@ mod tests {
#[test]
fn test_task_lifecycle() {
let dir = TempDir::new("metadata").unwrap();
let dir = tempdir().unwrap();
let log_dir = dir.path().join("log");
let metadata = Metadata::new(Arc::new(Config::default()), dir.path(), &log_dir).unwrap();
let task_id = "d3c4e940ad06c47fc36ac67801e6f8e36cb400e2391708620bc7e865b102062c";
@ -1032,7 +1028,7 @@ mod tests {
#[test]
fn test_piece_lifecycle() {
let dir = TempDir::new("metadata").unwrap();
let dir = tempdir().unwrap();
let log_dir = dir.path().join("log");
let metadata = Metadata::new(Arc::new(Config::default()), dir.path(), &log_dir).unwrap();
let task_id = "d3c4e940ad06c47fc36ac67801e6f8e36cb400e2391708620bc7e865b102062c";

View File

@ -65,6 +65,7 @@ pub trait Operations {
fn iter<O: DatabaseObject>(&self) -> Result<impl Iterator<Item = Result<(Box<[u8]>, O)>>>;
/// iter_raw iterates all objects without serialization.
#[allow(clippy::type_complexity)]
fn iter_raw<O: DatabaseObject>(
&self,
) -> Result<impl Iterator<Item = Result<(Box<[u8]>, Box<[u8]>)>>>;
@ -76,6 +77,7 @@ pub trait Operations {
) -> Result<impl Iterator<Item = Result<(Box<[u8]>, O)>>>;
/// prefix_iter_raw iterates all objects with prefix without serialization.
#[allow(clippy::type_complexity)]
fn prefix_iter_raw<O: DatabaseObject>(
&self,
prefix: &[u8],

View File

@ -24,7 +24,7 @@ use std::{
ops::Deref,
path::{Path, PathBuf},
};
use tracing::{info, instrument, warn};
use tracing::{info, warn};
/// RocksdbStorageEngine is a storage engine based on rocksdb.
pub struct RocksdbStorageEngine {
@ -67,7 +67,6 @@ impl RocksdbStorageEngine {
const DEFAULT_LOG_MAX_FILES: usize = 10;
/// open opens a rocksdb storage engine with the given directory and column families.
#[instrument(skip_all)]
pub fn open(dir: &Path, log_dir: &PathBuf, cf_names: &[&str], keep: bool) -> Result<Self> {
info!("initializing metadata directory: {:?} {:?}", dir, cf_names);
// Initialize rocksdb options.
@ -135,7 +134,6 @@ impl RocksdbStorageEngine {
/// RocksdbStorageEngine implements the storage engine operations.
impl Operations for RocksdbStorageEngine {
/// get gets the object by key.
#[instrument(skip_all)]
fn get<O: DatabaseObject>(&self, key: &[u8]) -> Result<Option<O>> {
let cf = cf_handle::<O>(self)?;
let value = self.get_cf(cf, key).or_err(ErrorType::StorageError)?;
@ -146,7 +144,6 @@ impl Operations for RocksdbStorageEngine {
}
/// is_exist checks if the object exists by key.
#[instrument(skip_all)]
fn is_exist<O: DatabaseObject>(&self, key: &[u8]) -> Result<bool> {
let cf = cf_handle::<O>(self)?;
Ok(self
@ -156,7 +153,6 @@ impl Operations for RocksdbStorageEngine {
}
/// put puts the object by key.
#[instrument(skip_all)]
fn put<O: DatabaseObject>(&self, key: &[u8], value: &O) -> Result<()> {
let cf = cf_handle::<O>(self)?;
self.put_cf(cf, key, value.serialized()?)
@ -165,7 +161,6 @@ impl Operations for RocksdbStorageEngine {
}
/// delete deletes the object by key.
#[instrument(skip_all)]
fn delete<O: DatabaseObject>(&self, key: &[u8]) -> Result<()> {
let cf = cf_handle::<O>(self)?;
let mut options = WriteOptions::default();
@ -177,7 +172,6 @@ impl Operations for RocksdbStorageEngine {
}
/// iter iterates all objects.
#[instrument(skip_all)]
fn iter<O: DatabaseObject>(&self) -> Result<impl Iterator<Item = Result<(Box<[u8]>, O)>>> {
let cf = cf_handle::<O>(self)?;
let iter = self.iterator_cf(cf, rocksdb::IteratorMode::Start);
@ -188,7 +182,6 @@ impl Operations for RocksdbStorageEngine {
}
/// iter_raw iterates all objects without serialization.
#[instrument(skip_all)]
fn iter_raw<O: DatabaseObject>(
&self,
) -> Result<impl Iterator<Item = Result<(Box<[u8]>, Box<[u8]>)>>> {
@ -202,7 +195,6 @@ impl Operations for RocksdbStorageEngine {
}
/// prefix_iter iterates all objects with prefix.
#[instrument(skip_all)]
fn prefix_iter<O: DatabaseObject>(
&self,
prefix: &[u8],
@ -216,7 +208,6 @@ impl Operations for RocksdbStorageEngine {
}
/// prefix_iter_raw iterates all objects with prefix without serialization.
#[instrument(skip_all)]
fn prefix_iter_raw<O: DatabaseObject>(
&self,
prefix: &[u8],
@ -229,7 +220,6 @@ impl Operations for RocksdbStorageEngine {
}
/// batch_delete deletes objects by keys.
#[instrument(skip_all)]
fn batch_delete<O: DatabaseObject>(&self, keys: Vec<&[u8]>) -> Result<()> {
let cf = cf_handle::<O>(self)?;
let mut batch = rocksdb::WriteBatch::default();
@ -246,7 +236,7 @@ impl Operations for RocksdbStorageEngine {
}
/// RocksdbStorageEngine implements the rocksdb of the storage engine.
impl<'db> StorageEngine<'db> for RocksdbStorageEngine {}
impl StorageEngine<'_> for RocksdbStorageEngine {}
/// cf_handle returns the column family handle for the given object.
fn cf_handle<T>(db: &rocksdb::DB) -> Result<&rocksdb::ColumnFamily>
@ -257,3 +247,399 @@ where
db.cf_handle(cf_name)
.ok_or_else(|| Error::ColumnFamilyNotFound(cf_name.to_string()))
}
#[cfg(test)]
mod tests {
use super::*;
use serde::{Deserialize, Serialize};
use tempfile::tempdir;
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
struct Object {
id: String,
value: i32,
}
impl DatabaseObject for Object {
const NAMESPACE: &'static str = "object";
}
fn create_test_engine() -> RocksdbStorageEngine {
let temp_dir = tempdir().unwrap();
let log_dir = temp_dir.path().to_path_buf();
RocksdbStorageEngine::open(temp_dir.path(), &log_dir, &[Object::NAMESPACE], false).unwrap()
}
#[test]
fn test_put_and_get() {
let engine = create_test_engine();
let object = Object {
id: "1".to_string(),
value: 42,
};
engine.put::<Object>(object.id.as_bytes(), &object).unwrap();
let retrieved_object = engine.get::<Object>(object.id.as_bytes()).unwrap().unwrap();
assert_eq!(object, retrieved_object);
}
#[test]
fn test_is_exist() {
let engine = create_test_engine();
let object = Object {
id: "2".to_string(),
value: 100,
};
assert!(!engine.is_exist::<Object>(object.id.as_bytes()).unwrap());
engine.put::<Object>(object.id.as_bytes(), &object).unwrap();
assert!(engine.is_exist::<Object>(object.id.as_bytes()).unwrap());
}
#[test]
fn test_delete() {
let engine = create_test_engine();
let object = Object {
id: "3".to_string(),
value: 200,
};
engine.put::<Object>(object.id.as_bytes(), &object).unwrap();
assert!(engine.is_exist::<Object>(object.id.as_bytes()).unwrap());
engine.delete::<Object>(object.id.as_bytes()).unwrap();
assert!(!engine.is_exist::<Object>(object.id.as_bytes()).unwrap());
}
#[test]
fn test_batch_delete() {
let engine = create_test_engine();
let objects = vec![
Object {
id: "1".to_string(),
value: 1,
},
Object {
id: "2".to_string(),
value: 2,
},
Object {
id: "3".to_string(),
value: 3,
},
];
for object in &objects {
engine.put::<Object>(object.id.as_bytes(), object).unwrap();
assert!(engine.is_exist::<Object>(object.id.as_bytes()).unwrap());
}
let ids: Vec<&[u8]> = objects.iter().map(|object| object.id.as_bytes()).collect();
engine.batch_delete::<Object>(ids).unwrap();
for object in &objects {
assert!(!engine.is_exist::<Object>(object.id.as_bytes()).unwrap());
}
}
#[test]
fn test_iter() {
let engine = create_test_engine();
let objects = vec![
Object {
id: "1".to_string(),
value: 10,
},
Object {
id: "2".to_string(),
value: 20,
},
Object {
id: "3".to_string(),
value: 30,
},
];
for object in &objects {
engine.put::<Object>(object.id.as_bytes(), object).unwrap();
}
let retrieved_objects = engine
.iter::<Object>()
.unwrap()
.collect::<Result<Vec<_>>>()
.unwrap();
assert_eq!(retrieved_objects.len(), objects.len());
for object in &objects {
let found = retrieved_objects
.iter()
.any(|(_, v)| v.id == object.id && v.value == object.value);
assert!(found, "could not find object with id {:?}", object.id);
}
}
#[test]
fn test_prefix_iter() {
let engine = create_test_engine();
// RocksDB prefix extractor is configured with fixed_prefix(64) in the open method.
let prefix_a = [b'a'; 64];
let prefix_b = [b'b'; 64];
// Create test keys with 64-byte identical prefixes.
let key_a1 = [&prefix_a[..], b"_suffix1"].concat();
let key_a2 = [&prefix_a[..], b"_suffix2"].concat();
let key_b1 = [&prefix_b[..], b"_suffix1"].concat();
let key_b2 = [&prefix_b[..], b"_suffix2"].concat();
let objects_with_prefix_a = vec![
(
key_a1.clone(),
Object {
id: "prefix_id_a1".to_string(),
value: 100,
},
),
(
key_a2.clone(),
Object {
id: "prefix_id_a2".to_string(),
value: 200,
},
),
];
let objects_with_prefix_b = vec![
(
key_b1.clone(),
Object {
id: "prefix_id_b1".to_string(),
value: 300,
},
),
(
key_b2.clone(),
Object {
id: "prefix_id_b2".to_string(),
value: 400,
},
),
];
for (key, obj) in &objects_with_prefix_a {
engine.put::<Object>(key, obj).unwrap();
}
for (key, obj) in &objects_with_prefix_b {
engine.put::<Object>(key, obj).unwrap();
}
let retrieved_objects = engine
.prefix_iter::<Object>(&prefix_a)
.unwrap()
.collect::<Result<Vec<_>>>()
.unwrap();
assert_eq!(
retrieved_objects.len(),
objects_with_prefix_a.len(),
"expected {} objects with prefix 'a', but got {}",
objects_with_prefix_a.len(),
retrieved_objects.len()
);
// Verify each object with prefix is correctly retrieved.
for (key, object) in &objects_with_prefix_a {
let found = retrieved_objects
.iter()
.any(|(_, v)| v.id == object.id && v.value == object.value);
assert!(found, "could not find object with key {:?}", key);
}
// Verify objects with different prefix are not retrieved.
for (key, object) in &objects_with_prefix_b {
let found = retrieved_objects
.iter()
.any(|(_, v)| v.id == object.id && v.value == object.value);
assert!(!found, "found object with different prefix: {:?}", key);
}
}
#[test]
fn test_iter_raw() {
let engine = create_test_engine();
let objects = vec![
Object {
id: "1".to_string(),
value: 10,
},
Object {
id: "2".to_string(),
value: 20,
},
Object {
id: "3".to_string(),
value: 30,
},
];
for object in &objects {
engine.put::<Object>(object.id.as_bytes(), object).unwrap();
}
let retrieved_objects = engine
.iter_raw::<Object>()
.unwrap()
.collect::<Result<Vec<_>>>()
.unwrap();
assert_eq!(retrieved_objects.len(), objects.len());
// Verify each object can be deserialized from the raw bytes.
for object in &objects {
let found = retrieved_objects
.iter()
.any(|(_, v)| match Object::deserialize_from(v) {
Ok(deserialized) => {
deserialized.id == object.id && deserialized.value == object.value
}
Err(_) => false,
});
assert!(
found,
"could not find or deserialize object with key {:?}",
object.id
);
}
}
#[test]
fn test_prefix_iter_raw() {
let engine = create_test_engine();
// RocksDB prefix extractor is configured with fixed_prefix(64) in the open method.
let prefix_a = [b'a'; 64];
let prefix_b = [b'b'; 64];
// Create test keys with 64-byte identical prefixes.
let key_a1 = [&prefix_a[..], b"_raw_suffix1"].concat();
let key_a2 = [&prefix_a[..], b"_raw_suffix2"].concat();
let key_b1 = [&prefix_b[..], b"_raw_suffix1"].concat();
let key_b2 = [&prefix_b[..], b"_raw_suffix2"].concat();
let objects_with_prefix_a = vec![
(
key_a1.clone(),
Object {
id: "raw_prefix_id_a1".to_string(),
value: 100,
},
),
(
key_a2.clone(),
Object {
id: "raw_prefix_id_a2".to_string(),
value: 200,
},
),
];
let objects_with_prefix_b = vec![
(
key_b1.clone(),
Object {
id: "raw_prefix_id_b1".to_string(),
value: 300,
},
),
(
key_b2.clone(),
Object {
id: "raw_prefix_id_b2".to_string(),
value: 400,
},
),
];
for (key, obj) in &objects_with_prefix_a {
engine.put::<Object>(key, obj).unwrap();
}
for (key, obj) in &objects_with_prefix_b {
engine.put::<Object>(key, obj).unwrap();
}
let retrieved_objects = engine
.prefix_iter_raw::<Object>(&prefix_a)
.unwrap()
.collect::<Result<Vec<_>>>()
.unwrap();
assert_eq!(
retrieved_objects.len(),
objects_with_prefix_a.len(),
"expected {} raw objects with prefix 'a', but got {}",
objects_with_prefix_a.len(),
retrieved_objects.len()
);
// Verify each object with prefix can be deserialized from raw bytes.
for (_, object) in &objects_with_prefix_a {
let found = retrieved_objects
.iter()
.any(|(_, v)| match Object::deserialize_from(v) {
Ok(deserialized) => {
deserialized.id == object.id && deserialized.value == object.value
}
Err(_) => false,
});
assert!(
found,
"could not find or deserialize object with key {:?}",
object.id
);
}
// Verify objects with different prefix are not retrieved.
for (key, _) in &objects_with_prefix_b {
let found = retrieved_objects
.iter()
.any(|(k, _)| k.as_ref() == key.as_slice());
assert!(!found, "found object with different prefix: {:?}", key);
}
}
#[test]
fn test_column_family_not_found() {
let engine = create_test_engine();
// Define a new type with a different namespace that hasn't been registered.
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct UnregisteredObject {
data: String,
}
impl DatabaseObject for UnregisteredObject {
const NAMESPACE: &'static str = "unregistered";
}
let key = b"unregistered";
let result = engine.get::<UnregisteredObject>(key);
assert!(result.is_err());
if let Err(err) = result {
assert!(format!("{:?}", err).contains("ColumnFamilyNotFound"));
}
}
}

View File

@ -13,7 +13,6 @@ edition.workspace = true
dragonfly-client-core.workspace = true
dragonfly-api.workspace = true
reqwest.workspace = true
hyper.workspace = true
http-range-header.workspace = true
http.workspace = true
tracing.workspace = true
@ -24,12 +23,17 @@ rustls-pki-types.workspace = true
rustls-pemfile.workspace = true
sha2.workspace = true
uuid.workspace = true
sysinfo.workspace = true
hex.workspace = true
crc32fast.workspace = true
openssl.workspace = true
blake3.workspace = true
crc.workspace = true
base16ct.workspace = true
lazy_static.workspace = true
bytesize.workspace = true
lru.workspace = true
tokio.workspace = true
rustix = { version = "1.0.8", features = ["fs"] }
base64 = "0.22.1"
pnet = "0.35.0"
[dev-dependencies]
tempfile.workspace = true

View File

@ -14,11 +14,10 @@
* limitations under the License.
*/
use crc::*;
use dragonfly_client_core::Result as ClientResult;
use dragonfly_client_core::{Error as ClientError, Result as ClientResult};
use sha2::Digest as Sha2Digest;
use std::fmt;
use std::io::Read;
use std::io::{self, Read};
use std::path::Path;
use std::str::FromStr;
use tracing::instrument;
@ -32,9 +31,6 @@ pub enum Algorithm {
/// Crc32 is crc32 algorithm for generate digest.
Crc32,
/// Blake3 is blake3 algorithm for generate digest.
Blake3,
/// Sha256 is sha256 algorithm for generate digest.
Sha256,
@ -48,7 +44,6 @@ impl fmt::Display for Algorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Algorithm::Crc32 => write!(f, "crc32"),
Algorithm::Blake3 => write!(f, "blake3"),
Algorithm::Sha256 => write!(f, "sha256"),
Algorithm::Sha512 => write!(f, "sha512"),
}
@ -63,7 +58,6 @@ impl FromStr for Algorithm {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"crc32" => Ok(Algorithm::Crc32),
"blake3" => Ok(Algorithm::Blake3),
"sha256" => Ok(Algorithm::Sha256),
"sha512" => Ok(Algorithm::Sha512),
_ => Err(format!("invalid digest algorithm: {}", s)),
@ -118,10 +112,36 @@ impl FromStr for Digest {
}
let algorithm = match parts[0] {
"crc32" => Algorithm::Crc32,
"blake3" => Algorithm::Blake3,
"sha256" => Algorithm::Sha256,
"sha512" => Algorithm::Sha512,
"crc32" => {
if parts[1].len() != 10 {
return Err(format!(
"invalid crc32 digest length: {}, expected 10",
parts[1].len()
));
}
Algorithm::Crc32
}
"sha256" => {
if parts[1].len() != 64 {
return Err(format!(
"invalid sha256 digest length: {}, expected 64",
parts[1].len()
));
}
Algorithm::Sha256
}
"sha512" => {
if parts[1].len() != 128 {
return Err(format!(
"invalid sha512 digest length: {}, expected 128",
parts[1].len()
));
}
Algorithm::Sha512
}
_ => return Err(format!("invalid digest algorithm: {}", parts[0])),
};
@ -133,43 +153,54 @@ impl FromStr for Digest {
#[instrument(skip_all)]
pub fn calculate_file_digest(algorithm: Algorithm, path: &Path) -> ClientResult<Digest> {
let f = std::fs::File::open(path)?;
let mut reader = std::io::BufReader::new(f);
let mut reader = io::BufReader::new(f);
match algorithm {
Algorithm::Crc32 => {
let mut buffer = [0; 4096];
let crc = Crc::<u32, Table<16>>::new(&CRC_32_ISCSI);
let mut digest = crc.digest();
let mut hasher = crc32fast::Hasher::new();
loop {
let n = reader.read(&mut buffer)?;
if n == 0 {
break;
}
digest.update(&buffer[..n]);
match reader.read(&mut buffer) {
Ok(0) => break,
Ok(n) => hasher.update(&buffer[..n]),
Err(ref err) if err.kind() == io::ErrorKind::Interrupted => continue,
Err(err) => return Err(err.into()),
};
}
Ok(Digest::new(algorithm, digest.finalize().to_string()))
}
Algorithm::Blake3 => {
let mut hasher = blake3::Hasher::new();
std::io::copy(&mut reader, &mut hasher)?;
Ok(Digest::new(
algorithm,
base16ct::lower::encode_string(hasher.finalize().as_bytes()),
))
Ok(Digest::new(algorithm, hasher.finalize().to_string()))
}
Algorithm::Sha256 => {
let mut hasher = sha2::Sha256::new();
std::io::copy(&mut reader, &mut hasher)?;
io::copy(&mut reader, &mut hasher)?;
Ok(Digest::new(algorithm, hex::encode(hasher.finalize())))
}
Algorithm::Sha512 => {
let mut hasher = sha2::Sha512::new();
std::io::copy(&mut reader, &mut hasher)?;
io::copy(&mut reader, &mut hasher)?;
Ok(Digest::new(algorithm, hex::encode(hasher.finalize())))
}
}
}
/// verify_file_digest verifies the digest of a file against an expected digest.
pub fn verify_file_digest(expected_digest: Digest, file_path: &Path) -> ClientResult<()> {
let digest = match calculate_file_digest(expected_digest.algorithm(), file_path) {
Ok(digest) => digest,
Err(err) => {
return Err(err);
}
};
if digest.to_string() != expected_digest.to_string() {
return Err(ClientError::DigestMismatch(
expected_digest.to_string(),
digest.to_string(),
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
@ -179,7 +210,6 @@ mod tests {
#[test]
fn test_algorithm_display() {
assert_eq!(Algorithm::Crc32.to_string(), "crc32");
assert_eq!(Algorithm::Blake3.to_string(), "blake3");
assert_eq!(Algorithm::Sha256.to_string(), "sha256");
assert_eq!(Algorithm::Sha512.to_string(), "sha512");
}
@ -187,7 +217,6 @@ mod tests {
#[test]
fn test_algorithm_from_str() {
assert_eq!("crc32".parse::<Algorithm>(), Ok(Algorithm::Crc32));
assert_eq!("blake3".parse::<Algorithm>(), Ok(Algorithm::Blake3));
assert_eq!("sha256".parse::<Algorithm>(), Ok(Algorithm::Sha256));
assert_eq!("sha512".parse::<Algorithm>(), Ok(Algorithm::Sha512));
assert!("invalid".parse::<Algorithm>().is_err());
@ -207,11 +236,6 @@ mod tests {
let mut file = File::create(path).expect("failed to create file");
file.write_all(content).expect("failed to write to file");
let expected_blake3 = "ead3df8af4aece7792496936f83b6b6d191a7f256585ce6b6028db161278017e";
let digest = calculate_file_digest(Algorithm::Blake3, path)
.expect("failed to calculate Blake3 hash");
assert_eq!(digest.encoded(), expected_blake3);
let expected_sha256 = "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72";
let digest = calculate_file_digest(Algorithm::Sha256, path)
.expect("failed to calculate Sha256 hash");
@ -222,9 +246,33 @@ mod tests {
.expect("failed to calculate Sha512 hash");
assert_eq!(digest.encoded(), expected_sha512);
let expected_crc32 = "422618885";
let expected_crc32 = "1475635037";
let digest =
calculate_file_digest(Algorithm::Crc32, path).expect("failed to calculate Sha512 hash");
calculate_file_digest(Algorithm::Crc32, path).expect("failed to calculate Crc32 hash");
assert_eq!(digest.encoded(), expected_crc32);
}
#[test]
fn test_verify_file_digest() {
let content = b"test content";
let temp_file = tempfile::NamedTempFile::new().expect("failed to create temp file");
let path = temp_file.path();
let mut file = File::create(path).expect("failed to create file");
file.write_all(content).expect("failed to write to file");
let expected_sha256_digest = Digest::new(
Algorithm::Sha256,
"6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72".to_string(),
);
assert!(verify_file_digest(expected_sha256_digest, path).is_ok());
let expected_sha512_digest = Digest::new(
Algorithm::Sha512,
"0cbf4caef38047bba9a24e621a961484e5d2a92176a859e7eb27df343dd34eb98d538a6c5f4da1ce302ec250b821cc001e46cc97a704988297185a4df7e99602".to_string(),
);
assert!(verify_file_digest(expected_sha512_digest, path).is_ok());
let expected_crc32_digest = Digest::new(Algorithm::Crc32, "1475635037".to_string());
assert!(verify_file_digest(expected_crc32_digest, path).is_ok());
}
}

View File

@ -0,0 +1,54 @@
/*
* Copyright 2025 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use dragonfly_client_core::Result;
use tokio::fs;
/// fallocate allocates the space for the file and fills it with zero, only on Linux.
#[allow(unused_variables)]
pub async fn fallocate(f: &fs::File, length: u64) -> Result<()> {
// No allocation needed for zero length. Avoids potential fallocate errors.
if length == 0 {
return Ok(());
}
#[cfg(target_os = "linux")]
{
use dragonfly_client_core::Error;
use rustix::fs::{fallocate, FallocateFlags};
use std::os::unix::io::AsFd;
use tokio::io;
// Set length (potential truncation).
f.set_len(length).await?;
let fd = f.as_fd();
let offset = 0;
let flags = FallocateFlags::KEEP_SIZE;
loop {
match fallocate(fd, flags, offset, length) {
Ok(_) => return Ok(()),
Err(rustix::io::Errno::INTR) => continue,
Err(err) => {
return Err(Error::IO(io::Error::from_raw_os_error(err.raw_os_error())))
}
}
}
}
#[cfg(not(target_os = "linux"))]
Ok(())
}

View File

@ -20,7 +20,6 @@ use dragonfly_client_core::{
Error, Result,
};
use http::header::{self, HeaderMap};
use tracing::instrument;
/// Credentials is the credentials for the basic auth.
pub struct Credentials {
@ -34,7 +33,6 @@ pub struct Credentials {
/// Credentials is the basic auth.
impl Credentials {
/// new returns a new Credentials.
#[instrument(skip_all)]
pub fn new(username: &str, password: &str) -> Credentials {
Self {
username: username.to_string(),

View File

@ -21,12 +21,10 @@ use dragonfly_client_core::{
};
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use std::collections::HashMap;
use tracing::instrument;
pub mod basic_auth;
/// headermap_to_hashmap converts a headermap to a hashmap.
#[instrument(skip_all)]
pub fn headermap_to_hashmap(header: &HeaderMap<HeaderValue>) -> HashMap<String, String> {
let mut hashmap: HashMap<String, String> = HashMap::with_capacity(header.len());
for (k, v) in header {
@ -39,7 +37,6 @@ pub fn headermap_to_hashmap(header: &HeaderMap<HeaderValue>) -> HashMap<String,
}
/// hashmap_to_headermap converts a hashmap to a headermap.
#[instrument(skip_all)]
pub fn hashmap_to_headermap(header: &HashMap<String, String>) -> Result<HeaderMap<HeaderValue>> {
let mut headermap = HeaderMap::with_capacity(header.len());
for (k, v) in header {
@ -52,7 +49,6 @@ pub fn hashmap_to_headermap(header: &HashMap<String, String>) -> Result<HeaderMa
}
/// header_vec_to_hashmap converts a vector of header string to a hashmap.
#[instrument(skip_all)]
pub fn header_vec_to_hashmap(raw_header: Vec<String>) -> Result<HashMap<String, String>> {
let mut header = HashMap::with_capacity(raw_header.len());
for h in raw_header {
@ -65,13 +61,11 @@ pub fn header_vec_to_hashmap(raw_header: Vec<String>) -> Result<HashMap<String,
}
/// header_vec_to_headermap converts a vector of header string to a reqwest headermap.
#[instrument(skip_all)]
pub fn header_vec_to_headermap(raw_header: Vec<String>) -> Result<HeaderMap> {
hashmap_to_headermap(&header_vec_to_hashmap(raw_header)?)
}
/// get_range gets the range from http header.
#[instrument(skip_all)]
pub fn get_range(header: &HeaderMap, content_length: u64) -> Result<Option<Range>> {
match header.get(reqwest::header::RANGE) {
Some(range) => {
@ -85,7 +79,6 @@ pub fn get_range(header: &HeaderMap, content_length: u64) -> Result<Option<Range
/// parse_range_header parses a Range header string as per RFC 7233,
/// supported Range Header: "Range": "bytes=100-200", "Range": "bytes=-50",
/// "Range": "bytes=150-", "Range": "bytes=0-0,-1".
#[instrument(skip_all)]
pub fn parse_range_header(range_header_value: &str, content_length: u64) -> Result<Range> {
let parsed_ranges =
http_range_header::parse_range_header(range_header_value).or_err(ErrorType::ParseError)?;

View File

@ -14,16 +14,14 @@
* limitations under the License.
*/
use crc::*;
use dragonfly_api::common::v2::TaskType;
use dragonfly_client_core::{
error::{ErrorType, OrErr},
Result,
};
use sha2::{Digest, Sha256};
use std::io::Read;
use std::io::{self, Read};
use std::path::PathBuf;
use tracing::instrument;
use url::Url;
use uuid::Uuid;
@ -33,6 +31,34 @@ const SEED_PEER_SUFFIX: &str = "seed";
/// PERSISTENT_CACHE_TASK_SUFFIX is the suffix of the persistent cache task.
const PERSISTENT_CACHE_TASK_SUFFIX: &str = "persistent-cache-task";
/// TaskIDParameter is the parameter of the task id.
pub enum TaskIDParameter {
/// Content uses the content to generate the task id.
Content(String),
/// URLBased uses the url, piece_length, tag, application and filtered_query_params to generate
/// the task id.
URLBased {
url: String,
piece_length: Option<u64>,
tag: Option<String>,
application: Option<String>,
filtered_query_params: Vec<String>,
},
}
/// PersistentCacheTaskIDParameter is the parameter of the persistent cache task id.
pub enum PersistentCacheTaskIDParameter {
/// Content uses the content to generate the persistent cache task id.
Content(String),
/// FileContentBased uses the file path, piece_length, tag and application to generate the persistent cache task id.
FileContentBased {
path: PathBuf,
piece_length: Option<u64>,
tag: Option<String>,
application: Option<String>,
},
}
/// IDGenerator is used to generate the id for the resources.
#[derive(Debug)]
pub struct IDGenerator {
@ -49,7 +75,6 @@ pub struct IDGenerator {
/// IDGenerator implements the IDGenerator.
impl IDGenerator {
/// new creates a new IDGenerator.
#[instrument(skip_all)]
pub fn new(ip: String, hostname: String, is_seed_peer: bool) -> Self {
IDGenerator {
ip,
@ -60,7 +85,6 @@ impl IDGenerator {
/// host_id generates the host id.
#[inline]
#[instrument(skip_all)]
pub fn host_id(&self) -> String {
if self.is_seed_peer {
return format!("{}-{}-{}", self.ip, self.hostname, "seed");
@ -71,107 +95,124 @@ impl IDGenerator {
/// task_id generates the task id.
#[inline]
#[instrument(skip_all)]
pub fn task_id(
&self,
url: &str,
piece_length: Option<u64>,
tag: Option<&str>,
application: Option<&str>,
filtered_query_params: Vec<String>,
) -> Result<String> {
// Filter the query parameters.
let url = Url::parse(url).or_err(ErrorType::ParseError)?;
let query = url
.query_pairs()
.filter(|(k, _)| !filtered_query_params.contains(&k.to_string()));
pub fn task_id(&self, parameter: TaskIDParameter) -> Result<String> {
match parameter {
TaskIDParameter::Content(content) => {
Ok(hex::encode(Sha256::digest(content.as_bytes())))
}
TaskIDParameter::URLBased {
url,
piece_length,
tag,
application,
filtered_query_params,
} => {
// Filter the query parameters.
let url = Url::parse(url.as_str()).or_err(ErrorType::ParseError)?;
let query = url
.query_pairs()
.filter(|(k, _)| !filtered_query_params.contains(&k.to_string()));
let mut artifact_url = url.clone();
if query.clone().count() == 0 {
artifact_url.set_query(None);
} else {
artifact_url.query_pairs_mut().clear().extend_pairs(query);
let mut artifact_url = url.clone();
if query.clone().count() == 0 {
artifact_url.set_query(None);
} else {
artifact_url.query_pairs_mut().clear().extend_pairs(query);
}
let artifact_url_str = artifact_url.to_string();
let final_url = if artifact_url_str.ends_with('/') && artifact_url.path() == "/" {
artifact_url_str.trim_end_matches('/').to_string()
} else {
artifact_url_str
};
// Initialize the hasher.
let mut hasher = Sha256::new();
// Add the url to generate the task id.
hasher.update(final_url);
// Add the tag to generate the task id.
if let Some(tag) = tag {
hasher.update(tag);
}
// Add the application to generate the task id.
if let Some(application) = application {
hasher.update(application);
}
// Add the piece length to generate the task id.
if let Some(piece_length) = piece_length {
hasher.update(piece_length.to_string());
}
hasher.update(TaskType::Standard.as_str_name().as_bytes());
// Generate the task id.
Ok(hex::encode(hasher.finalize()))
}
}
let artifact_url_str = artifact_url.to_string();
let final_url = if artifact_url_str.ends_with('/') && artifact_url.path() == "/" {
artifact_url_str.trim_end_matches('/').to_string()
} else {
artifact_url_str
};
// Initialize the hasher.
let mut hasher = Sha256::new();
// Add the url to generate the task id.
hasher.update(final_url);
// Add the tag to generate the task id.
if let Some(tag) = tag {
hasher.update(tag);
}
// Add the application to generate the task id.
if let Some(application) = application {
hasher.update(application);
}
// Add the piece length to generate the task id.
if let Some(piece_length) = piece_length {
hasher.update(piece_length.to_string());
}
// Generate the task id.
Ok(hex::encode(hasher.finalize()))
}
/// persistent_cache_task_id generates the persistent cache task id.
#[inline]
#[instrument(skip_all)]
pub fn persistent_cache_task_id(
&self,
path: &PathBuf,
piece_length: Option<u64>,
tag: Option<&str>,
application: Option<&str>,
parameter: PersistentCacheTaskIDParameter,
) -> Result<String> {
// Calculate the hash of the file.
let f = std::fs::File::open(path)?;
let mut buffer = [0; 4096];
let mut reader = std::io::BufReader::with_capacity(buffer.len(), f);
let crc = Crc::<u32, Table<16>>::new(&CRC_32_ISCSI);
let mut digest = crc.digest();
loop {
let n = reader.read(&mut buffer)?;
if n == 0 {
break;
let mut hasher = crc32fast::Hasher::new();
match parameter {
PersistentCacheTaskIDParameter::Content(content) => {
hasher.update(content.as_bytes());
Ok(hasher.finalize().to_string())
}
PersistentCacheTaskIDParameter::FileContentBased {
path,
piece_length,
tag,
application,
} => {
// Calculate the hash of the file.
let f = std::fs::File::open(path)?;
let mut buffer = [0; 4096];
let mut reader = io::BufReader::with_capacity(buffer.len(), f);
loop {
match reader.read(&mut buffer) {
Ok(0) => break,
Ok(n) => hasher.update(&buffer[..n]),
Err(ref err) if err.kind() == io::ErrorKind::Interrupted => continue,
Err(err) => return Err(err.into()),
};
}
digest.update(&buffer[..n]);
// Add the tag to generate the persistent cache task id.
if let Some(tag) = tag {
hasher.update(tag.as_bytes());
}
// Add the application to generate the persistent cache task id.
if let Some(application) = application {
hasher.update(application.as_bytes());
}
// Add the piece length to generate the persistent cache task id.
if let Some(piece_length) = piece_length {
hasher.update(piece_length.to_string().as_bytes());
}
hasher.update(TaskType::PersistentCache.as_str_name().as_bytes());
// Generate the task id by crc32.
Ok(hasher.finalize().to_string())
}
}
// Add the tag to generate the persistent cache task id.
if let Some(tag) = tag {
digest.update(tag.as_bytes());
}
// Add the application to generate the persistent cache task id.
if let Some(application) = application {
digest.update(application.as_bytes());
}
// Add the piece length to generate the persistent cache task id.
if let Some(piece_length) = piece_length {
digest.update(piece_length.to_string().as_bytes());
}
// Generate the task id by crc32.
Ok(digest.finalize().to_string())
}
/// peer_id generates the peer id.
#[inline]
#[instrument(skip_all)]
pub fn peer_id(&self) -> String {
if self.is_seed_peer {
return format!(
@ -187,7 +228,6 @@ impl IDGenerator {
}
/// task_type generates the task type by the task id.
#[instrument(skip_all)]
pub fn task_type(&self, id: &str) -> TaskType {
if id.ends_with(PERSISTENT_CACHE_TASK_SUFFIX) {
return TaskType::PersistentCache;
@ -227,116 +267,140 @@ mod tests {
let test_cases = vec![
(
IDGenerator::new("127.0.0.1".to_string(), "localhost".to_string(), false),
"https://example.com",
Some(1024_u64),
Some("foo"),
Some("bar"),
vec![],
"99a47b38e9d3321aebebd715bea0483c1400cef2f767f84d97458f9dcedff221",
TaskIDParameter::URLBased {
url: "https://example.com".to_string(),
piece_length: Some(1024_u64),
tag: Some("foo".to_string()),
application: Some("bar".to_string()),
filtered_query_params: vec![],
},
"27554d06dfc788c2c2c60e01960152ffbd4b145fc103fcb80b432b4dc238a6fe",
),
(
IDGenerator::new("127.0.0.1".to_string(), "localhost".to_string(), false),
"https://example.com",
None,
Some("foo"),
Some("bar"),
vec![],
"160fa7f001d9d2e893130894fbb60a5fb006e1d61bff82955f2946582bc9de1d",
TaskIDParameter::URLBased {
url: "https://example.com".to_string(),
piece_length: None,
tag: Some("foo".to_string()),
application: Some("bar".to_string()),
filtered_query_params: vec![],
},
"06408fbf247ddaca478f8cb9565fe5591c28efd0994b8fea80a6a87d3203c5ca",
),
(
IDGenerator::new("127.0.0.1".to_string(), "localhost".to_string(), false),
"https://example.com",
None,
Some("foo"),
None,
vec![],
"2773851c628744fb7933003195db436ce397c1722920696c4274ff804d86920b",
TaskIDParameter::URLBased {
url: "https://example.com".to_string(),
piece_length: None,
tag: Some("foo".to_string()),
application: None,
filtered_query_params: vec![],
},
"3c3f230ef9f191dd2821510346a7bc138e4894bee9aee184ba250a3040701d2a",
),
(
IDGenerator::new("127.0.0.1".to_string(), "localhost".to_string(), false),
"https://example.com",
None,
None,
Some("bar"),
vec![],
"63dee2822037636b0109876b58e95692233840753a882afa69b9b5ee82a6c57d",
TaskIDParameter::URLBased {
url: "https://example.com".to_string(),
piece_length: None,
tag: None,
application: Some("bar".to_string()),
filtered_query_params: vec![],
},
"c9f9261b7305c24371244f9f149f5d4589ed601348fdf22d7f6f4b10658fdba2",
),
(
IDGenerator::new("127.0.0.1".to_string(), "localhost".to_string(), false),
"https://example.com",
Some(1024_u64),
None,
None,
vec![],
"40c21de3ad2f1470ca1a19a2ad2577803a1829851f6cf862ffa2d4577ae51d38",
TaskIDParameter::URLBased {
url: "https://example.com".to_string(),
piece_length: Some(1024_u64),
tag: None,
application: None,
filtered_query_params: vec![],
},
"9f7c9aafbc6f30f8f41a96ca77eeae80c5b60964b3034b0ee43ccf7b2f9e52b8",
),
(
IDGenerator::new("127.0.0.1".to_string(), "localhost".to_string(), false),
"https://example.com?foo=foo&bar=bar",
None,
None,
None,
vec!["foo".to_string(), "bar".to_string()],
"100680ad546ce6a577f42f52df33b4cfdca756859e664b8d7de329b150d09ce9",
TaskIDParameter::URLBased {
url: "https://example.com?foo=foo&bar=bar".to_string(),
piece_length: None,
tag: None,
application: None,
filtered_query_params: vec!["foo".to_string(), "bar".to_string()],
},
"457b4328cde278e422c9e243f7bfd1e97f511fec43a80f535cf6b0ef6b086776",
),
(
IDGenerator::new("127.0.0.1".to_string(), "localhost".to_string(), false),
TaskIDParameter::Content("This is a test file".to_string()),
"e2d0fe1585a63ec6009c8016ff8dda8b17719a637405a4e23c0ff81339148249",
),
];
for (generator, url, piece_length, tag, application, filtered_query_params, expected_id) in
test_cases
{
let task_id = generator
.task_id(url, piece_length, tag, application, filtered_query_params)
.unwrap();
for (generator, parameter, expected_id) in test_cases {
let task_id = generator.task_id(parameter).unwrap();
assert_eq!(task_id, expected_id);
}
}
#[test]
fn should_generate_persistent_cache_task_id() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("testfile");
let mut f = File::create(&file_path).unwrap();
f.write_all("This is a test file".as_bytes()).unwrap();
let test_cases = vec![
(
IDGenerator::new("127.0.0.1".to_string(), "localhost".to_string(), false),
"This is a test file",
Some(1024_u64),
Some("tag1"),
Some("app1"),
"1692841488",
PersistentCacheTaskIDParameter::FileContentBased {
path: file_path.clone(),
piece_length: Some(1024_u64),
tag: Some("tag1".to_string()),
application: Some("app1".to_string()),
},
"3490958009",
),
(
IDGenerator::new("127.0.0.1".to_string(), "localhost".to_string(), false),
"This is a test file",
None,
None,
Some("app1"),
"1232797642",
PersistentCacheTaskIDParameter::FileContentBased {
path: file_path.clone(),
piece_length: None,
tag: None,
application: Some("app1".to_string()),
},
"735741469",
),
(
IDGenerator::new("127.0.0.1".to_string(), "localhost".to_string(), false),
"This is a test file",
None,
Some("tag1"),
None,
"2519859852",
PersistentCacheTaskIDParameter::FileContentBased {
path: file_path.clone(),
piece_length: None,
tag: Some("tag1".to_string()),
application: None,
},
"3954905097",
),
(
IDGenerator::new("127.0.0.1".to_string(), "localhost".to_string(), false),
"This is a test file",
Some(1024_u64),
None,
None,
"561935167",
PersistentCacheTaskIDParameter::FileContentBased {
path: file_path.clone(),
piece_length: Some(1024_u64),
tag: None,
application: None,
},
"4162557545",
),
(
IDGenerator::new("127.0.0.1".to_string(), "localhost".to_string(), false),
PersistentCacheTaskIDParameter::Content("This is a test file".to_string()),
"107352521",
),
];
for (generator, file_content, piece_length, tag, application, expected_id) in test_cases {
let dir = tempdir().unwrap();
let file_path = dir.path().join("testfile");
let mut f = File::create(&file_path).unwrap();
f.write_all(file_content.as_bytes()).unwrap();
let task_id = generator
.persistent_cache_task_id(&file_path, piece_length, tag, application)
.unwrap();
for (generator, parameter, expected_id) in test_cases {
let task_id = generator.persistent_cache_task_id(parameter).unwrap();
assert_eq!(task_id, expected_id);
}
}

View File

@ -15,6 +15,8 @@
*/
pub mod digest;
pub mod fs;
pub mod http;
pub mod id_generator;
pub mod net;
pub mod tls;

View File

@ -0,0 +1,230 @@
/*
* Copyright 2025 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use bytesize::ByteSize;
use pnet::datalink::{self, NetworkInterface};
use std::cmp::min;
use std::net::IpAddr;
use std::sync::Arc;
use std::time::Duration;
use sysinfo::Networks;
use tokio::sync::Mutex;
use tracing::{info, warn};
/// Interface represents a network interface with its information.
#[derive(Debug, Clone, Default)]
pub struct Interface {
/// name is the name of the network interface.
pub name: String,
/// bandwidth is the bandwidth of the network interface in bps.
pub bandwidth: u64,
// network_data_mutex is a mutex to protect access to network data.
network_data_mutex: Arc<Mutex<()>>,
}
/// NetworkData represents the network data for a specific interface,
#[derive(Debug, Clone, Default)]
pub struct NetworkData {
/// max_rx_bandwidth is the maximum receive bandwidth of the interface in bps.
pub max_rx_bandwidth: u64,
/// rx_bandwidth is the current receive bandwidth of the interface in bps.
pub rx_bandwidth: Option<u64>,
/// max_tx_bandwidth is the maximum transmit bandwidth of the interface in bps.
pub max_tx_bandwidth: u64,
/// tx_bandwidth is the current transmit bandwidth of the interface in bps.
pub tx_bandwidth: Option<u64>,
}
/// Interface methods provide functionality to get network interface information.
impl Interface {
/// DEFAULT_NETWORKS_REFRESH_INTERVAL is the default interval for refreshing network data.
const DEFAULT_NETWORKS_REFRESH_INTERVAL: Duration = Duration::from_secs(2);
/// new creates a new Interface instance based on the provided IP address and rate limit.
pub fn new(ip: IpAddr, rate_limit: ByteSize) -> Interface {
let rate_limit = Self::byte_size_to_bits(rate_limit); // convert to bps
let Some(interface) = Self::get_network_interface_by_ip(ip) else {
warn!(
"can not find interface for IP address {}, network interface unknown with bandwidth {} bps",
ip, rate_limit
);
return Interface {
name: "unknown".to_string(),
bandwidth: rate_limit,
network_data_mutex: Arc::new(Mutex::new(())),
};
};
match Self::get_speed(&interface.name) {
Some(speed) => {
let bandwidth = min(Self::megabits_to_bits(speed), rate_limit);
info!(
"network interface {} with bandwidth {} bps",
interface.name, bandwidth
);
Interface {
name: interface.name,
bandwidth,
network_data_mutex: Arc::new(Mutex::new(())),
}
}
None => {
warn!(
"can not get speed, network interface {} with bandwidth {} bps",
interface.name, rate_limit
);
Interface {
name: interface.name,
bandwidth: rate_limit,
network_data_mutex: Arc::new(Mutex::new(())),
}
}
}
}
/// get_network_data retrieves the network data for the interface.
pub async fn get_network_data(&self) -> NetworkData {
// Lock the mutex to ensure exclusive access to network data.
let _guard = self.network_data_mutex.lock().await;
// Initialize sysinfo network.
let mut networks = Networks::new_with_refreshed_list();
// Sleep to calculate the network traffic difference over
// the DEFAULT_NETWORKS_REFRESH_INTERVAL.
tokio::time::sleep(Self::DEFAULT_NETWORKS_REFRESH_INTERVAL).await;
// Refresh network information.
networks.refresh();
let Some(network_data) = networks.get(self.name.as_str()) else {
warn!("can not find network data for interface {}", self.name);
return NetworkData {
max_rx_bandwidth: self.bandwidth,
max_tx_bandwidth: self.bandwidth,
..Default::default()
};
};
// Calculate the receive and transmit bandwidth in bits per second.
let rx_bandwidth = (Self::bytes_to_bits(network_data.received()) as f64
/ Self::DEFAULT_NETWORKS_REFRESH_INTERVAL.as_secs_f64())
.round() as u64;
// Calculate the transmit bandwidth in bits per second.
let tx_bandwidth = (Self::bytes_to_bits(network_data.transmitted()) as f64
/ Self::DEFAULT_NETWORKS_REFRESH_INTERVAL.as_secs_f64())
.round() as u64;
NetworkData {
max_rx_bandwidth: self.bandwidth,
rx_bandwidth: Some(rx_bandwidth),
max_tx_bandwidth: self.bandwidth,
tx_bandwidth: Some(tx_bandwidth),
}
}
/// get_speed returns the speed of the network interface in Mbps.
pub fn get_speed(name: &str) -> Option<u64> {
#[cfg(target_os = "linux")]
{
let speed_path = format!("/sys/class/net/{}/speed", name);
std::fs::read_to_string(&speed_path)
.ok()
.and_then(|speed_str| speed_str.trim().parse::<u64>().ok())
}
#[cfg(not(target_os = "linux"))]
{
warn!("can not get interface {} speed on non-linux platform", name);
None
}
}
/// get_network_interface_by_ip returns the network interface that has the specified
/// IP address.
pub fn get_network_interface_by_ip(ip: IpAddr) -> Option<NetworkInterface> {
datalink::interfaces()
.into_iter()
.find(|interface| interface.ips.iter().any(|ip_net| ip_net.ip() == ip))
}
/// byte_size_to_bits converts a ByteSize to bits.
pub fn byte_size_to_bits(size: ByteSize) -> u64 {
size.as_u64() * 8
}
/// megabits_to_bit converts megabits to bits.
pub fn megabits_to_bits(size: u64) -> u64 {
size * 1_000_000 // 1 Mbit = 1,000,000 bits
}
/// bytes_to_bits converts bytes to bits.
pub fn bytes_to_bits(size: u64) -> u64 {
size * 8 // 1 byte = 8 bits
}
}
#[cfg(test)]
mod tests {
use super::*;
use bytesize::ByteSize;
#[test]
fn test_byte_size_to_bits() {
let test_cases = vec![
(ByteSize::kb(1), 8_000u64),
(ByteSize::mb(1), 8_000_000u64),
(ByteSize::gb(1), 8_000_000_000u64),
(ByteSize::b(0), 0u64),
];
for (input, expected) in test_cases {
let result = Interface::byte_size_to_bits(input);
assert_eq!(result, expected);
}
}
#[test]
fn test_megabits_to_bits() {
let test_cases = vec![
(1u64, 1_000_000u64),
(1000u64, 1_000_000_000u64),
(0u64, 0u64),
];
for (input, expected) in test_cases {
let result = Interface::megabits_to_bits(input);
assert_eq!(result, expected);
}
}
#[test]
fn test_bytes_to_bits() {
let test_cases = vec![(1u64, 8u64), (1000u64, 8_000u64), (0u64, 0u64)];
for (input, expected) in test_cases {
let result = Interface::bytes_to_bits(input);
assert_eq!(result, expected);
}
}
}

View File

@ -16,14 +16,35 @@
use dragonfly_client_core::error::{ErrorType, OrErr};
use dragonfly_client_core::{Error as ClientError, Result as ClientResult};
use lazy_static::lazy_static;
use lru::LruCache;
use rcgen::{Certificate, CertificateParams, KeyPair};
use rustls_pki_types::{CertificateDer, PrivateKeyDer, ServerName, UnixTime};
use std::num::NonZeroUsize;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::vec::Vec;
use std::{fs, io};
use tracing::instrument;
/// DEFAULT_CERTS_CACHE_CAPACITY is the default capacity of the certificates cache.
const DEFAULT_CERTS_CACHE_CAPACITY: usize = 1000;
/// CertKeyPair is the type of the certificate and private key pair.
type CertKeyPair = (Vec<CertificateDer<'static>>, PrivateKeyDer<'static>);
lazy_static! {
/// SELF_SIGNED_CERTS is a map that stores the self-signed certificates to avoid
/// generating the same certificates multiple times.
static ref SELF_SIGNED_CERTS: Arc<Mutex<LruCache<String, CertKeyPair>>> =
Arc::new(Mutex::new(LruCache::new(NonZeroUsize::new(DEFAULT_CERTS_CACHE_CAPACITY).unwrap())));
/// SIMPLE_SELF_SIGNED_CERTS is a map that stores the simple self-signed certificates to avoid
/// generating the same certificates multiple times.
static ref SIMPLE_SELF_SIGNED_CERTS: Arc<Mutex<LruCache<String, CertKeyPair>>> =
Arc::new(Mutex::new(LruCache::new(NonZeroUsize::new(DEFAULT_CERTS_CACHE_CAPACITY).unwrap())));
}
/// NoVerifier is a verifier that does not verify the server certificate.
/// It is used for testing and should not be used in production.
#[derive(Debug)]
@ -124,8 +145,15 @@ pub fn generate_cert_from_pem(cert_path: &PathBuf) -> ClientResult<Vec<Certifica
#[instrument(skip_all)]
pub fn generate_self_signed_certs_by_ca_cert(
ca_cert: &Certificate,
host: &str,
subject_alt_names: Vec<String>,
) -> ClientResult<(Vec<CertificateDer<'static>>, PrivateKeyDer<'static>)> {
let mut cache = SELF_SIGNED_CERTS.lock().unwrap();
if let Some((certs, key)) = cache.get(host) {
return Ok((certs.clone(), key.clone_key()));
};
drop(cache);
// Sign certificate with CA certificate by given subject alternative names.
let params = CertificateParams::new(subject_alt_names);
let cert = Certificate::from_params(params).or_err(ErrorType::CertificateError)?;
@ -143,14 +171,23 @@ pub fn generate_self_signed_certs_by_ca_cert(
let key = rustls_pemfile::private_key(&mut key_pem_reader)?
.ok_or_else(|| ClientError::Unknown("failed to load private key".to_string()))?;
let mut cache = SELF_SIGNED_CERTS.lock().unwrap();
cache.push(host.to_string(), (certs.clone(), key.clone_key()));
Ok((certs, key))
}
/// generate_simple_self_signed_certs generates a simple self-signed certificates
#[instrument(skip_all)]
pub fn generate_simple_self_signed_certs(
host: &str,
subject_alt_names: impl Into<Vec<String>>,
) -> ClientResult<(Vec<CertificateDer<'static>>, PrivateKeyDer<'static>)> {
let mut cache = SIMPLE_SELF_SIGNED_CERTS.lock().unwrap();
if let Some((certs, key)) = cache.get(host) {
return Ok((certs.clone(), key.clone_key()));
};
drop(cache);
let cert = rcgen::generate_simple_self_signed(subject_alt_names)
.or_err(ErrorType::CertificateError)?;
let key = rustls_pki_types::PrivateKeyDer::Pkcs8(cert.serialize_private_key_der().into());
@ -159,6 +196,8 @@ pub fn generate_simple_self_signed_certs(
.or_err(ErrorType::CertificateError)?
.into()];
let mut cache = SIMPLE_SELF_SIGNED_CERTS.lock().unwrap();
cache.push(host.to_string(), (certs.clone(), key.clone_key()));
Ok((certs, key))
}
@ -331,9 +370,10 @@ Z+yQ5jhu/fmSBNhqO/8Lp+Y=
&ca_key_file.path().to_path_buf(),
)
.unwrap();
let subject_alt_names = vec!["example.com".to_string()];
let host = "example.com";
let subject_alt_names = vec![host.to_string()];
let result = generate_self_signed_certs_by_ca_cert(&ca_cert, subject_alt_names);
let result = generate_self_signed_certs_by_ca_cert(&ca_cert, host, subject_alt_names);
assert!(result.is_ok());
let (certs, key) = result.unwrap();
assert!(!certs.is_empty());

View File

@ -18,10 +18,6 @@ path = "src/bin/dfdaemon/main.rs"
name = "dfget"
path = "src/bin/dfget/main.rs"
[[bin]]
name = "dfstore"
path = "src/bin/dfstore/main.rs"
[[bin]]
name = "dfcache"
path = "src/bin/dfcache/main.rs"
@ -38,8 +34,6 @@ hyper.workspace = true
hyper-util.workspace = true
hyper-rustls.workspace = true
tracing.workspace = true
validator.workspace = true
humantime.workspace = true
serde.workspace = true
chrono.workspace = true
prost-wkt-types.workspace = true
@ -57,39 +51,42 @@ http.workspace = true
openssl.workspace = true
clap.workspace = true
anyhow.workspace = true
blake3.workspace = true
bytes.workspace = true
bytesize.workspace = true
humantime.workspace = true
uuid.workspace = true
percent-encoding.workspace = true
tokio-rustls.workspace = true
serde_json.workspace = true
lru.workspace = true
fs2.workspace = true
lazy_static = "1.5"
tracing-log = "0.2"
tracing-subscriber = { version = "0.3", features = ["env-filter", "time", "chrono"] }
lazy_static.workspace = true
futures.workspace = true
local-ip-address.workspace = true
sysinfo.workspace = true
tracing-appender = "0.2.3"
tracing-subscriber = { version = "0.3", features = ["env-filter", "time", "chrono"] }
tracing-panic = "0.1.2"
tracing-opentelemetry = "0.30.0"
opentelemetry = { version = "0.29.1", default-features = false, features = ["trace"] }
opentelemetry-otlp = { version = "0.29.0", default-features = false, features = ["trace", "grpc-tonic", "http-proto", "reqwest-blocking-client"] }
opentelemetry_sdk = { version = "0.29.0", default-features = false, features = ["trace", "rt-tokio"] }
opentelemetry-semantic-conventions = { version = "0.30.0", features = ["semconv_experimental"] }
rolling-file = "0.2.0"
tracing-opentelemetry = "0.18.0"
tracing-flame = "0.2.0"
opentelemetry = { version = "0.18.0", default-features = false, features = ["trace", "rt-tokio"] }
opentelemetry-jaeger = { version = "0.17.0", features = ["rt-tokio"] }
pprof = { version = "0.14", features = ["flamegraph", "protobuf-codec"] }
pprof = { version = "0.15", features = ["flamegraph", "protobuf-codec"] }
prometheus = { version = "0.13", features = ["process"] }
tonic-health = "0.12.3"
bytes = "1.10"
sysinfo = "0.32.1"
tower = { version = "0.4.13", features = ["limit", "load-shed", "buffer"] }
indicatif = "0.17.11"
dashmap = "6.1.0"
indicatif = "0.18.0"
hashring = "0.3.6"
fslock = "0.2.1"
leaky-bucket = "1.1.2"
http-body-util = "0.1.2"
futures-util = "0.3.31"
termion = "4.0.3"
tabled = "0.18.0"
http-body-util = "0.1.3"
termion = "4.0.5"
tabled = "0.20.0"
path-absolutize = "3.1.1"
dashmap = "6.1.0"
fastrand = "2.3.0"
glob = "0.3.3"
console-subscriber = "0.4.1"
[dev-dependencies]
tempfile.workspace = true
@ -120,11 +117,6 @@ assets = [
"usr/bin/dfcache",
"755",
],
[
"../target/x86_64-unknown-linux-gnu/release/dfstore",
"usr/bin/dfstore",
"755",
],
[
"../ci/dfdaemon.service",
"lib/systemd/system/dfdaemon.service",
@ -167,11 +159,6 @@ assets = [
"usr/bin/dfcache",
"755",
],
[
"../target/x86_64-unknown-linux-musl/release/dfstore",
"usr/bin/dfstore",
"755",
],
[
"../ci/dfdaemon.service",
"lib/systemd/system/dfdaemon.service",
@ -214,11 +201,6 @@ assets = [
"usr/bin/dfcache",
"755",
],
[
"../target/aarch64-unknown-linux-gnu/release/dfstore",
"usr/bin/dfstore",
"755",
],
[
"../ci/dfdaemon.service",
"lib/systemd/system/dfdaemon.service",
@ -261,11 +243,6 @@ assets = [
"usr/bin/dfcache",
"755",
],
[
"../target/aarch64-unknown-linux-musl/release/dfstore",
"usr/bin/dfstore",
"755",
],
[
"../ci/dfdaemon.service",
"lib/systemd/system/dfdaemon.service",
@ -293,7 +270,6 @@ assets = [
{ source = "../target/x86_64-unknown-linux-gnu/release/dfget", dest = "/usr/bin/dfget", mode = "755" },
{ source = "../target/x86_64-unknown-linux-gnu/release/dfdaemon", dest = "/usr/bin/dfdaemon", mode = "755" },
{ source = "../target/x86_64-unknown-linux-gnu/release/dfcache", dest = "/usr/bin/dfcache", mode = "755" },
{ source = "../target/x86_64-unknown-linux-gnu/release/dfstore", dest = "/usr/bin/dfstore", mode = "755" },
{ source = "../ci/dfdaemon.service", dest = "/lib/systemd/system/dfdaemon.service", config = true, mode = "644" },
{ source = "../CONTRIBUTING.md", dest = "/usr/share/doc/client/CONTRIBUTING.md", mode = "644", doc = true },
{ source = "../LICENSE", dest = "/usr/share/doc/client/LICENSE.md", mode = "644", doc = true },
@ -305,7 +281,6 @@ assets = [
{ source = "../target/x86_64-unknown-linux-musl/release/dfget", dest = "/usr/bin/dfget", mode = "755" },
{ source = "../target/x86_64-unknown-linux-musl/release/dfdaemon", dest = "/usr/bin/dfdaemon", mode = "755" },
{ source = "../target/x86_64-unknown-linux-musl/release/dfcache", dest = "/usr/bin/dfcache", mode = "755" },
{ source = "../target/x86_64-unknown-linux-musl/release/dfstore", dest = "/usr/bin/dfstore", mode = "755" },
{ source = "../ci/dfdaemon.service", dest = "/lib/systemd/system/dfdaemon.service", config = true, mode = "644" },
{ source = "../CONTRIBUTING.md", dest = "/usr/share/doc/client/CONTRIBUTING.md", mode = "644", doc = true },
{ source = "../LICENSE", dest = "/usr/share/doc/client/LICENSE.md", mode = "644", doc = true },
@ -318,7 +293,6 @@ assets = [
{ source = "../target/aarch64-unknown-linux-gnu/release/dfget", dest = "/usr/bin/dfget", mode = "755" },
{ source = "../target/aarch64-unknown-linux-gnu/release/dfdaemon", dest = "/usr/bin/dfdaemon", mode = "755" },
{ source = "../target/aarch64-unknown-linux-gnu/release/dfcache", dest = "/usr/bin/dfcache", mode = "755" },
{ source = "../target/aarch64-unknown-linux-gnu/release/dfstore", dest = "/usr/bin/dfstore", mode = "755" },
{ source = "../ci/dfdaemon.service", dest = "/lib/systemd/system/dfdaemon.service", config = true, mode = "644" },
{ source = "../CONTRIBUTING.md", dest = "/usr/share/doc/client/CONTRIBUTING.md", mode = "644", doc = true },
{ source = "../LICENSE", dest = "/usr/share/doc/client/LICENSE.md", mode = "644", doc = true },
@ -330,7 +304,6 @@ assets = [
{ source = "../target/aarch64-unknown-linux-musl/release/dfget", dest = "/usr/bin/dfget", mode = "755" },
{ source = "../target/aarch64-unknown-linux-musl/release/dfdaemon", dest = "/usr/bin/dfdaemon", mode = "755" },
{ source = "../target/aarch64-unknown-linux-musl/release/dfcache", dest = "/usr/bin/dfcache", mode = "755" },
{ source = "../target/aarch64-unknown-linux-musl/release/dfstore", dest = "/usr/bin/dfstore", mode = "755" },
{ source = "../ci/dfdaemon.service", dest = "/lib/systemd/system/dfdaemon.service", config = true, mode = "644" },
{ source = "../CONTRIBUTING.md", dest = "/usr/share/doc/client/CONTRIBUTING.md", mode = "644", doc = true },
{ source = "../LICENSE", dest = "/usr/share/doc/client/LICENSE.md", mode = "644", doc = true },

View File

@ -14,10 +14,9 @@
* limitations under the License.
*/
use crate::grpc::{manager::ManagerClient, scheduler::SchedulerClient};
use crate::grpc::scheduler::SchedulerClient;
use crate::shutdown;
use dragonfly_api::common::v2::{Build, Cpu, Disk, Host, Memory, Network};
use dragonfly_api::manager::v2::{DeleteSeedPeerRequest, SourceType, UpdateSeedPeerRequest};
use dragonfly_api::scheduler::v2::{AnnounceHostRequest, DeleteHostRequest};
use dragonfly_client_config::{
dfdaemon::{Config, HostType},
@ -25,91 +24,13 @@ use dragonfly_client_config::{
};
use dragonfly_client_core::error::{ErrorType, OrErr};
use dragonfly_client_core::Result;
use dragonfly_client_util::net::Interface;
use std::env;
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use std::time::Duration;
use sysinfo::System;
use tokio::sync::mpsc;
use tracing::{error, info, instrument};
/// ManagerAnnouncer is used to announce the dfdaemon information to the manager.
pub struct ManagerAnnouncer {
/// config is the configuration of the dfdaemon.
config: Arc<Config>,
/// manager_client is the grpc client of the manager.
manager_client: Arc<ManagerClient>,
/// shutdown is used to shutdown the announcer.
shutdown: shutdown::Shutdown,
/// _shutdown_complete is used to notify the announcer is shutdown.
_shutdown_complete: mpsc::UnboundedSender<()>,
}
/// ManagerAnnouncer implements the manager announcer of the dfdaemon.
impl ManagerAnnouncer {
/// new creates a new manager announcer.
#[instrument(skip_all)]
pub fn new(
config: Arc<Config>,
manager_client: Arc<ManagerClient>,
shutdown: shutdown::Shutdown,
shutdown_complete_tx: mpsc::UnboundedSender<()>,
) -> Self {
Self {
config,
manager_client,
shutdown,
_shutdown_complete: shutdown_complete_tx,
}
}
/// run announces the dfdaemon information to the manager.
#[instrument(skip_all)]
pub async fn run(&self) -> Result<()> {
// Clone the shutdown channel.
let mut shutdown = self.shutdown.clone();
// If the seed peer is enabled, we should announce the seed peer to the manager.
if self.config.seed_peer.enable {
// Register the seed peer to the manager.
self.manager_client
.update_seed_peer(UpdateSeedPeerRequest {
source_type: SourceType::SeedPeerSource.into(),
hostname: self.config.host.hostname.clone(),
r#type: self.config.seed_peer.kind.to_string(),
idc: self.config.host.idc.clone(),
location: self.config.host.location.clone(),
ip: self.config.host.ip.unwrap().to_string(),
port: self.config.upload.server.port as i32,
download_port: self.config.upload.server.port as i32,
seed_peer_cluster_id: self.config.seed_peer.cluster_id,
})
.await?;
// Announce to scheduler shutting down with signals.
shutdown.recv().await;
// Delete the seed peer from the manager.
self.manager_client
.delete_seed_peer(DeleteSeedPeerRequest {
source_type: SourceType::SeedPeerSource.into(),
hostname: self.config.host.hostname.clone(),
ip: self.config.host.ip.unwrap().to_string(),
seed_peer_cluster_id: self.config.seed_peer.cluster_id,
})
.await?;
info!("announce to manager shutting down");
} else {
shutdown.recv().await;
info!("announce to manager shutting down");
}
Ok(())
}
}
use tracing::{debug, error, info, instrument};
/// Announcer is used to announce the dfdaemon information to the manager and scheduler.
pub struct SchedulerAnnouncer {
@ -122,8 +43,8 @@ pub struct SchedulerAnnouncer {
/// scheduler_client is the grpc client of the scheduler.
scheduler_client: Arc<SchedulerClient>,
// system is the system information.
system: Arc<Mutex<System>>,
/// interface is the network interface.
interface: Arc<Interface>,
/// shutdown is used to shutdown the announcer.
shutdown: shutdown::Shutdown,
@ -135,11 +56,11 @@ pub struct SchedulerAnnouncer {
/// SchedulerAnnouncer implements the scheduler announcer of the dfdaemon.
impl SchedulerAnnouncer {
/// new creates a new scheduler announcer.
#[instrument(skip_all)]
pub async fn new(
config: Arc<Config>,
host_id: String,
scheduler_client: Arc<SchedulerClient>,
interface: Arc<Interface>,
shutdown: shutdown::Shutdown,
shutdown_complete_tx: mpsc::UnboundedSender<()>,
) -> Result<Self> {
@ -147,7 +68,7 @@ impl SchedulerAnnouncer {
config,
host_id,
scheduler_client,
system: Arc::new(Mutex::new(System::new_all())),
interface,
shutdown,
_shutdown_complete: shutdown_complete_tx,
};
@ -155,13 +76,12 @@ impl SchedulerAnnouncer {
// Initialize the scheduler announcer.
announcer
.scheduler_client
.init_announce_host(announcer.make_announce_host_request(Duration::ZERO)?)
.init_announce_host(announcer.make_announce_host_request(Duration::ZERO).await?)
.await?;
Ok(announcer)
}
/// run announces the dfdaemon information to the scheduler.
#[instrument(skip_all)]
pub async fn run(&self) {
// Clone the shutdown channel.
let mut shutdown = self.shutdown.clone();
@ -171,7 +91,7 @@ impl SchedulerAnnouncer {
loop {
tokio::select! {
_ = interval.tick() => {
let request = match self.make_announce_host_request(interval.period()) {
let request = match self.make_announce_host_request(interval.period()).await {
Ok(request) => request,
Err(err) => {
error!("make announce host request failed: {}", err);
@ -200,7 +120,7 @@ impl SchedulerAnnouncer {
/// make_announce_host_request makes the announce host request.
#[instrument(skip_all)]
fn make_announce_host_request(&self, interval: Duration) -> Result<AnnounceHostRequest> {
async fn make_announce_host_request(&self, interval: Duration) -> Result<AnnounceHostRequest> {
// If the seed peer is enabled, we should announce the seed peer to the scheduler.
let host_type = if self.config.seed_peer.enable {
self.config.seed_peer.kind
@ -209,7 +129,7 @@ impl SchedulerAnnouncer {
};
// Refresh the system information.
let mut sys = self.system.lock().unwrap();
let mut sys = System::new_all();
sys.refresh_all();
// Get the process information.
@ -236,25 +156,25 @@ impl SchedulerAnnouncer {
free: sys.free_memory(),
};
// Wait for getting the network data.
let network_data = self.interface.get_network_data().await;
debug!(
"network data: rx bandwidth {}/{} bps, tx bandwidth {}/{} bps",
network_data.rx_bandwidth.unwrap_or(0),
network_data.max_rx_bandwidth,
network_data.tx_bandwidth.unwrap_or(0),
network_data.max_tx_bandwidth
);
// Get the network information.
let network = Network {
// TODO: Get the count of the tcp connection.
tcp_connection_count: 0,
// TODO: Get the count of the upload tcp connection.
upload_tcp_connection_count: 0,
idc: self.config.host.idc.clone(),
location: self.config.host.location.clone(),
// TODO: Get the network download rate, refer to
// https://docs.rs/sysinfo/latest/sysinfo/struct.NetworkData.html#method.received.
download_rate: 0,
download_rate_limit: self.config.download.rate_limit.as_u64(),
// TODO: Get the network download rate, refer to
// https://docs.rs/sysinfo/latest/sysinfo/struct.NetworkData.html#method.transmitted
upload_rate: 0,
upload_rate_limit: self.config.upload.rate_limit.as_u64(),
max_rx_bandwidth: network_data.max_rx_bandwidth,
rx_bandwidth: network_data.rx_bandwidth,
max_tx_bandwidth: network_data.max_tx_bandwidth,
tx_bandwidth: network_data.tx_bandwidth,
..Default::default()
};
// Get the disk information.

View File

@ -23,7 +23,9 @@ use dragonfly_client_core::{
error::{ErrorType, OrErr},
Error, Result,
};
use dragonfly_client_util::fs::fallocate;
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
use local_ip_address::local_ip;
use path_absolutize::*;
use std::path::{Path, PathBuf};
use std::time::Duration;
@ -48,6 +50,13 @@ pub struct ExportCommand {
)]
transfer_from_dfdaemon: bool,
#[arg(
long = "force-hard-link",
default_value_t = false,
help = "Specify whether the download file must be hard linked to the output path. If hard link is failed, download will be failed. If it is false, dfdaemon will copy the file to the output path if hard link is failed."
)]
force_hard_link: bool,
#[arg(
long = "application",
default_value = "",
@ -77,6 +86,13 @@ pub struct ExportCommand {
)]
timeout: Duration,
#[arg(
long = "digest",
required = false,
help = "Verify the integrity of the downloaded file using the specified digest, support sha256, sha512, crc32. If the digest is not specified, the downloaded file will not be verified. Format: <algorithm>:<digest>, e.g. sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef, crc32:12345678"
)]
digest: Option<String>,
#[arg(
short = 'e',
long = "endpoint",
@ -107,17 +123,19 @@ pub struct ExportCommand {
)]
log_max_files: usize,
#[arg(
long = "verbose",
default_value_t = false,
help = "Specify whether to print log"
)]
verbose: bool,
#[arg(long, default_value_t = false, help = "Specify whether to print log")]
console: bool,
}
/// Implement the execute for ExportCommand.
impl ExportCommand {
/// execute executes the export command.
/// Executes the export command with comprehensive validation and advanced error handling.
///
/// This function serves as the main entry point for the dfcache export command execution.
/// It handles the complete workflow including argument parsing, validation, logging setup,
/// dfdaemon client connection, and export operation execution. The function provides
/// sophisticated error reporting with colored terminal output, including specialized
/// handling for backend errors with HTTP status codes and headers.
pub async fn execute(&self) -> Result<()> {
// Parse command line arguments.
Args::parse();
@ -129,8 +147,12 @@ impl ExportCommand {
self.log_level,
self.log_max_files,
None,
None,
None,
None,
None,
false,
self.verbose,
self.console,
);
// Validate the command line arguments.
@ -420,7 +442,13 @@ impl ExportCommand {
Ok(())
}
/// run runs the export command.
/// Executes the export operation to retrieve cached files from the persistent cache system.
///
/// This function handles the core export functionality by downloading a cached file from the
/// dfdaemon persistent cache system. It supports two transfer modes: direct file transfer
/// by dfdaemon (hardlink/copy) or streaming piece content through the client for manual
/// file assembly. The operation provides real-time progress feedback and handles file
/// creation, directory setup, and efficient piece-by-piece writing with sparse file allocation.
async fn run(&self, dfdaemon_download_client: DfdaemonDownloadClient) -> Result<()> {
// Dfcache needs to notify dfdaemon to transfer the piece content of downloading file via unix domain socket
// when the `transfer_from_dfdaemon` is true. Otherwise, dfdaemon will download the file and hardlink or
@ -448,6 +476,9 @@ impl ExportCommand {
.or_err(ErrorType::ParseError)?,
),
need_piece_content,
force_hard_link: self.force_hard_link,
digest: self.digest.clone(),
remote_ip: Some(local_ip().unwrap().to_string()),
})
.await
.inspect_err(|err| {
@ -481,8 +512,8 @@ impl ExportCommand {
};
// Initialize progress bar.
let pb = ProgressBar::new(0);
pb.set_style(
let progress_bar = ProgressBar::new(0);
progress_bar.set_style(
ProgressStyle::with_template(
"[{elapsed_precise}] [{wide_bar}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})",
)
@ -503,7 +534,15 @@ impl ExportCommand {
Some(download_persistent_cache_task_response::Response::DownloadPersistentCacheTaskStartedResponse(
response,
)) => {
pb.set_length(response.content_length);
if let Some(f) = &f {
fallocate(f, response.content_length)
.await
.inspect_err(|err| {
error!("fallocate {:?} failed: {}", self.output, err);
})?;
}
progress_bar.set_length(response.content_length);
}
Some(download_persistent_cache_task_response::Response::DownloadPieceFinishedResponse(
response,
@ -527,18 +566,23 @@ impl ExportCommand {
};
downloaded += piece.length;
let position = min(downloaded + piece.length, pb.length().unwrap_or(0));
pb.set_position(position);
let position = min(downloaded + piece.length, progress_bar.length().unwrap_or(0));
progress_bar.set_position(position);
}
None => {}
}
}
pb.finish_with_message("downloaded");
progress_bar.finish_with_message("downloaded");
Ok(())
}
/// validate_args validates the command line arguments.
/// Validates command line arguments for the export operation to ensure safe file output.
///
/// This function performs essential validation of the output path to prevent file conflicts
/// and ensure the target location is suitable for export operations. It checks parent
/// directory existence, prevents accidental file overwrites, and validates path accessibility
/// before allowing the export operation to proceed.
fn validate_args(&self) -> Result<()> {
let absolute_path = Path::new(&self.output).absolutize()?;
match absolute_path.parent() {

View File

@ -24,6 +24,7 @@ use dragonfly_client_core::{
Error, Result,
};
use indicatif::{ProgressBar, ProgressStyle};
use local_ip_address::local_ip;
use path_absolutize::*;
use std::path::{Path, PathBuf};
use std::time::Duration;
@ -42,11 +43,10 @@ pub struct ImportCommand {
path: PathBuf,
#[arg(
long = "id",
required = false,
help = "Specify the id of the persistent cache task. If id is none, dfdaemon will generate the new task id based on the file content, tag and application by crc32 algorithm."
long = "content-for-calculating-task-id",
help = "Specify the content used to calculate the persistent cache task ID. If it is set, use its value to calculate the task ID, Otherwise, calculate the persistent cache task ID based on url, piece-length, tag, application, and filtered-query-params."
)]
id: Option<String>,
content_for_calculating_task_id: Option<String>,
#[arg(
long = "persistent-replica-count",
@ -122,17 +122,19 @@ pub struct ImportCommand {
)]
log_max_files: usize,
#[arg(
long = "verbose",
default_value_t = false,
help = "Specify whether to print log"
)]
verbose: bool,
#[arg(long, default_value_t = false, help = "Specify whether to print log")]
console: bool,
}
/// Implement the execute for ImportCommand.
impl ImportCommand {
/// execute executes the import sub command.
/// Executes the import sub command with comprehensive validation and error handling.
///
/// This function serves as the main entry point for the dfcache import command execution.
/// It handles the complete workflow including argument parsing, validation, logging setup,
/// dfdaemon client connection, and import operation execution. The function provides
/// detailed error reporting with colored terminal output and follows a fail-fast approach
/// with immediate process termination on any critical failures.
pub async fn execute(&self) -> Result<()> {
// Parse command line arguments.
Args::parse();
@ -144,8 +146,12 @@ impl ImportCommand {
self.log_level,
self.log_max_files,
None,
None,
None,
None,
None,
false,
self.verbose,
self.console,
);
// Validate the command line arguments.
@ -326,23 +332,29 @@ impl ImportCommand {
Ok(())
}
/// run runs the import sub command.
/// Executes the cache import operation by uploading a file to the persistent cache system.
///
/// This function handles the core import functionality by uploading a local file to the
/// dfdaemon persistent cache system. It provides visual feedback through a progress spinner,
/// converts the file path to absolute format, and configures the cache task with specified
/// parameters including TTL, replica count, and piece length. The operation is asynchronous
/// and provides completion feedback with the generated task ID.
async fn run(&self, dfdaemon_download_client: DfdaemonDownloadClient) -> Result<()> {
let absolute_path = Path::new(&self.path).absolutize()?;
info!("import file: {}", absolute_path.to_string_lossy());
let pb = ProgressBar::new_spinner();
pb.enable_steady_tick(DEFAULT_PROGRESS_BAR_STEADY_TICK_INTERVAL);
pb.set_style(
let progress_bar = ProgressBar::new_spinner();
progress_bar.enable_steady_tick(DEFAULT_PROGRESS_BAR_STEADY_TICK_INTERVAL);
progress_bar.set_style(
ProgressStyle::with_template("{spinner:.blue} {msg}")
.unwrap()
.tick_strings(&["", "", "", "", "", "", "", ""]),
);
pb.set_message("Importing...");
progress_bar.set_message("Importing...");
let persistent_cache_task = dfdaemon_download_client
.upload_persistent_cache_task(UploadPersistentCacheTaskRequest {
task_id: self.id.clone(),
content_for_calculating_task_id: self.content_for_calculating_task_id.clone(),
path: absolute_path.to_string_lossy().to_string(),
persistent_replica_count: self.persistent_replica_count,
tag: self.tag.clone(),
@ -355,14 +367,20 @@ impl ImportCommand {
prost_wkt_types::Duration::try_from(self.timeout)
.or_err(ErrorType::ParseError)?,
),
remote_ip: Some(local_ip().unwrap().to_string()),
})
.await?;
pb.finish_with_message(format!("Done: {}", persistent_cache_task.id));
progress_bar.finish_with_message(format!("Done: {}", persistent_cache_task.id));
Ok(())
}
/// validate_args validates the command line arguments.
/// Validates command line arguments for the import operation to ensure safe and correct execution.
///
/// This function performs comprehensive validation of import-specific parameters to prevent
/// invalid operations and ensure the import request meets all system requirements. It validates
/// TTL boundaries, file existence and type, and piece length constraints before allowing the
/// import operation to proceed.
fn validate_args(&self) -> Result<()> {
if self.ttl < Duration::from_secs(5 * 60)
|| self.ttl > Duration::from_secs(7 * 24 * 60 * 60)
@ -373,15 +391,6 @@ impl ImportCommand {
)));
}
if let Some(id) = self.id.as_ref() {
if id.len() != 64 {
return Err(Error::ValidationError(format!(
"id length must be 64 bytes, but got {}",
id.len()
)));
}
}
if self.path.is_dir() {
return Err(Error::ValidationError(format!(
"path {} is a directory",

View File

@ -106,7 +106,12 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}
/// get_and_check_dfdaemon_download_client gets a dfdaemon download client and checks its health.
/// Creates and validates a dfdaemon download client with health checking.
///
/// This function establishes a connection to the dfdaemon service via Unix domain socket
/// and performs a health check to ensure the service is running and ready to handle
/// download requests. Only after successful health verification does it return the
/// download client for actual use.
pub async fn get_dfdaemon_download_client(endpoint: PathBuf) -> Result<DfdaemonDownloadClient> {
// Check dfdaemon's health.
let health_client = HealthClient::new_unix(endpoint.clone()).await?;

View File

@ -22,6 +22,7 @@ use dragonfly_client_core::{
Error, Result,
};
use humantime::format_duration;
use local_ip_address::local_ip;
use std::time::Duration;
use tabled::{
settings::{object::Rows, Alignment, Modify, Style},
@ -67,17 +68,19 @@ pub struct StatCommand {
)]
log_max_files: usize,
#[arg(
long = "verbose",
default_value_t = false,
help = "Specify whether to print log"
)]
verbose: bool,
#[arg(long, default_value_t = false, help = "Specify whether to print log")]
console: bool,
}
/// Implement the execute for StatCommand.
impl StatCommand {
/// execute executes the stat command.
/// Executes the stat command with comprehensive error handling and user feedback.
///
/// This function serves as the main entry point for the dfcache stat command execution.
/// It handles the complete lifecycle including argument parsing, logging initialization,
/// dfdaemon client setup, and command execution with detailed error reporting. The
/// function provides colored terminal output for better user experience and exits
/// with appropriate status codes on failure.
pub async fn execute(&self) -> Result<()> {
// Parse command line arguments.
Args::parse();
@ -89,8 +92,12 @@ impl StatCommand {
self.log_level,
self.log_max_files,
None,
None,
None,
None,
None,
false,
self.verbose,
self.console,
);
// Get dfdaemon download client.
@ -233,11 +240,17 @@ impl StatCommand {
Ok(())
}
/// run runs the stat command.
/// Executes the stat command to retrieve and display persistent cache task information.
///
/// This function queries the dfdaemon service for detailed information about a specific
/// persistent cache task and presents it in a formatted table for user consumption.
/// It handles data conversion from raw protocol buffer values to human-readable formats
/// including byte sizes, durations, and timestamps with proper timezone conversion.
async fn run(&self, dfdaemon_download_client: DfdaemonDownloadClient) -> Result<()> {
let task = dfdaemon_download_client
.stat_persistent_cache_task(StatPersistentCacheTaskRequest {
task_id: self.id.clone(),
remote_ip: Some(local_ip().unwrap().to_string()),
})
.await?;

View File

@ -15,7 +15,7 @@
*/
use clap::Parser;
use dragonfly_client::announcer::{ManagerAnnouncer, SchedulerAnnouncer};
use dragonfly_client::announcer::SchedulerAnnouncer;
use dragonfly_client::dynconfig::Dynconfig;
use dragonfly_client::gc::GC;
use dragonfly_client::grpc::{
@ -30,10 +30,9 @@ use dragonfly_client::shutdown;
use dragonfly_client::stats::Stats;
use dragonfly_client::tracing::init_tracing;
use dragonfly_client_backend::BackendFactory;
use dragonfly_client_config::dfdaemon;
use dragonfly_client_config::VersionValueParser;
use dragonfly_client_config::{dfdaemon, VersionValueParser};
use dragonfly_client_storage::Storage;
use dragonfly_client_util::id_generator::IDGenerator;
use dragonfly_client_util::{id_generator::IDGenerator, net::Interface};
use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::Arc;
@ -92,12 +91,8 @@ struct Args {
)]
log_max_files: usize,
#[arg(
long = "verbose",
default_value_t = true,
help = "Specify whether to print log"
)]
verbose: bool,
#[arg(long, default_value_t = true, help = "Specify whether to print log")]
console: bool,
#[arg(
short = 'V',
@ -150,9 +145,13 @@ async fn main() -> Result<(), anyhow::Error> {
args.log_dir.clone(),
args.log_level,
args.log_max_files,
config.tracing.addr.to_owned(),
config.tracing.flamegraph,
args.verbose,
config.tracing.protocol.clone(),
config.tracing.endpoint.clone(),
config.tracing.path.clone(),
Some(config.tracing.headers.clone()),
Some(config.host.clone()),
config.seed_peer.enable,
args.console,
);
// Initialize storage.
@ -230,6 +229,9 @@ async fn main() -> Result<(), anyhow::Error> {
)?;
let persistent_cache_task = Arc::new(persistent_cache_task);
let interface = Interface::new(config.host.ip.unwrap(), config.upload.rate_limit);
let interface = Arc::new(interface);
// Initialize health server.
let health = Health::new(
SocketAddr::new(config.health.server.ip.unwrap(), config.health.server.port),
@ -259,19 +261,12 @@ async fn main() -> Result<(), anyhow::Error> {
shutdown_complete_tx.clone(),
);
// Initialize manager announcer.
let manager_announcer = ManagerAnnouncer::new(
config.clone(),
manager_client.clone(),
shutdown.clone(),
shutdown_complete_tx.clone(),
);
// Initialize scheduler announcer.
let scheduler_announcer = SchedulerAnnouncer::new(
config.clone(),
id_generator.host_id(),
scheduler_client.clone(),
interface.clone(),
shutdown.clone(),
shutdown_complete_tx.clone(),
)
@ -286,6 +281,7 @@ async fn main() -> Result<(), anyhow::Error> {
SocketAddr::new(config.upload.server.ip.unwrap(), config.upload.server.port),
task.clone(),
persistent_cache_task.clone(),
interface.clone(),
shutdown.clone(),
shutdown_complete_tx.clone(),
);
@ -334,10 +330,6 @@ async fn main() -> Result<(), anyhow::Error> {
info!("stats server exited");
},
_ = tokio::spawn(async move { manager_announcer.run().await.unwrap_or_else(|err| error!("announcer manager failed: {}", err))} ) => {
info!("announcer manager exited");
},
_ = tokio::spawn(async move { scheduler_announcer.run().await }) => {
info!("announcer scheduler exited");
},

View File

@ -17,29 +17,30 @@
use bytesize::ByteSize;
use clap::Parser;
use dragonfly_api::common::v2::{Download, Hdfs, ObjectStorage, TaskType};
use dragonfly_api::dfdaemon::v2::{download_task_response, DownloadTaskRequest};
use dragonfly_api::dfdaemon::v2::{
download_task_response, DownloadTaskRequest, ListTaskEntriesRequest,
};
use dragonfly_api::errordetails::v2::Backend;
use dragonfly_client::grpc::dfdaemon_download::DfdaemonDownloadClient;
use dragonfly_client::grpc::health::HealthClient;
use dragonfly_client::metrics::{
collect_backend_request_failure_metrics, collect_backend_request_finished_metrics,
collect_backend_request_started_metrics,
};
use dragonfly_client::resource::piece::MIN_PIECE_LENGTH;
use dragonfly_client::tracing::init_tracing;
use dragonfly_client_backend::{hdfs, object_storage, BackendFactory, DirEntry, HeadRequest};
use dragonfly_client_backend::{hdfs, object_storage, BackendFactory, DirEntry};
use dragonfly_client_config::VersionValueParser;
use dragonfly_client_config::{self, dfdaemon, dfget};
use dragonfly_client_core::error::{BackendError, ErrorType, OrErr};
use dragonfly_client_core::error::{ErrorType, OrErr};
use dragonfly_client_core::{Error, Result};
use dragonfly_client_util::http::{header_vec_to_hashmap, header_vec_to_headermap};
use dragonfly_client_util::{fs::fallocate, http::header_vec_to_hashmap};
use glob::Pattern;
use indicatif::{MultiProgress, ProgressBar, ProgressState, ProgressStyle};
use local_ip_address::local_ip;
use path_absolutize::*;
use percent_encoding::percent_decode_str;
use std::path::{Path, PathBuf};
use std::collections::{HashMap, HashSet};
use std::path::{Component, Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::time::Duration;
use std::{cmp::min, fmt::Write};
use termion::{color, style};
use tokio::fs::{self, OpenOptions};
@ -101,6 +102,19 @@ struct Args {
)]
transfer_from_dfdaemon: bool,
#[arg(
long = "force-hard-link",
default_value_t = false,
help = "Specify whether the download file must be hard linked to the output path. If hard link is failed, download will be failed. If it is false, dfdaemon will copy the file to the output path if hard link is failed."
)]
force_hard_link: bool,
#[arg(
long = "content-for-calculating-task-id",
help = "Specify the content used to calculate the task ID. If it is set, use its value to calculate the task ID, Otherwise, calculate the task ID based on URL, piece-length, tag, application, and filtered-query-params."
)]
content_for_calculating_task_id: Option<String>,
#[arg(
short = 'O',
long = "output",
@ -125,12 +139,11 @@ struct Args {
timeout: Duration,
#[arg(
short = 'd',
long = "digest",
default_value = "",
help = "Verify the integrity of the downloaded file using the specified digest, e.g. md5:86d3f3a95c324c9479bd8986968f4327"
required = false,
help = "Verify the integrity of the downloaded file using the specified digest, support sha256, sha512, crc32. If the digest is not specified, the downloaded file will not be verified. Format: <algorithm>:<digest>. Examples: sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef, crc32:12345678"
)]
digest: String,
digest: Option<String>,
#[arg(
short = 'p',
@ -150,14 +163,14 @@ struct Args {
#[arg(
long = "application",
default_value = "",
help = "Different applications for the same url will be divided into different tasks"
help = "Different applications for the same URL will be divided into different tasks"
)]
application: String,
#[arg(
long = "tag",
default_value = "",
help = "Different tags for the same url will be divided into different tasks"
help = "Different tags for the same URL will be divided into different tasks"
)]
tag: String,
@ -165,17 +178,24 @@ struct Args {
short = 'H',
long = "header",
required = false,
help = "Specify the header for downloading file, e.g. --header='Content-Type: application/json' --header='Accept: application/json'"
help = "Specify the header for downloading file. Examples: --header='Content-Type: application/json' --header='Accept: application/json'"
)]
header: Option<Vec<String>>,
#[arg(
long = "filtered-query-param",
required = false,
help = "Filter the query parameters of the downloaded URL. If the download URL is the same, it will be scheduled as the same task, e.g. --filtered-query-param='signature' --filtered-query-param='timeout'"
help = "Filter the query parameters of the downloaded URL. If the download URL is the same, it will be scheduled as the same task. Examples: --filtered-query-param='signature' --filtered-query-param='timeout'"
)]
filtered_query_params: Option<Vec<String>>,
#[arg(
long = "include-files",
required = false,
help = "Filter files to download in a directory using glob patterns relative to the root URL's path. Examples: --include-files='*.txt' --include-files='subdir/file.txt'"
)]
include_files: Option<Vec<String>>,
#[arg(
long = "disable-back-to-source",
default_value_t = false,
@ -262,12 +282,8 @@ struct Args {
)]
log_max_files: usize,
#[arg(
long = "verbose",
default_value_t = false,
help = "Specify whether to print log"
)]
verbose: bool,
#[arg(long, default_value_t = false, help = "Specify whether to print log")]
console: bool,
#[arg(
short = 'V',
@ -292,8 +308,12 @@ async fn main() -> anyhow::Result<()> {
args.log_level,
args.log_max_files,
None,
None,
None,
None,
None,
false,
args.verbose,
args.console,
);
// Validate command line arguments.
@ -583,7 +603,12 @@ async fn main() -> anyhow::Result<()> {
Ok(())
}
/// run runs the dfget command.
/// Runs the dfget command to download files or directories from a given URL.
///
/// This function serves as the main entry point for the dfget download operation.
/// It handles both single file downloads and directory downloads based on the URL format.
/// The function performs path normalization, validates the URL scheme's capabilities,
/// and delegates to the appropriate download handler.
async fn run(mut args: Args, dfdaemon_download_client: DfdaemonDownloadClient) -> Result<()> {
// Get the absolute path of the output file.
args.output = Path::new(&args.output).absolutize()?.into();
@ -593,7 +618,7 @@ async fn run(mut args: Args, dfdaemon_download_client: DfdaemonDownloadClient) -
// then download all files in the directory. Otherwise, download the single file.
let scheme = args.url.scheme();
if args.url.path().ends_with('/') {
if !BackendFactory::supported_download_directory(scheme) {
if BackendFactory::unsupported_download_directory(scheme) {
return Err(Error::Unsupported(format!("{} download directory", scheme)));
};
@ -603,7 +628,13 @@ async fn run(mut args: Args, dfdaemon_download_client: DfdaemonDownloadClient) -
download(args, ProgressBar::new(0), dfdaemon_download_client).await
}
/// download_dir downloads all files in the directory.
/// Downloads all files in a directory from various storage backends (object storage, HDFS, etc.).
///
/// This function handles directory-based downloads by recursively fetching all entries
/// in the specified directory. It supports filtering files based on include patterns,
/// enforces download limits, and performs concurrent downloads with configurable
/// concurrency control. The function creates the necessary directory structure
/// locally and downloads files while preserving the remote directory hierarchy.
async fn download_dir(args: Args, download_client: DfdaemonDownloadClient) -> Result<()> {
// Initialize the object storage config and the hdfs config.
let object_storage = Some(ObjectStorage {
@ -620,12 +651,17 @@ async fn download_dir(args: Args, download_client: DfdaemonDownloadClient) -> Re
delegation_token: args.hdfs_delegation_token.clone(),
});
// Get all entries in the directory. If the directory is empty, then return directly.
let entries = get_entries(args.clone(), object_storage, hdfs).await?;
// Get all entries in the directory.
let mut entries = get_entries(&args, object_storage, hdfs, download_client.clone()).await?;
if let Some(ref include_files) = args.include_files {
entries = filter_entries(&args.url, entries, include_files)?;
}
// If the entries is empty, then return directly.
if entries.is_empty() {
warn!("directory {} is empty", args.url);
warn!("no entries found in directory {}", args.url);
return Ok(());
};
}
// If the actual file count is greater than the max_files, then reject the downloading.
let count = entries.iter().filter(|entry| !entry.is_dir).count();
@ -696,7 +732,13 @@ async fn download_dir(args: Args, download_client: DfdaemonDownloadClient) -> Re
Ok(())
}
/// download downloads the single file.
/// Downloads a single file from various storage backends using the dfdaemon service.
///
/// This function handles single file downloads by communicating with a dfdaemon client.
/// It supports multiple storage protocols (object storage, HDFS, HTTP/HTTPS) and provides
/// two transfer modes: direct download by dfdaemon or streaming piece content through
/// the client. The function includes progress tracking, file creation, and proper error
/// handling throughout the download process.
async fn download(
args: Args,
progress_bar: ProgressBar,
@ -743,7 +785,7 @@ async fn download(
.download_task(DownloadTaskRequest {
download: Some(Download {
url: args.url.to_string(),
digest: Some(args.digest),
digest: args.digest,
// NOTE: Dfget does not support range download.
range: None,
r#type: TaskType::Standard as i32,
@ -766,7 +808,9 @@ async fn download(
need_piece_content,
object_storage,
hdfs,
load_to_cache: false,
force_hard_link: args.force_hard_link,
content_for_calculating_task_id: args.content_for_calculating_task_id,
remote_ip: Some(local_ip().unwrap().to_string()),
}),
})
.await
@ -822,6 +866,14 @@ async fn download(
})? {
match message.response {
Some(download_task_response::Response::DownloadTaskStartedResponse(response)) => {
if let Some(f) = &f {
fallocate(f, response.content_length)
.await
.inspect_err(|err| {
error!("fallocate {:?} failed: {}", args.output, err);
})?;
}
progress_bar.set_length(response.content_length);
}
Some(download_task_response::Response::DownloadPieceFinishedResponse(response)) => {
@ -858,69 +910,116 @@ async fn download(
Ok(())
}
/// get_entries gets all entries in the directory.
/// Retrieves all directory entries from a remote storage location.
///
/// This function communicates with the dfdaemon service to list all entries
/// (files and subdirectories) in the specified directory URL. It supports
/// various storage backends including object storage and HDFS by passing
/// the appropriate credentials and configuration. The function converts
/// the gRPC response into a local `DirEntry` format for further processing.
async fn get_entries(
args: Args,
args: &Args,
object_storage: Option<ObjectStorage>,
hdfs: Option<Hdfs>,
download_client: DfdaemonDownloadClient,
) -> Result<Vec<DirEntry>> {
// Initialize backend factory and build backend.
let backend_factory = BackendFactory::new(None)?;
let backend = backend_factory.build(args.url.as_str())?;
// Collect backend request started metrics.
collect_backend_request_started_metrics(backend.scheme().as_str(), http::Method::HEAD.as_str());
// Record the start time.
let start_time = Instant::now();
let response = backend
.head(HeadRequest {
// NOTE: Mock a task id for head request.
info!("list task entries: {:?}", args.url);
// List task entries.
let response = download_client
.list_task_entries(ListTaskEntriesRequest {
task_id: Uuid::new_v4().to_string(),
url: args.url.to_string(),
http_header: Some(header_vec_to_headermap(
args.header.clone().unwrap_or_default(),
)?),
timeout: args.timeout,
client_cert: None,
request_header: header_vec_to_hashmap(args.header.clone().unwrap_or_default())?,
timeout: None,
certificate_chain: Vec::new(),
object_storage,
hdfs,
remote_ip: Some(local_ip().unwrap().to_string()),
})
.await
.inspect_err(|_err| {
// Collect backend request failure metrics.
collect_backend_request_failure_metrics(
backend.scheme().as_str(),
http::Method::HEAD.as_str(),
);
.inspect_err(|err| {
error!("list task entries failed: {}", err);
})?;
// Return error when response is failed.
if !response.success {
// Collect backend request failure metrics.
collect_backend_request_failure_metrics(
backend.scheme().as_str(),
http::Method::HEAD.as_str(),
);
return Err(Error::BackendError(Box::new(BackendError {
message: response.error_message.unwrap_or_default(),
status_code: Some(response.http_status_code.unwrap_or_default()),
header: Some(response.http_header.unwrap_or_default()),
})));
}
// Collect backend request finished metrics.
collect_backend_request_finished_metrics(
backend.scheme().as_str(),
http::Method::HEAD.as_str(),
start_time.elapsed(),
);
Ok(response.entries)
Ok(response
.entries
.into_iter()
.map(|entry| DirEntry {
url: entry.url,
content_length: entry.content_length as usize,
is_dir: entry.is_dir,
})
.collect())
}
/// make_output_by_entry makes the output path by the entry information.
/// Filters directory entries based on include patterns and validates their URLs.
///
/// This function takes a collection of directory entries and filters them based on
/// glob patterns specified in `include_files`. It performs URL validation to ensure
/// all entries have valid URLs and that their paths fall within the scope of the
/// root URL. When an entry matches a pattern, both the entry and its parent
/// directory (if it exists) are included in the result.
fn filter_entries(
url: &Url,
entries: Vec<DirEntry>,
include_files: &[String],
) -> Result<Vec<DirEntry>> {
let patterns: Vec<Pattern> = include_files
.iter()
.filter_map(|include_file| Pattern::new(include_file).ok())
.collect();
// Build a HashMap of DirEntry objects keyed by relative paths for filtering and
// validates URLs and ensures paths are within the root URL's scope.
let mut entries_by_relative_path = HashMap::with_capacity(entries.len());
for entry in entries {
let entry_url = Url::parse(&entry.url).map_err(|err| {
error!("failed to parse entry URL '{}': {}", entry.url, err);
Error::ValidationError(format!("invalid URL: {}", entry.url))
})?;
let entry_path = entry_url.path();
match entry_path.strip_prefix(url.path()) {
Some(relative_path) => entries_by_relative_path
.insert(relative_path.trim_start_matches('/').to_string(), entry),
None => {
error!(
"entry path '{}' does not belong to the root path",
entry_path
);
return Err(Error::ValidationError(format!(
"path '{}' is outside the expected scope",
entry_path
)));
}
};
}
// Filter entries by matching relative paths against patterns, including
// parent directories for matches.
let mut filtered_entries = HashSet::new();
for (relative_path, entry) in &entries_by_relative_path {
if patterns.iter().any(|pat| pat.matches(relative_path)) {
filtered_entries.insert(entry.clone());
if let Some(parent) = std::path::Path::new(relative_path).parent() {
if let Some(parent_entry) =
entries_by_relative_path.get(&parent.join("").to_string_lossy().to_string())
{
filtered_entries.insert(parent_entry.clone());
}
}
}
}
Ok(filtered_entries.into_iter().collect())
}
/// Constructs the local output path for a directory entry based on its remote URL.
///
/// This function maps a remote directory entry to its corresponding local file system
/// path by replacing the remote root directory with the local output directory.
/// It handles URL percent-decoding to ensure proper path construction and maintains
/// the relative directory structure from the remote source.
fn make_output_by_entry(url: Url, output: &Path, entry: DirEntry) -> Result<PathBuf> {
// Get the root directory of the download directory and the output root directory.
let root_dir = url.path().to_string();
@ -938,7 +1037,12 @@ fn make_output_by_entry(url: Url, output: &Path, entry: DirEntry) -> Result<Path
.into())
}
/// get_and_check_dfdaemon_download_client gets a dfdaemon download client and checks its health.
/// Creates and validates a dfdaemon download client with health checking.
///
/// This function establishes a connection to the dfdaemon service via Unix domain socket
/// and performs a health check to ensure the service is running and ready to handle
/// download requests. Only after successful health verification does it return the
/// download client for actual use.
async fn get_dfdaemon_download_client(endpoint: PathBuf) -> Result<DfdaemonDownloadClient> {
// Check dfdaemon's health.
let health_client = HealthClient::new_unix(endpoint.clone()).await?;
@ -949,7 +1053,13 @@ async fn get_dfdaemon_download_client(endpoint: PathBuf) -> Result<DfdaemonDownl
Ok(dfdaemon_download_client)
}
/// validate_args validates the command line arguments.
/// Validates command line arguments for consistency and safety requirements.
///
/// This function performs comprehensive validation of the download arguments to ensure
/// they are logically consistent and safe to execute. It checks URL-output path matching,
/// directory existence, file conflicts, piece length constraints, and glob pattern validity.
/// The validation prevents common user errors and potential security issues before
/// starting the download process.
fn validate_args(args: &Args) -> Result<()> {
// If the URL is a directory, the output path should be a directory.
if args.url.path().ends_with('/') && !args.output.is_dir() {
@ -998,9 +1108,42 @@ fn validate_args(args: &Args) -> Result<()> {
}
}
if let Some(ref include_files) = args.include_files {
for include_file in include_files {
if Pattern::new(include_file).is_err() {
return Err(Error::ValidationError(format!(
"invalid glob pattern in include_files: '{}'",
include_file
)));
}
if !is_normal_relative_path(include_file) {
return Err(Error::ValidationError(format!(
"path is not a normal relative path in include_files: '{}'. It must not contain '..', '.', or start with '/'.",
include_file
)));
}
}
}
Ok(())
}
/// Validates that a path string is a normal relative path without unsafe components.
///
/// This function ensures that a given path is both relative (doesn't start with '/')
/// and contains only normal path components. It rejects paths with parent directory
/// references ('..'), current directory references ('.'), or any other special
/// path components that could be used for directory traversal attacks or unexpected
/// file system navigation.
fn is_normal_relative_path(path: &str) -> bool {
let path = Path::new(path);
path.is_relative()
&& path
.components()
.all(|comp| matches!(comp, Component::Normal(_)))
}
#[cfg(test)]
mod tests {
use super::*;
@ -1164,4 +1307,346 @@ mod tests {
let result = make_output_by_entry(url, output, entry);
assert!(result.is_err());
}
#[test]
fn should_filter_entries() {
let test_cases = vec![
(
Url::parse("http://example.com/root/").unwrap(),
vec![
DirEntry {
url: "http://example.com/root/dir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/file.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/file2.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file3.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file4.png".to_string(),
content_length: 100,
is_dir: false,
},
],
vec!["dir/file.txt".to_string()],
vec![
DirEntry {
url: "http://example.com/root/dir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/file.txt".to_string(),
content_length: 100,
is_dir: false,
},
],
),
(
Url::parse("http://example.com/root/").unwrap(),
vec![
DirEntry {
url: "http://example.com/root/dir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/file.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/file2.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file3.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file4.png".to_string(),
content_length: 100,
is_dir: false,
},
],
vec![
"dir/file.txt".to_string(),
"dir/subdir/file4.png".to_string(),
],
vec![
DirEntry {
url: "http://example.com/root/dir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/file.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file4.png".to_string(),
content_length: 100,
is_dir: false,
},
],
),
(
Url::parse("http://example.com/root/").unwrap(),
vec![
DirEntry {
url: "http://example.com/root/dir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/file.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/file2.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file3.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file4.png".to_string(),
content_length: 100,
is_dir: false,
},
],
vec!["dir/subdir/*.png".to_string()],
vec![
DirEntry {
url: "http://example.com/root/dir/subdir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file4.png".to_string(),
content_length: 100,
is_dir: false,
},
],
),
(
Url::parse("http://example.com/root/").unwrap(),
vec![
DirEntry {
url: "http://example.com/root/dir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/file.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/file2.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file3.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file4.png".to_string(),
content_length: 100,
is_dir: false,
},
],
vec!["dir/*".to_string()],
vec![
DirEntry {
url: "http://example.com/root/dir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/file.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/file2.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file3.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file4.png".to_string(),
content_length: 100,
is_dir: false,
},
],
),
(
Url::parse("http://example.com/root/").unwrap(),
vec![
DirEntry {
url: "http://example.com/root/dir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/file.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/file2.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file3.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file4.png".to_string(),
content_length: 100,
is_dir: false,
},
],
vec!["dir/".to_string()],
vec![DirEntry {
url: "http://example.com/root/dir/".to_string(),
content_length: 10,
is_dir: true,
}],
),
(
Url::parse("http://example.com/root/").unwrap(),
vec![
DirEntry {
url: "http://example.com/root/dir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/file.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/file2.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/".to_string(),
content_length: 10,
is_dir: true,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file3.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: "http://example.com/root/dir/subdir/file4.png".to_string(),
content_length: 100,
is_dir: false,
},
],
vec!["test".to_string()],
vec![],
),
(
Url::parse("http://example.com/root/").unwrap(),
vec![
DirEntry {
url: "http://example.com/root/dir/file.txt".to_string(),
content_length: 100,
is_dir: false,
},
DirEntry {
url: " ".to_string(),
content_length: 100,
is_dir: false,
},
],
vec!["dir/file.txt".to_string()],
vec![],
),
];
for (url, entries, include_files, expected_entries) in test_cases {
let result = filter_entries(&url, entries, &include_files);
if result.is_err() {
assert!(matches!(result, Err(Error::ValidationError(_))));
} else {
let filtered_entries = result.unwrap();
assert_eq!(filtered_entries.len(), expected_entries.len());
for filtered_entry in &filtered_entries {
assert!(expected_entries
.iter()
.any(|expected_entry| { expected_entry.url == filtered_entry.url }));
}
}
}
}
}

View File

@ -1,132 +0,0 @@
/*
* Copyright 2023 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use clap::{Parser, Subcommand};
use dragonfly_client::tracing::init_tracing;
use dragonfly_client_config::VersionValueParser;
use dragonfly_client_config::{dfdaemon, dfstore};
use std::path::PathBuf;
use tracing::Level;
#[derive(Debug, Parser)]
#[command(
name = dfstore::NAME,
author,
version,
about = "dfstore is a storage command line based on P2P technology in Dragonfly.",
long_about = "A storage command line based on P2P technology in Dragonfly that can rely on different types of object storage, \
such as S3 or OSS, to provide stable object storage capabilities. It uses the entire P2P network as a cache when storing objects. \
Rely on S3 or OSS as the backend to ensure storage reliability. In the process of object storage, \
P2P cache is effectively used for fast read and write storage.",
disable_version_flag = true
)]
struct Args {
#[arg(
short = 'e',
long = "endpoint",
default_value_os_t = dfdaemon::default_download_unix_socket_path(),
help = "Endpoint of dfdaemon's GRPC server"
)]
endpoint: PathBuf,
#[arg(
short = 'l',
long,
default_value = "info",
help = "Specify the logging level [trace, debug, info, warn, error]"
)]
log_level: Level,
#[arg(
long,
default_value_os_t = dfstore::default_dfstore_log_dir(),
help = "Specify the log directory"
)]
log_dir: PathBuf,
#[arg(
long,
default_value_t = 6,
help = "Specify the max number of log files"
)]
log_max_files: usize,
#[arg(
long = "verbose",
default_value_t = true,
help = "Specify whether to print log"
)]
verbose: bool,
#[arg(
short = 'V',
long = "version",
help = "Print version information",
default_value_t = false,
action = clap::ArgAction::SetTrue,
value_parser = VersionValueParser
)]
version: bool,
#[command(subcommand)]
command: Command,
}
#[derive(Debug, Clone, Subcommand)]
#[command()]
pub enum Command {
#[command(
name = "cp",
author,
version,
about = "Download or upload files using object storage in Dragonfly",
long_about = "Download a file from object storage in Dragonfly or upload a local file to object storage in Dragonfly"
)]
Copy(CopyCommand),
#[command(
name = "rm",
author,
version,
about = "Remove a file from Dragonfly object storage",
long_about = "Remove the P2P cache in Dragonfly and remove the file stored in the object storage."
)]
Remove(RemoveCommand),
}
/// Download or upload files using object storage in Dragonfly.
#[derive(Debug, Clone, Parser)]
pub struct CopyCommand {}
/// Remove a file from Dragonfly object storage.
#[derive(Debug, Clone, Parser)]
pub struct RemoveCommand {}
fn main() {
// Parse command line arguments.
let args = Args::parse();
// Initialize tracing.
let _guards = init_tracing(
dfstore::NAME,
args.log_dir,
args.log_level,
args.log_max_files,
None,
false,
args.verbose,
);
}

View File

@ -25,7 +25,7 @@ use dragonfly_client_core::{Error, Result};
use std::sync::Arc;
use tokio::sync::{mpsc, Mutex, RwLock};
use tonic_health::pb::health_check_response::ServingStatus;
use tracing::{error, info, instrument};
use tracing::{debug, error, info, instrument};
use url::Url;
/// Data is the dynamic configuration of the dfdaemon.
@ -65,7 +65,6 @@ pub struct Dynconfig {
/// Dynconfig is the implementation of Dynconfig.
impl Dynconfig {
/// new creates a new Dynconfig.
#[instrument(skip_all)]
pub async fn new(
config: Arc<Config>,
manager_client: Arc<ManagerClient>,
@ -88,7 +87,6 @@ impl Dynconfig {
}
/// run starts the dynconfig server.
#[instrument(skip_all)]
pub async fn run(&self) {
// Clone the shutdown channel.
let mut shutdown = self.shutdown.clone();
@ -98,9 +96,10 @@ impl Dynconfig {
loop {
tokio::select! {
_ = interval.tick() => {
if let Err(err) = self.refresh().await {
error!("refresh dynconfig failed: {}", err);
};
match self.refresh().await {
Err(err) => error!("refresh dynconfig failed: {}", err),
Ok(_) => debug!("refresh dynconfig success"),
}
}
_ = shutdown.recv() => {
// Dynconfig server shutting down with signals.
@ -163,6 +162,7 @@ impl Dynconfig {
location: self.config.host.location.clone(),
version: CARGO_PKG_VERSION.to_string(),
commit: GIT_COMMIT_SHORT_HASH.to_string(),
scheduler_cluster_id: self.config.host.scheduler_cluster_id.unwrap_or(0),
})
.await
}

View File

@ -53,7 +53,6 @@ pub struct GC {
impl GC {
/// new creates a new GC.
#[instrument(skip_all)]
pub fn new(
config: Arc<Config>,
host_id: String,
@ -73,7 +72,6 @@ impl GC {
}
/// run runs the garbage collector.
#[instrument(skip_all)]
pub async fn run(&self) {
// Clone the shutdown channel.
let mut shutdown = self.shutdown.clone();
@ -127,6 +125,7 @@ impl GC {
}
}
info!("evict by task ttl done");
Ok(())
}
@ -153,6 +152,8 @@ impl GC {
if let Err(err) = self.evict_task_space(need_evict_space as u64).await {
info!("failed to evict task by disk usage: {}", err);
}
info!("evict task by disk usage done");
}
Ok(())
@ -241,6 +242,7 @@ impl GC {
}
}
info!("evict by persistent cache task ttl done");
Ok(())
}
@ -270,6 +272,8 @@ impl GC {
{
info!("failed to evict task by disk usage: {}", err);
}
info!("evict persistent cache task by disk usage done");
}
Ok(())

View File

@ -18,31 +18,40 @@ use crate::metrics::{
collect_delete_host_failure_metrics, collect_delete_host_started_metrics,
collect_delete_task_failure_metrics, collect_delete_task_started_metrics,
collect_download_task_failure_metrics, collect_download_task_finished_metrics,
collect_download_task_started_metrics, collect_stat_task_failure_metrics,
collect_download_task_started_metrics, collect_list_task_entries_failure_metrics,
collect_list_task_entries_started_metrics, collect_stat_task_failure_metrics,
collect_stat_task_started_metrics, collect_upload_task_failure_metrics,
collect_upload_task_finished_metrics, collect_upload_task_started_metrics,
};
use crate::resource::{persistent_cache_task, task};
use crate::shutdown;
use dragonfly_api::common::v2::{PersistentCacheTask, Priority, Task, TaskType};
use dragonfly_api::common::v2::{CacheTask, PersistentCacheTask, Priority, Task, TaskType};
use dragonfly_api::dfdaemon::v2::{
dfdaemon_download_client::DfdaemonDownloadClient as DfdaemonDownloadGRPCClient,
dfdaemon_download_server::{
DfdaemonDownload, DfdaemonDownloadServer as DfdaemonDownloadGRPCServer,
},
DeleteTaskRequest, DownloadPersistentCacheTaskRequest, DownloadPersistentCacheTaskResponse,
DownloadTaskRequest, DownloadTaskResponse, StatPersistentCacheTaskRequest,
DeleteCacheTaskRequest, DeleteTaskRequest, DownloadCacheTaskRequest, DownloadCacheTaskResponse,
DownloadPersistentCacheTaskRequest, DownloadPersistentCacheTaskResponse, DownloadTaskRequest,
DownloadTaskResponse, Entry, ListTaskEntriesRequest, ListTaskEntriesResponse,
StatCacheTaskRequest as DfdaemonStatCacheTaskRequest, StatPersistentCacheTaskRequest,
StatTaskRequest as DfdaemonStatTaskRequest, UploadPersistentCacheTaskRequest,
};
use dragonfly_api::errordetails::v2::Backend;
use dragonfly_api::scheduler::v2::DeleteHostRequest as SchedulerDeleteHostRequest;
use dragonfly_client_backend::HeadRequest;
use dragonfly_client_config::dfdaemon::Config;
use dragonfly_client_core::{
error::{ErrorType, OrErr},
Error as ClientError, Result as ClientResult,
};
use dragonfly_client_util::http::{get_range, hashmap_to_headermap, headermap_to_hashmap};
use dragonfly_client_util::{
digest::{verify_file_digest, Digest},
http::{get_range, hashmap_to_headermap, headermap_to_hashmap},
id_generator::{PersistentCacheTaskIDParameter, TaskIDParameter},
};
use hyper_util::rt::TokioIo;
use opentelemetry::Context;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
@ -50,6 +59,7 @@ use std::time::{Duration, Instant};
use tokio::fs;
use tokio::net::{UnixListener, UnixStream};
use tokio::sync::mpsc;
use tokio::sync::mpsc::Sender;
use tokio::sync::Barrier;
use tokio_stream::wrappers::{ReceiverStream, UnixListenerStream};
use tonic::service::interceptor::InterceptedService;
@ -59,8 +69,9 @@ use tonic::{
};
use tower::{service_fn, ServiceBuilder};
use tracing::{error, info, instrument, Instrument, Span};
use tracing_opentelemetry::OpenTelemetrySpanExt;
use super::interceptor::TracingInterceptor;
use super::interceptor::{ExtractTracingInterceptor, InjectTracingInterceptor};
/// DfdaemonDownloadServer is the grpc unix server of the download.
pub struct DfdaemonDownloadServer {
@ -70,8 +81,11 @@ pub struct DfdaemonDownloadServer {
/// socket_path is the path of the unix domain socket.
socket_path: PathBuf,
/// service is the grpc service of the dfdaemon.
service: DfdaemonDownloadGRPCServer<DfdaemonDownloadServerHandler>,
/// task is the task manager.
task: Arc<task::Task>,
/// persistent_cache_task is the persistent cache task manager.
persistent_cache_task: Arc<persistent_cache_task::PersistentCacheTask>,
/// shutdown is used to shutdown the grpc server.
shutdown: shutdown::Shutdown,
@ -83,7 +97,6 @@ pub struct DfdaemonDownloadServer {
/// DfdaemonDownloadServer implements the grpc server of the download.
impl DfdaemonDownloadServer {
/// new creates a new DfdaemonServer.
#[instrument(skip_all)]
pub fn new(
config: Arc<Config>,
socket_path: PathBuf,
@ -92,27 +105,29 @@ impl DfdaemonDownloadServer {
shutdown: shutdown::Shutdown,
shutdown_complete_tx: mpsc::UnboundedSender<()>,
) -> Self {
// Initialize the grpc service.
let service = DfdaemonDownloadGRPCServer::new(DfdaemonDownloadServerHandler {
socket_path: socket_path.clone(),
task,
persistent_cache_task,
})
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX);
Self {
config,
socket_path,
service,
task,
persistent_cache_task,
shutdown,
_shutdown_complete: shutdown_complete_tx,
}
}
/// run starts the download server with unix domain socket.
#[instrument(skip_all)]
pub async fn run(&mut self, grpc_server_started_barrier: Arc<Barrier>) -> ClientResult<()> {
// Initialize the grpc service.
let service = DfdaemonDownloadGRPCServer::with_interceptor(
DfdaemonDownloadServerHandler {
config: self.config.clone(),
socket_path: self.socket_path.clone(),
task: self.task.clone(),
persistent_cache_task: self.persistent_cache_task.clone(),
},
ExtractTracingInterceptor,
);
// Register the reflection service.
let reflection = tonic_reflection::server::Builder::configure()
.register_encoded_file_descriptor_set(dragonfly_api::FILE_DESCRIPTOR_SET)
@ -124,26 +139,22 @@ impl DfdaemonDownloadServer {
// Initialize health reporter.
let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
// Set the serving status of the download grpc server.
health_reporter
.set_serving::<DfdaemonDownloadGRPCServer<DfdaemonDownloadServerHandler>>()
.await;
// Start download grpc server with unix domain socket.
fs::create_dir_all(self.socket_path.parent().unwrap()).await?;
if self.socket_path.is_file() {
// Remove the old unix domain socket file if it exists.
fs::remove_file(&self.socket_path).await?;
}
fs::remove_file(self.socket_path.clone())
.await
.unwrap_or_else(|err| {
info!("remove {:?} failed: {}", self.socket_path, err);
});
// Bind the unix domain socket and set the permissions for the socket.
let uds = UnixListener::bind(&self.socket_path)?;
let perms = std::fs::Permissions::from_mode(0o660);
let perms = std::fs::Permissions::from_mode(0o777);
fs::set_permissions(&self.socket_path, perms).await?;
// TODO(Gaius): RateLimitLayer is not implemented Clone, so we can't use it here.
// Only use the LoadShed layer and the ConcurrencyLimit layer.
let layer = ServiceBuilder::new()
let rate_limit_layer = ServiceBuilder::new()
.concurrency_limit(self.config.download.server.request_rate_limit as usize)
.load_shed()
.into_inner();
@ -154,10 +165,10 @@ impl DfdaemonDownloadServer {
.tcp_keepalive(Some(super::TCP_KEEPALIVE))
.http2_keepalive_interval(Some(super::HTTP2_KEEP_ALIVE_INTERVAL))
.http2_keepalive_timeout(Some(super::HTTP2_KEEP_ALIVE_TIMEOUT))
.layer(layer)
.add_service(reflection.clone())
.layer(rate_limit_layer)
.add_service(reflection)
.add_service(health_service)
.add_service(self.service.clone())
.add_service(service)
.serve_with_incoming_shutdown(uds_stream, async move {
// When the grpc server is started, notify the barrier. If the shutdown signal is received
// before barrier is waited successfully, the server will shutdown immediately.
@ -165,6 +176,12 @@ impl DfdaemonDownloadServer {
// Notify the download grpc server is started.
_ = grpc_server_started_barrier.wait() => {
info!("download server is ready to start");
health_reporter
.set_serving::<DfdaemonDownloadGRPCServer<DfdaemonDownloadServerHandler>>()
.await;
info!("download server's health status set to serving");
}
// Wait for shutdown signal.
_ = shutdown.recv() => {
@ -194,6 +211,9 @@ impl DfdaemonDownloadServer {
/// DfdaemonDownloadServerHandler is the handler of the dfdaemon download grpc service.
pub struct DfdaemonDownloadServerHandler {
/// config is the configuration of the dfdaemon.
config: Arc<Config>,
/// socket_path is the path of the unix domain socket.
socket_path: PathBuf,
@ -211,11 +231,19 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
type DownloadTaskStream = ReceiverStream<Result<DownloadTaskResponse, Status>>;
/// download_task tells the dfdaemon to download the task.
#[instrument(skip_all, fields(host_id, task_id, peer_id))]
#[instrument(
skip_all,
fields(host_id, task_id, peer_id, url, remote_ip, content_length)
)]
async fn download_task(
&self,
request: Request<DownloadTaskRequest>,
) -> Result<Response<Self::DownloadTaskStream>, Status> {
// If the parent context is set, use it as the parent context for the span.
if let Some(parent_ctx) = request.extensions().get::<Context>() {
Span::current().set_parent(parent_ctx.clone());
};
// Record the start time.
let start_time = Instant::now();
@ -232,13 +260,16 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
let task_id = self
.task
.id_generator
.task_id(
download.url.as_str(),
download.piece_length,
download.tag.as_deref(),
download.application.as_deref(),
download.filtered_query_params.clone(),
)
.task_id(match download.content_for_calculating_task_id.clone() {
Some(content) => TaskIDParameter::Content(content),
None => TaskIDParameter::URLBased {
url: download.url.clone(),
piece_length: download.piece_length,
tag: download.tag.clone(),
application: download.application.clone(),
filtered_query_params: download.filtered_query_params.clone(),
},
})
.map_err(|e| {
error!("generate task id: {}", e);
Status::invalid_argument(e.to_string())
@ -254,6 +285,11 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
Span::current().record("host_id", host_id.as_str());
Span::current().record("task_id", task_id.as_str());
Span::current().record("peer_id", peer_id.as_str());
Span::current().record("url", download.url.clone());
Span::current().record(
"remote_ip",
download.remote_ip.clone().unwrap_or_default().as_str(),
);
info!("download task in download server");
// Download task started.
@ -327,12 +363,15 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
error!("missing content length in the response");
return Err(Status::internal("missing content length in the response"));
};
info!(
"content length {}, piece length {}",
content_length,
task.piece_length().unwrap_or_default()
);
Span::current().record("content_length", content_length);
// Download's range priority is higher than the request header's range.
// If download protocol is http, use the range of the request header.
// If download protocol is not http, use the range of the download.
@ -388,6 +427,21 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
let task_manager_clone = task_manager.clone();
let task_clone = task.clone();
let (out_stream_tx, out_stream_rx) = mpsc::channel(10 * 1024);
// Define the error handler to send the error to the stream.
async fn handle_error(
out_stream_tx: &Sender<Result<DownloadTaskResponse, Status>>,
err: impl std::error::Error,
) {
out_stream_tx
.send_timeout(
Err(Status::internal(err.to_string())),
super::REQUEST_TIMEOUT,
)
.await
.unwrap_or_else(|err| error!("send download progress error: {:?}", err));
}
tokio::spawn(
async move {
match task_manager_clone
@ -423,28 +477,78 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
task_manager_clone.download_finished(task_clone.id.as_str())
{
error!("download task finished: {}", err);
handle_error(&out_stream_tx, err).await;
return;
}
}
// Check whether the output path is empty. If output path is empty,
// should not hard link or copy the task content to the destination.
if let Some(output_path) = download_clone.output_path.clone() {
// Hard link or copy the task content to the destination.
if let Err(err) = task_manager_clone
.hard_link_or_copy(&task_clone, Path::new(output_path.as_str()))
.await
{
error!("hard link or copy task: {}", err);
out_stream_tx
.send_timeout(
Err(Status::internal(err.to_string())),
super::REQUEST_TIMEOUT,
)
.await
.unwrap_or_else(|err| {
error!("send download progress error: {:?}", err)
});
};
if let Some(output_path) = &download_clone.output_path {
if !download_clone.force_hard_link {
let output_path = Path::new(output_path.as_str());
if output_path.exists() {
match task_manager_clone
.is_same_dev_inode(task_clone.id.as_str(), output_path)
.await
{
Ok(true) => {}
Ok(false) => {
error!(
"output path {} is already exists",
output_path.display()
);
handle_error(
&out_stream_tx,
Status::internal(format!(
"output path {} is already exists",
output_path.display()
)),
)
.await;
return;
}
Err(err) => {
error!("check output path: {}", err);
handle_error(&out_stream_tx, err).await;
return;
}
}
} else if let Err(err) = task_manager_clone
.copy_task(task_clone.id.as_str(), output_path)
.await
{
error!("copy task: {}", err);
handle_error(&out_stream_tx, err).await;
return;
}
}
// Verify the file digest if it is provided.
if let Some(raw_digest) = &download_clone.digest {
let digest = match raw_digest.parse::<Digest>() {
Ok(digest) => digest,
Err(err) => {
error!("parse digest: {}", err);
handle_error(
&out_stream_tx,
Status::invalid_argument(format!(
"invalid digest({}): {}",
raw_digest, err
)),
)
.await;
return;
}
};
if let Err(err) =
verify_file_digest(digest, Path::new(output_path.as_str()))
{
error!("verify file digest: {}", err);
handle_error(&out_stream_tx, err).await;
return;
}
}
}
}
}
Err(ClientError::BackendError(err)) => {
@ -471,31 +575,19 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
status_code: err.status_code.map(|code| code.as_u16() as i32),
}) {
Ok(json) => {
out_stream_tx
.send_timeout(
Err(Status::with_details(
Code::Internal,
err.to_string(),
json.into(),
)),
super::REQUEST_TIMEOUT,
)
.await
.unwrap_or_else(|err| {
error!("send download progress error: {:?}", err)
});
handle_error(
&out_stream_tx,
Status::with_details(
Code::Internal,
err.to_string(),
json.into(),
),
)
.await;
}
Err(err) => {
out_stream_tx
.send_timeout(
Err(Status::internal(err.to_string())),
super::REQUEST_TIMEOUT,
)
.await
.unwrap_or_else(|err| {
error!("send download progress error: {:?}", err)
});
error!("serialize error: {}", err);
handle_error(&out_stream_tx, err).await;
}
}
}
@ -520,15 +612,7 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
.await
.unwrap_or_else(|err| error!("download task failed: {}", err));
out_stream_tx
.send_timeout(
Err(Status::internal(err.to_string())),
super::REQUEST_TIMEOUT,
)
.await
.unwrap_or_else(|err| {
error!("send download progress error: {:?}", err)
});
handle_error(&out_stream_tx, err).await;
}
}
@ -586,11 +670,16 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
}
/// stat_task gets the status of the task.
#[instrument(skip_all, fields(host_id, task_id))]
#[instrument(skip_all, fields(host_id, task_id, remote_ip, local_only))]
async fn stat_task(
&self,
request: Request<DfdaemonStatTaskRequest>,
) -> Result<Response<Task>, Status> {
// If the parent context is set, use it as the parent context for the span.
if let Some(parent_ctx) = request.extensions().get::<Context>() {
Span::current().set_parent(parent_ctx.clone());
};
// Clone the request.
let request = request.into_inner();
@ -600,36 +689,137 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
// Get the task id from the request.
let task_id = request.task_id;
// Get the local_only flag from the request, default to false.
let local_only = request.local_only;
// Span record the host id and task id.
Span::current().record("host_id", host_id.as_str());
Span::current().record("task_id", task_id.as_str());
Span::current().record(
"remote_ip",
request.remote_ip.clone().unwrap_or_default().as_str(),
);
Span::current().record("local_only", local_only.to_string().as_str());
info!("stat task in download server");
// Collect the stat task metrics.
collect_stat_task_started_metrics(TaskType::Standard as i32);
// Get the task from the scheduler.
let task = self
match self
.task
.stat(task_id.as_str(), host_id.as_str())
.stat(task_id.as_str(), host_id.as_str(), local_only)
.await
.map_err(|err| {
{
Ok(task) => Ok(Response::new(task)),
Err(err) => {
// Collect the stat task failure metrics.
collect_stat_task_failure_metrics(TaskType::Standard as i32);
error!("stat task: {}", err);
// Log the error with detailed context.
error!("stat task failed: {}", err);
// Map the error to an appropriate gRPC status.
Err(match err {
ClientError::TaskNotFound(id) => {
Status::not_found(format!("task not found: {}", id))
}
_ => Status::internal(err.to_string()),
})
}
}
}
/// list_tasks lists the tasks.
#[instrument(skip_all, fields(task_id, url, remote_ip))]
async fn list_task_entries(
&self,
request: Request<ListTaskEntriesRequest>,
) -> Result<Response<ListTaskEntriesResponse>, Status> {
// If the parent context is set, use it as the parent context for the span.
if let Some(parent_ctx) = request.extensions().get::<Context>() {
Span::current().set_parent(parent_ctx.clone());
};
// Clone the request.
let request = request.into_inner();
// Span record the task id and url.
Span::current().record("task_id", request.task_id.as_str());
Span::current().record("url", request.url.as_str());
Span::current().record(
"remote_ip",
request.remote_ip.clone().unwrap_or_default().as_str(),
);
info!("list tasks in download server");
// Collect the list tasks started metrics.
collect_list_task_entries_started_metrics(TaskType::Standard as i32);
// Build the backend.
let backend = self
.task
.backend_factory
.build(request.url.as_str())
.map_err(|err| {
// Collect the list tasks failure metrics.
collect_list_task_entries_failure_metrics(TaskType::Standard as i32);
error!("build backend: {}", err);
Status::internal(err.to_string())
})?;
Ok(Response::new(task))
// Head the task entries.
let response = backend
.head(HeadRequest {
task_id: request.task_id.clone(),
url: request.url.clone(),
http_header: Some(hashmap_to_headermap(&request.request_header).map_err(
|err| {
error!("parse request header: {}", err);
Status::internal(err.to_string())
},
)?),
timeout: self.config.download.piece_timeout,
client_cert: None,
object_storage: request.object_storage.clone(),
hdfs: request.hdfs.clone(),
})
.await
.map_err(|err| {
// Collect the list tasks failure metrics.
collect_list_task_entries_failure_metrics(TaskType::Standard as i32);
error!("list task entries: {}", err);
Status::internal(err.to_string())
})?;
Ok(Response::new(ListTaskEntriesResponse {
content_length: response.content_length.unwrap_or_default(),
response_header: headermap_to_hashmap(&response.http_header.unwrap_or_default()),
status_code: response.http_status_code.map(|code| code.as_u16().into()),
entries: response
.entries
.into_iter()
.map(|dir_entry| Entry {
url: dir_entry.url,
content_length: dir_entry.content_length as u64,
is_dir: dir_entry.is_dir,
})
.collect(),
}))
}
/// delete_task calls the dfdaemon to delete the task.
#[instrument(skip_all, fields(host_id, task_id))]
#[instrument(skip_all, fields(host_id, task_id, remote_ip))]
async fn delete_task(
&self,
request: Request<DeleteTaskRequest>,
) -> Result<Response<()>, Status> {
// If the parent context is set, use it as the parent context for the span.
if let Some(parent_ctx) = request.extensions().get::<Context>() {
Span::current().set_parent(parent_ctx.clone());
};
// Clone the request.
let request = request.into_inner();
@ -642,6 +832,10 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
// Span record the host id and task id.
Span::current().record("host_id", host_id.as_str());
Span::current().record("task_id", task_id.as_str());
Span::current().record(
"remote_ip",
request.remote_ip.clone().unwrap_or_default().as_str(),
);
info!("delete task in download server");
// Collect the delete task started metrics.
@ -664,7 +858,12 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
/// delete_host calls the scheduler to delete the host.
#[instrument(skip_all, fields(host_id))]
async fn delete_host(&self, _: Request<()>) -> Result<Response<()>, Status> {
async fn delete_host(&self, request: Request<()>) -> Result<Response<()>, Status> {
// If the parent context is set, use it as the parent context for the span.
if let Some(parent_ctx) = request.extensions().get::<Context>() {
Span::current().set_parent(parent_ctx.clone());
};
// Generate the host id.
let host_id = self.task.id_generator.host_id();
@ -695,11 +894,16 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
ReceiverStream<Result<DownloadPersistentCacheTaskResponse, Status>>;
/// download_persistent_cache_task downloads the persistent cache task.
#[instrument(skip_all, fields(host_id, task_id, peer_id))]
#[instrument(skip_all, fields(host_id, task_id, peer_id, remote_ip, content_length))]
async fn download_persistent_cache_task(
&self,
request: Request<DownloadPersistentCacheTaskRequest>,
) -> Result<Response<Self::DownloadPersistentCacheTaskStream>, Status> {
// If the parent context is set, use it as the parent context for the span.
if let Some(parent_ctx) = request.extensions().get::<Context>() {
Span::current().set_parent(parent_ctx.clone());
};
// Record the start time.
let start_time = Instant::now();
@ -723,6 +927,10 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
Span::current().record("host_id", host_id.as_str());
Span::current().record("task_id", task_id.as_str());
Span::current().record("peer_id", peer_id.as_str());
Span::current().record(
"remote_ip",
request.remote_ip.clone().unwrap_or_default().as_str(),
);
info!("download persistent cache task in download server");
// Download task started.
@ -776,17 +984,35 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
task
}
};
info!(
"content length {}, piece length {}",
task.content_length(),
task.piece_length()
);
Span::current().record("content_length", task.content_length());
// Initialize stream channel.
let request_clone = request.clone();
let task_manager_clone = self.persistent_cache_task.clone();
let task_clone = task.clone();
let (out_stream_tx, out_stream_rx) = mpsc::channel(10 * 1024);
// Define the error handler to send the error to the stream.
async fn handle_error(
out_stream_tx: &Sender<Result<DownloadPersistentCacheTaskResponse, Status>>,
err: impl std::error::Error,
) {
out_stream_tx
.send_timeout(
Err(Status::internal(err.to_string())),
super::REQUEST_TIMEOUT,
)
.await
.unwrap_or_else(|err| error!("send download progress error: {:?}", err));
}
tokio::spawn(
async move {
match task_manager_clone
@ -821,28 +1047,82 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
task_manager_clone.download_finished(task_clone.id.as_str())
{
error!("download persistent cache task finished: {}", err);
handle_error(&out_stream_tx, err).await;
return;
}
// Hard link or copy the persistent cache task content to the destination.
if let Some(output_path) = request_clone.output_path {
if let Err(err) = task_manager_clone
.hard_link_or_copy(&task_clone, Path::new(output_path.as_str()))
.await
{
error!("hard link or copy persistent cache task: {}", err);
out_stream_tx
.send_timeout(
Err(Status::internal(err.to_string())),
super::REQUEST_TIMEOUT,
)
if let Some(output_path) = &request_clone.output_path {
if !request_clone.force_hard_link {
let output_path = Path::new(output_path.as_str());
if output_path.exists() {
match task_manager_clone
.is_same_dev_inode(task_clone.id.as_str(), output_path)
.await
{
Ok(true) => {}
Ok(false) => {
error!(
"output path {} is already exists",
output_path.display()
);
handle_error(
&out_stream_tx,
Status::internal(format!(
"output path {} is already exists",
output_path.display()
)),
)
.await;
return;
}
Err(err) => {
error!("check output path: {}", err);
handle_error(&out_stream_tx, err).await;
return;
}
}
} else if let Err(err) = task_manager_clone
.copy_task(task_clone.id.as_str(), output_path)
.await
.unwrap_or_else(|err| {
error!("send download progress error: {:?}", err)
});
};
{
error!("copy task: {}", err);
handle_error(&out_stream_tx, err).await;
return;
}
}
// Verify the file digest if it is provided.
if let Some(raw_digest) = &request_clone.digest {
let digest = match raw_digest.parse::<Digest>() {
Ok(digest) => digest,
Err(err) => {
error!("parse digest: {}", err);
handle_error(
&out_stream_tx,
Status::invalid_argument(format!(
"invalid digest({}): {}",
raw_digest, err
)),
)
.await;
return;
}
};
if let Err(err) =
verify_file_digest(digest, Path::new(output_path.as_str()))
{
error!("verify file digest: {}", err);
handle_error(&out_stream_tx, err).await;
return;
}
}
}
}
Err(err) => {
error!("download persistent cache task failed: {}", err);
// Collect download task failure metrics.
collect_download_task_failure_metrics(
task_type,
@ -851,8 +1131,6 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
task_priority.to_string().as_str(),
);
error!("download persistent cache task failed: {}", err);
// Download task failed.
task_manager_clone
.download_failed(task_clone.id.as_str())
@ -861,15 +1139,7 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
error!("download persistent cache task failed: {}", err)
});
out_stream_tx
.send_timeout(
Err(Status::internal(err.to_string())),
super::REQUEST_TIMEOUT,
)
.await
.unwrap_or_else(|err| {
error!("send download progress error: {:?}", err)
});
handle_error(&out_stream_tx, err).await;
}
}
@ -882,11 +1152,16 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
}
/// upload_persistent_cache_task uploads the persistent cache task.
#[instrument(skip_all, fields(host_id, task_id, peer_id))]
#[instrument(skip_all, fields(host_id, task_id, peer_id, remote_ip))]
async fn upload_persistent_cache_task(
&self,
request: Request<UploadPersistentCacheTaskRequest>,
) -> Result<Response<PersistentCacheTask>, Status> {
// If the parent context is set, use it as the parent context for the span.
if let Some(parent_ctx) = request.extensions().get::<Context>() {
Span::current().set_parent(parent_ctx.clone());
};
// Record the start time.
let start_time = Instant::now();
@ -896,22 +1171,22 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
info!("upload persistent cache task {:?}", request);
// Generate the task id.
let task_id = match request.task_id.as_deref() {
Some(task_id) => task_id.to_string(),
None => self
.task
.id_generator
.persistent_cache_task_id(
&path.to_path_buf(),
request.piece_length,
request.tag.as_deref(),
request.application.as_deref(),
)
.map_err(|err| {
error!("generate persistent cache task id: {}", err);
Status::invalid_argument(err.to_string())
})?,
};
let task_id = self
.task
.id_generator
.persistent_cache_task_id(match request.content_for_calculating_task_id.clone() {
Some(content) => PersistentCacheTaskIDParameter::Content(content),
None => PersistentCacheTaskIDParameter::FileContentBased {
path: path.to_path_buf(),
piece_length: request.piece_length,
tag: request.tag.clone(),
application: request.application.clone(),
},
})
.map_err(|err| {
error!("generate persistent cache task id: {}", err);
Status::invalid_argument(err.to_string())
})?;
info!("generate persistent cache task id: {}", task_id);
// Generate the host id.
@ -924,6 +1199,10 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
Span::current().record("host_id", host_id.as_str());
Span::current().record("task_id", task_id.as_str());
Span::current().record("peer_id", peer_id.as_str());
Span::current().record(
"remote_ip",
request.remote_ip.clone().unwrap_or_default().as_str(),
);
info!("upload persistent cache task in download server");
// Collect upload task started metrics.
@ -974,11 +1253,16 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
}
/// stat_persistent_cache_task stats the persistent cache task.
#[instrument(skip_all, fields(host_id, task_id))]
#[instrument(skip_all, fields(host_id, task_id, remote_ip))]
async fn stat_persistent_cache_task(
&self,
request: Request<StatPersistentCacheTaskRequest>,
) -> Result<Response<PersistentCacheTask>, Status> {
// If the parent context is set, use it as the parent context for the span.
if let Some(parent_ctx) = request.extensions().get::<Context>() {
Span::current().set_parent(parent_ctx.clone());
};
// Clone the request.
let request = request.into_inner();
@ -991,6 +1275,10 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
// Span record the host id and task id.
Span::current().record("host_id", host_id.as_str());
Span::current().record("task_id", task_id.as_str());
Span::current().record(
"remote_ip",
request.remote_ip.clone().unwrap_or_default().as_str(),
);
info!("stat persistent cache task in download server");
// Collect the stat persistent cache task started metrics.
@ -1010,19 +1298,51 @@ impl DfdaemonDownload for DfdaemonDownloadServerHandler {
Ok(Response::new(task))
}
/// DownloadCacheTaskStream is the stream of the download cache task response.
type DownloadCacheTaskStream = ReceiverStream<Result<DownloadCacheTaskResponse, Status>>;
/// download_cache_task tells the dfdaemon to download the cache task.
#[instrument(
skip_all,
fields(host_id, task_id, peer_id, url, remote_ip, content_length)
)]
async fn download_cache_task(
&self,
_request: Request<DownloadCacheTaskRequest>,
) -> Result<Response<Self::DownloadCacheTaskStream>, Status> {
todo!();
}
/// stat_cache_task gets the status of the cache task.
#[instrument(skip_all, fields(host_id, task_id, remote_pi, local_only))]
async fn stat_cache_task(
&self,
_request: Request<DfdaemonStatCacheTaskRequest>,
) -> Result<Response<CacheTask>, Status> {
todo!();
}
/// delete_cache_task calls the dfdaemon to delete the cache task.
#[instrument(skip_all, fields(host_id, task_id, remote_ip))]
async fn delete_cache_task(
&self,
_request: Request<DeleteCacheTaskRequest>,
) -> Result<Response<()>, Status> {
todo!();
}
}
/// DfdaemonDownloadClient is a wrapper of DfdaemonDownloadGRPCClient.
#[derive(Clone)]
pub struct DfdaemonDownloadClient {
/// client is the grpc client of the dfdaemon.
pub client: DfdaemonDownloadGRPCClient<InterceptedService<Channel, TracingInterceptor>>,
pub client: DfdaemonDownloadGRPCClient<InterceptedService<Channel, InjectTracingInterceptor>>,
}
/// DfdaemonDownloadClient implements the grpc client of the dfdaemon download.
impl DfdaemonDownloadClient {
/// new_unix creates a new DfdaemonDownloadClient with unix domain socket.
#[instrument(skip_all)]
pub async fn new_unix(socket_path: PathBuf) -> ClientResult<Self> {
// Ignore the uri because it is not used.
let channel = Endpoint::try_from("http://[::]:50051")
@ -1047,9 +1367,10 @@ impl DfdaemonDownloadClient {
})
.or_err(ErrorType::ConnectError)?;
let client = DfdaemonDownloadGRPCClient::with_interceptor(channel, TracingInterceptor)
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX);
let client =
DfdaemonDownloadGRPCClient::with_interceptor(channel, InjectTracingInterceptor)
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX);
Ok(Self { client })
}
@ -1087,6 +1408,18 @@ impl DfdaemonDownloadClient {
Ok(response.into_inner())
}
/// list_task_entries lists the task entries.
#[instrument(skip_all)]
pub async fn list_task_entries(
&self,
request: ListTaskEntriesRequest,
) -> ClientResult<ListTaskEntriesResponse> {
let request = Self::make_request(request);
info!("list task entries request: {:?}", request);
let response = self.client.clone().list_task_entries(request).await?;
Ok(response.into_inner())
}
/// delete_task tells the dfdaemon to delete the task.
#[instrument(skip_all)]
pub async fn delete_task(&self, request: DeleteTaskRequest) -> ClientResult<()> {
@ -1168,7 +1501,6 @@ impl DfdaemonDownloadClient {
}
/// make_request creates a new request with timeout.
#[instrument(skip_all)]
fn make_request<T>(request: T) -> tonic::Request<T> {
let mut request = tonic::Request::new(request);
request.set_timeout(super::REQUEST_TIMEOUT);

File diff suppressed because it is too large Load Diff

View File

@ -21,27 +21,27 @@ use dragonfly_client_core::{
use hyper_util::rt::TokioIo;
use std::path::PathBuf;
use tokio::net::UnixStream;
use tonic::service::interceptor::InterceptedService;
use tonic::transport::ClientTlsConfig;
use tonic::transport::{Channel, Endpoint, Uri};
use tonic::{service::interceptor::InterceptedService, transport::ClientTlsConfig};
use tonic_health::pb::{
health_client::HealthClient as HealthGRPCClient, HealthCheckRequest, HealthCheckResponse,
};
use tower::service_fn;
use tracing::{error, instrument};
use super::interceptor::TracingInterceptor;
use super::interceptor::InjectTracingInterceptor;
/// HealthClient is a wrapper of HealthGRPCClient.
#[derive(Clone)]
pub struct HealthClient {
/// client is the grpc client of the certificate.
client: HealthGRPCClient<InterceptedService<Channel, TracingInterceptor>>,
client: HealthGRPCClient<InterceptedService<Channel, InjectTracingInterceptor>>,
}
/// HealthClient implements the grpc client of the health.
impl HealthClient {
/// new creates a new HealthClient.
#[instrument(skip_all)]
pub async fn new(addr: &str, client_tls_config: Option<ClientTlsConfig>) -> Result<Self> {
let channel = match client_tls_config {
Some(client_tls_config) => Channel::from_shared(addr.to_string())
@ -73,14 +73,13 @@ impl HealthClient {
.or_err(ErrorType::ConnectError)?,
};
let client = HealthGRPCClient::with_interceptor(channel, TracingInterceptor)
let client = HealthGRPCClient::with_interceptor(channel, InjectTracingInterceptor)
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX);
Ok(Self { client })
}
/// new_unix creates a new HealthClient with unix domain socket.
#[instrument(skip_all)]
pub async fn new_unix(socket_path: PathBuf) -> Result<Self> {
// Ignore the uri because it is not used.
let channel = Endpoint::try_from("http://[::]:50051")
@ -98,7 +97,8 @@ impl HealthClient {
error!("connect failed: {}", err);
})
.or_err(ErrorType::ConnectError)?;
let client = HealthGRPCClient::with_interceptor(channel, TracingInterceptor)
let client = HealthGRPCClient::with_interceptor(channel, InjectTracingInterceptor)
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX);
Ok(Self { client })
@ -137,7 +137,6 @@ impl HealthClient {
}
/// make_request creates a new request with timeout.
#[instrument(skip_all)]
fn make_request<T>(request: T) -> tonic::Request<T> {
let mut request = tonic::Request::new(request);
request.set_timeout(super::REQUEST_TIMEOUT);

View File

@ -17,11 +17,30 @@
use tonic::{metadata, service::Interceptor, Request, Status};
use tracing_opentelemetry::OpenTelemetrySpanExt;
/// MetadataMap is a tracing meda data map container.
/// MetadataMap is a tracing meda data map container for span context.
struct MetadataMap<'a>(&'a mut metadata::MetadataMap);
/// MetadataMap implements the otel tracing Extractor.
impl opentelemetry::propagation::Extractor for MetadataMap<'_> {
/// Get a value for a key from the `MetadataMap`. If the value can't be converted to &str, returns None
fn get(&self, key: &str) -> Option<&str> {
self.0.get(key).and_then(|metadata| metadata.to_str().ok())
}
/// Collect all the keys from the `MetadataMap`.
fn keys(&self) -> Vec<&str> {
self.0
.keys()
.map(|key| match key {
tonic::metadata::KeyRef::Ascii(v) => v.as_str(),
tonic::metadata::KeyRef::Binary(v) => v.as_str(),
})
.collect::<Vec<_>>()
}
}
/// MetadataMap implements the otel tracing Injector.
impl<'a> opentelemetry::propagation::Injector for MetadataMap<'a> {
impl opentelemetry::propagation::Injector for MetadataMap<'_> {
/// set a key-value pair to the injector.
fn set(&mut self, key: &str, value: String) {
if let Ok(key) = metadata::MetadataKey::from_bytes(key.as_bytes()) {
@ -32,12 +51,12 @@ impl<'a> opentelemetry::propagation::Injector for MetadataMap<'a> {
}
}
/// TracingInterceptor is a auto-inject tracing gRPC interceptor.
/// InjectTracingInterceptor is a auto-inject tracing gRPC interceptor.
#[derive(Clone)]
pub struct TracingInterceptor;
pub struct InjectTracingInterceptor;
/// TracingInterceptor implements the tonic Interceptor interface.
impl Interceptor for TracingInterceptor {
/// InjectTracingInterceptor implements the tonic Interceptor interface.
impl Interceptor for InjectTracingInterceptor {
/// call and inject tracing context into lgobal propagator.
fn call(&mut self, mut request: Request<()>) -> std::result::Result<Request<()>, Status> {
let context = tracing::Span::current().context();
@ -48,3 +67,20 @@ impl Interceptor for TracingInterceptor {
Ok(request)
}
}
/// ExtractTracingInterceptor is a auto-extract tracing gRPC interceptor.
#[derive(Clone)]
pub struct ExtractTracingInterceptor;
/// ExtractTracingInterceptor implements the tonic Interceptor interface.
impl Interceptor for ExtractTracingInterceptor {
/// call and inject tracing context into lgobal propagator.
fn call(&mut self, mut request: Request<()>) -> std::result::Result<Request<()>, Status> {
let parent_cx = opentelemetry::global::get_text_map_propagator(|prop| {
prop.extract(&MetadataMap(request.metadata_mut()))
});
request.extensions_mut().insert(parent_cx);
Ok(request)
}
}

View File

@ -27,22 +27,21 @@ use dragonfly_client_core::{
use std::sync::Arc;
use tonic::{service::interceptor::InterceptedService, transport::Channel};
use tonic_health::pb::health_check_response::ServingStatus;
use tracing::{error, instrument, warn};
use tracing::{error, instrument};
use url::Url;
use super::interceptor::TracingInterceptor;
use super::interceptor::InjectTracingInterceptor;
/// ManagerClient is a wrapper of ManagerGRPCClient.
#[derive(Clone)]
pub struct ManagerClient {
/// client is the grpc client of the manager.
pub client: ManagerGRPCClient<InterceptedService<Channel, TracingInterceptor>>,
pub client: ManagerGRPCClient<InterceptedService<Channel, InjectTracingInterceptor>>,
}
/// ManagerClient implements the grpc client of the manager.
impl ManagerClient {
/// new creates a new ManagerClient.
#[instrument(skip_all)]
pub async fn new(config: Arc<Config>, addr: String) -> Result<Self> {
let domain_name = Url::parse(addr.as_str())?
.host_str()
@ -99,7 +98,7 @@ impl ManagerClient {
.or_err(ErrorType::ConnectError)?,
};
let client = ManagerGRPCClient::with_interceptor(channel, TracingInterceptor)
let client = ManagerGRPCClient::with_interceptor(channel, InjectTracingInterceptor)
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX);
Ok(Self { client })
@ -133,7 +132,6 @@ impl ManagerClient {
}
/// make_request creates a new request with timeout.
#[instrument(skip_all)]
fn make_request<T>(request: T) -> tonic::Request<T> {
let mut request = tonic::Request::new(request);
request.set_timeout(super::REQUEST_TIMEOUT);

View File

@ -34,8 +34,12 @@ pub mod scheduler;
/// CONNECT_TIMEOUT is the timeout for GRPC connection.
pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(2);
/// REQUEST_TIMEOUT is the timeout for GRPC requests.
pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(5);
/// REQUEST_TIMEOUT is the timeout for GRPC requests, default is 10 second.
/// Note: This timeout is used for the whole request, including wait for scheduler
/// scheduling, refer to https://d7y.io/docs/next/reference/configuration/scheduler/.
/// Scheduler'configure `scheduler.retryInterval`, `scheduler.retryBackToSourceLimit` and `scheduler.retryLimit`
/// is used for the scheduler to schedule the task.
pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(15);
/// TCP_KEEPALIVE is the keepalive duration for TCP connection.
pub const TCP_KEEPALIVE: Duration = Duration::from_secs(3600);
@ -46,11 +50,11 @@ pub const HTTP2_KEEP_ALIVE_INTERVAL: Duration = Duration::from_secs(300);
/// HTTP2_KEEP_ALIVE_TIMEOUT is the timeout for HTTP2 keep alive.
pub const HTTP2_KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(20);
/// MAX_FRAME_SIZE is the max frame size for GRPC, default is 12MB.
pub const MAX_FRAME_SIZE: u32 = 12 * 1024 * 1024;
/// MAX_FRAME_SIZE is the max frame size for GRPC, default is 4MB.
pub const MAX_FRAME_SIZE: u32 = 4 * 1024 * 1024;
/// INITIAL_WINDOW_SIZE is the initial window size for GRPC, default is 12MB.
pub const INITIAL_WINDOW_SIZE: u32 = 12 * 1024 * 1024;
/// INITIAL_WINDOW_SIZE is the initial window size for GRPC, default is 512KB.
pub const INITIAL_WINDOW_SIZE: u32 = 512 * 1024;
/// BUFFER_SIZE is the buffer size for GRPC, default is 64KB.
pub const BUFFER_SIZE: usize = 64 * 1024;

View File

@ -40,7 +40,7 @@ use tonic::transport::Channel;
use tracing::{error, info, instrument, Instrument};
use url::Url;
use super::interceptor::TracingInterceptor;
use super::interceptor::InjectTracingInterceptor;
/// VNode is the virtual node of the hashring.
#[derive(Debug, Copy, Clone, Hash, PartialEq)]
@ -79,7 +79,6 @@ pub struct SchedulerClient {
/// SchedulerClient implements the grpc client of the scheduler.
impl SchedulerClient {
/// new creates a new SchedulerClient.
#[instrument(skip_all)]
pub async fn new(config: Arc<Config>, dynconfig: Arc<Dynconfig>) -> Result<Self> {
let client = Self {
config,
@ -192,9 +191,10 @@ impl SchedulerClient {
})
.or_err(ErrorType::ConnectError)?;
let mut client = SchedulerGRPCClient::with_interceptor(channel, TracingInterceptor)
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX);
let mut client =
SchedulerGRPCClient::with_interceptor(channel, InjectTracingInterceptor)
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX);
client.announce_host(request).await?;
Ok(())
}
@ -245,9 +245,10 @@ impl SchedulerClient {
})
.or_err(ErrorType::ConnectError)?;
let mut client = SchedulerGRPCClient::with_interceptor(channel, TracingInterceptor)
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX);
let mut client =
SchedulerGRPCClient::with_interceptor(channel, InjectTracingInterceptor)
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX);
client.announce_host(request).await?;
Ok(())
}
@ -303,9 +304,10 @@ impl SchedulerClient {
})
.or_err(ErrorType::ConnectError)?;
let mut client = SchedulerGRPCClient::with_interceptor(channel, TracingInterceptor)
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX);
let mut client =
SchedulerGRPCClient::with_interceptor(channel, InjectTracingInterceptor)
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX);
client.delete_host(request).await?;
Ok(())
}
@ -457,7 +459,7 @@ impl SchedulerClient {
&self,
task_id: &str,
peer_id: Option<&str>,
) -> Result<SchedulerGRPCClient<InterceptedService<Channel, TracingInterceptor>>> {
) -> Result<SchedulerGRPCClient<InterceptedService<Channel, InjectTracingInterceptor>>> {
// Update scheduler addresses of the client.
self.update_available_scheduler_addrs().await?;
@ -516,7 +518,7 @@ impl SchedulerClient {
};
Ok(
SchedulerGRPCClient::with_interceptor(channel, TracingInterceptor)
SchedulerGRPCClient::with_interceptor(channel, InjectTracingInterceptor)
.max_decoding_message_size(usize::MAX)
.max_encoding_message_size(usize::MAX),
)
@ -619,7 +621,6 @@ impl SchedulerClient {
}
/// make_request creates a new request with timeout.
#[instrument(skip_all)]
fn make_request<T>(request: T) -> tonic::Request<T> {
let mut request = tonic::Request::new(request);
request.set_timeout(super::REQUEST_TIMEOUT);

View File

@ -36,7 +36,6 @@ pub struct Health {
/// Health implements the health server.
impl Health {
/// new creates a new Health.
#[instrument(skip_all)]
pub fn new(
addr: SocketAddr,
shutdown: shutdown::Shutdown,
@ -50,7 +49,6 @@ impl Health {
}
/// run starts the health server.
#[instrument(skip_all)]
pub async fn run(&self) {
// Clone the shutdown channel.
let mut shutdown = self.shutdown.clone();
@ -71,7 +69,6 @@ impl Health {
_ = shutdown.recv() => {
// Health server shutting down with signals.
info!("health server shutting down");
return
}
}
}

View File

@ -26,9 +26,8 @@ use prometheus::{
};
use std::net::SocketAddr;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::sync::Arc;
use std::time::Duration;
use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, RefreshKind, System, UpdateKind};
use tokio::sync::mpsc;
use tracing::{error, info, instrument, warn};
use warp::{Filter, Rejection, Reply};
@ -185,14 +184,6 @@ lazy_static! {
&[]
).expect("metric can be created");
/// PROXY_REQUEST_VIA_DFDAEMON_AND_CACHE_HITS_COUNT is used to count the number of proxy request via
/// dfdaemon and cache hits.
pub static ref PROXY_REQUEST_VIA_DFDAEMON_AND_CACHE_HITS_COUNT: IntCounterVec =
IntCounterVec::new(
Opts::new("proxy_request_via_dfdaemon_and_cache_hits_total", "Counter of the number of cache hits of the proxy request via dfdaemon.").namespace(dragonfly_client_config::SERVICE_NAME).subsystem(dragonfly_client_config::NAME),
&[]
).expect("metric can be created");
/// UPDATE_TASK_COUNT is used to count the number of update tasks.
pub static ref UPDATE_TASK_COUNT: IntCounterVec =
IntCounterVec::new(
@ -221,7 +212,21 @@ lazy_static! {
&["type"]
).expect("metric can be created");
/// DELETE_TASK_COUNT is used to count the number of delete tasks.
/// LIST_TASK_ENTRIES_COUNT is used to count the number of list task entries.
pub static ref LIST_TASK_ENTRIES_COUNT: IntCounterVec =
IntCounterVec::new(
Opts::new("list_task_entries_total", "Counter of the number of the list task entries.").namespace(dragonfly_client_config::SERVICE_NAME).subsystem(dragonfly_client_config::NAME),
&["type"]
).expect("metric can be created");
/// LIST_TASK_ENTRIES_FAILURE_COUNT is used to count the failed number of list task entries.
pub static ref LIST_TASK_ENTRIES_FAILURE_COUNT: IntCounterVec =
IntCounterVec::new(
Opts::new("list_task_entries_failure_total", "Counter of the number of failed of the list task entries.").namespace(dragonfly_client_config::SERVICE_NAME).subsystem(dragonfly_client_config::NAME),
&["type"]
).expect("metric can be created");
/// DELETE_TASK_COUNT is used to count the number of delete tasks.
pub static ref DELETE_TASK_COUNT: IntCounterVec =
IntCounterVec::new(
Opts::new("delete_task_total", "Counter of the number of the delete task.").namespace(dragonfly_client_config::SERVICE_NAME).subsystem(dragonfly_client_config::NAME),
@ -262,24 +267,9 @@ lazy_static! {
Opts::new("disk_usage_space_total", "Gauge of the disk usage space in bytes").namespace(dragonfly_client_config::SERVICE_NAME).subsystem(dragonfly_client_config::NAME),
&[]
).expect("metric can be created");
/// DISK_WRITTEN_BYTES is used to count of the disk written bytes.
pub static ref DISK_WRITTEN_BYTES: IntGaugeVec =
IntGaugeVec::new(
Opts::new("disk_written_bytes", "Gauge of the disk written bytes.").namespace(dragonfly_client_config::SERVICE_NAME).subsystem(dragonfly_client_config::NAME),
&[]
).expect("metric can be created");
/// DISK_READ_BYTES is used to count of the disk read bytes.
pub static ref DISK_READ_BYTES: IntGaugeVec =
IntGaugeVec::new(
Opts::new("disk_read_bytes", "Gauge of the disk read bytes.").namespace(dragonfly_client_config::SERVICE_NAME).subsystem(dragonfly_client_config::NAME),
&[]
).expect("metric can be created");
}
/// register_custom_metrics registers all custom metrics.
#[instrument(skip_all)]
fn register_custom_metrics() {
REGISTRY
.register(Box::new(VERSION_GAUGE.clone()))
@ -345,12 +335,6 @@ fn register_custom_metrics() {
.register(Box::new(PROXY_REQUEST_VIA_DFDAEMON_COUNT.clone()))
.expect("metric can be registered");
REGISTRY
.register(Box::new(
PROXY_REQUEST_VIA_DFDAEMON_AND_CACHE_HITS_COUNT.clone(),
))
.expect("metric can be registered");
REGISTRY
.register(Box::new(UPDATE_TASK_COUNT.clone()))
.expect("metric can be registered");
@ -367,6 +351,14 @@ fn register_custom_metrics() {
.register(Box::new(STAT_TASK_FAILURE_COUNT.clone()))
.expect("metric can be registered");
REGISTRY
.register(Box::new(LIST_TASK_ENTRIES_COUNT.clone()))
.expect("metric can be registered");
REGISTRY
.register(Box::new(LIST_TASK_ENTRIES_FAILURE_COUNT.clone()))
.expect("metric can be registered");
REGISTRY
.register(Box::new(DELETE_TASK_COUNT.clone()))
.expect("metric can be registered");
@ -390,18 +382,9 @@ fn register_custom_metrics() {
REGISTRY
.register(Box::new(DISK_USAGE_SPACE.clone()))
.expect("metric can be registered");
REGISTRY
.register(Box::new(DISK_WRITTEN_BYTES.clone()))
.expect("metric can be registered");
REGISTRY
.register(Box::new(DISK_READ_BYTES.clone()))
.expect("metric can be registered");
}
/// reset_custom_metrics resets all custom metrics.
#[instrument(skip_all)]
fn reset_custom_metrics() {
VERSION_GAUGE.reset();
DOWNLOAD_TASK_COUNT.reset();
@ -419,19 +402,18 @@ fn reset_custom_metrics() {
PROXY_REQUEST_COUNT.reset();
PROXY_REQUEST_FAILURE_COUNT.reset();
PROXY_REQUEST_VIA_DFDAEMON_COUNT.reset();
PROXY_REQUEST_VIA_DFDAEMON_AND_CACHE_HITS_COUNT.reset();
UPDATE_TASK_COUNT.reset();
UPDATE_TASK_FAILURE_COUNT.reset();
STAT_TASK_COUNT.reset();
STAT_TASK_FAILURE_COUNT.reset();
LIST_TASK_ENTRIES_COUNT.reset();
LIST_TASK_ENTRIES_FAILURE_COUNT.reset();
DELETE_TASK_COUNT.reset();
DELETE_TASK_FAILURE_COUNT.reset();
DELETE_HOST_COUNT.reset();
DELETE_HOST_FAILURE_COUNT.reset();
DISK_SPACE.reset();
DISK_USAGE_SPACE.reset();
DISK_WRITTEN_BYTES.reset();
DISK_READ_BYTES.reset();
}
/// TaskSize represents the size of the task.
@ -762,14 +744,6 @@ pub fn collect_proxy_request_via_dfdaemon_metrics() {
.inc();
}
/// collect_proxy_request_via_dfdaemon_and_cache_hits_metrics collects the proxy request via
/// dfdaemon and cache hits metrics.
pub fn collect_proxy_request_via_dfdaemon_and_cache_hits_metrics() {
PROXY_REQUEST_VIA_DFDAEMON_AND_CACHE_HITS_COUNT
.with_label_values(&[])
.inc();
}
/// collect_update_task_started_metrics collects the update task started metrics.
pub fn collect_update_task_started_metrics(typ: i32) {
UPDATE_TASK_COUNT
@ -798,6 +772,20 @@ pub fn collect_stat_task_failure_metrics(typ: i32) {
.inc();
}
/// collect_list_task_entries_started_metrics collects the list task entries started metrics.
pub fn collect_list_task_entries_started_metrics(typ: i32) {
LIST_TASK_ENTRIES_COUNT
.with_label_values(&[typ.to_string().as_str()])
.inc();
}
/// collect_list_task_entries_failure_metrics collects the list task entries failure metrics.
pub fn collect_list_task_entries_failure_metrics(typ: i32) {
LIST_TASK_ENTRIES_FAILURE_COUNT
.with_label_values(&[typ.to_string().as_str()])
.inc();
}
/// collect_delete_task_started_metrics collects the delete task started metrics.
pub fn collect_delete_task_started_metrics(typ: i32) {
DELETE_TASK_COUNT
@ -823,7 +811,7 @@ pub fn collect_delete_host_failure_metrics() {
}
/// collect_disk_metrics collects the disk metrics.
pub fn collect_disk_metrics(path: &Path, system: &Arc<Mutex<System>>) {
pub fn collect_disk_metrics(path: &Path) {
// Collect disk space metrics.
let stats = match fs2::statvfs(path) {
Ok(stats) => stats,
@ -840,24 +828,6 @@ pub fn collect_disk_metrics(path: &Path, system: &Arc<Mutex<System>>) {
DISK_USAGE_SPACE
.with_label_values(&[])
.set(usage_space as i64);
// Collect disk bandwidth metrics.
let mut sys = system.lock().unwrap();
sys.refresh_processes_specifics(
ProcessesToUpdate::All,
true,
ProcessRefreshKind::new()
.with_disk_usage()
.with_exe(UpdateKind::Always),
);
let process = sys.process(sysinfo::get_current_pid().unwrap()).unwrap();
DISK_WRITTEN_BYTES
.with_label_values(&[])
.set(process.disk_usage().written_bytes as i64);
DISK_READ_BYTES
.with_label_values(&[])
.set(process.disk_usage().read_bytes as i64);
}
/// Metrics is the metrics server.
@ -866,9 +836,6 @@ pub struct Metrics {
/// config is the configuration of the dfdaemon.
config: Arc<Config>,
// system is the system information, only used for collecting disk metrics.
system: Arc<Mutex<System>>,
/// shutdown is used to shutdown the metrics server.
shutdown: shutdown::Shutdown,
@ -879,7 +846,6 @@ pub struct Metrics {
/// Metrics implements the metrics server.
impl Metrics {
/// new creates a new Metrics.
#[instrument(skip_all)]
pub fn new(
config: Arc<Config>,
shutdown: shutdown::Shutdown,
@ -887,20 +853,12 @@ impl Metrics {
) -> Self {
Self {
config,
system: Arc::new(Mutex::new(System::new_with_specifics(
RefreshKind::new().with_processes(
ProcessRefreshKind::new()
.with_disk_usage()
.with_exe(UpdateKind::Always),
),
))),
shutdown,
_shutdown_complete: shutdown_complete_tx,
}
}
/// run starts the metrics server.
#[instrument(skip_all)]
pub async fn run(&self) {
// Clone the shutdown channel.
let mut shutdown = self.shutdown.clone();
@ -921,7 +879,6 @@ impl Metrics {
// Clone the config.
let config = self.config.clone();
let system = self.system.clone();
// Create the metrics server address.
let addr = SocketAddr::new(
@ -933,7 +890,7 @@ impl Metrics {
let get_metrics_route = warp::path!("metrics")
.and(warp::get())
.and(warp::path::end())
.and_then(move || Self::get_metrics_handler(config.clone(), system.clone()));
.and_then(move || Self::get_metrics_handler(config.clone()));
// Delete the metrics route.
let delete_metrics_route = warp::path!("metrics")
@ -952,19 +909,15 @@ impl Metrics {
_ = shutdown.recv() => {
// Metrics server shutting down with signals.
info!("metrics server shutting down");
return
}
}
}
/// get_metrics_handler handles the metrics request of getting.
#[instrument(skip_all)]
async fn get_metrics_handler(
config: Arc<Config>,
system: Arc<Mutex<System>>,
) -> Result<impl Reply, Rejection> {
async fn get_metrics_handler(config: Arc<Config>) -> Result<impl Reply, Rejection> {
// Collect the disk space metrics.
collect_disk_metrics(config.storage.dir.as_path(), &system);
collect_disk_metrics(config.storage.dir.as_path());
// Encode custom metrics.
let encoder = TextEncoder::new();

View File

@ -1,228 +0,0 @@
/*
* Copyright 2024 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::resource::task::Task;
use dragonfly_api::common::v2::Range;
use dragonfly_api::dfdaemon::v2::DownloadTaskRequest;
use dragonfly_client_core::{Error, Result};
use dragonfly_client_util::http::{get_range, hashmap_to_headermap};
use lru::LruCache;
use std::cmp::{max, min};
use std::num::NonZeroUsize;
use std::sync::{Arc, Mutex};
/// Cache is the cache for storing http response by LRU algorithm.
#[derive(Clone)]
pub struct Cache {
/// pieces stores the piece cache data with piece id and value.
pieces: Arc<Mutex<LruCache<String, bytes::Bytes>>>,
/// task is the task manager.
task: Arc<Task>,
}
/// Cache implements the cache for storing http response by LRU algorithm.
impl Cache {
/// new creates a new cache with the specified capacity.
pub fn new(capacity: usize, task: Arc<Task>) -> Result<Self> {
let capacity = NonZeroUsize::new(capacity).ok_or(Error::InvalidParameter)?;
let pieces = Arc::new(Mutex::new(LruCache::new(capacity)));
Ok(Cache { pieces, task })
}
/// get_by_request gets the content from the cache by the request.
pub async fn get_by_request(
&self,
request: &DownloadTaskRequest,
) -> Result<Option<bytes::Bytes>> {
let Some(download) = &request.download else {
return Err(Error::InvalidParameter);
};
let task_id = self.task.id_generator.task_id(
&download.url,
download.piece_length,
download.tag.as_deref(),
download.application.as_deref(),
download.filtered_query_params.clone(),
)?;
let Some(task) = self.task.get(&task_id)? else {
return Ok(None);
};
let (Some(content_length), Some(piece_length)) =
(task.content_length(), task.piece_length())
else {
return Ok(None);
};
let Ok(request_header) = hashmap_to_headermap(&download.request_header) else {
return Ok(None);
};
let Ok(range) = get_range(&request_header, content_length) else {
return Ok(None);
};
let interested_pieces =
self.task
.piece
.calculate_interested(piece_length, content_length, range)?;
// Calculate the content capacity based on the interested pieces and push the content into
// the bytes.
let content_capacity = interested_pieces.len() * piece_length as usize;
let mut content = bytes::BytesMut::with_capacity(content_capacity);
for interested_piece in interested_pieces {
let piece_id = self.task.piece.id(&task_id, interested_piece.number);
let Some(piece_content) = self.get_piece(&piece_id) else {
return Ok(None);
};
// Calculate the target offset and length based on the range.
let (piece_target_offset, piece_target_length) =
calculate_piece_range(interested_piece.offset, interested_piece.length, range);
let begin = piece_target_offset;
let end = piece_target_offset + piece_target_length;
if begin >= piece_content.len() || end > piece_content.len() {
return Err(Error::InvalidParameter);
}
let piece_content = piece_content.slice(begin..end);
content.extend_from_slice(&piece_content);
}
Ok(Some(content.freeze()))
}
/// get_piece gets the piece content from the cache.
pub fn get_piece(&self, id: &str) -> Option<bytes::Bytes> {
let mut pieces = self.pieces.lock().unwrap();
pieces.get(id).cloned()
}
/// add_piece create the piece content into the cache, if the key already exists, no operation will
/// be performed.
pub fn add_piece(&self, id: &str, content: bytes::Bytes) {
let mut pieces = self.pieces.lock().unwrap();
if pieces.contains(id) {
return;
}
pieces.put(id.to_string(), content);
}
/// contains_piece checks whether the piece exists in the cache.
pub fn contains_piece(&self, id: &str) -> bool {
let pieces = self.pieces.lock().unwrap();
pieces.contains(id)
}
}
/// calculate_piece_range calculates the target offset and length based on the piece range and
/// request range.
pub fn calculate_piece_range(offset: u64, length: u64, range: Option<Range>) -> (usize, usize) {
if let Some(range) = range {
let target_offset = max(offset, range.start) - offset;
let target_length =
min(offset + length - 1, range.start + range.length - 1) - target_offset - offset + 1;
(target_offset as usize, target_length as usize)
} else {
(0, length as usize)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_calculate_piece_range() {
let test_cases = vec![
(1, 4, None, 0, 4),
(
1,
4,
Some(Range {
start: 1,
length: 4,
}),
0,
4,
),
(
1,
4,
Some(Range {
start: 2,
length: 1,
}),
1,
1,
),
(
1,
4,
Some(Range {
start: 1,
length: 1,
}),
0,
1,
),
(
1,
4,
Some(Range {
start: 4,
length: 1,
}),
3,
1,
),
(
1,
4,
Some(Range {
start: 0,
length: 2,
}),
0,
1,
),
(
1,
4,
Some(Range {
start: 4,
length: 3,
}),
3,
1,
),
];
for (piece_offset, piece_length, range, expected_offset, expected_length) in test_cases {
let (target_offset, target_length) =
calculate_piece_range(piece_offset, piece_length, range);
assert_eq!(target_offset, expected_offset);
assert_eq!(target_length, expected_length);
}
}
}

View File

@ -17,7 +17,7 @@
use bytesize::ByteSize;
use dragonfly_api::common::v2::Priority;
use reqwest::header::HeaderMap;
use tracing::{error, instrument};
use tracing::error;
/// DRAGONFLY_TAG_HEADER is the header key of tag in http request.
pub const DRAGONFLY_TAG_HEADER: &str = "X-Dragonfly-Tag";
@ -52,21 +52,43 @@ pub const DRAGONFLY_USE_P2P_HEADER: &str = "X-Dragonfly-Use-P2P";
/// If the value is "false", the range request will fetch the range content.
pub const DRAGONFLY_PREFETCH_HEADER: &str = "X-Dragonfly-Prefetch";
/// DRAGONFLY_OUTPUT_PATH_HEADER is the header key of output path in http request.
/// If the value is set, the downloaded file will be saved to the specified path,
/// and response the file by the body. If the specified path is exist, the file will be
/// removed and saved to the specified path.
/// DRAGONFLY_OUTPUT_PATH_HEADER is the header key of absolute output path in http request.
///
/// If `X-Dragonfly-Output-Path` is set, the downloaded file will be saved to the specified path.
/// Dfdaemon will try to create hard link to the output path before starting the download. If hard link creation fails,
/// it will copy the file to the output path after the download is completed.
/// For more details refer to https://github.com/dragonflyoss/design/blob/main/systems-analysis/file-download-workflow-with-hard-link/README.md.
pub const DRAGONFLY_OUTPUT_PATH_HEADER: &str = "X-Dragonfly-Output-Path";
/// DRAGONFLY_PIECE_LENGTH is the header key of piece length in http request.
/// DRAGONFLY_FORCE_HARD_LINK_HEADER is the header key of force hard link in http request.
///
/// `X-Dragonfly-Force-Hard-Link` is the flag to indicate whether the download file must be hard linked to the output path.
/// For more details refer to https://github.com/dragonflyoss/design/blob/main/systems-analysis/file-download-workflow-with-hard-link/README.md.
pub const DRAGONFLY_FORCE_HARD_LINK_HEADER: &str = "X-Dragonfly-Force-Hard-Link";
/// DRAGONFLY_PIECE_LENGTH_HEADER is the header key of piece length in http request.
/// If the value is set, the piece length will be used to download the file.
/// Different piece length will generate different task id. The value needs to
/// be set with human readable format and needs to be greater than or equal
/// to 4mib, for example: 4mib, 1gib
pub const DRAGONFLY_PIECE_LENGTH: &str = "X-Dragonfly-Piece-Length";
pub const DRAGONFLY_PIECE_LENGTH_HEADER: &str = "X-Dragonfly-Piece-Length";
/// DRAGONFLY_CONTENT_FOR_CALCULATING_TASK_ID_HEADER is the header key of content for calculating task id.
/// If DRAGONFLY_CONTENT_FOR_CALCULATING_TASK_ID_HEADER is set, use its value to calculate the task ID.
/// Otherwise, calculate the task ID based on `url`, `piece_length`, `tag`, `application`, and `filtered_query_params`.
pub const DRAGONFLY_CONTENT_FOR_CALCULATING_TASK_ID_HEADER: &str =
"X-Dragonfly-Content-For-Calculating-Task-ID";
/// DRAGONFLY_TASK_DOWNLOAD_FINISHED_HEADER is the response header key to indicate whether the task download finished.
/// When the task download is finished, the response will include this header with the value `"true"`,
/// indicating that the download hit the local cache.
pub const DRAGONFLY_TASK_DOWNLOAD_FINISHED_HEADER: &str = "X-Dragonfly-Task-Download-Finished";
/// DRAGONFLY_TASK_ID_HEADER is the response header key of task id. Client will calculate the task ID
/// based on `url`, `piece_length`, `tag`, `application`, and `filtered_query_params`.
pub const DRAGONFLY_TASK_ID_HEADER: &str = "X-Dragonfly-Task-ID";
/// get_tag gets the tag from http header.
#[instrument(skip_all)]
pub fn get_tag(header: &HeaderMap) -> Option<String> {
header
.get(DRAGONFLY_TAG_HEADER)
@ -75,7 +97,6 @@ pub fn get_tag(header: &HeaderMap) -> Option<String> {
}
/// get_application gets the application from http header.
#[instrument(skip_all)]
pub fn get_application(header: &HeaderMap) -> Option<String> {
header
.get(DRAGONFLY_APPLICATION_HEADER)
@ -84,7 +105,6 @@ pub fn get_application(header: &HeaderMap) -> Option<String> {
}
/// get_priority gets the priority from http header.
#[instrument(skip_all)]
pub fn get_priority(header: &HeaderMap) -> i32 {
let default_priority = Priority::Level6 as i32;
match header.get(DRAGONFLY_PRIORITY_HEADER) {
@ -106,7 +126,6 @@ pub fn get_priority(header: &HeaderMap) -> i32 {
}
/// get_registry gets the custom address of container registry from http header.
#[instrument(skip_all)]
pub fn get_registry(header: &HeaderMap) -> Option<String> {
header
.get(DRAGONFLY_REGISTRY_HEADER)
@ -115,7 +134,6 @@ pub fn get_registry(header: &HeaderMap) -> Option<String> {
}
/// get_filters gets the filters from http header.
#[instrument(skip_all)]
pub fn get_filtered_query_params(
header: &HeaderMap,
default_filtered_query_params: Vec<String>,
@ -133,7 +151,6 @@ pub fn get_filtered_query_params(
}
/// get_use_p2p gets the use p2p from http header.
#[instrument(skip_all)]
pub fn get_use_p2p(header: &HeaderMap) -> bool {
match header.get(DRAGONFLY_USE_P2P_HEADER) {
Some(value) => match value.to_str() {
@ -148,7 +165,6 @@ pub fn get_use_p2p(header: &HeaderMap) -> bool {
}
/// get_prefetch gets the prefetch from http header.
#[instrument(skip_all)]
pub fn get_prefetch(header: &HeaderMap) -> Option<bool> {
match header.get(DRAGONFLY_PREFETCH_HEADER) {
Some(value) => match value.to_str() {
@ -170,9 +186,23 @@ pub fn get_output_path(header: &HeaderMap) -> Option<String> {
.map(|output_path| output_path.to_string())
}
/// get_force_hard_link gets the force hard link from http header.
pub fn get_force_hard_link(header: &HeaderMap) -> bool {
match header.get(DRAGONFLY_FORCE_HARD_LINK_HEADER) {
Some(value) => match value.to_str() {
Ok(value) => value.eq_ignore_ascii_case("true"),
Err(err) => {
error!("get force hard link from header failed: {}", err);
false
}
},
None => false,
}
}
/// get_piece_length gets the piece length from http header.
pub fn get_piece_length(header: &HeaderMap) -> Option<ByteSize> {
match header.get(DRAGONFLY_PIECE_LENGTH) {
match header.get(DRAGONFLY_PIECE_LENGTH_HEADER) {
Some(piece_length) => match piece_length.to_str() {
Ok(piece_length) => match piece_length.parse::<ByteSize>() {
Ok(piece_length) => Some(piece_length),
@ -190,6 +220,14 @@ pub fn get_piece_length(header: &HeaderMap) -> Option<ByteSize> {
}
}
/// get_content_for_calculating_task_id gets the content for calculating task id from http header.
pub fn get_content_for_calculating_task_id(header: &HeaderMap) -> Option<String> {
header
.get(DRAGONFLY_CONTENT_FOR_CALCULATING_TASK_ID_HEADER)
.and_then(|content| content.to_str().ok())
.map(|content| content.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
@ -308,19 +346,60 @@ mod tests {
assert_eq!(get_output_path(&empty_headers), None);
}
#[test]
fn test_get_force_hard_link() {
let mut headers = HeaderMap::new();
headers.insert(
DRAGONFLY_FORCE_HARD_LINK_HEADER,
HeaderValue::from_static("true"),
);
assert!(get_force_hard_link(&headers));
headers.insert(
DRAGONFLY_FORCE_HARD_LINK_HEADER,
HeaderValue::from_static("false"),
);
assert!(!get_force_hard_link(&headers));
let empty_headers = HeaderMap::new();
assert!(!get_force_hard_link(&empty_headers));
}
#[test]
fn test_get_piece_length() {
let mut headers = HeaderMap::new();
headers.insert(DRAGONFLY_PIECE_LENGTH, HeaderValue::from_static("4mib"));
headers.insert(
DRAGONFLY_PIECE_LENGTH_HEADER,
HeaderValue::from_static("4mib"),
);
assert_eq!(get_piece_length(&headers), Some(ByteSize::mib(4)));
let empty_headers = HeaderMap::new();
assert_eq!(get_piece_length(&empty_headers), None);
headers.insert(DRAGONFLY_PIECE_LENGTH, HeaderValue::from_static("invalid"));
headers.insert(
DRAGONFLY_PIECE_LENGTH_HEADER,
HeaderValue::from_static("invalid"),
);
assert_eq!(get_piece_length(&headers), None);
headers.insert(DRAGONFLY_PIECE_LENGTH, HeaderValue::from_static("0"));
headers.insert(DRAGONFLY_PIECE_LENGTH_HEADER, HeaderValue::from_static("0"));
assert_eq!(get_piece_length(&headers), Some(ByteSize::b(0)));
}
#[test]
fn test_get_content_for_calculating_task_id() {
let mut headers = HeaderMap::new();
headers.insert(
DRAGONFLY_CONTENT_FOR_CALCULATING_TASK_ID_HEADER,
HeaderValue::from_static("test-content"),
);
assert_eq!(
get_content_for_calculating_task_id(&headers),
Some("test-content".to_string())
);
let empty_headers = HeaderMap::new();
assert_eq!(get_registry(&empty_headers), None);
}
}

View File

@ -16,15 +16,13 @@
use crate::grpc::{dfdaemon_download::DfdaemonDownloadClient, REQUEST_TIMEOUT};
use crate::metrics::{
collect_download_piece_traffic_metrics, collect_proxy_request_failure_metrics,
collect_proxy_request_started_metrics,
collect_proxy_request_via_dfdaemon_and_cache_hits_metrics,
collect_proxy_request_failure_metrics, collect_proxy_request_started_metrics,
collect_proxy_request_via_dfdaemon_metrics,
};
use crate::resource::{piece::MIN_PIECE_LENGTH, task::Task};
use crate::shutdown;
use bytes::Bytes;
use dragonfly_api::common::v2::{Download, TaskType, TrafficType};
use dragonfly_api::common::v2::{Download, TaskType};
use dragonfly_api::dfdaemon::v2::{
download_task_response, DownloadTaskRequest, DownloadTaskStartedResponse,
};
@ -36,8 +34,8 @@ use dragonfly_client_util::{
http::{hashmap_to_headermap, headermap_to_hashmap},
tls::{generate_self_signed_certs_by_ca_cert, generate_simple_self_signed_certs, NoVerifier},
};
use futures_util::TryStreamExt;
use http_body_util::{combinators::BoxBody, BodyExt, Empty, Full, StreamBody};
use futures::TryStreamExt;
use http_body_util::{combinators::BoxBody, BodyExt, Empty, StreamBody};
use hyper::body::Frame;
use hyper::client::conn::http1::Builder as ClientBuilder;
use hyper::server::conn::http1::Builder as ServerBuilder;
@ -48,25 +46,28 @@ use hyper_util::{
client::legacy::Client,
rt::{tokio::TokioIo, TokioExecutor},
};
use lazy_static::lazy_static;
use rcgen::Certificate;
use rustls::{RootCertStore, ServerConfig};
use rustls_pki_types::CertificateDer;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::io::{AsyncWriteExt, BufReader, BufWriter};
use tokio::net::TcpListener;
use tokio::net::TcpStream;
use tokio::sync::{mpsc, Barrier};
use tokio::time::sleep;
use tokio_rustls::TlsAcceptor;
use tokio_util::io::ReaderStream;
use tracing::{debug, error, info, instrument, Instrument, Span};
pub mod cache;
pub mod header;
lazy_static! {
/// SUPPORTED_HTTP_PROTOCOLS is the supported HTTP protocols, including http/1.1 and http/1.0.
static ref SUPPORTED_HTTP_PROTOCOLS: Vec<Vec<u8>> = vec![b"http/1.1".to_vec(), b"http/1.0".to_vec()];
}
/// Response is the response of the proxy server.
pub type Response = hyper::Response<BoxBody<Bytes, ClientError>>;
@ -75,9 +76,6 @@ pub struct Proxy {
/// config is the configuration of the dfdaemon.
config: Arc<Config>,
/// cache is the cache manager for storing the piece content.
cache: Arc<cache::Cache>,
/// task is the task manager.
task: Arc<Task>,
@ -101,7 +99,6 @@ pub struct Proxy {
/// Proxy implements the proxy server.
impl Proxy {
/// new creates a new Proxy.
#[instrument(skip_all)]
pub fn new(
config: Arc<Config>,
task: Arc<Task>,
@ -110,7 +107,6 @@ impl Proxy {
) -> Self {
let mut proxy = Self {
config: config.clone(),
cache: Arc::new(cache::Cache::new(config.proxy.cache_capacity, task.clone()).unwrap()),
task: task.clone(),
addr: SocketAddr::new(config.proxy.server.ip.unwrap(), config.proxy.server.port),
registry_cert: Arc::new(None),
@ -147,9 +143,9 @@ impl Proxy {
}
/// run starts the proxy server.
#[instrument(skip_all)]
pub async fn run(&self, grpc_server_started_barrier: Arc<Barrier>) -> ClientResult<()> {
let mut shutdown = self.shutdown.clone();
let read_buffer_size = self.config.proxy.read_buffer_size;
// When the grpc server is started, notify the barrier. If the shutdown signal is received
// before barrier is waited successfully, the server will shutdown immediately.
@ -169,6 +165,23 @@ impl Proxy {
DfdaemonDownloadClient::new_unix(self.config.download.server.socket_path.clone())
.await?;
#[derive(Clone)]
struct Context {
config: Arc<Config>,
task: Arc<Task>,
dfdaemon_download_client: DfdaemonDownloadClient,
registry_cert: Arc<Option<Vec<CertificateDer<'static>>>>,
server_ca_cert: Arc<Option<Certificate>>,
}
let context = Context {
config: self.config.clone(),
task: self.task.clone(),
dfdaemon_download_client,
registry_cert: self.registry_cert.clone(),
server_ca_cert: self.server_ca_cert.clone(),
};
let listener = TcpListener::bind(self.addr).await?;
info!("proxy server listening on {}", self.addr);
@ -183,21 +196,21 @@ impl Proxy {
let io = TokioIo::new(tcp);
debug!("accepted connection from {}", remote_address);
let config = self.config.clone();
let cache = self.cache.clone();
let task = self.task.clone();
let dfdaemon_download_client = dfdaemon_download_client.clone();
let registry_cert = self.registry_cert.clone();
let server_ca_cert = self.server_ca_cert.clone();
let context = context.clone();
tokio::task::spawn(async move {
if let Err(err) = ServerBuilder::new()
.keep_alive(true)
.max_buf_size(read_buffer_size)
.preserve_header_case(true)
.title_case_headers(true)
.serve_connection(
io,
service_fn(move |request| handler(config.clone(), cache.clone(), task.clone(), request, dfdaemon_download_client.clone(), registry_cert.clone(), server_ca_cert.clone())),
service_fn(move |request|{
let context = context.clone();
async move {
handler(context.config, context.task, request, context.dfdaemon_download_client, context.registry_cert, context.server_ca_cert, remote_address.ip()).await
}
} ),
)
.with_upgrades()
.await
@ -218,16 +231,21 @@ impl Proxy {
}
/// handler handles the request from the client.
#[instrument(skip_all, fields(uri, method))]
#[instrument(skip_all, fields(url, method, remote_ip))]
pub async fn handler(
config: Arc<Config>,
cache: Arc<cache::Cache>,
task: Arc<Task>,
request: Request<hyper::body::Incoming>,
dfdaemon_download_client: DfdaemonDownloadClient,
registry_cert: Arc<Option<Vec<CertificateDer<'static>>>>,
server_ca_cert: Arc<Option<Certificate>>,
remote_ip: std::net::IpAddr,
) -> ClientResult<Response> {
// Span record the url and method.
Span::current().record("url", request.uri().to_string().as_str());
Span::current().record("method", request.method().as_str());
Span::current().record("remote_ip", remote_ip.to_string().as_str());
// Record the proxy request started metrics. The metrics will be recorded
// when the request is kept alive.
collect_proxy_request_started_metrics();
@ -238,9 +256,9 @@ pub async fn handler(
if Method::CONNECT == request.method() {
return registry_mirror_https_handler(
config,
cache,
task,
request,
remote_ip,
dfdaemon_download_client,
registry_cert,
server_ca_cert,
@ -250,26 +268,22 @@ pub async fn handler(
return registry_mirror_http_handler(
config,
cache,
task,
request,
remote_ip,
dfdaemon_download_client,
registry_cert,
)
.await;
}
// Span record the uri and method.
Span::current().record("uri", request.uri().to_string().as_str());
Span::current().record("method", request.method().as_str());
// Handle CONNECT request.
if Method::CONNECT == request.method() {
return https_handler(
config,
cache,
task,
request,
remote_ip,
dfdaemon_download_client,
registry_cert,
server_ca_cert,
@ -279,9 +293,9 @@ pub async fn handler(
http_handler(
config,
cache,
task,
request,
remote_ip,
dfdaemon_download_client,
registry_cert,
)
@ -292,18 +306,18 @@ pub async fn handler(
#[instrument(skip_all)]
pub async fn registry_mirror_http_handler(
config: Arc<Config>,
cache: Arc<cache::Cache>,
task: Arc<Task>,
request: Request<hyper::body::Incoming>,
remote_ip: std::net::IpAddr,
dfdaemon_download_client: DfdaemonDownloadClient,
registry_cert: Arc<Option<Vec<CertificateDer<'static>>>>,
) -> ClientResult<Response> {
let request = make_registry_mirror_request(config.clone(), request)?;
return http_handler(
config,
cache,
task,
request,
remote_ip,
dfdaemon_download_client,
registry_cert,
)
@ -314,9 +328,9 @@ pub async fn registry_mirror_http_handler(
#[instrument(skip_all)]
pub async fn registry_mirror_https_handler(
config: Arc<Config>,
cache: Arc<cache::Cache>,
task: Arc<Task>,
request: Request<hyper::body::Incoming>,
remote_ip: std::net::IpAddr,
dfdaemon_download_client: DfdaemonDownloadClient,
registry_cert: Arc<Option<Vec<CertificateDer<'static>>>>,
server_ca_cert: Arc<Option<Certificate>>,
@ -324,9 +338,9 @@ pub async fn registry_mirror_https_handler(
let request = make_registry_mirror_request(config.clone(), request)?;
return https_handler(
config,
cache,
task,
request,
remote_ip,
dfdaemon_download_client,
registry_cert,
server_ca_cert,
@ -338,9 +352,9 @@ pub async fn registry_mirror_https_handler(
#[instrument(skip_all)]
pub async fn http_handler(
config: Arc<Config>,
cache: Arc<cache::Cache>,
task: Arc<Task>,
request: Request<hyper::body::Incoming>,
remote_ip: std::net::IpAddr,
dfdaemon_download_client: DfdaemonDownloadClient,
registry_cert: Arc<Option<Vec<CertificateDer<'static>>>>,
) -> ClientResult<Response> {
@ -374,10 +388,10 @@ pub async fn http_handler(
);
return proxy_via_dfdaemon(
config,
cache,
task,
&rule,
request,
remote_ip,
dfdaemon_download_client,
)
.await;
@ -393,10 +407,10 @@ pub async fn http_handler(
);
return proxy_via_dfdaemon(
config,
cache,
task,
&Rule::default(),
request,
remote_ip,
dfdaemon_download_client,
)
.await;
@ -423,9 +437,9 @@ pub async fn http_handler(
#[instrument(skip_all)]
pub async fn https_handler(
config: Arc<Config>,
cache: Arc<cache::Cache>,
task: Arc<Task>,
request: Request<hyper::body::Incoming>,
remote_ip: std::net::IpAddr,
dfdaemon_download_client: DfdaemonDownloadClient,
registry_cert: Arc<Option<Vec<CertificateDer<'static>>>>,
server_ca_cert: Arc<Option<Certificate>>,
@ -435,15 +449,17 @@ pub async fn https_handler(
// Proxy the request directly to the remote server.
if let Some(host) = request.uri().host() {
let host = host.to_string();
let port = request.uri().port_u16().unwrap_or(443);
tokio::task::spawn(async move {
match hyper::upgrade::on(request).await {
Ok(upgraded) => {
if let Err(e) = upgraded_tunnel(
config,
cache,
task,
upgraded,
host,
port,
remote_ip,
dfdaemon_download_client,
registry_cert,
server_ca_cert,
@ -470,10 +486,11 @@ pub async fn https_handler(
#[instrument(skip_all)]
async fn upgraded_tunnel(
config: Arc<Config>,
cache: Arc<cache::Cache>,
task: Arc<Task>,
upgraded: Upgraded,
host: String,
port: u16,
remote_ip: std::net::IpAddr,
dfdaemon_download_client: DfdaemonDownloadClient,
registry_cert: Arc<Option<Vec<CertificateDer<'static>>>>,
server_ca_cert: Arc<Option<Certificate>>,
@ -485,11 +502,11 @@ async fn upgraded_tunnel(
let (server_certs, server_key) = match server_ca_cert.as_ref() {
Some(server_ca_cert) => {
info!("generate self-signed certificate by CA certificate");
generate_self_signed_certs_by_ca_cert(server_ca_cert, subject_alt_names)?
generate_self_signed_certs_by_ca_cert(server_ca_cert, host.as_ref(), subject_alt_names)?
}
None => {
info!("generate simple self-signed certificate");
generate_simple_self_signed_certs(subject_alt_names)?
generate_simple_self_signed_certs(host.as_ref(), subject_alt_names)?
}
};
@ -498,7 +515,7 @@ async fn upgraded_tunnel(
.with_no_client_auth()
.with_single_cert(server_certs, server_key)
.or_err(ErrorType::TLSConfigError)?;
server_config.alpn_protocols = vec![b"http/1.1".to_vec(), b"http/1.0".to_vec()];
server_config.alpn_protocols = SUPPORTED_HTTP_PROTOCOLS.clone();
let tls_acceptor = TlsAcceptor::from(Arc::new(server_config));
let tls_stream = tls_acceptor.accept(TokioIo::new(upgraded)).await?;
@ -518,10 +535,11 @@ async fn upgraded_tunnel(
service_fn(move |request| {
upgraded_handler(
config.clone(),
cache.clone(),
task.clone(),
host.clone(),
port,
request,
remote_ip,
dfdaemon_download_client.clone(),
registry_cert.clone(),
)
@ -537,18 +555,20 @@ async fn upgraded_tunnel(
}
/// upgraded_handler handles the upgraded https request from the client.
#[instrument(skip_all, fields(uri, method))]
#[allow(clippy::too_many_arguments)]
#[instrument(skip_all, fields(url, method))]
pub async fn upgraded_handler(
config: Arc<Config>,
cache: Arc<cache::Cache>,
task: Arc<Task>,
host: String,
port: u16,
mut request: Request<hyper::body::Incoming>,
remote_ip: std::net::IpAddr,
dfdaemon_download_client: DfdaemonDownloadClient,
registry_cert: Arc<Option<Vec<CertificateDer<'static>>>>,
) -> ClientResult<Response> {
// Span record the uri and method.
Span::current().record("uri", request.uri().to_string().as_str());
// Span record the url and method.
Span::current().record("url", request.uri().to_string().as_str());
Span::current().record("method", request.method().as_str());
// Authenticate the request with the basic auth.
@ -567,8 +587,18 @@ pub async fn upgraded_handler(
// If the scheme is not set, set the scheme to https.
if request.uri().scheme().is_none() {
*request.uri_mut() = format!("https://{}{}", host, request.uri())
.parse()
let builder = http::uri::Builder::new();
*request.uri_mut() = builder
.scheme("https")
.authority(format!("{}:{}", host, port))
.path_and_query(
request
.uri()
.path_and_query()
.map(|v| v.as_str())
.unwrap_or("/"),
)
.build()
.or_err(ErrorType::ParseError)?;
}
@ -585,10 +615,10 @@ pub async fn upgraded_handler(
);
return proxy_via_dfdaemon(
config,
cache,
task,
&rule,
request,
remote_ip,
dfdaemon_download_client,
)
.await;
@ -604,10 +634,10 @@ pub async fn upgraded_handler(
);
return proxy_via_dfdaemon(
config,
cache,
task,
&Rule::default(),
request,
remote_ip,
dfdaemon_download_client,
)
.await;
@ -634,55 +664,27 @@ pub async fn upgraded_handler(
#[instrument(skip_all, fields(host_id, task_id, peer_id))]
async fn proxy_via_dfdaemon(
config: Arc<Config>,
cache: Arc<cache::Cache>,
task: Arc<Task>,
rule: &Rule,
request: Request<hyper::body::Incoming>,
remote_ip: std::net::IpAddr,
dfdaemon_download_client: DfdaemonDownloadClient,
) -> ClientResult<Response> {
// Collect the metrics for the proxy request via dfdaemon.
collect_proxy_request_via_dfdaemon_metrics();
// Make the download task request.
let download_task_request = match make_download_task_request(config.clone(), rule, request) {
Ok(download_task_request) => download_task_request,
Err(err) => {
error!("make download task request failed: {}", err);
return Ok(make_error_response(
http::StatusCode::INTERNAL_SERVER_ERROR,
None,
));
}
};
// Get the content from the cache by the request.
match cache.get_by_request(&download_task_request).await {
Ok(None) => {
debug!("cache miss");
}
Ok(Some(content)) => {
info!("cache hit");
// Collect the download piece traffic metrics and the proxy request via dfdaemon and
// cache hits metrics.
collect_proxy_request_via_dfdaemon_and_cache_hits_metrics();
collect_download_piece_traffic_metrics(
&TrafficType::LocalPeer,
TaskType::Standard as i32,
content.len() as u64,
);
let body_boxed = Full::new(content).map_err(ClientError::from).boxed();
return Ok(Response::new(body_boxed));
}
Err(err) => {
error!("get content from cache failed: {}", err);
return Ok(make_error_response(
http::StatusCode::INTERNAL_SERVER_ERROR,
None,
));
}
}
let download_task_request =
match make_download_task_request(config.clone(), rule, request, remote_ip) {
Ok(download_task_request) => download_task_request,
Err(err) => {
error!("make download task request failed: {}", err);
return Ok(make_error_response(
http::StatusCode::INTERNAL_SERVER_ERROR,
None,
));
}
};
// Download the task by the dfdaemon download client.
let response = match dfdaemon_download_client
@ -749,25 +751,29 @@ async fn proxy_via_dfdaemon(
));
};
// Write the task data to the reader.
let (reader, mut writer) = tokio::io::duplex(256 * 1024);
// Write the status code to the writer.
let (sender, mut receiver) = mpsc::channel(10 * 1024);
// Get the read buffer size from the config.
let read_buffer_size = config.proxy.read_buffer_size;
// Write the task data to the reader.
let (reader, writer) = tokio::io::duplex(read_buffer_size);
let mut writer = BufWriter::with_capacity(read_buffer_size, writer);
let reader_stream = ReaderStream::with_capacity(reader, read_buffer_size);
// Construct the response body.
let reader_stream = ReaderStream::new(reader);
let stream_body = StreamBody::new(reader_stream.map_ok(Frame::data).map_err(ClientError::from));
let boxed_body = stream_body.boxed();
// Construct the response.
let mut response = Response::new(boxed_body);
*response.headers_mut() = make_response_headers(download_task_started_response.clone())?;
*response.headers_mut() = make_response_headers(
message.task_id.as_str(),
download_task_started_response.clone(),
)?;
*response.status_mut() = http::StatusCode::OK;
// Get the read buffer size from the config.
let read_buffer_size = config.proxy.read_buffer_size;
// Return the response if the client return the first piece.
let mut initialized = false;
@ -801,10 +807,6 @@ async fn proxy_via_dfdaemon(
),
) = message.response
{
// Sleep for a while to avoid the out stream is aborted. If the task is small, proxy read the piece
// before the task download is finished. It will cause `user body write aborted` error.
sleep(Duration::from_millis(1)).await;
// Send the none response to the client, if the first piece is received.
if !initialized {
debug!("first piece received, send response");
@ -824,9 +826,9 @@ async fn proxy_via_dfdaemon(
return;
};
let (piece_range_reader, piece_reader) = match task
let piece_range_reader = match task
.piece
.download_from_local_into_dual_async_read(
.download_from_local_into_async_read(
task.piece
.id(message.task_id.as_str(), piece.number)
.as_str(),
@ -838,7 +840,7 @@ async fn proxy_via_dfdaemon(
)
.await
{
Ok(dual_reader) => dual_reader,
Ok(piece_range_reader) => piece_range_reader,
Err(err) => {
error!("download piece reader error: {}", err);
if let Err(err) = writer.shutdown().await {
@ -853,14 +855,10 @@ async fn proxy_via_dfdaemon(
let piece_range_reader =
BufReader::with_capacity(read_buffer_size, piece_range_reader);
// Write the piece data to the pipe in order and store the piece reader
// in the cache.
finished_piece_readers
.insert(piece.number, (piece_range_reader, piece_reader));
while let Some((mut piece_range_reader, piece_reader)) =
finished_piece_readers
.get_mut(&need_piece_number)
.map(|(range_reader, reader)| (range_reader, reader))
// Write the piece data to the pipe in order.
finished_piece_readers.insert(piece.number, piece_range_reader);
while let Some(mut piece_range_reader) =
finished_piece_readers.remove(&need_piece_number)
{
debug!("copy piece {} to stream", need_piece_number);
if let Err(err) =
@ -874,31 +872,6 @@ async fn proxy_via_dfdaemon(
return;
}
// If the piece is not in the cache, add it to the cache.
let piece_id =
task.piece.id(message.task_id.as_str(), need_piece_number);
if !cache.contains_piece(&piece_id) {
let mut content =
bytes::BytesMut::with_capacity(piece.length as usize);
loop {
let n = match piece_reader.read_buf(&mut content).await {
Ok(n) => n,
Err(err) => {
error!("read piece reader error: {}", err);
break;
}
};
// When the piece reader reads to the end, add the piece
// to the cache.
if n == 0 {
cache.add_piece(&piece_id, content.freeze());
break;
}
}
}
need_piece_number += 1;
}
} else {
@ -1046,7 +1019,6 @@ async fn proxy_via_https(
}
/// make_registry_mirror_request makes a registry mirror request by the request.
#[instrument(skip_all)]
fn make_registry_mirror_request(
config: Arc<Config>,
mut request: Request<hyper::body::Incoming>,
@ -1080,11 +1052,11 @@ fn make_registry_mirror_request(
}
/// make_download_task_request makes a download task request by the request.
#[instrument(skip_all)]
fn make_download_task_request(
config: Arc<Config>,
rule: &Rule,
request: Request<hyper::body::Incoming>,
remote_ip: std::net::IpAddr,
) -> ClientResult<DownloadTaskRequest> {
// Convert the Reqwest header to the Hyper header.
let mut header = request.headers().clone();
@ -1119,6 +1091,7 @@ fn make_download_task_request(
),
request_header: headermap_to_hashmap(&header),
piece_length,
// Need the absolute path.
output_path: header::get_output_path(&header),
timeout: None,
need_back_to_source: false,
@ -1129,14 +1102,15 @@ fn make_download_task_request(
hdfs: None,
is_prefetch: false,
need_piece_content: false,
load_to_cache: false,
force_hard_link: header::get_force_hard_link(&header),
content_for_calculating_task_id: header::get_content_for_calculating_task_id(&header),
remote_ip: Some(remote_ip.to_string()),
}),
})
}
/// need_prefetch returns whether the prefetch is needed by the configuration and the request
/// header.
#[instrument(skip_all)]
fn need_prefetch(config: Arc<Config>, header: &http::HeaderMap) -> bool {
// If the header not contains the range header, the request does not need prefetch.
if !header.contains_key(reqwest::header::RANGE) {
@ -1150,11 +1124,10 @@ fn need_prefetch(config: Arc<Config>, header: &http::HeaderMap) -> bool {
}
// Return the prefetch value from the configuration.
return config.proxy.prefetch;
config.proxy.prefetch
}
/// make_download_url makes a download url by the given uri.
#[instrument(skip_all)]
fn make_download_url(
uri: &hyper::Uri,
use_tls: bool,
@ -1179,8 +1152,8 @@ fn make_download_url(
}
/// make_response_headers makes the response headers.
#[instrument(skip_all)]
fn make_response_headers(
task_id: &str,
mut download_task_started_response: DownloadTaskStartedResponse,
) -> ClientResult<hyper::header::HeaderMap> {
// Insert the content range header to the response header.
@ -1201,18 +1174,28 @@ fn make_response_headers(
);
};
if download_task_started_response.is_finished {
download_task_started_response.response_header.insert(
header::DRAGONFLY_TASK_DOWNLOAD_FINISHED_HEADER.to_string(),
"true".to_string(),
);
}
download_task_started_response.response_header.insert(
header::DRAGONFLY_TASK_ID_HEADER.to_string(),
task_id.to_string(),
);
hashmap_to_headermap(&download_task_started_response.response_header)
}
/// find_matching_rule returns whether the dfdaemon should be used to download the task.
/// If the dfdaemon should be used, return the matched rule.
#[instrument(skip_all)]
fn find_matching_rule(rules: Option<&[Rule]>, url: &str) -> Option<Rule> {
rules?.iter().find(|rule| rule.regex.is_match(url)).cloned()
}
/// make_error_response makes an error response with the given status and message.
#[instrument(skip_all)]
fn make_error_response(status: http::StatusCode, header: Option<http::HeaderMap>) -> Response {
let mut response = Response::new(empty());
*response.status_mut() = status;
@ -1226,7 +1209,6 @@ fn make_error_response(status: http::StatusCode, header: Option<http::HeaderMap>
}
/// empty returns an empty body.
#[instrument(skip_all)]
fn empty() -> BoxBody<Bytes, ClientError> {
Empty::<Bytes>::new()
.map_err(|never| match never {})

View File

@ -84,7 +84,6 @@ pub struct PersistentCacheTask {
/// PersistentCacheTask is the implementation of PersistentCacheTask.
impl PersistentCacheTask {
/// new creates a new PersistentCacheTask.
#[instrument(skip_all)]
pub fn new(
config: Arc<Config>,
id_generator: Arc<IDGenerator>,
@ -105,7 +104,7 @@ impl PersistentCacheTask {
id_generator,
storage,
scheduler_client,
piece: piece.clone(),
piece,
})
}
@ -129,8 +128,9 @@ impl PersistentCacheTask {
let ttl = Duration::try_from(request.ttl.ok_or(Error::UnexpectedResponse)?)
.or_err(ErrorType::ParseError)?;
// Get the content length of the file.
let content_length = std::fs::metadata(path.as_path())
// Get the content length of the file asynchronously.
let content_length = tokio::fs::metadata(path.as_path())
.await
.inspect_err(|err| {
error!("get file metadata error: {}", err);
})?
@ -150,8 +150,7 @@ impl PersistentCacheTask {
};
// Notify the scheduler that the persistent cache task is started.
match self
.scheduler_client
self.scheduler_client
.upload_persistent_cache_task_started(UploadPersistentCacheTaskStartedRequest {
host_id: host_id.to_string(),
task_id: task_id.to_string(),
@ -167,13 +166,7 @@ impl PersistentCacheTask {
ttl: request.ttl,
})
.await
{
Ok(_) => {}
Err(err) => {
error!("upload persistent cache task started: {}", err);
return Err(err);
}
}
.inspect_err(|err| error!("upload persistent cache task started: {}", err))?;
// Check if the storage has enough space to store the persistent cache task.
let has_enough_space = self.storage.has_enough_space(content_length)?;
@ -456,14 +449,40 @@ impl PersistentCacheTask {
}
}
self.storage.download_persistent_cache_task_started(
task_id,
ttl,
request.persistent,
response.piece_length,
response.content_length,
created_at.naive_utc(),
)
let task = self
.storage
.download_persistent_cache_task_started(
task_id,
ttl,
request.persistent,
response.piece_length,
response.content_length,
created_at.naive_utc(),
)
.await?;
// Attempt to create a hard link from the task file to the output path.
//
// Behavior based on force_hard_link setting:
// 1. force_hard_link is true:
// - Success: Continue processing
// - Failure: Return error immediately
// 2. force_hard_link is false:
// - Success: Continue processing
// - Failure: Fall back to copying the file instead
if let Some(output_path) = &request.output_path {
if let Err(err) = self
.storage
.hard_link_persistent_cache_task(task_id, Path::new(output_path.as_str()))
.await
{
if request.force_hard_link {
return Err(err);
}
}
}
Ok(task)
}
/// download_finished updates the metadata of the persistent cache task when the task downloads finished.
@ -482,18 +501,19 @@ impl PersistentCacheTask {
Ok(())
}
/// hard_link_or_copy hard links or copies the persistent cache task content to the destination.
#[instrument(skip_all)]
pub async fn hard_link_or_copy(
&self,
task: &metadata::PersistentCacheTask,
to: &Path,
) -> ClientResult<()> {
/// is_same_dev_inode checks if the persistent cache task is on the same device inode as the given path.
pub async fn is_same_dev_inode(&self, id: &str, to: &Path) -> ClientResult<bool> {
self.storage
.hard_link_or_copy_persistent_cache_task(task, to)
.is_same_dev_inode_as_persistent_cache_task(id, to)
.await
}
//// copy_task copies the persistent cache task content to the destination.
#[instrument(skip_all)]
pub async fn copy_task(&self, id: &str, to: &Path) -> ClientResult<()> {
self.storage.copy_persistent_cache_task(id, to).await
}
/// download downloads a persistent cache task.
#[allow(clippy::too_many_arguments)]
#[instrument(skip_all)]
@ -727,7 +747,7 @@ impl PersistentCacheTask {
})? {
// Check if the schedule count is exceeded.
schedule_count += 1;
if schedule_count >= self.config.scheduler.max_schedule_count {
if schedule_count > self.config.scheduler.max_schedule_count {
in_stream_tx
.send_timeout(
AnnouncePersistentCachePeerRequest {
@ -991,7 +1011,8 @@ impl PersistentCacheTask {
host: peer.host,
})
.collect(),
);
)
.await;
let mut piece_collector_rx = piece_collector.run().await;
// Initialize the interrupt. If download from parent failed with scheduler or download
@ -1122,13 +1143,13 @@ impl PersistentCacheTask {
REQUEST_TIMEOUT,
)
.await
.inspect_err(|err| {
.unwrap_or_else(|err| {
error!(
"send DownloadPieceFinishedRequest for piece {} failed: {:?}",
piece_id, err
);
interrupt.store(true, Ordering::SeqCst);
})?;
});
// Send the download progress.
download_progress_tx
@ -1148,13 +1169,13 @@ impl PersistentCacheTask {
REQUEST_TIMEOUT,
)
.await
.inspect_err(|err| {
.unwrap_or_else(|err| {
error!(
"send DownloadPieceFinishedResponse for piece {} failed: {:?}",
piece_id, err
);
interrupt.store(true, Ordering::SeqCst);
})?;
});
info!(
"finished persistent cache piece {} from parent {:?}",
@ -1349,12 +1370,12 @@ impl PersistentCacheTask {
REQUEST_TIMEOUT,
)
.await
.inspect_err(|err| {
.unwrap_or_else(|err| {
error!(
"send DownloadPieceFinishedResponse for piece {} failed: {:?}",
piece_id, err
);
})?;
});
// Store the finished piece.
finished_pieces.push(interested_piece.clone());

View File

@ -30,6 +30,7 @@ use dragonfly_client_util::id_generator::IDGenerator;
use leaky_bucket::RateLimiter;
use reqwest::header::{self, HeaderMap};
use std::collections::HashMap;
use std::io::Cursor;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::io::{AsyncRead, AsyncReadExt};
@ -45,7 +46,7 @@ pub const MAX_PIECE_COUNT: u64 = 500;
pub const MIN_PIECE_LENGTH: u64 = 4 * 1024 * 1024;
/// MAX_PIECE_LENGTH is the maximum piece length.
pub const MAX_PIECE_LENGTH: u64 = 16 * 1024 * 1024;
pub const MAX_PIECE_LENGTH: u64 = 64 * 1024 * 1024;
/// PieceLengthStrategy sets the optimization strategy of piece length.
pub enum PieceLengthStrategy {
@ -67,8 +68,8 @@ pub struct Piece {
/// storage is the local storage.
storage: Arc<Storage>,
/// downloader_factory is the piece downloader factory.
downloader_factory: Arc<piece_downloader::DownloaderFactory>,
/// downloader is the piece downloader.
downloader: Arc<dyn piece_downloader::Downloader>,
/// backend_factory is the backend factory.
backend_factory: Arc<BackendFactory>,
@ -86,7 +87,6 @@ pub struct Piece {
/// Piece implements the piece manager.
impl Piece {
/// new returns a new Piece.
#[instrument(skip_all)]
pub fn new(
config: Arc<Config>,
id_generator: Arc<IDGenerator>,
@ -97,10 +97,11 @@ impl Piece {
config: config.clone(),
id_generator,
storage,
downloader_factory: Arc::new(piece_downloader::DownloaderFactory::new(
downloader: piece_downloader::DownloaderFactory::new(
config.storage.server.protocol.as_str(),
config.clone(),
)?),
)?
.build(),
backend_factory,
download_rate_limiter: Arc::new(
RateLimiter::builder()
@ -134,17 +135,20 @@ impl Piece {
/// id generates a new piece id.
#[inline]
#[instrument(skip_all)]
pub fn id(&self, task_id: &str, number: u32) -> String {
self.storage.piece_id(task_id, number)
}
/// get gets a piece from the local storage.
#[instrument(skip_all)]
pub fn get(&self, piece_id: &str) -> Result<Option<metadata::Piece>> {
self.storage.get_piece(piece_id)
}
/// get_all gets all pieces of a task from the local storage.
pub fn get_all(&self, task_id: &str) -> Result<Vec<metadata::Piece>> {
self.storage.get_pieces(task_id)
}
/// calculate_interested calculates the interested pieces by content_length and range.
pub fn calculate_interested(
&self,
@ -336,6 +340,7 @@ impl Piece {
) -> Result<impl AsyncRead> {
// Span record the piece_id.
Span::current().record("piece_id", piece_id);
Span::current().record("piece_length", length);
// Acquire the upload rate limiter.
if !disable_rate_limit {
@ -346,38 +351,7 @@ impl Piece {
self.storage
.upload_piece(piece_id, task_id, range)
.await
.inspect(|_reader| {
collect_upload_piece_traffic_metrics(
self.id_generator.task_type(task_id) as i32,
length,
);
})
}
/// upload_from_local_into_async_read. It will return two readers, one is the range reader, and the other is the
/// full reader of the piece.
#[instrument(skip_all, fields(piece_id))]
pub async fn upload_from_local_into_dual_async_read(
&self,
piece_id: &str,
task_id: &str,
length: u64,
range: Option<Range>,
disable_rate_limit: bool,
) -> Result<(impl AsyncRead, impl AsyncRead)> {
// Span record the piece_id.
Span::current().record("piece_id", piece_id);
// Acquire the upload rate limiter.
if !disable_rate_limit {
self.upload_rate_limiter.acquire(length as usize).await;
}
// Upload the piece content.
self.storage
.upload_piece_with_dual_read(piece_id, task_id, range)
.await
.inspect(|_reader| {
.inspect(|_| {
collect_upload_piece_traffic_metrics(
self.id_generator.task_type(task_id) as i32,
length,
@ -398,6 +372,7 @@ impl Piece {
) -> Result<impl AsyncRead> {
// Span record the piece_id.
Span::current().record("piece_id", piece_id);
Span::current().record("piece_length", length);
// Acquire the download rate limiter.
if !disable_rate_limit {
@ -414,38 +389,6 @@ impl Piece {
self.storage.upload_piece(piece_id, task_id, range).await
}
/// download_from_local_into_dual_async_read returns two readers, one is the range reader, and
/// the other is the full reader of the piece.
#[instrument(skip_all, fields(piece_id))]
pub async fn download_from_local_into_dual_async_read(
&self,
piece_id: &str,
task_id: &str,
length: u64,
range: Option<Range>,
disable_rate_limit: bool,
is_prefetch: bool,
) -> Result<(impl AsyncRead, impl AsyncRead)> {
// Span record the piece_id.
Span::current().record("piece_id", piece_id);
// Acquire the download rate limiter.
if !disable_rate_limit {
if is_prefetch {
// Acquire the prefetch rate limiter.
self.prefetch_rate_limiter.acquire(length as usize).await;
} else {
// Acquire the download rate limiter.
self.download_rate_limiter.acquire(length as usize).await;
}
}
// Upload the piece content.
self.storage
.upload_piece_with_dual_read(piece_id, task_id, range)
.await
}
/// download_from_local downloads a single piece from local cache. Fake the download piece
/// from the local cache, just collect the metrics.
#[instrument(skip_all)]
@ -472,14 +415,7 @@ impl Piece {
) -> Result<metadata::Piece> {
// Span record the piece_id.
Span::current().record("piece_id", piece_id);
if is_prefetch {
// Acquire the prefetch rate limiter.
self.prefetch_rate_limiter.acquire(length as usize).await;
} else {
// Acquire the download rate limiter.
self.download_rate_limiter.acquire(length as usize).await;
}
Span::current().record("piece_length", length);
// Record the start of downloading piece.
let piece = self
@ -490,9 +426,18 @@ impl Piece {
// If the piece is downloaded by the other thread,
// return the piece directly.
if piece.is_finished() {
info!("finished piece {} from local", piece_id);
return Ok(piece);
}
if is_prefetch {
// Acquire the prefetch rate limiter.
self.prefetch_rate_limiter.acquire(length as usize).await;
} else {
// Acquire the download rate limiter.
self.download_rate_limiter.acquire(length as usize).await;
}
// Create a dfdaemon client.
let host = parent.host.clone().ok_or_else(|| {
error!("peer host is empty");
@ -504,8 +449,7 @@ impl Piece {
})?;
let (content, offset, digest) = self
.downloader_factory
.build()
.downloader
.download_piece(
format!("{}:{}", host.ip, host.port).as_str(),
number,
@ -519,6 +463,7 @@ impl Piece {
error!("set piece metadata failed: {}", err)
};
})?;
let mut reader = Cursor::new(content);
// Record the finish of downloading piece.
match self
@ -527,9 +472,11 @@ impl Piece {
piece_id,
task_id,
offset,
length,
digest.as_str(),
parent.id.as_str(),
&mut content.as_slice(),
&mut reader,
self.config.storage.write_piece_timeout,
)
.await
{
@ -571,14 +518,7 @@ impl Piece {
) -> Result<metadata::Piece> {
// Span record the piece_id.
Span::current().record("piece_id", piece_id);
if is_prefetch {
// Acquire the prefetch rate limiter.
self.prefetch_rate_limiter.acquire(length as usize).await;
} else {
// Acquire the download rate limiter.
self.download_rate_limiter.acquire(length as usize).await;
}
Span::current().record("piece_length", length);
// Record the start of downloading piece.
let piece = self
@ -589,9 +529,18 @@ impl Piece {
// If the piece is downloaded by the other thread,
// return the piece directly.
if piece.is_finished() {
info!("finished piece {} from local", piece_id);
return Ok(piece);
}
if is_prefetch {
// Acquire the prefetch rate limiter.
self.prefetch_rate_limiter.acquire(length as usize).await;
} else {
// Acquire the download rate limiter.
self.download_rate_limiter.acquire(length as usize).await;
}
// Add range header to the request by offset and length.
let mut request_header = request_header.clone();
request_header.insert(
@ -689,6 +638,7 @@ impl Piece {
offset,
length,
&mut response.reader,
self.config.storage.write_piece_timeout,
)
.await
{
@ -714,7 +664,6 @@ impl Piece {
/// persistent_cache_id generates a new persistent cache piece id.
#[inline]
#[instrument(skip_all)]
pub fn persistent_cache_id(&self, task_id: &str, number: u32) -> String {
self.storage.persistent_cache_piece_id(task_id, number)
}
@ -752,6 +701,7 @@ impl Piece {
) -> Result<impl AsyncRead> {
// Span record the piece_id.
Span::current().record("piece_id", piece_id);
Span::current().record("piece_length", length);
// Acquire the upload rate limiter.
self.upload_rate_limiter.acquire(length as usize).await;
@ -760,7 +710,7 @@ impl Piece {
self.storage
.upload_persistent_cache_piece(piece_id, task_id, range)
.await
.inspect(|_reader| {
.inspect(|_| {
collect_upload_piece_traffic_metrics(
self.id_generator.task_type(task_id) as i32,
length,
@ -781,6 +731,7 @@ impl Piece {
) -> Result<impl AsyncRead> {
// Span record the piece_id.
Span::current().record("piece_id", piece_id);
Span::current().record("piece_length", length);
// Acquire the download rate limiter.
if !disable_rate_limit {
@ -825,6 +776,7 @@ impl Piece {
) -> Result<metadata::Piece> {
// Span record the piece_id.
Span::current().record("piece_id", piece_id);
Span::current().record("piece_length", length);
if is_prefetch {
// Acquire the prefetch rate limiter.
@ -843,6 +795,7 @@ impl Piece {
// If the piece is downloaded by the other thread,
// return the piece directly.
if piece.is_finished() {
info!("finished persistent cache piece {} from local", piece_id);
return Ok(piece);
}
@ -861,8 +814,7 @@ impl Piece {
})?;
let (content, offset, digest) = self
.downloader_factory
.build()
.downloader
.download_persistent_cache_piece(
format!("{}:{}", host.ip, host.port).as_str(),
number,
@ -880,6 +832,7 @@ impl Piece {
error!("set persistent cache piece metadata failed: {}", err)
};
})?;
let mut reader = Cursor::new(content);
// Record the finish of downloading piece.
match self
@ -888,9 +841,10 @@ impl Piece {
piece_id,
task_id,
offset,
length,
digest.as_str(),
parent.id.as_str(),
&mut content.as_slice(),
&mut reader,
)
.await
{

View File

@ -28,6 +28,8 @@ use tokio::task::JoinSet;
use tokio_stream::StreamExt;
use tracing::{error, info, instrument, Instrument};
const DEFAULT_WAIT_FOR_PIECE_FROM_DIFFERENT_PARENTS: Duration = Duration::from_millis(5);
/// CollectedParent is the parent peer collected from the parent.
#[derive(Clone, Debug)]
pub struct CollectedParent {
@ -67,15 +69,14 @@ pub struct PieceCollector {
/// interested_pieces is the pieces interested by the collector.
interested_pieces: Vec<metadata::Piece>,
/// collected_pieces is the pieces collected from peers.
collected_pieces: Arc<DashMap<u32, String>>,
/// collected_pieces is a map to store the collected pieces from different parents.
collected_pieces: Arc<DashMap<u32, Vec<CollectedParent>>>,
}
/// PieceCollector is used to collect pieces from peers.
impl PieceCollector {
/// new creates a new PieceCollector.
#[instrument(skip_all)]
pub fn new(
pub async fn new(
config: Arc<Config>,
host_id: &str,
task_id: &str,
@ -84,7 +85,7 @@ impl PieceCollector {
) -> Self {
let collected_pieces = Arc::new(DashMap::with_capacity(interested_pieces.len()));
for interested_piece in &interested_pieces {
collected_pieces.insert(interested_piece.number, String::new());
collected_pieces.insert(interested_piece.number, Vec::new());
}
Self {
@ -106,8 +107,8 @@ impl PieceCollector {
let parents = self.parents.clone();
let interested_pieces = self.interested_pieces.clone();
let collected_pieces = self.collected_pieces.clone();
let collected_piece_timeout = self.config.download.piece_timeout;
let (collected_piece_tx, collected_piece_rx) = mpsc::channel(10 * 1024);
let collected_piece_timeout = self.config.download.collected_piece_timeout;
let (collected_piece_tx, collected_piece_rx) = mpsc::channel(128 * 1024);
tokio::spawn(
async move {
Self::collect_from_parents(
@ -131,7 +132,25 @@ impl PieceCollector {
collected_piece_rx
}
/// collect_from_parents collects pieces from parents.
/// collect_from_parents collects pieces from multiple parents with load balancing strategy.
///
/// The collection process works in two phases:
/// 1. **Synchronization Phase**: Waits for a configured duration (DEFAULT_WAIT_FOR_PIECE_FROM_DIFFERENT_PARENTS)
/// to collect the same piece information from different parents. This allows the collector
/// to gather multiple sources for each piece.
///
/// 2. **Selection Phase**: After the wait period, randomly selects one parent from the available
/// candidates for each piece and forwards it to the piece downloader.
///
/// **Load Balancing Strategy**:
/// The random parent selection is designed to distribute download load across multiple parents
/// during concurrent piece downloads. This approach ensures:
/// - Optimal utilization of bandwidth from multiple parent nodes
/// - Prevention of overwhelming any single parent with too many requests
/// - Better overall download performance through parallel connections
///
/// This strategy is particularly effective when downloading multiple pieces simultaneously,
/// as it naturally spreads the workload across the available parent pool.
#[allow(clippy::too_many_arguments)]
#[instrument(skip_all)]
async fn collect_from_parents(
@ -140,7 +159,7 @@ impl PieceCollector {
task_id: &str,
parents: Vec<CollectedParent>,
interested_pieces: Vec<metadata::Piece>,
collected_pieces: Arc<DashMap<u32, String>>,
collected_pieces: Arc<DashMap<u32, Vec<CollectedParent>>>,
collected_piece_tx: Sender<CollectedPiece>,
collected_piece_timeout: Duration,
) -> Result<()> {
@ -153,9 +172,8 @@ impl PieceCollector {
host_id: String,
task_id: String,
parent: CollectedParent,
parents: Vec<CollectedParent>,
interested_pieces: Vec<metadata::Piece>,
collected_pieces: Arc<DashMap<u32, String>>,
collected_pieces: Arc<DashMap<u32, Vec<CollectedParent>>>,
collected_piece_tx: Sender<CollectedPiece>,
collected_piece_timeout: Duration,
) -> Result<CollectedParent> {
@ -168,15 +186,18 @@ impl PieceCollector {
})?;
// Create a dfdaemon client.
let dfdaemon_upload_client =
DfdaemonUploadClient::new(config, format!("http://{}:{}", host.ip, host.port))
.await
.inspect_err(|err| {
error!(
"create dfdaemon upload client from parent {} failed: {}",
parent.id, err
);
})?;
let dfdaemon_upload_client = DfdaemonUploadClient::new(
config,
format!("http://{}:{}", host.ip, host.port),
false,
)
.await
.inspect_err(|err| {
error!(
"create dfdaemon upload client from parent {} failed: {}",
parent.id, err
);
})?;
let response = dfdaemon_upload_client
.sync_pieces(SyncPiecesRequest {
@ -200,26 +221,36 @@ impl PieceCollector {
error!("sync pieces from parent {} failed: {}", parent.id, err);
})? {
let message = message?;
let mut parent_id =
match collected_pieces.try_get_mut(&message.number).try_unwrap() {
Some(parent_id) => parent_id,
None => continue,
};
parent_id.push_str(&parent.id);
if let Some(mut parents) = collected_pieces.get_mut(&message.number) {
parents.push(parent.clone());
} else {
continue;
}
// Wait for collecting the piece from different parents when the first
// piece is collected.
tokio::time::sleep(DEFAULT_WAIT_FOR_PIECE_FROM_DIFFERENT_PARENTS).await;
let parents = match collected_pieces.remove(&message.number) {
Some((_, parents)) => parents,
None => continue,
};
let parent = match parents.get(fastrand::usize(..parents.len())) {
Some(parent) => parent,
None => {
error!(
"collected_pieces does not contain parent for piece {}",
message.number
);
continue;
}
};
info!(
"received piece {}-{} metadata from parent {}",
"picked up piece {}-{} metadata from parent {}",
task_id, message.number, parent.id
);
let parent = parents
.iter()
.find(|parent| parent.id == parent_id.as_str())
.ok_or_else(|| {
error!("parent {} not found", parent_id.as_str());
Error::InvalidPeer(parent_id.clone())
})?;
collected_piece_tx
.send(CollectedPiece {
number: message.number,
@ -230,12 +261,6 @@ impl PieceCollector {
.inspect_err(|err| {
error!("send CollectedPiece failed: {}", err);
})?;
// Release the lock of the piece with parent_id.
drop(parent_id);
// Remove the piece from collected_pieces.
collected_pieces.remove(&message.number);
}
Ok(parent)
@ -247,7 +272,6 @@ impl PieceCollector {
host_id.to_string(),
task_id.to_string(),
parent.clone(),
parents.clone(),
interested_pieces.clone(),
collected_pieces.clone(),
collected_piece_tx.clone(),
@ -299,15 +323,14 @@ pub struct PersistentCachePieceCollector {
/// interested_pieces is the pieces interested by the collector.
interested_pieces: Vec<metadata::Piece>,
/// collected_pieces is the pieces collected from peers.
collected_pieces: Arc<DashMap<u32, String>>,
/// collected_pieces is a map to store the collected pieces from different parents.
collected_pieces: Arc<DashMap<u32, Vec<CollectedParent>>>,
}
/// PersistentCachePieceCollector is used to collect persistent cache pieces from peers.
impl PersistentCachePieceCollector {
/// new creates a new PieceCollector.
#[instrument(skip_all)]
pub fn new(
pub async fn new(
config: Arc<Config>,
host_id: &str,
task_id: &str,
@ -316,7 +339,7 @@ impl PersistentCachePieceCollector {
) -> Self {
let collected_pieces = Arc::new(DashMap::with_capacity(interested_pieces.len()));
for interested_piece in &interested_pieces {
collected_pieces.insert(interested_piece.number, String::new());
collected_pieces.insert(interested_piece.number, Vec::new());
}
Self {
@ -363,7 +386,25 @@ impl PersistentCachePieceCollector {
collected_piece_rx
}
/// collect_from_parents collects pieces from parents.
/// collect_from_parents collects pieces from multiple parents with load balancing strategy.
///
/// The collection process works in two phases:
/// 1. **Synchronization Phase**: Waits for a configured duration (DEFAULT_WAIT_FOR_PIECE_FROM_DIFFERENT_PARENTS)
/// to collect the same piece information from different parents. This allows the collector
/// to gather multiple sources for each piece.
///
/// 2. **Selection Phase**: After the wait period, randomly selects one parent from the available
/// candidates for each piece and forwards it to the piece downloader.
///
/// **Load Balancing Strategy**:
/// The random parent selection is designed to distribute download load across multiple parents
/// during concurrent piece downloads. This approach ensures:
/// - Optimal utilization of bandwidth from multiple parent nodes
/// - Prevention of overwhelming any single parent with too many requests
/// - Better overall download performance through parallel connections
///
/// This strategy is particularly effective when downloading multiple pieces simultaneously,
/// as it naturally spreads the workload across the available parent pool.
#[allow(clippy::too_many_arguments)]
#[instrument(skip_all)]
async fn collect_from_parents(
@ -372,7 +413,7 @@ impl PersistentCachePieceCollector {
task_id: &str,
parents: Vec<CollectedParent>,
interested_pieces: Vec<metadata::Piece>,
collected_pieces: Arc<DashMap<u32, String>>,
collected_pieces: Arc<DashMap<u32, Vec<CollectedParent>>>,
collected_piece_tx: Sender<CollectedPiece>,
collected_piece_timeout: Duration,
) -> Result<()> {
@ -385,9 +426,8 @@ impl PersistentCachePieceCollector {
host_id: String,
task_id: String,
parent: CollectedParent,
parents: Vec<CollectedParent>,
interested_pieces: Vec<metadata::Piece>,
collected_pieces: Arc<DashMap<u32, String>>,
collected_pieces: Arc<DashMap<u32, Vec<CollectedParent>>>,
collected_piece_tx: Sender<CollectedPiece>,
collected_piece_timeout: Duration,
) -> Result<CollectedParent> {
@ -400,15 +440,18 @@ impl PersistentCachePieceCollector {
})?;
// Create a dfdaemon client.
let dfdaemon_upload_client =
DfdaemonUploadClient::new(config, format!("http://{}:{}", host.ip, host.port))
.await
.inspect_err(|err| {
error!(
"create dfdaemon upload client from parent {} failed: {}",
parent.id, err
);
})?;
let dfdaemon_upload_client = DfdaemonUploadClient::new(
config,
format!("http://{}:{}", host.ip, host.port),
false,
)
.await
.inspect_err(|err| {
error!(
"create dfdaemon upload client from parent {} failed: {}",
parent.id, err
);
})?;
let response = dfdaemon_upload_client
.sync_persistent_cache_pieces(SyncPersistentCachePiecesRequest {
@ -438,26 +481,36 @@ impl PersistentCachePieceCollector {
);
})? {
let message = message?;
let mut parent_id =
match collected_pieces.try_get_mut(&message.number).try_unwrap() {
Some(parent_id) => parent_id,
None => continue,
};
parent_id.push_str(&parent.id);
if let Some(mut parents) = collected_pieces.get_mut(&message.number) {
parents.push(parent.clone());
} else {
continue;
}
// Wait for collecting the piece from different parents when the first
// piece is collected.
tokio::time::sleep(DEFAULT_WAIT_FOR_PIECE_FROM_DIFFERENT_PARENTS).await;
let parents = match collected_pieces.remove(&message.number) {
Some((_, parents)) => parents,
None => continue,
};
let parent = match parents.get(fastrand::usize(..parents.len())) {
Some(parent) => parent,
None => {
error!(
"collected_pieces does not contain parent for piece {}",
message.number
);
continue;
}
};
info!(
"received persistent cache piece {}-{} metadata from parent {}",
"picked up piece {}-{} metadata from parent {}",
task_id, message.number, parent.id
);
let parent = parents
.iter()
.find(|parent| parent.id == parent_id.as_str())
.ok_or_else(|| {
error!("parent {} not found", parent_id.as_str());
Error::InvalidPeer(parent_id.clone())
})?;
collected_piece_tx
.send(CollectedPiece {
number: message.number,
@ -468,12 +521,6 @@ impl PersistentCachePieceCollector {
.inspect_err(|err| {
error!("send CollectedPiece failed: {}", err);
})?;
// Release the lock of the piece with parent_id.
drop(parent_id);
// Remove the piece from collected_pieces.
collected_pieces.remove(&message.number);
}
Ok(parent)
@ -485,7 +532,6 @@ impl PersistentCachePieceCollector {
host_id.to_string(),
task_id.to_string(),
parent.clone(),
parents.clone(),
interested_pieces.clone(),
collected_pieces.clone(),
collected_piece_tx.clone(),

View File

@ -19,13 +19,23 @@ use dragonfly_api::dfdaemon::v2::{DownloadPersistentCachePieceRequest, DownloadP
use dragonfly_client_config::dfdaemon::Config;
use dragonfly_client_core::{Error, Result};
use dragonfly_client_storage::metadata;
use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tracing::{error, instrument};
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
use tracing::{debug, error, instrument};
/// DEFAULT_DOWNLOADER_CAPACITY is the default capacity of the downloader to store the clients.
const DEFAULT_DOWNLOADER_CAPACITY: usize = 2000;
/// DEFAULT_DOWNLOADER_IDLE_TIMEOUT is the default idle timeout for the downloader.
const DEFAULT_DOWNLOADER_IDLE_TIMEOUT: Duration = Duration::from_secs(30);
/// Downloader is the interface for downloading pieces, which is implemented by different
/// protocols. The downloader is used to download pieces from the other peers.
#[tonic::async_trait]
pub trait Downloader {
pub trait Downloader: Send + Sync {
/// download_piece downloads a piece from the other peer by different protocols.
async fn download_piece(
&self,
@ -56,10 +66,13 @@ pub struct DownloaderFactory {
/// DownloadFactory implements the DownloadFactory trait.
impl DownloaderFactory {
/// new returns a new DownloadFactory.
#[instrument(skip_all)]
pub fn new(protocol: &str, config: Arc<Config>) -> Result<Self> {
let downloader = match protocol {
"grpc" => Arc::new(GRPCDownloader::new(config.clone())),
"grpc" => Arc::new(GRPCDownloader::new(
config.clone(),
DEFAULT_DOWNLOADER_CAPACITY,
DEFAULT_DOWNLOADER_IDLE_TIMEOUT,
)),
_ => {
error!("downloader unsupported protocol: {}", protocol);
return Err(Error::InvalidParameter);
@ -70,24 +83,181 @@ impl DownloaderFactory {
}
/// build returns the downloader.
#[instrument(skip_all)]
pub fn build(&self) -> Arc<dyn Downloader + Send + Sync> {
pub fn build(&self) -> Arc<dyn Downloader> {
self.downloader.clone()
}
}
/// RequestGuard is the guard for the request.
struct RequestGuard {
/// active_requests is the number of the active requests.
active_requests: Arc<AtomicUsize>,
}
/// RequestGuard implements the guard for the request to add or subtract the active requests.
impl RequestGuard {
/// new returns a new RequestGuard.
fn new(active_requests: Arc<AtomicUsize>) -> Self {
active_requests.fetch_add(1, Ordering::SeqCst);
Self { active_requests }
}
}
/// RequestGuard implements the Drop trait.
impl Drop for RequestGuard {
/// drop subtracts the active requests.
fn drop(&mut self) {
self.active_requests.fetch_sub(1, Ordering::SeqCst);
}
}
/// DfdaemonUploadClientEntry is the entry of the dfdaemon upload client.
#[derive(Clone)]
struct DfdaemonUploadClientEntry {
/// client is the dfdaemon upload client.
client: DfdaemonUploadClient,
/// active_requests is the number of the active requests.
active_requests: Arc<AtomicUsize>,
/// actived_at is the time when the client is the last active time.
actived_at: Arc<std::sync::Mutex<Instant>>,
}
/// GRPCDownloader is the downloader for downloading pieces by the gRPC protocol.
/// It will reuse the dfdaemon upload clients to download pieces from the other peers by
/// peer's address.
pub struct GRPCDownloader {
/// config is the configuration of the dfdaemon.
config: Arc<Config>,
/// clients is the map of the dfdaemon upload clients.
clients: Arc<Mutex<HashMap<String, DfdaemonUploadClientEntry>>>,
/// capacity is the capacity of the dfdaemon upload clients. If the number of the
/// clients exceeds the capacity, it will clean up the idle clients.
capacity: usize,
/// client_idle_timeout is the idle timeout for the client. If the client is idle for a long
/// time, it will be removed when cleaning up the idle clients.
idle_timeout: Duration,
/// cleanup_at is the time when the client is the last cleanup time.
cleanup_at: Arc<Mutex<Instant>>,
}
/// GRPCDownloader implements the downloader with the gRPC protocol.
impl GRPCDownloader {
/// new returns a new GRPCDownloader.
#[instrument(skip_all)]
pub fn new(config: Arc<Config>) -> Self {
Self { config }
pub fn new(config: Arc<Config>, capacity: usize, idle_timeout: Duration) -> Self {
Self {
config,
clients: Arc::new(Mutex::new(HashMap::new())),
capacity,
idle_timeout,
cleanup_at: Arc::new(Mutex::new(Instant::now())),
}
}
/// client returns the dfdaemon upload client by the address.
///
/// Opterations:
/// 1. If the client entry exists, it will return the client directly to reuse the client by
/// the address.
/// 2. If the client entry does not exist, it will create a new client entry and insert it
/// into the clients map.
async fn client(&self, addr: &str) -> Result<DfdaemonUploadClient> {
let now = Instant::now();
// Cleanup the idle clients first to avoid the clients exceeding the capacity and the
// clients are idle for a long time.
self.cleanup_idle_client_entries().await;
let clients = self.clients.lock().await;
if let Some(entry) = clients.get(addr) {
debug!("reusing client: {}", addr);
*entry.actived_at.lock().unwrap() = now;
return Ok(entry.client.clone());
}
drop(clients);
// If there are many concurrent requests to create the client, it will create multiple
// clients for the same address. But it will reuse the same client by entry operation.
debug!("creating client: {}", addr);
let client =
DfdaemonUploadClient::new(self.config.clone(), format!("http://{}", addr), true)
.await?;
let mut clients = self.clients.lock().await;
let entry = clients
.entry(addr.to_string())
.or_insert(DfdaemonUploadClientEntry {
client: client.clone(),
active_requests: Arc::new(AtomicUsize::new(0)),
actived_at: Arc::new(std::sync::Mutex::new(now)),
});
// If it is created by other concurrent requests and reused client, need to update the
// last active time.
*entry.actived_at.lock().unwrap() = now;
Ok(entry.client.clone())
}
/// get_client_entry returns the client entry by the address.
async fn get_client_entry(&self, addr: &str) -> Option<DfdaemonUploadClientEntry> {
let clients = self.clients.lock().await;
clients.get(addr).cloned()
}
/// remove_client_entry removes the client entry if it is idle.
async fn remove_client_entry(&self, addr: &str) {
let mut clients = self.clients.lock().await;
if let Some(entry) = clients.get(addr) {
if entry.active_requests.load(Ordering::SeqCst) == 0 {
clients.remove(addr);
}
}
}
/// cleanup_idle_clients cleans up the idle clients, which are idle for a long time or have no
/// active requests.
async fn cleanup_idle_client_entries(&self) {
let now = Instant::now();
// Avoid hot cleanup for the clients.
let cleanup_at = self.cleanup_at.lock().await;
let interval = self.idle_timeout / 2;
if now.duration_since(*cleanup_at) < interval {
debug!("avoid hot cleanup");
return;
}
drop(cleanup_at);
let mut clients = self.clients.lock().await;
let exceeds_capacity = clients.len() > self.capacity;
clients.retain(|addr, entry| {
let active_requests = entry.active_requests.load(Ordering::SeqCst);
let is_active = active_requests > 0;
let actived_at = entry.actived_at.lock().unwrap();
let idel_duration = now.duration_since(*actived_at);
let is_recent = idel_duration <= self.idle_timeout;
// Retain the client if it is active or not exceeds the capacity and is recent.
let should_retain = is_active || (!exceeds_capacity && is_recent);
if !should_retain {
debug!(
"removing idle client: {}, exceeds_capacity: {}, idle_duration: {}s",
addr,
exceeds_capacity,
idel_duration.as_secs(),
);
}
should_retain
});
// Update the cleanup time.
*self.cleanup_at.lock().await = now;
}
}
@ -103,10 +273,15 @@ impl Downloader for GRPCDownloader {
host_id: &str,
task_id: &str,
) -> Result<(Vec<u8>, u64, String)> {
let dfdaemon_upload_client =
DfdaemonUploadClient::new(self.config.clone(), format!("http://{}", addr)).await?;
let client = self.client(addr).await?;
let response = dfdaemon_upload_client
let entry = self
.get_client_entry(addr)
.await
.ok_or(Error::UnexpectedResponse)?;
let request_guard = RequestGuard::new(entry.active_requests.clone());
let response = match client
.download_piece(
DownloadPieceRequest {
host_id: host_id.to_string(),
@ -115,7 +290,17 @@ impl Downloader for GRPCDownloader {
},
self.config.download.piece_timeout,
)
.await?;
.await
{
Ok(response) => response,
Err(err) => {
// If the request fails, it will drop the request guard and remove the client
// entry to avoid using the invalid client.
drop(request_guard);
self.remove_client_entry(addr).await;
return Err(err);
}
};
let Some(piece) = response.piece else {
return Err(Error::InvalidParameter);
@ -158,10 +343,15 @@ impl Downloader for GRPCDownloader {
host_id: &str,
task_id: &str,
) -> Result<(Vec<u8>, u64, String)> {
let dfdaemon_upload_client =
DfdaemonUploadClient::new(self.config.clone(), format!("http://{}", addr)).await?;
let client = self.client(addr).await?;
let response = dfdaemon_upload_client
let entry = self
.get_client_entry(addr)
.await
.ok_or(Error::UnexpectedResponse)?;
let request_guard = RequestGuard::new(entry.active_requests.clone());
let response = match client
.download_persistent_cache_piece(
DownloadPersistentCachePieceRequest {
host_id: host_id.to_string(),
@ -170,7 +360,17 @@ impl Downloader for GRPCDownloader {
},
self.config.download.piece_timeout,
)
.await?;
.await
{
Ok(response) => response,
Err(err) => {
// If the request fails, it will drop the request guard and remove the client
// entry to avoid using the invalid client.
drop(request_guard);
self.remove_client_entry(addr).await;
return Err(err);
}
};
let Some(piece) = response.piece else {
return Err(Error::InvalidParameter);

View File

@ -20,7 +20,8 @@ use crate::metrics::{
collect_backend_request_started_metrics,
};
use dragonfly_api::common::v2::{
Download, Hdfs, ObjectStorage, Peer, Piece, Task as CommonTask, TrafficType,
Download, Hdfs, ObjectStorage, Peer, Piece, SizeScope, Task as CommonTask, TaskType,
TrafficType,
};
use dragonfly_api::dfdaemon::{
self,
@ -48,6 +49,7 @@ use dragonfly_client_util::{
id_generator::IDGenerator,
};
use reqwest::header::HeaderMap;
use std::collections::HashMap;
use std::path::Path;
use std::sync::{
atomic::{AtomicBool, Ordering},
@ -62,7 +64,7 @@ use tokio::sync::{
use tokio::task::JoinSet;
use tokio_stream::{wrappers::ReceiverStream, StreamExt};
use tonic::{Request, Status};
use tracing::{debug, error, info, instrument, Instrument};
use tracing::{debug, error, info, instrument, warn, Instrument};
use super::*;
@ -90,7 +92,6 @@ pub struct Task {
/// Task implements the task manager.
impl Task {
/// new returns a new Task.
#[instrument(skip_all)]
pub fn new(
config: Arc<Config>,
id_generator: Arc<IDGenerator>,
@ -117,6 +118,7 @@ impl Task {
}
/// get gets the metadata of the task.
#[instrument(skip_all)]
pub fn get(&self, id: &str) -> ClientResult<Option<metadata::Task>> {
self.storage.get_task(id)
}
@ -128,8 +130,30 @@ impl Task {
id: &str,
request: Download,
) -> ClientResult<metadata::Task> {
let task = self.storage.download_task_started(id, None, None, None)?;
let task = self.storage.prepare_download_task_started(id).await?;
if task.content_length.is_some() && task.piece_length.is_some() {
// Attempt to create a hard link from the task file to the output path.
//
// Behavior based on force_hard_link setting:
// 1. force_hard_link is true:
// - Success: Continue processing
// - Failure: Return error immediately
// 2. force_hard_link is false:
// - Success: Continue processing
// - Failure: Fall back to copying the file instead
if let Some(output_path) = &request.output_path {
if let Err(err) = self
.storage
.hard_link_task(id, Path::new(output_path.as_str()))
.await
{
if request.force_hard_link {
return Err(err);
}
}
}
return Ok(task);
}
@ -217,17 +241,38 @@ impl Task {
// store the task.
if !task.is_finished() && !self.storage.has_enough_space(content_length)? {
return Err(Error::NoSpace(format!(
"not enough space to store the persistent cache task: content_length={}",
"not enough space to store the task: content_length={}",
content_length
)));
}
self.storage.download_task_started(
id,
Some(piece_length),
Some(content_length),
response.http_header,
)
let task = self
.storage
.download_task_started(id, piece_length, content_length, response.http_header)
.await;
// Attempt to create a hard link from the task file to the output path.
//
// Behavior based on force_hard_link setting:
// 1. force_hard_link is true:
// - Success: Continue processing
// - Failure: Return error immediately
// 2. force_hard_link is false:
// - Success: Continue processing
// - Failure: Fall back to copying the file instead
if let Some(output_path) = &request.output_path {
if let Err(err) = self
.storage
.hard_link_task(id, Path::new(output_path.as_str()))
.await
{
if request.force_hard_link {
return Err(err);
}
}
}
task
}
/// download_finished updates the metadata of the task when the task downloads finished.
@ -254,10 +299,15 @@ impl Task {
self.storage.prefetch_task_failed(id).await
}
/// hard_link_or_copy hard links or copies the task content to the destination.
/// is_same_dev_inode checks if the task is on the same device inode as the given path.
pub async fn is_same_dev_inode(&self, id: &str, to: &Path) -> ClientResult<bool> {
self.storage.is_same_dev_inode_as_task(id, to).await
}
//// copy_task copies the task content to the destination.
#[instrument(skip_all)]
pub async fn hard_link_or_copy(&self, task: &metadata::Task, to: &Path) -> ClientResult<()> {
self.storage.hard_link_or_copy_task(task, to).await
pub async fn copy_task(&self, id: &str, to: &Path) -> ClientResult<()> {
self.storage.copy_task(id, to).await
}
/// download downloads a task.
@ -338,6 +388,7 @@ impl Task {
range: request.range,
response_header: task.response_header.clone(),
pieces,
is_finished: task.is_finished(),
},
),
),
@ -547,7 +598,7 @@ impl Task {
})? {
// Check if the schedule count is exceeded.
schedule_count += 1;
if schedule_count >= self.config.scheduler.max_schedule_count {
if schedule_count > self.config.scheduler.max_schedule_count {
in_stream_tx
.send_timeout(
AnnouncePeerRequest {
@ -945,7 +996,8 @@ impl Task {
host: peer.host,
})
.collect(),
);
)
.await;
let mut piece_collector_rx = piece_collector.run().await;
// Initialize the interrupt. If download from parent failed with scheduler or download
@ -1077,13 +1129,13 @@ impl Task {
REQUEST_TIMEOUT,
)
.await
.inspect_err(|err| {
.unwrap_or_else(|err| {
error!(
"send DownloadPieceFinishedRequest for piece {} failed: {:?}",
piece_id, err
);
interrupt.store(true, Ordering::SeqCst);
})?;
});
// Send the download progress.
download_progress_tx
@ -1103,13 +1155,13 @@ impl Task {
REQUEST_TIMEOUT,
)
.await
.inspect_err(|err| {
.unwrap_or_else(|err| {
error!(
"send DownloadPieceFinishedResponse for piece {} failed: {:?}",
piece_id, err
);
interrupt.store(true, Ordering::SeqCst);
})?;
});
info!(
"finished piece {} from parent {:?}",
@ -1333,9 +1385,9 @@ impl Task {
},
REQUEST_TIMEOUT,
)
.await.inspect_err(|err| {
.await.unwrap_or_else(|err| {
error!("send DownloadPieceBackToSourceFinishedRequest for piece {} failed: {:?}", piece_id, err);
})?;
});
// Send the download progress.
download_progress_tx
@ -1355,12 +1407,12 @@ impl Task {
REQUEST_TIMEOUT,
)
.await
.inspect_err(|err| {
.unwrap_or_else(|err| {
error!(
"send DownloadPieceFinishedResponse for piece {} failed: {:?}",
piece_id, err
);
})?;
});
info!("finished piece {} from source", piece_id);
Ok(metadata)
@ -1525,6 +1577,11 @@ impl Task {
}
};
if !piece.is_finished() {
debug!("piece {} is not finished, skip it", piece_id);
continue;
}
// Fake the download from the local.
self.piece.download_from_local(task_id, piece.length);
info!("finished piece {} from local", piece_id,);
@ -1585,12 +1642,12 @@ impl Task {
REQUEST_TIMEOUT,
)
.await
.inspect_err(|err| {
.unwrap_or_else(|err| {
error!(
"send DownloadPieceFinishedResponse for piece {} failed: {:?}",
piece_id, err
);
})?;
});
// Store the finished piece.
finished_pieces.push(interested_piece.clone());
@ -1697,12 +1754,12 @@ impl Task {
REQUEST_TIMEOUT,
)
.await
.inspect_err(|err| {
.unwrap_or_else(|err| {
error!(
"send DownloadPieceFinishedResponse for piece {} failed: {:?}",
piece_id, err
);
})?;
});
info!("finished piece {} from source", piece_id);
Ok(metadata)
@ -1764,7 +1821,74 @@ impl Task {
/// stat_task returns the task metadata.
#[instrument(skip_all)]
pub async fn stat(&self, task_id: &str, host_id: &str) -> ClientResult<CommonTask> {
pub async fn stat(
&self,
task_id: &str,
host_id: &str,
local_only: bool,
) -> ClientResult<CommonTask> {
if local_only {
let Some(task_metadata) = self.storage.get_task(task_id).inspect_err(|err| {
error!("get task {} from local storage error: {:?}", task_id, err);
})?
else {
return Err(Error::TaskNotFound(task_id.to_owned()));
};
let piece_metadatas = self.piece.get_all(task_id).inspect_err(|err| {
error!(
"get pieces for task {} from local storage error: {:?}",
task_id, err
);
})?;
let pieces = piece_metadatas
.into_iter()
.filter(|piece| piece.is_finished())
.map(|piece| {
// The traffic_type indicates whether the first download was from the source or hit the remote peer cache.
// If the parent_id exists, the piece was downloaded from a remote peer. Otherwise, it was
// downloaded from the source.
let traffic_type = match piece.parent_id {
None => TrafficType::BackToSource,
Some(_) => TrafficType::RemotePeer,
};
Piece {
number: piece.number,
parent_id: piece.parent_id.clone(),
offset: piece.offset,
length: piece.length,
digest: piece.digest.clone(),
content: None,
traffic_type: Some(traffic_type as i32),
cost: piece.prost_cost(),
created_at: Some(prost_wkt_types::Timestamp::from(piece.created_at)),
}
})
.collect::<Vec<Piece>>();
return Ok(CommonTask {
id: task_metadata.id,
r#type: TaskType::Standard as i32,
url: String::new(),
digest: None,
tag: None,
application: None,
filtered_query_params: Vec::new(),
request_header: HashMap::new(),
content_length: task_metadata.content_length.unwrap_or(0),
piece_count: pieces.len() as u32,
size_scope: SizeScope::Normal as i32,
pieces,
state: String::new(),
peer_count: 0,
has_available_peer: false,
created_at: Some(prost_wkt_types::Timestamp::from(task_metadata.created_at)),
updated_at: Some(prost_wkt_types::Timestamp::from(task_metadata.updated_at)),
});
}
let task = self
.scheduler_client
.stat_task(StatTaskRequest {
@ -1810,3 +1934,54 @@ impl Task {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
use tempfile::tempdir;
// test_delete_task_not_found tests the Task.delete method when the task does not exist.
#[tokio::test]
async fn test_delete_task_not_found() {
// Create a temporary directory for testing.
let temp_dir = tempdir().unwrap();
let log_dir = temp_dir.path().join("log");
std::fs::create_dir_all(&log_dir).unwrap();
// Create configuration.
let config = Config::default();
let config = Arc::new(config);
// Create storage.
let storage = Storage::new(config.clone(), temp_dir.path(), log_dir)
.await
.unwrap();
let storage = Arc::new(storage);
// Test Storage.get_task and Error::TaskNotFound.
let task_id = "non-existent-task-id";
// Verify that non-existent tasks return None.
let task = storage.get_task(task_id).unwrap();
assert!(task.is_none(), "non-existent tasks should return None");
// Create a task and save it to storage.
let task_id = "test-task-id";
storage
.download_task_started(task_id, 1024, 4096, None)
.await
.unwrap();
// Verify that the task exists.
let task = storage.get_task(task_id).unwrap();
assert!(task.is_some(), "task should exist");
// Delete the task from storage.
storage.delete_task(task_id).await;
// Verify that the task has been deleted.
let task = storage.get_task(task_id).unwrap();
assert!(task.is_none(), "task should be deleted");
}
}

View File

@ -109,3 +109,100 @@ pub async fn shutdown_signal() {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use tokio::time::{sleep, Duration};
#[tokio::test]
async fn test_shutdown_trigger_and_recv() {
// Create a new shutdown instance.
let mut shutdown = Shutdown::new();
// Trigger the shutdown signal in a separate task.
let shutdown_clone = shutdown.clone();
tokio::spawn(async move {
// Small delay to ensure the receiver is waiting.
sleep(Duration::from_millis(10)).await;
shutdown_clone.trigger();
});
// Wait for the shutdown signal.
shutdown.recv().await;
// Verify that is_shutdown is set to true.
assert!(shutdown.is_shutdown());
}
#[tokio::test]
async fn test_shutdown_multiple_receivers() {
// Create a new shutdown instance.
let mut shutdown1 = Shutdown::new();
let mut shutdown2 = shutdown1.clone();
let mut shutdown3 = shutdown1.clone();
// Trigger the shutdown signal.
shutdown1.trigger();
// All receivers should receive the signal.
shutdown1.recv().await;
shutdown2.recv().await;
shutdown3.recv().await;
// Verify that all instances have is_shutdown set to true.
assert!(shutdown1.is_shutdown());
assert!(shutdown2.is_shutdown());
assert!(shutdown3.is_shutdown());
}
#[tokio::test]
async fn test_shutdown_clone_behavior() {
// Create a new shutdown instance.
let mut shutdown1 = Shutdown::new();
// Set is_shutdown to true.
shutdown1.trigger();
shutdown1.recv().await;
assert!(shutdown1.is_shutdown());
// Clone the instance.
let shutdown2 = shutdown1.clone();
// Verify that the clone has the same is_shutdown value.
assert_eq!(shutdown1.is_shutdown(), shutdown2.is_shutdown());
// Create a new instance before triggering.
let mut shutdown3 = Shutdown::new();
let mut shutdown4 = shutdown3.clone();
// Trigger after cloning.
shutdown3.trigger();
// Both should receive the signal.
shutdown3.recv().await;
shutdown4.recv().await;
assert!(shutdown3.is_shutdown());
assert!(shutdown4.is_shutdown());
}
#[tokio::test]
async fn test_shutdown_already_triggered() {
// Create a new shutdown instance.
let mut shutdown = Shutdown::new();
// Trigger and receive.
shutdown.trigger();
shutdown.recv().await;
assert!(shutdown.is_shutdown());
// Call recv again, should return immediately.
let start = std::time::Instant::now();
shutdown.recv().await;
let elapsed = start.elapsed();
// Verify that recv returned immediately (less than 5ms).
assert!(elapsed < Duration::from_millis(5));
}
}

View File

@ -67,7 +67,6 @@ pub struct Stats {
/// Stats implements the stats server.
impl Stats {
/// new creates a new Stats.
#[instrument(skip_all)]
pub fn new(
addr: SocketAddr,
shutdown: shutdown::Shutdown,
@ -81,7 +80,6 @@ impl Stats {
}
/// run starts the stats server.
#[instrument(skip_all)]
pub async fn run(&self) {
// Clone the shutdown channel.
let mut shutdown = self.shutdown.clone();
@ -110,7 +108,6 @@ impl Stats {
_ = shutdown.recv() => {
// Stats server shutting down with signals.
info!("stats server shutting down");
return
}
}
}

View File

@ -14,14 +14,19 @@
* limitations under the License.
*/
use opentelemetry::sdk::propagation::TraceContextPropagator;
use dragonfly_client_config::dfdaemon::Host;
use opentelemetry::{global, trace::TracerProvider, KeyValue};
use opentelemetry_otlp::{WithExportConfig, WithTonicConfig};
use opentelemetry_sdk::{propagation::TraceContextPropagator, Resource};
use rolling_file::*;
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;
use tonic::metadata::{MetadataKey, MetadataMap, MetadataValue};
use tracing::{info, Level};
use tracing_appender::non_blocking::WorkerGuard;
use tracing_flame::FlameLayer;
use tracing_log::LogTracer;
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::{
filter::LevelFilter,
fmt::{time::ChronoLocal, Layer},
@ -29,6 +34,9 @@ use tracing_subscriber::{
EnvFilter, Registry,
};
/// SPAN_EXPORTER_TIMEOUT is the timeout for the span exporter.
const SPAN_EXPORTER_TIMEOUT: Duration = Duration::from_secs(10);
/// init_tracing initializes the tracing system.
#[allow(clippy::too_many_arguments)]
pub fn init_tracing(
@ -36,9 +44,13 @@ pub fn init_tracing(
log_dir: PathBuf,
log_level: Level,
log_max_files: usize,
jaeger_addr: Option<String>,
flamegraph: bool,
verbose: bool,
otel_protocol: Option<String>,
otel_endpoint: Option<String>,
otel_path: Option<PathBuf>,
otel_headers: Option<reqwest::header::HeaderMap>,
host: Option<Host>,
is_seed_peer: bool,
console: bool,
) -> Vec<WorkerGuard> {
let mut guards = vec![];
@ -47,7 +59,7 @@ pub fn init_tracing(
guards.push(stdout_guard);
// Initialize stdout layer.
let stdout_filter = if verbose {
let stdout_filter = if console {
LevelFilter::DEBUG
} else {
LevelFilter::OFF
@ -90,41 +102,116 @@ pub fn init_tracing(
let env_filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::default().add_directive(log_level.into()));
// Setup flame layer.
let flame_layer = if flamegraph {
let (flame_layer, _guard) = FlameLayer::with_file(log_dir.join("tracing.folded"))
.expect("failed to create flame layer");
Some(flame_layer)
// Enable console subscriber layer for tracing spawn tasks on `127.0.0.1:6669` when log level is TRACE.
let console_subscriber_layer = if log_level == Level::TRACE {
Some(console_subscriber::spawn())
} else {
None
};
let subscriber = Registry::default()
.with(env_filter)
.with(console_subscriber_layer)
.with(file_logging_layer)
.with(stdout_logging_layer)
.with(flame_layer);
.with(stdout_logging_layer);
// Setup jaeger layer.
if let Some(jaeger_addr) = jaeger_addr {
opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());
let tracer = opentelemetry_jaeger::new_agent_pipeline()
.with_service_name(name)
.with_endpoint(jaeger_addr)
.install_batch(opentelemetry::runtime::Tokio)
.expect("install");
let jaeger_layer = tracing_opentelemetry::layer().with_tracer(tracer);
let subscriber = subscriber.with(jaeger_layer);
// If OTLP protocol and endpoint are provided, set up OpenTelemetry tracing.
if let (Some(protocol), Some(endpoint)) = (otel_protocol, otel_endpoint) {
let otlp_exporter = match protocol.as_str() {
"grpc" => {
let mut metadata = MetadataMap::new();
if let Some(headers) = otel_headers {
for (key, value) in headers.iter() {
metadata.insert(
MetadataKey::from_str(key.as_str())
.expect("failed to create metadata key"),
MetadataValue::from_str(value.to_str().unwrap())
.expect("failed to create metadata value"),
);
}
}
tracing::subscriber::set_global_default(subscriber)
.expect("failed to set global subscriber");
let endpoint_url = url::Url::parse(&format!("http://{}", endpoint))
.expect("failed to parse OTLP endpoint URL");
opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.with_endpoint(endpoint_url)
.with_timeout(SPAN_EXPORTER_TIMEOUT)
.with_metadata(metadata)
.build()
.expect("failed to create OTLP exporter")
}
"http" | "https" => {
let mut endpoint_url = url::Url::parse(&format!("{}://{}", protocol, endpoint))
.expect("failed to parse OTLP endpoint URL");
if let Some(path) = otel_path {
endpoint_url = endpoint_url
.join(path.to_str().unwrap())
.expect("failed to join OTLP endpoint path");
}
opentelemetry_otlp::SpanExporter::builder()
.with_http()
.with_endpoint(endpoint_url.as_str())
.with_protocol(opentelemetry_otlp::Protocol::HttpJson)
.with_timeout(SPAN_EXPORTER_TIMEOUT)
.build()
.expect("failed to create OTLP exporter")
}
_ => {
panic!("unsupported OTLP protocol: {}", protocol);
}
};
let host = host.unwrap();
let provider = opentelemetry_sdk::trace::SdkTracerProvider::builder()
.with_batch_exporter(otlp_exporter)
.with_resource(
Resource::builder()
.with_service_name(format!("{}-{}", name, host.ip.unwrap()))
.with_schema_url(
[
KeyValue::new(
opentelemetry_semantic_conventions::attribute::SERVICE_NAMESPACE,
"dragonfly",
),
KeyValue::new(
opentelemetry_semantic_conventions::attribute::HOST_NAME,
host.hostname,
),
KeyValue::new(
opentelemetry_semantic_conventions::attribute::HOST_IP,
host.ip.unwrap().to_string(),
),
],
opentelemetry_semantic_conventions::SCHEMA_URL,
)
.with_attribute(opentelemetry::KeyValue::new(
"host.idc",
host.idc.unwrap_or_default(),
))
.with_attribute(opentelemetry::KeyValue::new(
"host.location",
host.location.unwrap_or_default(),
))
.with_attribute(opentelemetry::KeyValue::new("host.seed_peer", is_seed_peer))
.build(),
)
.build();
let tracer = provider.tracer(name.to_string());
global::set_tracer_provider(provider.clone());
global::set_text_map_propagator(TraceContextPropagator::new());
let jaeger_layer = OpenTelemetryLayer::new(tracer);
subscriber.with(jaeger_layer).init();
} else {
tracing::subscriber::set_global_default(subscriber)
.expect("failed to set global subscriber");
subscriber.init();
}
LogTracer::init().expect("failed to init LogTracer");
std::panic::set_hook(Box::new(tracing_panic::panic_hook));
info!(
"tracing initialized directory: {}, level: {}",
log_dir.as_path().display(),

View File

@ -1,2 +1,2 @@
[toolchain]
channel = "1.82.0"
channel = "1.85.0"