This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 676bb5f6b31 Add proxy_random to it.cluster.adapters (#32371)
676bb5f6b31 is described below
commit 676bb5f6b31984b2c24b424eb8b1a485cd793ead
Author: Cong Hu <[email protected]>
AuthorDate: Mon Aug 5 19:34:02 2024 +0800
Add proxy_random to it.cluster.adapters (#32371)
---
.../AdapterType.java => ComboITContainer.java} | 22 ++--
.../e2e/env/container/atomic/ITContainers.java | 4 +-
.../atomic/adapter/AdapterContainerFactory.java | 3 +
.../ShardingSphereMultiProxyClusterContainer.java | 144 +++++++++++++++++++++
.../impl/ShardingSphereProxyClusterContainer.java | 6 +-
.../env/container/atomic/enums/AdapterType.java | 4 +-
.../compose/ContainerComposerRegistry.java | 2 +-
.../e2e/engine/TotalSuitesCountCalculator.java | 5 +-
.../sql/src/test/resources/env/it-env.properties | 2 +-
9 files changed, 173 insertions(+), 19 deletions(-)
diff --git
a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/enums/AdapterType.java
b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/ComboITContainer.java
similarity index 73%
copy from
test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/enums/AdapterType.java
copy to
test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/ComboITContainer.java
index 2c5ca7552f0..1ca5ed6be98 100644
---
a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/enums/AdapterType.java
+++
b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/ComboITContainer.java
@@ -15,21 +15,19 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.test.e2e.env.container.atomic.enums;
+package org.apache.shardingsphere.test.e2e.env.container.atomic;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+import java.util.Collection;
/**
- * Adapter type.
+ * Combo IT container.
*/
-@RequiredArgsConstructor
-@Getter
-public enum AdapterType {
+public interface ComboITContainer extends ITContainer {
- JDBC("jdbc"),
-
- PROXY("proxy");
-
- private final String value;
+ /**
+ * Get containers.
+ *
+ * @return containers
+ */
+ Collection<ITContainer> getContainers();
}
diff --git
a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/ITContainers.java
b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/ITContainers.java
index 383a637e92c..ceafa7ee091 100644
---
a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/ITContainers.java
+++
b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/ITContainers.java
@@ -57,7 +57,9 @@ public final class ITContainers implements Startable {
* @return registered container
*/
public <T extends ITContainer> T registerContainer(final T container) {
- if (container instanceof EmbeddedITContainer) {
+ if (container instanceof ComboITContainer) {
+ ((ComboITContainer)
container).getContainers().forEach(this::registerContainer);
+ } else if (container instanceof EmbeddedITContainer) {
embeddedContainers.add((EmbeddedITContainer) container);
} else {
DockerITContainer dockerContainer = (DockerITContainer) container;
diff --git
a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/AdapterContainerFactory.java
b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/AdapterContainerFactory.java
index 396c670f5ca..091ce133b7a 100644
---
a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/AdapterContainerFactory.java
+++
b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/AdapterContainerFactory.java
@@ -22,6 +22,7 @@ import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import
org.apache.shardingsphere.test.e2e.env.container.atomic.adapter.config.AdaptorContainerConfiguration;
import
org.apache.shardingsphere.test.e2e.env.container.atomic.adapter.impl.ShardingSphereJdbcContainer;
+import
org.apache.shardingsphere.test.e2e.env.container.atomic.adapter.impl.ShardingSphereMultiProxyClusterContainer;
import
org.apache.shardingsphere.test.e2e.env.container.atomic.adapter.impl.ShardingSphereProxyClusterContainer;
import
org.apache.shardingsphere.test.e2e.env.container.atomic.adapter.impl.ShardingSphereProxyStandaloneContainer;
import
org.apache.shardingsphere.test.e2e.env.container.atomic.enums.AdapterMode;
@@ -53,6 +54,8 @@ public final class AdapterContainerFactory {
return AdapterMode.CLUSTER == mode
? new
ShardingSphereProxyClusterContainer(databaseType, containerConfig)
: new
ShardingSphereProxyStandaloneContainer(databaseType, containerConfig);
+ case PROXY_RANDOM:
+ return new
ShardingSphereMultiProxyClusterContainer(databaseType, containerConfig);
case JDBC:
return new ShardingSphereJdbcContainer(storageContainer,
scenario, databaseType);
default:
diff --git
a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereMultiProxyClusterContainer.java
b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereMultiProxyClusterContainer.java
new file mode 100644
index 00000000000..3bd4be35ebe
--- /dev/null
+++
b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereMultiProxyClusterContainer.java
@@ -0,0 +1,144 @@
+/*
+ * 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.test.e2e.env.container.atomic.adapter.impl;
+
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import
org.apache.shardingsphere.test.e2e.env.container.atomic.ComboITContainer;
+import org.apache.shardingsphere.test.e2e.env.container.atomic.ITContainer;
+import
org.apache.shardingsphere.test.e2e.env.container.atomic.adapter.AdapterContainer;
+import
org.apache.shardingsphere.test.e2e.env.container.atomic.adapter.config.AdaptorContainerConfiguration;
+import
org.apache.shardingsphere.test.e2e.env.container.atomic.constants.ProxyContainerConstants;
+import org.testcontainers.lifecycle.Startable;
+
+import javax.sql.DataSource;
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Set;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+
+/**
+ * ShardingSphere proxy container for cluster mode.
+ */
+public final class ShardingSphereMultiProxyClusterContainer implements
AdapterContainer, ComboITContainer {
+
+ private final AtomicReference<DataSource> targetDataSourceProvider = new
AtomicReference<>();
+
+ private final Collection<ShardingSphereProxyClusterContainer>
proxyClusterContainers = new LinkedList<>();
+
+ public ShardingSphereMultiProxyClusterContainer(final DatabaseType
databaseType, final AdaptorContainerConfiguration config) {
+ ShardingSphereProxyClusterContainer proxy1 = new
ShardingSphereProxyClusterContainer(databaseType, config);
+ proxy1.setAbbreviation("proxy1");
+ proxy1.setName("proxy1");
+ proxyClusterContainers.add(proxy1);
+ ShardingSphereProxyClusterContainer proxy2 = new
ShardingSphereProxyClusterContainer(databaseType, config);
+ proxy1.setAbbreviation("proxy2");
+ proxy1.setName("proxy2");
+ proxyClusterContainers.add(proxy2);
+ }
+
+ @Override
+ public DataSource getTargetDataSource(final String serverLists) {
+ DataSource dataSource = targetDataSourceProvider.get();
+ if (null == dataSource) {
+ targetDataSourceProvider.set(new
RandomDataSourceAdapter(proxyClusterContainers.stream().map(each ->
each.getTargetDataSource(serverLists)).collect(Collectors.toSet())));
+ }
+ return targetDataSourceProvider.get();
+ }
+
+ @Override
+ public String getAbbreviation() {
+ return ProxyContainerConstants.PROXY_CONTAINER_ABBREVIATION;
+ }
+
+ @Override
+ public void start() {
+ proxyClusterContainers.forEach(Startable::start);
+ }
+
+ @Override
+ public Collection<ITContainer> getContainers() {
+ return proxyClusterContainers.stream().map(each -> (ITContainer)
each).collect(Collectors.toList());
+ }
+
+ private static class RandomDataSourceAdapter implements DataSource {
+
+ private final DataSource[] dataSources;
+
+ RandomDataSourceAdapter(final Set<DataSource> dataSources) {
+ this.dataSources = dataSources.toArray(new DataSource[0]);
+ }
+
+ private DataSource getDataSource() {
+ return
dataSources[ThreadLocalRandom.current().nextInt(dataSources.length)];
+ }
+
+ @Override
+ public Connection getConnection() throws SQLException {
+ return getDataSource().getConnection();
+ }
+
+ @Override
+ public Connection getConnection(final String username, final String
password) throws SQLException {
+ return getDataSource().getConnection(username, password);
+ }
+
+ @Override
+ public PrintWriter getLogWriter() throws SQLException {
+ return getDataSource().getLogWriter();
+ }
+
+ @Override
+ public void setLogWriter(final PrintWriter out) throws SQLException {
+ getDataSource().setLogWriter(out);
+ }
+
+ @Override
+ public void setLoginTimeout(final int seconds) throws SQLException {
+ for (DataSource each : dataSources) {
+ each.setLoginTimeout(seconds);
+ }
+ }
+
+ @Override
+ public int getLoginTimeout() throws SQLException {
+ return getDataSource().getLoginTimeout();
+ }
+
+ @Override
+ public Logger getParentLogger() throws SQLFeatureNotSupportedException
{
+ return getDataSource().getParentLogger();
+ }
+
+ @Override
+ public <T> T unwrap(final Class<T> iface) throws SQLException {
+ return getDataSource().unwrap(iface);
+ }
+
+ @Override
+ public boolean isWrapperFor(final Class<?> iface) throws SQLException {
+ return getDataSource().isWrapperFor(iface);
+ }
+ }
+}
diff --git
a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereProxyClusterContainer.java
b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereProxyClusterContainer.java
index 3a131743f8c..9e3ce76fa34 100644
---
a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereProxyClusterContainer.java
+++
b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/adapter/impl/ShardingSphereProxyClusterContainer.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.test.e2e.env.container.atomic.adapter.impl;
import com.google.common.base.Strings;
+import lombok.Setter;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import
org.apache.shardingsphere.test.e2e.env.container.atomic.DockerITContainer;
import
org.apache.shardingsphere.test.e2e.env.container.atomic.adapter.AdapterContainer;
@@ -45,6 +46,9 @@ public final class ShardingSphereProxyClusterContainer
extends DockerITContainer
private final AtomicReference<DataSource> targetDataSourceProvider = new
AtomicReference<>();
+ @Setter
+ private String abbreviation =
ProxyContainerConstants.PROXY_CONTAINER_ABBREVIATION;
+
public ShardingSphereProxyClusterContainer(final DatabaseType
databaseType, final AdaptorContainerConfiguration config) {
super(ProxyContainerConstants.PROXY_CONTAINER_NAME_PREFIX,
config.getAdapterContainerImage());
this.databaseType = databaseType;
@@ -94,6 +98,6 @@ public final class ShardingSphereProxyClusterContainer
extends DockerITContainer
@Override
public String getAbbreviation() {
- return ProxyContainerConstants.PROXY_CONTAINER_ABBREVIATION;
+ return abbreviation;
}
}
diff --git
a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/enums/AdapterType.java
b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/enums/AdapterType.java
index 2c5ca7552f0..81c3a789b5d 100644
---
a/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/enums/AdapterType.java
+++
b/test/e2e/env/src/test/java/org/apache/shardingsphere/test/e2e/env/container/atomic/enums/AdapterType.java
@@ -29,7 +29,9 @@ public enum AdapterType {
JDBC("jdbc"),
- PROXY("proxy");
+ PROXY("proxy"),
+
+ PROXY_RANDOM("proxy_random");
private final String value;
}
diff --git
a/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/container/compose/ContainerComposerRegistry.java
b/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/container/compose/ContainerComposerRegistry.java
index 0ca340fbaa3..fa9098e5c4e 100644
---
a/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/container/compose/ContainerComposerRegistry.java
+++
b/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/container/compose/ContainerComposerRegistry.java
@@ -64,7 +64,7 @@ public final class ContainerComposerRegistry implements
AutoCloseable {
private boolean isClusterMode(final AdapterMode adapterMode, final
AdapterType adapterType) {
// TODO cluster mode often throw exception sometimes, issue is #15517
- return AdapterMode.CLUSTER == adapterMode && AdapterType.PROXY ==
adapterType;
+ return AdapterMode.CLUSTER == adapterMode && AdapterType.PROXY ==
adapterType || AdapterType.PROXY_RANDOM == adapterType;
}
private ContainerComposer createContainerComposer(final boolean
clusterMode, final String scenario, final DatabaseType databaseType, final
AdapterMode adapterMode, final AdapterType adapterType) {
diff --git
a/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/TotalSuitesCountCalculator.java
b/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/TotalSuitesCountCalculator.java
index bea93d4d67e..d625f010727 100644
---
a/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/TotalSuitesCountCalculator.java
+++
b/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/TotalSuitesCountCalculator.java
@@ -19,8 +19,8 @@ package org.apache.shardingsphere.test.e2e.engine;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import
org.apache.shardingsphere.test.e2e.env.container.atomic.enums.AdapterType;
import
org.apache.shardingsphere.test.e2e.env.container.atomic.enums.AdapterMode;
+import
org.apache.shardingsphere.test.e2e.env.container.atomic.enums.AdapterType;
import org.apache.shardingsphere.test.e2e.env.runtime.E2ETestEnvironment;
/**
@@ -66,6 +66,7 @@ public final class TotalSuitesCountCalculator {
}
private static boolean isRunProxy() {
- return ENV.getRunModes().contains(AdapterMode.CLUSTER.getValue()) &&
ENV.getClusterEnvironment().getAdapters().contains(AdapterType.PROXY.getValue());
+ return ENV.getRunModes().contains(AdapterMode.CLUSTER.getValue()) &&
ENV.getClusterEnvironment().getAdapters().contains(AdapterType.PROXY.getValue())
|| ENV.getClusterEnvironment()
+ .getAdapters().contains(AdapterType.PROXY_RANDOM.getValue());
}
}
diff --git a/test/e2e/sql/src/test/resources/env/it-env.properties
b/test/e2e/sql/src/test/resources/env/it-env.properties
index aa8f6118546..eb04eb6efe0 100644
--- a/test/e2e/sql/src/test/resources/env/it-env.properties
+++ b/test/e2e/sql/src/test/resources/env/it-env.properties
@@ -26,7 +26,7 @@
it.scenarios=db,tbl,readwrite_splitting,encrypt,shadow,dbtbl_with_readwrite_spli
# it.cluster.env.type=DOCKER,NATIVE
it.cluster.env.type=DOCKER
-# it.cluster.adapters=jdbc,proxy
+# it.cluster.adapters=jdbc,proxy,proxy_random
it.cluster.adapters=proxy
# it.cluster.databases=MySQL,PostgreSQL,openGauss