You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/11/15 11:48:08 UTC
[shardingsphere] branch master updated: Clear up jdbc lock (#13620)
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 573670f Clear up jdbc lock (#13620)
573670f is described below
commit 573670fe572894eaf8a3e93f4b1a0664ce3eb83e
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Mon Nov 15 19:47:25 2021 +0800
Clear up jdbc lock (#13620)
Co-authored-by: shardingsphere <de...@shardingsphere.apache.org>
---
.../driver/executor/DriverJDBCExecutor.java | 25 ++++-
.../driver/executor/JDBCLockEngine.java | 124 ---------------------
2 files changed, 20 insertions(+), 129 deletions(-)
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/DriverJDBCExecutor.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/DriverJDBCExecutor.java
index 21fb190..5d03ae8 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/DriverJDBCExecutor.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/DriverJDBCExecutor.java
@@ -21,7 +21,7 @@ import lombok.Getter;
import org.apache.shardingsphere.driver.executor.callback.ExecuteQueryCallback;
import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.infra.context.refresher.MetaDataRefreshEngine;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
@@ -31,10 +31,13 @@ import org.apache.shardingsphere.infra.executor.sql.process.ExecuteProcessEngine
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
+import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
+import java.util.stream.Collectors;
/**
* Driver JDBC executor.
@@ -48,13 +51,14 @@ public final class DriverJDBCExecutor {
@Getter
private final JDBCExecutor jdbcExecutor;
- private final JDBCLockEngine jdbcLockEngine;
+ private final MetaDataRefreshEngine metadataRefreshEngine;
public DriverJDBCExecutor(final String schemaName, final MetaDataContexts metaDataContexts, final JDBCExecutor jdbcExecutor) {
this.schemaName = schemaName;
this.metaDataContexts = metaDataContexts;
this.jdbcExecutor = jdbcExecutor;
- jdbcLockEngine = new JDBCLockEngine(schemaName, metaDataContexts, jdbcExecutor);
+ metadataRefreshEngine = new MetaDataRefreshEngine(metaDataContexts.getMetaData(schemaName),
+ metaDataContexts.getOptimizerContext().getMetaData().getSchemas().get(schemaName), metaDataContexts.getProps());
}
/**
@@ -93,7 +97,7 @@ public final class DriverJDBCExecutor {
try {
ExecuteProcessEngine.initialize(logicSQL, executionGroupContext, metaDataContexts.getProps());
SQLStatementContext<?> sqlStatementContext = logicSQL.getSqlStatementContext();
- List<Integer> results = jdbcLockEngine.execute(executionGroupContext, sqlStatementContext, routeUnits, callback);
+ List<Integer> results = doExecute(executionGroupContext, sqlStatementContext.getSqlStatement(), routeUnits, callback);
int result = isNeedAccumulate(metaDataContexts.getMetaData(schemaName).getRuleMetaData().getRules(), sqlStatementContext) ? accumulate(results) : results.get(0);
ExecuteProcessEngine.finish(executionGroupContext.getExecutionID());
return result;
@@ -124,7 +128,7 @@ public final class DriverJDBCExecutor {
final Collection<RouteUnit> routeUnits, final JDBCExecutorCallback<Boolean> callback) throws SQLException {
try {
ExecuteProcessEngine.initialize(logicSQL, executionGroupContext, metaDataContexts.getProps());
- List<Boolean> results = jdbcLockEngine.execute(executionGroupContext, logicSQL.getSqlStatementContext(), routeUnits, callback);
+ List<Boolean> results = doExecute(executionGroupContext, logicSQL.getSqlStatementContext().getSqlStatement(), routeUnits, callback);
boolean result = null != results && !results.isEmpty() && null != results.get(0) && results.get(0);
ExecuteProcessEngine.finish(executionGroupContext.getExecutionID());
return result;
@@ -132,4 +136,15 @@ public final class DriverJDBCExecutor {
ExecuteProcessEngine.clean();
}
}
+
+ private <T> List<T> doExecute(final ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext, final SQLStatement sqlStatement, final Collection<RouteUnit> routeUnits,
+ final JDBCExecutorCallback<T> callback) throws SQLException {
+ List<T> results = jdbcExecutor.execute(executionGroupContext, callback);
+ refreshMetaData(sqlStatement, routeUnits);
+ return results;
+ }
+
+ private void refreshMetaData(final SQLStatement sqlStatement, final Collection<RouteUnit> routeUnits) throws SQLException {
+ metadataRefreshEngine.refresh(sqlStatement, routeUnits.stream().map(each -> each.getDataSourceMapper().getLogicName()).collect(Collectors.toList()));
+ }
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/JDBCLockEngine.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/JDBCLockEngine.java
deleted file mode 100644
index 612c68a..0000000
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/executor/JDBCLockEngine.java
+++ /dev/null
@@ -1,124 +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.driver.executor;
-
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
-import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
-import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
-import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
-import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback;
-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.infra.route.context.RouteUnit;
-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.List;
-import java.util.stream.Collectors;
-
-/**
- * JDBC lock engine.
- */
-public final class JDBCLockEngine {
-
- private final String schemaName;
-
- private final MetaDataContexts metaDataContexts;
-
- private final JDBCExecutor jdbcExecutor;
-
- private final MetaDataRefreshEngine metadataRefreshEngine;
-
- private final Collection<String> lockNames = new ArrayList<>();
-
- public JDBCLockEngine(final String schemaName, final MetaDataContexts metaDataContexts, final JDBCExecutor jdbcExecutor) {
- this.schemaName = schemaName;
- this.metaDataContexts = metaDataContexts;
- this.jdbcExecutor = jdbcExecutor;
- metadataRefreshEngine = new MetaDataRefreshEngine(metaDataContexts.getMetaData(schemaName),
- metaDataContexts.getOptimizerContext().getMetaData().getSchemas().get(schemaName), metaDataContexts.getProps());
- }
-
- /**
- * Execute.
- *
- * @param executionGroupContext execution group context
- * @param sqlStatementContext sql statement context
- * @param routeUnits route units
- * @param callback callback
- * @param <T> the type of return value
- * @return result
- * @throws SQLException SQL exception
- */
- public <T> List<T> execute(final ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext, final SQLStatementContext<?> sqlStatementContext,
- final Collection<RouteUnit> routeUnits, final JDBCExecutorCallback<T> callback) throws SQLException {
- SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
- if (metaDataContexts.getLock().isPresent()) {
- ShardingSphereLock lock = metaDataContexts.getLock().get();
- try {
- if (sqlStatement instanceof DDLStatement) {
- tryTableLock(lock, sqlStatementContext.getTablesContext().getTableNames());
- } else if (sqlStatement instanceof DMLStatement && !(sqlStatement instanceof SelectStatement)) {
- checkTableLock(lock, sqlStatementContext.getTablesContext().getTableNames());
- }
- return doExecute(executionGroupContext, routeUnits, callback, sqlStatement);
- } finally {
- if (!lockNames.isEmpty()) {
- lockNames.forEach(lock::releaseLock);
- }
- }
- }
- return doExecute(executionGroupContext, routeUnits, callback, sqlStatement);
- }
-
- private void tryTableLock(final ShardingSphereLock lock, final Collection<String> tableNames) throws SQLException {
- for (String each : tableNames) {
- String lockName = LockNameUtil.getTableLockName(schemaName, each);
- if (!lock.tryLock(lockName)) {
- throw new SQLException(String.format("Table %s lock wait timeout of %s ms exceeded", each, lock.getDefaultTimeOut()));
- }
- lockNames.add(lockName);
- }
- }
-
- private void checkTableLock(final ShardingSphereLock lock, final Collection<String> tableNames) throws SQLException {
- for (String each : tableNames) {
- if (lock.isLocked(LockNameUtil.getTableLockName(schemaName, each))) {
- throw new SQLException(String.format("Table %s is locked", each));
- }
- }
- }
-
- private <T> List<T> doExecute(final ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext, final Collection<RouteUnit> routeUnits,
- final JDBCExecutorCallback<T> callback, final SQLStatement sqlStatement) throws SQLException {
- List<T> results = jdbcExecutor.execute(executionGroupContext, callback);
- refreshMetaData(sqlStatement, routeUnits);
- return results;
- }
-
- private void refreshMetaData(final SQLStatement sqlStatement, final Collection<RouteUnit> routeUnits) throws SQLException {
- metadataRefreshEngine.refresh(sqlStatement, routeUnits.stream().map(each -> each.getDataSourceMapper().getLogicName()).collect(Collectors.toList()));
- }
-}