Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor db-discovery it use awaitility framework replace sleep #24105

Merged
merged 6 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/e2e/discovery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@

package org.apache.shardingsphere.test.e2e.discovery.build;

import org.apache.shardingsphere.data.pipeline.core.util.ThreadUtil;
import org.apache.shardingsphere.test.e2e.discovery.cases.base.BaseDiscoveryE2EIT;
import org.apache.shardingsphere.test.e2e.discovery.command.DiscoveryDistSQLCommand;
import org.awaitility.Awaitility;
import org.awaitility.Durations;

import javax.sql.DataSource;
import javax.xml.bind.JAXB;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
* Build discovery rule.
Expand Down Expand Up @@ -60,23 +61,38 @@ public void buildDiscoveryEnvironment() throws SQLException {
}

private void createDatabase(final Statement statement) throws SQLException {
statement.execute("CREATE DATABASE db_discovery");
ThreadUtil.sleep(1, TimeUnit.SECONDS);
statement.execute("USE db_discovery");
statement.execute(discoveryDistSQLCommand.getCreateDatabase().getExecuteSQL());
Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> assertResult(statement, discoveryDistSQLCommand.getCreateDatabase().getAssertionSQL()));
}

private boolean assertResult(final Statement statement, final String assertionSQL) {
try (ResultSet resultSet = statement.executeQuery(assertionSQL)) {
return true;
} catch (final SQLException ignored) {
return false;
}
}

private void registerStorageUnits(final Statement statement) throws SQLException {
statement.execute(discoveryDistSQLCommand.getRegisterStorageUnit());
ThreadUtil.sleep(2, TimeUnit.SECONDS);
statement.execute(discoveryDistSQLCommand.getRegisterStorageUnits().getExecuteSQL());
Awaitility.await().atMost(Durations.TWO_SECONDS).until(() -> assertResult0(statement, discoveryDistSQLCommand.getRegisterStorageUnits().getAssertionSQL()));
}

private void createDiscoveryRule(final Statement statement) throws SQLException {
statement.execute(discoveryDistSQLCommand.getCreateDiscoveryRule());
ThreadUtil.sleep(2, TimeUnit.SECONDS);
statement.execute(discoveryDistSQLCommand.getCreateDiscoveryRule().getExecuteSQL());
Awaitility.await().atMost(Durations.TWO_SECONDS).until(() -> assertResult0(statement, discoveryDistSQLCommand.getCreateDiscoveryRule().getAssertionSQL()));
}

private void createReadwriteSplittingRule(final Statement statement) throws SQLException {
statement.execute(discoveryDistSQLCommand.getCreateReadwriteSplittingRule());
ThreadUtil.sleep(2, TimeUnit.SECONDS);
statement.execute(discoveryDistSQLCommand.getCreateReadwriteSplittingRule().getExecuteSQL());
Awaitility.await().atMost(Durations.TWO_SECONDS).until(() -> assertResult0(statement, discoveryDistSQLCommand.getCreateReadwriteSplittingRule().getAssertionSQL()));
}

