Skip to content

Dapr state store clickhouse #3675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 52 commits into
base: main
Choose a base branch
from

Conversation

middt
Copy link

@middt middt commented Feb 23, 2025

Description

This PR adds a new state store component for ClickHouse, a column-oriented database management system. The ClickHouse state store component provides the following features:

  • Basic CRUD operations for state management
  • Support for TTL (Time-To-Live)
  • ETag support for optimistic concurrency
  • Bulk operations support
  • Username/password authentication
  • Uses ReplacingMergeTree engine for efficient updates

Key implementation details:

  • Uses the official ClickHouse Go driver
  • Implements the full state.Store interface
  • Handles connection management and cleanup
  • Includes comprehensive integration tests
  • Follows Dapr's component development guidelines

Implementation Details

The component includes:

  1. State store implementation (clickhouse.go)

    • Full CRUD operations
    • Bulk operations
    • TTL support
    • ETag support
    • Authentication support
  2. Tests (clickhouse_test.go)

    • Integration tests for CRUD operations
    • Metadata validation tests
    • Authentication tests
    • Cleanup handling
  3. Configuration options:

    apiVersion: dapr.io/v1alpha1
    kind: Component
    metadata:
      name: statestore
      namespace: default
    spec:
      type: state.clickhouse
      version: v1
      metadata:
      - name: clickhouseURL
        value: "tcp://localhost:9000"
      - name: databaseName
        value: "dapr_state"
      - name: tableName
        value: "state_store"
      - name: username
        value: "default"
      - name: password
        value: ""

Checklist

  • Code compiles correctly
  • Created/updated tests
  • Extended the documentation / Created issue in the https://github.com/dapr/docs/ repo: dapr/docs#[issue number]

Testing Done

  1. Unit tests for metadata validation
  2. Integration tests for:
    • Basic CRUD operations
    • TTL functionality
    • ETag support
    • Bulk operations
    • Authentication
    • Error handling

Tests were run against ClickHouse v23.8 using the official Go driver.

Additional Notes

  • The component uses ReplacingMergeTree engine for better update performance
  • Includes proper cleanup in tests to avoid test pollution
  • Follows ClickHouse best practices for table design
  • Implements proper connection management and cleanup

@middt middt requested review from a team as code owners February 23, 2025 04:10
@JoshVanL
Copy link
Contributor

Thanks @middt, implementation generally looks good to me!

Would it be possible to add conformance tests for this too? It seems like we should be able to spin up a click house docker compose.

@middt middt force-pushed the dapr-state-store-clickhouse branch from 1b2a8de to 2c8934c Compare March 14, 2025 07:02
@middt
Copy link
Author

middt commented Mar 14, 2025

Thank you for the feedback! @JoshVanL I've implemented the conformance tests for the ClickHouse state store in this PR:

  1. Created a Docker Compose setup in /tests/certification/state/clickhouse/docker-compose.yml that spins up a ClickHouse server with proper configuration
  2. Added a conformance test file in /tests/certification/state/clickhouse/clickhouse_test.go that verifies all state store operations
  3. Created configuration files in /tests/config/state/clickhouse/clickhouse.yml for the test environment
  4. Added ClickHouse to the state store tests configuration in /tests/config/state/tests.yml

The tests verify all the key functionality including:

  • Basic CRUD operations
  • ETag support for optimistic concurrency
  • Error handling for empty and non-existent keys
  • Feature reporting

I've also addressed authentication issues by properly configuring username and password in both the Docker Compose setup and the state store implementation.

All unit tests are now passing, confirming that the implementation works correctly with the ClickHouse server.

@JoshVanL
Copy link
Contributor

JoshVanL commented Apr 9, 2025

Thanks @middt, I think the only thing left is to do a go mod tidy and it should be good to go 🙂

Appreciate the work on this!

@cicoyle
Copy link
Contributor

cicoyle commented Jun 25, 2025

Also, mind checking the linter locally? It appears to fail in CI

@middt middt force-pushed the dapr-state-store-clickhouse branch from 0eb488c to a910129 Compare June 26, 2025 06:30
middt and others added 3 commits June 26, 2025 09:49
- Fix import ordering and formatting (goimports, gofmt)
- Replace fmt.Sprintf with string concatenation for SQL queries
- Add nolint comments for legitimate SQL string concatenation
- Fix variable shadowing issues (govet)
- Remove unused function and imports
- Use t.Context() instead of context.Background() in tests
- Update go.mod/go.sum with ClickHouse dependencies

All linter checks now pass successfully.

Signed-off-by: Mehmet TOSUN <[email protected]>
- GPG key has been added to GitHub account
- All commits should now show as verified
- Retrigger CI pipeline to validate GPG signatures

Signed-off-by: Mehmet TOSUN <[email protected]>
- Updated Git email to [email protected]
- Created new GPG key matching Git email
- This commit should now show as verified with correct email

Signed-off-by: Mehmet TOSUN <[email protected]>
@middt middt force-pushed the dapr-state-store-clickhouse branch from 5e268b9 to 9392000 Compare June 26, 2025 06:49
@middt
Copy link
Author

middt commented Jun 26, 2025

Also, mind checking the linter locally? It appears to fail in CI

Also, mind checking the linter locally? It appears to fail in CI

Thanks for the feedback! @cicoyle I've already fixed all linter issues locally (import formatting, gosec warnings, variable shadowing, etc.) and the latest commits should pass CI.

@middt
Copy link
Author

middt commented Jun 26, 2025

@middt thanks for the PR 🎉

mind opening a dapr/docs PR targeting the 1.16 branch documenting this PR's details?

Absolutely! I've created a corresponding PR targeting the v1.16 branch with the same ClickHouse state store documentation.
dapr/docs#4697

dapr-bot and others added 2 commits June 26, 2025 06:37
- Add missing metadata.yaml file for component metadata schema
- Add ClickHouse to test-info.mjs configuration for CI/CD
- Add docker-compose-clickhouse.yml infrastructure setup
- Enable conformance and certification testing for ClickHouse

This resolves the 'Annotations 5 errors' build failures.

Signed-off-by: Mehmet TOSUN <[email protected]>
@middt middt force-pushed the dapr-state-store-clickhouse branch from a305a6a to a0e0d9a Compare June 26, 2025 13:51
@nelson-parente
Copy link
Contributor

@middt can you please run go mod tidy?

- Update metadata.yaml to use struct field names (ClickhouseURL, DatabaseName, TableName, Username, Password)
- Remove mapstructure tags from clickhouseMetadata struct since field names now match
- Update tests to use new field names
- Use kitmd.DecodeMetadata for automatic metadata parsing
- This resolves the metadata validation errors in the build process

Signed-off-by: Mehmet TOSUN <[email protected]>
@middt
Copy link
Author

middt commented Jul 10, 2025

@middt can you please run go mod tidy?

✅ Done! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autoupdate automatically keeps PR up to date against master documentation required This issue needs documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants