components-contrib icon indicating copy to clipboard operation
components-contrib copied to clipboard

Dapr state store clickhouse

Open middt opened this issue 10 months ago • 19 comments

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

  • [x] Code compiles correctly
  • [x] 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 avatar Feb 23 '25 04:02 middt

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.

JoshVanL avatar Mar 13 '25 14:03 JoshVanL

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.

middt avatar Mar 14 '25 07:03 middt

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

Appreciate the work on this!

JoshVanL avatar Apr 09 '25 19:04 JoshVanL

Done :) ! Just ran go mod tidy and pushed the changes. Thanks for the review and guidance! 👍

@JoshVanL

middt avatar Apr 10 '25 06:04 middt

@middt - mind resolving the conflicts with go.mod & go.sum?

cicoyle avatar May 22 '25 14:05 cicoyle

Hi @cicoyle,

I've resolved the conflicts in go.mod and go.sum by accepting the changes from the main branch, which includes the latest dependency updates. The changes have been pushed to the branch.

The main updates include:

  • Updated OpenTelemetry dependencies to their latest versions
  • Updated Google Cloud dependencies
  • Added new indirect dependencies required by the latest versions

All tests are passing and the implementation remains unchanged. The PR should now be ready for review.

middt avatar May 22 '25 15:05 middt

@middt thanks for the PR 🎉

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

cicoyle avatar Jun 25 '25 20:06 cicoyle

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

cicoyle avatar Jun 25 '25 20:06 cicoyle

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 avatar Jun 26 '25 07:06 middt

@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.
https://github.com/dapr/docs/pull/4697

middt avatar Jun 26 '25 08:06 middt

@middt can you please run go mod tidy?

nelson-parente avatar Jul 09 '25 17:07 nelson-parente

@middt can you please run go mod tidy?

✅ Done! :)

middt avatar Jul 10 '25 07:07 middt

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

github-actions[bot] avatar Aug 21 '25 14:08 github-actions[bot]

ping pong

middt avatar Aug 22 '25 14:08 middt

Hey @middt, tests are failing

JoshVanL avatar Aug 29 '25 12:08 JoshVanL

Hey @middt, tests are failing

Hi @JoshVanL, Fixed! Tests now pass:, Ready for review!

middt avatar Aug 31 '25 03:08 middt

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

github-actions[bot] avatar Nov 08 '25 16:11 github-actions[bot]

This pull request has been automatically closed because it has not had activity in the last 37 days. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

github-actions[bot] avatar Nov 15 '25 16:11 github-actions[bot]

@JoshVanL did you have any other feedback here by chance?

sicoyle avatar Dec 09 '25 21:12 sicoyle

@sicoyle I think this one is actually super interesting to get in. I've seen an uptick in Clickhouse lately so supporting this would be beneficial imo. @middt please address the comments so we can get this one merged 😄

CasperGN avatar Dec 15 '25 08:12 CasperGN

Hi @sicoyle @CasperGN! Thank you for the thorough review! I've addressed all the feedback in the latest commit (1be750bf):

✅ Changes Made:

  1. Copyright Year Updated (2021 → 2025)

    • Updated in all ClickHouse-related files (clickhouse.go, clickhouse_test.go, clickhouse_integration_test.go, and tests/certification/state/clickhouse/clickhouse_test.go)
  2. Added Length Checks for Database and Table Names

    • Added a maxIdentifierLength constant set to 256 characters (based on ClickHouse documentation)
    • Added validation in parseAndValidateMetadata() to return an error if names exceed the limit
  3. Updated Metadata Field Names to camelCase

    • ClickhouseURLclickhouseUrl
    • DatabaseNamedatabaseName
    • TableNametableName
    • Usernameusername
    • Passwordpassword
    • Added proper mapstructure tags in the Go struct for proper binding
    • Updated all test files and config files to use the new camelCase names
  4. Documentation PR

    • Yes, a docs PR was already created: https://github.com/dapr/docs/pull/4697 (targeting v1.16 branch)
  5. Username/Password Authentication

    • Confirmed: ClickHouse can be configured to run without authentication (using the default user with no password). This is common in development environments. In production, authentication should be configured. The fields are correctly marked as required: false in the metadata schema.

All tests are passing and linting is clean. Ready for re-review! 🚀

middt avatar Dec 15 '25 11:12 middt