Skip to content

Instantly share code, notes, and snippets.

@kdkanishka
Created June 17, 2021 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdkanishka/689b5b3ff5c44ed6942a6e4050079bef to your computer and use it in GitHub Desktop.
Save kdkanishka/689b5b3ff5c44ed6942a6e4050079bef to your computer and use it in GitHub Desktop.
Demonstrating locking abilities via etcdv3 client.
package etcd;
import io.etcd.jetcd.*;
import io.etcd.jetcd.kv.GetResponse;
import io.etcd.jetcd.kv.PutResponse;
import io.etcd.jetcd.lease.LeaseGrantResponse;
import io.etcd.jetcd.lease.LeaseRevokeResponse;
import io.etcd.jetcd.lock.LockResponse;
import io.etcd.jetcd.lock.UnlockResponse;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Added dependency
* <!-- https://mvnrepository.com/artifact/io.etcd/jetcd-core -->
* <dependency>
* <groupId>io.etcd</groupId>
* <artifactId>jetcd-core</artifactId>
* <version>0.5.7</version>
* </dependency>
*
*/
public class EtcdV3Client {
public static void main(String[] args) throws Exception {
Client client = Client.builder().endpoints("http://172.17.42.1:4001").build();
KV kvClient = client.getKVClient();
Lease leaseClient = client.getLeaseClient();
CompletableFuture<LeaseGrantResponse> grant = leaseClient.grant(200);
LeaseGrantResponse leaseGrantResponse = grant.get();
long leaseId = leaseGrantResponse.getID();
System.out.println("Lease ID : " + leaseId);
Lock lockClient = client.getLockClient();
String lockId = "Lock4";
CompletableFuture<LockResponse> lock = lockClient.lock(ByteSequence.from(lockId.getBytes()), leaseId);
LockResponse lockResponse = null;
boolean isLockGranted = false;
try {
lockResponse = lock.get(5, TimeUnit.SECONDS);
isLockGranted = true;
}catch (TimeoutException e){
System.out.println(e.getMessage());
}
if(isLockGranted) {
System.out.println("Lock acquired : " + new String(lockResponse.getKey().getBytes()));
ByteSequence key = ByteSequence.from("test_key".getBytes());
ByteSequence value = ByteSequence.from("test_value".getBytes());
PutResponse putResponse = kvClient.put(key, value).get();
System.out.println("PUT response");
System.out.println(putResponse.toString());
CompletableFuture<UnlockResponse> unlock = lockClient.unlock(ByteSequence.from(lockId.getBytes()));
UnlockResponse unlockResponse = unlock.get();
System.out.println("Lock released : " + unlockResponse.toString());
System.out.println(unlockResponse);
CompletableFuture<GetResponse> getFuture = kvClient.get(key);
GetResponse response = getFuture.get();
// System.out.println("Response______________________");
// System.out.println(response.toString());
CompletableFuture<LeaseRevokeResponse> revoke = leaseClient.revoke(leaseId);
LeaseRevokeResponse leaseRevokeResponse = revoke.get();
System.out.println("Lease revoked :" + leaseRevokeResponse);
lockClient.close();
kvClient.close();
client.close();
}else{
System.out.println("Unable to acquire lock");
}
System.out.println("Done");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment