# jwcjlu/dragonfly2 architecture

> Architecture diagram of the GitHub repository jwcjlu/dragonfly2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jwcjlu/dragonfly2
- Repository: https://github.com/jwcjlu/dragonfly2

## Overview

##### Main Components
- Manager:
- Responsible for cluster management, dynamic configuration, and RBAC.
- Includes a front-end console for visual oversight.
- Relevant directories: “manager”, “manager/handlers”, “manager/rpcserver”, “manager/config”, “manager/database”, and “manager/router”.
- Scheduler:
- Handles selecting the optimal parent peer for content distribution,
including evaluating network topology and resource availability.
- Relevant directories: “scheduler”, “scheduler/scheduling”, “scheduler/resource”, “scheduler/rpcserver”, “scheduler/config”.
- Seed Peer:
- Acts as the primary “back-to-source” download peer in the P2P cluster.
- This is integrated into the dfdaemon when operated in Seed Peer mode.
- Peer:
- Represents the client side running the “dfdaemon” and “dfget” command-line tool.
- Includes functionalities for file download and communication with other peers.
- Relevant directories: “client” (with various subdirectories such as “daemon”, “dfget”, “dfstore”), “cmd/dfget”, “cmd/dfcache”, “cmd/dfstore”.
- Additional components:
- Trainer: Found under “trainer” (used for training, perhaps around performance or data handling).
- API layer: The “api” directory indicates an HTTP API layer (with Swagger files available) for external communication.
- Plugin support and core libraries: The “pkg” and “internal” directories contain the underlying libraries, plugins, and common utilities.

##### Out the Relationships and Interactions
- The Manager interacts with the Scheduler via APIs (or RPC interfaces) to maintain cluster state, schedule download tasks, and handle configuration changes.
- Peers (running dfdaemon and dfget) communicate with each other for P2P file distribution. They can request downloads via the Scheduler and may fall back on Seed Peer mode if needed.
- The API layer (in “api”) serves external or administrative calls to the Manager.
- There’s communication between the Manager, Scheduler, and even Trainer components through established RPC protocols. Look for directories like “pkg/rpc” and “manager/rpcserver” which encapsulate these interactions.
- Internal libraries (under “internal” and “pkg”) support fundamental patterns such as caching, logging, networking, and security which are used across all main components.

##### Architectural Patterns and Technologies
- Distributed P2P architecture: Emphasize the use of peer-to-peer interactions for file distribution.
- Client-Server and RPC-based microservices: Manager, Scheduler, and Peer components interact over defined APIs/RPC servers (multiple “client” folders under “pkg/rpc”).
- Containerized deployment: Notice the “build/images”, “deploy/docker-compose”, and “deploy/helm-charts” directories which show container/Docker and Kubernetes deployment support.
- CI/CD Pipeline: The .github/workflows directory and other build scripts reflect integration and release automation.
- Security aspects such as RBAC (in Manager), TLS, and audit reports (linked in the README).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Control Plane
    subgraph "Control Plane"
        M["Manager"]:::manager
        S["Scheduler"]:::scheduler
        T["Trainer"]:::trainer
        A["API Layer"]:::api
        L["Core Libraries"]:::shared
    end

    %% Data Plane
    subgraph "Data Plane"
        P["Peer (dfdaemon/dfget)"]:::peer
        SP["Seed Peer"]:::peer
    end

    %% Relationships within Control Plane
    M <-->|"RPC/API"| S
    A -->|"HTTP"| M
    T -->|"RPC"| M
    M -.-> L
    S -.-> L
    T -.-> L
    P -.-> L

    %% Data Plane interactions
    P -->|"DownloadRequest"| S
    S -->|"Fallback"| SP
    SP -->|"DataStream"| P

    %% Click Events for Manager
    click M "https://github.com/jwcjlu/dragonfly2/tree/main/manager"
    click M "https://github.com/jwcjlu/dragonfly2/tree/main/manager/handlers"
    click M "https://github.com/jwcjlu/dragonfly2/tree/main/manager/rpcserver"
    click M "https://github.com/jwcjlu/dragonfly2/tree/main/manager/config"
    click M "https://github.com/jwcjlu/dragonfly2/tree/main/manager/database"
    click M "https://github.com/jwcjlu/dragonfly2/tree/main/manager/router"
    click M "https://github.com/jwcjlu/dragonfly2/tree/main/cmd/manager"

    %% Click Events for Scheduler
    click S "https://github.com/jwcjlu/dragonfly2/tree/main/scheduler"
    click S "https://github.com/jwcjlu/dragonfly2/tree/main/scheduler/scheduling"
    click S "https://github.com/jwcjlu/dragonfly2/tree/main/scheduler/resource"
    click S "https://github.com/jwcjlu/dragonfly2/tree/main/scheduler/rpcserver"
    click S "https://github.com/jwcjlu/dragonfly2/tree/main/scheduler/config"
    click S "https://github.com/jwcjlu/dragonfly2/tree/main/cmd/scheduler"

    %% Click Events for Seed Peer
    click SP "https://github.com/jwcjlu/dragonfly2/tree/main/client/daemon/pex"
    click SP "https://github.com/jwcjlu/dragonfly2/blob/main/deploy/docker-compose/template/seed-peer.template.yaml"

    %% Click Events for Peer
    click P "https://github.com/jwcjlu/dragonfly2/tree/main/client"
    click P "https://github.com/jwcjlu/dragonfly2/tree/main/cmd/dfget"
    click P "https://github.com/jwcjlu/dragonfly2/tree/main/cmd/dfcache"
    click P "https://github.com/jwcjlu/dragonfly2/tree/main/cmd/dfstore"

    %% Click Events for Trainer
    click T "https://github.com/jwcjlu/dragonfly2/tree/main/trainer"
    click T "https://github.com/jwcjlu/dragonfly2/tree/main/trainer/config"
    click T "https://github.com/jwcjlu/dragonfly2/tree/main/trainer/metrics"
    click T "https://github.com/jwcjlu/dragonfly2/tree/main/trainer/rpcserver"
    click T "https://github.com/jwcjlu/dragonfly2/tree/main/trainer/service"
    click T "https://github.com/jwcjlu/dragonfly2/tree/main/trainer/storage"
    click T "https://github.com/jwcjlu/dragonfly2/tree/main/trainer/training"
    click T "https://github.com/jwcjlu/dragonfly2/tree/main/cmd/trainer"

    %% Click Event for API Layer
    click A "https://github.com/jwcjlu/dragonfly2/tree/main/api"

    %% Click Events for Core Libraries
    click L "https://github.com/jwcjlu/dragonfly2/tree/main/pkg"
    click L "https://github.com/jwcjlu/dragonfly2/tree/main/internal"

    %% Styles
    classDef manager fill:#f9e79f,stroke:#000,stroke-width:2px;
    classDef scheduler fill:#aed6f1,stroke:#000,stroke-width:2px;
    classDef peer fill:#a9dfbf,stroke:#000,stroke-width:2px;
    classDef trainer fill:#f5cba7,stroke:#000,stroke-width:2px;
    classDef api fill:#d7bde2,stroke:#000,stroke-width:2px;
    classDef shared fill:#d5f5e3,stroke:#000,stroke-width:2px;
```
