# skyapm/r3 architecture

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

- Interactive diagram: https://gitdiagram.com/skyapm/r3
- Repository: https://github.com/skyapm/r3

## Overview

##### Project Type & Purpose
- R3 is a backend-focused microservice that provides REST endpoint pattern recognition via a gRPC/HTTP API.
- It bundles a core algorithm (URIDrain) under `models/uri_drain`, a simple multiprocess server in `servers/simple`, and demo/test clients.
- It supports containerized deployment (Dockerfile, demo/docker-compose) and local Python execution.

##### File Structure
Top-level components:
- models/uri_drain: the core ML/log-clustering algorithm and its config, masking, template miner, persistence, and post-processing modules.
- servers/simple: a producer-consumer multiprocessing server exposing the gRPC/HTTP endpoint, worker processes, results manager, and configuration (`uri_drain.ini`).
- servers/protos: gRPC service definition (`ai_http_uri_recognition.proto`).
- demo: sample URIs, Python scripts (`demo_big_file_uri.py`, `demo_gradio.py`), and a Docker Compose for stress testing.
- test & servers/tests: E2E and unit tests, mock clients, expected outputs.
- Tools & config: `tools/grpc_gen.py` (auto-gen from proto), Dockerfile, CI workflows, Makefile, pyproject.toml & Poetry.

##### Key Architectural Insights from README
- The service exposes a gRPC/HTTP interface (port 17128) to classify URIs into patterns.
- A simple multiprocess design: a producer feeds incoming URIs into a queue; multiple workers consume, apply URIDrain, and send results to a ResultsManager.
- Persistence is pluggable (handlers in `persistence_handler.py`) and can log/record patterns.

