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 f3162a2e8d9 Add lock judge engine (#18072)
f3162a2e8d9 is described below

commit f3162a2e8d9e60c8b1a159d138bc5a30e3b11f4e
Author: gin <[email protected]>
AuthorDate: Mon May 30 19:30:46 2022 +0800

    Add lock judge engine (#18072)
---
 .../shardingsphere/infra/lock/LockJudgeEngine.java | 35 ++++++++++++++++
 .../lock/ShardingSphereLockJudgeEngine.java        | 49 ++++++++++++++++++++++
 .../communication/DatabaseCommunicationEngine.java | 27 +++---------
 3 files changed, 90 insertions(+), 21 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/LockJudgeEngine.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/LockJudgeEngine.java
new file mode 100644
index 00000000000..8142fad6cce
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/lock/LockJudgeEngine.java
@@ -0,0 +1,35 @@
+/*
+ * 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.infra.lock;
+
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+
+/**
+ * Lock judge engine.
+ */
+public interface LockJudgeEngine {
+    
+    /**
+     * Is locked.
+     *
+     * @param databaseName database name
+     * @param sqlStatement SQL statement
+     * @return is locked or not
+     */
+    boolean isLocked(String databaseName, SQLStatement sqlStatement);
+}
diff --git 
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/lock/ShardingSphereLockJudgeEngine.java
 
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/lock/ShardingSphereLockJudgeEngine.java
new file mode 100644
index 00000000000..f11396f34f6
--- /dev/null
+++ 
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/lock/ShardingSphereLockJudgeEngine.java
@@ -0,0 +1,49 @@
+/*
+ * 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.manager.lock;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.lock.LockContext;
+import org.apache.shardingsphere.infra.lock.LockJudgeEngine;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
+
+/**
+ * Lock judge engine for ShardingSphere.
+ */
+@RequiredArgsConstructor
+public final class ShardingSphereLockJudgeEngine implements LockJudgeEngine {
+    
+    private final LockContext lockContext;
+    
+    @Override
+    public boolean isLocked(final String databaseName, final SQLStatement 
sqlStatement) {
+        if (sqlStatement instanceof DMLStatement) {
+            if (sqlStatement instanceof SelectStatement) {
+                return false;
+            }
+            return lockContext.isLocked(databaseName);
+        }
+        if (sqlStatement instanceof DDLStatement) {
+            return lockContext.isLocked(databaseName);
+        }
+        return false;
+    }
+}
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 4722fd6f5d0..932c3df2e8c 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
@@ -31,11 +31,13 @@ import 
org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
 import 
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
 import 
org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
 import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.JDBCDriverType;
+import org.apache.shardingsphere.infra.lock.LockJudgeEngine;
 import org.apache.shardingsphere.infra.merge.MergeEngine;
 import org.apache.shardingsphere.infra.merge.result.MergedResult;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import 
org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
+import 
org.apache.shardingsphere.mode.manager.lock.ShardingSphereLockJudgeEngine;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import 
org.apache.shardingsphere.proxy.backend.exception.DatabaseLockedException;
 import org.apache.shardingsphere.proxy.backend.response.data.QueryResponseCell;
@@ -46,10 +48,6 @@ import 
org.apache.shardingsphere.proxy.backend.response.header.query.QueryHeader
 import 
org.apache.shardingsphere.proxy.backend.response.header.query.QueryHeaderBuilderEngine;
 import 
org.apache.shardingsphere.proxy.backend.response.header.query.QueryResponseHeader;
 import 
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
-import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
-import 
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
 
 import java.sql.SQLException;
 import java.util.ArrayList;
@@ -84,6 +82,8 @@ public abstract class DatabaseCommunicationEngine<T> {
     
     private final BackendConnection<?> backendConnection;
     
+    private final LockJudgeEngine lockJudgeEngine;
+    
     public DatabaseCommunicationEngine(final String driverType, final 
ShardingSphereDatabase database, final LogicSQL logicSQL, final 
BackendConnection<?> backendConnection) {
         this.driverType = driverType;
         this.database = database;
@@ -94,6 +94,7 @@ public abstract class DatabaseCommunicationEngine<T> {
                 
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getOptimizerContext().getFederationMetaData().getDatabases().get(databaseName),
                 
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getOptimizerContext().getPlannerContexts(),
                 
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps());
+        lockJudgeEngine = new 
ShardingSphereLockJudgeEngine(ProxyContext.getInstance().getContextManager().getInstanceContext().getLockContext());
     }
     
     /**
@@ -219,23 +220,7 @@ public abstract class DatabaseCommunicationEngine<T> {
     }
     
     protected void checkLockedDatabase(final ExecutionContext 
executionContext) {
-        if 
(isLockedDatabase(backendConnection.getConnectionSession().getDatabaseName())) {
-            
lockedWrite(executionContext.getSqlStatementContext().getSqlStatement());
-        }
-    }
-    
-    private boolean isLockedDatabase(final String databaseName) {
-        return 
ProxyContext.getInstance().getContextManager().getInstanceContext().getLockContext().isLocked(databaseName);
-    }
-    
-    private void lockedWrite(final SQLStatement sqlStatement) {
-        if (sqlStatement instanceof DMLStatement) {
-            if (sqlStatement instanceof SelectStatement) {
-                return;
-            }
-            throw new 
DatabaseLockedException(backendConnection.getConnectionSession().getDatabaseName());
-        }
-        if (sqlStatement instanceof DDLStatement) {
+        if 
(lockJudgeEngine.isLocked(backendConnection.getConnectionSession().getDatabaseName(),
 executionContext.getSqlStatementContext().getSqlStatement())) {
             throw new 
DatabaseLockedException(backendConnection.getConnectionSession().getDatabaseName());
         }
     }

Reply via email to