From a7ce754b6c03e0349a28194b00e9c9e0ee84d858 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Tue, 25 Sep 2018 18:32:27 +0800 Subject: [PATCH] for #1172, add connection size for root invoke --- .../io/shardingsphere/core/spi/root/RootInvokeHook.java | 4 +++- .../shardingsphere/core/spi/root/SPIRootInvokeHook.java | 4 ++-- .../core/jdbc/adapter/AbstractConnectionAdapter.java | 3 ++- .../handler/root/OpenTracingRootInvokeHandler.java | 4 ++-- .../handler/root/OpenTracingRootInvokeHandlerTest.java | 2 +- .../OpenTracingGetConnectionEventHandlerTest.java | 4 ++-- .../OpenTracingSQLExecutionEventHandlerTest.java | 4 ++-- .../proxy/backend/jdbc/connection/BackendConnection.java | 9 +++++++++ .../proxy/frontend/mysql/MySQLFrontendHandler.java | 5 ++++- 9 files changed, 27 insertions(+), 12 deletions(-) diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHook.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHook.java index c4afa755854a4..6a1da1eacc676 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHook.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/root/RootInvokeHook.java @@ -31,6 +31,8 @@ public interface RootInvokeHook { /** * Handle when root invoke finished success. + * + * @param connectionCount connection count */ - void finishSuccess(); + void finishSuccess(int connectionCount); } diff --git a/sharding-core/src/main/java/io/shardingsphere/core/spi/root/SPIRootInvokeHook.java b/sharding-core/src/main/java/io/shardingsphere/core/spi/root/SPIRootInvokeHook.java index 2175f30bbd1b7..da273a83d9707 100644 --- a/sharding-core/src/main/java/io/shardingsphere/core/spi/root/SPIRootInvokeHook.java +++ b/sharding-core/src/main/java/io/shardingsphere/core/spi/root/SPIRootInvokeHook.java @@ -36,9 +36,9 @@ public void start() { } @Override - public void finishSuccess() { + public void finishSuccess(final int connectionCount) { for (RootInvokeHook each : SERVICE_LOADER) { - each.finishSuccess(); + each.finishSuccess(connectionCount); } } } 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 4a7219278dda8..c737370def8ef 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 @@ -221,6 +221,7 @@ public final void close() throws SQLException { HintManagerHolder.clear(); MasterVisitedManager.clear(); TransactionTypeHolder.clear(); + int connectionSize = cachedConnections.size(); forceExecuteTemplateForClose.execute(cachedConnections.entries(), new ForceExecuteCallback>() { @Override @@ -238,7 +239,7 @@ public void execute(final Entry cachedConnections) throws SQ } } }); - rootInvokeHook.finishSuccess(); + rootInvokeHook.finishSuccess(connectionSize); } @Override 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 d7386921f7d28..ea7d7eebc2a80 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 @@ -44,8 +44,8 @@ public void start() { } @Override - public void finishSuccess() { - ACTIVE_SPAN.get().deactivate(); + public void finishSuccess(final int connectionCount) { + ACTIVE_SPAN.get().setTag(ShardingTags.CONNECTION_COUNT.getKey(), connectionCount).deactivate(); ACTIVE_SPAN.remove(); } 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 b3314c16647fb..88c51f4b66622 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 @@ -38,7 +38,7 @@ public void assertRootInvoke() { assertTrue(OpenTracingRootInvokeHandler.isTrunkThread()); assertNotNull(OpenTracingRootInvokeHandler.getActiveSpan().get()); assertTrue(ExecutorDataMap.getDataMap().containsKey(OpenTracingRootInvokeHandler.ROOT_SPAN_CONTINUATION)); - rootInvokeHook.finishSuccess(); + rootInvokeHook.finishSuccess(1); assertFalse(OpenTracingRootInvokeHandler.isTrunkThread()); assertNull(OpenTracingRootInvokeHandler.getActiveSpan().get()); } 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 index 49757df7d09b0..d9fa2e8565196 100644 --- 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 @@ -63,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().finishSuccess(); + new OpenTracingRootInvokeHandler().finishSuccess(2); } @Test @@ -104,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().finishSuccess(); + new OpenTracingRootInvokeHandler().finishSuccess(2); } } diff --git a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/executor/OpenTracingSQLExecutionEventHandlerTest.java b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/executor/OpenTracingSQLExecutionEventHandlerTest.java index 8968e88cf0ee4..5c8ca15cfc5e4 100644 --- a/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/executor/OpenTracingSQLExecutionEventHandlerTest.java +++ b/sharding-opentracing/src/test/java/io/shardingsphere/opentracing/handler/tracing/executor/OpenTracingSQLExecutionEventHandlerTest.java @@ -71,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().finishSuccess(); + new OpenTracingRootInvokeHandler().finishSuccess(2); } @Test @@ -125,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().finishSuccess(); + new OpenTracingRootInvokeHandler().finishSuccess(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 5c4a161f87fc8..82a0afd88a0f1 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 @@ -58,6 +58,15 @@ public final class BackendConnection implements AutoCloseable { private final CloseConnectionHook closeConnectionHook = new SPICloseConnectionHook(); + /** + * Get connection size. + * + * @return connection size + */ + public int getConnectionSize() { + return cachedConnections.size(); + } + /** * Get connections of current thread datasource. * 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 6ddf719de9ad7..9b148f6ba2146 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 @@ -117,6 +117,7 @@ class CommandExecutor implements Runnable { @Override public void run() { rootInvokeHook.start(); + int connectionSize = 0; try (MySQLPacketPayload payload = new MySQLPacketPayload(message); BackendConnection backendConnection = new BackendConnection()) { setBackendConnection(backendConnection); @@ -131,14 +132,16 @@ public void run() { if (commandPacket instanceof QueryCommandPacket && !(responsePackets.get().getHeadPacket() instanceof OKPacket) && !(responsePackets.get().getHeadPacket() instanceof ErrPacket)) { writeMoreResults((QueryCommandPacket) commandPacket, responsePackets.get().getPackets().size()); } + connectionSize = backendConnection.getConnectionSize(); } catch (final SQLException ex) { context.writeAndFlush(new ErrPacket(++currentSequenceId, ex)); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON context.writeAndFlush(new ErrPacket(1, ServerErrorCode.ER_STD_UNKNOWN_EXCEPTION, ex.getMessage())); + } finally { + rootInvokeHook.finishSuccess(connectionSize); } - rootInvokeHook.finishSuccess(); } private CommandPacket getCommandPacket(final MySQLPacketPayload payload, final BackendConnection backendConnection, final FrontendHandler frontendHandler) throws SQLException {