Skip to content

feat(sdk): add dfcache Python SDK - #2148

Open
hock1024always wants to merge 4 commits into
dragonflyoss:mainfrom
hock1024always:feat/dfcache-python-sdk
Open

hock1024always wants to merge 4 commits into
dragonflyoss:mainfrom
hock1024always:feat/dfcache-python-sdk

Conversation

@hock1024always

Copy link
Copy Markdown

Description

Implement the dfcache Python SDK for dragonflyoss/dragonfly#4422, targeting AI developers that share models, datasets and checkpoints across nodes through the Dragonfly P2P persistent cache.

The SDK wraps the dfdaemon download/upload gRPC services over the unix domain socket (default /var/run/dragonfly/dfdaemon.sock) using the existing dragonfly-api PyPI bindings (generated from dragonflyoss/api), mirroring the dfcache CLI — no Rust code is required:

SDK method CLI / RPC
client.import_file(path, ...) dfcache import → DfdaemonDownload/UploadPersistentCacheTask
client.export_file(task_id, output=..., ...) dfcache export → DfdaemonDownload/DownloadPersistentCacheTask
client.stat(task_id) dfcache stat → DfdaemonDownload/StatPersistentCacheTask
client.delete(task_id) exposed by the proto, missing from the CLI → DfdaemonUpload/DeletePersistentCacheTask

Design decisions

  • Sync-first API — the typical AI/data-eng usage is sequential; an AsyncDfcacheClient can follow later. The client is thread-safe (shared gRPC channel).
  • Client-side validation mirrors the CLI rules (ttl 5m–7d, replica count 1–5, piece length ≥ 4MiB, <algorithm>:<hash> digest format) so users get immediate actionable errors.
  • Typed exceptions — gRPC status codes surface as DfcacheTaskNotFoundError, DfcachePermissionDeniedError (blocklist), DfcacheConnectionError, etc., all deriving from DfcacheError.
  • Failed exports remove the partial output file, mirroring the CLI's remove-on-error behavior.
  • Dataclasses decouple the public API from the generated protobufs.

Layout

sdks/python/
├── dragonfly_cache/     # client, models, validators, exceptions
├── examples/            # AI model checkpoint sharing walkthrough
├── tests/               # 39 tests against an in-process fake dfdaemon
│                        # over a real unix domain socket (no deployment needed)
└── pyproject.toml       # package name: dragonfly-cache (PyPI TBD, see issue)

CI: a new python-sdk.yml workflow runs the suite on Python 3.9–3.12 with codecov (flags: python-sdk).

Questions for maintainers (also posted in the issue)

  1. Repo placement — the issue lives in dragonflyoss/dragonfly but dfdaemon/the CLI live here; is sdks/python/ in this repo the right home, or would a separate repo be preferred?
  2. PyPI package name — dragonfly-cache? (avoiding confusion with the Redis-compatible dragonflydb)
  3. Publishing workflow — should releases be cut from this repo's CI?

Related Issue

Fixes #4422 (dragonflyoss/dragonfly)

🤖 Generated with Claude Code

Implement the dfcache Python SDK for issue dragonflyoss/dragonfly#4422,
targeting AI developers that share models, datasets and checkpoints
across nodes through the Dragonfly P2P persistent cache.

The SDK wraps the dfdaemon download/upload gRPC services over the unix
domain socket (default /var/run/dragonfly/dfdaemon.sock) using the
existing dragonfly-api PyPI bindings, mirroring the dfcache CLI:

* import_file  - dfcache import (UploadPersistentCacheTask)
* export_file  - dfcache export (DownloadPersistentCacheTask)
* stat         - dfcache stat (StatPersistentCacheTask)
* delete       - exposed by the proto, missing from the CLI
                 (DeletePersistentCacheTask on the upload service)

Client-side validation mirrors the CLI rules (ttl 5m-7d, replica count
1-5, piece length >= 4MiB, digest format). gRPC errors surface as typed
exceptions; failed exports remove the partial output file. The test
suite runs against an in-process fake dfdaemon over a real unix socket.

Signed-off-by: hock1024always <hock1024always@users.noreply.github.com>
@codecov

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 321 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.16%. Comparing base (ca04fb0) to head (06ade1c).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
sdks/python/dragonfly_cache/client.py 0.00% 115 Missing ⚠️
sdks/python/dragonfly_cache/aclient.py 0.00% 94 Missing ⚠️
sdks/python/dragonfly_cache/validators.py 0.00% 44 Missing ⚠️
sdks/python/dragonfly_cache/models.py 0.00% 37 Missing ⚠️
sdks/python/dragonfly_cache/exceptions.py 0.00% 25 Missing ⚠️
sdks/python/dragonfly_cache/__init__.py 0.00% 6 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2148      +/-   ##
==========================================
- Coverage   65.75%   65.16%   -0.59%     
==========================================
  Files         102      108       +6     
  Lines       31270    31745     +475     
==========================================
+ Hits        20561    20688     +127     
- Misses      10709    11057     +348     
Flag Coverage Δ
python-sdk 0.00% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sdks/python/dragonfly_cache/__init__.py 0.00% <0.00%> (ø)
sdks/python/dragonfly_cache/exceptions.py 0.00% <0.00%> (ø)
sdks/python/dragonfly_cache/models.py 0.00% <0.00%> (ø)
sdks/python/dragonfly_cache/validators.py 0.00% <0.00%> (ø)
sdks/python/dragonfly_cache/aclient.py 0.00% <0.00%> (ø)
sdks/python/dragonfly_cache/client.py 0.00% <0.00%> (ø)

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

hock1024always and others added 2 commits November 9, 2026 14:18
Python 3.9 is not available for x64 on ubuntu-latest (26.04), pin the
runner to ubuntu-24.04 so the full 3.9-3.12 matrix runs.

Signed-off-by: hock1024always <hoc***@users.noreply.github.com>
…uds authority

Two issues found while testing the SDK against a locally built dfdaemon:

1. DeletePersistentCacheTask is served by the dfdaemon upload gRPC
   server, which listens on TCP (default port 4000); the download unix
   domain socket only hosts the download service. Calling delete through
   the download socket returned UNIMPLEMENTED against the real daemon.
   The client now opens a second channel to the upload service, with an
   upload_endpoint parameter defaulting to 127.0.0.1:4000. The test
   fake mirrors the real topology: download over the unix socket,
   upload over TCP.

2. grpc-python over a unix domain socket derives the http/2 :authority
   header from the percent-encoded socket path (e.g.
   tmp%2Fdragonfly%2Fdfdaemon.sock). Server stacks using h2 >= 0.4
   reject it as a malformed authority and reset the stream, so every
   RPC died with RST_STREAM PROTOCOL_ERROR. The channel is now created
   with an explicit grpc.default_authority option.

Validated end to end against a locally built dfdaemon backed by a fake
scheduler: import, stat (ttl/replicas), digest-verified export, wrong
digest failure with output cleanup, delete and post-delete export
failure.

Also adds a --reference integrity flag to the example.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: hock1024always <hock1024always@users.noreply.github.com>
@hock1024always
hock1024always force-pushed the feat/dfcache-python-sdk branch from 30540fc to 94431dc Compare November 9, 2026 08:43
AsyncDfcacheClient mirrors the sync DfcacheClient surface with async
def methods on grpc.aio channels, for callers already running an event
loop (training jobs, notebooks, FastAPI handlers). The download
response stream is consumed with async for; the preferred-local-ip
probe runs off the loop thread via run_in_executor.

Co-Authored-By: Claude <nore***@anthropic.com>
Signed-off-by: hock1024always <hoc***@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant