You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/02/24 11:38:44 UTC

[shardingsphere] branch master updated: remove sql router hook (#9491)

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 25187f0  remove sql router hook (#9491)
25187f0 is described below

commit 25187f0947fa1a272b8559d1cecfa4d2fc6a2a76
Author: xiaoyu <54...@qq.com>
AuthorDate: Wed Feb 24 19:38:04 2021 +0800

    remove sql router hook (#9491)
    
    * remove sql router hook
    
    * remove sql router hook
---
 .../infra/route/engine/SQLRouteEngine.java         | 19 +----
 .../infra/route/hook/RoutingHook.java              | 49 -------------
 .../infra/route/hook/SPIRoutingHook.java           | 57 ---------------
 .../infra/route/engine/SQLRouteEngineTest.java     | 26 +------
 .../infra/route/hook/SPIRoutingHookTest.java       | 83 ----------------------
 .../route/hook/fixture/RoutingHookFixture.java     | 51 -------------
 ...che.shardingsphere.infra.route.hook.RoutingHook | 18 -----
 7 files changed, 4 insertions(+), 299 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java
index 01902b7..fef142f 100644
--- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java
@@ -20,12 +20,11 @@ package org.apache.shardingsphere.infra.route.engine;
 import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.infra.binder.LogicSQL;
 import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.route.context.RouteContext;
 import org.apache.shardingsphere.infra.route.engine.impl.AllSQLRouteExecutor;
 import org.apache.shardingsphere.infra.route.engine.impl.PartialSQLRouteExecutor;
-import org.apache.shardingsphere.infra.route.hook.SPIRoutingHook;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement;
 
@@ -41,8 +40,6 @@ public final class SQLRouteEngine {
     
     private final ConfigurationProperties props;
     
-    private final SPIRoutingHook routingHook = new SPIRoutingHook();
-    
     /**
      * Route SQL.
      *
@@ -51,18 +48,8 @@ public final class SQLRouteEngine {
      * @return route context
      */
     public RouteContext route(final LogicSQL logicSQL, final ShardingSphereMetaData metaData) {
-        routingHook.start(logicSQL.getSql());
-        try {
-            SQLRouteExecutor executor = isNeedAllSchemas(logicSQL.getSqlStatementContext().getSqlStatement()) ? new AllSQLRouteExecutor() : new PartialSQLRouteExecutor(rules, props);
-            RouteContext result = executor.route(logicSQL, metaData);
-            routingHook.finishSuccess(result, metaData.getSchema());
-            return result;
-            // CHECKSTYLE:OFF
-        } catch (final Exception ex) {
-            // CHECKSTYLE:ON
-            routingHook.finishFailure(ex);
-            throw ex;
-        }
+        SQLRouteExecutor executor = isNeedAllSchemas(logicSQL.getSqlStatementContext().getSqlStatement()) ? new AllSQLRouteExecutor() : new PartialSQLRouteExecutor(rules, props);
+        return executor.route(logicSQL, metaData);
     }
     
     // TODO use dynamic config to judge UnconfiguredSchema
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/hook/RoutingHook.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/hook/RoutingHook.java
deleted file mode 100644
index faef8d7..0000000
--- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/hook/RoutingHook.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.infra.route.hook;
-
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
-
-/**
- * Routing hook.
- */
-public interface RoutingHook {
-    
-    /**
-     * Handle when routing started.
-     *
-     * @param sql SQL to be routing
-     */
-    void start(String sql);
-    
-    /**
-     * Handle when routing finished success.
-     *
-     * @param routeContext route context
-     * @param schema ShardingSphere schema
-     */
-    void finishSuccess(RouteContext routeContext, ShardingSphereSchema schema);
-    
-    /**
-     * Handle when routing finished failure.
-     * 
-     * @param cause failure cause
-     */
-    void finishFailure(Exception cause);
-}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/hook/SPIRoutingHook.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/hook/SPIRoutingHook.java
deleted file mode 100644
index d416446..0000000
--- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/hook/SPIRoutingHook.java
+++ /dev/null
@@ -1,57 +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.infra.route.hook;
-
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
-
-import java.util.Collection;
-
-/**
- * Routing hook for SPI.
- */
-public final class SPIRoutingHook implements RoutingHook {
-    
-    private final Collection<RoutingHook> routingHooks = ShardingSphereServiceLoader.newServiceInstances(RoutingHook.class);
-    
-    static {
-        ShardingSphereServiceLoader.register(RoutingHook.class);
-    }
-    
-    @Override
-    public void start(final String sql) {
-        for (RoutingHook each : routingHooks) {
-            each.start(sql);
-        }
-    }
-    
-    @Override
-    public void finishSuccess(final RouteContext routeContext, final ShardingSphereSchema schema) {
-        for (RoutingHook each : routingHooks) {
-            each.finishSuccess(routeContext, schema);
-        }
-    }
-    
-    @Override
-    public void finishFailure(final Exception cause) {
-        for (RoutingHook each : routingHooks) {
-            each.finishFailure(cause);
-        }
-    }
-}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java
index 6a8ee59..80fc0f3 100644
--- a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.infra.route.engine;
 
-import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.binder.LogicSQL;
 import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
 import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
@@ -29,13 +28,11 @@ import org.apache.shardingsphere.infra.route.context.RouteContext;
 import org.apache.shardingsphere.infra.route.context.RouteUnit;
 import org.apache.shardingsphere.infra.route.fixture.rule.RouteFailureRuleFixture;
 import org.apache.shardingsphere.infra.route.fixture.rule.RouteRuleFixture;
-import org.apache.shardingsphere.infra.route.hook.SPIRoutingHook;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
-import java.lang.reflect.Field;
 import java.util.Collections;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -43,7 +40,6 @@ import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
 
 @RunWith(MockitoJUnitRunner.class)
 public final class SQLRouteEngineTest {
@@ -54,24 +50,18 @@ public final class SQLRouteEngineTest {
     @Mock
     private ConfigurationProperties props;
     
-    @Mock
-    private SPIRoutingHook routingHook;
-    
     @Test
     public void assertRouteSuccess() {
         LogicSQL logicSQL = new LogicSQL(mock(SQLStatementContext.class), "SELECT 1", Collections.emptyList());
         ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.emptyList(), Collections.singleton(new RouteRuleFixture()));
         ShardingSphereMetaData metaData = new ShardingSphereMetaData("logic_schema", mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, schema);
         SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(Collections.singleton(new RouteRuleFixture()), props);
-        setSPIRoutingHook(sqlRouteEngine);
         RouteContext actual = sqlRouteEngine.route(logicSQL, metaData);
         assertThat(actual.getRouteUnits().size(), is(1));
         RouteUnit routeUnit = actual.getRouteUnits().iterator().next();
         assertThat(routeUnit.getDataSourceMapper().getLogicName(), is("ds"));
         assertThat(routeUnit.getDataSourceMapper().getActualName(), is("ds_0"));
         assertTrue(routeUnit.getTableMappers().isEmpty());
-        verify(routingHook).start("SELECT 1");
-        verify(routingHook).finishSuccess(actual, schema);
     }
     
     @Test(expected = UnsupportedOperationException.class)
@@ -80,20 +70,6 @@ public final class SQLRouteEngineTest {
         ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(Collections.emptyList(), Collections.singleton(new RouteRuleFixture()));
         ShardingSphereMetaData metaData = new ShardingSphereMetaData("logic_schema", mock(ShardingSphereResource.class, RETURNS_DEEP_STUBS), ruleMetaData, schema);
         SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(Collections.singleton(new RouteFailureRuleFixture()), props);
-        setSPIRoutingHook(sqlRouteEngine);
-        try {
-            sqlRouteEngine.route(logicSQL, metaData);
-        } catch (final UnsupportedOperationException ex) {
-            verify(routingHook).start("SELECT 1");
-            verify(routingHook).finishFailure(ex);
-            throw ex;
-        }
-    }
-    
-    @SneakyThrows(ReflectiveOperationException.class)
-    private void setSPIRoutingHook(final SQLRouteEngine sqlRouteEngine) {
-        Field field = SQLRouteEngine.class.getDeclaredField("routingHook");
-        field.setAccessible(true);
-        field.set(sqlRouteEngine, routingHook);
+        sqlRouteEngine.route(logicSQL, metaData);
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/hook/SPIRoutingHookTest.java b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/hook/SPIRoutingHookTest.java
deleted file mode 100644
index d09f16d..0000000
--- a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/hook/SPIRoutingHookTest.java
+++ /dev/null
@@ -1,83 +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.infra.route.hook;
-
-import lombok.SneakyThrows;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.hook.fixture.RoutingHookFixture;
-import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import java.lang.reflect.Field;
-import java.util.Collection;
-
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-@RunWith(MockitoJUnitRunner.class)
-public final class SPIRoutingHookTest {
-    
-    private final SPIRoutingHook spiRoutingHook = new SPIRoutingHook();
-    
-    @Mock
-    private RouteContext routeContext;
-    
-    @Mock
-    private ShardingSphereSchema schema;
-    
-    @Mock
-    private Exception exception;
-    
-    @Test
-    public void assertStart() {
-        String sql = "SELECT * FROM table";
-        spiRoutingHook.start(sql);
-        RoutingHook routingHook = getFixtureHook();
-        assertThat(routingHook, instanceOf(RoutingHookFixture.class));
-        assertThat(((RoutingHookFixture) routingHook).getSql(), is(sql));
-    }
-    
-    @Test
-    public void assertFinishSuccess() {
-        spiRoutingHook.finishSuccess(routeContext, schema);
-        RoutingHook routingHook = getFixtureHook();
-        assertThat(routingHook, instanceOf(RoutingHookFixture.class));
-        assertThat(((RoutingHookFixture) routingHook).getRouteContext(), is(routeContext));
-        assertThat(((RoutingHookFixture) routingHook).getSchema(), is(schema));
-    }
-    
-    @Test
-    public void assertFinishFailure() {
-        spiRoutingHook.finishFailure(exception);
-        RoutingHook routingHook = getFixtureHook();
-        assertThat(routingHook, instanceOf(RoutingHookFixture.class));
-        assertThat(((RoutingHookFixture) routingHook).getCause(), is(exception));
-    }
-    
-    @SuppressWarnings("unchecked")
-    @SneakyThrows(ReflectiveOperationException.class)
-    private RoutingHook getFixtureHook() {
-        Field routingHooksField = SPIRoutingHook.class.getDeclaredField("routingHooks");
-        routingHooksField.setAccessible(true);
-        return ((Collection<RoutingHook>) routingHooksField.get(spiRoutingHook)).iterator().next();
-    }
-}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/hook/fixture/RoutingHookFixture.java b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/hook/fixture/RoutingHookFixture.java
deleted file mode 100644
index d30c19a..0000000
--- a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/hook/fixture/RoutingHookFixture.java
+++ /dev/null
@@ -1,51 +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.infra.route.hook.fixture;
-
-import lombok.Getter;
-import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.hook.RoutingHook;
-
-@Getter
-public final class RoutingHookFixture implements RoutingHook {
-    
-    private String sql;
-    
-    private RouteContext routeContext;
-    
-    private ShardingSphereSchema schema;
-    
-    private Exception cause;
-    
-    @Override
-    public void start(final String sql) {
-        this.sql = sql;
-    }
-    
-    @Override
-    public void finishSuccess(final RouteContext routeContext, final ShardingSphereSchema schema) {
-        this.routeContext = routeContext;
-        this.schema = schema;
-    }
-    
-    @Override
-    public void finishFailure(final Exception cause) {
-        this.cause = cause;
-    }
-}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.route.hook.RoutingHook b/shardingsphere-infra/shardingsphere-infra-route/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.route.hook.RoutingHook
deleted file mode 100644
index 3519a11..0000000
--- a/shardingsphere-infra/shardingsphere-infra-route/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.route.hook.RoutingHook
+++ /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.infra.route.hook.fixture.RoutingHookFixture