Skip to content

Commit 79e3953

Browse files
Adds support for metadata in TransactionalStateOperation (#791)
* Adds support for metadata in TransactionalStateOperation Signed-off-by: Elena Kolevska <[email protected]> * Fixes double code Signed-off-by: Elena Kolevska <[email protected]> --------- Signed-off-by: Elena Kolevska <[email protected]>
1 parent 809f80a commit 79e3953

File tree

6 files changed

+63
-1
lines changed

6 files changed

+63
-1
lines changed

dapr/clients/grpc/_request.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ def __init__(
285285
data: Optional[Union[bytes, str]] = None,
286286
etag: Optional[str] = None,
287287
operation_type: TransactionOperationType = TransactionOperationType.upsert,
288+
metadata: Optional[Dict[str, str]] = None,
288289
):
289290
"""Initializes TransactionalStateOperation item from
290291
:obj:`runtime_v1.TransactionalStateOperation`.
@@ -305,6 +306,7 @@ def __init__(
305306
self._data = data # type: ignore
306307
self._etag = etag
307308
self._operation_type = operation_type
309+
self._metadata = metadata
308310

309311
@property
310312
def key(self) -> str:
@@ -326,6 +328,11 @@ def operation_type(self) -> TransactionOperationType:
326328
"""Gets etag."""
327329
return self._operation_type
328330

331+
@property
332+
def metadata(self) -> Dict[str, str]:
333+
"""Gets metadata."""
334+
return {} if self._metadata is None else self._metadata
335+
329336

330337
class EncryptRequestIterator(DaprRequest):
331338
"""An iterator for cryptography encrypt API requests.

dapr/clients/grpc/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ def execute_state_transaction(
939939
key=o.key,
940940
value=to_bytes(o.data) if o.data is not None else to_bytes(''),
941941
etag=common_v1.Etag(value=o.etag) if o.etag is not None else None,
942+
metadata=o.metadata,
942943
),
943944
)
944945
for o in operations

examples/state_store/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ expected_stdout_lines:
4141
- "== APP == Cannot save bulk due to bad etags. ErrorCode=StatusCode.ABORTED"
4242
- "== APP == Got value=b'value_1' eTag=1"
4343
- "== APP == Got items with etags: [(b'value_1_updated', '2'), (b'value_2', '2')]"
44+
- "== APP == Transaction with outbox pattern executed successfully!"
45+
- "== APP == Got value after outbox pattern: b'val1'"
4446
- "== APP == Got values after transaction delete: [b'', b'']"
4547
- "== APP == Got value after delete: b''"
4648
timeout_seconds: 5
4749
-->
4850

4951
```bash
50-
dapr run -- python3 state_store.py
52+
dapr run --resources-path components/ -- python3 state_store.py
5153
```
5254
<!-- END_STEP -->
5355

@@ -68,6 +70,10 @@ The output should be as follows:
6870
6971
== APP == Got items with etags: [(b'value_1_updated', '2'), (b'value_2', '2')]
7072
73+
== APP == Transaction with outbox pattern executed successfully!
74+
75+
== APP == Got value after outbox pattern: b'val1'
76+
7177
== APP == Got values after transaction delete: [b'', b'']
7278
7379
== APP == Got value after delete: b''
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: pubsub
5+
spec:
6+
type: pubsub.redis
7+
version: v1
8+
metadata:
9+
- name: redisHost
10+
value: localhost:6379
11+
- name: redisPassword
12+
value: ""
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: dapr.io/v1alpha1
2+
kind: Component
3+
metadata:
4+
name: statestore
5+
spec:
6+
type: state.redis
7+
version: v1
8+
metadata:
9+
- name: redisHost
10+
value: localhost:6379
11+
- name: redisPassword
12+
value: ""
13+
- name: actorStateStore
14+
value: "true"
15+
- name: outboxPublishPubsub
16+
value: "pubsub"
17+
- name: outboxPublishTopic
18+
value: "test"

examples/state_store/state_store.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@
8888
).items
8989
print(f'Got items with etags: {[(i.data, i.etag) for i in items]}')
9090

91+
# Outbox pattern
92+
# pass in the ("outbox.projection", "true") metadata to the transaction to enable the outbox pattern.
93+
d.execute_state_transaction(
94+
store_name=storeName,
95+
operations=[
96+
TransactionalStateOperation(
97+
key='key1', data='val1', metadata={'outbox.projection': 'false'}
98+
),
99+
TransactionalStateOperation(
100+
key='key1', data='val2', metadata={'outbox.projection': 'true'}
101+
),
102+
],
103+
)
104+
print('Transaction with outbox pattern executed successfully!')
105+
106+
val = d.get_state(store_name=storeName, key='key1').data
107+
print(f'Got value after outbox pattern: {val}')
108+
91109
# Transaction delete
92110
d.execute_state_transaction(
93111
store_name=storeName,

0 commit comments

Comments
 (0)