상태 저장소 (State Store)
이 문서는 프리뷰 기능에 대한 문서예요.
상태 저장소 컴포넌트를 만드는 데는 몇 가지 기본 단계만 거치면 돼요.
출처: Dapr 문서
본문
상태 저장소 네임스페이스 추가하기
상태 저장소와 관련된 네임스페이스에 대한 using 문을 추가해요.
using Dapr.PluggableComponents.Components;
using Dapr.PluggableComponents.Components.StateStore;
IStateStore 구현하기
IStateStore 인터페이스를 구현하는 클래스를 만들어요.
internal sealed class MyStateStore : IStateStore
{
public Task DeleteAsync(StateStoreDeleteRequest request, CancellationToken cancellationToken = default)
{
// Delete the requested key from the state store...
}
public Task<StateStoreGetResponse?> GetAsync(StateStoreGetRequest request, CancellationToken cancellationToken = default)
{
// Get the requested key value from from the state store, else return null...
}
public Task InitAsync(MetadataRequest request, CancellationToken cancellationToken = default)
{
// Called to initialize the component with its configured metadata...
}
public Task SetAsync(StateStoreSetRequest request, CancellationToken cancellationToken = default)
{
// Set the requested key to the specified value in the state store...
}
}
상태 저장소 컴포넌트 등록하기
메인 프로그램 파일(예: Program.cs)에서 애플리케이션 서비스에 상태 저장소를 등록해요.
using Dapr.PluggableComponents;
var app = DaprPluggableComponentsApplication.Create();
app.RegisterService(
"<socket name>",
serviceBuilder =>
{
serviceBuilder.RegisterStateStore<MyStateStore>();
});
app.Run();
벌크(bulk) 상태 저장소
벌크 연산을 지원하려는 상태 저장소는 선택적 IBulkStateStore 인터페이스를 구현해야 해요. 그 메서드들은 기본 IStateStore 인터페이스의 메서드를 그대로 따르지만, 여러 개의 요청된 값을 포함해요.
참고
Dapr 런타임은 IBulkStateStore를 구현하지 않는 상태 저장소에 대해서는 연산을 개별적으로 호출해 벌크 상태 저장소 연산을 에뮬레이션할 거예요.
internal sealed class MyStateStore : IStateStore, IBulkStateStore
{
// ...
public Task BulkDeleteAsync(StateStoreDeleteRequest[] requests, CancellationToken cancellationToken = default)
{
// Delete all of the requested values from the state store...
}
public Task<StateStoreBulkStateItem[]> BulkGetAsync(StateStoreGetRequest[] requests, CancellationToken cancellationToken = default)
{
// Return the values of all of the requested values from the state store...
}
public Task BulkSetAsync(StateStoreSetRequest[] requests, CancellationToken cancellationToken = default)
{
// Set all of the values of the requested keys in the state store...
}
}
트랜잭션 상태 저장소
트랜잭션을 지원하려는 상태 저장소는 선택적 ITransactionalStateStore 인터페이스를 구현해야 해요. 그것의 TransactAsync() 메서드는 트랜잭션 내에서 수행할 delete 및/또는 set 연산의 시퀀스가 담긴 요청을 전달받아요. 상태 저장소는 그 시퀀스를 순회하며 각 연산의 Visit() 메서드를 호출하는데, 각 연산 유형에 대해 취할 동작을 나타내는 콜백을 전달해요.
internal sealed class MyStateStore : IStateStore, ITransactionalStateStore
{
// ...
public async Task TransactAsync(StateStoreTransactRequest request, CancellationToken cancellationToken = default)
{
// Start transaction...
try
{
foreach (var operation in request.Operations)
{
await operation.Visit(
async deleteRequest =>
{
// Process delete request...
},
async setRequest =>
{
// Process set request...
});
}
}
catch
{
// Rollback transaction...
throw;
}
// Commit transaction...
}
}
쿼리 가능한 상태 저장소
쿼리를 지원하려는 상태 저장소는 선택적 IQueryableStateStore 인터페이스를 구현해야 해요. 그것의 QueryAsync() 메서드는 쿼리에 대한 세부 정보(예: 필터, 결과 한도 및 페이지네이션, 결과의 정렬 순서)를 전달받아요. 상태 저장소는 그 세부 정보를 사용해 응답의 일부로 반환할 값 집합을 생성해야 해요.
internal sealed class MyStateStore : IStateStore, IQueryableStateStore
{
// ...
public Task<StateStoreQueryResponse> QueryAsync(StateStoreQueryRequest request, CancellationToken cancellationToken = default)
{
// Generate and return results...
}
}
ETag 및 기타 의미 오류 처리
Dapr 런타임은 일부 상태 저장소 연산에서 발생하는 특정 오류 조건에 대한 추가 처리를 수행해요. 상태 저장소는 그 연산 로직에서 특정 예외를 던져 그러한 조건을 나타낼 수 있어요.
| 예외 | 적용 가능한 연산 | 설명 | | ETagInvalidException | Delete, Set, Bulk Delete, Bulk Set | ETag가 유효하지 않을 때 | | ETagMismatchException | Delete, Set, Bulk Delete, Bulk Set | ETag가 기대한 값과 일치하지 않을 때 | | BulkDeleteRowMismatchException | Bulk Delete | 영향을 받은 행 수가 기대한 행 수와 일치하지 않을 때 |
다음 단계
- 플러그형 컴포넌트 .NET SDK의 고급 단계 알아보기
- 다음 용도로 플러그형 컴포넌트 .NET SDK를 사용하는 방법 알아보기:바인딩
- Pub/sub