You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by yx...@apache.org on 2022/08/19 02:07:39 UTC

[shardingsphere] branch master updated: Refactor RALUpdater, add databaseName parameter (#20286)

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

yx9o 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 247863535f2 Refactor RALUpdater, add databaseName parameter (#20286)
247863535f2 is described below

commit 247863535f29775b2473cf01c9d8a3da711585ac
Author: Hongsheng Zhong <sa...@126.com>
AuthorDate: Fri Aug 19 10:07:29 2022 +0800

    Refactor RALUpdater, add databaseName parameter (#20286)
---
 .../update/AddMigrationSourceResourceUpdater.java  |  2 +-
 .../handler/update/ApplyMigrationUpdater.java      |  2 +-
 .../handler/update/DropMigrationUpdater.java       |  2 +-
 .../handler/update/MigrateTableUpdater.java        |  2 +-
 .../handler/update/ResetMigrationUpdater.java      |  2 +-
 .../RestoreMigrationSourceWritingUpdater.java      |  2 +-
 .../handler/update/StartMigrationUpdater.java      |  2 +-
 .../update/StopMigrationSourceWritingUpdater.java  |  2 +-
 .../handler/update/StopMigrationUpdater.java       |  2 +-
 .../infra/distsql/update/RALUpdater.java           |  5 +++--
 .../infra/fixture/FixtureRALUpdater.java           |  2 +-
 .../distsql/ral/RALBackendHandlerFactory.java      |  2 +-
 .../UpdatableMigrationRALBackendHandler.java       | 26 ++++++++++++++++++++--
 13 files changed, 38 insertions(+), 15 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/AddMigrationSourceResourceUpdater.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/AddMigrationSourceResourceUpdater.java
index d131431d8b2..dc704c82e10 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/AddMigrationSourceResourceUpdater.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/AddMigrationSourceResourceUpdater.java
@@ -28,7 +28,7 @@ import org.apache.shardingsphere.migration.distsql.statement.AddMigrationSourceR
 public final class AddMigrationSourceResourceUpdater implements RALUpdater<AddMigrationSourceResourceStatement> {
     
     @Override
-    public void executeUpdate(final AddMigrationSourceResourceStatement sqlStatement) {
+    public void executeUpdate(final String databaseName, final AddMigrationSourceResourceStatement sqlStatement) {
         // TODO add migration source resource later
     }
     
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/ApplyMigrationUpdater.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/ApplyMigrationUpdater.java
index 793be29ceef..4602935a7b3 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/ApplyMigrationUpdater.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/ApplyMigrationUpdater.java
@@ -30,7 +30,7 @@ public final class ApplyMigrationUpdater implements RALUpdater<ApplyMigrationSta
     private static final MigrationJobPublicAPI JOB_API = PipelineJobPublicAPIFactory.getMigrationJobPublicAPI();
     
     @Override
-    public void executeUpdate(final ApplyMigrationStatement sqlStatement) {
+    public void executeUpdate(final String databaseName, final ApplyMigrationStatement sqlStatement) {
         JOB_API.switchClusterConfiguration(sqlStatement.getJobId());
     }
     
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/DropMigrationUpdater.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/DropMigrationUpdater.java
index eac7749d309..1566c95a790 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/DropMigrationUpdater.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/DropMigrationUpdater.java
@@ -30,7 +30,7 @@ public final class DropMigrationUpdater implements RALUpdater<DropMigrationState
     private static final MigrationJobPublicAPI JOB_API = PipelineJobPublicAPIFactory.getMigrationJobPublicAPI();
     
     @Override
-    public void executeUpdate(final DropMigrationStatement sqlStatement) {
+    public void executeUpdate(final String databaseName, final DropMigrationStatement sqlStatement) {
         JOB_API.remove(sqlStatement.getJobId());
     }
     
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/MigrateTableUpdater.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/MigrateTableUpdater.java
index 4842b7ac1eb..b90a65aa39d 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/MigrateTableUpdater.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/MigrateTableUpdater.java
@@ -30,7 +30,7 @@ public final class MigrateTableUpdater implements RALUpdater<MigrateTableStateme
     private static final MigrationJobPublicAPI JOB_API = PipelineJobPublicAPIFactory.getMigrationJobPublicAPI();
     
     @Override
-    public void executeUpdate(final MigrateTableStatement sqlStatement) {
+    public void executeUpdate(final String databaseName, final MigrateTableStatement sqlStatement) {
         // TODO implement migrate table
         JOB_API.getType();
     }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/ResetMigrationUpdater.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/ResetMigrationUpdater.java
index 5356a368483..f5ec2d5fc23 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/ResetMigrationUpdater.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/ResetMigrationUpdater.java
@@ -30,7 +30,7 @@ public final class ResetMigrationUpdater implements RALUpdater<ResetMigrationSta
     private static final MigrationJobPublicAPI JOB_API = PipelineJobPublicAPIFactory.getMigrationJobPublicAPI();
     
     @Override
-    public void executeUpdate(final ResetMigrationStatement sqlStatement) {
+    public void executeUpdate(final String databaseName, final ResetMigrationStatement sqlStatement) {
         JOB_API.reset(sqlStatement.getJobId());
     }
     
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/RestoreMigrationSourceWritingUpdater.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/RestoreMigrationSourceWritingUpdater.java
index 8907586f6e6..b6f49fbbe31 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/RestoreMigrationSourceWritingUpdater.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/RestoreMigrationSourceWritingUpdater.java
@@ -30,7 +30,7 @@ public final class RestoreMigrationSourceWritingUpdater implements RALUpdater<Re
     private static final MigrationJobPublicAPI JOB_API = PipelineJobPublicAPIFactory.getMigrationJobPublicAPI();
     
     @Override
-    public void executeUpdate(final RestoreMigrationSourceWritingStatement sqlStatement) {
+    public void executeUpdate(final String databaseName, final RestoreMigrationSourceWritingStatement sqlStatement) {
         JOB_API.restoreClusterWriteDB(sqlStatement.getJobId());
     }
     
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/StartMigrationUpdater.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/StartMigrationUpdater.java
index 3846be4ceba..c50db7c8666 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/StartMigrationUpdater.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/StartMigrationUpdater.java
@@ -30,7 +30,7 @@ public final class StartMigrationUpdater implements RALUpdater<StartMigrationSta
     private static final MigrationJobPublicAPI JOB_API = PipelineJobPublicAPIFactory.getMigrationJobPublicAPI();
     
     @Override
-    public void executeUpdate(final StartMigrationStatement sqlStatement) {
+    public void executeUpdate(final String databaseName, final StartMigrationStatement sqlStatement) {
         JOB_API.startDisabledJob(sqlStatement.getJobId());
     }
     
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/StopMigrationSourceWritingUpdater.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/StopMigrationSourceWritingUpdater.java
index c096aa65e99..ca4bdae4b4e 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/StopMigrationSourceWritingUpdater.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/StopMigrationSourceWritingUpdater.java
@@ -30,7 +30,7 @@ public final class StopMigrationSourceWritingUpdater implements RALUpdater<StopM
     private static final MigrationJobPublicAPI JOB_API = PipelineJobPublicAPIFactory.getMigrationJobPublicAPI();
     
     @Override
-    public void executeUpdate(final StopMigrationSourceWritingStatement sqlStatement) {
+    public void executeUpdate(final String databaseName, final StopMigrationSourceWritingStatement sqlStatement) {
         JOB_API.stopClusterWriteDB(sqlStatement.getJobId());
     }
     
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/StopMigrationUpdater.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/StopMigrationUpdater.java
index bb7883bdfbf..5d03fbeaa5a 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/StopMigrationUpdater.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/migration/distsql/handler/update/StopMigrationUpdater.java
@@ -30,7 +30,7 @@ public final class StopMigrationUpdater implements RALUpdater<StopMigrationState
     private static final MigrationJobPublicAPI JOB_API = PipelineJobPublicAPIFactory.getMigrationJobPublicAPI();
     
     @Override
-    public void executeUpdate(final StopMigrationStatement sqlStatement) {
+    public void executeUpdate(final String databaseName, final StopMigrationStatement sqlStatement) {
         JOB_API.stop(sqlStatement.getJobId());
     }
     
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/update/RALUpdater.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/update/RALUpdater.java
index d26590fabff..1ea60cdafa9 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/update/RALUpdater.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/update/RALUpdater.java
@@ -31,8 +31,9 @@ public interface RALUpdater<T extends SQLStatement> extends TypedSPI {
     
     /**
      * Execute update.
-     * 
+     *
+     * @param databaseName database name
      * @param sqlStatement updatable RAL statement
      */
-    void executeUpdate(T sqlStatement);
+    void executeUpdate(String databaseName, T sqlStatement);
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/fixture/FixtureRALUpdater.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/fixture/FixtureRALUpdater.java
index b068111a17c..119a49dcc51 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/fixture/FixtureRALUpdater.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/fixture/FixtureRALUpdater.java
@@ -22,7 +22,7 @@ import org.apache.shardingsphere.infra.distsql.update.RALUpdater;
 public final class FixtureRALUpdater implements RALUpdater<FixtureRALStatement> {
     
     @Override
-    public void executeUpdate(final FixtureRALStatement sqlStatement) {
+    public void executeUpdate(final String databaseName, final FixtureRALStatement sqlStatement) {
     }
     
     @Override
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
index 1f5f5684820..7488007b2ec 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/RALBackendHandlerFactory.java
@@ -149,7 +149,7 @@ public final class RALBackendHandlerFactory {
             return new QueryableMigrationRALBackendHandler(sqlStatement, connectionSession, (DatabaseDistSQLResultSet) resultSet);
         }
         if (sqlStatement instanceof UpdatableScalingRALStatement) {
-            return new UpdatableMigrationRALBackendHandler((UpdatableScalingRALStatement) sqlStatement);
+            return new UpdatableMigrationRALBackendHandler((UpdatableScalingRALStatement) sqlStatement, connectionSession);
         }
         if (sqlStatement instanceof QueryableGlobalRuleRALStatement) {
             return QueryableGlobalRuleRALBackendHandlerFactory.newInstance((QueryableGlobalRuleRALStatement) sqlStatement);
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/migration/update/UpdatableMigrationRALBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/migration/update/UpdatableMigrationRALBackendHandler.java
index 3403e6fe918..704e63b62ab 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/migration/update/UpdatableMigrationRALBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/migration/update/UpdatableMigrationRALBackendHandler.java
@@ -19,25 +19,47 @@ package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.migration.up
 
 import lombok.RequiredArgsConstructor;
 import lombok.Setter;
+import org.apache.shardingsphere.dialect.exception.syntax.database.NoDatabaseSelectedException;
+import org.apache.shardingsphere.dialect.exception.syntax.database.UnknownDatabaseException;
 import org.apache.shardingsphere.distsql.parser.statement.ral.scaling.UpdatableScalingRALStatement;
 import org.apache.shardingsphere.infra.distsql.update.RALUpdaterFactory;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
 import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
-import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
 
 /**
  * Updatable migration RAL backend handler factory.
  */
 @RequiredArgsConstructor
 @Setter
+// TODO rename, and also similar ones
 public final class UpdatableMigrationRALBackendHandler implements ProxyBackendHandler {
     
     private final UpdatableScalingRALStatement sqlStatement;
     
+    private final ConnectionSession connectionSession;
+    
     @SuppressWarnings("unchecked")
     @Override
     public ResponseHeader execute() {
-        RALUpdaterFactory.getInstance(sqlStatement.getClass()).executeUpdate(sqlStatement);
+        String databaseName = getDatabaseName(connectionSession);
+        checkDatabaseName(databaseName);
+        RALUpdaterFactory.getInstance(sqlStatement.getClass()).executeUpdate(databaseName, sqlStatement);
         return new UpdateResponseHeader(sqlStatement);
     }
+    
+    private String getDatabaseName(final ConnectionSession connectionSession) {
+        return connectionSession.getDatabaseName();
+    }
+    
+    private void checkDatabaseName(final String databaseName) {
+        if (null == databaseName) {
+            throw new NoDatabaseSelectedException();
+        }
+        if (!ProxyContext.getInstance().databaseExists(databaseName)) {
+            throw new UnknownDatabaseException(databaseName);
+        }
+    }
 }