components-contrib/state
Alessandro (Ale) Segala d9ea9c69c9
Fixes for SQL Server state store (#2912)
Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>
Signed-off-by: joshvanl <me@joshvanl.dev>
Co-authored-by: joshvanl <me@joshvanl.dev>
Co-authored-by: Artur Souza <artursouza.ms@outlook.com>
2023-06-19 18:04:46 +00:00
..
aerospike Add conformance tests for etags in Bulk operations (#2843) 2023-05-17 16:28:29 -07:00
alicloud/tablestore Add conformance tests for etags in Bulk operations (#2843) 2023-05-17 16:28:29 -07:00
aws/dynamodb dynamodb: return ttlExpiryTime in GetResponse (#2899) 2023-06-08 15:32:59 +00:00
azure cosmosdb: return ttlExpiryTime in GetResponse (#2898) 2023-06-08 15:34:25 +00:00
cassandra Metadata YAML for state.cassandra. (#2800) 2023-04-24 10:59:34 -07:00
cloudflare/workerskv [Cloudflare Workers KV] Adds Component Metadata Schema (#2908) 2023-06-19 16:36:30 +00:00
cockroachdb Add conformance tests for etags in Bulk operations (#2843) 2023-05-17 16:28:29 -07:00
couchbase Add conformance tests for etags in Bulk operations (#2843) 2023-05-17 16:28:29 -07:00
etcd Add conformance tests for etags in Bulk operations (#2843) 2023-05-17 16:28:29 -07:00
gcp/firestore GCP Firestore - Certification Tests (#2754) 2023-04-26 22:12:56 +00:00
hashicorp/consul Refactor state store bulk operations (#2747) 2023-04-07 19:19:47 +00:00
hazelcast Refactor state store bulk operations (#2747) 2023-04-07 19:19:47 +00:00
in-memory inmemory return ttlExpiryTime in GetResponse (#2870) 2023-06-06 15:28:08 +00:00
jetstream Refactor state store bulk operations (#2747) 2023-04-07 19:19:47 +00:00
memcached memached: if tllInSeconds larger than 30 days, convert to unix timestamp (#2890) 2023-06-06 15:45:11 +00:00
mongodb MongoDB return ttlExpiryTime in GetResponse (#2888) 2023-06-06 15:18:06 +00:00
mysql mysql: return ttlExpiryTime in GetResponse (#2871) 2023-06-07 20:45:24 +00:00
oci/objectstorage Refactor state store bulk operations (#2747) 2023-04-07 19:19:47 +00:00
oracledatabase oracale: return ttlExpiryTime in GetResponse (#2897) 2023-06-08 15:37:58 +00:00
postgresql Add conformance tests for etags in Bulk operations (#2843) 2023-05-17 16:28:29 -07:00
query Expose parsefilter function 2022-12-27 14:56:11 -03:00
redis Add conformance tests for etags in Bulk operations (#2843) 2023-05-17 16:28:29 -07:00
rethinkdb Remove native/transactional BulkSet/BulkDelete from state stores (#2834) 2023-05-17 14:56:34 -07:00
sqlite sqlite return ttlExpiryTime in GetResponse (#2869) 2023-06-06 08:03:44 -07:00
sqlserver Fixes for SQL Server state store (#2912) 2023-06-19 18:04:46 +00:00
utils 💄 2022-11-22 04:53:24 +00:00
zookeeper Add conformance tests for etags in Bulk operations (#2843) 2023-05-17 16:28:29 -07:00
README.md middleware: changes wasm basic to use waPC (#1833) 2022-09-13 17:12:59 -07:00
bulk.go Remove native/transactional BulkSet/BulkDelete from state stores (#2834) 2023-05-17 14:56:34 -07:00
bulk_test.go Remove native/transactional BulkSet/BulkDelete from state stores (#2834) 2023-05-17 14:56:34 -07:00
errors.go Remove native/transactional BulkSet/BulkDelete from state stores (#2834) 2023-05-17 14:56:34 -07:00
errors_test.go Refactor state store bulk operations (#2747) 2023-04-07 19:19:47 +00:00
feature.go Refactor state store bulk operations (#2747) 2023-04-07 19:19:47 +00:00
metadata.go Refactor state store bulk operations (#2747) 2023-04-07 19:19:47 +00:00
request_options.go Refactor state store bulk operations (#2747) 2023-04-07 19:19:47 +00:00
request_options_test.go Refactor state store bulk operations (#2747) 2023-04-07 19:19:47 +00:00
requests.go Add conformance tests for etags in Bulk operations (#2843) 2023-05-17 16:28:29 -07:00
responses.go Postgresql + cockroachDB return `ttlExpiryTime` in `GetResponse` (#2864) 2023-06-06 15:14:18 +00:00
store.go Refactor state store bulk operations (#2747) 2023-04-07 19:19:47 +00:00

README.md

State Stores

State Stores provide a common way to interact with different data store implementations, and allow users to opt-in to advanced capabilities using defined metadata.

Implementing a new State Store

A compliant state store needs to implement one or more interfaces: Store and TransactionalStore, defined in the store.go file.

See the documentation site for examples.

Implementing State Query API

State Store has an optional API for querying the state.

Please refer to the documentation site for API description and definition.

// Querier is an interface to execute queries.
type Querier interface {
        Query(req *QueryRequest) (*QueryResponse, error)
}

Below are the definitions of structures (including nested) for QueryRequest and QueryResponse.

// QueryResponse is the request object for querying the state.
type QueryRequest struct {
        Query    query.Query       `json:"query"`
        Metadata map[string]string `json:"metadata,omitempty"`
}

type Query struct {
        Filters map[string]interface{} `json:"filter"`
        Sort    []Sorting              `json:"sort"`
        Page    Pagination             `json:"page"`

        // derived from Filters
        Filter Filter
}

type Sorting struct {
        Key   string `json:"key"`
        Order string `json:"order,omitempty"`
}

type Pagination struct {
        Limit int    `json:"limit"`
        Token string `json:"token,omitempty"`
}

// QueryResponse is the response object on querying state.
type QueryResponse struct {
        Results  []QueryItem       `json:"results"`
        Token    string            `json:"token,omitempty"`
        Metadata map[string]string `json:"metadata,omitempty"`
}

// QueryItem is an object representing a single entry in query results.
type QueryItem struct {
        Key   string  `json:"key"`
        Data  []byte  `json:"data"`
        ETag  *string `json:"etag,omitempty"`
        Error string  `json:"error,omitempty"`
}

Upon receiving the query request, Dapr validates it and transforms into object Query, which, in turn, is passed on to the state store component.

The Query object has a member Filter that implements parsing interface per component as described below.

type Filter interface {
	Parse(interface{}) error
}

type FilterEQ struct {
	Key string
	Val interface{}
}

type FilterIN struct {
	Key  string
	Vals []interface{}
}

type FilterAND struct {
	Filters []Filter
}

type FilterOR struct {
	Filters []Filter
}

To simplify the process of query translation, we leveraged visitor design pattern. A state store component developer would need to implement the visit method, and the runtime will use it to construct the native query statement.

type Visitor interface {
	// returns "equal" expression
	VisitEQ(*FilterEQ) (string, error)
	// returns "in" expression
	VisitIN(*FilterIN) (string, error)
	// returns "and" expression
	VisitAND(*FilterAND) (string, error)
	// returns "or" expression
	VisitOR(*FilterOR) (string, error)
	// receives concatenated filters and finalizes the native query
	Finalize(string, *MidQuery) error
}

The Dapr runtime implements QueryBuilder object that takes in Visitor interface and constructs the native query.

type QueryBuilder struct {
	visitor Visitor
}

func (h *QueryBuilder) BuildQuery(mq *MidQuery) error {...}

The last part is to implement Querier interface in the component:

type Querier interface {
	Query(req *QueryRequest) (*QueryResponse, error)
}

A sample implementation might look like that:

func (m *MyComponent) Query(req *state.QueryRequest) (*state.QueryResponse, error) {
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()

	query := &Query{} // Query implements Visitor interface
	qbuilder := state.NewQueryBuilder(query)
	if err := qbuilder.BuildQuery(&req.Query); err != nil {
		return &state.QueryResponse{}, err
	}
	data, token, err := query.execute(ctx)
	if err != nil {
		return &state.QueryResponse{}, err
	}
	return &state.QueryResponse{
		Results:  data,
		Token:    token,
	}, nil
}

Some of the examples of State Query API implementation are MongoDB and CosmosDB state store components.