未验证 提交 622a035e 编写于 作者: L Liang Zhang 提交者: GitHub

Move SQLLogger and RouteHook to underlying module (#4971)

* move SQLLogger to shardingsphere-executor module

* move RoutingHook to shardingsphere-route module
上级 1d863090
......@@ -20,13 +20,11 @@ package org.apache.shardingsphere.core.shard;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.rule.MasterSlaveRule;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.core.shard.log.ShardingSQLLogger;
import org.apache.shardingsphere.encrypt.rewrite.context.EncryptSQLRewriteContextDecorator;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.masterslave.route.engine.MasterSlaveRouteDecorator;
import org.apache.shardingsphere.sharding.rewrite.context.ShardingSQLRewriteContextDecorator;
import org.apache.shardingsphere.sharding.route.engine.ShardingRouteDecorator;
import org.apache.shardingsphere.sharding.route.hook.SPIRoutingHook;
import org.apache.shardingsphere.sql.parser.SQLParserEngine;
import org.apache.shardingsphere.underlying.common.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.underlying.common.config.properties.ConfigurationPropertyKey;
......@@ -35,6 +33,7 @@ import org.apache.shardingsphere.underlying.common.rule.BaseRule;
import org.apache.shardingsphere.underlying.executor.context.ExecutionContext;
import org.apache.shardingsphere.underlying.executor.context.ExecutionUnit;
import org.apache.shardingsphere.underlying.executor.context.SQLUnit;
import org.apache.shardingsphere.underlying.executor.log.SQLLogger;
import org.apache.shardingsphere.underlying.rewrite.SQLRewriteEntry;
import org.apache.shardingsphere.underlying.rewrite.context.SQLRewriteContext;
import org.apache.shardingsphere.underlying.rewrite.engine.SQLRewriteEngine;
......@@ -43,6 +42,7 @@ import org.apache.shardingsphere.underlying.rewrite.engine.SQLRouteRewriteEngine
import org.apache.shardingsphere.underlying.route.DataNodeRouter;
import org.apache.shardingsphere.underlying.route.context.RouteContext;
import org.apache.shardingsphere.underlying.route.context.RouteUnit;
import org.apache.shardingsphere.underlying.route.hook.SPIRoutingHook;
import java.util.Collection;
import java.util.Collections;
......@@ -90,7 +90,7 @@ public abstract class BaseShardingEngine {
ExecutionContext result = new ExecutionContext(routeContext.getSqlStatementContext());
result.getExecutionUnits().addAll(executeRewrite(sql, clonedParameters, routeContext));
if (properties.<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW)) {
ShardingSQLLogger.logSQL(sql, properties.<Boolean>getValue(ConfigurationPropertyKey.SQL_SIMPLE), result.getSqlStatementContext(), result.getExecutionUnits());
SQLLogger.logSQL(sql, properties.<Boolean>getValue(ConfigurationPropertyKey.SQL_SIMPLE), result.getSqlStatementContext(), result.getExecutionUnits());
}
return result;
}
......
......@@ -17,7 +17,7 @@
package org.apache.shardingsphere.core.shard.fixture;
import org.apache.shardingsphere.sharding.route.hook.RoutingHook;
import org.apache.shardingsphere.underlying.route.hook.RoutingHook;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import org.apache.shardingsphere.underlying.route.context.RouteContext;
......
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.shard.log;
package org.apache.shardingsphere.underlying.executor.log;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
......@@ -28,14 +28,14 @@ import java.util.HashSet;
import java.util.Set;
/**
* SQL logger for sharding.
* SQL logger.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Slf4j(topic = "ShardingSphere-SQL")
public final class ShardingSQLLogger {
public final class SQLLogger {
/**
* Print SQL log for sharding rule.
* Print SQL log.
*
* @param logicSQL logic SQL
* @param showSimple whether show SQL in simple style
......@@ -43,7 +43,6 @@ public final class ShardingSQLLogger {
* @param executionUnits execution units
*/
public static void logSQL(final String logicSQL, final boolean showSimple, final SQLStatementContext sqlStatementContext, final Collection<ExecutionUnit> executionUnits) {
log("Rule Type: sharding");
log("Logic SQL: {}", logicSQL);
log("SQLStatement: {}", sqlStatementContext);
if (showSimple) {
......
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.shard.log;
package org.apache.shardingsphere.underlying.executor.log;
import org.apache.shardingsphere.underlying.executor.context.ExecutionUnit;
import org.apache.shardingsphere.underlying.executor.context.SQLUnit;
......@@ -33,19 +33,16 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static org.mockito.Mockito.inOrder;
@RunWith(MockitoJUnitRunner.class)
public final class ShardingSQLLoggerTest {
public final class SQLLoggerTest {
private static final String SQL = "SELECT * FROM t_user";
private Collection<String> dataSourceNames;
private Collection<ExecutionUnit> executionUnits;
@Mock
......@@ -53,17 +50,18 @@ public final class ShardingSQLLoggerTest {
@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException {
this.dataSourceNames = Arrays.asList("db1", "db2", "db3");
this.executionUnits = mockExecutionUnits(dataSourceNames, SQL);
Field field = ShardingSQLLogger.class.getDeclaredField("log");
setFinalStatic(field, logger);
executionUnits = mockExecutionUnits(Arrays.asList("db1", "db2", "db3"), SQL);
setFinalStatic(SQLLogger.class.getDeclaredField("log"), logger);
}
private Collection<ExecutionUnit> mockExecutionUnits(final Collection<String> dataSourceNames, final String sql) {
return dataSourceNames.stream().map(each -> new ExecutionUnit(each, new SQLUnit(sql, new ArrayList<>()))).collect(Collectors.toList());
}
@Test
public void assertLogSQLShard() {
ShardingSQLLogger.logSQL(SQL, false, null, executionUnits);
public void assertLogNormalSQLWithoutParameter() {
SQLLogger.logSQL(SQL, false, null, executionUnits);
InOrder inOrder = inOrder(logger);
inOrder.verify(logger).info("Rule Type: sharding", new Object[]{});
inOrder.verify(logger).info("Logic SQL: {}", new Object[]{SQL});
inOrder.verify(logger).info("SQLStatement: {}", new Object[]{null});
inOrder.verify(logger).info("Actual SQL: {} ::: {}", new Object[]{"db1", SQL});
......@@ -72,12 +70,11 @@ public final class ShardingSQLLoggerTest {
}
@Test
public void assertLogSQLShardWithParameters() {
public void assertLogNormalSQLWithParameters() {
List<Object> parameters = executionUnits.iterator().next().getSqlUnit().getParameters();
parameters.add("parameter");
ShardingSQLLogger.logSQL(SQL, false, null, executionUnits);
SQLLogger.logSQL(SQL, false, null, executionUnits);
InOrder inOrder = inOrder(logger);
inOrder.verify(logger).info("Rule Type: sharding", new Object[]{});
inOrder.verify(logger).info("Logic SQL: {}", new Object[]{SQL});
inOrder.verify(logger).info("SQLStatement: {}", new Object[]{null});
inOrder.verify(logger).info("Actual SQL: {} ::: {} ::: {}", "db1", SQL, parameters);
......@@ -86,37 +83,16 @@ public final class ShardingSQLLoggerTest {
}
@Test
public void assertLogSQLShardSimple() {
ShardingSQLLogger.logSQL(SQL, true, null, executionUnits);
public void assertLogSimpleSQL() {
SQLLogger.logSQL(SQL, true, null, executionUnits);
InOrder inOrder = inOrder(logger);
inOrder.verify(logger).info("Rule Type: sharding", new Object[]{});
inOrder.verify(logger).info("Logic SQL: {}", new Object[]{SQL});
inOrder.verify(logger).info("SQLStatement: {}", new Object[]{null});
inOrder.verify(logger).info("Actual SQL(simple): {} ::: {}", new Object[]{buildDataSourceNamesSet(), executionUnits.size()});
}
private Set<String> buildDataSourceNamesSet() {
Set<String> dataSourceNamesSet = new HashSet<>(executionUnits.size());
for (ExecutionUnit each : executionUnits) {
dataSourceNamesSet.add(each.getDataSourceName());
}
return dataSourceNamesSet;
}
private Collection<ExecutionUnit> mockExecutionUnits(final Collection<String> dataSourceNames, final String sql) {
List<ExecutionUnit> result = new LinkedList<>();
for (String dsName : dataSourceNames) {
result.addAll(mockOneShard(dsName, 1, sql));
}
return result;
inOrder.verify(logger).info("Actual SQL(simple): {} ::: {}", new Object[]{buildDataSourceNames(), executionUnits.size()});
}
private Collection<ExecutionUnit> mockOneShard(final String dataSourceName, final int size, final String sql) {
Collection<ExecutionUnit> result = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
result.add(new ExecutionUnit(dataSourceName, new SQLUnit(sql, new ArrayList<>())));
}
return result;
private Collection<String> buildDataSourceNames() {
return executionUnits.stream().map(ExecutionUnit::getDataSourceName).collect(Collectors.toCollection(() -> new HashSet<>(executionUnits.size())));
}
private static void setFinalStatic(final Field field, final Object newValue) throws NoSuchFieldException, IllegalAccessException {
......
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sharding.route.hook;
package org.apache.shardingsphere.underlying.route.hook;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import org.apache.shardingsphere.underlying.route.context.RouteContext;
......
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sharding.route.hook;
package org.apache.shardingsphere.underlying.route.hook;
import org.apache.shardingsphere.spi.NewInstanceServiceLoader;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
......
......@@ -15,12 +15,12 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sharding.route.hook;
package org.apache.shardingsphere.underlying.route.hook;
import lombok.SneakyThrows;
import org.apache.shardingsphere.sharding.route.fixture.RoutingHookFixture;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import org.apache.shardingsphere.underlying.route.context.RouteContext;
import org.apache.shardingsphere.underlying.route.hook.fixture.RoutingHookFixture;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
......
......@@ -15,10 +15,10 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sharding.route.fixture;
package org.apache.shardingsphere.underlying.route.hook.fixture;
import lombok.Getter;
import org.apache.shardingsphere.sharding.route.hook.RoutingHook;
import org.apache.shardingsphere.underlying.route.hook.RoutingHook;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import org.apache.shardingsphere.underlying.route.context.RouteContext;
......@@ -48,5 +48,4 @@ public final class RoutingHookFixture implements RoutingHook {
public void finishFailure(final Exception cause) {
this.cause = cause;
}
}
......@@ -15,4 +15,4 @@
# limitations under the License.
#
org.apache.shardingsphere.sharding.route.fixture.RoutingHookFixture
org.apache.shardingsphere.underlying.route.hook.fixture.RoutingHookFixture
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册