Updating state management example to include etag mismatch exception handling (#458)

* Updating state management example to include etag mismatch exception scenario. Dapr runtime grpc exception handling was recently updated to return aborted error code on etage mismatch for mutations, the example is updated to reflect that the runtime updates will be propagated through the java sdk.

* Addressed feedback to merge block call with api call.

* Updating readme with block() call updates, missed in previous commit
This commit is contained in:
karishma-chawla 2021-01-26 17:14:51 -08:00 committed by GitHub
parent c5b4cbd3f5
commit efee05681e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 11 deletions

View File

@ -80,17 +80,30 @@ public class StateClient {
retrievedMessagesMono.block().forEach(System.out::println);
System.out.println("Deleting states...");
System.out.println("Verify delete key request is aborted if an etag different from stored is passed.");
// delete state API
Mono<Void> mono = client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME);
mono.block();
try {
client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME, "100", null).block();
} catch (DaprException ex) {
if (ex.getErrorCode().equals(Status.Code.ABORTED.toString())) {
// Expected error due to etag mismatch.
System.out.println(String.format("Expected failure. %s ", ex.getMessage()));
} else {
System.out.println("Unexpected exception.");
throw ex;
}
}
System.out.println("Trying to delete again with correct etag.");
String storedEtag = client.getState(STATE_STORE_NAME, FIRST_KEY_NAME, MyClass.class).block().getEtag();
client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME, storedEtag, null).block();
// Delete operation using transaction API
operationList.clear();
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.DELETE,
new State<>(SECOND_KEY_NAME)));
mono = client.executeStateTransaction(STATE_STORE_NAME, operationList);
mono.block();
client.executeStateTransaction(STATE_STORE_NAME, operationList).block();
Mono<List<State<MyClass>>> retrievedDeletedMessageMono = client.getStates(STATE_STORE_NAME,
Arrays.asList(FIRST_KEY_NAME, SECOND_KEY_NAME), MyClass.class);
@ -112,7 +125,7 @@ This example performs multiple operations:
* `client.getState(...)` operation in order to retrieve back the persisted state using the same key.
* `client.executeStateTransaction(...)` operation in order to update existing state and add new state.
* `client.getBulkState(...)` operation in order to retrieve back the persisted states using the same keys.
* `client.deleteState(...)` operation to remove one of the persisted states.
* `client.deleteState(...)` operation to remove one of the persisted states. An example of etag mismatch error if a different than current etag is added to request.
* `client.executeStateTransaction(...)` operation in order to remove the other persisted state.
Finally, the code tries to retrieve the deleted states, which should not be found.

View File

@ -9,6 +9,8 @@ import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.State;
import io.dapr.client.domain.TransactionalStateOperation;
import io.dapr.exceptions.DaprException;
import io.grpc.Status;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
@ -84,16 +86,28 @@ public class StateClient {
System.out.println("Deleting states...");
System.out.println("Verify delete key request is aborted if an etag different from stored is passed.");
// delete state API
Mono<Void> mono = client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME);
mono.block();
try {
client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME, "100", null).block();
} catch (DaprException ex) {
if (ex.getErrorCode().equals(Status.Code.ABORTED.toString())) {
// Expected error due to etag mismatch.
System.out.println(String.format("Expected failure. %s ", ex.getMessage()));
} else {
System.out.println("Unexpected exception.");
throw ex;
}
}
System.out.println("Trying to delete again with correct etag.");
String storedEtag = client.getState(STATE_STORE_NAME, FIRST_KEY_NAME, MyClass.class).block().getEtag();
client.deleteState(STATE_STORE_NAME, FIRST_KEY_NAME, storedEtag, null).block();
// Delete operation using transaction API
operationList.clear();
operationList.add(new TransactionalStateOperation<>(TransactionalStateOperation.OperationType.DELETE,
new State<>(SECOND_KEY_NAME)));
mono = client.executeStateTransaction(STATE_STORE_NAME, operationList);
mono.block();
client.executeStateTransaction(STATE_STORE_NAME, operationList).block();
Mono<List<State<MyClass>>> retrievedDeletedMessageMono = client.getBulkState(STATE_STORE_NAME,
Arrays.asList(FIRST_KEY_NAME, SECOND_KEY_NAME), MyClass.class);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 152 KiB

After

Width:  |  Height:  |  Size: 4.6 MiB

View File

@ -26,7 +26,7 @@ public class State<T> {
/**
* The ETag to be used
* Keep in mind that for some state stores (like reids) only numbers are supported.
* Keep in mind that for some state stores (like redis) only numbers are supported.
*/
private final String etag;