From 49798ec0b503298ad13fbbc7283aecf39eb0abfe Mon Sep 17 00:00:00 2001 From: Haishi2016 Date: Thu, 3 Oct 2019 14:17:49 -0700 Subject: [PATCH 1/3] how-to: query Redis store --- howto/query-state-store/query-redis-store.md | 68 ++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 howto/query-state-store/query-redis-store.md diff --git a/howto/query-state-store/query-redis-store.md b/howto/query-state-store/query-redis-store.md new file mode 100644 index 00000000000..0a16e8f806d --- /dev/null +++ b/howto/query-state-store/query-redis-store.md @@ -0,0 +1,68 @@ +# Query Redis data store + +Dapr doesn't transform state values while saving and retriving states. And Dapr requires all data store implementations abidding to certain key format scheme (see [Dapr state management spec](https://github.com/dapr/spec/blob/master/state.md)). You can directly interact with the underlying store to manipulate the state data, such querying states, creating aggregated views and making backups. + +>**NOTE:** The following samples uses Redis CLI against a Redis store using the default Dapr state store implementation. + +## 1. List keys by Dapr id + +To get all state keys associated with a Dapr id "myapp", use command: + +```bash +KEYS myapp* +``` + +The above command returns a list of existing keys: +```bash +1) "myapp-balance" +2) "myapp-amount" +``` + +## 2. Get specific state data + +Dapr saves state values as hash values. Each hash value contains a "data" field, which contains the state data; and a "version" field, wich contains an ever-incrementing version serving as the ETag. + +To get the state data by a key "myapp-balance", use command: + +```bash +HGET myapp-balance data +``` + +To get the state version/ETag, use command: +```bash +HGET myapp-balance version +``` +## 3. Read Actor state + +To get all state keys associated with an Actor instance "leroy" of Actor type "cat" belonging to Dapr id "mypets", use command: + +```bash +KEYS mypets-cat-leroy* +``` +And to get a specific Actor state such as "food", use command: + +```bash +HGET mypets-cat-leroy-food value +``` + +## 4. Delete state + +> **WARNING:** You should not manually update or delete states. The following instructions are provided for rare cases that you have to manually intervene. + +To delete a state by the key "myapp-balance", use command: +```bash +DEL myapp-balance +``` +## 5. Update state + +> **WARNING:** You should not manually update or delete states. The following instructions are provided for rare cases that you have to manually intervene. + +When you manually update a state, you should maintain the associated version together with your state updates as a single transaction. This can be done by [running a Lua script](https://redis.io/commands/eval). + +For example, the following command updates the state associated with the key "dapr1-mystate" to "new state", if the current ETag value in Redis is "3". + +```bash +EVAL "local var1 = redis.pcall(\"HGET\", KEYS[1], \"version\"); if type(var1) == \"table\" then redis.call(\"DEL\", KEYS[1]); end; if not var1 or type(var1)==\"table\" or var1 == \"\" or var1 == ARGV[1] or ARGV[1] == \"0\" then redis.call(\"HSET\", KEYS[1], \"data\", ARGV[2]) return redis.call(\"HINCRBY\", KEYS[1], \"version\", 1) else return error(\"failed to set key \" .. KEYS[1]) end" 1 "dapr1-mystate" "3" "new state" +``` + +If there's a ETag mismatch, you'll get an error saying ```failed to set key dapr1-mysate```. \ No newline at end of file From 129cc5ceccc22abfd4e87d57f080ace69e853842 Mon Sep 17 00:00:00 2001 From: Haishi2016 Date: Thu, 3 Oct 2019 16:12:46 -0700 Subject: [PATCH 2/3] Update query-redis-store.md --- howto/query-state-store/query-redis-store.md | 30 ++++---------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/howto/query-state-store/query-redis-store.md b/howto/query-state-store/query-redis-store.md index 0a16e8f806d..fd1127585c3 100644 --- a/howto/query-state-store/query-redis-store.md +++ b/howto/query-state-store/query-redis-store.md @@ -6,7 +6,7 @@ Dapr doesn't transform state values while saving and retriving states. And Dapr ## 1. List keys by Dapr id -To get all state keys associated with a Dapr id "myapp", use command: +To get all state keys associated with application "myapp", use command: ```bash KEYS myapp* @@ -20,9 +20,9 @@ The above command returns a list of existing keys: ## 2. Get specific state data -Dapr saves state values as hash values. Each hash value contains a "data" field, which contains the state data; and a "version" field, wich contains an ever-incrementing version serving as the ETag. +Dapr saves state values as hash values. Each hash value contains a "data" field, which contains the state data; and a "version" field, which contains an ever-incrementing version serving as the ETag. -To get the state data by a key "myapp-balance", use command: +To get the state data by a key "balance" for application "myapp", use command: ```bash HGET myapp-balance data @@ -34,7 +34,7 @@ HGET myapp-balance version ``` ## 3. Read Actor state -To get all state keys associated with an Actor instance "leroy" of Actor type "cat" belonging to Dapr id "mypets", use command: +To get all state keys associated with an Actor instance "leroy" of Actor type "cat" belonging to application "mypets", use command: ```bash KEYS mypets-cat-leroy* @@ -45,24 +45,4 @@ And to get a specific Actor state such as "food", use command: HGET mypets-cat-leroy-food value ``` -## 4. Delete state - -> **WARNING:** You should not manually update or delete states. The following instructions are provided for rare cases that you have to manually intervene. - -To delete a state by the key "myapp-balance", use command: -```bash -DEL myapp-balance -``` -## 5. Update state - -> **WARNING:** You should not manually update or delete states. The following instructions are provided for rare cases that you have to manually intervene. - -When you manually update a state, you should maintain the associated version together with your state updates as a single transaction. This can be done by [running a Lua script](https://redis.io/commands/eval). - -For example, the following command updates the state associated with the key "dapr1-mystate" to "new state", if the current ETag value in Redis is "3". - -```bash -EVAL "local var1 = redis.pcall(\"HGET\", KEYS[1], \"version\"); if type(var1) == \"table\" then redis.call(\"DEL\", KEYS[1]); end; if not var1 or type(var1)==\"table\" or var1 == \"\" or var1 == ARGV[1] or ARGV[1] == \"0\" then redis.call(\"HSET\", KEYS[1], \"data\", ARGV[2]) return redis.call(\"HINCRBY\", KEYS[1], \"version\", 1) else return error(\"failed to set key \" .. KEYS[1]) end" 1 "dapr1-mystate" "3" "new state" -``` - -If there's a ETag mismatch, you'll get an error saying ```failed to set key dapr1-mysate```. \ No newline at end of file +> **WARNING:** You should not manually update or delete states. From fcc798e2a279d400fb22557a46528f30e1b28c75 Mon Sep 17 00:00:00 2001 From: Mark Fussell Date: Thu, 3 Oct 2019 17:33:51 -0700 Subject: [PATCH 3/3] Updates to the doc --- howto/query-state-store/query-redis-store.md | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/howto/query-state-store/query-redis-store.md b/howto/query-state-store/query-redis-store.md index fd1127585c3..beadff45347 100644 --- a/howto/query-state-store/query-redis-store.md +++ b/howto/query-state-store/query-redis-store.md @@ -1,18 +1,18 @@ -# Query Redis data store +# Query Redis state store -Dapr doesn't transform state values while saving and retriving states. And Dapr requires all data store implementations abidding to certain key format scheme (see [Dapr state management spec](https://github.com/dapr/spec/blob/master/state.md)). You can directly interact with the underlying store to manipulate the state data, such querying states, creating aggregated views and making backups. +Dapr doesn't transform state values while saving and retriving states. Dapr requires all state store implementations to abide by a certain key format scheme (see [Dapr state management spec](https://github.com/dapr/spec/blob/master/state.md)). You can directly interact with the underlying store to manipulate the state data, such querying states, creating aggregated views and making backups. ->**NOTE:** The following samples uses Redis CLI against a Redis store using the default Dapr state store implementation. +>**NOTE:** The following examples uses Redis CLI against a Redis store using the default Dapr state store implementation. ## 1. List keys by Dapr id -To get all state keys associated with application "myapp", use command: +To get all state keys associated with application "myapp", use the command: ```bash KEYS myapp* ``` -The above command returns a list of existing keys: +The above command returns a list of existing keys, for example: ```bash 1) "myapp-balance" 2) "myapp-amount" @@ -20,29 +20,29 @@ The above command returns a list of existing keys: ## 2. Get specific state data -Dapr saves state values as hash values. Each hash value contains a "data" field, which contains the state data; and a "version" field, which contains an ever-incrementing version serving as the ETag. +Dapr saves state values as hash values. Each hash value contains a "data" field, which contains the state data and a "version" field, which contains an ever-incrementing version serving as the ETag. -To get the state data by a key "balance" for application "myapp", use command: +For example, to get the state data by a key "balance" for the application "myapp", use the command: ```bash HGET myapp-balance data ``` -To get the state version/ETag, use command: +To get the state version/ETag, use the command: ```bash HGET myapp-balance version ``` -## 3. Read Actor state +## 3. Read actor state -To get all state keys associated with an Actor instance "leroy" of Actor type "cat" belonging to application "mypets", use command: +To get all the state keys associated with an actor with the instance ID "leroy" of actor type "cat" belonging to the application with ID "mypets", use the command: ```bash KEYS mypets-cat-leroy* ``` -And to get a specific Actor state such as "food", use command: +And to get a specific actor state such as "food", use the command: ```bash HGET mypets-cat-leroy-food value ``` -> **WARNING:** You should not manually update or delete states. +> **WARNING:** You should not manually update or delete states in the store. All writes and delete operations should be done via the Dapr runtime.