private boolean assertResult0(final Statement statement, final String assertionSQL) {
try (ResultSet resultSet = statement.executeQuery(assertionSQL)) {
return resultSet.next();
} catch (final SQLException ignored) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shardingsphere.data.pipeline.core.util.ThreadUtil;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.test.e2e.discovery.build.DiscoveryRuleBuilder;
import org.apache.shardingsphere.test.e2e.discovery.cases.DatabaseClusterEnvironment;
import org.apache.shardingsphere.test.e2e.discovery.env.DiscoveryE2ETestEnvironment;
import org.apache.shardingsphere.test.e2e.discovery.framework.container.compose.BaseContainerComposer;
import org.apache.shardingsphere.test.e2e.discovery.framework.container.compose.DockerContainerComposer;
import org.apache.shardingsphere.test.e2e.discovery.framework.parameter.DiscoveryTestParameter;
import org.awaitility.Awaitility;
import org.awaitility.Durations;

import javax.sql.DataSource;
import java.sql.Connection;
Expand All @@ -39,11 +40,9 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotEquals;

@Getter(AccessLevel.PROTECTED)
@Slf4j
Expand Down Expand Up @@ -84,8 +83,7 @@ public void initDiscoveryEnvironment() throws SQLException {
public void assertClosePrimaryDataSource(final DatabaseClusterEnvironment mgrEnvironment) throws SQLException {
String oldPrimaryDataSourceName = getPrimaryDataSourceName();
closeDataSource(mgrEnvironment.getDataSources().get(oldPrimaryDataSourceName));
String newPrimaryDataSourceName = getPrimaryDataSourceName();
assertPrimaryDataSourceChanged(oldPrimaryDataSourceName, newPrimaryDataSourceName);
Awaitility.await().atMost(Durations.ONE_MINUTE).until(() -> !oldPrimaryDataSourceName.equals(getPrimaryDataSourceName()));
mgrEnvironment.getDataSources().remove(oldPrimaryDataSourceName);
}

Expand Down Expand Up @@ -122,11 +120,6 @@ private void closeDataSource(final DataSource dataSource) throws SQLException {
Statement statement = connection.createStatement()) {
statement.execute("SHUTDOWN");
}
ThreadUtil.sleep(35, TimeUnit.SECONDS);
}

private void assertPrimaryDataSourceChanged(final String oldPrimaryDataSourceName, final String newPrimaryDataSourceName) {
assertNotEquals(oldPrimaryDataSourceName, newPrimaryDataSourceName);
}

/**
Expand All @@ -138,8 +131,8 @@ public void assertCloseReplicationDataSource(final DatabaseClusterEnvironment mg
mgrEnvironment.getDataSources().remove(getPrimaryDataSourceName());
String closedRoutingDataSourceName = getCloseReplicationDataSourceName(mgrEnvironment);
mgrEnvironment.getDataSources().remove(closedRoutingDataSourceName);
String routeDataSourceName = getRouteDataSourceName();
assertRouteDataSourceName(routeDataSourceName, Objects.requireNonNull(mgrEnvironment.getDataSources().entrySet().stream().findFirst().orElse(null)).getKey());
Awaitility.await().atMost(Durations.ONE_MINUTE).until(() ->
getRouteDataSourceName().equals(Objects.requireNonNull(mgrEnvironment.getDataSources().entrySet().stream().findFirst().orElse(null)).getKey()));
}

private String getCloseReplicationDataSourceName(final DatabaseClusterEnvironment mgrEnvironment) throws SQLException {
Expand All @@ -164,18 +157,13 @@ private String getRouteDataSourceName(final Statement statement) throws SQLExcep
}
}

private void assertRouteDataSourceName(final String actualRouteDataSourceName, final String expectedRouteDataSourceName) {
Preconditions.checkState(StringUtils.isNotBlank(actualRouteDataSourceName) && StringUtils.isNotBlank(expectedRouteDataSourceName));
assertThat(actualRouteDataSourceName, is(expectedRouteDataSourceName));
}

/**
* Assert close all replication data source.
* @param mgrEnvironment mgr environment
* @throws SQLException SQL Exception
*/
public void assertCloseAllReplicationDataSource(final DatabaseClusterEnvironment mgrEnvironment) throws SQLException {
closeDataSource(Objects.requireNonNull(mgrEnvironment.getDataSources().values().stream().findFirst().orElse(null)));
assertRouteDataSourceName(getRouteDataSourceName(), getPrimaryDataSourceName());
Awaitility.await().atMost(Durations.ONE_MINUTE).until(() -> getRouteDataSourceName().equals(getPrimaryDataSourceName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.test.e2e.discovery.pojo.DistSQLCommandPOJO;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
Expand All @@ -30,15 +31,19 @@
@XmlAccessorType(XmlAccessType.FIELD)
public final class DiscoveryDistSQLCommand {

@XmlElement(name = "create-database")
@Getter
private DistSQLCommandPOJO createDatabase;

@XmlElement(name = "register-storage-unit")
@Getter
private String registerStorageUnit;
private DistSQLCommandPOJO registerStorageUnits;

@XmlElement(name = "create-discovery-rule")
@Getter
private String createDiscoveryRule;
private DistSQLCommandPOJO createDiscoveryRule;

@XmlElement(name = "create-readwrite-splitting-rule")
@Getter
private String createReadwriteSplittingRule;
private DistSQLCommandPOJO createReadwriteSplittingRule;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.discovery.pojo;

import lombok.Getter;
import lombok.Setter;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@Setter
@XmlRootElement(name = "command")
@XmlAccessorType(XmlAccessType.FIELD)
public final class DistSQLCommandPOJO {

@XmlElement(name = "execute-sql")
@Getter
private String executeSQL;

@XmlElement(name = "assertion-sql")
@Getter
private String assertionSQL;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,72 @@
-->

<command>
<create-database>
<execute-sql>
CREATE DATABASE db_discovery;
</execute-sql>
<assertion-sql>
USE db_discovery;
</assertion-sql>
</create-database>
<register-storage-unit>
REGISTER STORAGE UNIT ds_0 (
HOST="mysql_1",
PORT=3306,
DB="it_discovery_test",
USER="test_user",
PASSWORD="Test@123",
PROPERTIES("connectionTimeout"="5000")
),ds_1 (
HOST="mysql_2",
PORT=3306,
DB="it_discovery_test",
USER="test_user",
PASSWORD="Test@123",
PROPERTIES("connectionTimeout"="5000")
),ds_2 (
HOST="mysql_3",
PORT=3306,
DB="it_discovery_test",
USER="test_user",
PASSWORD="Test@123",
PROPERTIES("connectionTimeout"="5000")
),ds_3 (
HOST="mysql_4",
PORT=3306,
DB="it_discovery_test",
USER="test_user",
PASSWORD="Test@123",
PROPERTIES("connectionTimeout"="5000")
);
<execute-sql>
REGISTER STORAGE UNIT ds_0 (
HOST="mysql_1",
PORT=3306,
DB="it_discovery_test",
USER="test_user",
PASSWORD="Test@123",
PROPERTIES("connectionTimeout"="5000")
),ds_1 (
HOST="mysql_2",
PORT=3306,
DB="it_discovery_test",
USER="test_user",
PASSWORD="Test@123",
PROPERTIES("connectionTimeout"="5000")
),ds_2 (
HOST="mysql_3",
PORT=3306,
DB="it_discovery_test",
USER="test_user",
PASSWORD="Test@123",
PROPERTIES("connectionTimeout"="5000")
),ds_3 (
HOST="mysql_4",
PORT=3306,
DB="it_discovery_test",
USER="test_user",
PASSWORD="Test@123",
PROPERTIES("connectionTimeout"="5000")
);
</execute-sql>
<assertion-sql>
SHOW STORAGE UNITS;
</assertion-sql>
</register-storage-unit>

<create-discovery-rule>
CREATE DB_DISCOVERY RULE replica_ds (
STORAGE_UNITS(ds_0, ds_1, ds_2, ds_3),
TYPE(NAME='MySQL.MGR',PROPERTIES('group-name'='558edd3c-02ec-11ea-9bb3-080027e39bd2')),
HEARTBEAT(PROPERTIES('keep-alive-cron'='0/2 * * * * ?'))
);
<execute-sql>
CREATE DB_DISCOVERY RULE replica_ds (
STORAGE_UNITS(ds_0, ds_1, ds_2, ds_3),
TYPE(NAME='MySQL.MGR',PROPERTIES('group-name'='558edd3c-02ec-11ea-9bb3-080027e39bd2')),
HEARTBEAT(PROPERTIES('keep-alive-cron'='0/2 * * * * ?'))
);
</execute-sql>
<assertion-sql>
SHOW DB_DISCOVERY RULES replica_ds;
</assertion-sql>
</create-discovery-rule>

<create-readwrite-splitting-rule>
CREATE READWRITE_SPLITTING RULE readwrite_ds (
AUTO_AWARE_RESOURCE=replica_ds
);
<execute-sql>
CREATE READWRITE_SPLITTING RULE readwrite_ds (
AUTO_AWARE_RESOURCE=replica_ds
);
</execute-sql>
<assertion-sql>
SHOW READWRITE_SPLITTING RULES replica_ds;
</assertion-sql>
</create-readwrite-splitting-rule>
</command>