|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +This file provides guidance to AI coding agents (e.g., Claude Code, Cursor, ChatGPT Codex, Gemini) when working with code in this repository. |
| 4 | + |
| 5 | +While working on Apache Kvrocks, please remember: |
| 6 | + |
| 7 | +- Always use English in code and comments. |
| 8 | +- Only add meaningful comments when the code's behavior is difficult to understand. |
| 9 | +- Add or update tests to cover externally observable behavior and regressions when you change or add functionality. |
| 10 | +- Always run the formatter before submitting changes. |
| 11 | + |
| 12 | +## Build and Development Commands |
| 13 | + |
| 14 | +### Building |
| 15 | + |
| 16 | +```bash |
| 17 | +# Build kvrocks and utilities |
| 18 | +./x.py build # Build to ./build directory |
| 19 | +./x.py build -j N # Build with N parallel jobs |
| 20 | +./x.py build --unittest # Build with unit tests |
| 21 | +./x.py build -DENABLE_OPENSSL=ON # Build with TLS support |
| 22 | +./x.py build --ninja # Use Ninja build system |
| 23 | +./x.py build --skip-build # Only run CMake configure |
| 24 | +./x.py build -DCMAKE_BUILD_TYPE=Debug # Debug build |
| 25 | + |
| 26 | +# Run a local server |
| 27 | +./build/kvrocks -c kvrocks.conf |
| 28 | + |
| 29 | +# Fetch dependencies |
| 30 | +./x.py fetch-deps # Fetch dependency archives |
| 31 | +``` |
| 32 | + |
| 33 | +### Testing |
| 34 | + |
| 35 | +```bash |
| 36 | +# Build and run C++ unit tests |
| 37 | +./x.py build --unittest |
| 38 | +./x.py test cpp |
| 39 | + |
| 40 | +# Run Go integration tests |
| 41 | +./x.py test go |
| 42 | + |
| 43 | +# Run specific Go test by path |
| 44 | +./x.py test go tests/gocase/unit/... |
| 45 | +``` |
| 46 | + |
| 47 | +### Lint |
| 48 | + |
| 49 | +You must run the formatter and linters before submitting code changes. This ensures code quality and consistency across the project. It requires installing `clang-format`, `clang-tidy`, and `golangci-lint` locally. Please refer to the CONTRIBUTING.md for setup instructions. |
| 50 | + |
| 51 | +```bash |
| 52 | +# Format code (must pass before submitting) |
| 53 | +./x.py format |
| 54 | + |
| 55 | +# Check code format (fails if not formatted) |
| 56 | +./x.py check format |
| 57 | + |
| 58 | +# Run clang-tidy |
| 59 | +./x.py check tidy |
| 60 | + |
| 61 | +# Run golangci-lint for Go tests |
| 62 | +./x.py check golangci-lint |
| 63 | +``` |
| 64 | + |
| 65 | +## Architecture Overview |
| 66 | + |
| 67 | +Apache Kvrocks is a distributed key-value NoSQL database compatible with the Redis protocol, using RocksDB as its storage engine. |
| 68 | + |
| 69 | +### Core Structure |
| 70 | + |
| 71 | +- **`src/server/`**: Main server orchestration, connection handling, and worker threads. The `Server` class manages the event loop, worker threads, and coordinates all components. |
| 72 | +- **`src/storage/`**: RocksDB integration layer. Key classes: |
| 73 | + - `Storage`: Manages RocksDB instance, column families, and write batches |
| 74 | + - `Context`: Passes snapshot and batch between APIs for transactional consistency |
| 75 | +- **`src/commands/`**: Redis protocol command implementations. Each command type has a corresponding `Commander` subclass. |
| 76 | +- **`src/types/`**: Redis data structure implementations (String, Hash, List, Set, ZSet, Stream, etc.) |
| 77 | +- **`src/cluster/`**: Cluster management, slot migration, and replication. |
| 78 | +- **`src/search/`**: Full-text search and vector search (HNSW) implementation. |
| 79 | +- **`src/config/`**: Server configuration parsing and management. |
| 80 | +- **`src/cli/`**: Command-line interface utilities. |
| 81 | +- **`src/common/`**: Shared utilities and helper functions. |
| 82 | +- **`src/stats/`**: Statistics and metrics collection. |
| 83 | + |
| 84 | +### Key Patterns |
| 85 | + |
| 86 | +- **Column Families**: 8 column families are used - PrimarySubkey, Metadata, SecondarySubkey, PubSub, Propagate, Stream, Search, Index. |
| 87 | +- **Command Registration**: Commands are registered via the `REDIS_REGISTER_COMMANDS` macro with flags like `kCmdWrite`, `kCmdReadOnly`, `kCmdBlocking`, etc. |
| 88 | +- **Write Batch with Index**: Used for transactional mode to group writes before commit. |
| 89 | +- **Worker Thread Model**: Libevent-based async I/O with dedicated worker threads. |
| 90 | +- **Namespace Isolation**: Token-based multi-tenancy using the `__namespace` column family. |
| 91 | + |
| 92 | +### Data Encoding |
| 93 | + |
| 94 | +- `METADATA_ENCODING_VERSION=1` (default): Encodes 64-bit size and expire time in milliseconds. |
| 95 | +- `METADATA_ENCODING_VERSION=0`: Legacy encoding. |
| 96 | + |
| 97 | +Refer to https://kvrocks.apache.org/community/data-structure-on-rocksdb for more details. |
| 98 | + |
| 99 | +## Coding Style and Naming Conventions |
| 100 | + |
| 101 | +- C++ formatting follows `.clang-format` (Google-based, 2-space indent, 120-column limit, sorted includes). |
| 102 | +- Use `.cc`/`.h` file extensions with `snake_case` filenames. |
| 103 | +- Types use `PascalCase`; match existing patterns in nearby code. |
| 104 | +- Use existing utilities and helper functions when possible; avoid reinventing the wheel. |
| 105 | +- Go code should stay `gofmt`-clean and comply with `tests/gocase/.golangci.yml`. |
| 106 | + |
| 107 | +## Testing Guidelines |
| 108 | + |
| 109 | +You could provide Go tests for integration-level verification of command behaviors and C++ unit tests for internal logic. Focus on testing new features or bug fixes, and avoid adding tests that don't verify meaningful behavior changes. |
| 110 | + |
| 111 | +- **Go Integration Tests** (`tests/gocase/`): Use `*_test.go` files, organized by feature (unit/, integration/, tls/). |
| 112 | +- **C++ Unit Tests** (`tests/cppunit/`): Use `*_test.cc` files with GoogleTest framework. |
| 113 | +- Add or update tests alongside behavior changes. |
| 114 | +- Prefer focused unit tests; add integration coverage when commands or replication/storage behaviors change. |
| 115 | +- Use `./x.py test ...` entry points for consistent setup. |
| 116 | + |
| 117 | +## Commit Message Format |
| 118 | + |
| 119 | +Use conventional commits with a scope indicating the affected component: |
| 120 | + |
| 121 | +``` |
| 122 | +feat(rdb): add DUMP support for SortedInt type |
| 123 | +fix(replication): prevent WAL exhaustion from slow consumers |
| 124 | +fix(string): add empty string value check for INCR to match Redis behavior |
| 125 | +perf(hash): use MultiGet to reduce RocksDB calls in HMSET |
| 126 | +chore(deps): Bump rocksdb to v10.10.1 |
| 127 | +chore(ci): bump crate-ci/typos action to v1.43.1 |
| 128 | +chore(tests): replace to slices.Reverse() in go test |
| 129 | +``` |
| 130 | + |
| 131 | +Common scopes: `server`, `storage`, `commands`, `cluster`, `search`, `types`, `replication`, `rdb`, `stream`, `hash`, `string`, `list`, `set`, `zset`, `deps`, `ci`, `tests`, `conf`. |
| 132 | + |
| 133 | +## Common Tasks |
| 134 | + |
| 135 | +### Adding a New Command |
| 136 | + |
| 137 | +1. Create or update the command handler in `src/commands/`. |
| 138 | +2. Implement the `Commander` subclass with `Parse()` and `Execute()` methods. |
| 139 | +3. Register the command using `REDIS_REGISTER_COMMANDS` macro with appropriate flags. |
| 140 | +4. Add the underlying data operation in `src/types/` if needed. |
| 141 | +5. Add C++ unit tests in `tests/cppunit/`. |
| 142 | +6. Add Go integration tests in `tests/gocase/`. |
| 143 | + |
| 144 | +### Adding a New Data Type |
| 145 | + |
| 146 | +1. Implement the type in `src/types/` following existing patterns. |
| 147 | +2. Define the metadata encoding in `src/storage/`. |
| 148 | +3. Add command handlers in `src/commands/`. |
| 149 | +4. Register commands with the `REDIS_REGISTER_COMMANDS` macro. |
| 150 | +5. Add tests for both the type operations and command handlers. |
| 151 | + |
| 152 | +### Debugging |
| 153 | + |
| 154 | +1. Check server logs (configurable log level in kvrocks config). |
| 155 | +2. Use the `DEBUG` command for runtime inspection. |
| 156 | +3. Use sanitizers via build flags for memory and thread issues. |
| 157 | +4. Refer to `tests/lsan-suppressions` and `tests/tsan-suppressions` for known suppression rules. |
| 158 | + |
| 159 | +## Important Notes |
| 160 | + |
| 161 | +- Kvrocks aims for Redis protocol compatibility; always verify behavior against Redis when implementing or fixing commands. |
| 162 | +- All changes must pass `./x.py check format` and `./x.py check tidy`. |
| 163 | +- Don't change public command behavior unless requested. |
| 164 | +- RocksDB is the core storage dependency; be cautious with storage-layer changes. |
| 165 | +- Adding a new column family breaks forward compatibility; avoid this if possible and prefer using existing column families. |
0 commit comments