# tikv/pd architecture

> Architecture diagram of the GitHub repository tikv/pd, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tikv/pd
- Repository: https://github.com/tikv/pd

## Overview

##### Main Components Identification

The system should be divided into these major components:

a. Core Services:
- TSO (Timestamp Oracle) Service
- Scheduling Service
- Resource Manager Service
- Meta Storage Service
- Key Space Management Service

b. Infrastructure Layer:
- Embedded etcd for persistence and fault tolerance
- gRPC/HTTP API servers
- Client SDK

##### Key Architectural Elements

a. Server Components:
- API Layer (server/api/*)
- Cluster Management (server/cluster/*)
- Configuration Management (server/config/*)
- Storage Layer (pkg/storage/*)

b. Client Components:
- Client SDK (client/*)
- HTTP Client (client/http/*)
- Various Service Clients (client/clients/*)

##### External Interface Layer
- HTTP/gRPC APIs
- Client SDKs
- Dashboard UI

##### Core Services Layer
- TSO Service (Timestamp Oracle)
- Scheduling Service
- Resource Management
- Meta Storage
- Key Space Management

##### Internal Components Layer
- Cluster Management
- Store Management
- Region Management
- Statistics Collection

##### Storage Layer
- Embedded etcd
- Local Storage

##### Connection Flows

- Client request flows
- Internal service communication
- Data persistence paths
- Scheduling decision flows

##### Detailed Component Relationships

Interactions between:
- TSO service and clients
- Scheduler and TiKV nodes
- Resource manager and consumers
- Meta storage and other services

##### Metrics and Monitoring

Highlight:
- Prometheus metrics endpoints
- Grafana dashboard connections
- Alert manager integration

## Diagram (Mermaid)

```mermaid
graph TB
    %% External Interface Layer
    subgraph Interface
        API[HTTP/gRPC API]
        SDK[Client SDK]
        DASH[Dashboard UI]
    end

    %% Core Services Layer
    subgraph CoreServices
        TSO[TSO Service]:::core
        SCHED[Scheduling Service]:::core
        RM[Resource Manager]:::core
        META[Meta Storage]:::core
        KSM[Key Space Management]:::core
    end

    %% Internal Components Layer
    subgraph Internal
        CM[Cluster Management]:::internal
        SM[Store Management]:::internal
        RM_INT[Region Management]:::internal
        STATS[Statistics Collection]:::internal
    end

    %% Storage Layer
    subgraph Storage
        ETCD[Embedded etcd]:::storage
        LOCAL[Local Storage]:::storage
    end

    %% Monitoring Layer
    subgraph Monitoring
        PROM[Prometheus]:::monitoring
        GRAF[Grafana]:::monitoring
        ALERT[Alert Manager]:::monitoring
    end

    %% Connections
    API --> TSO
    API --> SCHED
    API --> RM
    API --> META
    API --> KSM
    
    SDK --> API
    DASH --> API

    TSO --> CM
    SCHED --> CM
    RM --> CM
    META --> CM
    KSM --> CM

    CM --> SM
    CM --> RM_INT
    CM --> STATS

    SM --> ETCD
    RM_INT --> ETCD
    STATS --> LOCAL

    CM --> PROM
    PROM --> GRAF
    PROM --> ALERT

    %% Styles
    classDef core fill:#90EE90
    classDef internal fill:#ADD8E6
    classDef storage fill:#FFD700
    classDef monitoring fill:#FFA500

    %% Click Events
    click TSO "https://github.com/tikv/pd/tree/master/pkg/mcs/tso/server/"
    click SCHED "https://github.com/tikv/pd/tree/master/pkg/mcs/scheduling/server/"
    click RM "https://github.com/tikv/pd/tree/master/pkg/mcs/resourcemanager/server/"
    click META "https://github.com/tikv/pd/tree/master/pkg/mcs/metastorage/server/"
    click KSM "https://github.com/tikv/pd/tree/master/pkg/keyspace/"
    click SDK "https://github.com/tikv/pd/tree/master/client/"
    click API "https://github.com/tikv/pd/tree/master/server/api/"
    click LOCAL "https://github.com/tikv/pd/tree/master/pkg/storage/"
    click ETCD "https://github.com/tikv/pd/blob/master/pkg/storage/kv/etcd_kv.go"
    click API "https://github.com/tikv/pd/blob/master/server/grpc_service.go"
    click CM "https://github.com/tikv/pd/tree/master/pkg/cluster/"
    click CONFIG "https://github.com/tikv/pd/tree/master/server/config/"
    click SM "https://github.com/tikv/pd/blob/master/pkg/core/store.go"
    click RM_INT "https://github.com/tikv/pd/blob/master/pkg/core/region.go"
    click STATS "https://github.com/tikv/pd/tree/master/pkg/statistics/"
    click PROM "https://github.com/tikv/pd/tree/master/metrics/"

    %% Legend
    subgraph Legend
        L1[Core Services]:::core
        L2[Internal Components]:::internal
        L3[Storage]:::storage
        L4[Monitoring]:::monitoring
    end
```