##### Main Components & Their Interactions
a. Clients & Entry Points
- gRPC/HTTP client (in-production SkyWalking pipeline or demo scripts).
- E2E test client (test/e2e/client.py).
b. API Layer (servers/simple/run.py → server.py)
- Listens on port 17128, decodes incoming requests via generated gRPC stubs.
- Publishes URI strings into a multiprocessing Queue.
c. Worker Pool (servers/simple/worker.py)
- Each worker pulls URIs from the Queue, invokes `models/uri_drain.uri_drain.URI_Drain` to detect patterns, and forwards results to ResultsManager.
d. Core Algorithm (models/uri_drain/*)
- Word splitting, masking, template mining, post-processing (NLTK), and persistence.
- Configurable via `template_miner_config.py` and `uri_drain.ini`.
e. Results Manager & Persistence (servers/simple/results_manager.py & models/uri_drain/persistence_handler.py)
- Aggregates results, writes to storage or in-memory structures.
f. Demo & Testing
- Gradio demo (`demo/demo_gradio.py`) for interactive algorithm exploration.
- Bulk URI processing script (`demo/demo_big_file_uri.py`).
- Automated E2E Docker Compose harness.

##### Important Architectural Patterns & Principles
- Producer-consumer multiprocessing for concurrency and isolation.
- Layered separation: API layer, processing layer, persistence layer.
- Pluggable modules: algorithm steps can be extended via `add_ons/` and `template_miner_config.py`.
- Container-first deployment: Dockerfile + CI pipeline for image publishing.
- Strict interface definition via Protocol Buffers.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients Layer
    subgraph "Clients"
        direction TB
        ClientHTTP["gRPC/HTTP Client"]:::client
        E2EClient["E2E Test Client"]:::client
    end

    %% API Layer
    subgraph "API Layer"
        direction TB
        APIEntrance["API Server Launcher"]:::api
        ServerImpl["gRPC/HTTP Server"]:::api
        ProtoDef["Protocol Buffers Definition"]:::api
        ToolsGen["gRPC Stub Generator"]:::api
    end

    %% Processing Layers
    Queue["Multiprocessing Queue"]:::worker
    subgraph "Worker Pool"
        direction TB
        Worker["Worker Process"]:::worker
        ResultsManager["Results Manager"]:::worker
    end

    subgraph "URIDrain Pipeline"
        direction TB
        URIDrainEntry["URIDrain Entry Point"]:::algorithm
        WordSplitter["Word Splitting Module"]:::algorithm
        Masking["Masking Module"]:::algorithm
        TemplateMiner["Template Miner"]:::algorithm
        TemplateConfig["Template Config"]:::config
        PostProcessor["Post-Processing Add-On"]:::algorithm
        PersistenceHandler["Persistence Handler"]:::algorithm
        UtilsLogger["Logging Utility"]:::utility
        UtilsProfiler["Profiling Utility"]:::utility
    end

    %% Demo & Bulk
    subgraph "Demo & Testing"
        direction TB
        DemoUI["Gradio Demo UI"]:::demo
        BulkScript["Bulk URI Processing Script"]:::demo
    end

    %% Persistence Storage
    Storage["Persistence Store"]:::storage

    %% Infrastructure
    subgraph "Infrastructure"
        direction TB
        Docker["Docker & Compose"]:::infra
        CI["CI/CD Pipelines"]:::infra
    end

    %% Connections
    ClientHTTP -->|"gRPC request"| APIEntrance
    E2EClient -->|"gRPC request"| APIEntrance
    APIEntrance -->|"start"| ServerImpl
    ProtoDef -->|"defines service"| ToolsGen
    ToolsGen -->|"generates stubs"| ServerImpl

    ServerImpl -->|"enqueues URI"| Queue
    Queue -->|"dequeues URI"| Worker
    Worker -->|"invoke"| URIDrainEntry
    URIDrainEntry -->|"split words"| WordSplitter
    WordSplitter -->|"mask tokens"| Masking
    Masking -->|"mine templates"| TemplateMiner
    TemplateMiner -->|"config loaded"| TemplateConfig
    TemplateMiner -->|"post-process"| PostProcessor
    PostProcessor -->|"handle persistence"| PersistenceHandler
    PersistenceHandler -->|"send results"| ResultsManager
    ResultsManager -->|"store patterns"| Storage

    DemoUI -->|"invoke pipeline"| URIDrainEntry
    BulkScript -->|"invoke pipeline"| URIDrainEntry

    CI -->|"build image"| Docker
    Docker -->|"deploy service"| APIEntrance

    %% Click Events
    click URIDrainEntry "https://github.com/skyapm/r3/blob/main/models/uri_drain/uri_drain.py"
    click WordSplitter "https://github.com/skyapm/r3/blob/main/models/uri_drain/word_splitter.py"
    click Masking "https://github.com/skyapm/r3/blob/main/models/uri_drain/masking.py"
    click TemplateMiner "https://github.com/skyapm/r3/blob/main/models/uri_drain/template_miner.py"
    click TemplateConfig "https://github.com/skyapm/r3/blob/main/models/uri_drain/template_miner_config.py"
    click PostProcessor "https://github.com/skyapm/r3/blob/main/models/uri_drain/add_ons/post_processor_nltk.py"
    click PersistenceHandler "https://github.com/skyapm/r3/blob/main/models/uri_drain/persistence_handler.py"
    click UtilsLogger "https://github.com/skyapm/r3/blob/main/models/utils/logger.py"
    click UtilsProfiler "https://github.com/skyapm/r3/blob/main/models/utils/simple_profiler.py"
    click APIEntrance "https://github.com/skyapm/r3/blob/main/servers/simple/run.py"
    click ServerImpl "https://github.com/skyapm/r3/blob/main/servers/simple/server.py"
    click ProtoDef "https://github.com/skyapm/r3/blob/main/servers/protos/ai_http_uri_recognition.proto"
    click ToolsGen "https://github.com/skyapm/r3/blob/main/tools/grpc_gen.py"
    click DemoUI "https://github.com/skyapm/r3/blob/main/demo/demo_gradio.py"
    click BulkScript "https://github.com/skyapm/r3/blob/main/demo/demo_big_file_uri.py"
    click Storage "https://github.com/skyapm/r3/blob/main/models/uri_drain/persistence_handler.py"
    click CI "https://github.com/skyapm/r3/blob/main/.github/workflows/CI.yaml"
    click Docker "https://github.com/skyapm/r3/blob/main/demo/crazy_endpoint_generator/docker-compose.yaml"

    %% Styles
    classDef client fill:#B3E5FC,stroke:#0288D1,color:#01579B;
    classDef api fill:#BBDEFB,stroke:#1976D2,color:#0D47A1;
    classDef worker fill:#C8E6C9,stroke:#388E3C,color:#1B5E20;
    classDef algorithm fill:#DCEDC8,stroke:#689F38,color:#33691E;
    classDef storage fill:#FFE0B2,stroke:#F57C00,color:#E65100;
    classDef config fill:#FFF9C4,stroke:#FBC02D,color:#F57F17;
    classDef utility fill:#E1BEE7,stroke:#8E24AA,color:#4A148C;
    classDef demo fill:#FFE0E0,stroke:#E53935,color:#B71C1C;
    classDef infra fill:#ECEFF1,stroke:#546E7A,color:#263238;
```
