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 6c716d03802dc..344c0b323b38a 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,17 +19,13 @@ 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; @@ -53,14 +49,10 @@ @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. * @@ -120,15 +112,7 @@ 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; - 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()); + List connections = callback.getConnections(connectionMode, dataSourceName, sqlUnitGroups.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/spi/connection/close/CloseConnectionHook.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/close/CloseConnectionHook.java deleted file mode 100644 index 49dbfd26fad81..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/close/CloseConnectionHook.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.core.spi.connection.close; - -import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; - -/** - * Connection hook. - * - * @author zhangliang - */ -public interface CloseConnectionHook { - - /** - * Handle when close connection started. - * - * @param dataSourceName data source name - * @param dataSourceMetaData data source meta data - */ - void start(String dataSourceName, 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 deleted file mode 100644 index 3a08a6e230c55..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/close/SPICloseConnectionHook.java +++ /dev/null @@ -1,53 +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.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 = 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/connection/get/GetConnectionHook.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/get/GetConnectionHook.java deleted file mode 100644 index 93e881d4f0dc0..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/get/GetConnectionHook.java +++ /dev/null @@ -1,50 +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.connection.get; - -import io.shardingsphere.core.metadata.datasource.DataSourceMetaData; - -/** - * Connection hook. - * - * @author zhangliang - */ -public interface GetConnectionHook { - - /** - * Handle when get connection started. - * - * @param dataSourceName data source name - */ - void start(String dataSourceName); - - /** - * 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 deleted file mode 100644 index e68944d965eea..0000000000000 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/connection/get/SPIGetConnectionHook.java +++ /dev/null @@ -1,53 +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.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 = 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-jdbc/src/main/java/io/shardingsphere/core/executor/AbstractStatementExecutor.java b/sharding-jdbc/src/main/java/io/shardingsphere/core/executor/AbstractStatementExecutor.java index c4ad09731ef4e..239d0a1e7cfad 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,11 +88,10 @@ public AbstractStatementExecutor(final int resultSetType, final int resultSetCon this.resultSetConcurrency = resultSetConcurrency; this.resultSetHoldability = resultSetHoldability; this.connection = shardingConnection; - 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); + ? new SQLExecutePrepareTemplate(maxConnectionsSizePerQuery) : new SQLExecutePrepareTemplate(maxConnectionsSizePerQuery, executeEngine); sqlExecuteTemplate = new SQLExecuteTemplate(executeEngine); } 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 08b08fc5d1d95..809bdf954cb88 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 @@ -30,10 +30,7 @@ import io.shardingsphere.core.jdbc.adapter.executor.ForceExecuteCallback; import io.shardingsphere.core.jdbc.adapter.executor.ForceExecuteTemplate; 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.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; @@ -78,8 +75,6 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera private final RootInvokeHook rootInvokeHook = new SPIRootInvokeHook(); - private final CloseConnectionHook closeConnectionHook = new SPICloseConnectionHook(); - /** * Get database connection. * @@ -226,17 +221,7 @@ public final void close() throws SQLException { @Override public void execute(final Entry cachedConnections) throws SQLException { - Connection connection = cachedConnections.getValue(); - closeConnectionHook.start(cachedConnections.getKey(), DataSourceMetaDataFactory.newInstance(databaseType, connection.getMetaData().getURL())); - try { - connection.close(); - closeConnectionHook.finishSuccess(); - // CHECKSTYLE:OFF - } catch (final Exception ex) { - // CHECKSTYLE:ON - closeConnectionHook.finishFailure(ex); - throw ex; - } + cachedConnections.getValue().close(); } }); rootInvokeHook.finish(connectionSize); 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 deleted file mode 100644 index 07a0550b98387..0000000000000 --- a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingCloseConnectionHook.java +++ /dev/null @@ -1,63 +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.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 deleted file mode 100644 index 2e737586ef33e..0000000000000 --- a/sharding-opentracing/src/main/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingGetConnectionHook.java +++ /dev/null @@ -1,78 +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.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/test/java/io/shardingsphere/opentracing/handler/AllHandlerTests.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/AllHandlerTests.java index 2c23c97ed7287..4c941f5c6f4c4 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,20 +17,16 @@ package io.shardingsphere.opentracing.handler; -import io.shardingsphere.opentracing.handler.tracing.connection.OpenTracingCloseConnectionEventHandlerTest; -import io.shardingsphere.opentracing.handler.tracing.connection.OpenTracingGetConnectionEventHandlerTest; +import io.shardingsphere.opentracing.handler.root.OpenTracingRootInvokeHandlerTest; 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; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({ - OpenTracingRootInvokeHandlerTest.class, - OpenTracingGetConnectionEventHandlerTest.class, - OpenTracingCloseConnectionEventHandlerTest.class, + OpenTracingRootInvokeHandlerTest.class, OpenTracingParsingEventHandlerTest.class, OpenTracingSQLExecutionEventHandlerTest.class }) diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingCloseConnectionEventHandlerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingCloseConnectionEventHandlerTest.java deleted file mode 100644 index 5c4755ca311e1..0000000000000 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingCloseConnectionEventHandlerTest.java +++ /dev/null @@ -1,77 +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.tracing.connection; - -import io.opentracing.mock.MockSpan; -import io.opentracing.tag.Tags; -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; -import org.junit.Test; - -import java.util.Map; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public final class OpenTracingCloseConnectionEventHandlerTest extends BaseOpenTracingHandlerTest { - - 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); - 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/")); - Map actualTags = actual.tags(); - assertThat(actualTags.get(Tags.COMPONENT.getKey()), CoreMatchers.is(ShardingTags.COMPONENT_NAME)); - assertThat(actualTags.get(Tags.SPAN_KIND.getKey()), CoreMatchers.is(Tags.SPAN_KIND_CLIENT)); - assertThat(actualTags.get(Tags.DB_INSTANCE.getKey()), CoreMatchers.is("test_ds_name")); - assertThat(actualTags.get(Tags.PEER_HOSTNAME.getKey()), CoreMatchers.is("localhost")); - assertThat(actualTags.get(Tags.PEER_PORT.getKey()), CoreMatchers.is(8888)); - } - - @Test - public void assertExecuteFailure() { - DataSourceMetaData dataSourceMetaData = mock(DataSourceMetaData.class); - when(dataSourceMetaData.getHostName()).thenReturn("localhost"); - when(dataSourceMetaData.getPort()).thenReturn(8888); - 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/")); - Map actualTags = actual.tags(); - assertThat(actualTags.get(Tags.COMPONENT.getKey()), CoreMatchers.is(ShardingTags.COMPONENT_NAME)); - assertThat(actualTags.get(Tags.SPAN_KIND.getKey()), CoreMatchers.is(Tags.SPAN_KIND_CLIENT)); - assertThat(actualTags.get(Tags.DB_INSTANCE.getKey()), CoreMatchers.is("test_ds_name")); - assertThat(actualTags.get(Tags.PEER_HOSTNAME.getKey()), CoreMatchers.is("localhost")); - assertThat(actualTags.get(Tags.PEER_PORT.getKey()), CoreMatchers.is(8888)); - assertSpanError(actual, RuntimeException.class, "close connection error"); - } -} diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingGetConnectionEventHandlerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingGetConnectionEventHandlerTest.java deleted file mode 100644 index 6422921650226..0000000000000 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/connection/OpenTracingGetConnectionEventHandlerTest.java +++ /dev/null @@ -1,109 +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.tracing.connection; - -import io.opentracing.ActiveSpan; -import io.opentracing.ActiveSpan.Continuation; -import io.opentracing.mock.MockSpan; -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.core.spi.connection.get.SPIGetConnectionHook; -import io.shardingsphere.opentracing.constant.ShardingTags; -import io.shardingsphere.opentracing.handler.BaseOpenTracingHandlerTest; -import io.shardingsphere.opentracing.handler.root.OpenTracingRootInvokeHandler; -import org.hamcrest.CoreMatchers; -import org.junit.Test; - -import java.util.Map; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public final class OpenTracingGetConnectionEventHandlerTest extends BaseOpenTracingHandlerTest { - - private final GetConnectionHook getConnectionHook = new SPIGetConnectionHook(); - - @Test - public void assertExecuteSuccessTrunkThread() { - new OpenTracingRootInvokeHandler().start(); - getConnectionHook.start("test_ds_name"); - DataSourceMetaData dataSourceMetaData = mock(DataSourceMetaData.class); - when(dataSourceMetaData.getHostName()).thenReturn("localhost"); - when(dataSourceMetaData.getPort()).thenReturn(8888); - getConnectionHook.finishSuccess(dataSourceMetaData, 3); - assertThat(getTracer().finishedSpans().size(), is(1)); - MockSpan actual = getTracer().finishedSpans().get(0); - assertThat(actual.operationName(), is("/Sharding-Sphere/getConnection/")); - Map actualTags = actual.tags(); - assertThat(actualTags.get(Tags.COMPONENT.getKey()), CoreMatchers.is(ShardingTags.COMPONENT_NAME)); - assertThat(actualTags.get(Tags.SPAN_KIND.getKey()), CoreMatchers.is(Tags.SPAN_KIND_CLIENT)); - assertThat(actualTags.get(Tags.DB_INSTANCE.getKey()), CoreMatchers.is("test_ds_name")); - 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(2); - } - - @Test - public void assertExecuteSuccessBranchThread() { - Continuation activeSpanContinuation = mock(Continuation.class); - ActiveSpan activeSpan = mock(ActiveSpan.class); - when(activeSpanContinuation.activate()).thenReturn(activeSpan); - ExecutorDataMap.getDataMap().put(OpenTracingRootInvokeHandler.ROOT_SPAN_CONTINUATION, activeSpanContinuation); - getConnectionHook.start("test_ds_name"); - assertNotNull(OpenTracingRootInvokeHandler.getActiveSpan().get()); - DataSourceMetaData dataSourceMetaData = mock(DataSourceMetaData.class); - when(dataSourceMetaData.getHostName()).thenReturn("localhost"); - when(dataSourceMetaData.getPort()).thenReturn(8888); - getConnectionHook.finishSuccess(dataSourceMetaData, 3); - assertThat(getTracer().finishedSpans().size(), is(1)); - MockSpan actual = getTracer().finishedSpans().get(0); - assertThat(actual.operationName(), is("/Sharding-Sphere/getConnection/")); - Map actualTags = actual.tags(); - assertThat(actualTags.get(Tags.COMPONENT.getKey()), CoreMatchers.is(ShardingTags.COMPONENT_NAME)); - assertThat(actualTags.get(Tags.SPAN_KIND.getKey()), CoreMatchers.is(Tags.SPAN_KIND_CLIENT)); - assertThat(actualTags.get(Tags.DB_INSTANCE.getKey()), CoreMatchers.is("test_ds_name")); - 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)); - verify(activeSpan).deactivate(); - assertNull(OpenTracingRootInvokeHandler.getActiveSpan().get()); - } - - @Test - public void assertExecuteFailure() { - new OpenTracingRootInvokeHandler().start(); - 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/")); - Map actualTags = actual.tags(); - 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(2); - } -} 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 82a0afd88a0f1..370ce0c391bc5 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 @@ -18,11 +18,7 @@ package io.shardingsphere.proxy.backend.jdbc.connection; import io.shardingsphere.core.constant.ConnectionMode; -import io.shardingsphere.core.constant.DatabaseType; -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; @@ -56,8 +52,6 @@ public final class BackendConnection implements AutoCloseable { private final Collection cachedResultSets = new CopyOnWriteArrayList<>(); - private final CloseConnectionHook closeConnectionHook = new SPICloseConnectionHook(); - /** * Get connection size. * @@ -150,11 +144,8 @@ private Collection closeConnections() { Collection result = new LinkedList<>(); for (Connection each : cachedConnections) { try { - closeConnectionHook.start(each.getCatalog(), DataSourceMetaDataFactory.newInstance(DatabaseType.MySQL, each.getMetaData().getURL())); each.close(); - closeConnectionHook.finishSuccess(); } catch (SQLException ex) { - closeConnectionHook.finishFailure(ex); result.add(ex); } } 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 7d81d5defcd2f..68546957799f3 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 @@ -98,11 +98,10 @@ public final class JDBCExecuteEngine implements SQLExecuteEngine { public JDBCExecuteEngine(final BackendConnection backendConnection, final JDBCExecutorWrapper jdbcExecutorWrapper) { this.backendConnection = backendConnection; this.jdbcExecutorWrapper = jdbcExecutorWrapper; - 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); + ? new SQLExecutePrepareTemplate(maxConnectionsSizePerQuery) : new SQLExecutePrepareTemplate(maxConnectionsSizePerQuery, executeEngine); sqlExecuteTemplate = new SQLExecuteTemplate(executeEngine); }