diff --git a/daprdocs/content/en/developing-applications/building-blocks/actors/actors-overview.md b/daprdocs/content/en/developing-applications/building-blocks/actors/actors-overview.md index fec0bfe8aaa..813e6c2f333 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/actors/actors-overview.md +++ b/daprdocs/content/en/developing-applications/building-blocks/actors/actors-overview.md @@ -104,7 +104,7 @@ The Dapr actor runtime provides a simple turn-based access model for accessing a ### State -Transactional state stores can be used to store actor state. Regardless of whether you intend to store any state in your actor, you must specify a value for property `actorStateStore` as `true` in the state store component's metadata section. Actors state is stored with a specific scheme in transactional state stores, allowing for consistent querying. Only a single state store component can be used as the state store for all actors. Read the [state API reference]({{% ref state_api %}}) and the [actors API reference]({{% ref actors_api %}}) to learn more about state stores for actors. +Transactional state stores can be used to store actor state. Regardless of whether you intend to store any state in your actor, you must specify a value for property `actorStateStore` as `true` in the state store component's metadata section. Actors state is stored with a specific scheme in transactional state stores, allowing for consistent querying. Only a single state store component can be marked as the actor state store, and it keeps the state of all actor types by default. To keep the state of specific actor types in other state stores, [bind them in the Configuration]({{% ref actors-state-store-binding.md %}}). Read the [state API reference]({{% ref state_api %}}) and the [actors API reference]({{% ref actors_api %}}) to learn more about state stores for actors. {{% alert title="Tip" color="primary" %}} During local development, use the [Dapr Dev Dashboard]({{% ref dapr-dev-dashboard.md %}}) to inspect the actor types of your running apps and the number of active actors per type, and to browse the actor state in the state store. diff --git a/daprdocs/content/en/developing-applications/building-blocks/actors/actors-state-store-binding.md b/daprdocs/content/en/developing-applications/building-blocks/actors/actors-state-store-binding.md new file mode 100644 index 00000000000..8e7a55068e5 --- /dev/null +++ b/daprdocs/content/en/developing-applications/building-blocks/actors/actors-state-store-binding.md @@ -0,0 +1,200 @@ +--- +type: docs +title: "Bind actor types to state stores" +linkTitle: "State store binding" +weight: 45 +description: "Keep the state of specific actor types in their own state stores" +--- + +By default, the state of every actor type is kept in the single state store marked with `actorStateStore: "true"`. With `spec.actors.stateStores` in a [Configuration]({{% ref configuration-schema.md %}}), you can bind actor types to other state stores, so that each bound actor type keeps its state in its own store. + +This is useful when actor types have different load or data profiles. For example, you can keep a high-write `TransactionActor` and a read-mostly `BalanceActor` in separate stores, so that one does not slow down the other, or so that each can be tuned and scaled independently. + +## Configure the bindings + +Declare the state store components as usual. Exactly one of them keeps `actorStateStore: "true"`: it is the default actor state store, and it is still required. + +```yaml +apiVersion: dapr.io/v1alpha1 +kind: Component +metadata: + name: actorstore +spec: + type: state.redis + version: v1 + metadata: + - name: redisHost + value: localhost:6379 + - name: actorStateStore + value: "true" +--- +apiVersion: dapr.io/v1alpha1 +kind: Component +metadata: + name: wallet-transactions +spec: + type: state.postgresql + version: v2 + metadata: + - name: connectionString + value: "" +--- +apiVersion: dapr.io/v1alpha1 +kind: Component +metadata: + name: wallet-balance +spec: + type: state.redis + version: v1 + metadata: + - name: redisHost + value: balance-redis:6379 +``` + +Then bind the actor types to the stores in the Configuration of the app that hosts them: + +```yaml +apiVersion: dapr.io/v1alpha1 +kind: Configuration +metadata: + name: wallet +spec: + actors: + stateStores: + - name: wallet-transactions + actorTypes: ["TransactionActor", "AuditActor"] + - name: wallet-balance + actorTypes: ["BalanceActor"] +``` + +| Field | Required | Description | +|-------|:--------:|-------------| +| `stateStores` | N | List of bindings. | +| `stateStores[].name` | Y | Name of the state store component that keeps the state of the listed actor types. | +| `stateStores[].actorTypes` | Y | Actor types whose state is kept in this store. | + +With this configuration, `TransactionActor` and `AuditActor` keep their state in `wallet-transactions`, `BalanceActor` in `wallet-balance`, and every other actor type in `actorstore`. Without `spec.actors.stateStores`, nothing changes. + +The bindings do not change the [actors API]({{% ref actors_api %}}) or the actor SDKs: apps keep reading and writing actor state as before, and Dapr routes each request to the store bound to its actor type. + +## Behavior + +- **No fallback.** A bound actor type uses only its bound store. If that store is not loaded, the actor type's state operations fail with `ERR_ACTOR_STATE_STORE_NOT_FOUND` (HTTP `500`, gRPC `Internal`), whose message names the actor type and the store. Its state is never written to the default actor state store. +- **The default actor state store is still required.** Actor hosting, placement, reminders and workflows depend on it, as before, even when every actor type is bound. +- **Startup warning.** If a bound store is not loaded when the sidecar starts, Dapr logs a warning such as `Actor type TransactionActor is bound to state store wallet-transactions, which is not loaded`, and keeps starting. +- **Hot reloading.** When [hot reloading]({{% ref "component-updates.md" %}}) is enabled, adding, updating or removing a bound state store component takes effect without restarting the sidecar. Removing it fails only the actor types bound to it; adding it back makes their existing state available again. Changing `spec.actors.stateStores` itself is a Configuration change, which gracefully restarts the sidecar like any other. +- **Actors are deactivated when their bound store is removed or reloaded.** So that no actor keeps state it read from the previous store, Dapr deactivates the active actors of the actor types bound to the store. In-flight calls to those actors are drained as in a rebalance: with `drainRebalancedActors` enabled, they get `drainOngoingCallTimeout` to complete. The actors reactivate on their next call. Adding a bound store that was not loaded deactivates nothing. +- **Timers do not survive a reload of the bound store.** [Actor timers]({{% ref "actors-timers-reminders.md" %}}) of the deactivated actors are deleted, and nothing registers them again. If an actor of a bound type needs periodic work to continue across a reload, register its timers again when it activates, or use reminders, which are persisted. +- **Store requirements.** Like the default actor state store, a bound store must support transactions and ETags, for every state operation including reads. See the [state store components]({{% ref supported-state-stores %}}) for the stores that support both. + +## Validation + +The sidecar refuses to start if `spec.actors.stateStores` is not valid: + +- Each binding must have a `name` and at least one non-empty actor type. +- An actor type can be listed only once in a binding, and can be bound to only one state store. +- Internal actor types, such as the ones Dapr uses for workflows (`dapr.internal.*`), cannot be listed. To bind the workflow actor types, use [`spec.workflow.stateStore`](#bind-workflow-state-to-a-state-store). + +On Kubernetes, the Configuration resource also rejects an empty `name`, an empty `actorTypes` list and an empty actor type when it is applied. + +## Bind workflow state to a state store + +[Dapr Workflow]({{% ref workflow-overview.md %}}) keeps its state in internal actors. With `spec.workflow.stateStore`, you bind all the workflow actor types of an app to one state store, without naming them: + +```yaml +apiVersion: dapr.io/v1alpha1 +kind: Configuration +metadata: + name: wallet +spec: + workflow: + stateStore: wallet-workflows +``` + +The state of the app's workflows and activities is then kept in `wallet-workflows`, and listing and purging workflow instances use that store too. You can combine it with `spec.actors.stateStores` in the same Configuration, to keep workflow state apart from the state of your own actors. + +The behavior described above applies, with these differences: + +- A workflow's state moves as one unit. You cannot keep parts of it, such as its history, in a different store. +- Workflow operations fail with an error that names the store when the bound store is not loaded, or does not support transactions and ETags. +- In a [multi-app workflow]({{% ref workflow-multi-app.md %}}), each app keeps the state of its own workflows and activities in the store of its own Configuration. + +{{% alert title="Warning" color="warning" %}} +Setting or changing `spec.workflow.stateStore` does not migrate state. The workflows started with another store stay in that store, where Dapr no longer reads, lists or purges them: they are not found and do not continue. Set or change it only when the app has no workflows in progress. +{{% /alert %}} + +## Roll out a binding + +Each sidecar applies the bindings of its own Configuration, and the placement service does not know about them. Follow these rules to keep the replicas of an app consistent: + +- **Use the same bindings on every replica of an app.** If replicas disagree, for example during a rolling update that changes a binding, an actor that moves between them reads and writes a different store. +- **Binding an actor type that has no state yet is safe.** Binding or rebinding an actor type that already has state does not migrate it: stop all the replicas of the app and copy the state to the new store first. Actor state keys do not depend on the store, so the keys can be copied as they are. +- **On Kubernetes, upgrade the Configuration CRD before using the fields.** With a CRD from a Dapr version that does not have them, Kubernetes drops the fields when the Configuration is applied, and every actor type uses the default actor state store. Helm does not upgrade CRDs on its own: see [upgrading Dapr on Kubernetes]({{% ref kubernetes-upgrade.md %}}). +- **A sidecar from a Dapr version without this feature ignores the bindings** and uses the default actor state store. Take this into account before rolling back. + +## Verify the bindings + +The [metadata API]({{% ref metadata_api.md %}}) reports the bindings that the sidecar applies under `actorRuntime.stateStores`, with the actor types of each store and whether the store is loaded: + +```shell +curl http://localhost:3500/v1.0/metadata +``` + +```json +{ + "actorRuntime": { + "runtimeStatus": "RUNNING", + "hostReady": true, + "placement": "placement: connected", + "stateStores": [ + { + "name": "wallet-balance", + "actorTypes": ["BalanceActor"], + "loaded": false + }, + { + "name": "wallet-transactions", + "actorTypes": ["AuditActor", "TransactionActor"], + "loaded": true + } + ] + } +} +``` + +A binding that is missing from the list was not applied, for example because the Configuration CRD is outdated. A store with `"loaded": false` is not loaded, and the state operations of its actor types fail. With `spec.workflow.stateStore`, the list includes the store with the internal workflow actor types of the app. + +## Separate actor types within one database server + +A binding names a state store component, and a component already defines where its data is kept. To separate actor types inside one server, declare one component per table, collection, database or prefix: + +| State store | Setting to separate | What is separated | +|-------------|---------------------|-------------------| +| Azure Cosmos DB | `database`, `collection` | Container | +| AWS DynamoDB | `table` | Table | +| CockroachDB | `tableName` | Table | +| etcd | `keyPrefixPath` | Key prefix | +| MongoDB | `databaseName`, `collectionName` | Database or collection | +| MySQL and MariaDB | `tableName`, `schemaName` | Table or schema | +| Oracle Database | `tableName` | Table | +| PostgreSQL v1 | `tableName` | Table | +| PostgreSQL v2 | `tablePrefix` | Table | +| RavenDB | `DatabaseName` | Database | +| Redis | `redisDB` | Key namespace | +| SQL Server | `tableName`, `schemaName` | Table or schema | +| SQLite | `tableName`, or a separate file | Table or file | + +How much this isolates depends on the store. Separate tables or collections have their own indexes and storage. A key prefix or a Redis database only organizes the keys. For full isolation, point each component at a different server. + +Keep in mind: + +- **On SQL stores, give each component its own `metadataTableName`.** PostgreSQL v1 and SQLite record the schema migrations of a component under a fixed key of the metadata table, so a second component on the same metadata table does not create its state table. PostgreSQL v1, MySQL, SQL Server and SQLite also keep one time-to-live cleanup record per metadata table. PostgreSQL v2 includes `tablePrefix` in both keys, so its components can share the metadata table. +- **Each component opens its own connections.** Account for the number of components times the number of sidecars in the connection limits of the server. + +## Related links + +- [Actors overview]({{% ref actors-overview.md %}}) +- [Configuration spec]({{% ref configuration-schema.md %}}) +- [Metadata API reference]({{% ref metadata_api.md %}}) +- [Workflow architecture]({{% ref workflow-architecture.md %}}) +- [Error codes reference]({{% ref error-codes-reference.md %}}) diff --git a/daprdocs/content/en/developing-applications/building-blocks/workflow/workflow-architecture.md b/daprdocs/content/en/developing-applications/building-blocks/workflow/workflow-architecture.md index 5c8513edddf..7041cbc1c4e 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/workflow/workflow-architecture.md +++ b/daprdocs/content/en/developing-applications/building-blocks/workflow/workflow-architecture.md @@ -173,6 +173,8 @@ Like any actors, these workflow actors store their state in the configured actor This is done by specifying a state store component in your Dapr configuration and then referencing that state store in the `actorStateStore` property of the configuration's `actors` section. Read the [state API reference]({{% ref state_api %}}) and the [actors API reference]({{% ref actors_api %}}) to learn more about state stores for actors. +To keep workflow state in a state store other than the default actor state store, set `spec.workflow.stateStore` in the app's Configuration. See [Bind workflow state to a state store]({{% ref "actors-state-store-binding.md#bind-workflow-state-to-a-state-store" %}}). + Any state store that supports actors implicitly supports Dapr Workflow. As discussed in the [workflow actors]({{% ref "workflow-architecture.md#workflow-actors" %}}) section, workflows save their state incrementally by appending to a history log. diff --git a/daprdocs/content/en/developing-applications/error-codes/error-codes-reference.md b/daprdocs/content/en/developing-applications/error-codes/error-codes-reference.md index 45c3d314d52..2cf9d8db3a3 100644 --- a/daprdocs/content/en/developing-applications/error-codes/error-codes-reference.md +++ b/daprdocs/content/en/developing-applications/error-codes/error-codes-reference.md @@ -18,6 +18,7 @@ An effort is underway to enrich all gRPC error responses according to the [Riche | `ERR_ACTOR_INVOKE_METHOD` | | Error invoking actor method | | `ERR_ACTOR_RUNTIME_NOT_FOUND` | | Actor runtime not found | | `ERR_ACTOR_STATE_GET` | | Error getting actor state | +| `ERR_ACTOR_STATE_STORE_NOT_FOUND` | | The state store bound to the actor type in `spec.actors.stateStores` is not loaded | | `ERR_ACTOR_STATE_TRANSACTION_SAVE` | | Error saving actor transaction | | `ERR_ACTOR_REMINDER_CREATE` | | Error creating actor reminder | | `ERR_ACTOR_REMINDER_DELETE` | | Error deleting actor reminder | diff --git a/daprdocs/content/en/operations/configuration/configuration-overview.md b/daprdocs/content/en/operations/configuration/configuration-overview.md index 13bba092472..fa169bba4a0 100644 --- a/daprdocs/content/en/operations/configuration/configuration-overview.md +++ b/daprdocs/content/en/operations/configuration/configuration-overview.md @@ -70,6 +70,7 @@ The following menu includes all of the configuration settings you can set: - [Middleware](#middleware) - [Name resolution](#name-resolution) - [Workflow](#workflow) +- [Actors](#actors) - [Scope secret store access](#scope-secret-store-access) - [Access Control allow lists for building block APIs](#access-control-allow-lists-for-building-block-apis) - [Access Control allow lists for service invocation API](#access-control-allow-lists-for-service-invocation-api) @@ -285,6 +286,25 @@ The `workflow` section contains properties for configuring [Workflows]({{% ref " | `globalMaxConcurrentActivityInvocations` | int32 | Maximum concurrent activity executions across all replicas, enforced by the scheduler. Default is unlimited. | | `workflowConcurrencyLimits` | array | Per-workflow-name concurrency limits across all replicas. Each entry has `name` (string) and `maxConcurrent` (int32). | | `activityConcurrencyLimits` | array | Per-activity-name concurrency limits across all replicas. Each entry has `name` (string) and `maxConcurrent` (int32). | +| `stateStore` | string | Name of the state store component that keeps the state of the app's workflows, instead of the default actor state store. Setting or changing it does not migrate state. See [Bind workflow state to a state store]({{% ref "actors-state-store-binding.md#bind-workflow-state-to-a-state-store" %}}). | + +#### Actors + +The `actors` section contains properties for configuring [Actors]({{% ref "actors-overview.md" %}}). + +| Property | Type | Description | +|------------------|--------|-----| +| `stateStores` | array | Binds actor types to state stores other than the default actor state store. Each entry has `name` (string, the state store component) and `actorTypes` (array of strings). See [Bind actor types to state stores]({{% ref "actors-state-store-binding.md" %}}). | + +```yaml +spec: + actors: + stateStores: + - name: wallet-transactions + actorTypes: ["TransactionActor", "AuditActor"] + - name: wallet-balance + actorTypes: ["BalanceActor"] +``` #### Scope secret store access diff --git a/daprdocs/content/en/reference/api/metadata_api.md b/daprdocs/content/en/reference/api/metadata_api.md index ed77aca1b6c..6543207fbe5 100644 --- a/daprdocs/content/en/reference/api/metadata_api.md +++ b/daprdocs/content/en/reference/api/metadata_api.md @@ -40,6 +40,9 @@ The metadata API returns information related to Dapr's connection to the app. Th ### Scheduler connection details Information related to the connection to one or more scheduler hosts. +### Actor runtime details +Information related to the actor runtime: its status, the count of active actors per type, the placement connection, and the state stores that [actor types are bound to]({{% ref actors-state-store-binding.md %}}). + ### Workflow API runtime details Information related to the Workflow API runtime details. @@ -90,6 +93,7 @@ subscriptions | [Metadata API Response Subscription](#metadataapirespon appConnectionProperties| [Metadata API Response AppConnectionProperties](#metadataapiresponseappconnectionproperties) | A json encoded object of app connection properties. scheduler | [Metadata API Response Scheduler](#metadataapiresponsescheduler) | A json encoded object of scheduler connection properties. workflows | [Metadata API Response Workflows](#metadataapiresponseworkflows) | A json encoded object of workflows runtime properties +actorRuntime | [Metadata API Response Actor Runtime](#metadataapiresponseactorruntime) | A json encoded object of actor runtime properties. **Metadata API Response Registered Actor** @@ -162,6 +166,24 @@ Name | Type | Description ---- | ---- | ----------- connectedWorkers | integer | Number of connected workflow workers. +**Metadata API Response Actor Runtime** + +Name | Type | Description +---- | ---- | ----------- +runtimeStatus | string | Status of the actor runtime: `INITIALIZING`, `DISABLED` or `RUNNING`. +activeActors | [Metadata API Response Registered Actor](#metadataapiresponseactor)[] | Count of active actors per actor type. +hostReady | boolean | Whether the actor runtime is ready to host actors. +placement | string | Status of the connection to the placement service. +stateStores | [Metadata API Response Actor Runtime State Store](#metadataapiresponseactorruntimestatestore)[] | State stores bound to actor types by `spec.actors.stateStores` and `spec.workflow.stateStore` in the Configuration, sorted by name. Actor types that are not listed use the default actor state store. + +**Metadata API Response Actor Runtime State Store** + +Name | Type | Description +---- | ---- | ----------- +name | string | Name of the state store component. +actorTypes | string[] | Actor types whose state is kept in this state store. +loaded | boolean | Whether the state store component is loaded. State operations of the actor types fail while it is not. + ### Examples ```shell @@ -244,6 +266,25 @@ curl http://localhost:3500/v1.0/metadata }, "workflows": { "connectedWorkers": 1 + }, + "actorRuntime": { + "runtimeStatus": "RUNNING", + "activeActors": [ + { + "type": "DemoActor" + } + ], + "hostReady": true, + "placement": "placement: connected", + "stateStores": [ + { + "name": "demo-actors", + "actorTypes": [ + "DemoActor" + ], + "loaded": true + } + ] } } ``` diff --git a/daprdocs/content/en/reference/resource-specs/configuration-schema.md b/daprdocs/content/en/reference/resource-specs/configuration-schema.md index ab9b4a2a926..5a639a6133e 100644 --- a/daprdocs/content/en/reference/resource-specs/configuration-schema.md +++ b/daprdocs/content/en/reference/resource-specs/configuration-schema.md @@ -83,6 +83,13 @@ spec: features: - name: enabled: + actors: + stateStores: + - name: + actorTypes: + - + workflow: + stateStore: accessControl: defaultAction: trustDomain: @@ -102,6 +109,7 @@ spec: | Field | Required | Details | Example | |--------------------|:--------:|---------|---------| | accessControl | N | Applied to Dapr sidecar for the called application. Enables the configuration of policies that restrict what operations calling applications can perform (via service invocation) on the called appliaction. | [Learn more about the `accessControl` configuration.]({{% ref invoke-allowlist.md %}}) | +| actors | N | Configure actors, such as binding actor types to state stores other than the default actor state store. | [Learn more about the `actors` configuration.]({{% ref actors-state-store-binding.md %}}) | | api | N | Used to enable only the Dapr sidecar APIs used by the application. | [Learn more about the `api` configuration.]({{% ref api-allowlist.md %}}) | | httpPipeline | N | Configure API middleware pipelines | [Middleware pipeline configuration overview]({{% ref "configuration-overview.md#middleware" %}})
[Learn more about the `httpPipeline` configuration.]({{% ref "middleware.md#configure-api-middleware-pipelines" %}}) | | appHttpPipeline | N | Configure application middleware pipelines | [Middleware pipeline configuration overview]({{% ref "configuration-overview.md#middleware" %}})
[Learn more about the `appHttpPipeline` configuration.]({{% ref "middleware.md#configure-app-middleware-pipelines" %}}) | @@ -112,6 +120,7 @@ spec: | nameResolution | N | Name resolution configuration spec for the service invocation building block. | [Learn more about the `nameResolution` configuration per components.]({{% ref supported-name-resolution.md %}}) | | secrets | N | Limit the secrets to which your Dapr application has access. | [Learn more about the `secrets` configuration.]({{% ref secret-scope.md %}}) | | tracing | N | Turns on tracing for an application. | [Learn more about the `tracing` configuration.]({{% ref "configuration-overview.md#tracing" %}}) | +| workflow | N | Configure workflows, such as concurrency limits and the state store that keeps workflow state. | [Learn more about the `workflow` configuration.]({{% ref "configuration-overview.md#workflow" %}}) | ## Control plane format