You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/05/21 03:05:22 UTC

[GitHub] [shardingsphere] menghaoranss commented on a change in pull request #10391: Add etcd lock

menghaoranss commented on a change in pull request #10391:
URL: https://github.com/apache/shardingsphere/pull/10391#discussion_r636603769



##########
File path: shardingsphere-governance/shardingsphere-governance-repository/shardingsphere-governance-repository-provider/shardingsphere-governance-repository-etcd/src/main/java/org/apache/shardingsphere/governance/repository/etcd/EtcdRepository.java
##########
@@ -129,23 +129,36 @@ private Type getEventChangedType(final WatchEvent event) {
                 return Type.IGNORED;
         }
     }
-    
+
     @Override
     public boolean tryLock(final String key, final long time, final TimeUnit unit) {
-        // TODO
-        return false;
+        try {
+            long leaseId = client.getLeaseClient().grant(etcdProperties.getValue(EtcdPropertyKey.TIME_TO_LIVE_SECONDS)).get().getID();
+            client.getLockClient().lock(ByteSequence.from(key, StandardCharsets.UTF_8), leaseId).get(time, unit);
+            return true;
+            // CHECKSTYLE:OFF
+        } catch (final Exception ex) {
+            // CHECKSTYLE:ON
+            return false;

Review comment:
       The exception need be printed by @slf4j

##########
File path: shardingsphere-governance/shardingsphere-governance-repository/shardingsphere-governance-repository-provider/shardingsphere-governance-repository-etcd/src/main/java/org/apache/shardingsphere/governance/repository/etcd/EtcdRepository.java
##########
@@ -129,23 +129,36 @@ private Type getEventChangedType(final WatchEvent event) {
                 return Type.IGNORED;
         }
     }
-    
+
     @Override
     public boolean tryLock(final String key, final long time, final TimeUnit unit) {
-        // TODO
-        return false;
+        try {
+            long leaseId = client.getLeaseClient().grant(etcdProperties.getValue(EtcdPropertyKey.TIME_TO_LIVE_SECONDS)).get().getID();
+            client.getLockClient().lock(ByteSequence.from(key, StandardCharsets.UTF_8), leaseId).get(time, unit);
+            return true;
+            // CHECKSTYLE:OFF
+        } catch (final Exception ex) {
+            // CHECKSTYLE:ON
+            return false;
+        }
     }
-    
+
     @Override
     public void releaseLock(final String key) {
-        // TODO
+        try {
+            client.getLockClient().unlock(ByteSequence.from(key, StandardCharsets.UTF_8)).get(etcdProperties.getValue(EtcdPropertyKey.CONNECTION_TIMEOUT_SECONDS), TimeUnit.SECONDS);
+            // CHECKSTYLE:OFF
+        } catch (final Exception ex) {
+            // CHECKSTYLE:ON
+            // do nothing

Review comment:
       The exception need be printed by @slf4j, and remove // do nothing




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org