Repository navigation
Go: introduce clickhouse - #16996
Go: introduce clickhouse#16996
Conversation
Signed-off-by: Jin Hai <[email protected]>
📝 WalkthroughWalkthroughClickHouse support was added across configuration, Docker Compose, Go dependencies, server startup, connection management, and admin health reporting. Service configuration responses now omit selected sensitive fields, and OTel/ClickHouse services receive explicit service-type mappings. ChangesClickHouse configuration and provisioning
ClickHouse runtime and service integration
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant ServerMain
participant ClickhouseDriver
participant ClickHouse
participant AdminService
ServerMain->>ClickhouseDriver: Init ClickHouse configuration
ClickhouseDriver->>ClickHouse: Open connection and Ping
ClickHouse-->>ClickhouseDriver: Connection result
AdminService->>ClickhouseDriver: Request Status
ClickhouseDriver->>ClickHouse: Read pool stats and server version
ClickHouse-->>AdminService: Status or error
Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 5
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@docker/docker-compose-base.yml`:
- Around line 356-359: Update the clickhouse service environment to include
CLICKHOUSE_DB, assigning it from the configured CLICKHOUSE_DATABASE value so the
official entrypoint creates the selected database on startup. Preserve the
existing user, password, and access-management settings.
In `@docker/service_conf.yaml.template`:
- Around line 66-71: Update the ClickHouse connection configuration to use the
container’s internal port by replacing the CLICKHOUSE_TCP_PORT-based value in
the clickhouse service block with port 9000. Keep the existing host,
credentials, and database settings unchanged.
In `@internal/admin/service.go`:
- Around line 1396-1401: Update the ClickHouse-uninitialized branch in the
health-check logic to set status to the standard value "timeout" and move
"ClickHouse engine not initialized" into the message field, preserving the
existing service_name and return behavior.
In `@internal/engine/clickhouse/clickhouse.go`:
- Around line 81-102: Update Ping, Status, and Close to guard d.conn or
globalDriver.conn before dereferencing them, returning a descriptive error for
uninitialized connections and making Close safely no-op or return an error
without panicking. Preserve normal behavior when the ClickHouse connection is
initialized.
In `@internal/server/config.go`:
- Around line 445-448: Update the "clickhouse" case in the service configuration
switch to remove the user and password fields from configDict before it can be
exposed, matching the sanitization performed for mysql, minio, and redis while
preserving the existing id, name, and service_type assignments.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 5fb82fb7-1ea0-429e-aa1c-c68320a93f20
⛔ Files ignored due to path filters (1)
go.sumis excluded by!**/*.sum
📒 Files selected for processing (10)
cmd/ragflow_server.goconf/service_conf.yamldocker/.envdocker/docker-compose-base.ymldocker/init-clickhouse.sqldocker/service_conf.yaml.templatego.modinternal/admin/service.gointernal/engine/clickhouse/clickhouse.gointernal/server/config.go
| environment: | ||
| - CLICKHOUSE_USER=${CLICKHOUSE_USER:-ragflow} | ||
| - CLICKHOUSE_PASSWORD=${CLICKHOUSE_PASSWORD:-infini_rag_flow} | ||
| - CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1 |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Ensure dynamic creation of the configured ClickHouse database.
The clickhouse service environment lacks the CLICKHOUSE_DB variable. While an init script might be mapped, if a user overrides CLICKHOUSE_DATABASE in .env, the customized target database won't be automatically created on startup by the ClickHouse entrypoint. This will cause RAGFlow to crash when it attempts to connect.
Adding CLICKHOUSE_DB instructs the official ClickHouse entrypoint to auto-create the specified database if it doesn't already exist.
🐛 Proposed fix to pass the database environment variable
environment:
- CLICKHOUSE_USER=${CLICKHOUSE_USER:-ragflow}
- CLICKHOUSE_PASSWORD=${CLICKHOUSE_PASSWORD:-infini_rag_flow}
+ - CLICKHOUSE_DB=${CLICKHOUSE_DATABASE:-ragflow}
- CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| environment: | |
| - CLICKHOUSE_USER=${CLICKHOUSE_USER:-ragflow} | |
| - CLICKHOUSE_PASSWORD=${CLICKHOUSE_PASSWORD:-infini_rag_flow} | |
| - CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1 | |
| environment: | |
| - CLICKHOUSE_USER=${CLICKHOUSE_USER:-ragflow} | |
| - CLICKHOUSE_PASSWORD=${CLICKHOUSE_PASSWORD:-infini_rag_flow} | |
| - CLICKHOUSE_DB=${CLICKHOUSE_DATABASE:-ragflow} | |
| - CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1 |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@docker/docker-compose-base.yml` around lines 356 - 359, Update the clickhouse
service environment to include CLICKHOUSE_DB, assigning it from the configured
CLICKHOUSE_DATABASE value so the official entrypoint creates the selected
database on startup. Preserve the existing user, password, and access-management
settings.
| clickhouse: | ||
| host: '${CLICKHOUSE_HOST:-clickhouse}' | ||
| port: ${CLICKHOUSE_TCP_PORT:-9000} | ||
| user: '${CLICKHOUSE_USER:-ragflow}' | ||
| password: '${CLICKHOUSE_PASSWORD:-infini_rag_flow}' | ||
| database: '${CLICKHOUSE_DATABASE:-ragflow}' |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Use the internal container port for ClickHouse communication.
This template evaluates port: ${CLICKHOUSE_TCP_PORT:-9000}. However, CLICKHOUSE_TCP_PORT (default 9900 in .env) represents the host port mapped by Docker Compose.
Inside the ragflow Docker network, the ClickHouse container always listens on its native internal port (9000). If CLICKHOUSE_TCP_PORT evaluates to 9900, RAGFlow will attempt to connect to clickhouse:9900 inside the Docker network, resulting in a connection failure. Hardcode the port to 9000 to correctly match the internal network topology.
🐛 Proposed fix to use the internal port
clickhouse:
host: '${CLICKHOUSE_HOST:-clickhouse}'
- port: ${CLICKHOUSE_TCP_PORT:-9000}
+ port: 9000
user: '${CLICKHOUSE_USER:-ragflow}'
password: '${CLICKHOUSE_PASSWORD:-infini_rag_flow}'
database: '${CLICKHOUSE_DATABASE:-ragflow}'📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| clickhouse: | |
| host: '${CLICKHOUSE_HOST:-clickhouse}' | |
| port: ${CLICKHOUSE_TCP_PORT:-9000} | |
| user: '${CLICKHOUSE_USER:-ragflow}' | |
| password: '${CLICKHOUSE_PASSWORD:-infini_rag_flow}' | |
| database: '${CLICKHOUSE_DATABASE:-ragflow}' | |
| clickhouse: | |
| host: '${CLICKHOUSE_HOST:-clickhouse}' | |
| port: 9000 | |
| user: '${CLICKHOUSE_USER:-ragflow}' | |
| password: '${CLICKHOUSE_PASSWORD:-infini_rag_flow}' | |
| database: '${CLICKHOUSE_DATABASE:-ragflow}' |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@docker/service_conf.yaml.template` around lines 66 - 71, Update the
ClickHouse connection configuration to use the container’s internal port by
replacing the CLICKHOUSE_TCP_PORT-based value in the clickhouse service block
with port 9000. Keep the existing host, credentials, and database settings
unchanged.
| if clickhouseDriver == nil { | ||
| return map[string]interface{}{ | ||
| "service_name": name, | ||
| "status": "ClickHouse engine not initialized", | ||
| }, nil | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Fix the health check status value.
Returning "ClickHouse engine not initialized" in the status field breaks the standard convention where status is typically a simple enum-like string ("alive", "timeout", or "unknown"). Move this explanation to the message field and set status to "timeout".
🐛 Proposed fix
if clickhouseDriver == nil {
return map[string]interface{}{
"service_name": name,
- "status": "ClickHouse engine not initialized",
+ "status": "timeout",
+ "message": "ClickHouse engine not initialized",
}, nil
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if clickhouseDriver == nil { | |
| return map[string]interface{}{ | |
| "service_name": name, | |
| "status": "ClickHouse engine not initialized", | |
| }, nil | |
| } | |
| if clickhouseDriver == nil { | |
| return map[string]interface{}{ | |
| "service_name": name, | |
| "status": "timeout", | |
| "message": "ClickHouse engine not initialized", | |
| }, nil | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/admin/service.go` around lines 1396 - 1401, Update the
ClickHouse-uninitialized branch in the health-check logic to set status to the
standard value "timeout" and move "ClickHouse engine not initialized" into the
message field, preserving the existing service_name and return behavior.
| func (d *Driver) Ping(ctx context.Context) error { | ||
| return d.conn.Ping(ctx) | ||
| } | ||
|
|
||
| func (d *Driver) Status() (map[string]interface{}, error) { | ||
| stats := d.conn.Stats() | ||
| version, err := d.conn.ServerVersion() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get server version: %w", err) | ||
| } | ||
| return map[string]interface{}{ | ||
| "max_open_conns": stats.MaxOpenConns, | ||
| "max_idle_conns": stats.MaxIdleConns, | ||
| "open": stats.Open, | ||
| "idle": stats.Idle, | ||
| "server_version": version.String(), | ||
| }, nil | ||
| } | ||
|
|
||
| func Close() error { | ||
| return globalDriver.conn.Close() | ||
| } |
There was a problem hiding this comment.
🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win
Add nil checks to prevent panics on uninitialized connections.
If ClickHouse fails to initialize (e.g., unreachable host or invalid config), Init returns an error, but the server continues execution. Because Close() is deferred, and Status() can be called via the admin health check, any method accessing d.conn or globalDriver.conn will trigger a nil-pointer dereference (NPE) and crash the application.
🛡️ Proposed fix to protect against nil pointers
func (d *Driver) Ping(ctx context.Context) error {
+ if d == nil || d.conn == nil {
+ return fmt.Errorf("clickhouse connection is not initialized")
+ }
return d.conn.Ping(ctx)
}
func (d *Driver) Status() (map[string]interface{}, error) {
+ if d == nil || d.conn == nil {
+ return nil, fmt.Errorf("clickhouse connection is not initialized")
+ }
stats := d.conn.Stats()
version, err := d.conn.ServerVersion()
if err != nil {
return nil, fmt.Errorf("failed to get server version: %w", err)
}
return map[string]interface{}{
"max_open_conns": stats.MaxOpenConns,
"max_idle_conns": stats.MaxIdleConns,
"open": stats.Open,
"idle": stats.Idle,
"server_version": version.String(),
}, nil
}
func Close() error {
+ if globalDriver == nil || globalDriver.conn == nil {
+ return nil
+ }
return globalDriver.conn.Close()
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| func (d *Driver) Ping(ctx context.Context) error { | |
| return d.conn.Ping(ctx) | |
| } | |
| func (d *Driver) Status() (map[string]interface{}, error) { | |
| stats := d.conn.Stats() | |
| version, err := d.conn.ServerVersion() | |
| if err != nil { | |
| return nil, fmt.Errorf("failed to get server version: %w", err) | |
| } | |
| return map[string]interface{}{ | |
| "max_open_conns": stats.MaxOpenConns, | |
| "max_idle_conns": stats.MaxIdleConns, | |
| "open": stats.Open, | |
| "idle": stats.Idle, | |
| "server_version": version.String(), | |
| }, nil | |
| } | |
| func Close() error { | |
| return globalDriver.conn.Close() | |
| } | |
| func (d *Driver) Ping(ctx context.Context) error { | |
| if d == nil || d.conn == nil { | |
| return fmt.Errorf("clickhouse connection is not initialized") | |
| } | |
| return d.conn.Ping(ctx) | |
| } | |
| func (d *Driver) Status() (map[string]interface{}, error) { | |
| if d == nil || d.conn == nil { | |
| return nil, fmt.Errorf("clickhouse connection is not initialized") | |
| } | |
| stats := d.conn.Stats() | |
| version, err := d.conn.ServerVersion() | |
| if err != nil { | |
| return nil, fmt.Errorf("failed to get server version: %w", err) | |
| } | |
| return map[string]interface{}{ | |
| "max_open_conns": stats.MaxOpenConns, | |
| "max_idle_conns": stats.MaxIdleConns, | |
| "open": stats.Open, | |
| "idle": stats.Idle, | |
| "server_version": version.String(), | |
| }, nil | |
| } | |
| func Close() error { | |
| if globalDriver == nil || globalDriver.conn == nil { | |
| return nil | |
| } | |
| return globalDriver.conn.Close() | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/engine/clickhouse/clickhouse.go` around lines 81 - 102, Update Ping,
Status, and Close to guard d.conn or globalDriver.conn before dereferencing
them, returning a descriptive error for uninitialized connections and making
Close safely no-op or return an error without panicking. Preserve normal
behavior when the ClickHouse connection is initialized.
| case "clickhouse": | ||
| configDict["id"] = id | ||
| configDict["name"] = "clickhouse" | ||
| configDict["service_type"] = "olap" |
There was a problem hiding this comment.
🔒 Security & Privacy | 🔴 Critical | ⚡ Quick win
Prevent leakage of ClickHouse credentials.
The clickhouse configuration map retains the user and password fields, unlike the mysql, minio, and redis cases. If this configuration dictionary is exposed via admin API endpoints (such as GetServiceDetails), the plaintext credentials will be leaked. Sanitize the dictionary by deleting these sensitive fields.
🔒️ Proposed fix to sanitize the credentials
case "clickhouse":
configDict["id"] = id
configDict["name"] = "clickhouse"
configDict["service_type"] = "olap"
+ delete(configDict, "user")
+ delete(configDict, "password")📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| case "clickhouse": | |
| configDict["id"] = id | |
| configDict["name"] = "clickhouse" | |
| configDict["service_type"] = "olap" | |
| case "clickhouse": | |
| configDict["id"] = id | |
| configDict["name"] = "clickhouse" | |
| configDict["service_type"] = "olap" | |
| delete(configDict, "user") | |
| delete(configDict, "password") |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/server/config.go` around lines 445 - 448, Update the "clickhouse"
case in the service configuration switch to remove the user and password fields
from configDict before it can be exposed, matching the sanitization performed
for mysql, minio, and redis while preserving the existing id, name, and
service_type assignments.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #16996 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
Introduce Clickhouse for data statistics.