-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support distributed lock API #764
Merged
Merged
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3f00ab3
support distributed lock API
CrazyHZM e31daa9
add Distributed Lock API Example
CrazyHZM 426fa95
fix bash
CrazyHZM f5835a6
fix bash
CrazyHZM e27b9d7
Merge branch 'master' into feat/support_lock_api
CrazyHZM d352212
update dapr proto version
CrazyHZM 4ba0bcc
revert update dapr proto version
CrazyHZM 549d9c6
Merge branch 'master' into feat/support_lock_api
artursouza f941ac6
Merge branch 'master' into feat/support_lock_api
CrazyHZM 86a2834
changed to json serializable
CrazyHZM 9d902cf
Merge branch 'master' into feat/support_lock_api
CrazyHZM a8bb618
fix checkstyle
CrazyHZM 01ec591
Merge branch 'master' into feat/support_lock_api
CrazyHZM 2a32004
Merge branch 'master' into feat/support_lock_api
CrazyHZM afd6113
Merge branch 'master' into feat/support_lock_api
CrazyHZM 255078e
Merge remote-tracking branch 'upstream/master' into feat/support_lock…
artursouza f7eddf5
Fix compilation error due to Mono version change.
artursouza a32bc85
Merge remote-tracking branch 'upstream/master' into feat/support_lock…
artursouza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: lockstore | ||
spec: | ||
type: lock.redis | ||
version: v1 | ||
metadata: | ||
- name: redisHost | ||
value: localhost:6379 | ||
- name: redisPassword | ||
value: "" |
75 changes: 75 additions & 0 deletions
75
examples/src/main/java/io/dapr/examples/lock/grpc/DistributedLockGrpcClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2022 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.examples.lock.grpc; | ||
|
||
|
||
import io.dapr.client.DaprClientBuilder; | ||
import io.dapr.client.DaprPreviewClient; | ||
import io.dapr.client.domain.LockRequest; | ||
import io.dapr.client.domain.UnlockRequest; | ||
import io.dapr.client.domain.UnlockResponseStatus; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* DistributedLockGrpcClient. | ||
*/ | ||
public class DistributedLockGrpcClient { | ||
private static final String LOCK_STORE_NAME = "lockstore"; | ||
|
||
/** | ||
* Executes various methods to check the different apis. | ||
* | ||
* @param args arguments | ||
* @throws Exception throws Exception | ||
*/ | ||
public static void main(String[] args) throws Exception { | ||
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) { | ||
System.out.println("Using preview client..."); | ||
tryLock(client); | ||
unlock(client); | ||
} | ||
} | ||
|
||
/** | ||
* Trying to get lock. | ||
* | ||
* @param client DaprPreviewClient object | ||
*/ | ||
public static void tryLock(DaprPreviewClient client) { | ||
System.out.println("*******trying to get a free distributed lock********"); | ||
try { | ||
LockRequest lockRequest = new LockRequest(LOCK_STORE_NAME, "resouce1", "owner1", 5); | ||
Mono<Boolean> result = client.tryLock(lockRequest); | ||
System.out.println("Lock result -> " + (Boolean.TRUE.equals(result.block()) ? "SUCCESS" : "FAIL")); | ||
} catch (Exception ex) { | ||
System.out.println(ex.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Unlock a lock. | ||
* | ||
* @param client DaprPreviewClient object | ||
*/ | ||
public static void unlock(DaprPreviewClient client) { | ||
System.out.println("*******unlock a distributed lock********"); | ||
try { | ||
UnlockRequest unlockRequest = new UnlockRequest(LOCK_STORE_NAME, "resouce1", "owner1"); | ||
Mono<UnlockResponseStatus> result = client.unlock(unlockRequest); | ||
System.out.println("Unlock result ->" + result.block().name()); | ||
} catch (Exception ex) { | ||
System.out.println(ex.getMessage()); | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
examples/src/main/java/io/dapr/examples/lock/grpc/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
## Distributed Lock API Example | ||
|
||
This example provides the different capabilities provided by Dapr Java SDK for Distributed Lock. For further information about Distributed Lock APIs please refer to [this link](https://docs.dapr.io/developing-applications/building-blocks/distributed-lock/) | ||
**This API is available in Preview Mode**. | ||
|
||
### Using the Distributed Lock API | ||
|
||
The java SDK exposes several methods for this - | ||
* `client.tryLock(...)` for getting a free distributed lock | ||
* `client.unlock(...)` for unlocking a lock | ||
|
||
## Pre-requisites | ||
|
||
* [Dapr and Dapr Cli](https://docs.dapr.io/getting-started/install-dapr/). | ||
* Java JDK 11 (or greater): | ||
* [Microsoft JDK 11](https://docs.microsoft.com/en-us/java/openjdk/download#openjdk-11) | ||
* [Oracle JDK 11](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11) | ||
* [OpenJDK 11](https://jdk.java.net/11/) | ||
* [Apache Maven](https://maven.apache.org/install.html) version 3.x. | ||
|
||
### Checking out the code | ||
|
||
Clone this repository: | ||
|
||
```sh | ||
git clone https://github.com/dapr/java-sdk.git | ||
cd java-sdk | ||
``` | ||
|
||
Then build the Maven project: | ||
|
||
```sh | ||
# make sure you are in the `java-sdk` directory. | ||
mvn install | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
### Running the example | ||
|
||
Get into the examples' directory: | ||
```sh | ||
cd examples | ||
``` | ||
|
||
Use the following command to run this example- | ||
|
||
<!-- STEP | ||
name: Run DistributedLockGrpcClient example | ||
expected_stdout_lines: | ||
- "== APP == Using preview client..." | ||
- "== APP == *******trying to get a free distributed lock********" | ||
- "== APP == Lock result -> SUCCESS" | ||
- "== APP == *******unlock a distributed lock********" | ||
- "== APP == Unlock result -> SUCCESS" | ||
background: true | ||
sleep: 5 | ||
--> | ||
|
||
```bash | ||
dapr run --components-path ./components/lock --app-id lockgrpc --log-level debug -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.lock.grpc.DistributedLockGrpcClient | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
### Sample output | ||
``` | ||
== APP == Using preview client... | ||
== APP == *******trying to get a free distributed lock******** | ||
== APP == Lock result -> SUCCESS | ||
== APP == *******unlock a distributed lock******** | ||
== APP == Unlock result -> SUCCESS | ||
``` | ||
### Cleanup | ||
|
||
To stop the app, run (or press CTRL+C): | ||
|
||
<!-- STEP | ||
name: Cleanup | ||
--> | ||
|
||
```bash | ||
dapr stop --app-id lockgrpc | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
75 changes: 75 additions & 0 deletions
75
examples/src/main/java/io/dapr/examples/lock/http/DistributedLockHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2022 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.examples.lock.http; | ||
|
||
|
||
import io.dapr.client.DaprClientBuilder; | ||
import io.dapr.client.DaprPreviewClient; | ||
import io.dapr.client.domain.LockRequest; | ||
import io.dapr.client.domain.UnlockRequest; | ||
import io.dapr.client.domain.UnlockResponseStatus; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* DistributedLockGrpcClient. | ||
*/ | ||
public class DistributedLockHttpClient { | ||
private static final String LOCK_STORE_NAME = "lockstore"; | ||
|
||
/** | ||
* Executes various methods to check the different apis. | ||
* | ||
* @param args arguments | ||
* @throws Exception throws Exception | ||
*/ | ||
public static void main(String[] args) throws Exception { | ||
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) { | ||
System.out.println("Using preview client..."); | ||
tryLock(client); | ||
unlock(client); | ||
} | ||
} | ||
|
||
/** | ||
* Trying to get lock. | ||
* | ||
* @param client DaprPreviewClient object | ||
*/ | ||
public static void tryLock(DaprPreviewClient client) { | ||
System.out.println("*******trying to get a free distributed lock********"); | ||
try { | ||
LockRequest lockRequest = new LockRequest(LOCK_STORE_NAME, "resouce1", "owner1", 5); | ||
Mono<Boolean> result = client.tryLock(lockRequest); | ||
System.out.println("Lock result -> " + (Boolean.TRUE.equals(result.block()) ? "SUCCESS" : "FAIL")); | ||
} catch (Exception ex) { | ||
System.out.println(ex.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Unlock a lock. | ||
* | ||
* @param client DaprPreviewClient object | ||
*/ | ||
public static void unlock(DaprPreviewClient client) { | ||
System.out.println("*******unlock a distributed lock********"); | ||
try { | ||
UnlockRequest unlockRequest = new UnlockRequest(LOCK_STORE_NAME, "resouce1", "owner1"); | ||
Mono<UnlockResponseStatus> result = client.unlock(unlockRequest); | ||
System.out.println("Unlock result ->" + result.block().name()); | ||
} catch (Exception ex) { | ||
System.out.println(ex.getMessage()); | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
examples/src/main/java/io/dapr/examples/lock/http/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
## Distributed Lock API Example | ||
|
||
This example provides the different capabilities provided by Dapr Java SDK for Distributed Lock. For further information about Distributed Lock APIs please refer to [this link](https://docs.dapr.io/developing-applications/building-blocks/distributed-lock/) | ||
**This API is available in Preview Mode**. | ||
|
||
### Using the Distributed Lock API | ||
|
||
The java SDK exposes several methods for this - | ||
* `client.tryLock(...)` for getting a free distributed lock | ||
* `client.unlock(...)` for unlocking a lock | ||
|
||
## Pre-requisites | ||
|
||
* [Dapr and Dapr Cli](https://docs.dapr.io/getting-started/install-dapr/). | ||
* Java JDK 11 (or greater): | ||
* [Microsoft JDK 11](https://docs.microsoft.com/en-us/java/openjdk/download#openjdk-11) | ||
* [Oracle JDK 11](https://www.oracle.com/technetwork/java/javase/downloads/index.html#JDK11) | ||
* [OpenJDK 11](https://jdk.java.net/11/) | ||
* [Apache Maven](https://maven.apache.org/install.html) version 3.x. | ||
|
||
### Checking out the code | ||
|
||
Clone this repository: | ||
|
||
```sh | ||
git clone https://github.com/dapr/java-sdk.git | ||
cd java-sdk | ||
``` | ||
|
||
Then build the Maven project: | ||
|
||
```sh | ||
# make sure you are in the `java-sdk` directory. | ||
mvn install | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
### Running the example | ||
|
||
Get into the examples' directory: | ||
```sh | ||
cd examples | ||
``` | ||
|
||
Use the following command to run this example- | ||
|
||
<!-- STEP | ||
name: Run DistributedLockHttpClient example | ||
expected_stdout_lines: | ||
- "== APP == Using preview client..." | ||
- "== APP == *******trying to get a free distributed lock********" | ||
- "== APP == Lock result -> SUCCESS" | ||
- "== APP == *******unlock a distributed lock********" | ||
- "== APP == Unlock result -> SUCCESS" | ||
background: true | ||
sleep: 5 | ||
--> | ||
|
||
```bash | ||
dapr run --components-path ./components/lock --app-id lockhttp --log-level debug -- java -Ddapr.api.protocol=HTTP -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.lock.http.DistributedLockHttpClient | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
||
### Sample output | ||
``` | ||
== APP == Using preview client... | ||
== APP == *******trying to get a free distributed lock******** | ||
== APP == Lock result -> SUCCESS | ||
== APP == *******unlock a distributed lock******** | ||
== APP == Unlock result -> SUCCESS | ||
``` | ||
### Cleanup | ||
|
||
To stop the app, run (or press CTRL+C): | ||
|
||
<!-- STEP | ||
name: Cleanup | ||
--> | ||
|
||
```bash | ||
dapr stop --app-id lockhttp | ||
``` | ||
|
||
<!-- END_STEP --> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need an example for both HTTP and gRPC since the SDK abstract that.
It would be great if the example was using the APIs to make something more useful, like leader election, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has not been deleted for the time being, and new examples can be added in the future.