This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new d9727920df3 Use exclusive thread in MySQL Proxy if necessary (#18756)
d9727920df3 is described below
commit d9727920df30dcad50cce32cedcde35f09dae4eb
Author: 吴伟杰 <[email protected]>
AuthorDate: Fri Jul 1 15:33:46 2022 +0800
Use exclusive thread in MySQL Proxy if necessary (#18756)
* Use exclusive thread in MySQL Proxy if necessary
* Remove reference count introduced in #16429
* Fix checkstyle in MySQLFrontendContextTest
---
.../jdbc/connection/JDBCBackendConnection.java | 12 +----
.../frontend/state/impl/JDBCOKProxyState.java | 6 +--
.../frontend/state/impl/JDBCOKProxyStateTest.java | 2 +-
.../proxy/frontend/mysql/MySQLFrontendContext.java | 42 +++++++++++++++++
.../proxy/frontend/mysql/MySQLFrontendEngine.java | 2 +-
.../frontend/mysql/MySQLFrontendContextTest.java | 52 ++++++++++++++++++++++
.../postgresql/PostgreSQLFrontendContext.java} | 16 +++----
.../postgresql/PostgreSQLFrontendEngine.java | 2 +-
.../postgresql/PostgreSQLFrontendContextTest.java} | 19 ++++----
.../proxy/frontend/context/FrontendContext.java | 15 ++++---
10 files changed, 127 insertions(+), 41 deletions(-)
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java
index 8c0c6d076cc..241a3826500 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/JDBCBackendConnection.java
@@ -20,7 +20,6 @@ package
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection;
import com.google.common.base.Preconditions;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
-import java.util.concurrent.atomic.AtomicBoolean;
import lombok.Getter;
import lombok.Setter;
import
org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
@@ -43,6 +42,7 @@ import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
/**
* JDBC backend connection.
@@ -67,8 +67,6 @@ public final class JDBCBackendConnection implements
BackendConnection<Void>, Exe
private final AtomicBoolean closed;
- private volatile int connectionReferenceCount;
-
public JDBCBackendConnection(final ConnectionSession connectionSession) {
this.connectionSession = connectionSession;
closed = new AtomicBoolean(false);
@@ -180,10 +178,7 @@ public final class JDBCBackendConnection implements
BackendConnection<Void>, Exe
@Override
public Void prepareForTaskExecution() {
- synchronized (this) {
- connectionReferenceCount++;
- return null;
- }
+ return null;
}
@Override
@@ -198,9 +193,6 @@ public final class JDBCBackendConnection implements
BackendConnection<Void>, Exe
@Override
public Void closeExecutionResources() throws BackendConnectionException {
synchronized (this) {
- if (connectionReferenceCount > 0 && connectionReferenceCount-- >
1) {
- return null;
- }
Collection<Exception> result = new LinkedList<>();
result.addAll(closeDatabaseCommunicationEngines(false));
result.addAll(closeFederationExecutor());
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/impl/JDBCOKProxyState.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/impl/JDBCOKProxyState.java
index 584f6b4174c..0ef4ea6977c 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/impl/JDBCOKProxyState.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/state/impl/JDBCOKProxyState.java
@@ -37,17 +37,17 @@ public final class JDBCOKProxyState implements OKProxyState
{
@Override
public void execute(final ChannelHandlerContext context, final Object
message, final DatabaseProtocolFrontendEngine databaseProtocolFrontendEngine,
final ConnectionSession connectionSession) {
CommandExecutorTask commandExecutorTask = new
CommandExecutorTask(databaseProtocolFrontendEngine, connectionSession, context,
message);
- ExecutorService executorService =
determineSuitableExecutorService(context, databaseProtocolFrontendEngine,
connectionSession);
+ ExecutorService executorService =
determineSuitableExecutorService(context, message,
databaseProtocolFrontendEngine, connectionSession);
executorService.execute(commandExecutorTask);
}
- private ExecutorService determineSuitableExecutorService(final
ChannelHandlerContext context, final DatabaseProtocolFrontendEngine
databaseProtocolFrontendEngine,
+ private ExecutorService determineSuitableExecutorService(final
ChannelHandlerContext context, final Object message, final
DatabaseProtocolFrontendEngine databaseProtocolFrontendEngine,
final
ConnectionSession connectionSession) {
if (requireOccupyThreadForConnection(connectionSession)) {
return
ConnectionThreadExecutorGroup.getInstance().get(connectionSession.getConnectionId());
} else if (isPreferNettyEventLoop()) {
return context.executor();
- } else if
(databaseProtocolFrontendEngine.getFrontendContext().isRequiredSameThreadForConnection())
{
+ } else if
(databaseProtocolFrontendEngine.getFrontendContext().isRequiredSameThreadForConnection(message))
{
return
ConnectionThreadExecutorGroup.getInstance().get(connectionSession.getConnectionId());
}
return UserExecutorGroup.getInstance().getExecutorService();
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/JDBCOKProxyStateTest.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/JDBCOKProxyStateTest.java
index 38a16acef20..fe9f7bb9070 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/JDBCOKProxyStateTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/state/impl/JDBCOKProxyStateTest.java
@@ -101,7 +101,7 @@ public final class JDBCOKProxyStateTest extends
ProxyContextRestorer {
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.PROXY_HINT_ENABLED)).thenReturn(false);
when(ProxyContext.getInstance().getContextManager()
.getMetaDataContexts().getMetaData().getProps().<String>getValue(ConfigurationPropertyKey.PROXY_BACKEND_EXECUTOR_SUITABLE)).thenReturn("OLAP");
-
when(frontendEngine.getFrontendContext().isRequiredSameThreadForConnection()).thenReturn(true);
+
when(frontendEngine.getFrontendContext().isRequiredSameThreadForConnection(null)).thenReturn(true);
ExecutorService executorService = registerMockExecutorService(1);
new JDBCOKProxyState().execute(context, null, frontendEngine,
connectionSession);
verify(executorService).execute(any(CommandExecutorTask.class));
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendContext.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendContext.java
new file mode 100644
index 00000000000..697ad39610d
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendContext.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.frontend.mysql;
+
+import io.netty.buffer.ByteBuf;
+import
org.apache.shardingsphere.db.protocol.mysql.packet.command.MySQLCommandPacketType;
+import org.apache.shardingsphere.proxy.frontend.context.FrontendContext;
+
+/**
+ * {@link FrontendContext} implementations for MySQL.
+ */
+public final class MySQLFrontendContext implements FrontendContext {
+
+ private boolean previousCommandRequiresNoServerResponse;
+
+ @Override
+ public boolean isRequiredSameThreadForConnection(final Object message) {
+ ByteBuf byteBuf = (ByteBuf) message;
+ if (byteBuf.readableBytes() < 2) {
+ return false;
+ }
+ int commandType = byteBuf.getUnsignedByte(byteBuf.readerIndex() + 1);
+ boolean result = previousCommandRequiresNoServerResponse;
+ previousCommandRequiresNoServerResponse =
MySQLCommandPacketType.COM_STMT_CLOSE.getValue() == commandType ||
MySQLCommandPacketType.COM_STMT_SEND_LONG_DATA.getValue() == commandType;
+ return previousCommandRequiresNoServerResponse || result;
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngine.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngine.java
index 92ed5880826..21b640cf207 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngine.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngine.java
@@ -39,7 +39,7 @@ import
org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngi
@Getter
public final class MySQLFrontendEngine implements
DatabaseProtocolFrontendEngine {
- private final FrontendContext frontendContext = new FrontendContext(false);
+ private final FrontendContext frontendContext = new MySQLFrontendContext();
private final AuthenticationEngine authenticationEngine = new
MySQLAuthenticationEngine();
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendContextTest.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendContextTest.java
new file mode 100644
index 00000000000..148e31321ba
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendContextTest.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.frontend.mysql;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import
org.apache.shardingsphere.db.protocol.mysql.packet.command.MySQLCommandPacketType;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public final class MySQLFrontendContextTest {
+
+ @Test
+ public void assertIsRequiredSameThreadForConnection() {
+ MySQLFrontendContext actual = new MySQLFrontendContext();
+ ByteBuf comStmtExecuteMessage = Unpooled.wrappedBuffer(new
byte[]{0x00, (byte) MySQLCommandPacketType.COM_STMT_EXECUTE.getValue()});
+ ByteBuf comStmtSendLongData = Unpooled.wrappedBuffer(new byte[]{0x00,
(byte) MySQLCommandPacketType.COM_STMT_SEND_LONG_DATA.getValue()});
+
assertFalse(actual.isRequiredSameThreadForConnection(comStmtExecuteMessage));
+
assertTrue(actual.isRequiredSameThreadForConnection(comStmtSendLongData));
+
assertTrue(actual.isRequiredSameThreadForConnection(comStmtSendLongData));
+
assertTrue(actual.isRequiredSameThreadForConnection(comStmtExecuteMessage));
+
assertFalse(actual.isRequiredSameThreadForConnection(comStmtExecuteMessage));
+ ByteBuf comStmtCloseMessage = Unpooled.wrappedBuffer(new byte[]{0x00,
(byte) MySQLCommandPacketType.COM_STMT_CLOSE.getValue()});
+
assertTrue(actual.isRequiredSameThreadForConnection(comStmtCloseMessage));
+
assertTrue(actual.isRequiredSameThreadForConnection(comStmtCloseMessage));
+
assertTrue(actual.isRequiredSameThreadForConnection(comStmtExecuteMessage));
+
assertFalse(actual.isRequiredSameThreadForConnection(comStmtExecuteMessage));
+ }
+
+ @Test
+ public void assertNoEnoughReadableBytes() {
+ MySQLFrontendContext actual = new MySQLFrontendContext();
+
assertFalse(actual.isRequiredSameThreadForConnection(Unpooled.wrappedBuffer(new
byte[1])));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/context/FrontendContext.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendContext.java
similarity index 68%
copy from
shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/context/FrontendContext.java
copy to
shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendContext.java
index 6ebdb6466f4..d79b8ce9a7b 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/context/FrontendContext.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendContext.java
@@ -15,17 +15,17 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.frontend.context;
+package org.apache.shardingsphere.proxy.frontend.postgresql;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.proxy.frontend.context.FrontendContext;
/**
- * Frontend context.
+ * {@link FrontendContext} implementations for PostgreSQL.
*/
-@RequiredArgsConstructor
-@Getter
-public final class FrontendContext {
+public final class PostgreSQLFrontendContext implements FrontendContext {
- private final boolean requiredSameThreadForConnection;
+ @Override
+ public boolean isRequiredSameThreadForConnection(final Object message) {
+ return true;
+ }
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
index a589ab1b741..2c35027b352 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngine.java
@@ -38,7 +38,7 @@ import
org.apache.shardingsphere.proxy.frontend.spi.DatabaseProtocolFrontendEngi
@Getter
public final class PostgreSQLFrontendEngine implements
DatabaseProtocolFrontendEngine {
- private final FrontendContext frontendContext = new FrontendContext(true);
+ private final FrontendContext frontendContext = new
PostgreSQLFrontendContext();
private final AuthenticationEngine authenticationEngine = new
PostgreSQLAuthenticationEngine();
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/context/FrontendContext.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendContextTest.java
similarity index 70%
copy from
shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/context/FrontendContext.java
copy to
shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendContextTest.java
index 6ebdb6466f4..4336c7bff1b 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/context/FrontendContext.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendContextTest.java
@@ -15,17 +15,16 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.proxy.frontend.context;
+package org.apache.shardingsphere.proxy.frontend.postgresql;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+import org.junit.Test;
-/**
- * Frontend context.
- */
-@RequiredArgsConstructor
-@Getter
-public final class FrontendContext {
+import static org.junit.Assert.assertTrue;
+
+public final class PostgreSQLFrontendContextTest {
- private final boolean requiredSameThreadForConnection;
+ @Test
+ public void assertIsRequiredSameThreadForConnection() {
+ assertTrue(new
PostgreSQLFrontendContext().isRequiredSameThreadForConnection(null));
+ }
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/context/FrontendContext.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/context/FrontendContext.java
index 6ebdb6466f4..c79544e4bb6 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/context/FrontendContext.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/context/FrontendContext.java
@@ -17,15 +17,16 @@
package org.apache.shardingsphere.proxy.frontend.context;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-
/**
* Frontend context.
*/
-@RequiredArgsConstructor
-@Getter
-public final class FrontendContext {
+public interface FrontendContext {
- private final boolean requiredSameThreadForConnection;
+ /**
+ * Whether Proxy should use same thread to execute tasks.
+ *
+ * @param message message
+ * @return is same thread required
+ */
+ boolean isRequiredSameThreadForConnection(Object message);
}