Skip to content

Commit

Permalink
StateStore API implementation for CockroachDB (dapr#1559)
Browse files Browse the repository at this point in the history
* statestore for CockroachDB

Signed-off-by: Ricardo Corrêa <[email protected]>

* Upgrade nacos sdk version to meet compliance (dapr#1547)

* Upgrade nacos version

Signed-off-by: Shubham Sharma <[email protected]>

* Run go mod tidy all

Signed-off-by: Shubham Sharma <[email protected]>

* Add MaxSize to settings

Signed-off-by: Shubham Sharma <[email protected]>
Signed-off-by: Ricardo Corrêa <[email protected]>

* conformance tests for CockroachDB

Signed-off-by: Ricardo Corrêa <[email protected]>

* fix comment for internalNew func (CockroachDB)

Signed-off-by: Ricardo Corrêa <[email protected]>

* remove metadata from log (CockroachDB)

Signed-off-by: Ricardo Corrêa <[email protected]>

* removed unnecessary nil fields from GetResponse

Signed-off-by: Ricardo Corrêa <[email protected]>

* Fix ordering in transaction API for CockroachDB

Signed-off-by: Ricardo Corrêa <[email protected]>

* fix non-deterministic etag error (CockroachDB)

Signed-off-by: Ricardo Corrêa <[email protected]>

* fix error message for delete operation without key (CockroachDB)

Signed-off-by: Ricardo Corrêa <[email protected]>

Co-authored-by: Shubham Sharma <[email protected]>
Co-authored-by: Yaron Schneider <[email protected]>
Co-authored-by: Taction <[email protected]>
Co-authored-by: Bernd Verst <[email protected]>
Co-authored-by: Looong Dai <[email protected]>
Signed-off-by: Surender Singh Malik <[email protected]>
  • Loading branch information
6 people authored and surenderssm committed Apr 23, 2022
1 parent d6c3c3a commit ced9170
Show file tree
Hide file tree
Showing 13 changed files with 2,260 additions and 1 deletion.
19 changes: 19 additions & 0 deletions .github/infrastructure/docker-compose-cockroachdb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: '2'
services:
cockroachdb:
image: cockroachdb/cockroach:v21.2.3
hostname: cockroachdb
command: start-single-node --cluster-name=single-node --logtostderr=WARNING --log-file-verbosity=WARNING --insecure
restart: always
ports:
- "26257:26257"

cockroachdb-init:
hostname: cockroachdb-init
image: timveil/cockroachdb-remote-client:latest
environment:
- COCKROACH_HOST=cockroachdb:26257
- COCKROACH_INSECURE=true
- DATABASE_NAME=dapr_test
depends_on:
- cockroachdb
6 changes: 6 additions & 0 deletions .github/workflows/conformance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
- state.postgresql
- state.redis
- state.sqlserver
- state.cockroachdb
EOF
)
echo "::set-output name=pr-components::$PR_COMPONENTS"
Expand Down Expand Up @@ -301,6 +302,11 @@ jobs:
docker-compose -f ./.github/infrastructure/docker-compose-cassandra.yml -p cassandra up -d
if: contains(matrix.component, 'cassandra')

- name: Start cockroachdb
run: |
docker-compose -f ./.github/infrastructure/docker-compose-cockroachdb.yml -p cockroachdb up -d
if: contains(matrix.component, 'cockroachdb')

- name: Setup KinD test data
if: contains(matrix.component, 'kubernetes')
run: |
Expand Down
108 changes: 108 additions & 0 deletions state/cockroachdb/cockroachdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2022 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cockroachdb

import (
"github.com/dapr/components-contrib/state"
"github.com/dapr/kit/logger"
)

// CockroachDB state store.
type CockroachDB struct {
features []state.Feature
logger logger.Logger
dbaccess dbAccess
}

// New creates a new instance of CockroachDB state store.
func New(logger logger.Logger) *CockroachDB {
dba := newCockroachDBAccess(logger)

return internalNew(logger, dba)
}

// internalNew creates a new instance of a CockroachDB state store.
// This unexported constructor allows injecting a dbAccess instance for unit testing.
func internalNew(logger logger.Logger, dba dbAccess) *CockroachDB {
return &CockroachDB{
features: []state.Feature{state.FeatureETag, state.FeatureTransactional},
logger: logger,
dbaccess: dba,
}
}

// Init initializes the CockroachDB state store.
func (c *CockroachDB) Init(metadata state.Metadata) error {
return c.dbaccess.Init(metadata)
}

// Features returns the features available in this state store.
func (c *CockroachDB) Features() []state.Feature {
return c.features
}

// Delete removes an entity from the store.
func (c *CockroachDB) Delete(req *state.DeleteRequest) error {
return c.dbaccess.Delete(req)
}

// Get returns an entity from store.
func (c *CockroachDB) Get(req *state.GetRequest) (*state.GetResponse, error) {
return c.dbaccess.Get(req)
}

// Set adds/updates an entity on store.
func (c *CockroachDB) Set(req *state.SetRequest) error {
return c.dbaccess.Set(req)
}

// Ping checks if database is available.
func (c *CockroachDB) Ping() error {
return c.dbaccess.Ping()
}

// BulkDelete removes multiple entries from the store.
func (c *CockroachDB) BulkDelete(req []state.DeleteRequest) error {
return c.dbaccess.BulkDelete(req)
}

// BulkGet performs a bulks get operations.
func (c *CockroachDB) BulkGet(req []state.GetRequest) (bool, []state.BulkGetResponse, error) {
// TODO: replace with ExecuteMulti for performance.
return false, nil, nil
}

// BulkSet adds/updates multiple entities on store.
func (c *CockroachDB) BulkSet(req []state.SetRequest) error {
return c.dbaccess.BulkSet(req)
}

// Multi handles multiple transactions. Implements TransactionalStore.
func (c *CockroachDB) Multi(request *state.TransactionalStateRequest) error {
return c.dbaccess.ExecuteMulti(request)
}

// Query executes a query against store.
func (c *CockroachDB) Query(req *state.QueryRequest) (*state.QueryResponse, error) {
return c.dbaccess.Query(req)
}

// Close implements io.Closer.
func (c *CockroachDB) Close() error {
if c.dbaccess != nil {
return c.dbaccess.Close()
}

return nil
}
Loading

0 comments on commit ced9170

Please sign in to comment.