Skip to content

Commit

Permalink
Support multi storage datatype.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhucong committed Aug 16, 2024
1 parent 23e7fff commit 8394494
Show file tree
Hide file tree
Showing 38 changed files with 278 additions and 406 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,25 @@ public ITContainers(final String scenario) {
* @return registered container
*/
public <T extends ITContainer> T registerContainer(final T container) {
return registerContainer(container, getNetworkAlias(container));
}

/**
* Register container.
*
* @param container container to be registered
* @param networkAlias network alias
* @param <T> type of container
* @return registered container
*/
public <T extends ITContainer> T registerContainer(final T container, final String networkAlias) {
if (container instanceof ComboITContainer) {
((ComboITContainer) container).getContainers().forEach(this::registerContainer);
} else if (container instanceof EmbeddedITContainer) {
embeddedContainers.add((EmbeddedITContainer) container);
} else {
DockerITContainer dockerContainer = (DockerITContainer) container;
dockerContainer.setNetwork(network);
String networkAlias = getNetworkAlias(container);
dockerContainer.setNetworkAliases(Collections.singletonList(networkAlias));
String loggerName = String.join(":", scenario, dockerContainer.getName());
dockerContainer.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(loggerName), false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ public abstract class DockerStorageContainer extends DockerITContainer implement

private final DatabaseType databaseType;

private final Map<String, DataSource> actualDataSourceMap = new LinkedHashMap<>();
private final Collection<String> databases;

private final Map<String, DataSource> expectedDataSourceMap = new LinkedHashMap<>();
private final Map<String, DataSource> dataSourceMap = new LinkedHashMap<>();

protected DockerStorageContainer(final DatabaseType databaseType, final String containerImage) {
protected DockerStorageContainer(final DatabaseType databaseType, final String containerImage, final Collection<String> databases) {
super(databaseType.getType().toLowerCase(), containerImage);
this.databaseType = databaseType;
this.databases = databases;
}

@Override
Expand Down Expand Up @@ -85,14 +86,9 @@ protected final void mapResources(final Map<String, String> resources) {

@Override
protected void postStart() {
actualDataSourceMap.putAll(createAccessDataSource(getDatabaseNames()));
expectedDataSourceMap.putAll(createAccessDataSource(getExpectedDatabaseNames()));
dataSourceMap.putAll(createAccessDataSource(databases));
}

protected abstract Collection<String> getDatabaseNames();

protected abstract Collection<String> getExpectedDatabaseNames();

/**
* Create access data source.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
import org.apache.shardingsphere.test.e2e.env.container.atomic.EmbeddedITContainer;
import org.apache.shardingsphere.test.e2e.env.container.atomic.util.StorageContainerUtils;
import org.apache.shardingsphere.test.e2e.env.runtime.DataSourceEnvironment;
import org.apache.shardingsphere.test.e2e.env.runtime.scenario.database.DatabaseEnvironmentManager;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

/**
* Embedded storage container.
Expand All @@ -41,31 +38,20 @@ public abstract class EmbeddedStorageContainer implements EmbeddedITContainer, S

private final String scenario;

private final Map<String, DataSource> actualDataSourceMap;
private final Collection<String> databases;

private final Map<String, DataSource> expectedDataSourceMap;
private final Map<String, DataSource> dataSourceMap;

protected EmbeddedStorageContainer(final DatabaseType databaseType, final String scenario) {
protected EmbeddedStorageContainer(final DatabaseType databaseType, final String scenario, final Collection<String> databases) {
this.databaseType = databaseType;
this.scenario = scenario;
actualDataSourceMap = createActualDataSourceMap();
expectedDataSourceMap = createExpectedDataSourceMap();
this.databases = databases;
dataSourceMap = createDataSourceMap();
}

private Map<String, DataSource> createActualDataSourceMap() {
Collection<String> databaseNames = DatabaseEnvironmentManager.getDatabaseTypes(scenario, databaseType).entrySet().stream()
.filter(entry -> entry.getValue().getClass().isAssignableFrom(databaseType.getClass())).map(Entry::getKey).collect(Collectors.toList());
Map<String, DataSource> result = new LinkedHashMap<>(databaseNames.size(), 1F);
databaseNames.forEach(each -> result.put(each, StorageContainerUtils.generateDataSource(DataSourceEnvironment.getURL(databaseType, null, 0, scenario + each),
"root", "Root@123")));
return result;
}

private Map<String, DataSource> createExpectedDataSourceMap() {
Collection<String> databaseNames = DatabaseEnvironmentManager.getExpectedDatabaseTypes(scenario, databaseType).entrySet().stream()
.filter(entry -> entry.getValue().getClass().isAssignableFrom(databaseType.getClass())).map(Entry::getKey).collect(Collectors.toList());
Map<String, DataSource> result = new LinkedHashMap<>(databaseNames.size(), 1F);
databaseNames.forEach(each -> result.put(each, StorageContainerUtils.generateDataSource(DataSourceEnvironment.getURL(databaseType, null, 0, scenario + each),
private Map<String, DataSource> createDataSourceMap() {
Map<String, DataSource> result = new LinkedHashMap<>(databases.size(), 1F);
databases.forEach(each -> result.put(each, StorageContainerUtils.generateDataSource(DataSourceEnvironment.getURL(databaseType, null, 0, scenario + each),
"root", "Root@123")));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,5 @@ public interface StorageContainer extends ITContainer {
*
* @return actual data source map
*/
Map<String, DataSource> getActualDataSourceMap();

/**
* Get expected data source map.
*
* @return expected data source map
*/
Map<String, DataSource> getExpectedDataSourceMap();
Map<String, DataSource> getDataSourceMap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.impl.OpenGaussContainer;
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.impl.PostgreSQLContainer;

import java.util.Collection;
import java.util.Collections;

/**
* Storage container factory.
*/
Expand All @@ -42,19 +45,33 @@ public final class StorageContainerFactory {
* @return created instance
* @throws RuntimeException runtime exception
*/
public static StorageContainer newInstance(final DatabaseType databaseType, final String storageContainerImage, final StorageContainerConfiguration storageContainerConfig) {
return newInstance(databaseType, storageContainerImage, storageContainerConfig, Collections.emptyList());
}

/**
* Create new instance of storage container.
*
* @param databaseType database type
* @param storageContainerImage storage container image
* @param storageContainerConfig storage container configuration
* @param databases databases
* @return created instance
* @throws RuntimeException runtime exception
*/
public static StorageContainer newInstance(final DatabaseType databaseType, final String storageContainerImage,
final StorageContainerConfiguration storageContainerConfig) {
final StorageContainerConfiguration storageContainerConfig, final Collection<String> databases) {
switch (databaseType.getType()) {
case "MySQL":
return new MySQLContainer(storageContainerImage, storageContainerConfig);
return new MySQLContainer(storageContainerImage, storageContainerConfig, databases);
case "PostgreSQL":
return new PostgreSQLContainer(storageContainerImage, storageContainerConfig);
return new PostgreSQLContainer(storageContainerImage, storageContainerConfig, databases);
case "openGauss":
return new OpenGaussContainer(storageContainerImage, storageContainerConfig);
return new OpenGaussContainer(storageContainerImage, storageContainerConfig, databases);
case "H2":
return new H2Container(storageContainerConfig);
return new H2Container(storageContainerConfig, databases);
case "MariaDB":
return new MariaDBContainer(storageContainerImage, storageContainerConfig);
return new MariaDBContainer(storageContainerImage, storageContainerConfig, databases);
default:
throw new RuntimeException(String.format("Database `%s` is unknown.", databaseType.getType()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;

import java.util.Map;

Expand All @@ -38,14 +37,7 @@ public class StorageContainerConfiguration {

private final Map<String, String> mountedResources;

private final Map<String, DatabaseType> databaseTypes;

private final Map<String, DatabaseType> expectedDatabaseTypes;

public StorageContainerConfiguration(final String containerCommand, final Map<String, String> containerEnvironments, final Map<String, String> mountedResources,
final Map<String, DatabaseType> databaseTypes, final Map<String, DatabaseType> expectedDatabaseTypes) {
this.databaseTypes = databaseTypes;
this.expectedDatabaseTypes = expectedDatabaseTypes;
public StorageContainerConfiguration(final String containerCommand, final Map<String, String> containerEnvironments, final Map<String, String> mountedResources) {
scenario = null;
this.containerCommand = containerCommand;
this.containerEnvironments = containerEnvironments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.impl.mysql.MySQLContainerConfigurationFactory;
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.impl.opengauss.OpenGaussContainerConfigurationFactory;
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.impl.postgresql.PostgreSQLContainerConfigurationFactory;
import org.apache.shardingsphere.test.e2e.env.runtime.scenario.path.ScenarioDataPath.Type;

/**
* Storage container configuration factory.
Expand All @@ -38,19 +39,20 @@ public final class StorageContainerConfigurationFactory {
*
* @param databaseType database type
* @param scenario scenario
* @param type type
* @return created instance
* @throws RuntimeException runtime exception
*/
public static StorageContainerConfiguration newInstance(final DatabaseType databaseType, final String scenario) {
public static StorageContainerConfiguration newInstance(final DatabaseType databaseType, final String scenario, final Type type) {
switch (databaseType.getType()) {
case "MySQL":
return MySQLContainerConfigurationFactory.newInstance(scenario);
return MySQLContainerConfigurationFactory.newInstance(scenario, type);
case "PostgreSQL":
return PostgreSQLContainerConfigurationFactory.newInstance(scenario);
return PostgreSQLContainerConfigurationFactory.newInstance(scenario, type);
case "openGauss":
return OpenGaussContainerConfigurationFactory.newInstance(scenario);
return OpenGaussContainerConfigurationFactory.newInstance(scenario, type);
case "H2":
return H2ContainerConfigurationFactory.newInstance(scenario);
return H2ContainerConfigurationFactory.newInstance(scenario, type);
default:
throw new RuntimeException(String.format("Database `%s` is unknown.", databaseType.getType()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.config.StorageContainerConfiguration;
import org.apache.shardingsphere.test.e2e.env.runtime.scenario.database.DatabaseEnvironmentManager;
import org.apache.shardingsphere.test.e2e.env.runtime.scenario.path.ScenarioDataPath;
import org.apache.shardingsphere.test.e2e.env.runtime.scenario.path.ScenarioDataPath.Type;

Expand All @@ -44,23 +43,25 @@ public final class H2ContainerConfigurationFactory {
public static StorageContainerConfiguration newInstance() {
Map<String, String> mountedResources = new HashMap<>(1, 1F);
mountedResources.put("/env/mysql/01-initdb.sql", "/docker-entrypoint-initdb.d/01-initdb.sql");
return new StorageContainerConfiguration("", Collections.emptyMap(), mountedResources, Collections.emptyMap(), Collections.emptyMap());
return new StorageContainerConfiguration("", Collections.emptyMap(), mountedResources);
}

/**
* Create new instance of h2 container configuration.
*
* @param scenario scenario
* @param type type
* @return created instance
*/
public static StorageContainerConfiguration newInstance(final String scenario) {
Map<String, String> mountedResources = new HashMap<>(2, 1F);
mountedResources.put(new ScenarioDataPath(scenario).getInitSQLResourcePath(Type.ACTUAL, TypedSPILoader.getService(DatabaseType.class, "H2")) + "/01-actual-init.sql",
"/docker-entrypoint-initdb.d/01-actual-init.sql");
mountedResources.put(new ScenarioDataPath(scenario).getInitSQLResourcePath(Type.EXPECTED, TypedSPILoader.getService(DatabaseType.class, "H2")) + "/01-expected-init.sql",
"/docker-entrypoint-initdb.d/01-expected-init.sql");
return new StorageContainerConfiguration(scenario, "", Collections.emptyMap(), mountedResources,
DatabaseEnvironmentManager.getDatabaseTypes(scenario, TypedSPILoader.getService(DatabaseType.class, "H2")),
DatabaseEnvironmentManager.getExpectedDatabaseTypes(scenario, TypedSPILoader.getService(DatabaseType.class, "H2")));
public static StorageContainerConfiguration newInstance(final String scenario, final Type type) {
Map<String, String> mountedResources = new HashMap<>(1, 1F);
if (Type.ACTUAL == type) {
mountedResources.put(new ScenarioDataPath(scenario).getInitSQLResourcePath(Type.ACTUAL, TypedSPILoader.getService(DatabaseType.class, "H2")) + "/01-actual-init.sql",
"/docker-entrypoint-initdb.d/01-actual-init.sql");
} else {
mountedResources.put(new ScenarioDataPath(scenario).getInitSQLResourcePath(Type.EXPECTED, TypedSPILoader.getService(DatabaseType.class, "H2")) + "/01-expected-init.sql",
"/docker-entrypoint-initdb.d/01-expected-init.sql");
}
return new StorageContainerConfiguration(scenario, "", Collections.emptyMap(), mountedResources);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.shardingsphere.test.e2e.env.container.atomic.storage.impl.MariaDBContainer;
import org.apache.shardingsphere.test.e2e.env.container.atomic.util.ContainerUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -39,7 +38,7 @@ public final class MariaDBContainerConfigurationFactory {
* @return created instance
*/
public static StorageContainerConfiguration newInstance() {
return new StorageContainerConfiguration(getCommand(), getContainerEnvironments(), getMountedResources(), Collections.emptyMap(), Collections.emptyMap());
return new StorageContainerConfiguration(getCommand(), getContainerEnvironments(), getMountedResources());
}

private static String getCommand() {
Expand Down
Loading

0 comments on commit 8394494

Please sign in to comment.