This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 3301da7  Clear up proxy lock (#13614)
3301da7 is described below

commit 3301da7f285412c00c6eda0ec651df541d00dc66
Author: Haoran Meng <[email protected]>
AuthorDate: Mon Nov 15 17:37:24 2021 +0800

    Clear up proxy lock (#13614)
    
    Co-authored-by: shardingsphere <[email protected]>
---
 .../properties/ConfigurationPropertyKey.java       |   5 -
 .../communication/DatabaseCommunicationEngine.java |  22 +++-
 .../backend/communication/ProxyLockEngine.java     | 112 ---------------------
 .../proxy/backend/context/ProxyContext.java        |  11 --
 4 files changed, 17 insertions(+), 133 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
index 759e2b9..8b1c86a 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/properties/ConfigurationPropertyKey.java
@@ -88,11 +88,6 @@ public enum ConfigurationPropertyKey implements 
TypedPropertyKey {
     LOCK_WAIT_TIMEOUT_MILLISECONDS("lock-wait-timeout-milliseconds", 
String.valueOf(50000L), long.class),
     
     /**
-     * Whether enable lock.
-     */
-    LOCK_ENABLED("lock-enabled", String.valueOf(Boolean.FALSE), boolean.class),
-    
-    /**
      * Proxy backend query fetch size. A larger value may increase the memory 
usage of ShardingSphere Proxy.
      * The default value is -1, which means set the minimum value for 
different JDBC drivers.
      */
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 a5dfcb7..0927e8e 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
@@ -44,6 +44,7 @@ import 
org.apache.shardingsphere.proxy.backend.response.header.query.QueryRespon
 import 
org.apache.shardingsphere.proxy.backend.response.header.query.impl.QueryHeader;
 import 
org.apache.shardingsphere.proxy.backend.response.header.query.impl.QueryHeaderBuilder;
 import 
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -72,12 +73,12 @@ public final class DatabaseCommunicationEngine {
     
     private final KernelProcessor kernelProcessor;
     
+    private final MetaDataRefreshEngine metadataRefreshEngine;
+    
     private List<QueryHeader> queryHeaders;
     
     private MergedResult mergedResult;
     
-    private ProxyLockEngine proxyLockEngine;
-    
     private final Collection<Statement> cachedStatements = new 
CopyOnWriteArrayList<>();
     
     private final Collection<ResultSet> cachedResultSets = new 
CopyOnWriteArrayList<>();
@@ -88,9 +89,9 @@ public final class DatabaseCommunicationEngine {
         this.logicSQL = logicSQL;
         proxySQLExecutor = new ProxySQLExecutor(driverType, backendConnection, 
this);
         kernelProcessor = new KernelProcessor();
-        proxyLockEngine = new ProxyLockEngine(proxySQLExecutor, new 
MetaDataRefreshEngine(metaData,
+        metadataRefreshEngine = new MetaDataRefreshEngine(metaData,
                 
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getOptimizerContext().getMetaData().getSchemas().get(backendConnection.getSchemaName()),
-                
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getProps()),
 backendConnection.getSchemaName());
+                
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getProps());
     }
     
     /**
@@ -123,13 +124,24 @@ public final class DatabaseCommunicationEngine {
             return new 
UpdateResponseHeader(executionContext.getSqlStatementContext().getSqlStatement());
         }
         proxySQLExecutor.checkExecutePrerequisites(executionContext);
-        Collection<ExecuteResult> executeResults = 
proxyLockEngine.execute(executionContext);
+        Collection<ExecuteResult> executeResults = doExecute(executionContext);
         ExecuteResult executeResultSample = executeResults.iterator().next();
         return executeResultSample instanceof QueryResult
                 ? processExecuteQuery(executionContext, 
executeResults.stream().map(each -> (QueryResult) 
each).collect(Collectors.toList()), (QueryResult) executeResultSample)
                 : processExecuteUpdate(executionContext, 
executeResults.stream().map(each -> (UpdateResult) 
each).collect(Collectors.toList()));
     }
     
+    private Collection<ExecuteResult> doExecute(final ExecutionContext 
executionContext) throws SQLException {
+        Collection<ExecuteResult> result = 
proxySQLExecutor.execute(executionContext);
+        refreshMetaData(executionContext);
+        return result;
+    }
+    
+    private void refreshMetaData(final ExecutionContext executionContext) 
throws SQLException {
+        SQLStatement sqlStatement = 
executionContext.getSqlStatementContext().getSqlStatement();
+        metadataRefreshEngine.refresh(sqlStatement, 
executionContext.getRouteContext().getRouteUnits().stream().map(each -> 
each.getDataSourceMapper().getLogicName()).collect(Collectors.toList()));
+    }
+    
     private QueryResponseHeader processExecuteQuery(final ExecutionContext 
executionContext, final List<QueryResult> queryResults, final QueryResult 
queryResultSample) throws SQLException {
         queryHeaders = createQueryHeaders(executionContext, queryResultSample);
         mergedResult = mergeQuery(executionContext.getSqlStatementContext(), 
queryResults);
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/ProxyLockEngine.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/ProxyLockEngine.java
deleted file mode 100644
index d8770f8..0000000
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/ProxyLockEngine.java
+++ /dev/null
@@ -1,112 +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.proxy.backend.communication;
-
-import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
-import 
org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
-import org.apache.shardingsphere.infra.lock.LockNameUtil;
-import org.apache.shardingsphere.infra.lock.ShardingSphereLock;
-import org.apache.shardingsphere.infra.context.refresher.MetaDataRefreshEngine;
-import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import 
org.apache.shardingsphere.proxy.backend.exception.TableLockWaitTimeoutException;
-import org.apache.shardingsphere.proxy.backend.exception.TableLockedException;
-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;
-import java.util.Collection;
-import java.util.stream.Collectors;
-
-/**
- * Proxy lock engine.
- */
-public final class ProxyLockEngine {
-    
-    private final ProxySQLExecutor proxySQLExecutor;
-    
-    private final MetaDataRefreshEngine metadataRefreshEngine;
-    
-    private final String schemaName;
-    
-    private final Collection<String> lockNames = new ArrayList<>();
-    
-    public ProxyLockEngine(final ProxySQLExecutor proxySQLExecutor, final 
MetaDataRefreshEngine metadataRefreshEngine, final String schemaName) {
-        this.proxySQLExecutor = proxySQLExecutor;
-        this.metadataRefreshEngine = metadataRefreshEngine;
-        this.schemaName = schemaName;
-    }
-    
-    /**
-     * Execute.
-     * 
-     * @param executionContext execution context
-     * @return collection of execute result
-     * @throws SQLException SQL exception
-     */
-    public Collection<ExecuteResult> execute(final ExecutionContext 
executionContext) throws SQLException {
-        if (ProxyContext.getInstance().getLock().isPresent()) {
-            ShardingSphereLock lock = 
ProxyContext.getInstance().getLock().get();
-            try {
-                SQLStatement sqlStatement = 
executionContext.getSqlStatementContext().getSqlStatement();
-                if (sqlStatement instanceof DDLStatement) {
-                    tryTableLock(lock, 
executionContext.getSqlStatementContext().getTablesContext().getTableNames());
-                } else if (sqlStatement instanceof DMLStatement && 
!(sqlStatement instanceof SelectStatement)) {
-                    checkTableLock(lock, 
executionContext.getSqlStatementContext().getTablesContext().getTableNames());
-                }
-                return doExecute(executionContext);
-            } finally {
-                if (!lockNames.isEmpty()) {
-                    lockNames.forEach(lock::releaseLock);
-                }
-            }
-        }
-        return doExecute(executionContext);
-    }
-    
-    private void tryTableLock(final ShardingSphereLock lock, final 
Collection<String> tableNames) {
-        for (String each : tableNames) {
-            String lockName = LockNameUtil.getTableLockName(schemaName, each);
-            if (!lock.tryLock(lockName)) {
-                throw new TableLockWaitTimeoutException(schemaName, each, 
lock.getDefaultTimeOut());
-            }
-            lockNames.add(lockName);
-        }
-    }
-    
-    private void checkTableLock(final ShardingSphereLock lock, final 
Collection<String> tableNames) {
-        for (String each : tableNames) {
-            if (lock.isLocked(LockNameUtil.getTableLockName(schemaName, 
each))) {
-                throw new TableLockedException(schemaName, each);
-            }
-        }
-    }
-    
-    private Collection<ExecuteResult> doExecute(final ExecutionContext 
executionContext) throws SQLException {
-        Collection<ExecuteResult> result = 
proxySQLExecutor.execute(executionContext);
-        refreshMetaData(executionContext);
-        return result;
-    }
-    
-    private void refreshMetaData(final ExecutionContext executionContext) 
throws SQLException {
-        SQLStatement sqlStatement = 
executionContext.getSqlStatementContext().getSqlStatement();
-        metadataRefreshEngine.refresh(sqlStatement, 
executionContext.getRouteContext().getRouteUnits().stream().map(each -> 
each.getDataSourceMapper().getLogicName()).collect(Collectors.toList()));
-    }
-}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/context/ProxyContext.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/context/ProxyContext.java
index fe7558a..6982fca 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/context/ProxyContext.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/context/ProxyContext.java
@@ -21,7 +21,6 @@ import com.google.common.base.Strings;
 import lombok.AccessLevel;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.infra.lock.ShardingSphereLock;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.state.StateContext;
@@ -32,7 +31,6 @@ import 
org.apache.shardingsphere.scaling.core.api.ScalingWorker;
 
 import java.util.Collection;
 import java.util.LinkedList;
-import java.util.Optional;
 
 /**
  * Proxy context.
@@ -98,15 +96,6 @@ public final class ProxyContext {
     }
     
     /**
-     * Get lock.
-     * 
-     * @return lock
-     */
-    public Optional<ShardingSphereLock> getLock() {
-        return Optional.empty();
-    }
-    
-    /**
      * Get state context.
      * 
      * @return state context

Reply via email to