dapr / components-contrib

Community driven, reusable components for distributed apps
Apache License 2.0
548 stars 480 forks source link

Cosmos DB state store not setting partitionKey on state transactions #2880

Closed JonathanLydall closed 1 year ago

JonathanLydall commented 1 year ago

Expected Behavior

When using a transaction to upsert to a Cosmos DB state store, the partitionKey field should not be blank.

Actual Behavior

An example of the document once saved:

{
    "id": "orderprocessing||order_1",
    "value": "250",
    "isBinary": false,
    "partitionKey": "",
    "Etag": "",
    "_rid": "zXQ1AO0LbbEBAAAAAAAAAA==",
    "_self": "dbs/zXQ1AA==/colls/zXQ1AO0LbbE=/docs/zXQ1AO0LbbEBAAAAAAAAAA==/",
    "_etag": "\"0300cc6a-0000-3000-0000-64789c4d0000\"",
    "_attachments": "attachments/",
    "_ts": 1685625933
}

Steps to Reproduce the Problem

I first noticed the problem when trying with the .NET SDK, but it's easily reproduceable with PowerShell.

Change your state store component configuration to the following (which is configured for the Cosmos DB emulator, but the same issue occurs with a regular Azure Cosmos DB instance):

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: statestore
spec:
  type: state.azure.cosmosdb
  version: v1
  metadata:
  - name: url
    value: https://localhost:8081
  - name: masterKey
    value: C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw== # local
  - name: database
    value: DaprDatabase
  - name: collection
    value: DaprContainer

Start a Dapr side car:

dapr --app-id orderprocessing --dapr-http-port 3601 run

Run the following:

Invoke-RestMethod -Method Post -ContentType 'application/json' -Body '{"operations": [{"operation":"upsert", "request": {"key": "order_1", "value": "250"}}, {"operation":"upsert", "request": {"key": "order_2", "value": "250"}}]}' -Uri 'http://localhost:3601/v1.0/state/statestore/transaction'

I've also tried the following (I would actually expect the above to fail with the partition keys being different):

Invoke-RestMethod -Method Post -ContentType 'application/json' -Body '{"operations": [{"operation":"upsert", "request": {"key": "order_1", "value": "250"}, "metadata": {"partitionKey": "orderGroup1"}}, {"operation":"upsert", "request": {"key": "order_2", "value": "250"}, "metadata": {"partitionKey": "orderGroup1"}}]}' -Uri 'http://localhost:3601/v1.0/state/statestore/transaction'

Release Note

RELEASE NOTE: FIX Bug in runtime.

ItalyPaleAle commented 1 year ago

Thanks for reporting this! This is already being fixed in Dapr 1.11.0