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 f0289ec845d Add DistributedLockCreator (#21913)
f0289ec845d is described below
commit f0289ec845d5c4bc568534c7bf4d53a159c8f85d
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Nov 3 01:24:27 2022 +0800
Add DistributedLockCreator (#21913)
* Add DistributedLockCreator
* Refactor DistributedLockHolder
---
...LockHolder.java => DistributedLockCreator.java} | 22 ++++++++++----
...der.java => DistributedLockCreatorFactory.java} | 25 ++++++++++++----
.../cluster/lock/DistributedLockHolder.java | 34 ++++++++++++++++++++--
.../cluster/consul/ConsulRepository.java | 10 +++----
...lder.java => ConsulDistributedLockCreator.java} | 29 ++++++------------
....repository.cluster.lock.DistributedLockCreator | 18 ++++++++++++
.../cluster/consul/ConsulRepositoryTest.java | 4 +--
.../repository/cluster/etcd/EtcdRepository.java | 3 +-
...Holder.java => EtcdDistributedLockCreator.java} | 31 +++++---------------
....repository.cluster.lock.DistributedLockCreator | 18 ++++++++++++
.../zookeeper/CuratorZookeeperRepository.java | 5 ++--
...=> CuratorZooKeeperDistributedLockCreator.java} | 28 +++++++-----------
....repository.cluster.lock.DistributedLockCreator | 18 ++++++++++++
.../zookeeper/CuratorZookeeperRepositoryTest.java | 7 +++--
14 files changed, 161 insertions(+), 91 deletions(-)
diff --git
a/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockHolder.java
b/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockCreator.java
similarity index 59%
copy from
mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockHolder.java
copy to
mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockCreator.java
index ce388370c2b..31909f18131 100644
---
a/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockHolder.java
+++
b/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockCreator.java
@@ -17,16 +17,26 @@
package org.apache.shardingsphere.mode.repository.cluster.lock;
+import org.apache.shardingsphere.infra.util.props.TypedProperties;
+import org.apache.shardingsphere.infra.util.spi.annotation.SingletonSPI;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPI;
+
/**
- * Distributed lock holder.
+ * Distributed lock creator.
+ *
+ * @param <C> type of distributed lock client
+ * @param <P> type of typed properties
*/
-public interface DistributedLockHolder {
+@SingletonSPI
+public interface DistributedLockCreator<C, P extends TypedProperties<?>>
extends TypedSPI {
/**
- * Get distributed lock.
- *
+ * Create distributed lock.
+ *
* @param lockKey lock key
- * @return distributed lock
+ * @param client client
+ * @param props props
+ * @return created distributed lock
*/
- DistributedLock getDistributedLock(String lockKey);
+ DistributedLock create(String lockKey, C client, P props);
}
diff --git
a/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockHolder.java
b/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockCreatorFactory.java
similarity index 56%
copy from
mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockHolder.java
copy to
mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockCreatorFactory.java
index ce388370c2b..3be7532fde6 100644
---
a/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockHolder.java
+++
b/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockCreatorFactory.java
@@ -17,16 +17,29 @@
package org.apache.shardingsphere.mode.repository.cluster.lock;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPIRegistry;
+
/**
- * Distributed lock holder.
+ * Distributed lock creator factory.
*/
-public interface DistributedLockHolder {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class DistributedLockCreatorFactory {
+
+ static {
+ ShardingSphereServiceLoader.register(DistributedLockCreator.class);
+ }
/**
- * Get distributed lock.
- *
- * @param lockKey lock key
+ * Get distributed lock.
+ *
+ * @param type type
* @return distributed lock
*/
- DistributedLock getDistributedLock(String lockKey);
+ @SuppressWarnings("rawtypes")
+ public static DistributedLockCreator newInstance(final String type) {
+ return
TypedSPIRegistry.getRegisteredService(DistributedLockCreator.class, type);
+ }
}
diff --git
a/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockHolder.java
b/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockHolder.java
index ce388370c2b..92ca8ef7ce0 100644
---
a/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockHolder.java
+++
b/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/DistributedLockHolder.java
@@ -17,16 +17,44 @@
package org.apache.shardingsphere.mode.repository.cluster.lock;
+import org.apache.shardingsphere.infra.util.props.TypedProperties;
+
+import java.util.HashMap;
+import java.util.Map;
+
/**
* Distributed lock holder.
*/
-public interface DistributedLockHolder {
+public final class DistributedLockHolder {
+
+ private final DistributedLockCreator<Object, TypedProperties<?>> creator;
+
+ private final Object client;
+
+ private final TypedProperties<?> props;
+
+ private final Map<String, DistributedLock> locks;
+
+ @SuppressWarnings("unchecked")
+ public DistributedLockHolder(final String type, final Object client, final
TypedProperties<?> props) {
+ creator = DistributedLockCreatorFactory.newInstance(type);
+ this.client = client;
+ this.props = props;
+ locks = new HashMap<>();
+ }
/**
* Get distributed lock.
- *
+ *
* @param lockKey lock key
* @return distributed lock
*/
- DistributedLock getDistributedLock(String lockKey);
+ public synchronized DistributedLock getDistributedLock(final String
lockKey) {
+ DistributedLock result = locks.get(lockKey);
+ if (null == result) {
+ result = creator.create(lockKey, client, props);
+ locks.put(lockKey, result);
+ }
+ return result;
+ }
}
diff --git
a/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepository.java
b/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepository.java
index f7a501da8a8..3734851fc23 100644
---
a/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepository.java
+++
b/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepository.java
@@ -29,11 +29,11 @@ import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
-import
org.apache.shardingsphere.mode.repository.cluster.consul.lock.ConsulDistributedLockHolder;
import
org.apache.shardingsphere.mode.repository.cluster.consul.props.ConsulProperties;
import
org.apache.shardingsphere.mode.repository.cluster.consul.props.ConsulPropertyKey;
import
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
import
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEventListener;
+import
org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockHolder;
import java.util.Collection;
import java.util.Collections;
@@ -56,7 +56,7 @@ public class ConsulRepository implements
ClusterPersistRepository {
private ConsulProperties consulProps;
- private ConsulDistributedLockHolder consulDistributedLockHolder;
+ private DistributedLockHolder distributedLockHolder;
private Map<String, Collection<String>> watchKeyMap;
@@ -65,7 +65,7 @@ public class ConsulRepository implements
ClusterPersistRepository {
ConsulRawClient rawClient =
Strings.isNullOrEmpty(config.getServerLists()) ? new ConsulRawClient() : new
ConsulRawClient(config.getServerLists());
consulClient = new ShardingSphereConsulClient(rawClient);
consulProps = new ConsulProperties(config.getProps());
- consulDistributedLockHolder = new
ConsulDistributedLockHolder(consulClient, consulProps);
+ distributedLockHolder = new DistributedLockHolder(getType(),
consulClient, consulProps);
watchKeyMap = new HashMap<>(6, 1);
}
@@ -131,12 +131,12 @@ public class ConsulRepository implements
ClusterPersistRepository {
@Override
public boolean tryLock(final String lockKey, final long timeoutMillis) {
- return
consulDistributedLockHolder.getDistributedLock(lockKey).tryLock(timeoutMillis);
+ return
distributedLockHolder.getDistributedLock(lockKey).tryLock(timeoutMillis);
}
@Override
public void unlock(final String lockKey) {
- consulDistributedLockHolder.getDistributedLock(lockKey).unlock();
+ distributedLockHolder.getDistributedLock(lockKey).unlock();
}
@Override
diff --git
a/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/lock/ConsulDistributedLockHolder.java
b/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/lock/ConsulDistributedLockCreator.java
similarity index 61%
rename from
mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/lock/ConsulDistributedLockHolder.java
rename to
mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/lock/ConsulDistributedLockCreator.java
index 0cc8b45d827..33ad78742f3 100644
---
a/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/lock/ConsulDistributedLockHolder.java
+++
b/mode/type/cluster/repository/provider/consul/src/main/java/org/apache/shardingsphere/mode/repository/cluster/consul/lock/ConsulDistributedLockCreator.java
@@ -18,33 +18,22 @@
package org.apache.shardingsphere.mode.repository.cluster.consul.lock;
import com.ecwid.consul.v1.ConsulClient;
-import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.mode.repository.cluster.consul.props.ConsulProperties;
import org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLock;
-import
org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockHolder;
-
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
+import
org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockCreator;
/**
- * Consul distributed lock holder.
+ * Consul distributed lock creator.
*/
-@RequiredArgsConstructor
-public class ConsulDistributedLockHolder implements DistributedLockHolder {
-
- private final Map<String, ConsulDistributedLock> locks = new
ConcurrentHashMap<>();
-
- private final ConsulClient client;
+public final class ConsulDistributedLockCreator implements
DistributedLockCreator<ConsulClient, ConsulProperties> {
- private final ConsulProperties props;
+ @Override
+ public DistributedLock create(final String lockKey, final ConsulClient
client, final ConsulProperties props) {
+ return new ConsulDistributedLock(lockKey, client, props);
+ }
@Override
- public DistributedLock getDistributedLock(final String lockKey) {
- ConsulDistributedLock result = locks.get(lockKey);
- if (null == result) {
- result = new ConsulDistributedLock(lockKey, client, props);
- locks.put(lockKey, result);
- }
- return result;
+ public String getType() {
+ return "Consul";
}
}
diff --git
a/mode/type/cluster/repository/provider/consul/src/main/resources/META-INF.services/org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockCreator
b/mode/type/cluster/repository/provider/consul/src/main/resources/META-INF.services/org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockCreator
new file mode 100644
index 00000000000..2d2664d4099
--- /dev/null
+++
b/mode/type/cluster/repository/provider/consul/src/main/resources/META-INF.services/org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockCreator
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+org.apache.shardingsphere.mode.repository.cluster.consul.lock.ConsulDistributedLockCreator
diff --git
a/mode/type/cluster/repository/provider/consul/src/test/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepositoryTest.java
b/mode/type/cluster/repository/provider/consul/src/test/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepositoryTest.java
index d713df3e801..fb21e902e08 100644
---
a/mode/type/cluster/repository/provider/consul/src/test/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepositoryTest.java
+++
b/mode/type/cluster/repository/provider/consul/src/test/java/org/apache/shardingsphere/mode/repository/cluster/consul/ConsulRepositoryTest.java
@@ -23,8 +23,8 @@ import com.ecwid.consul.v1.kv.model.GetValue;
import com.ecwid.consul.v1.kv.model.PutParams;
import com.ecwid.consul.v1.session.model.NewSession;
import lombok.SneakyThrows;
-import
org.apache.shardingsphere.mode.repository.cluster.consul.lock.ConsulDistributedLockHolder;
import
org.apache.shardingsphere.mode.repository.cluster.consul.props.ConsulProperties;
+import
org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockHolder;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -98,7 +98,7 @@ public final class ConsulRepositoryTest {
when(responseGetValueList.getValue()).thenReturn(getValueList);
when(client.setKVValue(any(String.class),
any(String.class))).thenReturn(responseBoolean);
Plugins.getMemberAccessor().set(repository.getClass().getDeclaredField("consulClient"),
repository, client);
-
Plugins.getMemberAccessor().set(repository.getClass().getDeclaredField("consulDistributedLockHolder"),
repository, mock(ConsulDistributedLockHolder.class));
+
Plugins.getMemberAccessor().set(repository.getClass().getDeclaredField("distributedLockHolder"),
repository, mock(DistributedLockHolder.class));
}
@SneakyThrows(ReflectiveOperationException.class)
diff --git
a/mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/EtcdRepository.java
b/mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/EtcdRepository.java
index 0a2df21a5a1..7a28814114c 100644
---
a/mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/EtcdRepository.java
+++
b/mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/EtcdRepository.java
@@ -35,7 +35,6 @@ import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
-import
org.apache.shardingsphere.mode.repository.cluster.etcd.lock.EtcdDistributedLockHolder;
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.listener.DataChangedEvent;
@@ -66,7 +65,7 @@ public final class EtcdRepository implements
ClusterPersistRepository {
.namespace(ByteSequence.from(config.getNamespace(),
StandardCharsets.UTF_8))
.maxInboundMessageSize((int) 32e9)
.build();
- distributedLockHolder = new EtcdDistributedLockHolder(client,
etcdProps);
+ distributedLockHolder = new DistributedLockHolder(getType(), client,
etcdProps);
}
@SneakyThrows({InterruptedException.class, ExecutionException.class})
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/EtcdDistributedLockCreator.java
similarity index 61%
rename from
mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdDistributedLockHolder.java
rename to
mode/type/cluster/repository/provider/etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdDistributedLockCreator.java
index 4e130ff722f..3cc0bec7787 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/EtcdDistributedLockCreator.java
@@ -18,37 +18,22 @@
package org.apache.shardingsphere.mode.repository.cluster.etcd.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.lock.DistributedLock;
-import
org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockHolder;
-
-import java.util.HashMap;
-import java.util.Map;
+import
org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockCreator;
/**
- * Etcd distributed lock holder.
+ * Etcd distributed lock creator.
*/
-@RequiredArgsConstructor
-public final class EtcdDistributedLockHolder implements DistributedLockHolder {
-
- private final Map<String, EtcdDistributedLock> locks = new HashMap<>();
-
- private final Client client;
-
- private final EtcdProperties props;
+public final class EtcdDistributedLockCreator implements
DistributedLockCreator<Client, EtcdProperties> {
@Override
- public synchronized DistributedLock getDistributedLock(final String
lockKey) {
- EtcdDistributedLock result = locks.get(lockKey);
- if (null == result) {
- result = createLock(lockKey);
- locks.put(lockKey, result);
- }
- return result;
+ public DistributedLock create(final String lockKey, final Client client,
final EtcdProperties props) {
+ return new EtcdDistributedLock(lockKey, client, props);
}
- private EtcdDistributedLock createLock(final String lockKey) {
- return new EtcdDistributedLock(lockKey, client, props);
+ @Override
+ public String getType() {
+ return "etcd";
}
}
diff --git
a/mode/type/cluster/repository/provider/etcd/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockCreator
b/mode/type/cluster/repository/provider/etcd/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockCreator
new file mode 100644
index 00000000000..6765fd5d9fd
--- /dev/null
+++
b/mode/type/cluster/repository/provider/etcd/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockCreator
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+org.apache.shardingsphere.mode.repository.cluster.etcd.lock.EtcdDistributedLockCreator
diff --git
a/mode/type/cluster/repository/provider/zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepository.java
b/mode/type/cluster/repository/provider/zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepository.java
index 75e17d9051a..e9e39e120ab 100644
---
a/mode/type/cluster/repository/provider/zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepository.java
+++
b/mode/type/cluster/repository/provider/zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepository.java
@@ -39,7 +39,6 @@ import
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEve
import
org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockHolder;
import
org.apache.shardingsphere.mode.repository.cluster.zookeeper.handler.CuratorZookeeperExceptionHandler;
import
org.apache.shardingsphere.mode.repository.cluster.zookeeper.listener.SessionConnectionListener;
-import
org.apache.shardingsphere.mode.repository.cluster.zookeeper.lock.CuratorZookeeperDistributedLockHolder;
import
org.apache.shardingsphere.mode.repository.cluster.zookeeper.props.ZookeeperProperties;
import
org.apache.shardingsphere.mode.repository.cluster.zookeeper.props.ZookeeperPropertyKey;
import org.apache.zookeeper.CreateMode;
@@ -50,9 +49,9 @@ import org.apache.zookeeper.data.ACL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
import java.util.Map;
-import java.util.Comparator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
@@ -76,7 +75,7 @@ public final class CuratorZookeeperRepository implements
ClusterPersistRepositor
this.instanceMetaData = instanceMetaData;
ZookeeperProperties zookeeperProps = new
ZookeeperProperties(config.getProps());
client = buildCuratorClient(config, zookeeperProps);
- distributedLockHolder = new
CuratorZookeeperDistributedLockHolder(client);
+ distributedLockHolder = new DistributedLockHolder(getType(), client,
zookeeperProps);
initCuratorClient(zookeeperProps);
}
diff --git
a/mode/type/cluster/repository/provider/zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/lock/CuratorZookeeperDistributedLockHolder.java
b/mode/type/cluster/repository/provider/zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/lock/CuratorZooKeeperDistributedLockCreator.java
similarity index 60%
rename from
mode/type/cluster/repository/provider/zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/lock/CuratorZookeeperDistributedLockHolder.java
rename to
mode/type/cluster/repository/provider/zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/lock/CuratorZooKeeperDistributedLockCreator.java
index eec2b19f31f..4831f4f61c2 100644
---
a/mode/type/cluster/repository/provider/zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/lock/CuratorZookeeperDistributedLockHolder.java
+++
b/mode/type/cluster/repository/provider/zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/lock/CuratorZooKeeperDistributedLockCreator.java
@@ -17,31 +17,23 @@
package org.apache.shardingsphere.mode.repository.cluster.zookeeper.lock;
-import lombok.RequiredArgsConstructor;
import org.apache.curator.framework.CuratorFramework;
import org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLock;
-import
org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockHolder;
-
-import java.util.HashMap;
-import java.util.Map;
+import
org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockCreator;
+import
org.apache.shardingsphere.mode.repository.cluster.zookeeper.props.ZookeeperProperties;
/**
- * Curator ZooKeeper distributed lock holder.
+ * Curator zooKeeper distributed lock creator.
*/
-@RequiredArgsConstructor
-public final class CuratorZookeeperDistributedLockHolder implements
DistributedLockHolder {
-
- private final Map<String, CuratorZookeeperDistributedLock> locks = new
HashMap<>();
+public final class CuratorZooKeeperDistributedLockCreator implements
DistributedLockCreator<CuratorFramework, ZookeeperProperties> {
- private final CuratorFramework client;
+ @Override
+ public DistributedLock create(final String lockKey, final CuratorFramework
client, final ZookeeperProperties props) {
+ return new CuratorZookeeperDistributedLock(lockKey, client);
+ }
@Override
- public synchronized DistributedLock getDistributedLock(final String
lockKey) {
- CuratorZookeeperDistributedLock result = locks.get(lockKey);
- if (null == result) {
- result = new CuratorZookeeperDistributedLock(lockKey, client);
- locks.put(lockKey, result);
- }
- return result;
+ public String getType() {
+ return "ZooKeeper";
}
}
diff --git
a/mode/type/cluster/repository/provider/zookeeper-curator/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockCreator
b/mode/type/cluster/repository/provider/zookeeper-curator/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockCreator
new file mode 100644
index 00000000000..f15989ce7d0
--- /dev/null
+++
b/mode/type/cluster/repository/provider/zookeeper-curator/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockCreator
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+org.apache.shardingsphere.mode.repository.cluster.zookeeper.lock.CuratorZooKeeperDistributedLockCreator
diff --git
a/mode/type/cluster/repository/provider/zookeeper-curator/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepositoryTest.java
b/mode/type/cluster/repository/provider/zookeeper-curator/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepositoryTest.java
index 91952526cec..b4fcd592759 100644
---
a/mode/type/cluster/repository/provider/zookeeper-curator/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepositoryTest.java
+++
b/mode/type/cluster/repository/provider/zookeeper-curator/src/test/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepositoryTest.java
@@ -38,8 +38,9 @@ import
org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMeta
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
import
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent;
import
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEvent.Type;
+import
org.apache.shardingsphere.mode.repository.cluster.lock.DistributedLockHolder;
import
org.apache.shardingsphere.mode.repository.cluster.zookeeper.lock.CuratorZookeeperDistributedLock;
-import
org.apache.shardingsphere.mode.repository.cluster.zookeeper.lock.CuratorZookeeperDistributedLockHolder;
+import
org.apache.shardingsphere.mode.repository.cluster.zookeeper.props.ZookeeperProperties;
import
org.apache.shardingsphere.mode.repository.cluster.zookeeper.props.ZookeeperPropertyKey;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
@@ -143,8 +144,8 @@ public final class CuratorZookeeperRepositoryTest {
private void mockDistributedLockHolder() {
Field distributedLockHolderField =
CuratorZookeeperRepository.class.getDeclaredField("distributedLockHolder");
distributedLockHolderField.setAccessible(true);
- CuratorZookeeperDistributedLockHolder distributedLockHolder = new
CuratorZookeeperDistributedLockHolder(client);
- Field locksFiled =
CuratorZookeeperDistributedLockHolder.class.getDeclaredField("locks");
+ DistributedLockHolder distributedLockHolder = new
DistributedLockHolder("Zookeeper", client, new ZookeeperProperties(new
Properties()));
+ Field locksFiled =
DistributedLockHolder.class.getDeclaredField("locks");
locksFiled.setAccessible(true);
locksFiled.set(distributedLockHolder,
Collections.singletonMap("/locks/glock",
mock(CuratorZookeeperDistributedLock.class)));
distributedLockHolderField.set(REPOSITORY, distributedLockHolder);