You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2022/10/30 11:07:51 UTC
[shardingsphere] branch master updated: Refactor EtcdDistributedLock (#21851)
This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new f19a4d2f7ab Refactor EtcdDistributedLock (#21851)
f19a4d2f7ab is described below
commit f19a4d2f7aba54f23adaf71cd5e990c2530acea0
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Oct 30 19:07:43 2022 +0800
Refactor EtcdDistributedLock (#21851)
---
.../cluster/etcd/lock/EtcdDistributedLock.java | 19 +++++++++++--------
.../cluster/etcd/lock/EtcdDistributedLockHolder.java | 15 ++-------------
2 files changed, 13 insertions(+), 21 deletions(-)
diff --git a/mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdDistributedLock.java b/mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdDistributedLock.java
index f3f9519a6fe..2923d56f675 100644
--- a/mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdDistributedLock.java
+++ b/mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdDistributedLock.java
@@ -18,8 +18,10 @@
package org.apache.shardingsphere.mode.repository.cluster.etcd.lock;
import io.etcd.jetcd.ByteSequence;
-import io.etcd.jetcd.Lock;
+import io.etcd.jetcd.Client;
import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.mode.repository.cluster.etcd.props.EtcdProperties;
+import org.apache.shardingsphere.mode.repository.cluster.etcd.props.EtcdPropertyKey;
import org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLock;
import java.nio.charset.StandardCharsets;
@@ -31,19 +33,20 @@ import java.util.concurrent.TimeUnit;
@RequiredArgsConstructor
public final class EtcdDistributedLock implements DistributedLock {
- private final Lock lock;
-
private final String lockKey;
- private final long leaseId;
+ private final Client client;
+
+ private final EtcdProperties props;
@Override
public boolean tryLock(final long timeoutMillis) {
try {
- lock.lock(ByteSequence.from(lockKey, StandardCharsets.UTF_8), leaseId).get(timeoutMillis, TimeUnit.MILLISECONDS);
+ long leaseId = client.getLeaseClient().grant(props.getValue(EtcdPropertyKey.TIME_TO_LIVE_SECONDS)).get().getID();
+ client.getLockClient().lock(ByteSequence.from(lockKey, StandardCharsets.UTF_8), leaseId).get(timeoutMillis, TimeUnit.MILLISECONDS);
return true;
// CHECKSTYLE:OFF
- } catch (final Exception ex) {
+ } catch (final Exception ignored) {
// CHECKSTYLE:ON
return false;
}
@@ -52,9 +55,9 @@ public final class EtcdDistributedLock implements DistributedLock {
@Override
public void unlock() {
try {
- lock.unlock(ByteSequence.from(lockKey, StandardCharsets.UTF_8)).get();
+ client.getLockClient().unlock(ByteSequence.from(lockKey, StandardCharsets.UTF_8)).get();
// CHECKSTYLE:OFF
- } catch (final Exception ex) {
+ } catch (final Exception ignored) {
// CHECKSTYLE:ON
}
}
diff --git a/mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdDistributedLockHolder.java b/mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdDistributedLockHolder.java
index 0525485c96a..4e130ff722f 100644
--- a/mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdDistributedLockHolder.java
+++ b/mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdDistributedLockHolder.java
@@ -19,9 +19,7 @@ package org.apache.shardingsphere.mode.repository.cluster.etcd.lock;
import io.etcd.jetcd.Client;
import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.mode.repository.cluster.etcd.props.EtcdProperties;
-import org.apache.shardingsphere.mode.repository.cluster.etcd.props.EtcdPropertyKey;
import org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLock;
import org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockHolder;
@@ -32,14 +30,13 @@ import java.util.Map;
* Etcd distributed lock holder.
*/
@RequiredArgsConstructor
-@Slf4j
public final class EtcdDistributedLockHolder implements DistributedLockHolder {
private final Map<String, EtcdDistributedLock> locks = new HashMap<>();
private final Client client;
- private final EtcdProperties etcdProps;
+ private final EtcdProperties props;
@Override
public synchronized DistributedLock getDistributedLock(final String lockKey) {
@@ -52,14 +49,6 @@ public final class EtcdDistributedLockHolder implements DistributedLockHolder {
}
private EtcdDistributedLock createLock(final String lockKey) {
- try {
- long leaseId = client.getLeaseClient().grant(etcdProps.getValue(EtcdPropertyKey.TIME_TO_LIVE_SECONDS)).get().getID();
- return new EtcdDistributedLock(client.getLockClient(), lockKey, leaseId);
- // CHECKSTYLE:OFF
- } catch (final Exception ex) {
- // CHECKSTYLE:ON
- log.error("Create etcd internal lock failed, lockName:{}", lockKey, ex);
- }
- return null;
+ return new EtcdDistributedLock(lockKey, client, props);
}
}