From 5c6cb1905f54c2a93ea6b7d1bf5b9b4f5061edc4 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 25 Sep 2018 16:47:06 +0800 Subject: [PATCH] for #1172, refactor spi for ot --- .../core/bootstrap/ShardingBootstrap.java | 2 - .../sql/execute/SQLExecuteCallback.java | 17 ++-- .../prepare/SQLExecutePrepareTemplate.java | 20 ++++- .../router/sharding/ParsingSQLRouter.java | 18 ++--- .../core/spi/ShardingSPILoader.java | 46 ----------- .../close/CloseConnectionHook.java} | 33 +++++--- .../close/SPICloseConnectionHook.java | 57 +++++++++++++ .../get/GetConnectionHook.java} | 35 +++++--- .../connection/get/SPIGetConnectionHook.java | 57 +++++++++++++ .../core/spi/event/ShardingEventHandler.java | 6 +- .../spi/event/ShardingEventHandlerLoader.java | 6 +- .../core/spi/event/ShardingFinishEvent.java | 2 +- .../CloseConnectionEventHandlerLoader.java | 61 -------------- .../close/CloseConnectionFinishEvent.java | 28 ------- .../get/GetConnectionEventHandler.java | 28 ------- .../get/GetConnectionEventHandlerLoader.java | 61 -------------- .../get/GetConnectionStartEvent.java | 34 -------- .../SQLExecutionEventHandlerLoader.java | 61 -------------- .../event/parsing/ParsingEventHandler.java | 28 ------- .../parsing/ParsingEventHandlerLoader.java | 61 -------------- .../spi/event/parsing/ParsingFinishEvent.java | 28 ------- .../spi/event/parsing/ParsingStartEvent.java | 34 -------- .../spi/executor/SPISQLExecutionHook.java | 59 ++++++++++++++ .../SQLExecutionHook.java} | 32 +++++--- .../core/spi/parsing/ParsingHook.java | 45 +++++++++++ .../core/spi/parsing/SPIParsingHook.java | 55 +++++++++++++ ...InvokeHandler.java => RootInvokeHook.java} | 10 +-- ...dlerLoader.java => SPIRootInvokeHook.java} | 37 +++------ .../executor/AbstractStatementExecutor.java | 9 ++- .../adapter/AbstractConnectionAdapter.java | 31 +++----- .../core/connection/ShardingConnection.java | 7 +- .../assertion/IntegrateTestCasesLoader.java | 8 +- ...penTracingCloseConnectionEventHandler.java | 48 ----------- .../OpenTracingGetConnectionEventHandler.java | 72 ----------------- .../OpenTracingSQLExecutionEventHandler.java | 69 ---------------- .../OpenTracingParsingEventHandler.java | 46 ----------- .../root/OpenTracingRootInvokeHandler.java | 6 +- .../OpenTracingHandlerTemplate.java} | 64 +++++++-------- .../OpenTracingSpanFinishCallback.java | 20 ++++- .../OpenTracingSpanFinishCallbackAdapter.java | 16 +++- ...ingSpanFinishRootCleanCallbackAdapter.java | 45 +++++++++++ .../tracing/OpenTracingSpanStartCallback.java | 17 +++- .../OpenTracingCloseConnectionHook.java | 63 +++++++++++++++ .../OpenTracingGetConnectionHook.java | 78 ++++++++++++++++++ .../executor/OpenTracingSQLExecutionHook.java | 79 +++++++++++++++++++ .../parsing/OpenTracingParsingHook.java | 60 ++++++++++++++ ...e.spi.connection.close.CloseConnectionHook | 1 + ....core.spi.connection.get.GetConnectionHook | 1 + ...nnection.close.CloseConnectionEventHandler | 1 - ...t.connection.get.GetConnectionEventHandler | 1 - ...pi.event.executor.SQLExecutionEventHandler | 1 - ...core.spi.event.parsing.ParsingEventHandler | 1 - ...gsphere.core.spi.executor.SQLExecutionHook | 1 + ...hardingsphere.core.spi.parsing.ParsingHook | 1 + ...ardingsphere.core.spi.root.RootInvokeHook} | 0 .../opentracing/handler/AllHandlerTests.java | 8 +- .../OpenTracingRootInvokeHandlerTest.java | 9 ++- ...racingCloseConnectionEventHandlerTest.java | 19 ++--- ...nTracingGetConnectionEventHandlerTest.java | 27 +++---- ...enTracingSQLExecutionEventHandlerTest.java | 27 +++---- .../OpenTracingParsingEventHandlerTest.java | 19 ++--- .../jdbc/connection/BackendConnection.java | 37 +++------ .../jdbc/execute/JDBCExecuteEngine.java | 10 ++- .../frontend/mysql/MySQLFrontendHandler.java | 9 ++- 64 files changed, 905 insertions(+), 967 deletions(-) delete mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/ShardingSPILoader.java rename sharding-core/src/main/java/io/shardingsphere/core/spi/{event/connection/close/CloseConnectionStartEvent.java => connection/close/CloseConnectionHook.java} (52%) create mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/connection/close/SPICloseConnectionHook.java rename sharding-core/src/main/java/io/shardingsphere/core/spi/{event/connection/get/GetConnectionFinishEvent.java => connection/get/GetConnectionHook.java} (50%) create mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/connection/get/SPIGetConnectionHook.java delete mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/close/CloseConnectionEventHandlerLoader.java delete mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/close/CloseConnectionFinishEvent.java delete mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionEventHandler.java delete mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionEventHandlerLoader.java delete mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionStartEvent.java delete mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionEventHandlerLoader.java delete mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingEventHandler.java delete mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingEventHandlerLoader.java delete mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingFinishEvent.java delete mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingStartEvent.java create mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/executor/SPISQLExecutionHook.java rename sharding-core/src/main/java/io/shardingsphere/core/spi/{event/executor/SQLExecutionStartEvent.java => executor/SQLExecutionHook.java} (56%) create mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/parsing/ParsingHook.java create mode 100644 sharding-core/src/main/java/io/shardingsphere/core/spi/parsing/SPIParsingHook.java rename sharding-core/src/main/java/io/shardingsphere/core/spi/root/{RootInvokeHandler.java => RootInvokeHook.java} (81%) rename sharding-core/src/main/java/io/shardingsphere/core/spi/root/{RootInvokeHandlerLoader.java => SPIRootInvokeHook.java} (52%) delete mode 100644 sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/connection/OpenTracingCloseConnectionEventHandler.java delete mode 100644 sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/connection/OpenTracingGetConnectionEventHandler.java delete mode 100644 sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/executor/OpenTracingSQLExecutionEventHandler.java delete mode 100644 sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/parsing/OpenTracingParsingEventHandler.java rename sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/{OpenTracingHandler.java => tracing/OpenTracingHandlerTemplate.java} (61%) rename sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionFinishEvent.java => sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanFinishCallback.java (63%) rename sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/close/CloseConnectionEventHandler.java => sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanFinishCallbackAdapter.java (64%) create mode 100644 sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanFinishRootCleanCallbackAdapter.java rename sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionEventHandler.java => sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanStartCallback.java (63%) create mode 100644 sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingCloseConnectionHook.java create mode 100644 sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingGetConnectionHook.java create mode 100644 sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/executor/OpenTracingSQLExecutionHook.java create mode 100644 sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/parsing/OpenTracingParsingHook.java create mode 100644 sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.connection.close.CloseConnectionHook create mode 100644 sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.connection.get.GetConnectionHook delete mode 100644 sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.connection.close.CloseConnectionEventHandler delete mode 100644 sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.connection.get.GetConnectionEventHandler delete mode 100644 sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.executor.SQLExecutionEventHandler delete mode 100644 sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.parsing.ParsingEventHandler create mode 100644 sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.executor.SQLExecutionHook create mode 100644 sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.parsing.ParsingHook rename sharding-opentracing/src/main/resources/META-INF/services/{io.shardingsphere.core.spi.root.RootInvokeHandler => io.shardingsphere.core.spi.root.RootInvokeHook} (100%) rename sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/{ => tracing}/connection/OpenTracingCloseConnectionEventHandlerTest.java (79%) rename sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/{ => tracing}/connection/OpenTracingGetConnectionEventHandlerTest.java (82%) rename sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/{ => tracing}/executor/OpenTracingSQLExecutionEventHandlerTest.java (85%) rename sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/{ => tracing}/parsing/OpenTracingParsingEventHandlerTest.java (77%) diff --git a/sharding-core/src/main/java/io/shardingsphere/core/bootstrap/ShardingBootstrap.java b/sharding-core/src/main/java/io/shardingsphere/core/bootstrap/ShardingBootstrap.java index 0fd2d00905c21..3c8acaa6ceccd 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/bootstrap/ShardingBootstrap.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/bootstrap/ShardingBootstrap.java @@ -18,7 +18,6 @@ package io.shardingsphere.core.bootstrap; import io.shardingsphere.core.event.ShardingEventListenerRegistrySPILoader; -import io.shardingsphere.core.spi.ShardingSPILoader; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -34,7 +33,6 @@ public final class ShardingBootstrap { * Initialize sharding bootstrap. */ public static void init() { - ShardingSPILoader.loadAllShardingSPI(); ShardingEventListenerRegistrySPILoader.registerListeners(); } } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/execute/SQLExecuteCallback.java b/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/execute/SQLExecuteCallback.java index ff7b31b98834d..6b72073f6566e 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/execute/SQLExecuteCallback.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/execute/SQLExecuteCallback.java @@ -23,9 +23,6 @@ import io.shardingsphere.core.event.ShardingEventBusInstance; import io.shardingsphere.core.event.executor.SQLExecutionEvent; import io.shardingsphere.core.event.executor.SQLExecutionEventFactory; -import io.shardingsphere.core.spi.event.executor.SQLExecutionEventHandlerLoader; -import io.shardingsphere.core.spi.event.executor.SQLExecutionFinishEvent; -import io.shardingsphere.core.spi.event.executor.SQLExecutionStartEvent; import io.shardingsphere.core.executor.ShardingExecuteCallback; import io.shardingsphere.core.executor.ShardingGroupExecuteCallback; import io.shardingsphere.core.executor.StatementExecuteUnit; @@ -33,6 +30,8 @@ import io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorExceptionHandler; import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; import io.shardingsphere.core.metadata.datasource.DataSourceMetaDataFactory; +import io.shardingsphere.core.spi.executor.SPISQLExecutionHook; +import io.shardingsphere.core.spi.executor.SQLExecutionHook; import lombok.RequiredArgsConstructor; import java.sql.SQLException; @@ -62,6 +61,8 @@ public abstract class SQLExecuteCallback implements ShardingExecuteCallback> parameterSets = statementExecuteUnit.getRouteUnit().getSqlUnit().getParameterSets(); DataSourceMetaData dataSourceMetaData = DataSourceMetaDataFactory.newInstance(databaseType, statementExecuteUnit.getStatement().getConnection().getMetaData().getURL()); for (List each : parameterSets) { - SQLExecutionEventHandlerLoader.getInstance().start(new SQLExecutionStartEvent(statementExecuteUnit.getRouteUnit(), each, dataSourceMetaData)); + sqlExecutionHook.start(statementExecuteUnit.getRouteUnit(), each, dataSourceMetaData); // TODO remove after BED removed shardingEventBus.post(SQLExecutionEventFactory.createEvent(sqlType, statementExecuteUnit, each, dataSourceMetaData)); } try { T result = executeSQL(statementExecuteUnit); for (List each : parameterSets) { + sqlExecutionHook.finishSuccess(); + // TODO remove after BED removed SQLExecutionEvent finishEvent = SQLExecutionEventFactory.createEvent(sqlType, statementExecuteUnit, each, dataSourceMetaData); finishEvent.setExecuteSuccess(); - SQLExecutionEventHandlerLoader.getInstance().finish(new SQLExecutionFinishEvent()); - // TODO remove after BED removed shardingEventBus.post(finishEvent); } return result; } catch (final SQLException ex) { for (List each : parameterSets) { + sqlExecutionHook.finishFailure(ex); + // TODO remove after BED removed SQLExecutionEvent finishEvent = SQLExecutionEventFactory.createEvent(sqlType, statementExecuteUnit, each, dataSourceMetaData); finishEvent.setExecuteFailure(ex); - SQLExecutionEventHandlerLoader.getInstance().finish(new SQLExecutionFinishEvent()); - // TODO remove after BED removed shardingEventBus.post(finishEvent); } ExecutorExceptionHandler.handleException(ex); diff --git a/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.java b/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.java index 1d909faea92b9..6c716d03802dc 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/executor/sql/prepare/SQLExecutePrepareTemplate.java @@ -19,13 +19,17 @@ import com.google.common.collect.Lists; import io.shardingsphere.core.constant.ConnectionMode; +import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.executor.ShardingExecuteCallback; import io.shardingsphere.core.executor.ShardingExecuteEngine; import io.shardingsphere.core.executor.ShardingExecuteGroup; import io.shardingsphere.core.executor.StatementExecuteUnit; import io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorDataMap; +import io.shardingsphere.core.metadata.datasource.DataSourceMetaDataFactory; import io.shardingsphere.core.routing.RouteUnit; import io.shardingsphere.core.routing.SQLUnit; +import io.shardingsphere.core.spi.connection.get.GetConnectionHook; +import io.shardingsphere.core.spi.connection.get.SPIGetConnectionHook; import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; @@ -49,10 +53,14 @@ @AllArgsConstructor public final class SQLExecutePrepareTemplate { + private final DatabaseType databaseType; + private final int maxConnectionsSizePerQuery; private ShardingExecuteEngine shardingExecuteEngine; + private final GetConnectionHook getConnectionHook = new SPIGetConnectionHook(); + /** * Get execute unit groups. * @@ -81,7 +89,7 @@ private Collection> getAsynchronizedE final Map dataMap = ExecutorDataMap.getDataMap(); List>> results = shardingExecuteEngine.execute(sqlUnitGroups.entrySet(), new ShardingExecuteCallback>, Collection>>() { - + @Override public Collection> execute(final Entry> input) throws SQLException { ExecutorDataMap.setDataMap(dataMap); @@ -112,7 +120,15 @@ private List> getSQLExecuteGroups( int desiredPartitionSize = Math.max(sqlUnits.size() / maxConnectionsSizePerQuery, 1); List> sqlUnitGroups = Lists.partition(sqlUnits, desiredPartitionSize); ConnectionMode connectionMode = maxConnectionsSizePerQuery < sqlUnits.size() ? ConnectionMode.CONNECTION_STRICTLY : ConnectionMode.MEMORY_STRICTLY; - List connections = callback.getConnections(connectionMode, dataSourceName, sqlUnitGroups.size()); + getConnectionHook.start(dataSourceName); + List connections; + try { + connections = callback.getConnections(connectionMode, dataSourceName, sqlUnitGroups.size()); + } catch (final SQLException ex) { + getConnectionHook.finishFailure(ex); + throw ex; + } + getConnectionHook.finishSuccess(DataSourceMetaDataFactory.newInstance(databaseType, connections.get(0).getMetaData().getURL()), connections.size()); int count = 0; for (List each : sqlUnitGroups) { result.add(getSQLExecuteGroup(connectionMode, connections.get(count++), dataSourceName, each, callback)); diff --git a/sharding-core/src/main/java/io/shardingsphere/core/routing/router/sharding/ParsingSQLRouter.java b/sharding-core/src/main/java/io/shardingsphere/core/routing/router/sharding/ParsingSQLRouter.java index 6cfe6ff113c89..0d56389b0eb62 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/routing/router/sharding/ParsingSQLRouter.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/routing/router/sharding/ParsingSQLRouter.java @@ -19,9 +19,6 @@ import com.google.common.base.Optional; import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.spi.event.parsing.ParsingEventHandlerLoader; -import io.shardingsphere.core.spi.event.parsing.ParsingFinishEvent; -import io.shardingsphere.core.spi.event.parsing.ParsingStartEvent; import io.shardingsphere.core.metadata.datasource.ShardingDataSourceMetaData; import io.shardingsphere.core.metadata.table.ShardingTableMetaData; import io.shardingsphere.core.optimizer.OptimizeEngineFactory; @@ -54,6 +51,8 @@ import io.shardingsphere.core.routing.type.unicast.UnicastRoutingEngine; import io.shardingsphere.core.rule.ShardingRule; import io.shardingsphere.core.rule.TableRule; +import io.shardingsphere.core.spi.parsing.ParsingHook; +import io.shardingsphere.core.spi.parsing.SPIParsingHook; import io.shardingsphere.core.util.SQLLogger; import lombok.RequiredArgsConstructor; @@ -83,19 +82,20 @@ public final class ParsingSQLRouter implements ShardingRouter { private final ShardingDataSourceMetaData shardingDataSourceMetaData; + private final ParsingHook parsingHook = new SPIParsingHook(); + @Override public SQLStatement parse(final String logicSQL, final boolean useCache) { - ParsingEventHandlerLoader.getInstance().start(new ParsingStartEvent(logicSQL)); - ParsingFinishEvent finishEvent = new ParsingFinishEvent(); + parsingHook.start(logicSQL); try { - return new SQLParsingEngine(databaseType, logicSQL, shardingRule, shardingTableMetaData).parse(useCache); + SQLStatement result = new SQLParsingEngine(databaseType, logicSQL, shardingRule, shardingTableMetaData).parse(useCache); + parsingHook.finishSuccess(); + return result; // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON - finishEvent.setException(ex); + parsingHook.finishFailure(ex); throw ex; - } finally { - ParsingEventHandlerLoader.getInstance().finish(finishEvent); } } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/ShardingSPILoader.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/ShardingSPILoader.java deleted file mode 100644 index ea14ed15a976b..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/ShardingSPILoader.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.core.spi; - -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionEventHandlerLoader; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionEventHandlerLoader; -import io.shardingsphere.core.spi.event.executor.SQLExecutionEventHandlerLoader; -import io.shardingsphere.core.spi.event.parsing.ParsingEventHandlerLoader; -import io.shardingsphere.core.spi.root.RootInvokeHandlerLoader; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -/** - * Sharding SPI loader. - * - * @author zhangliang - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class ShardingSPILoader { - - /** - * Load all sharding SPI. - */ - public static void loadAllShardingSPI() { - RootInvokeHandlerLoader.getInstance(); - ParsingEventHandlerLoader.getInstance(); - GetConnectionEventHandlerLoader.getInstance(); - SQLExecutionEventHandlerLoader.getInstance(); - CloseConnectionEventHandlerLoader.getInstance(); - } -} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/close/CloseConnectionStartEvent.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/close/CloseConnectionHook.java similarity index 52% rename from sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/close/CloseConnectionStartEvent.java rename to sharding-core/src/main/java/io/shardingsphere/core/spi/connection/close/CloseConnectionHook.java index 4a75f08fe1047..49dbfd26fad81 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/close/CloseConnectionStartEvent.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/close/CloseConnectionHook.java @@ -15,23 +15,34 @@ *

