상태 저장소 (State Store)
이 문서는 프리뷰 기능에 대한 문서예요.
상태 저장소 컴포넌트를 만드는 데는 몇 가지 기본 단계만 거치면 돼요.
출처: Dapr 문서
본문
상태 저장소 패키지 가져오기
components/statestore.go 파일을 만들고 상태 저장소 관련 패키지에 대한 import 문을 추가해요.
package components
import (
"context"
"github.com/dapr/components-contrib/state"
)
Store 인터페이스 구현하기
Store 인터페이스를 구현하는 타입을 만들어요.
type MyStateStore struct {
}
func (store *MyStateStore) Init(metadata state.Metadata) error {
// Called to initialize the component with its configured metadata...
}
func (store *MyStateStore) GetComponentMetadata() map[string]string {
// Not used with pluggable components...
return map[string]string{}
}
func (store *MyStateStore) Features() []state.Feature {
// Return a list of features supported by the state store...
}
func (store *MyStateStore) Delete(ctx context.Context, req *state.DeleteRequest) error {
// Delete the requested key from the state store...
}
func (store *MyStateStore) Get(ctx context.Context, req *state.GetRequest) (*state.GetResponse, error) {
// Get the requested key value from the state store, else return an empty response...
}
func (store *MyStateStore) Set(ctx context.Context, req *state.SetRequest) error {
// Set the requested key to the specified value in the state store...
}
func (store *MyStateStore) BulkGet(ctx context.Context, req []state.GetRequest) (bool, []state.BulkGetResponse, error) {
// Get the requested key values from the state store...
}
func (store *MyStateStore) BulkDelete(ctx context.Context, req []state.DeleteRequest) error {
// Delete the requested keys from the state store...
}
func (store *MyStateStore) BulkSet(ctx context.Context, req []state.SetRequest) error {
// Set the requested keys to their specified values in the state store...
}
상태 저장소 컴포넌트 등록하기
메인 애플리케이션 파일(예: main.go)에서 애플리케이션 서비스에 상태 저장소를 등록해요.
package main
import (
"example/components"
dapr "github.com/dapr-sandbox/components-go-sdk"
"github.com/dapr-sandbox/components-go-sdk/state/v1"
)
func main() {
dapr.Register("<socket name>", dapr.WithStateStore(func() state.Store {
return &components.MyStateStoreComponent{}
}))
dapr.MustRun()
}
벌크(bulk) 상태 저장소
상태 저장소는 벌크 연산을 지원해야 하지만, 그 구현은 개별 연산 메서드에 순차적으로 위임해요.
트랜잭션 상태 저장소
트랜잭션을 지원하려는 상태 저장소는 선택적 TransactionalStore 인터페이스를 구현해야 해요. 그것의 Multi() 메서드는 트랜잭션 내에서 수행할 delete 및/또는 set 연산의 시퀀스가 담긴 요청을 전달받아요. 상태 저장소는 그 시퀀스를 순회하며 각 연산을 적용해야 해요.
func (store *MyStateStoreComponent) Multi(ctx context.Context, request *state.TransactionalStateRequest) error {
// Start transaction...
for _, operation := range request.Operations {
switch operation.Operation {
case state.Delete:
deleteRequest := operation.Request.(state.DeleteRequest)
// Process delete request...
case state.Upsert:
setRequest := operation.Request.(state.SetRequest)
// Process set request...
}
}
// End (or rollback) transaction...
return nil
}
쿼리 가능한 상태 저장소
쿼리를 지원하려는 상태 저장소는 선택적 Querier 인터페이스를 구현해야 해요. 그것의 Query() 메서드는 쿼리에 대한 세부 정보(예: 필터, 결과 한도, 페이지네이션, 결과의 정렬 순서)를 전달받아요. 상태 저장소는 그 세부 정보를 사용해 응답의 일부로 반환할 값 집합을 생성해요.
func (store *MyStateStoreComponent) Query(ctx context.Context, req *state.QueryRequest) (*state.QueryResponse, error) {
// Generate and return results...
}
ETag 및 기타 의미 오류 처리
Dapr 런타임은 일부 상태 저장소 연산에서 발생하는 특정 오류 조건에 대한 추가 처리를 수행해요. 상태 저장소는 그 연산 로직에서 특정 오류를 반환해 그러한 조건을 나타낼 수 있어요.
| 오류 | 적용 가능한 연산 | 설명 | | NewETagError(state.ETagInvalid, ...) | Delete, Set, Bulk Delete, Bulk Set | ETag가 유효하지 않을 때 | | NewETagError(state.ETagMismatch, ...) | Delete, Set, Bulk Delete, Bulk Set | ETag가 기대한 값과 일치하지 않을 때 | | NewBulkDeleteRowMismatchError(...) | Bulk Delete | 영향을 받은 행 수가 기대한 행 수와 일치하지 않을 때 |
다음 단계
- 플러그형 컴포넌트 Go SDK의 고급 기법
- 다음 구현 방법 더 알아보기:바인딩
- Pub/sub