|
| 1 | +# TiFlash Agent Guide |
| 2 | + |
| 3 | +This document provides essential information for agentic coding tools operating in the TiFlash repository. |
| 4 | +It focuses on the fastest safe path to build, test, and navigate the codebase. |
| 5 | + |
| 6 | +## 🚀 Quick Start |
| 7 | +- **Configure (preset):** `cmake --preset dev` |
| 8 | +- **Build:** |
| 9 | + * tiflash binary: `cmake --build --preset dev` |
| 10 | + * unit test binary: `cmake --build --preset unit-tests` |
| 11 | +- **Run one test:** `cmake-build-debug/dbms/gtests_dbms --gtest_filter=TestName.*` |
| 12 | + |
| 13 | +## 🛠 Build & Development |
| 14 | + |
| 15 | +TiFlash uses CMake with presets for configuration and building. |
| 16 | + |
| 17 | +### Build Presets |
| 18 | +Common presets defined in `CMakePresets.json`: |
| 19 | +- `dev`: DEBUG build with tests enabled. (Recommended for development) |
| 20 | +- `release`: RELWITHDEBINFO build without tests. |
| 21 | +- `asan`: AddressSanitizer build. |
| 22 | +- `tsan`: ThreadSanitizer build. |
| 23 | +- `benchmarks`: RELEASE build with benchmarks. |
| 24 | + |
| 25 | +### Dependencies & Versions |
| 26 | +- **CMake/Ninja/Clang/LLVM/Python/Rust**: Use versions supported by your platform toolchain. |
| 27 | +- **Linux vs macOS**: Toolchains live under `release-linux-llvm/` and `release-darwin/` respectively. |
| 28 | +- **Submodules/third-party**: Ensure any required submodules are initialized before building. |
| 29 | + |
| 30 | +### Common Commands |
| 31 | +- **Configure & Build (Dev):** `cmake --workflow --preset dev` |
| 32 | +- **Preset-only (recommended):** |
| 33 | + - Configure: `cmake --preset dev` |
| 34 | + - Build: `cmake --build --preset dev` |
| 35 | +- **Manual Build:** |
| 36 | + ```bash |
| 37 | + mkdir cmake-build-debug && cd cmake-build-debug |
| 38 | + cmake .. -GNinja -DCMAKE_BUILD_TYPE=DEBUG -DENABLE_TESTS=ON |
| 39 | + ninja tiflash |
| 40 | + ``` |
| 41 | +- **Linting & Formatting:** |
| 42 | + - Format diff: `python3 format-diff.py --diff_from $(git merge-base upstream/master HEAD)` |
| 43 | + (Use `origin/master` or another base if `upstream` is not configured.) |
| 44 | + - Clang-Tidy: `python3 release-linux-llvm/scripts/run-clang-tidy.py -p cmake-build-debug` |
| 45 | + |
| 46 | +## 🧪 Testing |
| 47 | + |
| 48 | +### Unit Tests (Google Test) |
| 49 | +Build targets: `gtests_dbms`, `gtests_libdaemon`, `gtests_libcommon`. |
| 50 | + |
| 51 | +- **Run all:** `cmake-build-debug/dbms/gtests_dbms` |
| 52 | +- **Run single test:** `cmake-build-debug/dbms/gtests_dbms --gtest_filter=TestName.*` |
| 53 | +- **List tests:** `cmake-build-debug/dbms/gtests_dbms --gtest_list_tests` |
| 54 | +- **Parallel runner:** |
| 55 | + ```bash |
| 56 | + python3 tests/gtest_10x.py cmake-build-debug/dbms/gtests_dbms |
| 57 | + ``` |
| 58 | +- **Other targets:** |
| 59 | + - `cmake-build-debug/dbms/gtests_libdaemon` |
| 60 | + - `cmake-build-debug/dbms/gtests_libcommon` |
| 61 | + |
| 62 | +### Integration Tests |
| 63 | +See `tests/AGENTS.md` for prerequisites and usage. |
| 64 | + |
| 65 | +### Sanitizers |
| 66 | +When running with ASAN/TSAN, use suppression files: |
| 67 | +```bash |
| 68 | +LSAN_OPTIONS="suppressions=tests/sanitize/asan.suppression" ./dbms/gtests_dbms |
| 69 | +TSAN_OPTIONS="suppressions=tests/sanitize/tsan.suppression" ./dbms/gtests_dbms |
| 70 | +``` |
| 71 | + |
| 72 | +## 🎨 Code Style (C++) |
| 73 | + |
| 74 | +TiFlash follows a style based on Google, enforced by `clang-format` 17.0.0+. |
| 75 | + |
| 76 | +### General |
| 77 | +- **Naming:** |
| 78 | + - Classes/Structs: `PascalCase` (e.g., `StorageDeltaMerge`) |
| 79 | + - Methods/Variables: `camelCase` (e.g., `readBlock`, `totalBytes`) |
| 80 | + - Files: `PascalCase` matching class name (e.g., `StorageDeltaMerge.cpp`) |
| 81 | +- **Namespaces:** Primary code resides in `namespace DB`. |
| 82 | +- **Headers:** Use `#pragma once`. Use relative paths from `dbms/src` (e.g., `#include <Core/Types.h>`). |
| 83 | + |
| 84 | +### Types & Error Handling |
| 85 | +- **Types:** Use explicit width types from `dbms/src/Core/Types.h`: `UInt8`, `UInt32`, `Int64`, `Float64`, `String`. |
| 86 | +- **Smart Pointers:** Prefer `std::shared_ptr` and `std::unique_ptr`. Use `std::make_shared` and `std::make_unique`. |
| 87 | +- **Error Handling:** |
| 88 | + - Use `DB::Exception`. |
| 89 | + - Prefer the fmt-style constructor with error code first: `throw Exception(ErrorCodes::SOME_CODE, "Message with {}", arg);` |
| 90 | + - For fixed strings without formatting, `throw Exception("Message", ErrorCodes::SOME_CODE);` is still acceptable. |
| 91 | + - Error codes are defined in `dbms/src/Common/ErrorCodes.cpp` and `errors.toml`. |
| 92 | + - In broad `catch (...)` paths, prefer `tryLogCurrentException(log, "context")` to avoid duplicated exception-formatting code. |
| 93 | +- **Logging:** Use macros like `LOG_INFO(log, "message {}", arg)`. `log` is usually a `DB::LoggerPtr`. |
| 94 | + - When only log level differs by runtime condition, prefer `LOG_IMPL(log, level, ...)` (with `Poco::Message::Priority`) instead of duplicated `if/else` log blocks. |
| 95 | + |
| 96 | +### Modern C++ Practices |
| 97 | +- Prefer `auto` for complex iterators/templates. |
| 98 | +- Use `std::string_view` for read-only string parameters. |
| 99 | +- Use `fmt::format` for string construction. |
| 100 | +- Prefer `FmtBuffer` for complex string building in performance-critical paths. |
| 101 | + |
| 102 | +## 🦀 Rust Code |
| 103 | +Rust is used in: |
| 104 | +- `contrib/tiflash-proxy`: The proxy layer between TiFlash and TiKV. |
| 105 | +- `contrib/tiflash-proxy-next-gen`: Disaggregated architecture components. |
| 106 | + |
| 107 | +Follow standard Rust idioms and `cargo fmt`. Use `cargo clippy` for linting. |
| 108 | + |
| 109 | +## 📚 Module-Specific Guides |
| 110 | +For more detailed information on specific subsystems, refer to: |
| 111 | +- **Storage Engine**: `dbms/src/Storages/AGENTS.md` (DeltaMerge, KVStore, PageStorage) |
| 112 | +- **Computation Engine**: `dbms/src/Flash/AGENTS.md` (Planner, MPP, Pipeline) |
| 113 | +- **TiDB Integration**: `dbms/src/TiDB/AGENTS.md` (Schema Sync, Decoding, Collation) |
| 114 | +- **Testing Utilities**: `dbms/src/TestUtils/AGENTS.md` (Base classes, Mocking, Data generation) |
| 115 | + |
| 116 | +## 📂 Directory Structure |
| 117 | +- `dbms/src`: Main TiFlash C++ source code. |
| 118 | +- `libs/`: Shared libraries used by TiFlash. |
| 119 | +- `tests/`: Integration and unit test utilities. |
| 120 | +- `docs/`: Design and development documentation. |
| 121 | +- `release-linux-llvm/`: Build scripts and environment configurations for Linux. |
| 122 | + |
| 123 | +## 💡 Debugging Tips |
| 124 | +- **LLDB:** Use to debug crashes or hangs. |
| 125 | +- **Coredumps:** Ensure coredumps are enabled in your environment. |
| 126 | +- **Failpoints:** TiFlash uses failpoints and syncpoints for testing error paths. |
| 127 | + - Search for `FAIL_POINT_TRIGGER_EXCEPTION` or `FAIL_POINT_PAUSE` for failpoints in the code. |
| 128 | + - Search for `SyncPointCtl` or `SYNC_FOR` for syncpoints in the code. |
| 129 | +- **Build artifacts:** If `compile_commands.json` is missing, ensure you configured with a preset. |
| 130 | + |
| 131 | +## 📖 References |
| 132 | +- `docs/DEVELOPMENT.md`: General engineering practices. |
| 133 | +- `docs/design/`: Design documents for major features. |
| 134 | +- [TiDB Developer Guide](https://pingcap.github.io/tidb-dev-guide/): General TiDB ecosystem information. |
0 commit comments