*/ -package io.shardingsphere.core.spi.event.connection.close; +package io.shardingsphere.core.spi.connection.close; -import io.shardingsphere.core.spi.event.ShardingStartEvent; import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; -import lombok.Getter; -import lombok.RequiredArgsConstructor; /** - * Close connection start event. + * Connection hook. * - * @author zhangyonglun + * @author zhangliang */ -@RequiredArgsConstructor -@Getter -public final class CloseConnectionStartEvent extends ShardingStartEvent { +public interface CloseConnectionHook { - private final String dataSource; + /** + * Handle when close connection started. + * + * @param dataSourceName data source name + * @param dataSourceMetaData data source meta data + */ + void start(String dataSourceName, DataSourceMetaData dataSourceMetaData); - private final DataSourceMetaData dataSourceMetaData; + /** + * Handle when close connection finished success. + */ + void finishSuccess(); + + /** + * Handle when close connection finished failure. + * + * @param cause failure cause + */ + void finishFailure(Exception cause); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/close/SPICloseConnectionHook.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/close/SPICloseConnectionHook.java new file mode 100644 index 0000000000000..d79b8a803f12d --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/close/SPICloseConnectionHook.java @@ -0,0 +1,57 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.core.spi.connection.close; + +import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; + +import java.util.ServiceLoader; + +/** + * Connection hook for SPI. + * + * @author zhangliang + */ +public final class SPICloseConnectionHook implements CloseConnectionHook { + + private static final ServiceLoader SERVICE_LOADER; + + static { + SERVICE_LOADER = ServiceLoader.load(CloseConnectionHook.class); + } + + @Override + public void start(final String dataSourceName, final DataSourceMetaData dataSourceMetaData) { + for (CloseConnectionHook each : SERVICE_LOADER) { + each.start(dataSourceName, dataSourceMetaData); + } + } + + @Override + public void finishSuccess() { + for (CloseConnectionHook each : SERVICE_LOADER) { + each.finishSuccess(); + } + } + + @Override + public void finishFailure(final Exception cause) { + for (CloseConnectionHook each : SERVICE_LOADER) { + each.finishFailure(cause); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionFinishEvent.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/get/GetConnectionHook.java similarity index 50% rename from sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionFinishEvent.java rename to sharding-core/src/main/java/io/shardingsphere/core/spi/connection/get/GetConnectionHook.java index 1d51d2d884cb5..93e881d4f0dc0 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionFinishEvent.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/get/GetConnectionHook.java @@ -15,23 +15,36 @@ *

*/ -package io.shardingsphere.core.spi.event.connection.get; +package io.shardingsphere.core.spi.connection.get; -import io.shardingsphere.core.spi.event.ShardingFinishEvent; import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; -import lombok.Getter; -import lombok.RequiredArgsConstructor; /** - * Get connection finish event. + * Connection hook. * - * @author zhangyonglun + * @author zhangliang */ -@RequiredArgsConstructor -@Getter -public final class GetConnectionFinishEvent extends ShardingFinishEvent { +public interface GetConnectionHook { - private final int connectionCount; + /** + * Handle when get connection started. + * + * @param dataSourceName data source name + */ + void start(String dataSourceName); - private final DataSourceMetaData dataSourceMetaData; + /** + * Handle when get connection finished success. + * + * @param dataSourceMetaData data source meta data + * @param connectionCount connection count + */ + void finishSuccess(DataSourceMetaData dataSourceMetaData, int connectionCount); + + /** + * Handle when get connection finished failure. + * + * @param cause failure cause + */ + void finishFailure(Exception cause); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/get/SPIGetConnectionHook.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/get/SPIGetConnectionHook.java new file mode 100644 index 0000000000000..ec691dd38288d --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/get/SPIGetConnectionHook.java @@ -0,0 +1,57 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.core.spi.connection.get; + +import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; + +import java.util.ServiceLoader; + +/** + * Connection hook loader. + * + * @author zhangliang + */ +public final class SPIGetConnectionHook implements GetConnectionHook { + + private static final ServiceLoader SERVICE_LOADER; + + static { + SERVICE_LOADER = ServiceLoader.load(GetConnectionHook.class); + } + + @Override + public void start(final String dataSourceName) { + for (GetConnectionHook each : SERVICE_LOADER) { + each.start(dataSourceName); + } + } + + @Override + public void finishSuccess(final DataSourceMetaData dataSourceMetaData, final int connectionCount) { + for (GetConnectionHook each : SERVICE_LOADER) { + each.finishSuccess(dataSourceMetaData, connectionCount); + } + } + + @Override + public void finishFailure(final Exception cause) { + for (GetConnectionHook each : SERVICE_LOADER) { + each.finishFailure(cause); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/ShardingEventHandler.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/event/ShardingEventHandler.java index 383f7f909230c..78e8ca30b06bc 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/ShardingEventHandler.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/event/ShardingEventHandler.java @@ -22,7 +22,7 @@ * * @author zhangliang * @param type of start event - * @param type of finish event + * @param type of finishSuccess event */ public interface ShardingEventHandler { @@ -34,9 +34,9 @@ public interface ShardingEventHandler type of start event - * @param type of finish event + * @param type of finishSuccess event */ public interface ShardingEventHandlerLoader { @@ -34,9 +34,9 @@ public interface ShardingEventHandlerLoader - * Licensed 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 io.shardingsphere.core.spi.event.connection.close; - -import io.shardingsphere.core.spi.event.ShardingEventHandlerLoader; - -import java.util.ServiceLoader; - -/** - * Connection event handler loader. - * - * @author zhangliang - */ -public final class CloseConnectionEventHandlerLoader implements ShardingEventHandlerLoader { - - private static final CloseConnectionEventHandlerLoader INSTANCE = new CloseConnectionEventHandlerLoader(); - - private final ServiceLoader serviceLoader; - - private CloseConnectionEventHandlerLoader() { - serviceLoader = ServiceLoader.load(CloseConnectionEventHandler.class); - } - - /** - * Get instance. - * - * @return instance - */ - public static CloseConnectionEventHandlerLoader getInstance() { - return INSTANCE; - } - - @Override - public void start(final CloseConnectionStartEvent event) { - for (CloseConnectionEventHandler each : serviceLoader) { - each.start(event); - } - } - - @Override - public void finish(final CloseConnectionFinishEvent event) { - for (CloseConnectionEventHandler each : serviceLoader) { - each.finish(event); - } - } -} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/close/CloseConnectionFinishEvent.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/close/CloseConnectionFinishEvent.java deleted file mode 100644 index e4c2f81fee735..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/close/CloseConnectionFinishEvent.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.core.spi.event.connection.close; - -import io.shardingsphere.core.spi.event.ShardingFinishEvent; - -/** - * Close connection finish event. - * - * @author zhangyonglun - */ -public final class CloseConnectionFinishEvent extends ShardingFinishEvent { -} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionEventHandler.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionEventHandler.java deleted file mode 100644 index 2f3a074ca0114..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionEventHandler.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.core.spi.event.connection.get; - -import io.shardingsphere.core.spi.event.ShardingEventHandler; - -/** - * Connection event handler. - * - * @author zhangliang - */ -public interface GetConnectionEventHandler extends ShardingEventHandler { -} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionEventHandlerLoader.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionEventHandlerLoader.java deleted file mode 100644 index 71d6f8d6d205e..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionEventHandlerLoader.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.core.spi.event.connection.get; - -import io.shardingsphere.core.spi.event.ShardingEventHandlerLoader; - -import java.util.ServiceLoader; - -/** - * Connection event handler loader. - * - * @author zhangliang - */ -public final class GetConnectionEventHandlerLoader implements ShardingEventHandlerLoader { - - private static final GetConnectionEventHandlerLoader INSTANCE = new GetConnectionEventHandlerLoader(); - - private final ServiceLoader serviceLoader; - - private GetConnectionEventHandlerLoader() { - serviceLoader = ServiceLoader.load(GetConnectionEventHandler.class); - } - - /** - * Get instance. - * - * @return instance - */ - public static GetConnectionEventHandlerLoader getInstance() { - return INSTANCE; - } - - @Override - public void start(final GetConnectionStartEvent event) { - for (GetConnectionEventHandler each : serviceLoader) { - each.start(event); - } - } - - @Override - public void finish(final GetConnectionFinishEvent event) { - for (GetConnectionEventHandler each : serviceLoader) { - each.finish(event); - } - } -} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionStartEvent.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionStartEvent.java deleted file mode 100644 index fffb91b65a94b..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/get/GetConnectionStartEvent.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.core.spi.event.connection.get; - -import io.shardingsphere.core.spi.event.ShardingStartEvent; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -/** - * Get connection start event. - * - * @author zhangyonglun - */ -@RequiredArgsConstructor -@Getter -public final class GetConnectionStartEvent extends ShardingStartEvent { - - private final String dataSource; -} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionEventHandlerLoader.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionEventHandlerLoader.java deleted file mode 100644 index 4b5823f12ff5e..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionEventHandlerLoader.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.core.spi.event.executor; - -import io.shardingsphere.core.spi.event.ShardingEventHandlerLoader; - -import java.util.ServiceLoader; - -/** - * SQL Execution event handler loader. - * - * @author zhangliang - */ -public final class SQLExecutionEventHandlerLoader implements ShardingEventHandlerLoader { - - private static final SQLExecutionEventHandlerLoader INSTANCE = new SQLExecutionEventHandlerLoader(); - - private final ServiceLoader serviceLoader; - - private SQLExecutionEventHandlerLoader() { - serviceLoader = ServiceLoader.load(SQLExecutionEventHandler.class); - } - - /** - * Get instance. - * - * @return instance - */ - public static SQLExecutionEventHandlerLoader getInstance() { - return INSTANCE; - } - - @Override - public void start(final SQLExecutionStartEvent event) { - for (SQLExecutionEventHandler each : serviceLoader) { - each.start(event); - } - } - - @Override - public void finish(final SQLExecutionFinishEvent event) { - for (SQLExecutionEventHandler each : serviceLoader) { - each.finish(event); - } - } -} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingEventHandler.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingEventHandler.java deleted file mode 100644 index 03e71921e9cbc..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingEventHandler.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.core.spi.event.parsing; - -import io.shardingsphere.core.spi.event.ShardingEventHandler; - -/** - * Parsing event handler. - * - * @author zhangliang - */ -public interface ParsingEventHandler extends ShardingEventHandler { -} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingEventHandlerLoader.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingEventHandlerLoader.java deleted file mode 100644 index 66c4d6cd6e5a1..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingEventHandlerLoader.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.core.spi.event.parsing; - -import io.shardingsphere.core.spi.event.ShardingEventHandlerLoader; - -import java.util.ServiceLoader; - -/** - * Parsing event handler loader. - * - * @author zhangliang - */ -public final class ParsingEventHandlerLoader implements ShardingEventHandlerLoader { - - private static final ParsingEventHandlerLoader INSTANCE = new ParsingEventHandlerLoader(); - - private final ServiceLoader serviceLoader; - - private ParsingEventHandlerLoader() { - serviceLoader = ServiceLoader.load(ParsingEventHandler.class); - } - - /** - * Get instance. - * - * @return instance - */ - public static ParsingEventHandlerLoader getInstance() { - return INSTANCE; - } - - @Override - public void start(final ParsingStartEvent event) { - for (ParsingEventHandler each : serviceLoader) { - each.start(event); - } - } - - @Override - public void finish(final ParsingFinishEvent event) { - for (ParsingEventHandler each : serviceLoader) { - each.finish(event); - } - } -} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingFinishEvent.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingFinishEvent.java deleted file mode 100644 index 7ac5cc4a4547f..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingFinishEvent.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.core.spi.event.parsing; - -import io.shardingsphere.core.spi.event.ShardingFinishEvent; - -/** - * Parsing finish event. - * - * @author zhangyonglun - */ -public final class ParsingFinishEvent extends ShardingFinishEvent { -} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingStartEvent.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingStartEvent.java deleted file mode 100644 index 2180d6ff87d16..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/parsing/ParsingStartEvent.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.core.spi.event.parsing; - -import io.shardingsphere.core.spi.event.ShardingStartEvent; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -/** - * Parsing start event. - * - * @author zhangyonglun - */ -@RequiredArgsConstructor -@Getter -public final class ParsingStartEvent extends ShardingStartEvent { - - private final String sql; -} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/executor/SPISQLExecutionHook.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/executor/SPISQLExecutionHook.java new file mode 100644 index 0000000000000..ed59132a041dc --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/executor/SPISQLExecutionHook.java @@ -0,0 +1,59 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.core.spi.executor; + +import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; +import io.shardingsphere.core.routing.RouteUnit; + +import java.util.List; +import java.util.ServiceLoader; + +/** + * SQL Execution hook for SPI. + * + * @author zhangliang + */ +public final class SPISQLExecutionHook implements SQLExecutionHook { + + private static final ServiceLoader SERVICE_LOADER; + + static { + SERVICE_LOADER = ServiceLoader.load(SQLExecutionHook.class); + } + + @Override + public void start(final RouteUnit routeUnit, final List parameters, final DataSourceMetaData dataSourceMetaData) { + for (SQLExecutionHook each : SERVICE_LOADER) { + each.start(routeUnit, parameters, dataSourceMetaData); + } + } + + @Override + public void finishSuccess() { + for (SQLExecutionHook each : SERVICE_LOADER) { + each.finishSuccess(); + } + } + + @Override + public void finishFailure(final Exception cause) { + for (SQLExecutionHook each : SERVICE_LOADER) { + each.finishFailure(cause); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionStartEvent.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/executor/SQLExecutionHook.java similarity index 56% rename from sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionStartEvent.java rename to sharding-core/src/main/java/io/shardingsphere/core/spi/executor/SQLExecutionHook.java index 2eb3d24bebb80..2f88ec8bbff04 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionStartEvent.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/executor/SQLExecutionHook.java @@ -15,28 +15,38 @@ *

*/ -package io.shardingsphere.core.spi.event.executor; +package io.shardingsphere.core.spi.executor; -import io.shardingsphere.core.spi.event.ShardingStartEvent; import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; import io.shardingsphere.core.routing.RouteUnit; -import lombok.Getter; -import lombok.RequiredArgsConstructor; import java.util.List; /** - * SQL execution start event. + * SQL Execution hook. * * @author zhangliang */ -@RequiredArgsConstructor -@Getter -public final class SQLExecutionStartEvent extends ShardingStartEvent { +public interface SQLExecutionHook { - private final RouteUnit routeUnit; + /** + * Handle when SQL execution started. + * + * @param routeUnit route unit + * @param parameters parameters + * @param dataSourceMetaData data source meta data + */ + void start(RouteUnit routeUnit, List parameters, DataSourceMetaData dataSourceMetaData); - private final List parameters; + /** + * Handle when SQL execution finished success. + */ + void finishSuccess(); - private final DataSourceMetaData dataSourceMetaData; + /** + * Handle when SQL execution finished failure. + * + * @param cause failure cause + */ + void finishFailure(Exception cause); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/parsing/ParsingHook.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/parsing/ParsingHook.java new file mode 100644 index 0000000000000..02dc7a39a396a --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/parsing/ParsingHook.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.core.spi.parsing; + +/** + * Parsing hook. + * + * @author zhangliang + */ +public interface ParsingHook { + + /** + * Handle when parse started. + * + * @param sql SQL to be parsed + */ + void start(String sql); + + /** + * Handle when parse finished success. + */ + void finishSuccess(); + + /** + * Handle when parse finished failure. + * + * @param cause failure cause + */ + void finishFailure(Exception cause); +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/parsing/SPIParsingHook.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/parsing/SPIParsingHook.java new file mode 100644 index 0000000000000..117b03e80111b --- /dev/null +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/parsing/SPIParsingHook.java @@ -0,0 +1,55 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.core.spi.parsing; + +import java.util.ServiceLoader; + +/** + * Parsing hook for SPI. + * + * @author zhangliang + */ +public final class SPIParsingHook implements ParsingHook { + + private static final ServiceLoader SERVICE_LOADER; + + static { + SERVICE_LOADER = ServiceLoader.load(ParsingHook.class); + } + + @Override + public void start(final String sql) { + for (ParsingHook each : SERVICE_LOADER) { + each.start(sql); + } + } + + @Override + public void finishSuccess() { + for (ParsingHook each : SERVICE_LOADER) { + each.finishSuccess(); + } + } + + @Override + public void finishFailure(final Exception cause) { + for (ParsingHook each : SERVICE_LOADER) { + each.finishFailure(cause); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHandler.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHook.java similarity index 81% rename from sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHandler.java rename to sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHook.java index dd2c284a8910f..c4afa755854a4 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHandler.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHook.java @@ -18,19 +18,19 @@ package io.shardingsphere.core.spi.root; /** - * Root invoke handler. + * Root invoke hook. * * @author zhangliang */ -public interface RootInvokeHandler { +public interface RootInvokeHook { /** - * Start invoke. + * Handle when root invoke started. */ void start(); /** - * Finish invoke. + * Handle when root invoke finished success. */ - void finish(); + void finishSuccess(); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHandlerLoader.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/root/SPIRootInvokeHook.java similarity index 52% rename from sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHandlerLoader.java rename to sharding-core/src/main/java/io/shardingsphere/core/spi/root/SPIRootInvokeHook.java index 790dc0c3f7632..ac38e9f66da41 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHandlerLoader.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/root/SPIRootInvokeHook.java @@ -20,44 +20,29 @@ import java.util.ServiceLoader; /** - * Root invoke handler loader. + * Root invoke hook for SPI. * * @author zhangliang */ -public final class RootInvokeHandlerLoader { +public final class SPIRootInvokeHook implements RootInvokeHook { - private static final RootInvokeHandlerLoader INSTANCE = new RootInvokeHandlerLoader(); + private static final ServiceLoader SERVICE_LOADER; - private final ServiceLoader serviceLoader; - - private RootInvokeHandlerLoader() { - serviceLoader = ServiceLoader.load(RootInvokeHandler.class); - } - - /** - * Get instance. - * - * @return instance - */ - public static RootInvokeHandlerLoader getInstance() { - return INSTANCE; + static { + SERVICE_LOADER = ServiceLoader.load(RootInvokeHook.class); } - /** - * Start invoke. - */ + @Override public void start() { - for (RootInvokeHandler each : serviceLoader) { + for (RootInvokeHook each : SERVICE_LOADER) { each.start(); } } - /** - * Finish invoke. - */ - public void finish() { - for (RootInvokeHandler each : serviceLoader) { - each.finish(); + @Override + public void finishSuccess() { + for (RootInvokeHook each : SERVICE_LOADER) { + each.finishSuccess(); } } } diff --git a/sharding-jdbc/src/main/java/io/shardingsphere/core/executor/AbstractStatementExecutor.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/executor/AbstractStatementExecutor.java index d822b7d5aa576..c4ad09731ef4e 100644 --- a/sharding-jdbc/src/main/java/io/shardingsphere/core/executor/AbstractStatementExecutor.java +++ b/sharding-jdbc/src/main/java/io/shardingsphere/core/executor/AbstractStatementExecutor.java @@ -88,9 +88,12 @@ public AbstractStatementExecutor(final int resultSetType, final int resultSetCon this.resultSetConcurrency = resultSetConcurrency; this.resultSetHoldability = resultSetHoldability; this.connection = shardingConnection; - sqlExecutePrepareTemplate = TransactionType.XA == TransactionTypeHolder.get() ? new SQLExecutePrepareTemplate(connection.getShardingContext().getMaxConnectionsSizePerQuery()) - : new SQLExecutePrepareTemplate(connection.getShardingContext().getMaxConnectionsSizePerQuery(), connection.getShardingContext().getExecuteEngine()); - sqlExecuteTemplate = new SQLExecuteTemplate(connection.getShardingContext().getExecuteEngine()); + DatabaseType databaseType = connection.getShardingContext().getDatabaseType(); + int maxConnectionsSizePerQuery = connection.getShardingContext().getMaxConnectionsSizePerQuery(); + ShardingExecuteEngine executeEngine = connection.getShardingContext().getExecuteEngine(); + sqlExecutePrepareTemplate = TransactionType.XA == TransactionTypeHolder.get() + ? new SQLExecutePrepareTemplate(databaseType, maxConnectionsSizePerQuery) : new SQLExecutePrepareTemplate(databaseType, maxConnectionsSizePerQuery, executeEngine); + sqlExecuteTemplate = new SQLExecuteTemplate(executeEngine); } protected final void cacheStatements() { diff --git a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java index 79af61eb3c92b..4a7219278dda8 100644 --- a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java +++ b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java @@ -25,12 +25,6 @@ import io.shardingsphere.core.constant.transaction.TransactionOperationType; import io.shardingsphere.core.constant.transaction.TransactionType; import io.shardingsphere.core.event.ShardingEventBusInstance; -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionEventHandlerLoader; -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionFinishEvent; -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionStartEvent; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionEventHandlerLoader; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionFinishEvent; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionStartEvent; import io.shardingsphere.core.event.transaction.xa.XATransactionEvent; import io.shardingsphere.core.hint.HintManagerHolder; import io.shardingsphere.core.jdbc.adapter.executor.ForceExecuteCallback; @@ -38,7 +32,10 @@ import io.shardingsphere.core.jdbc.unsupported.AbstractUnsupportedOperationConnection; import io.shardingsphere.core.metadata.datasource.DataSourceMetaDataFactory; import io.shardingsphere.core.routing.router.masterslave.MasterVisitedManager; -import io.shardingsphere.core.spi.root.RootInvokeHandlerLoader; +import io.shardingsphere.core.spi.connection.close.CloseConnectionHook; +import io.shardingsphere.core.spi.connection.close.SPICloseConnectionHook; +import io.shardingsphere.core.spi.root.RootInvokeHook; +import io.shardingsphere.core.spi.root.SPIRootInvokeHook; import io.shardingsphere.core.transaction.TransactionTypeHolder; import lombok.RequiredArgsConstructor; @@ -77,7 +74,11 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera private final ForceExecuteTemplate forceExecuteTemplate = new ForceExecuteTemplate<>(); - private final ForceExecuteTemplate> forceExecuteTemplateForClose = new ForceExecuteTemplate<>(); + private final ForceExecuteTemplate> forceExecuteTemplateForClose = new ForceExecuteTemplate<>(); + + private final RootInvokeHook rootInvokeHook = new SPIRootInvokeHook(); + + private final CloseConnectionHook closeConnectionHook = new SPICloseConnectionHook(); /** * Get database connection. @@ -100,7 +101,6 @@ public final Connection getConnection(final String dataSourceName) throws SQLExc * @throws SQLException SQL exception */ public final List getConnections(final ConnectionMode connectionMode, final String dataSourceName, final int connectionSize) throws SQLException { - GetConnectionEventHandlerLoader.getInstance().start(new GetConnectionStartEvent(dataSourceName)); DataSource dataSource = getDataSourceMap().get(dataSourceName); Preconditions.checkState(null != dataSource, "Missing the data source name: '%s'", dataSourceName); Collection connections; @@ -124,8 +124,6 @@ public final List getConnections(final ConnectionMode connectionMode cachedConnections.putAll(dataSourceName, result); } } - GetConnectionEventHandlerLoader.getInstance().finish( - new GetConnectionFinishEvent(connections.size(), DataSourceMetaDataFactory.newInstance(databaseType, result.get(0).getMetaData().getURL()))); return result; } @@ -228,22 +226,19 @@ public final void close() throws SQLException { @Override public void execute(final Entry cachedConnections) throws SQLException { Connection connection = cachedConnections.getValue(); - CloseConnectionEventHandlerLoader.getInstance().start( - new CloseConnectionStartEvent(cachedConnections.getKey(), DataSourceMetaDataFactory.newInstance(databaseType, connection.getMetaData().getURL()))); - CloseConnectionFinishEvent finishEvent = new CloseConnectionFinishEvent(); + closeConnectionHook.start(cachedConnections.getKey(), DataSourceMetaDataFactory.newInstance(databaseType, connection.getMetaData().getURL())); try { connection.close(); + closeConnectionHook.finishSuccess(); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON - finishEvent.setException(ex); + closeConnectionHook.finishFailure(ex); throw ex; - } finally { - CloseConnectionEventHandlerLoader.getInstance().finish(finishEvent); } } }); - RootInvokeHandlerLoader.getInstance().finish(); + rootInvokeHook.finishSuccess(); } @Override diff --git a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/connection/ShardingConnection.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/connection/ShardingConnection.java index e7bde79d0c460..c0ceef129ff06 100644 --- a/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/connection/ShardingConnection.java +++ b/sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/core/connection/ShardingConnection.java @@ -22,7 +22,8 @@ import io.shardingsphere.core.jdbc.core.statement.ShardingPreparedStatement; import io.shardingsphere.core.jdbc.core.statement.ShardingStatement; import io.shardingsphere.core.rule.MasterSlaveRule; -import io.shardingsphere.core.spi.root.RootInvokeHandlerLoader; +import io.shardingsphere.core.spi.root.RootInvokeHook; +import io.shardingsphere.core.spi.root.SPIRootInvokeHook; import lombok.Getter; import javax.sql.DataSource; @@ -48,11 +49,13 @@ public final class ShardingConnection extends AbstractConnectionAdapter { private final ShardingContext shardingContext; + private final RootInvokeHook rootInvokeHook = new SPIRootInvokeHook(); + public ShardingConnection(final Map dataSourceMap, final ShardingContext shardingContext) { super(shardingContext.getDatabaseType()); this.dataSourceMap = dataSourceMap; this.shardingContext = shardingContext; - RootInvokeHandlerLoader.getInstance().start(); + rootInvokeHook.start(); } /** diff --git a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/assertion/IntegrateTestCasesLoader.java b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/assertion/IntegrateTestCasesLoader.java index 9c0f94b4cee43..ac7c956383ea5 100644 --- a/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/assertion/IntegrateTestCasesLoader.java +++ b/sharding-jdbc/src/test/java/io/shardingsphere/dbtest/cases/assertion/IntegrateTestCasesLoader.java @@ -163,7 +163,7 @@ public DQLIntegrateTestCase getDQLIntegrateTestCase(final String sqlCaseId) { // Preconditions.checkState(dqlIntegrateTestCaseMap.containsKey(sqlCaseId), "Can't find SQL of id: " + sqlCaseId); // TODO remove when transfer finished if (!dqlIntegrateTestCaseMap.containsKey(sqlCaseId)) { - log.warn("Have not finish case `{}`", sqlCaseId); + log.warn("Have not finishSuccess case `{}`", sqlCaseId); } return (DQLIntegrateTestCase) dqlIntegrateTestCaseMap.get(sqlCaseId); } @@ -179,7 +179,7 @@ public DMLIntegrateTestCase getDMLIntegrateTestCase(final String sqlCaseId) { // Preconditions.checkState(dmlIntegrateTestCaseMap.containsKey(sqlCaseId), "Can't find SQL of id: " + sqlCaseId); // TODO remove when transfer finished if (!dmlIntegrateTestCaseMap.containsKey(sqlCaseId)) { - log.warn("Have not finish case `{}`", sqlCaseId); + log.warn("Have not finishSuccess case `{}`", sqlCaseId); } return (DMLIntegrateTestCase) dmlIntegrateTestCaseMap.get(sqlCaseId); } @@ -195,7 +195,7 @@ public DDLIntegrateTestCase getDDLIntegrateTestCase(final String sqlCaseId) { // Preconditions.checkState(ddlIntegrateTestCaseMap.containsKey(sqlCaseId), "Can't find SQL of id: " + sqlCaseId); // TODO remove when transfer finished if (!ddlIntegrateTestCaseMap.containsKey(sqlCaseId)) { - log.warn("Have not finish case `{}`", sqlCaseId); + log.warn("Have not finishSuccess case `{}`", sqlCaseId); } return (DDLIntegrateTestCase) ddlIntegrateTestCaseMap.get(sqlCaseId); } @@ -211,7 +211,7 @@ public DCLIntegrateTestCase getDCLIntegrateTestCase(final String sqlCaseId) { // Preconditions.checkState(ddlIntegrateTestCaseMap.containsKey(sqlCaseId), "Can't find SQL of id: " + sqlCaseId); // TODO remove when transfer finished if (!dclIntegrateTestCaseMap.containsKey(sqlCaseId)) { - log.warn("Have not finish case `{}`", sqlCaseId); + log.warn("Have not finishSuccess case `{}`", sqlCaseId); } return (DCLIntegrateTestCase) dclIntegrateTestCaseMap.get(sqlCaseId); } diff --git a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/connection/OpenTracingCloseConnectionEventHandler.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/connection/OpenTracingCloseConnectionEventHandler.java deleted file mode 100644 index 3df05fa91a41f..0000000000000 --- a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/connection/OpenTracingCloseConnectionEventHandler.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.opentracing.handler.connection; - -import io.opentracing.Span; -import io.opentracing.Tracer.SpanBuilder; -import io.opentracing.tag.Tags; -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionEventHandler; -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionFinishEvent; -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionStartEvent; -import io.shardingsphere.opentracing.constant.ShardingTags; -import io.shardingsphere.opentracing.handler.OpenTracingHandler; - -/** - * Open tracing close connection event handler. - * - * @author zhangliang - */ -public final class OpenTracingCloseConnectionEventHandler extends OpenTracingHandler implements CloseConnectionEventHandler { - - private static final String OPERATION_NAME = "/" + ShardingTags.COMPONENT_NAME + "/closeConnection/"; - - public OpenTracingCloseConnectionEventHandler() { - super(OPERATION_NAME); - } - - @Override - protected Span initSpan(final CloseConnectionStartEvent event, final SpanBuilder spanBuilder) { - return spanBuilder.withTag(Tags.DB_INSTANCE.getKey(), event.getDataSource()) - .withTag(Tags.PEER_HOSTNAME.getKey(), event.getDataSourceMetaData().getHostName()) - .withTag(Tags.PEER_PORT.getKey(), event.getDataSourceMetaData().getPort()).startManual(); - } -} diff --git a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/connection/OpenTracingGetConnectionEventHandler.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/connection/OpenTracingGetConnectionEventHandler.java deleted file mode 100644 index eed2ab22a75a8..0000000000000 --- a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/connection/OpenTracingGetConnectionEventHandler.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.opentracing.handler.connection; - -import io.opentracing.ActiveSpan; -import io.opentracing.Span; -import io.opentracing.Tracer.SpanBuilder; -import io.opentracing.tag.Tags; -import io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorDataMap; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionEventHandler; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionFinishEvent; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionStartEvent; -import io.shardingsphere.opentracing.constant.ShardingTags; -import io.shardingsphere.opentracing.handler.OpenTracingHandler; -import io.shardingsphere.opentracing.handler.root.OpenTracingRootInvokeHandler; - -/** - * Open tracing get connection event handler. - * - * @author zhangliang - */ -public final class OpenTracingGetConnectionEventHandler extends OpenTracingHandler implements GetConnectionEventHandler { - - private static final String OPERATION_NAME = "/" + ShardingTags.COMPONENT_NAME + "/getConnection/"; - - private final ThreadLocal isTrunkThread = new ThreadLocal<>(); - - public OpenTracingGetConnectionEventHandler() { - super(OPERATION_NAME); - } - - @Override - protected Span initSpan(final GetConnectionStartEvent event, final SpanBuilder spanBuilder) { - isTrunkThread.set(OpenTracingRootInvokeHandler.isTrunkThread()); - if (ExecutorDataMap.getDataMap().containsKey(OpenTracingRootInvokeHandler.ROOT_SPAN_CONTINUATION) && !isTrunkThread.get()) { - OpenTracingRootInvokeHandler.getActiveSpan().set(((ActiveSpan.Continuation) ExecutorDataMap.getDataMap().get(OpenTracingRootInvokeHandler.ROOT_SPAN_CONTINUATION)).activate()); - } - return spanBuilder.withTag(Tags.DB_INSTANCE.getKey(), event.getDataSource()).startManual(); - } - - @Override - protected void updateSpan(final GetConnectionFinishEvent event, final Span span) { - if (null != event.getDataSourceMetaData()) { - span.setTag(Tags.PEER_HOSTNAME.getKey(), event.getDataSourceMetaData().getHostName()) - .setTag(Tags.PEER_PORT.getKey(), event.getDataSourceMetaData().getPort()) - .setTag(ShardingTags.CONNECTION_COUNT.getKey(), event.getConnectionCount()); - } - } - - @Override - protected void afterTracingFinish(final GetConnectionFinishEvent event) { - if (!isTrunkThread.get()) { - OpenTracingRootInvokeHandler.getActiveSpan().get().deactivate(); - OpenTracingRootInvokeHandler.getActiveSpan().remove(); - } - } -} diff --git a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/executor/OpenTracingSQLExecutionEventHandler.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/executor/OpenTracingSQLExecutionEventHandler.java deleted file mode 100644 index 7e9d67aa13568..0000000000000 --- a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/executor/OpenTracingSQLExecutionEventHandler.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.opentracing.handler.executor; - -import com.google.common.base.Joiner; -import io.opentracing.ActiveSpan; -import io.opentracing.Span; -import io.opentracing.Tracer.SpanBuilder; -import io.opentracing.tag.Tags; -import io.shardingsphere.core.spi.event.executor.SQLExecutionEventHandler; -import io.shardingsphere.core.spi.event.executor.SQLExecutionFinishEvent; -import io.shardingsphere.core.spi.event.executor.SQLExecutionStartEvent; -import io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorDataMap; -import io.shardingsphere.opentracing.constant.ShardingTags; -import io.shardingsphere.opentracing.handler.OpenTracingHandler; -import io.shardingsphere.opentracing.handler.root.OpenTracingRootInvokeHandler; - -/** - * Open tracing SQL execution event handler. - * - * @author zhangliang - */ -public final class OpenTracingSQLExecutionEventHandler extends OpenTracingHandler implements SQLExecutionEventHandler { - - private static final String OPERATION_NAME = "/" + ShardingTags.COMPONENT_NAME + "/executeSQL/"; - - private final ThreadLocal isTrunkThread = new ThreadLocal<>(); - - public OpenTracingSQLExecutionEventHandler() { - super(OPERATION_NAME); - } - - @Override - protected Span initSpan(final SQLExecutionStartEvent event, final SpanBuilder spanBuilder) { - isTrunkThread.set(OpenTracingRootInvokeHandler.isTrunkThread()); - if (ExecutorDataMap.getDataMap().containsKey(OpenTracingRootInvokeHandler.ROOT_SPAN_CONTINUATION) && !isTrunkThread.get()) { - OpenTracingRootInvokeHandler.getActiveSpan().set(((ActiveSpan.Continuation) ExecutorDataMap.getDataMap().get(OpenTracingRootInvokeHandler.ROOT_SPAN_CONTINUATION)).activate()); - } - return spanBuilder.withTag(Tags.PEER_HOSTNAME.getKey(), event.getDataSourceMetaData().getHostName()) - .withTag(Tags.PEER_PORT.getKey(), event.getDataSourceMetaData().getPort()) - .withTag(Tags.DB_TYPE.getKey(), "sql") - .withTag(Tags.DB_INSTANCE.getKey(), event.getRouteUnit().getDataSourceName()) - .withTag(Tags.DB_STATEMENT.getKey(), event.getRouteUnit().getSqlUnit().getSql()) - .withTag(ShardingTags.DB_BIND_VARIABLES.getKey(), event.getParameters().isEmpty() ? "" : Joiner.on(",").join(event.getParameters())).startManual(); - } - - @Override - protected void afterTracingFinish(final SQLExecutionFinishEvent event) { - if (!isTrunkThread.get()) { - OpenTracingRootInvokeHandler.getActiveSpan().get().deactivate(); - OpenTracingRootInvokeHandler.getActiveSpan().remove(); - } - } -} diff --git a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/parsing/OpenTracingParsingEventHandler.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/parsing/OpenTracingParsingEventHandler.java deleted file mode 100644 index 618a171e35f40..0000000000000 --- a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/parsing/OpenTracingParsingEventHandler.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2016-2018 shardingsphere.io. - *

- * Licensed 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 io.shardingsphere.opentracing.handler.parsing; - -import io.opentracing.Span; -import io.opentracing.Tracer.SpanBuilder; -import io.opentracing.tag.Tags; -import io.shardingsphere.core.spi.event.parsing.ParsingEventHandler; -import io.shardingsphere.core.spi.event.parsing.ParsingFinishEvent; -import io.shardingsphere.core.spi.event.parsing.ParsingStartEvent; -import io.shardingsphere.opentracing.constant.ShardingTags; -import io.shardingsphere.opentracing.handler.OpenTracingHandler; - -/** - * Open tracing parsing event handler. - * - * @author zhangliang - */ -public final class OpenTracingParsingEventHandler extends OpenTracingHandler implements ParsingEventHandler { - - private static final String OPERATION_NAME = "/" + ShardingTags.COMPONENT_NAME + "/parseSQL/"; - - public OpenTracingParsingEventHandler() { - super(OPERATION_NAME); - } - - @Override - protected Span initSpan(final ParsingStartEvent event, final SpanBuilder spanBuilder) { - return spanBuilder.withTag(Tags.DB_STATEMENT.getKey(), event.getSql()).startManual(); - } -} diff --git a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/root/OpenTracingRootInvokeHandler.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/root/OpenTracingRootInvokeHandler.java index 15d3f569740a6..d7386921f7d28 100644 --- a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/root/OpenTracingRootInvokeHandler.java +++ b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/root/OpenTracingRootInvokeHandler.java @@ -20,7 +20,7 @@ import io.opentracing.ActiveSpan; import io.opentracing.tag.Tags; import io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorDataMap; -import io.shardingsphere.core.spi.root.RootInvokeHandler; +import io.shardingsphere.core.spi.root.RootInvokeHook; import io.shardingsphere.opentracing.ShardingTracer; import io.shardingsphere.opentracing.constant.ShardingTags; @@ -29,7 +29,7 @@ * * @author zhangliang */ -public final class OpenTracingRootInvokeHandler implements RootInvokeHandler { +public final class OpenTracingRootInvokeHandler implements RootInvokeHook { public static final String ROOT_SPAN_CONTINUATION = "ROOT_SPAN_CONTINUATION"; @@ -44,7 +44,7 @@ public void start() { } @Override - public void finish() { + public void finishSuccess() { ACTIVE_SPAN.get().deactivate(); ACTIVE_SPAN.remove(); } diff --git a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/OpenTracingHandler.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingHandlerTemplate.java similarity index 61% rename from sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/OpenTracingHandler.java rename to sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingHandlerTemplate.java index b798deaf0108a..d24e7a4b0be1b 100644 --- a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/OpenTracingHandler.java +++ b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingHandlerTemplate.java @@ -15,14 +15,11 @@ *

*/ -package io.shardingsphere.opentracing.handler; +package io.shardingsphere.opentracing.handler.tracing; import io.opentracing.Span; import io.opentracing.Tracer.SpanBuilder; import io.opentracing.tag.Tags; -import io.shardingsphere.core.spi.event.ShardingEventHandler; -import io.shardingsphere.core.spi.event.ShardingFinishEvent; -import io.shardingsphere.core.spi.event.ShardingStartEvent; import io.shardingsphere.opentracing.ShardingTracer; import io.shardingsphere.opentracing.constant.ShardingErrorLogTags; import io.shardingsphere.opentracing.constant.ShardingTags; @@ -32,48 +29,59 @@ import java.util.Map; /** - * Opentracing listener. + * Opentracing handler template. * * @author zhangliang - * - * @param type of sharding start event - * @param type of sharding finish event */ @RequiredArgsConstructor -public abstract class OpenTracingHandler implements ShardingEventHandler { +public final class OpenTracingHandlerTemplate { private final ThreadLocal spanHolder = new ThreadLocal<>(); private final String operationName; - @Override - public final void start(final S event) { - spanHolder.set(initSpan(event, createSpanBuilder())); + /** + * Start record span. + * + * @param spanStartCallback span start callback + */ + public void start(final OpenTracingSpanStartCallback spanStartCallback) { + spanHolder.set(spanStartCallback.initSpan(createSpanBuilder())); } - @Override - public final void finish(final F event) { - if (null != event.getException()) { - setErrorInfo(event); - } - tracingFinish(event); + /** + * Finish record succeed span. + * + * @param spanFinishCallback span finish callback + */ + public void finishSuccess(final OpenTracingSpanFinishCallback spanFinishCallback) { + tracingFinish(spanFinishCallback); } private SpanBuilder createSpanBuilder() { return ShardingTracer.get().buildSpan(operationName).withTag(Tags.COMPONENT.getKey(), ShardingTags.COMPONENT_NAME).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT); } - protected abstract Span initSpan(S event, SpanBuilder spanBuilder); - - private void tracingFinish(final F event) { - updateSpan(event, spanHolder.get()); + private void tracingFinish(final OpenTracingSpanFinishCallback spanFinishCallback) { + spanFinishCallback.updateSpan(spanHolder.get()); spanHolder.get().finish(); spanHolder.remove(); - afterTracingFinish(event); + spanFinishCallback.afterTracingFinish(); + } + + /** + * Finish record failed span. + * + * @param cause failure cause + * @param spanFinishCallback span finish callback + */ + public void finishFailure(final Exception cause, final OpenTracingSpanFinishCallback spanFinishCallback) { + setErrorInfo(cause); + tracingFinish(spanFinishCallback); } - private void setErrorInfo(final F event) { - spanHolder.get().setTag(Tags.ERROR.getKey(), true).log(System.currentTimeMillis(), getReason(event.getException())); + private void setErrorInfo(final Exception cause) { + spanHolder.get().setTag(Tags.ERROR.getKey(), true).log(System.currentTimeMillis(), getReason(cause)); } private Map getReason(final Throwable cause) { @@ -83,10 +91,4 @@ private void setErrorInfo(final F event) { result.put(ShardingErrorLogTags.MESSAGE, cause.getMessage()); return result; } - - protected void updateSpan(final F event, final Span span) { - } - - protected void afterTracingFinish(final F event) { - } } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionFinishEvent.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanFinishCallback.java similarity index 63% rename from sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionFinishEvent.java rename to sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanFinishCallback.java index 7e9cb64ed9203..aab50f9aeda41 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionFinishEvent.java +++ b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanFinishCallback.java @@ -15,14 +15,26 @@ *

*/ -package io.shardingsphere.core.spi.event.executor; +package io.shardingsphere.opentracing.handler.tracing; -import io.shardingsphere.core.spi.event.ShardingFinishEvent; +import io.opentracing.Span; /** - * SQL execution finish event. + * Open tracing span finish callback. * * @author zhangliang */ -public final class SQLExecutionFinishEvent extends ShardingFinishEvent { +public interface OpenTracingSpanFinishCallback { + + /** + * Update span. + * + * @param span span to be updated. + */ + void updateSpan(Span span); + + /** + * Hook after tracing finished. + */ + void afterTracingFinish(); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/close/CloseConnectionEventHandler.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanFinishCallbackAdapter.java similarity index 64% rename from sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/close/CloseConnectionEventHandler.java rename to sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanFinishCallbackAdapter.java index d99e7d0ef27cc..b9a415b4ba530 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/connection/close/CloseConnectionEventHandler.java +++ b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanFinishCallbackAdapter.java @@ -15,14 +15,22 @@ *

*/ -package io.shardingsphere.core.spi.event.connection.close; +package io.shardingsphere.opentracing.handler.tracing; -import io.shardingsphere.core.spi.event.ShardingEventHandler; +import io.opentracing.Span; /** - * Connection event handler. + * Open tracing span finish callback adapter. * * @author zhangliang */ -public interface CloseConnectionEventHandler extends ShardingEventHandler { +public class OpenTracingSpanFinishCallbackAdapter implements OpenTracingSpanFinishCallback { + + @Override + public void updateSpan(final Span span) { + } + + @Override + public void afterTracingFinish() { + } } diff --git a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanFinishRootCleanCallbackAdapter.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanFinishRootCleanCallbackAdapter.java new file mode 100644 index 0000000000000..60f6caaefd8d2 --- /dev/null +++ b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanFinishRootCleanCallbackAdapter.java @@ -0,0 +1,45 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.opentracing.handler.tracing; + +import io.opentracing.Span; +import io.shardingsphere.opentracing.handler.root.OpenTracingRootInvokeHandler; +import lombok.RequiredArgsConstructor; + +/** + * Open tracing span finish root clean callback adapter. + * + * @author zhangliang + */ +@RequiredArgsConstructor +public class OpenTracingSpanFinishRootCleanCallbackAdapter implements OpenTracingSpanFinishCallback { + + private final boolean isTrunkThread; + + @Override + public void updateSpan(final Span span) { + } + + @Override + public final void afterTracingFinish() { + if (!isTrunkThread) { + OpenTracingRootInvokeHandler.getActiveSpan().get().deactivate(); + OpenTracingRootInvokeHandler.getActiveSpan().remove(); + } + } +} diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionEventHandler.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanStartCallback.java similarity index 63% rename from sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionEventHandler.java rename to sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanStartCallback.java index 4a838ae16cb51..9f03dfae702d8 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/event/executor/SQLExecutionEventHandler.java +++ b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/OpenTracingSpanStartCallback.java @@ -15,14 +15,23 @@ *

*/ -package io.shardingsphere.core.spi.event.executor; +package io.shardingsphere.opentracing.handler.tracing; -import io.shardingsphere.core.spi.event.ShardingEventHandler; +import io.opentracing.Span; +import io.opentracing.Tracer.SpanBuilder; /** - * SQL Execution event handler. + * Open tracing span start callback. * * @author zhangliang */ -public interface SQLExecutionEventHandler extends ShardingEventHandler { +public interface OpenTracingSpanStartCallback { + + /** + * Initialize span. + * + * @param spanBuilder span builder + * @return span to be initialized + */ + Span initSpan(SpanBuilder spanBuilder); } diff --git a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingCloseConnectionHook.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingCloseConnectionHook.java new file mode 100644 index 0000000000000..07a0550b98387 --- /dev/null +++ b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingCloseConnectionHook.java @@ -0,0 +1,63 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.opentracing.handler.tracing.connection; + +import io.opentracing.Span; +import io.opentracing.Tracer.SpanBuilder; +import io.opentracing.tag.Tags; +import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; +import io.shardingsphere.core.spi.connection.close.CloseConnectionHook; +import io.shardingsphere.opentracing.constant.ShardingTags; +import io.shardingsphere.opentracing.handler.tracing.OpenTracingHandlerTemplate; +import io.shardingsphere.opentracing.handler.tracing.OpenTracingSpanFinishCallbackAdapter; +import io.shardingsphere.opentracing.handler.tracing.OpenTracingSpanStartCallback; + +/** + * Open tracing close connection hook. + * + * @author zhangliang + */ +public final class OpenTracingCloseConnectionHook implements CloseConnectionHook { + + private static final String OPERATION_NAME = "/" + ShardingTags.COMPONENT_NAME + "/closeConnection/"; + + private final OpenTracingHandlerTemplate handlerTemplate = new OpenTracingHandlerTemplate(OPERATION_NAME); + + @Override + public void start(final String dataSourceName, final DataSourceMetaData dataSourceMetaData) { + handlerTemplate.start(new OpenTracingSpanStartCallback() { + + @Override + public Span initSpan(final SpanBuilder spanBuilder) { + return spanBuilder.withTag(Tags.DB_INSTANCE.getKey(), dataSourceName) + .withTag(Tags.PEER_HOSTNAME.getKey(), dataSourceMetaData.getHostName()) + .withTag(Tags.PEER_PORT.getKey(), dataSourceMetaData.getPort()).startManual(); + } + }); + } + + @Override + public void finishSuccess() { + handlerTemplate.finishSuccess(new OpenTracingSpanFinishCallbackAdapter()); + } + + @Override + public void finishFailure(final Exception cause) { + handlerTemplate.finishFailure(cause, new OpenTracingSpanFinishCallbackAdapter()); + } +} diff --git a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingGetConnectionHook.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingGetConnectionHook.java new file mode 100644 index 0000000000000..2e737586ef33e --- /dev/null +++ b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingGetConnectionHook.java @@ -0,0 +1,78 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.opentracing.handler.tracing.connection; + +import io.opentracing.ActiveSpan; +import io.opentracing.Span; +import io.opentracing.Tracer.SpanBuilder; +import io.opentracing.tag.Tags; +import io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorDataMap; +import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; +import io.shardingsphere.core.spi.connection.get.GetConnectionHook; +import io.shardingsphere.opentracing.constant.ShardingTags; +import io.shardingsphere.opentracing.handler.root.OpenTracingRootInvokeHandler; +import io.shardingsphere.opentracing.handler.tracing.OpenTracingHandlerTemplate; +import io.shardingsphere.opentracing.handler.tracing.OpenTracingSpanFinishRootCleanCallbackAdapter; +import io.shardingsphere.opentracing.handler.tracing.OpenTracingSpanStartCallback; + +/** + * Open tracing get connection hook. + * + * @author zhangliang + */ +public final class OpenTracingGetConnectionHook implements GetConnectionHook { + + private static final String OPERATION_NAME = "/" + ShardingTags.COMPONENT_NAME + "/getConnection/"; + + private final ThreadLocal isTrunkThread = new ThreadLocal<>(); + + private final OpenTracingHandlerTemplate handlerTemplate = new OpenTracingHandlerTemplate(OPERATION_NAME); + + @Override + public void start(final String dataSourceName) { + handlerTemplate.start(new OpenTracingSpanStartCallback() { + + @Override + public Span initSpan(final SpanBuilder spanBuilder) { + isTrunkThread.set(OpenTracingRootInvokeHandler.isTrunkThread()); + if (ExecutorDataMap.getDataMap().containsKey(OpenTracingRootInvokeHandler.ROOT_SPAN_CONTINUATION) && !isTrunkThread.get()) { + OpenTracingRootInvokeHandler.getActiveSpan().set(((ActiveSpan.Continuation) ExecutorDataMap.getDataMap().get(OpenTracingRootInvokeHandler.ROOT_SPAN_CONTINUATION)).activate()); + } + return spanBuilder.withTag(Tags.DB_INSTANCE.getKey(), dataSourceName).startManual(); + } + }); + } + + @Override + public void finishSuccess(final DataSourceMetaData dataSourceMetaData, final int connectionCount) { + handlerTemplate.finishSuccess(new OpenTracingSpanFinishRootCleanCallbackAdapter(isTrunkThread.get()) { + + @Override + public void updateSpan(final Span span) { + span.setTag(Tags.PEER_HOSTNAME.getKey(), dataSourceMetaData.getHostName()) + .setTag(Tags.PEER_PORT.getKey(), dataSourceMetaData.getPort()) + .setTag(ShardingTags.CONNECTION_COUNT.getKey(), connectionCount); + } + }); + } + + @Override + public void finishFailure(final Exception cause) { + handlerTemplate.finishFailure(cause, new OpenTracingSpanFinishRootCleanCallbackAdapter(isTrunkThread.get())); + } +} diff --git a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/executor/OpenTracingSQLExecutionHook.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/executor/OpenTracingSQLExecutionHook.java new file mode 100644 index 0000000000000..74a35054f001a --- /dev/null +++ b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/executor/OpenTracingSQLExecutionHook.java @@ -0,0 +1,79 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.opentracing.handler.tracing.executor; + +import com.google.common.base.Joiner; +import io.opentracing.ActiveSpan; +import io.opentracing.Span; +import io.opentracing.Tracer.SpanBuilder; +import io.opentracing.tag.Tags; +import io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorDataMap; +import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; +import io.shardingsphere.core.routing.RouteUnit; +import io.shardingsphere.core.spi.executor.SQLExecutionHook; +import io.shardingsphere.opentracing.constant.ShardingTags; +import io.shardingsphere.opentracing.handler.root.OpenTracingRootInvokeHandler; +import io.shardingsphere.opentracing.handler.tracing.OpenTracingHandlerTemplate; +import io.shardingsphere.opentracing.handler.tracing.OpenTracingSpanFinishRootCleanCallbackAdapter; +import io.shardingsphere.opentracing.handler.tracing.OpenTracingSpanStartCallback; + +import java.util.List; + +/** + * Open tracing SQL execution hook. + * + * @author zhangliang + */ +public final class OpenTracingSQLExecutionHook implements SQLExecutionHook { + + private static final String OPERATION_NAME = "/" + ShardingTags.COMPONENT_NAME + "/executeSQL/"; + + private final ThreadLocal isTrunkThread = new ThreadLocal<>(); + + private final OpenTracingHandlerTemplate handlerTemplate = new OpenTracingHandlerTemplate(OPERATION_NAME); + + @Override + public void start(final RouteUnit routeUnit, final List parameters, final DataSourceMetaData dataSourceMetaData) { + handlerTemplate.start(new OpenTracingSpanStartCallback() { + + @Override + public Span initSpan(final SpanBuilder spanBuilder) { + isTrunkThread.set(OpenTracingRootInvokeHandler.isTrunkThread()); + if (ExecutorDataMap.getDataMap().containsKey(OpenTracingRootInvokeHandler.ROOT_SPAN_CONTINUATION) && !isTrunkThread.get()) { + OpenTracingRootInvokeHandler.getActiveSpan().set(((ActiveSpan.Continuation) ExecutorDataMap.getDataMap().get(OpenTracingRootInvokeHandler.ROOT_SPAN_CONTINUATION)).activate()); + } + return spanBuilder.withTag(Tags.PEER_HOSTNAME.getKey(), dataSourceMetaData.getHostName()) + .withTag(Tags.PEER_PORT.getKey(), dataSourceMetaData.getPort()) + .withTag(Tags.DB_TYPE.getKey(), "sql") + .withTag(Tags.DB_INSTANCE.getKey(), routeUnit.getDataSourceName()) + .withTag(Tags.DB_STATEMENT.getKey(), routeUnit.getSqlUnit().getSql()) + .withTag(ShardingTags.DB_BIND_VARIABLES.getKey(), parameters.isEmpty() ? "" : Joiner.on(",").join(parameters)).startManual(); + } + }); + } + + @Override + public void finishSuccess() { + handlerTemplate.finishSuccess(new OpenTracingSpanFinishRootCleanCallbackAdapter(isTrunkThread.get())); + } + + @Override + public void finishFailure(final Exception cause) { + handlerTemplate.finishFailure(cause, new OpenTracingSpanFinishRootCleanCallbackAdapter(isTrunkThread.get())); + } +} diff --git a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/parsing/OpenTracingParsingHook.java b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/parsing/OpenTracingParsingHook.java new file mode 100644 index 0000000000000..9c170eedb32ac --- /dev/null +++ b/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/parsing/OpenTracingParsingHook.java @@ -0,0 +1,60 @@ +/* + * Copyright 2016-2018 shardingsphere.io. + *

+ * Licensed 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 io.shardingsphere.opentracing.handler.tracing.parsing; + +import io.opentracing.Span; +import io.opentracing.Tracer.SpanBuilder; +import io.opentracing.tag.Tags; +import io.shardingsphere.core.spi.parsing.ParsingHook; +import io.shardingsphere.opentracing.constant.ShardingTags; +import io.shardingsphere.opentracing.handler.tracing.OpenTracingHandlerTemplate; +import io.shardingsphere.opentracing.handler.tracing.OpenTracingSpanFinishCallbackAdapter; +import io.shardingsphere.opentracing.handler.tracing.OpenTracingSpanStartCallback; + +/** + * Open tracing parsing hook. + * + * @author zhangliang + */ +public final class OpenTracingParsingHook implements ParsingHook { + + private static final String OPERATION_NAME = "/" + ShardingTags.COMPONENT_NAME + "/parseSQL/"; + + private final OpenTracingHandlerTemplate handlerTemplate = new OpenTracingHandlerTemplate(OPERATION_NAME); + + @Override + public void start(final String sql) { + handlerTemplate.start(new OpenTracingSpanStartCallback() { + + @Override + public Span initSpan(final SpanBuilder spanBuilder) { + return spanBuilder.withTag(Tags.DB_STATEMENT.getKey(), sql).startManual(); + } + }); + } + + @Override + public void finishSuccess() { + handlerTemplate.finishSuccess(new OpenTracingSpanFinishCallbackAdapter()); + } + + @Override + public void finishFailure(final Exception cause) { + handlerTemplate.finishFailure(cause, new OpenTracingSpanFinishCallbackAdapter()); + } +} diff --git a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.connection.close.CloseConnectionHook b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.connection.close.CloseConnectionHook new file mode 100644 index 0000000000000..e951760c43422 --- /dev/null +++ b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.connection.close.CloseConnectionHook @@ -0,0 +1 @@ +io.shardingsphere.opentracing.handler.tracing.connection.OpenTracingCloseConnectionHook diff --git a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.connection.get.GetConnectionHook b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.connection.get.GetConnectionHook new file mode 100644 index 0000000000000..c5002654e8ed3 --- /dev/null +++ b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.connection.get.GetConnectionHook @@ -0,0 +1 @@ +io.shardingsphere.opentracing.handler.tracing.connection.OpenTracingGetConnectionHook diff --git a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.connection.close.CloseConnectionEventHandler b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.connection.close.CloseConnectionEventHandler deleted file mode 100644 index 7a469b59afc23..0000000000000 --- a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.connection.close.CloseConnectionEventHandler +++ /dev/null @@ -1 +0,0 @@ -io.shardingsphere.opentracing.handler.connection.OpenTracingCloseConnectionEventHandler diff --git a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.connection.get.GetConnectionEventHandler b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.connection.get.GetConnectionEventHandler deleted file mode 100644 index 058abad3bae57..0000000000000 --- a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.connection.get.GetConnectionEventHandler +++ /dev/null @@ -1 +0,0 @@ -io.shardingsphere.opentracing.handler.connection.OpenTracingGetConnectionEventHandler diff --git a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.executor.SQLExecutionEventHandler b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.executor.SQLExecutionEventHandler deleted file mode 100644 index 29b8f0ba748a3..0000000000000 --- a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.executor.SQLExecutionEventHandler +++ /dev/null @@ -1 +0,0 @@ -io.shardingsphere.opentracing.handler.executor.OpenTracingSQLExecutionEventHandler diff --git a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.parsing.ParsingEventHandler b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.parsing.ParsingEventHandler deleted file mode 100644 index cc968b7cacc0f..0000000000000 --- a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.event.parsing.ParsingEventHandler +++ /dev/null @@ -1 +0,0 @@ -io.shardingsphere.opentracing.handler.parsing.OpenTracingParsingEventHandler diff --git a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.executor.SQLExecutionHook b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.executor.SQLExecutionHook new file mode 100644 index 0000000000000..4102fbd9efacd --- /dev/null +++ b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.executor.SQLExecutionHook @@ -0,0 +1 @@ +io.shardingsphere.opentracing.handler.tracing.executor.OpenTracingSQLExecutionHook diff --git a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.parsing.ParsingHook b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.parsing.ParsingHook new file mode 100644 index 0000000000000..35beb2ec30c9c --- /dev/null +++ b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.parsing.ParsingHook @@ -0,0 +1 @@ +io.shardingsphere.opentracing.handler.tracing.parsing.OpenTracingParsingHook diff --git a/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.root.RootInvokeHandler b/sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.root.RootInvokeHook similarity index 100% rename from sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.root.RootInvokeHandler rename to sharding-opentracing/src/main/resources/META-INF/services/io.shardingsphere.core.spi.root.RootInvokeHook diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/AllHandlerTests.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/AllHandlerTests.java index 77de7ab38a860..2c23c97ed7287 100644 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/AllHandlerTests.java +++ b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/AllHandlerTests.java @@ -17,10 +17,10 @@ package io.shardingsphere.opentracing.handler; -import io.shardingsphere.opentracing.handler.connection.OpenTracingCloseConnectionEventHandlerTest; -import io.shardingsphere.opentracing.handler.connection.OpenTracingGetConnectionEventHandlerTest; -import io.shardingsphere.opentracing.handler.executor.OpenTracingSQLExecutionEventHandlerTest; -import io.shardingsphere.opentracing.handler.parsing.OpenTracingParsingEventHandlerTest; +import io.shardingsphere.opentracing.handler.tracing.connection.OpenTracingCloseConnectionEventHandlerTest; +import io.shardingsphere.opentracing.handler.tracing.connection.OpenTracingGetConnectionEventHandlerTest; +import io.shardingsphere.opentracing.handler.tracing.executor.OpenTracingSQLExecutionEventHandlerTest; +import io.shardingsphere.opentracing.handler.tracing.parsing.OpenTracingParsingEventHandlerTest; import io.shardingsphere.opentracing.handler.root.OpenTracingRootInvokeHandlerTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/root/OpenTracingRootInvokeHandlerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/root/OpenTracingRootInvokeHandlerTest.java index 6b762bc883c46..b3314c16647fb 100644 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/root/OpenTracingRootInvokeHandlerTest.java +++ b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/root/OpenTracingRootInvokeHandlerTest.java @@ -18,7 +18,8 @@ package io.shardingsphere.opentracing.handler.root; import io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorDataMap; -import io.shardingsphere.core.spi.root.RootInvokeHandlerLoader; +import io.shardingsphere.core.spi.root.RootInvokeHook; +import io.shardingsphere.core.spi.root.SPIRootInvokeHook; import io.shardingsphere.opentracing.handler.BaseOpenTracingHandlerTest; import org.junit.Test; @@ -29,15 +30,15 @@ public final class OpenTracingRootInvokeHandlerTest extends BaseOpenTracingHandlerTest { - private final RootInvokeHandlerLoader loader = RootInvokeHandlerLoader.getInstance(); + private final RootInvokeHook rootInvokeHook = new SPIRootInvokeHook(); @Test public void assertRootInvoke() { - loader.start(); + rootInvokeHook.start(); assertTrue(OpenTracingRootInvokeHandler.isTrunkThread()); assertNotNull(OpenTracingRootInvokeHandler.getActiveSpan().get()); assertTrue(ExecutorDataMap.getDataMap().containsKey(OpenTracingRootInvokeHandler.ROOT_SPAN_CONTINUATION)); - loader.finish(); + rootInvokeHook.finishSuccess(); assertFalse(OpenTracingRootInvokeHandler.isTrunkThread()); assertNull(OpenTracingRootInvokeHandler.getActiveSpan().get()); } diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/connection/OpenTracingCloseConnectionEventHandlerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingCloseConnectionEventHandlerTest.java similarity index 79% rename from sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/connection/OpenTracingCloseConnectionEventHandlerTest.java rename to sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingCloseConnectionEventHandlerTest.java index d4d8eb66fe8b0..5c4755ca311e1 100644 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/connection/OpenTracingCloseConnectionEventHandlerTest.java +++ b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingCloseConnectionEventHandlerTest.java @@ -15,14 +15,13 @@ *

*/ -package io.shardingsphere.opentracing.handler.connection; +package io.shardingsphere.opentracing.handler.tracing.connection; import io.opentracing.mock.MockSpan; import io.opentracing.tag.Tags; -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionEventHandlerLoader; -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionFinishEvent; -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionStartEvent; import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; +import io.shardingsphere.core.spi.connection.close.CloseConnectionHook; +import io.shardingsphere.core.spi.connection.close.SPICloseConnectionHook; import io.shardingsphere.opentracing.constant.ShardingTags; import io.shardingsphere.opentracing.handler.BaseOpenTracingHandlerTest; import org.hamcrest.CoreMatchers; @@ -37,15 +36,15 @@ public final class OpenTracingCloseConnectionEventHandlerTest extends BaseOpenTracingHandlerTest { - private final CloseConnectionEventHandlerLoader loader = CloseConnectionEventHandlerLoader.getInstance(); + private final CloseConnectionHook closeConnectionHook = new SPICloseConnectionHook(); @Test public void assertExecuteSuccess() { DataSourceMetaData dataSourceMetaData = mock(DataSourceMetaData.class); when(dataSourceMetaData.getHostName()).thenReturn("localhost"); when(dataSourceMetaData.getPort()).thenReturn(8888); - loader.start(new CloseConnectionStartEvent("test_ds_name", dataSourceMetaData)); - loader.finish(new CloseConnectionFinishEvent()); + closeConnectionHook.start("test_ds_name", dataSourceMetaData); + closeConnectionHook.finishSuccess(); assertThat(getTracer().finishedSpans().size(), is(1)); MockSpan actual = getTracer().finishedSpans().get(0); assertThat(actual.operationName(), is("/Sharding-Sphere/closeConnection/")); @@ -62,10 +61,8 @@ public void assertExecuteFailure() { DataSourceMetaData dataSourceMetaData = mock(DataSourceMetaData.class); when(dataSourceMetaData.getHostName()).thenReturn("localhost"); when(dataSourceMetaData.getPort()).thenReturn(8888); - loader.start(new CloseConnectionStartEvent("test_ds_name", dataSourceMetaData)); - CloseConnectionFinishEvent finishEvent = new CloseConnectionFinishEvent(); - finishEvent.setException(new RuntimeException("close connection error")); - loader.finish(finishEvent); + closeConnectionHook.start("test_ds_name", dataSourceMetaData); + closeConnectionHook.finishFailure(new RuntimeException("close connection error")); assertThat(getTracer().finishedSpans().size(), is(1)); MockSpan actual = getTracer().finishedSpans().get(0); assertThat(actual.operationName(), is("/Sharding-Sphere/closeConnection/")); diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/connection/OpenTracingGetConnectionEventHandlerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingGetConnectionEventHandlerTest.java similarity index 82% rename from sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/connection/OpenTracingGetConnectionEventHandlerTest.java rename to sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingGetConnectionEventHandlerTest.java index dbb0c21eb8866..49757df7d09b0 100644 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/connection/OpenTracingGetConnectionEventHandlerTest.java +++ b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingGetConnectionEventHandlerTest.java @@ -15,7 +15,7 @@ *

*/ -package io.shardingsphere.opentracing.handler.connection; +package io.shardingsphere.opentracing.handler.tracing.connection; import io.opentracing.ActiveSpan; import io.opentracing.ActiveSpan.Continuation; @@ -23,9 +23,8 @@ import io.opentracing.tag.Tags; import io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorDataMap; import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionEventHandlerLoader; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionFinishEvent; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionStartEvent; +import io.shardingsphere.core.spi.connection.get.GetConnectionHook; +import io.shardingsphere.core.spi.connection.get.SPIGetConnectionHook; import io.shardingsphere.opentracing.constant.ShardingTags; import io.shardingsphere.opentracing.handler.BaseOpenTracingHandlerTest; import io.shardingsphere.opentracing.handler.root.OpenTracingRootInvokeHandler; @@ -44,16 +43,16 @@ public final class OpenTracingGetConnectionEventHandlerTest extends BaseOpenTracingHandlerTest { - private final GetConnectionEventHandlerLoader loader = GetConnectionEventHandlerLoader.getInstance(); + private final GetConnectionHook getConnectionHook = new SPIGetConnectionHook(); @Test public void assertExecuteSuccessTrunkThread() { new OpenTracingRootInvokeHandler().start(); - loader.start(new GetConnectionStartEvent("test_ds_name")); + getConnectionHook.start("test_ds_name"); DataSourceMetaData dataSourceMetaData = mock(DataSourceMetaData.class); when(dataSourceMetaData.getHostName()).thenReturn("localhost"); when(dataSourceMetaData.getPort()).thenReturn(8888); - loader.finish(new GetConnectionFinishEvent(3, dataSourceMetaData)); + getConnectionHook.finishSuccess(dataSourceMetaData, 3); assertThat(getTracer().finishedSpans().size(), is(1)); MockSpan actual = getTracer().finishedSpans().get(0); assertThat(actual.operationName(), is("/Sharding-Sphere/getConnection/")); @@ -64,7 +63,7 @@ public void assertExecuteSuccessTrunkThread() { assertThat(actualTags.get(Tags.PEER_HOSTNAME.getKey()), CoreMatchers.is("localhost")); assertThat(actualTags.get(Tags.PEER_PORT.getKey()), CoreMatchers.is(8888)); assertThat(actualTags.get(ShardingTags.CONNECTION_COUNT.getKey()), CoreMatchers.is(3)); - new OpenTracingRootInvokeHandler().finish(); + new OpenTracingRootInvokeHandler().finishSuccess(); } @Test @@ -73,12 +72,12 @@ public void assertExecuteSuccessBranchThread() { ActiveSpan activeSpan = mock(ActiveSpan.class); when(activeSpanContinuation.activate()).thenReturn(activeSpan); ExecutorDataMap.getDataMap().put(OpenTracingRootInvokeHandler.ROOT_SPAN_CONTINUATION, activeSpanContinuation); - loader.start(new GetConnectionStartEvent("test_ds_name")); + getConnectionHook.start("test_ds_name"); assertNotNull(OpenTracingRootInvokeHandler.getActiveSpan().get()); DataSourceMetaData dataSourceMetaData = mock(DataSourceMetaData.class); when(dataSourceMetaData.getHostName()).thenReturn("localhost"); when(dataSourceMetaData.getPort()).thenReturn(8888); - loader.finish(new GetConnectionFinishEvent(3, dataSourceMetaData)); + getConnectionHook.finishSuccess(dataSourceMetaData, 3); assertThat(getTracer().finishedSpans().size(), is(1)); MockSpan actual = getTracer().finishedSpans().get(0); assertThat(actual.operationName(), is("/Sharding-Sphere/getConnection/")); @@ -96,10 +95,8 @@ public void assertExecuteSuccessBranchThread() { @Test public void assertExecuteFailure() { new OpenTracingRootInvokeHandler().start(); - loader.start(new GetConnectionStartEvent("test_ds_name")); - GetConnectionFinishEvent finishEvent = new GetConnectionFinishEvent(3, null); - finishEvent.setException(new RuntimeException("get connection error")); - loader.finish(finishEvent); + getConnectionHook.start("test_ds_name"); + getConnectionHook.finishFailure(new RuntimeException("get connection error")); assertThat(getTracer().finishedSpans().size(), is(1)); MockSpan actual = getTracer().finishedSpans().get(0); assertThat(actual.operationName(), is("/Sharding-Sphere/getConnection/")); @@ -107,6 +104,6 @@ public void assertExecuteFailure() { assertThat(actualTags.get(Tags.COMPONENT.getKey()), CoreMatchers.is(ShardingTags.COMPONENT_NAME)); assertThat(actualTags.get(Tags.SPAN_KIND.getKey()), CoreMatchers.is(Tags.SPAN_KIND_CLIENT)); assertSpanError(actual, RuntimeException.class, "get connection error"); - new OpenTracingRootInvokeHandler().finish(); + new OpenTracingRootInvokeHandler().finishSuccess(); } } diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/executor/OpenTracingSQLExecutionEventHandlerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/executor/OpenTracingSQLExecutionEventHandlerTest.java similarity index 85% rename from sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/executor/OpenTracingSQLExecutionEventHandlerTest.java rename to sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/executor/OpenTracingSQLExecutionEventHandlerTest.java index e5b2ed7abdd23..8968e88cf0ee4 100644 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/executor/OpenTracingSQLExecutionEventHandlerTest.java +++ b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/executor/OpenTracingSQLExecutionEventHandlerTest.java @@ -15,19 +15,18 @@ *

*/ -package io.shardingsphere.opentracing.handler.executor; +package io.shardingsphere.opentracing.handler.tracing.executor; import io.opentracing.ActiveSpan; import io.opentracing.ActiveSpan.Continuation; import io.opentracing.mock.MockSpan; import io.opentracing.tag.Tags; -import io.shardingsphere.core.spi.event.executor.SQLExecutionEventHandlerLoader; -import io.shardingsphere.core.spi.event.executor.SQLExecutionFinishEvent; -import io.shardingsphere.core.spi.event.executor.SQLExecutionStartEvent; import io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorDataMap; import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; import io.shardingsphere.core.routing.RouteUnit; import io.shardingsphere.core.routing.SQLUnit; +import io.shardingsphere.core.spi.executor.SPISQLExecutionHook; +import io.shardingsphere.core.spi.executor.SQLExecutionHook; import io.shardingsphere.opentracing.constant.ShardingTags; import io.shardingsphere.opentracing.handler.BaseOpenTracingHandlerTest; import io.shardingsphere.opentracing.handler.root.OpenTracingRootInvokeHandler; @@ -49,7 +48,7 @@ public final class OpenTracingSQLExecutionEventHandlerTest extends BaseOpenTracingHandlerTest { - private final SQLExecutionEventHandlerLoader loader = SQLExecutionEventHandlerLoader.getInstance(); + private final SQLExecutionHook sqlExecutionHook = new SPISQLExecutionHook(); @Test public void assertExecuteSuccessForTrunkThread() { @@ -58,8 +57,8 @@ public void assertExecuteSuccessForTrunkThread() { when(dataSourceMetaData.getHostName()).thenReturn("localhost"); when(dataSourceMetaData.getPort()).thenReturn(8888); SQLUnit sqlUnit = new SQLUnit("SELECT * FROM XXX;", Collections.>emptyList()); - loader.start(new SQLExecutionStartEvent(new RouteUnit("ds_test", sqlUnit), Arrays.asList("1", 2), dataSourceMetaData)); - loader.finish(new SQLExecutionFinishEvent()); + sqlExecutionHook.start(new RouteUnit("ds_test", sqlUnit), Arrays.asList("1", 2), dataSourceMetaData); + sqlExecutionHook.finishSuccess(); assertThat(getTracer().finishedSpans().size(), is(1)); MockSpan actual = getTracer().finishedSpans().get(0); assertThat(actual.operationName(), is("/Sharding-Sphere/executeSQL/")); @@ -72,7 +71,7 @@ public void assertExecuteSuccessForTrunkThread() { assertThat(actualTags.get(Tags.DB_INSTANCE.getKey()), CoreMatchers.is("ds_test")); assertThat(actualTags.get(Tags.DB_STATEMENT.getKey()), CoreMatchers.is("SELECT * FROM XXX;")); assertThat(actualTags.get(ShardingTags.DB_BIND_VARIABLES.getKey()), CoreMatchers.is("1,2")); - new OpenTracingRootInvokeHandler().finish(); + new OpenTracingRootInvokeHandler().finishSuccess(); } @Test @@ -85,9 +84,9 @@ public void assertExecuteSuccessForBranchThread() { when(dataSourceMetaData.getHostName()).thenReturn("localhost"); when(dataSourceMetaData.getPort()).thenReturn(8888); SQLUnit sqlUnit = new SQLUnit("SELECT * FROM XXX;", Collections.>emptyList()); - loader.start(new SQLExecutionStartEvent(new RouteUnit("ds_test", sqlUnit), Arrays.asList("1", 2), dataSourceMetaData)); + sqlExecutionHook.start(new RouteUnit("ds_test", sqlUnit), Arrays.asList("1", 2), dataSourceMetaData); assertNotNull(OpenTracingRootInvokeHandler.getActiveSpan().get()); - loader.finish(new SQLExecutionFinishEvent()); + sqlExecutionHook.finishSuccess(); assertThat(getTracer().finishedSpans().size(), is(1)); MockSpan actual = getTracer().finishedSpans().get(0); assertThat(actual.operationName(), is("/Sharding-Sphere/executeSQL/")); @@ -111,10 +110,8 @@ public void assertExecuteFailure() { when(dataSourceMetaData.getHostName()).thenReturn("localhost"); when(dataSourceMetaData.getPort()).thenReturn(8888); SQLUnit sqlUnit = new SQLUnit("SELECT * FROM XXX;", Collections.>emptyList()); - loader.start(new SQLExecutionStartEvent(new RouteUnit("ds_test", sqlUnit), Collections.emptyList(), dataSourceMetaData)); - SQLExecutionFinishEvent finishEvent = new SQLExecutionFinishEvent(); - finishEvent.setException(new RuntimeException("SQL execution error")); - loader.finish(finishEvent); + sqlExecutionHook.start(new RouteUnit("ds_test", sqlUnit), Collections.emptyList(), dataSourceMetaData); + sqlExecutionHook.finishFailure(new RuntimeException("SQL execution error")); assertThat(getTracer().finishedSpans().size(), is(1)); MockSpan actual = getTracer().finishedSpans().get(0); assertThat(actual.operationName(), is("/Sharding-Sphere/executeSQL/")); @@ -128,6 +125,6 @@ public void assertExecuteFailure() { assertThat(actualTags.get(Tags.DB_STATEMENT.getKey()), CoreMatchers.is("SELECT * FROM XXX;")); assertThat(actualTags.get(ShardingTags.DB_BIND_VARIABLES.getKey()), CoreMatchers.is("")); assertSpanError(actual, RuntimeException.class, "SQL execution error"); - new OpenTracingRootInvokeHandler().finish(); + new OpenTracingRootInvokeHandler().finishSuccess(); } } diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/parsing/OpenTracingParsingEventHandlerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/parsing/OpenTracingParsingEventHandlerTest.java similarity index 77% rename from sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/parsing/OpenTracingParsingEventHandlerTest.java rename to sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/parsing/OpenTracingParsingEventHandlerTest.java index fc8dfc249292e..29d4dcd105df7 100644 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/parsing/OpenTracingParsingEventHandlerTest.java +++ b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/parsing/OpenTracingParsingEventHandlerTest.java @@ -15,14 +15,13 @@ *

*/ -package io.shardingsphere.opentracing.handler.parsing; +package io.shardingsphere.opentracing.handler.tracing.parsing; import io.opentracing.mock.MockSpan; import io.opentracing.tag.Tags; -import io.shardingsphere.core.spi.event.parsing.ParsingEventHandlerLoader; -import io.shardingsphere.core.spi.event.parsing.ParsingFinishEvent; -import io.shardingsphere.core.spi.event.parsing.ParsingStartEvent; import io.shardingsphere.core.exception.ShardingException; +import io.shardingsphere.core.spi.parsing.ParsingHook; +import io.shardingsphere.core.spi.parsing.SPIParsingHook; import io.shardingsphere.opentracing.constant.ShardingTags; import io.shardingsphere.opentracing.handler.BaseOpenTracingHandlerTest; import org.hamcrest.CoreMatchers; @@ -35,12 +34,12 @@ public final class OpenTracingParsingEventHandlerTest extends BaseOpenTracingHandlerTest { - private final ParsingEventHandlerLoader loader = ParsingEventHandlerLoader.getInstance(); + private final ParsingHook parsingHook = new SPIParsingHook(); @Test public void assertExecuteSuccess() { - loader.start(new ParsingStartEvent("SELECT * FROM XXX;")); - loader.finish(new ParsingFinishEvent()); + parsingHook.start("SELECT * FROM XXX;"); + parsingHook.finishSuccess(); assertThat(getTracer().finishedSpans().size(), is(1)); MockSpan actual = getTracer().finishedSpans().get(0); assertThat(actual.operationName(), is("/Sharding-Sphere/parseSQL/")); @@ -52,10 +51,8 @@ public void assertExecuteSuccess() { @Test public void assertExecuteFailure() { - loader.start(new ParsingStartEvent("SELECT * FROM XXX;")); - ParsingFinishEvent finishEvent = new ParsingFinishEvent(); - finishEvent.setException(new ShardingException("parse SQL error")); - loader.finish(finishEvent); + parsingHook.start("SELECT * FROM XXX;"); + parsingHook.finishFailure(new ShardingException("parse SQL error")); assertThat(getTracer().finishedSpans().size(), is(1)); MockSpan actual = getTracer().finishedSpans().get(0); assertThat(actual.operationName(), is("/Sharding-Sphere/parseSQL/")); diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/connection/BackendConnection.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/connection/BackendConnection.java index bf6955f6dd106..5c4a161f87fc8 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/connection/BackendConnection.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/connection/BackendConnection.java @@ -19,14 +19,10 @@ import io.shardingsphere.core.constant.ConnectionMode; import io.shardingsphere.core.constant.DatabaseType; -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionEventHandlerLoader; -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionFinishEvent; -import io.shardingsphere.core.spi.event.connection.close.CloseConnectionStartEvent; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionEventHandlerLoader; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionFinishEvent; -import io.shardingsphere.core.spi.event.connection.get.GetConnectionStartEvent; import io.shardingsphere.core.metadata.datasource.DataSourceMetaDataFactory; import io.shardingsphere.core.routing.router.masterslave.MasterVisitedManager; +import io.shardingsphere.core.spi.connection.close.CloseConnectionHook; +import io.shardingsphere.core.spi.connection.close.SPICloseConnectionHook; import io.shardingsphere.proxy.config.RuleRegistry; import lombok.Getter; import lombok.NoArgsConstructor; @@ -60,6 +56,8 @@ public final class BackendConnection implements AutoCloseable { private final Collection cachedResultSets = new CopyOnWriteArrayList<>(); + private final CloseConnectionHook closeConnectionHook = new SPICloseConnectionHook(); + /** * Get connections of current thread datasource. * @@ -70,21 +68,9 @@ public final class BackendConnection implements AutoCloseable { * @throws SQLException SQL exception */ public List getConnections(final ConnectionMode connectionMode, final String dataSourceName, final int connectionSize) throws SQLException { - try { - GetConnectionEventHandlerLoader.getInstance().start(new GetConnectionStartEvent(dataSourceName)); - List result = ruleRegistry.getBackendDataSource().getConnections(connectionMode, dataSourceName, connectionSize); - cachedConnections.addAll(result); - GetConnectionEventHandlerLoader.getInstance().finish( - new GetConnectionFinishEvent(result.size(), DataSourceMetaDataFactory.newInstance(DatabaseType.MySQL, result.get(0).getMetaData().getURL()))); - return result; - // CHECKSTYLE:OFF - } catch (final Exception ex) { - // CHECKSTYLE:ON - GetConnectionFinishEvent finishEvent = new GetConnectionFinishEvent(0, null); - finishEvent.setException(ex); - GetConnectionEventHandlerLoader.getInstance().finish(finishEvent); - throw ex; - } + List result = ruleRegistry.getBackendDataSource().getConnections(connectionMode, dataSourceName, connectionSize); + cachedConnections.addAll(result); + return result; } /** @@ -154,16 +140,13 @@ private Collection closeStatements() { private Collection closeConnections() { Collection result = new LinkedList<>(); for (Connection each : cachedConnections) { - CloseConnectionFinishEvent finishEvent = new CloseConnectionFinishEvent(); try { - CloseConnectionEventHandlerLoader.getInstance().start( - new CloseConnectionStartEvent(each.getCatalog(), DataSourceMetaDataFactory.newInstance(DatabaseType.MySQL, each.getMetaData().getURL()))); + closeConnectionHook.start(each.getCatalog(), DataSourceMetaDataFactory.newInstance(DatabaseType.MySQL, each.getMetaData().getURL())); each.close(); + closeConnectionHook.finishSuccess(); } catch (SQLException ex) { - finishEvent.setException(ex); + closeConnectionHook.finishFailure(ex); result.add(ex); - } finally { - CloseConnectionEventHandlerLoader.getInstance().finish(finishEvent); } } return result; diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/execute/JDBCExecuteEngine.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/execute/JDBCExecuteEngine.java index f820e4784b914..7d81d5defcd2f 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/execute/JDBCExecuteEngine.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/backend/jdbc/execute/JDBCExecuteEngine.java @@ -21,6 +21,7 @@ import io.shardingsphere.core.constant.DatabaseType; import io.shardingsphere.core.constant.SQLType; import io.shardingsphere.core.constant.transaction.TransactionType; +import io.shardingsphere.core.executor.ShardingExecuteEngine; import io.shardingsphere.core.executor.ShardingExecuteGroup; import io.shardingsphere.core.executor.StatementExecuteUnit; import io.shardingsphere.core.executor.sql.execute.SQLExecuteCallback; @@ -97,9 +98,12 @@ public final class JDBCExecuteEngine implements SQLExecuteEngine { public JDBCExecuteEngine(final BackendConnection backendConnection, final JDBCExecutorWrapper jdbcExecutorWrapper) { this.backendConnection = backendConnection; this.jdbcExecutorWrapper = jdbcExecutorWrapper; - sqlExecutePrepareTemplate = TransactionType.XA == ProxyContext.getInstance().getTransactionType() ? new SQLExecutePrepareTemplate(ProxyContext.getInstance().getMaxConnectionsSizePerQuery()) - : new SQLExecutePrepareTemplate(ProxyContext.getInstance().getMaxConnectionsSizePerQuery(), BackendExecutorContext.getInstance().getExecuteEngine()); - sqlExecuteTemplate = new SQLExecuteTemplate(BackendExecutorContext.getInstance().getExecuteEngine()); + DatabaseType databaseType = DatabaseType.MySQL; + int maxConnectionsSizePerQuery = ProxyContext.getInstance().getMaxConnectionsSizePerQuery(); + ShardingExecuteEngine executeEngine = BackendExecutorContext.getInstance().getExecuteEngine(); + sqlExecutePrepareTemplate = TransactionType.XA == ProxyContext.getInstance().getTransactionType() + ? new SQLExecutePrepareTemplate(databaseType, maxConnectionsSizePerQuery) : new SQLExecutePrepareTemplate(databaseType, maxConnectionsSizePerQuery, executeEngine); + sqlExecuteTemplate = new SQLExecuteTemplate(executeEngine); } @SuppressWarnings("unchecked") diff --git a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java index 361e15989b4d1..6ddf719de9ad7 100644 --- a/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java +++ b/sharding-proxy/src/main/java/io/shardingsphere/proxy/frontend/mysql/MySQLFrontendHandler.java @@ -22,7 +22,8 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.EventLoopGroup; -import io.shardingsphere.core.spi.root.RootInvokeHandlerLoader; +import io.shardingsphere.core.spi.root.RootInvokeHook; +import io.shardingsphere.core.spi.root.SPIRootInvokeHook; import io.shardingsphere.proxy.backend.jdbc.connection.BackendConnection; import io.shardingsphere.proxy.config.ProxyContext; import io.shardingsphere.proxy.frontend.common.FrontendHandler; @@ -60,6 +61,8 @@ public final class MySQLFrontendHandler extends FrontendHandler { private final AuthorityHandler authorityHandler = new AuthorityHandler(); + private final RootInvokeHook rootInvokeHook = new SPIRootInvokeHook(); + @Override protected void handshake(final ChannelHandlerContext context) { int connectionId = ConnectionIdGenerator.getInstance().nextId(); @@ -113,7 +116,7 @@ class CommandExecutor implements Runnable { @Override public void run() { - RootInvokeHandlerLoader.getInstance().start(); + rootInvokeHook.start(); try (MySQLPacketPayload payload = new MySQLPacketPayload(message); BackendConnection backendConnection = new BackendConnection()) { setBackendConnection(backendConnection); @@ -135,7 +138,7 @@ public void run() { // CHECKSTYLE:ON context.writeAndFlush(new ErrPacket(1, ServerErrorCode.ER_STD_UNKNOWN_EXCEPTION, ex.getMessage())); } - RootInvokeHandlerLoader.getInstance().finish(); + rootInvokeHook.finishSuccess(); } private CommandPacket getCommandPacket(final MySQLPacketPayload payload, final BackendConnection backendConnection, final FrontendHandler frontendHandler) throws SQLException {