|
1 | 1 | # Repository Guidelines |
2 | 2 |
|
| 3 | +## Agent Change Discipline |
| 4 | + |
| 5 | +- Make surgical changes: touch only files required by the task. |
| 6 | +- Do not refactor adjacent code, rename symbols, or reformat unrelated files unless the task requires it. |
| 7 | +- Prefer the simplest solution that solves the problem; do not add speculative abstractions, features, or configurability. |
| 8 | +- Match existing style and patterns. Remove only unused imports, variables, functions, or files introduced by your change. |
| 9 | +- If requirements are ambiguous, state the assumption or ask before coding. |
| 10 | + |
3 | 11 | ## Project Structure & Module Organization |
4 | 12 |
|
5 | 13 | - `cmd/`: buildable binaries (e.g. `cmd/cdc`, `cmd/kafka-consumer`). |
|
17 | 25 | - `make check`: pre-submit checks (fmt, tidy, codegen, dashboards, Makefile formatting). |
18 | 26 | - `make unit_test`: unit tests with race + failpoints enabled (uses `--tags=intest`). |
19 | 27 | - `make unit_test_pkg PKG=./pkg/sink/...`: narrow unit test scope. |
| 28 | +- `make generate_mock`: regenerate gomock-based mocks via `scripts/generate-mock.sh`. |
20 | 29 | - `make integration_test_kafka CASE=<name>` (and `*_mysql|*_storage|*_pulsar`): run integration suites; requires binaries in `bin/` (`make check_third_party_binary`). |
21 | 30 |
|
22 | | -## Coding Style & Naming Conventions |
| 31 | +## Go Coding Rules |
23 | 32 |
|
24 | | -- Go: keep `gofmt` clean; use `make fmt` before pushing. |
| 33 | +- Formatting: keep `gofmt` clean; use `make fmt` before pushing. |
25 | 34 | - Naming: |
26 | 35 | - Functions: use camel case and **do not** include `_` (e.g. `getPartitionNum`, not `get_partition_num`). |
27 | 36 | - Variables: use lowerCamelCase (e.g. `flushInterval`, not `flush_interval`). |
28 | | -- Logging: structured logs via `github.com/pingcap/log` + `zap` fields; message strings should **not** include function names and should avoid `-` (use spaces instead). |
29 | | -- Errors: when an error comes from a third party/library call, wrap it immediately with `errors.Trace(err)` or `errors.WrapError(...)` to attach a stack trace; upstream callers should propagate wrapped errors without wrapping again. Avoid using `errors.New` to create error objects; instead, utilize the predefined objects available in the `cerrors` package. |
| 37 | +- Imports: do not rename imports unless required to resolve a package name conflict or to follow an existing local convention. |
| 38 | + |
| 39 | +## Errors |
| 40 | + |
| 41 | +- Use predefined errors from the repository error package; keep the local import name consistent with surrounding code. |
| 42 | +- When an error comes from a third-party or library call, wrap it immediately at the boundary with `errors.WrapError(predefinedError, err, args...)`. |
| 43 | +- After an error has been wrapped with `errors.WrapError`, propagate it directly; do not call `errors.Trace` again on later paths. |
| 44 | +- When creating a TiCDC error, use `GenWithStack...` or `GenWithStackByArgs...` on a predefined error and pass concrete details through arguments when needed. |
| 45 | +- Decide whether a newly generated error needs stack information. If a stack is unnecessary, especially on hot paths, use `FastGen...` or `FastGenByArgs...`. |
| 46 | +- Avoid other error creation or wrapping styles, including `errors.New`, bare `fmt.Errorf`, and adding stack information multiple times. |
| 47 | + |
| 48 | +## Logging |
| 49 | + |
| 50 | +- Use structured logs via `github.com/pingcap/log` with `zap` fields. |
| 51 | +- Treat logs as operational signals, not control-flow comments. Keep normal paths quiet. |
| 52 | +- Default `INFO`/`WARN` logs should record high-value lifecycle events, state changes, external dependency abnormalities, or invariant violations. |
| 53 | +- Choose log levels by required action: |
| 54 | + - `ERROR`: correctness, availability, or key progress is affected and needs attention. |
| 55 | + - `WARN`: the system is abnormal but can continue through recovery, retry, fallback, or degraded behavior. |
| 56 | + - `INFO`: key lifecycle events, important state changes, important configuration, or summary information. |
| 57 | + - `DEBUG`: bounded, low-frequency diagnostics with clear troubleshooting value. |
| 58 | +- Do not add `DEBUG` logs by default. Delete low-value logs instead of moving them to `DEBUG`. |
| 59 | +- Keep `message` stable and concise: summarize what happened, why it happened, and what the system will do next. |
| 60 | +- Put object details in stable camelCase `zap` fields such as `changefeedID`, `nodeID`, `dispatcherID`, `regionID`, `subscriptionID`, and `requestID`. |
| 61 | +- Message strings should not include function names and should avoid `-` (use spaces instead). |
| 62 | +- Avoid per-object or per-iteration logs, duplicated logs on the same error path, large objects, raw payloads, and long error dumps in default logs. |
| 63 | +- Use metrics for counts, scale, frequency, and trends; use windowed summaries or representative samples for high-cardinality events. |
| 64 | +- Before adding, keeping, or rewriting a log, verify that it answers a real diagnostic question, identifies the object, reason, action, and impact, and will not grow linearly with object count or retry/loop frequency. |
30 | 65 |
|
31 | 66 | ## Testing Guidelines |
32 | 67 |
|
33 | 68 | - Unit tests: `*_test.go`, favor deterministic tests; use `testify/require`. |
| 69 | +- Unit tests should cover meaningful behavior only; avoid redundant or low-value cases. |
| 70 | +- Do not test across feature boundaries. Keep each test focused on the behavior owned by the package or component under test. |
| 71 | +- Reuse existing tests when possible. Add a new test only when reuse would make the existing test unclear or incomplete. |
| 72 | +- If several test functions are highly related, merge them into one concise table-driven or scenario-based test. |
| 73 | +- When a test needs mocked components, prefer existing gomock-generated mocks over handwritten mocks. If the required mock does not exist, add it to the mock generation flow and run `make generate_mock`. |
| 74 | +- Keep tests efficient, simple, focused, and easy to update. |
34 | 75 | - Failpoints: `make unit_test` enables/disables automatically. If you enable manually, disable before committing to avoid a dirty tree. |
| 76 | +- For documentation-only changes, unit tests are usually unnecessary. If tests are skipped, state in the final response that only documentation was changed. |
35 | 77 |
|
36 | 78 | ## Commit & Pull Request Guidelines |
37 | 79 |
|
|
0 commit comments