This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 8347c02ca4a Refactor lock context and remove lock judge engine (#19984)
8347c02ca4a is described below
commit 8347c02ca4ab9b9aab0ace09940a6b0ca6bac5df
Author: gin <[email protected]>
AuthorDate: Tue Aug 9 19:34:33 2022 +0800
Refactor lock context and remove lock judge engine (#19984)
---
.../infra/lock/ShardingSphereLock.java | 16 -----
.../fixture/FixtureClusterPersistRepository.java | 11 ----
.../core/api/impl/RuleAlteredJobAPIImpl.java | 6 +-
.../rulealtered/RuleAlteredJobPreparer.java | 4 +-
.../{definition => }/ExclusiveLockDefinition.java | 3 +-
.../mode/lock/{util => }/LockKeyUtil.java | 33 +++-------
.../lock/{manager/state => }/LockStateContext.java | 2 +-
.../mode/lock/ShardingSphereLockContext.java | 25 ++++---
.../lock/definition/LockDefinitionFactory.java | 49 --------------
.../mode/lock/engine/LockJudgeEngine.java | 40 -----------
.../mode/lock/engine/LockJudgeEngineFactory.java | 43 ------------
.../lock/engine/ShardingSphereLockJudgeEngine.java | 34 ----------
.../lock/manager/ShardingSphereLockManager.java | 69 -------------------
...shardingsphere.mode.lock.engine.LockJudgeEngine | 18 -----
.../mode/lock/ExclusiveLockDefinitionTest.java} | 27 ++++----
.../shardingsphere/mode/lock/LockKeyUtilTest.java} | 30 ++++-----
.../mode/lock/LockStateContextTest.java | 30 ++++-----
.../mode/lock/ShardingSphereLockContextTest.java | 67 +++++++++++++++++++
.../coordinator/ClusterLockPersistService.java | 2 +-
.../fixture/ClusterPersistRepositoryFixture.java | 11 ----
...ProcessListClusterPersistRepositoryFixture.java | 11 ----
.../cluster/ClusterPersistRepository.java | 22 -------
.../mode/repository/cluster/lock/InternalLock.java | 30 ++++-----
.../cluster/lock/InternalLockHolder.java} | 28 ++------
.../repository/cluster/etcd/EtcdRepository.java | 15 +----
.../cluster/etcd/lock/EtcdInternalLockHolder.java | 77 +++++-----------------
.../zookeeper/CuratorZookeeperRepository.java | 15 +----
.../lock/ZookeeperInternalLockHolder.java | 69 ++++---------------
.../lock/ShardingSphereStandaloneLock.java | 24 +------
.../lock/StandaloneLockPersistService.java | 2 +-
.../communication/DatabaseCommunicationEngine.java | 13 ----
.../jdbc/JDBCDatabaseCommunicationEngine.java | 1 -
.../vertx/VertxDatabaseCommunicationEngine.java | 1 -
.../distsql/DistSQLBackendHandlerFactory.java | 20 ------
.../fixture/ClusterPersistRepositoryFixture.java | 11 ----
.../frontend/mysql/err/MySQLErrPacketFactory.java | 5 --
.../postgresql/err/PostgreSQLErrPacketFactory.java | 5 --
.../fixture/TestClusterPersistRepository.java | 11 ----
38 files changed, 190 insertions(+), 690 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/ShardingSphereLock.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/ShardingSphereLock.java
index f1e656bb111..da3af5fc7bf 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/ShardingSphereLock.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/ShardingSphereLock.java
@@ -22,14 +22,6 @@ package org.apache.shardingsphere.infra.lock;
*/
public interface ShardingSphereLock {
- /**
- * Try to lock.
- *
- * @param lockName lock name
- * @return true if get the lock, false if not
- */
- boolean tryLock(String lockName);
-
/**
* Try to lock with time out.
*
@@ -45,12 +37,4 @@ public interface ShardingSphereLock {
* @param lockName lock name
*/
void unlock(String lockName);
-
- /**
- * Check whether resource is locked in current instance.
- *
- * @param lockName lock name
- * @return true if locked, false if not
- */
- boolean isLocked(String lockName);
}
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/fixture/FixtureClusterPersistRepository.java
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/fixture/FixtureClusterPersistRepository.java
index 35c1b69fbb6..e9e1137a179 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/fixture/FixtureClusterPersistRepository.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/fixture/FixtureClusterPersistRepository.java
@@ -27,7 +27,6 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.locks.Lock;
public final class FixtureClusterPersistRepository implements
ClusterPersistRepository {
@@ -75,16 +74,6 @@ public final class FixtureClusterPersistRepository
implements ClusterPersistRepo
return false;
}
- @Override
- public Lock getInternalMutexLock(final String lockName) {
- return null;
- }
-
- @Override
- public Lock getInternalReentrantMutexLock(final String lockName) {
- return null;
- }
-
@Override
public void close() {
registryData.clear();
diff --git
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/api/impl/RuleAlteredJobAPIImpl.java
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/api/impl/RuleAlteredJobAPIImpl.java
index 05afdeb0a4b..a952c9b1eb3 100644
---
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/api/impl/RuleAlteredJobAPIImpl.java
+++
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/api/impl/RuleAlteredJobAPIImpl.java
@@ -58,7 +58,7 @@ import
org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.lock.LockDefinition;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
-import org.apache.shardingsphere.mode.lock.definition.LockDefinitionFactory;
+import org.apache.shardingsphere.mode.lock.ExclusiveLockDefinition;
import
org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.config.event.rule.ScalingTaskFinishedEvent;
import java.time.LocalDateTime;
@@ -188,7 +188,7 @@ public final class RuleAlteredJobAPIImpl extends
AbstractPipelineJobAPIImpl impl
public void stopClusterWriteDB(final RuleAlteredJobConfiguration
jobConfig) {
String databaseName = jobConfig.getDatabaseName();
LockContext lockContext =
PipelineContext.getContextManager().getInstanceContext().getLockContext();
- LockDefinition lockDefinition =
LockDefinitionFactory.newDatabaseLockDefinition(databaseName);
+ LockDefinition lockDefinition = new
ExclusiveLockDefinition(databaseName);
if (lockContext.isLocked(lockDefinition)) {
log.info("stopClusterWriteDB, already stopped");
return;
@@ -214,7 +214,7 @@ public final class RuleAlteredJobAPIImpl extends
AbstractPipelineJobAPIImpl impl
public void restoreClusterWriteDB(final RuleAlteredJobConfiguration
jobConfig) {
String databaseName = jobConfig.getDatabaseName();
LockContext lockContext =
PipelineContext.getContextManager().getInstanceContext().getLockContext();
- LockDefinition lockDefinition =
LockDefinitionFactory.newDatabaseLockDefinition(databaseName);
+ LockDefinition lockDefinition = new
ExclusiveLockDefinition(databaseName);
if (lockContext.isLocked(lockDefinition)) {
log.info("restoreClusterWriteDB, before unlock, databaseName={},
jobId={}", databaseName, jobConfig.getJobId());
lockContext.unlock(lockDefinition);
diff --git
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/rulealtered/RuleAlteredJobPreparer.java
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/rulealtered/RuleAlteredJobPreparer.java
index c3231d5ea35..349e9261bd3 100644
---
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/rulealtered/RuleAlteredJobPreparer.java
+++
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/rulealtered/RuleAlteredJobPreparer.java
@@ -54,7 +54,7 @@ import
org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCre
import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.lock.LockDefinition;
-import org.apache.shardingsphere.mode.lock.definition.LockDefinitionFactory;
+import org.apache.shardingsphere.mode.lock.ExclusiveLockDefinition;
import
org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper;
import javax.sql.DataSource;
@@ -105,7 +105,7 @@ public final class RuleAlteredJobPreparer {
// TODO the lock will be replaced
String lockName = "prepare-" + jobConfig.getJobId();
LockContext lockContext =
PipelineContext.getContextManager().getInstanceContext().getLockContext();
- LockDefinition lockDefinition =
LockDefinitionFactory.newExclusiveLockDefinition(lockName);
+ LockDefinition lockDefinition = new ExclusiveLockDefinition(lockName);
if (lockContext.tryLock(lockDefinition, 3000)) {
try {
prepareAndCheckTarget(jobContext);
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/definition/ExclusiveLockDefinition.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/ExclusiveLockDefinition.java
similarity index 91%
copy from
shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/definition/ExclusiveLockDefinition.java
copy to
shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/ExclusiveLockDefinition.java
index e2bb7fc14e9..ea05588939e 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/definition/ExclusiveLockDefinition.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/ExclusiveLockDefinition.java
@@ -15,11 +15,10 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mode.lock.definition;
+package org.apache.shardingsphere.mode.lock;
import lombok.Getter;
import org.apache.shardingsphere.infra.lock.LockDefinition;
-import org.apache.shardingsphere.mode.lock.util.LockKeyUtil;
/**
* Exclusive lock definition.
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/util/LockKeyUtil.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/LockKeyUtil.java
similarity index 78%
rename from
shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/util/LockKeyUtil.java
rename to
shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/LockKeyUtil.java
index c660f49b898..b48f69f610e 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/util/LockKeyUtil.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/LockKeyUtil.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mode.lock.util;
+package org.apache.shardingsphere.mode.lock;
/**
* Lock key util.
@@ -30,18 +30,6 @@ public final class LockKeyUtil {
private static final String LOCKS_NODE_EXCLUSIVE = "exclusive";
- private static final String LOCKS_NODE_DATABASE = "database";
-
- /**
- * Generate lock key leases.
- *
- * @param lockKey lock key
- * @return locks name
- */
- public static String generateLockKeyLeases(final String lockKey) {
- return lockKey + "/leases";
- }
-
/**
* Generate exclusive lock key.
*
@@ -52,22 +40,17 @@ public final class LockKeyUtil {
return generateLocksNodePath(LOCKS_NODE_EXCLUSIVE) + "/" + lockName;
}
- /**
- * Generate database lock key.
- *
- * @param lockName locks name
- * @return locks name
- */
- public static String generateDatabaseLockKey(final String lockName) {
- return generateLocksNodePath(LOCKS_NODE_DATABASE) + "/" + lockName;
+ private static String generateLocksNodePath(final String lockPath) {
+ return PATH_DELIMITER + LOCK_ROOT + PATH_DELIMITER + lockPath +
PATH_DELIMITER + LOCKS_NODE;
}
/**
- * Generate locks node path.
+ * Generate lock key leases.
*
- * @return locks node path
+ * @param lockKey lock key
+ * @return locks name
*/
- private static String generateLocksNodePath(final String lockPath) {
- return PATH_DELIMITER + LOCK_ROOT + PATH_DELIMITER + lockPath +
PATH_DELIMITER + LOCKS_NODE;
+ public static String generateLockKeyLeases(final String lockKey) {
+ return lockKey + "/leases";
}
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/manager/state/LockStateContext.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/LockStateContext.java
similarity index 98%
rename from
shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/manager/state/LockStateContext.java
rename to
shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/LockStateContext.java
index 8b54b357bf5..2faca7dfce0 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/manager/state/LockStateContext.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/LockStateContext.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mode.lock.manager.state;
+package org.apache.shardingsphere.mode.lock;
import org.apache.shardingsphere.infra.lock.LockDefinition;
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/ShardingSphereLockContext.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/ShardingSphereLockContext.java
index d30a8cc3a48..62a8f9b604c 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/ShardingSphereLockContext.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/ShardingSphereLockContext.java
@@ -17,39 +17,44 @@
package org.apache.shardingsphere.mode.lock;
+import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.infra.lock.LockDefinition;
-import org.apache.shardingsphere.mode.lock.manager.ShardingSphereLockManager;
-import org.apache.shardingsphere.mode.lock.util.TimeoutMilliseconds;
/**
* Lock context of ShardingSphere.
*/
+@RequiredArgsConstructor
public final class ShardingSphereLockContext implements LockContext {
- private final ShardingSphereLockManager lockManager;
+ public static final long MAX_TRY_LOCK = 3 * 60 * 1000L;
- public ShardingSphereLockContext(final LockPersistService
lockPersistService) {
- this.lockManager = new ShardingSphereLockManager(lockPersistService);
- }
+ private final LockStateContext lockStateContext = new LockStateContext();
+
+ private final LockPersistService lockPersistService;
@Override
public boolean tryLock(final LockDefinition lockDefinition) {
- return lockManager.tryLock(lockDefinition,
TimeoutMilliseconds.MAX_TRY_LOCK);
+ return tryLock(lockDefinition, MAX_TRY_LOCK);
}
@Override
public boolean tryLock(final LockDefinition lockDefinition, final long
timeoutMillis) {
- return lockManager.tryLock(lockDefinition, timeoutMillis);
+ if (lockPersistService.tryLock(lockDefinition, timeoutMillis)) {
+ lockStateContext.register(lockDefinition);
+ return true;
+ }
+ return false;
}
@Override
public void unlock(final LockDefinition lockDefinition) {
- lockManager.unLock(lockDefinition);
+ lockPersistService.unlock(lockDefinition);
+ lockStateContext.unregister(lockDefinition);
}
@Override
public boolean isLocked(final LockDefinition lockDefinition) {
- return lockManager.isLocked(lockDefinition);
+ return lockStateContext.isLocked(lockDefinition);
}
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/definition/LockDefinitionFactory.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/definition/LockDefinitionFactory.java
deleted file mode 100644
index f8ba7a0830d..00000000000
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/definition/LockDefinitionFactory.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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.
- */
-
-package org.apache.shardingsphere.mode.lock.definition;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.infra.lock.LockDefinition;
-
-/**
- * Lock definition factory.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class LockDefinitionFactory {
-
- /**
- * New exclusive lock definition.
- *
- * @param lockName lock name
- * @return database lock definition
- */
- public static LockDefinition newExclusiveLockDefinition(final String
lockName) {
- return new ExclusiveLockDefinition(lockName);
- }
-
- /**
- * New database lock definition.
- *
- * @param databaseName database name
- * @return database lock definition
- */
- public static LockDefinition newDatabaseLockDefinition(final String
databaseName) {
- return new DatabaseLockDefinition(databaseName);
- }
-}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/engine/LockJudgeEngine.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/engine/LockJudgeEngine.java
deleted file mode 100644
index 884da44c78f..00000000000
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/engine/LockJudgeEngine.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * 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.
- */
-
-package org.apache.shardingsphere.mode.lock.engine;
-
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.lock.LockContext;
-import org.apache.shardingsphere.infra.util.spi.annotation.SingletonSPI;
-import org.apache.shardingsphere.infra.util.spi.type.required.RequiredSPI;
-
-/**
- * Lock judge engine.
- */
-@SingletonSPI
-public interface LockJudgeEngine extends RequiredSPI {
-
- /**
- * Is locked.
- *
- * @param lockContext lock context
- * @param databaseName database name
- * @param sqlStatementContext sql statement context
- * @return is locked or not
- */
- boolean isLocked(LockContext lockContext, String databaseName,
SQLStatementContext<?> sqlStatementContext);
-}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/engine/LockJudgeEngineFactory.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/engine/LockJudgeEngineFactory.java
deleted file mode 100644
index f426aca266a..00000000000
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/engine/LockJudgeEngineFactory.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * 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.
- */
-
-package org.apache.shardingsphere.mode.lock.engine;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
-import
org.apache.shardingsphere.infra.util.spi.type.required.RequiredSPIRegistry;
-
-/**
- * Lock judge engine factory.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class LockJudgeEngineFactory {
-
- static {
- ShardingSphereServiceLoader.register(LockJudgeEngine.class);
- }
-
- /**
- * Get instance of lock judge engine.
- *
- * @return got instance
- */
- public static LockJudgeEngine getInstance() {
- return RequiredSPIRegistry.getRegisteredService(LockJudgeEngine.class);
- }
-}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/engine/ShardingSphereLockJudgeEngine.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/engine/ShardingSphereLockJudgeEngine.java
deleted file mode 100644
index f10f355eb11..00000000000
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/engine/ShardingSphereLockJudgeEngine.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * 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.
- */
-
-package org.apache.shardingsphere.mode.lock.engine;
-
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.lock.LockContext;
-import org.apache.shardingsphere.mode.lock.definition.LockDefinitionFactory;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatementType;
-
-/**
- * Lock judge engine for ShardingSphere.
- */
-public final class ShardingSphereLockJudgeEngine implements LockJudgeEngine {
-
- @Override
- public boolean isLocked(final LockContext lockContext, final String
databaseName, final SQLStatementContext<?> sqlStatementContext) {
- return
SQLStatementType.involvesDataChanges(sqlStatementContext.getSqlStatement()) &&
lockContext.isLocked(LockDefinitionFactory.newDatabaseLockDefinition(databaseName));
- }
-}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/manager/ShardingSphereLockManager.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/manager/ShardingSphereLockManager.java
deleted file mode 100644
index cfee7551966..00000000000
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/manager/ShardingSphereLockManager.java
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * 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.
- */
-
-package org.apache.shardingsphere.mode.lock.manager;
-
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.lock.LockDefinition;
-import org.apache.shardingsphere.mode.lock.LockPersistService;
-import org.apache.shardingsphere.mode.lock.manager.state.LockStateContext;
-
-/**
- * Lock manager of ShardingSphere.
- */
-@RequiredArgsConstructor
-public final class ShardingSphereLockManager {
-
- private final LockStateContext lockStateContext = new LockStateContext();
-
- private final LockPersistService lockPersistService;
-
- /**
- * Try lock.
- *
- * @param lockDefinition lock definition
- * @param timeoutMillis timeout millis
- * @return is locked or not
- */
- public boolean tryLock(final LockDefinition lockDefinition, final long
timeoutMillis) {
- if (lockPersistService.tryLock(lockDefinition, timeoutMillis)) {
- lockStateContext.register(lockDefinition);
- return true;
- }
- return false;
- }
-
- /**
- * Unlock.
- *
- * @param lockDefinition lock definition
- */
- public void unLock(final LockDefinition lockDefinition) {
- lockPersistService.unlock(lockDefinition);
- lockStateContext.unregister(lockDefinition);
- }
-
- /**
- * Is locked.
- *
- * @param lockDefinition lock definition
- * @return is locked or not
- */
- public boolean isLocked(final LockDefinition lockDefinition) {
- return lockStateContext.isLocked(lockDefinition);
- }
-}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.lock.engine.LockJudgeEngine
b/shardingsphere-mode/shardingsphere-mode-core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.lock.engine.LockJudgeEngine
deleted file mode 100644
index 0f9649adc56..00000000000
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.lock.engine.LockJudgeEngine
+++ /dev/null
@@ -1,18 +0,0 @@
-#
-# 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.lock.engine.ShardingSphereLockJudgeEngine
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/definition/ExclusiveLockDefinition.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/lock/ExclusiveLockDefinitionTest.java
similarity index 59%
rename from
shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/definition/ExclusiveLockDefinition.java
rename to
shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/lock/ExclusiveLockDefinitionTest.java
index e2bb7fc14e9..13257a1df3a 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/definition/ExclusiveLockDefinition.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/lock/ExclusiveLockDefinitionTest.java
@@ -15,24 +15,19 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mode.lock.definition;
+package org.apache.shardingsphere.mode.lock;
-import lombok.Getter;
-import org.apache.shardingsphere.infra.lock.LockDefinition;
-import org.apache.shardingsphere.mode.lock.util.LockKeyUtil;
+import org.junit.Test;
-/**
- * Exclusive lock definition.
- */
-@Getter
-public final class ExclusiveLockDefinition implements LockDefinition {
-
- private final String lockKey;
-
- private final String lockName;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class ExclusiveLockDefinitionTest {
- public ExclusiveLockDefinition(final String lockName) {
- this.lockName = lockName;
- lockKey = LockKeyUtil.generateExclusiveLockKey(lockName);
+ @Test
+ public void assertNewLockDefinition() {
+ ExclusiveLockDefinition lockDefinition = new
ExclusiveLockDefinition("exclusive_lock");
+ assertThat(lockDefinition.getLockName(), is("exclusive_lock"));
+ assertThat(lockDefinition.getLockKey(),
is("/lock/exclusive/locks/exclusive_lock"));
}
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/definition/DatabaseLockDefinition.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/lock/LockKeyUtilTest.java
similarity index 56%
rename from
shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/definition/DatabaseLockDefinition.java
rename to
shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/lock/LockKeyUtilTest.java
index 9a684be130f..e3710457a4b 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/definition/DatabaseLockDefinition.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/lock/LockKeyUtilTest.java
@@ -15,26 +15,22 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mode.lock.definition;
+package org.apache.shardingsphere.mode.lock;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.lock.LockDefinition;
-import org.apache.shardingsphere.mode.lock.util.LockKeyUtil;
+import org.junit.Test;
-/**
- * Database lock definition.
- */
-@RequiredArgsConstructor
-@Getter
-public final class DatabaseLockDefinition implements LockDefinition {
-
- private final String lockKey;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class LockKeyUtilTest {
- private final String databaseName;
+ @Test
+ public void assertGenerateExclusiveLockKey() {
+ assertThat(LockKeyUtil.generateExclusiveLockKey("exclusive_lock"),
is("/lock/exclusive/locks/exclusive_lock"));
+ }
- public DatabaseLockDefinition(final String databaseName) {
- this.databaseName = databaseName;
- lockKey = LockKeyUtil.generateDatabaseLockKey(databaseName);
+ @Test
+ public void assertGenerateLockKeyLeases() {
+
assertThat(LockKeyUtil.generateLockKeyLeases("/lock/exclusive/locks/exclusive_lock"),
is("/lock/exclusive/locks/exclusive_lock/leases"));
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/UnsupportedUpdateOperationException.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/lock/LockStateContextTest.java
similarity index 55%
copy from
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/UnsupportedUpdateOperationException.java
copy to
shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/lock/LockStateContextTest.java
index 2f49f27550e..9774ecb3ee7 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/UnsupportedUpdateOperationException.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/lock/LockStateContextTest.java
@@ -15,25 +15,23 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.exception;
+package org.apache.shardingsphere.mode.lock;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+import org.junit.Test;
-/**
- * Unsupported update operation exception.
- */
-@RequiredArgsConstructor
-@Getter
-public final class UnsupportedUpdateOperationException extends
BackendException {
-
- private static final long serialVersionUID = -5409739222950362541L;
-
- private final String databaseName;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public final class LockStateContextTest {
- private final String errorMessage;
+ private final LockStateContext lockStateContext = new LockStateContext();
- public UnsupportedUpdateOperationException(final String databaseName) {
- this(databaseName, String.format("The database %s is read-only",
databaseName));
+ @Test
+ public void assertLockState() {
+ ExclusiveLockDefinition lockDefinition = new
ExclusiveLockDefinition("exclusive_lock");
+ lockStateContext.register(lockDefinition);
+ assertTrue(lockStateContext.isLocked(lockDefinition));
+ lockStateContext.unregister(lockDefinition);
+ assertFalse(lockStateContext.isLocked(lockDefinition));
}
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/lock/ShardingSphereLockContextTest.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/lock/ShardingSphereLockContextTest.java
new file mode 100644
index 00000000000..2afa45e5c16
--- /dev/null
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/lock/ShardingSphereLockContextTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.
+ */
+
+package org.apache.shardingsphere.mode.lock;
+
+import org.apache.shardingsphere.infra.lock.LockDefinition;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public final class ShardingSphereLockContextTest {
+
+ public static final long MAX_TRY_LOCK = 3 * 60 * 1000L;
+
+ private ShardingSphereLockContext lockContext;
+
+ private LockPersistService lockPersistService;
+
+ private LockDefinition lockDefinition;
+
+ @Before
+ public void init() {
+ lockDefinition = new ExclusiveLockDefinition("exclusive_lock");
+ lockPersistService = mock(LockPersistService.class);
+ when(lockPersistService.tryLock(lockDefinition,
MAX_TRY_LOCK)).thenReturn(true);
+ when(lockPersistService.tryLock(lockDefinition,
3000)).thenReturn(true);
+ doAnswer(invocationOnMock ->
null).when(lockPersistService).unlock(lockDefinition);
+ lockContext = new ShardingSphereLockContext(lockPersistService);
+ }
+
+ @Test
+ public void assertTryLock() {
+ assertTrue(lockContext.tryLock(lockDefinition));
+ assertTrue(lockContext.isLocked(lockDefinition));
+ }
+
+ @Test
+ public void assertTryLockTimeout() {
+ assertTrue(lockContext.tryLock(lockDefinition, 3000));
+ assertTrue(lockContext.isLocked(lockDefinition));
+ }
+
+ @Test
+ public void assertUnlock() {
+ lockContext.unlock(lockDefinition);
+ verify(lockPersistService).unlock(lockDefinition);
+ }
+}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterLockPersistService.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterLockPersistService.java
index e2ee9812f49..c4a77f3fd32 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterLockPersistService.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterLockPersistService.java
@@ -20,7 +20,7 @@ package
org.apache.shardingsphere.mode.manager.cluster.coordinator;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.lock.LockDefinition;
import org.apache.shardingsphere.mode.lock.LockPersistService;
-import org.apache.shardingsphere.mode.lock.util.LockKeyUtil;
+import org.apache.shardingsphere.mode.lock.LockKeyUtil;
import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;
/**
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/fixture/ClusterPersistRepositoryFixture.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/fixture/ClusterPersistRepositoryFixture.java
index 68f765e7082..798880247f6 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/fixture/ClusterPersistRepositoryFixture.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/fixture/ClusterPersistRepositoryFixture.java
@@ -24,7 +24,6 @@ import
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEve
import java.util.Collections;
import java.util.List;
-import java.util.concurrent.locks.Lock;
public final class ClusterPersistRepositoryFixture implements
ClusterPersistRepository {
@@ -67,16 +66,6 @@ public final class ClusterPersistRepositoryFixture
implements ClusterPersistRepo
return false;
}
- @Override
- public Lock getInternalMutexLock(final String lockName) {
- return null;
- }
-
- @Override
- public Lock getInternalReentrantMutexLock(final String lockName) {
- return null;
- }
-
@Override
public void close() {
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/ProcessListClusterPersistRepositoryFixture.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/ProcessListClusterPersistRepositoryFix
[...]
index 9a1e4379e62..3067d4016b9 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/ProcessListClusterPersistRepositoryFixture.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/registry/process/ProcessListClusterPersistRepositoryFixture.java
@@ -26,7 +26,6 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.locks.Lock;
public final class ProcessListClusterPersistRepositoryFixture implements
ClusterPersistRepository {
@@ -74,16 +73,6 @@ public final class
ProcessListClusterPersistRepositoryFixture implements Cluster
return false;
}
- @Override
- public Lock getInternalMutexLock(final String lockName) {
- return null;
- }
-
- @Override
- public Lock getInternalReentrantMutexLock(final String lockName) {
- return null;
- }
-
@Override
public void close() {
REGISTRY_DATA.clear();
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/ClusterPersistRepository.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/ClusterPer
[...]
index 63bf7e35dda..892cb92638f 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/ClusterPersistRepository.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/ClusterPersistRepository.java
@@ -21,8 +21,6 @@ import
org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.mode.persist.PersistRepository;
import
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEventListener;
-import java.util.concurrent.locks.Lock;
-
/**
* Cluster persist repository.
*/
@@ -66,24 +64,4 @@ public interface ClusterPersistRepository extends
PersistRepository {
* @return is locked or not
*/
boolean tryLock(String lockKey, long timeoutMillis);
-
- /**
- * Get internal mutex lock.
- *
- * @param lockName lock name
- * @return internal mutex lock
- * @deprecated remove me when the distributed lock refactoring was
completed
- */
- @Deprecated
- Lock getInternalMutexLock(String lockName);
-
- /**
- * Get internal reentrant mutex lock.
- *
- * @param lockName lock name
- * @return internal reentrant mutex lock
- * @deprecated remove me when the distributed lock refactoring was
completed
- */
- @Deprecated
- Lock getInternalReentrantMutexLock(String lockName);
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/UnsupportedUpdateOperationException.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/InternalLock.java
similarity index 57%
rename from
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/UnsupportedUpdateOperationException.java
rename to
shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/InternalLock.java
index 2f49f27550e..8fa7b08dcf0 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/UnsupportedUpdateOperationException.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/InternalLock.java
@@ -15,25 +15,23 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.backend.exception;
-
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+package org.apache.shardingsphere.mode.repository.cluster.lock;
/**
- * Unsupported update operation exception.
+ * Internal lock.
*/
-@RequiredArgsConstructor
-@Getter
-public final class UnsupportedUpdateOperationException extends
BackendException {
-
- private static final long serialVersionUID = -5409739222950362541L;
-
- private final String databaseName;
+public interface InternalLock {
- private final String errorMessage;
+ /**
+ * Try lock.
+ *
+ * @param timeoutMillis timeout millis
+ * @return is locked or not
+ */
+ boolean tryLock(long timeoutMillis);
- public UnsupportedUpdateOperationException(final String databaseName) {
- this(databaseName, String.format("The database %s is read-only",
databaseName));
- }
+ /**
+ * Unlock.
+ */
+ void unlock();
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/util/TimeoutMilliseconds.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/InternalLockHolder.java
similarity index 55%
rename from
shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/util/TimeoutMilliseconds.java
rename to
shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/InternalLockHolder.java
index 18cb7fa00a9..ad5378fa066 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/lock/util/TimeoutMilliseconds.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/lock/InternalLockHolder.java
@@ -15,32 +15,18 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mode.lock.util;
-
-import lombok.SneakyThrows;
-
-import java.util.concurrent.TimeUnit;
+package org.apache.shardingsphere.mode.repository.cluster.lock;
/**
- * Timeout milliseconds.
+ * Internal lock holder.
*/
-public final class TimeoutMilliseconds {
-
- public static final long MAX_TRY_LOCK = 3 * 60 * 1000L;
-
- public static final long MIN_TRY_LOCK = 200L;
-
- public static final long DEFAULT_REGISTRY = 50L;
-
- public static final long MAX_ACK_EXPEND = 100L;
+public interface InternalLockHolder {
/**
- * Sleep interval.
+ * Get internal lock.
*
- * @param timeMilliseconds time milliseconds
+ * @param lockKey lock key
+ * @return internal lock
*/
- @SneakyThrows(InterruptedException.class)
- public static void sleepInterval(final long timeMilliseconds) {
- TimeUnit.MILLISECONDS.sleep(timeMilliseconds);
- }
+ InternalLock getInternalLock(String lockKey);
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/EtcdRepository.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mo
[...]
index f96ce8893a2..e7ea57cc557 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/EtcdRepository.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/EtcdRepository.java
@@ -44,8 +44,6 @@ import
org.apache.shardingsphere.mode.repository.cluster.listener.DataChangedEve
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.Lock;
import java.util.stream.Collectors;
/**
@@ -147,19 +145,8 @@ public final class EtcdRepository implements
ClusterPersistRepository {
}
@Override
- @SneakyThrows(InterruptedException.class)
public boolean tryLock(final String lockKey, final long timeoutMillis) {
- return
etcdInternalLockHolder.getInternalMutexLock(lockKey).tryLock(timeoutMillis,
TimeUnit.MILLISECONDS);
- }
-
- @Override
- public Lock getInternalMutexLock(final String lockName) {
- return etcdInternalLockHolder.getInternalMutexLock(lockName);
- }
-
- @Override
- public Lock getInternalReentrantMutexLock(final String lockName) {
- return etcdInternalLockHolder.getInternalReentrantMutexLock(lockName);
+ return
etcdInternalLockHolder.getInternalLock(lockKey).tryLock(timeoutMillis);
}
@Override
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdInternalLockHolder.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphe
[...]
index 49feea27551..ea3177023b7 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdInternalLockHolder.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-etcd/src/main/java/org/apache/shardingsphere/mode/repository/cluster/etcd/lock/EtcdInternalLockHolder.java
@@ -23,21 +23,21 @@ 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.InternalLock;
+import
org.apache.shardingsphere.mode.repository.cluster.lock.InternalLockHolder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.Condition;
-import java.util.concurrent.locks.Lock;
/**
* Etcd internal lock holder.
*/
@RequiredArgsConstructor
@Slf4j
-public final class EtcdInternalLockHolder {
+public final class EtcdInternalLockHolder implements InternalLockHolder {
private final Map<String, EtcdInternalLock> locks = new
ConcurrentHashMap<>();
@@ -45,39 +45,24 @@ public final class EtcdInternalLockHolder {
private final EtcdProperties etcdProps;
- /**
- * Get internal mutex lock.
- *
- * @param lockName lock name
- * @return internal mutex lock
- */
- public Lock getInternalMutexLock(final String lockName) {
- return getInternalReentrantMutexLock(lockName);
- }
-
- /**
- * Get internal reentrant mutex lock.
- *
- * @param lockName lock name
- * @return internal reentrant mutex lock
- */
- public Lock getInternalReentrantMutexLock(final String lockName) {
- EtcdInternalLock result = locks.get(lockName);
+ @Override
+ public synchronized InternalLock getInternalLock(final String lockKey) {
+ EtcdInternalLock result = locks.get(lockKey);
if (Objects.isNull(result)) {
- result = createLock(lockName);
- locks.put(lockName, result);
+ result = createLock(lockKey);
+ locks.put(lockKey, result);
}
return result;
}
- private EtcdInternalLock createLock(final String lockName) {
+ private EtcdInternalLock createLock(final String lockKey) {
try {
long leaseId =
client.getLeaseClient().grant(etcdProps.getValue(EtcdPropertyKey.TIME_TO_LIVE_SECONDS)).get().getID();
- return new EtcdInternalLock(client.getLockClient(), lockName,
leaseId);
+ return new EtcdInternalLock(client.getLockClient(), lockKey,
leaseId);
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
- log.error("EtcdRepository tryLock error, lockName:{}", lockName,
ex);
+ log.error("EtcdRepository tryLock error, lockName:{}", lockKey,
ex);
}
return null;
}
@@ -86,39 +71,23 @@ public final class EtcdInternalLockHolder {
* Etcd internal lock.
*/
@RequiredArgsConstructor
- private static class EtcdInternalLock implements Lock {
+ private static class EtcdInternalLock implements InternalLock {
private final io.etcd.jetcd.Lock lock;
- private final String lockName;
+ private final String lockKey;
private final long leaseId;
@Override
- public void lock() {
+ public boolean tryLock(final long timeout) {
try {
- this.lock.lock(ByteSequence.from(lockName,
StandardCharsets.UTF_8), leaseId).get();
- // CHECKSTYLE:OFF
- } catch (final Exception ex) {
- // CHECKSTYLE:ON
- log.error("EtcdRepository tryLock error, lockName:{}",
lockName, ex);
- }
- }
-
- @Override
- public boolean tryLock() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean tryLock(final long time, final TimeUnit timeUnit) {
- try {
- this.lock.lock(ByteSequence.from(lockName,
StandardCharsets.UTF_8), leaseId).get(time, timeUnit);
+ this.lock.lock(ByteSequence.from(lockKey,
StandardCharsets.UTF_8), leaseId).get(timeout, TimeUnit.MILLISECONDS);
return true;
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
- log.error("EtcdRepository tryLock error, lockName:{}",
lockName, ex);
+ log.error("EtcdRepository tryLock error, lockName:{}",
lockKey, ex);
return false;
}
}
@@ -126,22 +95,12 @@ public final class EtcdInternalLockHolder {
@Override
public void unlock() {
try {
- lock.unlock(ByteSequence.from(lockName,
StandardCharsets.UTF_8)).get();
+ lock.unlock(ByteSequence.from(lockKey,
StandardCharsets.UTF_8)).get();
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
- log.error("EtcdRepository unlock error, lockName:{}",
lockName, ex);
+ log.error("EtcdRepository unlock error, lockName:{}", lockKey,
ex);
}
}
-
- @Override
- public void lockInterruptibly() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Condition newCondition() {
- throw new UnsupportedOperationException();
- }
}
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepository.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-prov
[...]
index 4b802b48bd5..922b52f662a 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepository.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/CuratorZookeeperRepository.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.mode.repository.cluster.zookeeper;
import com.google.common.base.Strings;
-import lombok.SneakyThrows;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.CuratorFrameworkFactory.Builder;
@@ -51,7 +50,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.Lock;
/**
* Registry repository of ZooKeeper.
@@ -257,19 +255,8 @@ public final class CuratorZookeeperRepository implements
ClusterPersistRepositor
}
@Override
- public Lock getInternalMutexLock(final String lockName) {
- return internalLockHolder.getInternalMutexLock(lockName);
- }
-
- @Override
- public Lock getInternalReentrantMutexLock(final String lockName) {
- return internalLockHolder.getInternalReentrantMutexLock(lockName);
- }
-
- @Override
- @SneakyThrows(InterruptedException.class)
public boolean tryLock(final String lockKey, final long timeoutMillis) {
- return
internalLockHolder.getInternalMutexLock(lockKey).tryLock(timeoutMillis,
TimeUnit.MILLISECONDS);
+ return
internalLockHolder.getInternalLock(lockKey).tryLock(timeoutMillis);
}
@Override
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/lock/ZookeeperInternalLockHolder.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repositor
[...]
index a4dd8a04d7f..7a5909bad06 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/lock/ZookeeperInternalLockHolder.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-repository/shardingsphere-cluster-mode-repository-provider/shardingsphere-cluster-mode-repository-zookeeper-curator/src/main/java/org/apache/shardingsphere/mode/repository/cluster/zookeeper/lock/ZookeeperInternalLockHolder.java
@@ -20,53 +20,32 @@ package
org.apache.shardingsphere.mode.repository.cluster.zookeeper.lock;
import lombok.RequiredArgsConstructor;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
-import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex;
+import org.apache.shardingsphere.mode.repository.cluster.lock.InternalLock;
+import
org.apache.shardingsphere.mode.repository.cluster.lock.InternalLockHolder;
import
org.apache.shardingsphere.mode.repository.cluster.zookeeper.handler.CuratorZookeeperExceptionHandler;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.Condition;
-import java.util.concurrent.locks.Lock;
/**
* Zookeeper internal lock holder.
*/
@RequiredArgsConstructor
-public final class ZookeeperInternalLockHolder {
+public final class ZookeeperInternalLockHolder implements InternalLockHolder {
private final Map<String, ZookeeperInternalLock> locks = new
LinkedHashMap<>();
private final CuratorFramework client;
- /**
- * Get internal mutex lock.
- *
- * @param lockName lock name
- * @return internal mutex lock
- */
- public synchronized Lock getInternalMutexLock(final String lockName) {
- ZookeeperInternalLock result = locks.get(lockName);
- if (Objects.isNull(result)) {
- result = new ZookeeperInternalLock(new
InterProcessSemaphoreMutex(client, lockName));
- locks.put(lockName, result);
- }
- return result;
- }
-
- /**
- * Get internal reentrant mutex lock.
- *
- * @param lockName lock name
- * @return internal reentrant mutex lock
- */
- public synchronized Lock getInternalReentrantMutexLock(final String
lockName) {
- ZookeeperInternalLock result = locks.get(lockName);
+ @Override
+ public InternalLock getInternalLock(final String lockKey) {
+ ZookeeperInternalLock result = locks.get(lockKey);
if (Objects.isNull(result)) {
- result = new ZookeeperInternalLock(new InterProcessMutex(client,
lockName));
- locks.put(lockName, result);
+ result = new ZookeeperInternalLock(new
InterProcessSemaphoreMutex(client, lockKey));
+ locks.put(lockKey, result);
}
return result;
}
@@ -75,30 +54,14 @@ public final class ZookeeperInternalLockHolder {
* Zookeeper internal lock.
*/
@RequiredArgsConstructor
- public static class ZookeeperInternalLock implements Lock {
+ public static class ZookeeperInternalLock implements InternalLock {
private final InterProcessLock lock;
@Override
- public void lock() {
+ public boolean tryLock(final long timeout) {
try {
- lock.acquire();
- // CHECKSTYLE:OFF
- } catch (final Exception ex) {
- // CHECKSTYLE:ON
- CuratorZookeeperExceptionHandler.handleException(ex);
- }
- }
-
- @Override
- public boolean tryLock() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean tryLock(final long time, final TimeUnit timeUnit) {
- try {
- return lock.acquire(time, timeUnit);
+ return lock.acquire(timeout, TimeUnit.MILLISECONDS);
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
@@ -117,15 +80,5 @@ public final class ZookeeperInternalLockHolder {
CuratorZookeeperExceptionHandler.handleException(ex);
}
}
-
- @Override
- public void lockInterruptibly() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Condition newCondition() {
- throw new UnsupportedOperationException();
- }
}
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/lock/ShardingSphereStandaloneLock.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/lock/ShardingSphereStandaloneLock.java
index 13a34f4d34a..e2d638e8085 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/lock/ShardingSphereStandaloneLock.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/lock/ShardingSphereStandaloneLock.java
@@ -35,16 +35,7 @@ public final class ShardingSphereStandaloneLock implements
ShardingSphereLock {
private final Map<String, ReentrantLock> locks = new ConcurrentHashMap<>();
@Override
- public boolean tryLock(final String lockName) {
- return tryLock(lockName, DEFAULT_TRY_LOCK_TIMEOUT_MILLISECONDS);
- }
-
- @Override
- public boolean tryLock(final String lockName, final long timeoutMillis) {
- return innerTryLock(lockName, timeoutMillis);
- }
-
- private synchronized boolean innerTryLock(final String lockName, final
long timeoutMillis) {
+ public synchronized boolean tryLock(final String lockName, final long
timeoutMillis) {
Preconditions.checkNotNull(lockName, "Try lock args lockName name can
not be null.");
ReentrantLock lock = locks.get(lockName);
if (null == lock) {
@@ -67,17 +58,4 @@ public final class ShardingSphereStandaloneLock implements
ShardingSphereLock {
Preconditions.checkNotNull(lockName, "Release lock args lockName name
can not be null.");
locks.get(lockName).unlock();
}
-
- @Override
- public boolean isLocked(final String lockName) {
- Preconditions.checkNotNull(lockName, "Is locked args lockName name can
not be null.");
- if (locks.isEmpty()) {
- return false;
- }
- ReentrantLock lock = locks.get(lockName);
- if (null == lock) {
- return false;
- }
- return lock.isLocked();
- }
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/lock/StandaloneLockPersistService.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/lock/StandaloneLockPersistService.java
index e7acba162e9..524bfc6d423 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/lock/StandaloneLockPersistService.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/lock/StandaloneLockPersistService.java
@@ -30,7 +30,7 @@ public final class StandaloneLockPersistService implements
LockPersistService {
@Override
public boolean tryLock(final LockDefinition lockDefinition, final long
timeoutMillis) {
- return standaloneLock.tryLock(lockDefinition.getLockKey());
+ return standaloneLock.tryLock(lockDefinition.getLockKey(),
timeoutMillis);
}
@Override
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngine.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngine.java
index 516efd32e84..86ce03274b4 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngine.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngine.java
@@ -42,11 +42,8 @@ import
org.apache.shardingsphere.infra.metadata.database.schema.event.MetaDataRe
import
org.apache.shardingsphere.infra.metadata.database.schema.util.SystemSchemaUtil;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import
org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
-import org.apache.shardingsphere.mode.lock.engine.LockJudgeEngine;
-import org.apache.shardingsphere.mode.lock.engine.LockJudgeEngineFactory;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import
org.apache.shardingsphere.proxy.backend.exception.RuleNotExistedException;
-import
org.apache.shardingsphere.proxy.backend.exception.UnsupportedUpdateOperationException;
import
org.apache.shardingsphere.proxy.backend.handler.data.DatabaseBackendHandler;
import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseCell;
import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseRow;
@@ -86,8 +83,6 @@ public abstract class DatabaseCommunicationEngine implements
DatabaseBackendHand
private final BackendConnection<?> backendConnection;
- private final LockJudgeEngine lockJudgeEngine;
-
public DatabaseCommunicationEngine(final String driverType, final
ShardingSphereDatabase database, final LogicSQL logicSQL, final
BackendConnection<?> backendConnection) {
SQLStatementContext<?> sqlStatementContext =
logicSQL.getSqlStatementContext();
failedIfBackendNotReady(backendConnection.getConnectionSession(),
sqlStatementContext);
@@ -96,7 +91,6 @@ public abstract class DatabaseCommunicationEngine implements
DatabaseBackendHand
this.logicSQL = logicSQL;
this.backendConnection = backendConnection;
metadataRefreshEngine = new MetaDataRefreshEngine(database,
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps());
- lockJudgeEngine = LockJudgeEngineFactory.getInstance();
if (sqlStatementContext instanceof CursorAvailable) {
prepareCursorStatementContext((CursorAvailable)
sqlStatementContext, backendConnection.getConnectionSession());
}
@@ -239,11 +233,4 @@ public abstract class DatabaseCommunicationEngine
implements DatabaseBackendHand
}
return new QueryResponseRow(cells);
}
-
- protected void checkLockedDatabase(final ExecutionContext
executionContext) {
- if
(lockJudgeEngine.isLocked(ProxyContext.getInstance().getContextManager().getInstanceContext().getLockContext(),
- backendConnection.getConnectionSession().getDatabaseName(),
executionContext.getSqlStatementContext())) {
- throw new
UnsupportedUpdateOperationException(backendConnection.getConnectionSession().getDatabaseName());
- }
- }
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
index 804b862cd1e..414c05bdaa8 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
@@ -129,7 +129,6 @@ public final class JDBCDatabaseCommunicationEngine extends
DatabaseCommunication
return new
UpdateResponseHeader(executionContext.getSqlStatementContext().getSqlStatement());
}
proxySQLExecutor.checkExecutePrerequisites(executionContext);
- checkLockedDatabase(executionContext);
List result = proxySQLExecutor.execute(executionContext);
refreshMetaData(executionContext);
Object executeResultSample = result.iterator().next();
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxDatabaseCommunicationEngine.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxDatabaseCommunicationEngine.java
index 85d9dd5b70a..21c96a3d60e 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxDatabaseCommunicationEngine.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxDatabaseCommunicationEngine.java
@@ -68,7 +68,6 @@ public final class VertxDatabaseCommunicationEngine extends
DatabaseCommunicatio
return Future.succeededFuture(new
UpdateResponseHeader(executionContext.getSqlStatementContext().getSqlStatement()));
}
reactiveProxySQLExecutor.checkExecutePrerequisites(executionContext);
- checkLockedDatabase(executionContext);
return
reactiveProxySQLExecutor.execute(executionContext).compose(result -> {
try {
refreshMetaData(executionContext);
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/DistSQLBackendHandlerFactory.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/DistSQLBackendHandlerFactory.java
index df19612ff8b..0d910a543bc 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/DistSQLBackendHandlerFactory.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/DistSQLBackendHandlerFactory.java
@@ -21,14 +21,9 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.distsql.parser.statement.DistSQLStatement;
import org.apache.shardingsphere.distsql.parser.statement.ral.RALStatement;
-import
org.apache.shardingsphere.distsql.parser.statement.ral.UpdatableRALStatement;
import org.apache.shardingsphere.distsql.parser.statement.rdl.RDLStatement;
import org.apache.shardingsphere.distsql.parser.statement.rql.RQLStatement;
import org.apache.shardingsphere.distsql.parser.statement.rul.RULStatement;
-import org.apache.shardingsphere.infra.lock.LockContext;
-import org.apache.shardingsphere.mode.lock.definition.LockDefinitionFactory;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import
org.apache.shardingsphere.proxy.backend.exception.UnsupportedUpdateOperationException;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.RALBackendHandlerFactory;
@@ -57,13 +52,9 @@ public final class DistSQLBackendHandlerFactory {
return RQLBackendHandlerFactory.newInstance((RQLStatement)
sqlStatement, connectionSession);
}
if (sqlStatement instanceof RDLStatement) {
- checkDatabaseLocked(connectionSession);
return RDLBackendHandlerFactory.newInstance((RDLStatement)
sqlStatement, connectionSession);
}
if (sqlStatement instanceof RALStatement) {
- if (sqlStatement instanceof UpdatableRALStatement) {
- checkDatabaseLocked(connectionSession);
- }
return RALBackendHandlerFactory.newInstance((RALStatement)
sqlStatement, connectionSession);
}
if (sqlStatement instanceof RULStatement) {
@@ -71,15 +62,4 @@ public final class DistSQLBackendHandlerFactory {
}
throw new
UnsupportedOperationException(sqlStatement.getClass().getCanonicalName());
}
-
- private static void checkDatabaseLocked(final ConnectionSession
connectionSession) {
- String databaseName = connectionSession.getDatabaseName();
- if (null == databaseName) {
- return;
- }
- LockContext lockContext =
ProxyContext.getInstance().getContextManager().getInstanceContext().getLockContext();
- if
(lockContext.isLocked(LockDefinitionFactory.newDatabaseLockDefinition(databaseName)))
{
- throw new UnsupportedUpdateOperationException(databaseName);
- }
- }
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/fixture/ClusterPersistRepositoryFixture.java
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/fixture/ClusterPersistRepositoryFixture.java
index 9fd4902ed7f..7bd8778d649 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/fixture/ClusterPersistRepositoryFixture.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/fixture/ClusterPersistRepositoryFixture.java
@@ -26,7 +26,6 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.locks.Lock;
public final class ClusterPersistRepositoryFixture implements
ClusterPersistRepository {
@@ -73,16 +72,6 @@ public final class ClusterPersistRepositoryFixture
implements ClusterPersistRepo
return false;
}
- @Override
- public Lock getInternalMutexLock(final String lockName) {
- return null;
- }
-
- @Override
- public Lock getInternalReentrantMutexLock(final String lockName) {
- return null;
- }
-
@Override
public void close() {
REGISTRY_DATA.clear();
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
index 1110abf7d33..1067121f09b 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
@@ -30,7 +30,6 @@ import
org.apache.shardingsphere.infra.exception.DatabaseNotExistedException;
import org.apache.shardingsphere.proxy.backend.exception.CircuitBreakException;
import
org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
import
org.apache.shardingsphere.proxy.backend.exception.DBDropNotExistsException;
-import
org.apache.shardingsphere.proxy.backend.exception.UnsupportedUpdateOperationException;
import
org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
import
org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
import
org.apache.shardingsphere.proxy.backend.exception.RuleNotExistedException;
@@ -116,10 +115,6 @@ public final class MySQLErrPacketFactory {
if (cause instanceof RuleNotExistedException || cause instanceof
ResourceNotExistedException) {
return new MySQLErrPacket(1,
MySQLServerErrorCode.ER_SP_DOES_NOT_EXIST);
}
- if (cause instanceof UnsupportedUpdateOperationException) {
- UnsupportedUpdateOperationException exception =
(UnsupportedUpdateOperationException) cause;
- return new MySQLErrPacket(1,
CommonErrorCode.DATABASE_WRITE_LOCKED, exception.getDatabaseName());
- }
if (cause instanceof TableLockWaitTimeoutException) {
TableLockWaitTimeoutException exception =
(TableLockWaitTimeoutException) cause;
return new MySQLErrPacket(1,
CommonErrorCode.TABLE_LOCK_WAIT_TIMEOUT, exception.getTableName(),
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrPacketFactory.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrPacketFactory.java
index af1a46f71bd..ca92e5e0505 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrPacketFactory.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrPacketFactory.java
@@ -26,7 +26,6 @@ import
org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQ
import
org.apache.shardingsphere.infra.exception.InsertColumnsAndValuesMismatchedException;
import
org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
import
org.apache.shardingsphere.proxy.backend.exception.InTransactionException;
-import
org.apache.shardingsphere.proxy.backend.exception.UnsupportedUpdateOperationException;
import
org.apache.shardingsphere.proxy.frontend.postgresql.authentication.exception.InvalidAuthorizationSpecificationException;
import
org.apache.shardingsphere.proxy.frontend.postgresql.authentication.exception.PostgreSQLAuthenticationException;
import
org.apache.shardingsphere.proxy.frontend.postgresql.authentication.exception.PostgreSQLProtocolViolationException;
@@ -82,10 +81,6 @@ public final class PostgreSQLErrPacketFactory {
return
PostgreSQLErrorResponsePacket.newBuilder(PostgreSQLMessageSeverityLevel.ERROR,
PostgreSQLErrorCode.DUPLICATE_DATABASE,
String.format(PostgreSQLErrorCode.DUPLICATE_DATABASE.getConditionName(),
((DBCreateExistsException) cause).getDatabaseName())).build();
}
- if (cause instanceof UnsupportedUpdateOperationException) {
- UnsupportedUpdateOperationException exception =
(UnsupportedUpdateOperationException) cause;
- return
PostgreSQLErrorResponsePacket.newBuilder(PostgreSQLMessageSeverityLevel.ERROR,
PostgreSQLErrorCode.MODIFYING_SQL_DATA_NOT_PERMITTED,
exception.getErrorMessage()).build();
- }
// TODO PostgreSQL need consider FrontendConnectionLimitException
return createErrorResponsePacketForUnknownException(cause);
}
diff --git
a/shardingsphere-test/shardingsphere-integration-driver-test/src/test/java/org/apache/shardingsphere/driver/fixture/TestClusterPersistRepository.java
b/shardingsphere-test/shardingsphere-integration-driver-test/src/test/java/org/apache/shardingsphere/driver/fixture/TestClusterPersistRepository.java
index 54e65acb303..73c95ef736b 100644
---
a/shardingsphere-test/shardingsphere-integration-driver-test/src/test/java/org/apache/shardingsphere/driver/fixture/TestClusterPersistRepository.java
+++
b/shardingsphere-test/shardingsphere-integration-driver-test/src/test/java/org/apache/shardingsphere/driver/fixture/TestClusterPersistRepository.java
@@ -27,7 +27,6 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.locks.Lock;
public final class TestClusterPersistRepository implements
ClusterPersistRepository {
@@ -75,16 +74,6 @@ public final class TestClusterPersistRepository implements
ClusterPersistReposit
return false;
}
- @Override
- public Lock getInternalMutexLock(final String lockName) {
- return null;
- }
-
- @Override
- public Lock getInternalReentrantMutexLock(final String lockName) {
- return null;
- }
-
@Override
public void close() {
registryData.clear();