Alibaba Druid

repository·master·Indexed 12 days ago

https://github.com/alibaba/druid

A high-performance database connection pool and SQL parser for Java applications. It provides JDBC connection pooling, SQL analysis, security protection via a SQL Firewall, and real-time monitoring. Includes specialized starters for Spring Boot 2.x, 3.x (requiring Java 17+), and 4.x, as well as Druid Admin for centralized cluster monitoring via Nacos, Consul, or Eureka registries.

Tokens
41.9K
Snippets
78
Records
157
Agent score
94%

What's inside Druid

  1. Overview of Alibaba Druid

    master

    Alibaba Druid is a high-performance, scalable JDBC connection pool implementation for Java applications. It provides efficient database connection management along with advanced features for monitoring, statistics, and security.

    Key Features:

    • JDBC Connection Pooling: Managed via DruidDataSource.
    • SQL Monitoring & Analysis: Includes SQL parsing and statistics collection.
    • Security: Features a SQL firewall via WallFilter.
    • Spring Boot Integration: Provides dedicated starters for Spring Boot 2.x (druid-spring-boot-starter) and Spring Boot 3.x (druid-spring-boot-3-starter).
  2. Overview of Alibaba Druid core capabilities

    master

    Alibaba Druid is a database middleware providing two primary functional areas:

    1. JDBC Connection Pool: A high-performance, monitorable implementation of a database connection pool.
    2. SQL Parser: A comprehensive framework for SQL parsing, Abstract Syntax Tree (AST) construction, and visitor patterns, supporting over 30 database dialects.

    It is designed for Java 8+ and is licensed under Apache License 2.0.

  3. Understand the Druid Architecture Overview

    master

    Druid is composed of four core subsystems that collaborate through clear API boundaries:

    1. Connection Pool Subsystem: Manages the lifecycle of physical database connections via DruidDataSource.
    2. Filter-Chain Subsystem: An interceptor mechanism using the Responsibility Chain pattern to inject logic (security, logging, statistics) into JDBC operations.
    3. SQL Parser Engine: Converts SQL text into a structured Abstract Syntax Tree (AST) using a pipeline of Lexer, Parser, and Visitors, supporting over 30 dialects.
    4. Monitoring & Statistics Subsystem: Collects performance metrics (SQL execution time, slow SQL, pool status) via StatFilter and exposes them through web interfaces or programmatic APIs.
  4. What is High Available DataSource (HA DataSource)

    master

    HighAvailableDataSource is a wrapper around the standard Druid DataSource that provides client-side load balancing without requiring external tools like LVS or HA Proxy. It is particularly useful for:

    1. Read/Write Splitting: Distributing read requests across multiple database replicas.
    2. Middleware Scaling: Distributing connections across multiple instances of a sharding middleware (e.g., MyCat).

    Key Features:

    • Node Routing: Supports routing by name, random, or sticky random selection.
    • Flexible Configuration: Nodes can be configured manually, via property files, or dynamically via ZooKeeper.
    • Health Checking: Automatically detects and blacklists unhealthy nodes using a background validation mechanism.
  5. What is Druid Admin

    master
    Druid Admin is a cluster monitoring and management component for Druid. While standard Druid deployments use the built-in StatViewServlet for single-node monitoring, Druid Admin is designed for cluster environments. It discovers application nodes through a service registry, collects Druid monitoring data from each node, and provides a centralized, aggregated view of the entire cluster's monitoring data.
  6. How SQL Wall security checks work

    master
    Druid's SQL Wall security mechanism follows a Parse-Then-Validate flow. Instead of relying on fragile raw string matching or regex, the security engine parses the incoming SQL into an Abstract Syntax Tree (AST). Security decisions are then made by inspecting the structure of the parsed statements and expressions. This ensures that obfuscated SQL or variations in whitespace do not bypass security rules.
  7. Druid Architecture Capability Map

    master

    Druid's architecture is composed of several core capability modules. Understanding these modules helps in identifying which part of the system handles specific tasks:

    • sql-parser-core: Manages the Lexer, Parser, and AST (Abstract Syntax Tree) pipeline, including dialect dispatching.
    • connection-pool-core: Handles connection pool capacity, lifecycle management tasks, and ensures concurrency safety.
    • filter-chain: Provides an ordered interception mechanism and allows for extensible filter integration.
    • wall-security: Performs AST-based SQL security validation with awareness of specific SQL dialects.
    • monitoring-stat: Responsible for collecting runtime statistics and managing their exposure channels.
  8. Canonical Naming Convention for Lexer and Parser Feature Gates

    master

    To ensure SQL parser feature gates remain readable and reviewable, all LexerFeature and ParserFeature identifiers must follow a deterministic naming convention.

    Naming Pattern: Use the pattern Scope + Intent.

    Key Rules:

    1. Consistent Scope: Use aligned scope wording for equivalent behavior constraints across both lexer and parser paths. Avoid using divergent synonyms for the same concept.
    2. Explicit Polarity: Names describing enabled or disabled behavior must use explicit polarity terms. Avoid double negatives and ambiguous abbreviations to ensure intent is clear.
    3. Semantic Clarity: Reviewers should be able to infer the equivalence of gates without inspecting implementation details.
  9. How dialect-specific parser dispatch works

    master

    The parser framework uses a deterministic two-step strategy to select the correct parser for a specific SQL dialect. This allows for custom dialect providers to be registered without modifying the core parser logic.

    Dispatch Logic:

    1. Registered Provider: The framework first attempts to resolve a registered dialect provider for the given dialect key. If found, the parser creation uses this provider, and the built-in dispatch is bypassed.
    2. Built-in Fallback: If no provider is registered for the dialect key, the framework falls back to the built-in DbType parser dispatch path.
    3. Unregistration: If a previously registered provider is unregistered, the framework automatically reverts to the built-in DbType dispatch.

    This mechanism ensures that the base parser remains decoupled from concrete dialect type constants.

  10. How dialect differences are implemented: Features vs Overrides

    master

    Druid uses two primary mechanisms to handle dialect-specific behavior. Understanding when to use which is critical for extending the parser:

    1. DialectFeature (Bitmask): Used for simple, toggleable behaviors or switches. These are declared in the Lexer and checked via dialectFeatureEnabled(). Use this for non-structural differences (e.g., ScanString2PutDoubleBackslash).
    2. Class Inheritance & Method Overriding: Used when a dialect requires a fundamentally different logic flow or structural parsing rule. Use this when you need to change how a specific token is scanned or how a statement is structured (e.g., HiveLexer.scanString()).
  11. How the Parser works

    master

    The Parser layer uses a hand-written recursive descent approach to build the AST. It is organized into a hierarchy of specialized parsers:

    • SQLParser: The base class providing core utilities like accept(Token) and alias resolution.
    • SQLExprParser: Handles complex expression logic (e.g., primary(), relational(), additive()).
    • SQLStatementParser: The primary engine for parsing full statements (e.g., parseSelect(), parseInsert(), parseCreate()).
    • SQLSelectParser: A specialized parser for SELECT queries, handling WITH clauses, JOINs, and GROUP BY logic.

    Each database dialect (like MySQL) provides its own implementation of these parsers (e.g., MySqlStatementParser) to handle vendor-specific syntax like SHOW commands or specific ENGINE options in CREATE TABLE statements.

  12. How GROUP BY GROUPING SETS separators are handled

    master

    The parser tracks whether a SQLGroupingSetExpr (a sibling in a GROUP BY clause) was preceded by a comma in the source SQL. This is critical for preserving the exact formatting when serializing the AST back to SQL.

    Key Behaviors:

    • hasPrefixComma Flag: The SQLGroupingSetExpr contains a hasPrefixComma flag. If true, the output visitor emits a comma before the GROUPING SETS keyword. If false, no comma is emitted.
    • Default Behavior: For programmatically constructed SQLGroupingSetExpr nodes, the hasPrefixComma flag defaults to true to maintain compatibility with existing AST builders that expect the comma form (e.g., GROUP BY x, GROUPING SETS(...)).
    • Single Item Exception: If GROUPING SETS is the only item in the GROUP BY clause, the output visitor will not emit a leading comma, regardless of the flag value.
    • AST Equality: Two SQLGroupingSetExpr instances with identical parameters but different hasPrefixComma values are considered unequal (equals returns false) to allow AST diff tools to distinguish between the two surface forms.