diff --git a/doc/architecture/VISUAL-GUIDE.md b/doc/architecture/VISUAL-GUIDE.md new file mode 100644 index 000000000..4fe6d9ff7 --- /dev/null +++ b/doc/architecture/VISUAL-GUIDE.md @@ -0,0 +1,1226 @@ +# ProxySQL Visual Architecture Guide + +## Table of Contents + +1. [System Overview](#system-overview) +2. [Code Layout Trees](#code-layout-trees) +3. [Class Hierarchy Diagrams](#class-hierarchy-diagrams) +4. [Database Schema ERD](#database-schema-erd) +5. [Data Flow Architecture](#data-flow-architecture) +6. [Protocol Sequence Diagrams](#protocol-sequence-diagrams) +7. [Thread Architecture](#thread-architecture) +8. [Connection Pooling Architecture](#connection-pooling-architecture) +9. [Query Processing Pipeline](#query-processing-pipeline) +10. [Deployment Topologies](#deployment-topologies) + +## System Overview + +### High-Level Architecture + +```mermaid +graph TB + subgraph "Client Applications" + MA[MySQL Apps
Port 6033] + PA[PostgreSQL Apps
Port 6033] + AA[Admin Tools
Port 6032] + RA[REST Clients
Port 6080] + end + + subgraph "ProxySQL Core Engine" + subgraph "Protocol Handlers" + MPH[MySQL Protocol
Handler] + PPH[PgSQL Protocol
Handler] + APH[Admin Protocol
Handler] + end + + subgraph "Core Services" + QP[Query Processor
& Router] + QC[Query Cache] + CP[Connection Pool] + AUTH[Authentication
Manager] + MON[Monitor Service] + end + + subgraph "Data Layer" + CONF[(Configuration
SQLite3)] + STATS[(Statistics
SQLite3)] + MONDB[(Monitor
SQLite3)] + end + end + + subgraph "Backend Databases" + subgraph "MySQL Backends" + M1[(Primary)] + M2[(Replica 1)] + M3[(Replica 2)] + end + + subgraph "PostgreSQL Backends" + P1[(Primary)] + P2[(Standby)] + end + end + + MA --> MPH + PA --> PPH + AA --> APH + RA --> APH + + MPH --> QP + PPH --> QP + APH --> CONF + + QP --> QC + QP --> CP + QP --> AUTH + + CP --> M1 + CP --> M2 + CP --> M3 + CP --> P1 + CP --> P2 + + MON --> M1 + MON --> M2 + MON --> M3 + MON --> P1 + MON --> P2 + + MON --> MONDB + QP --> STATS + + style MA fill:#e1f5fe + style PA fill:#c8e6c9 + style AA fill:#fff3e0 + style CONF fill:#f3e5f5 + style M1 fill:#ffebee + style P1 fill:#e8f5e8 +``` + +## Code Layout Trees + +### Source Code Directory Structure + +``` +proxysql/ +├── src/ [Main Entry Points - 4 files] +│ ├── main.cpp # Application entry, thread initialization +│ ├── SQLite3_Server.cpp # Embedded config database server +│ ├── proxy_tls.cpp # TLS/SSL implementation +│ └── proxysql_global.cpp # Global variables and config +│ +├── lib/ [Core Libraries - 86+ files] +│ ├── [MySQL Components - 25+ files] +│ │ ├── MySQL_Session.cpp # Client session management +│ │ ├── MySQL_Protocol.cpp # Wire protocol implementation +│ │ ├── MySQL_HostGroups_Manager.cpp # Backend server management +│ │ ├── MySQL_Monitor.cpp # Health monitoring +│ │ ├── MySQL_Authentication.cpp # Auth methods (native, sha256, etc) +│ │ ├── MySQL_Query_Processor.cpp # Query routing logic +│ │ ├── MySQL_Query_Cache.cpp # Result caching +│ │ ├── MySQL_Thread.cpp # Thread pool management +│ │ ├── MySQL_Logger.cpp # Query/error logging +│ │ ├── MySQL_PreparedStatement.cpp # PS protocol handling +│ │ └── MySQL_Variables.cpp # Session variables +│ │ +│ ├── [PostgreSQL Components - 20+ files] +│ │ ├── PgSQL_Session.cpp # Client session management +│ │ ├── PgSQL_Protocol.cpp # Wire protocol v3 +│ │ ├── PgSQL_HostGroups_Manager.cpp # Backend management +│ │ ├── PgSQL_Monitor.cpp # Health checks +│ │ ├── PgSQL_Authentication.cpp # SASL/SCRAM support +│ │ ├── PgSQL_Query_Processor.cpp # Query routing +│ │ ├── PgSQL_Query_Cache.cpp # Result caching +│ │ ├── PgSQL_Thread.cpp # Thread management +│ │ └── PgSQL_Logger.cpp # Logging +│ │ +│ ├── [Base Infrastructure - 10+ files] +│ │ ├── Base_Session.cpp # Template base for sessions +│ │ ├── Base_Thread.cpp # Common threading +│ │ ├── Base_HostGroups_Manager.cpp # Template base for HG +│ │ ├── Query_Processor.cpp # Common query processing +│ │ └── Query_Cache.cpp # Common caching logic +│ │ +│ ├── [Admin & Monitoring - 15+ files] +│ │ ├── ProxySQL_Admin.cpp # Admin interface (6032) +│ │ ├── ProxySQL_Admin_Stats.cpp # Statistics collection +│ │ ├── ProxySQL_RESTAPI_Server.cpp # REST API (6080) +│ │ ├── ProxySQL_HTTP_Server.cpp # HTTP server +│ │ ├── ProxySQL_Cluster.cpp # Cluster coordination +│ │ └── ProxySQL_Config.cpp # Configuration management +│ │ +│ └── [Supporting Libraries] +│ ├── Standard_Query_Cache.cpp # Query cache implementation +│ ├── MySQL_ResultSet.cpp # Result set handling +│ ├── network.cpp # Network utilities +│ ├── debug.cpp # Debug/logging utilities +│ └── libproxysql.a # Compiled static library (340MB+) +│ +├── include/ [Header Files - 89+ files] +│ ├── [Protocol Headers] +│ │ ├── MySQL_Protocol.h # MySQL protocol definitions +│ │ ├── PgSQL_Protocol.h # PostgreSQL protocol defs +│ │ ├── mysql_connection.h # MySQL connection handling +│ │ └── pgsql_connection.h # PostgreSQL connections +│ │ +│ ├── [Core Headers] +│ │ ├── proxysql.h # Main header +│ │ ├── proxysql_structs.h # Core data structures +│ │ ├── Base_Session.h # Session template base +│ │ ├── Base_Thread.h # Thread base class +│ │ └── Base_HostGroups_Manager.h # HG template base +│ │ +│ └── [Utility Headers] +│ ├── btree_map.h # B-tree implementation +│ ├── SpookyV2.h # Hash functions +│ ├── gen_utils.h # General utilities +│ └── thread.h # Threading utilities +│ +├── deps/ [External Dependencies - 23 libraries] +│ ├── mariadb-client-library/ # MySQL/MariaDB connector +│ ├── postgresql/ # PostgreSQL client library +│ ├── sqlite3/ # Embedded database +│ ├── libev/ # Event loop (epoll/kqueue) +│ ├── jemalloc/ # Memory allocator +│ ├── prometheus-cpp/ # Metrics library +│ ├── re2/ # Google RE2 regex +│ ├── libinjection/ # SQL injection detection +│ ├── clickhouse-cpp/ # ClickHouse support +│ ├── libscram/ # SCRAM authentication +│ ├── curl/ # HTTP client +│ └── [12 more libraries...] +│ +└── test/ [Test Infrastructure] + ├── tap/ # TAP test framework + │ └── tests/ # 220+ test files + │ ├── test_mysql_*.cpp # MySQL-specific tests + │ ├── test_pgsql_*.cpp # PostgreSQL tests + │ └── test_admin_*.cpp # Admin interface tests + ├── cluster/ # Cluster tests + └── PrepStmt/ # Prepared statement tests +``` + +## Class Hierarchy Diagrams + +### Template-Based Class Architecture + +```mermaid +classDiagram + class Base_Session~S,DS,B,T~ { + <