You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/01/18 11:36:25 UTC
[shardingsphere] branch master updated: #7318,
add test cases (#9077)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 b472804 #7318, add test cases (#9077)
b472804 is described below
commit b472804d128dcb0b3e7f11838ec4466e81f217cb
Author: Zhang Yonglun <zh...@apache.org>
AuthorDate: Mon Jan 18 19:35:53 2021 +0800
#7318, add test cases (#9077)
---
.../apache/shardingsphere/ha/mgr/MGRHAType.java | 2 +-
...{MGRPeriodicalJob.java => MGRHeartbeatJob.java} | 11 +-
.../shardingsphere/ha/mgr/MGRHATypeTest.java | 120 +++++++++++++++++++++
3 files changed, 125 insertions(+), 8 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHAType.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHAType.java
index f1297c4..b61654b 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHAType.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHAType.java
@@ -278,7 +278,7 @@ public final class MGRHAType implements HAType {
if (null != SCHEDULE_JOB_BOOTSTRAP_MAP.get(groupName)) {
SCHEDULE_JOB_BOOTSTRAP_MAP.get(groupName).shutdown();
}
- SCHEDULE_JOB_BOOTSTRAP_MAP.put(groupName, new ScheduleJobBootstrap(coordinatorRegistryCenter, new MGRPeriodicalJob(this, dataSourceMap, schemaName, disabledDataSourceNames,
+ SCHEDULE_JOB_BOOTSTRAP_MAP.put(groupName, new ScheduleJobBootstrap(coordinatorRegistryCenter, new MGRHeartbeatJob(this, dataSourceMap, schemaName, disabledDataSourceNames,
groupName, primaryDataSourceName), JobConfiguration.newBuilder("MGR-" + groupName, 1).cron(props.getProperty("keepAliveCron")).build()));
SCHEDULE_JOB_BOOTSTRAP_MAP.get(groupName).schedule();
}
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRPeriodicalJob.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHeartbeatJob.java
similarity index 81%
rename from shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRPeriodicalJob.java
rename to shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHeartbeatJob.java
index 606bff0..9c6ec7d 100644
--- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRPeriodicalJob.java
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/main/java/org/apache/shardingsphere/ha/mgr/MGRHeartbeatJob.java
@@ -25,12 +25,14 @@ import org.apache.shardingsphere.ha.spi.HAType;
import javax.sql.DataSource;
import java.util.Collection;
-import java.util.HashMap;
import java.util.Map;
+/**
+ * MGR heartbeat job.
+ */
@RequiredArgsConstructor
@Slf4j
-public final class MGRPeriodicalJob implements SimpleJob {
+public final class MGRHeartbeatJob implements SimpleJob {
private final HAType haType;
@@ -46,11 +48,6 @@ public final class MGRPeriodicalJob implements SimpleJob {
@Override
public void execute(final ShardingContext shardingContext) {
- Map<String, DataSource> activeDataSourceMap = new HashMap<>(dataSourceMap);
- if (!disabledDataSourceNames.isEmpty()) {
- activeDataSourceMap.entrySet().removeIf(each -> disabledDataSourceNames.contains(each.getKey()));
- }
- log.info("|activeDataSourceMap| " + activeDataSourceMap.toString());
haType.updatePrimaryDataSource(dataSourceMap, schemaName, disabledDataSourceNames, groupName, primaryDataSourceName);
haType.updateMemberState(dataSourceMap, schemaName, disabledDataSourceNames);
}
diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/test/java/org/apache/shardingsphere/ha/mgr/MGRHATypeTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/test/java/org/apache/shardingsphere/ha/mgr/MGRHATypeTest.java
new file mode 100644
index 0000000..ac5cb73
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-provider/shardingsphere-ha-mgr/src/test/java/org/apache/shardingsphere/ha/mgr/MGRHATypeTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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.ha.mgr;
+
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import org.junit.Test;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class MGRHATypeTest {
+
+ private static final String PLUGIN_STATUS = "SELECT * FROM information_schema.PLUGINS WHERE PLUGIN_NAME='group_replication'";
+
+ private static final String MEMBER_COUNT = "SELECT count(*) FROM performance_schema.replication_group_members";
+
+ private static final String GROUP_NAME = "SELECT * FROM performance_schema.global_variables WHERE VARIABLE_NAME='group_replication_group_name'";
+
+ private static final String SINGLE_PRIMARY = "SELECT * FROM performance_schema.global_variables WHERE VARIABLE_NAME='group_replication_single_primary_mode'";
+
+ private final MGRHAType mgrHaType = new MGRHAType();
+
+ @Test
+ public void checkHAConfig() {
+ DataSource dataSource = mock(DataSource.class);
+ Connection connection = mock(Connection.class);
+ Statement statement = mock(Statement.class);
+ ResultSet resultSet = mock(ResultSet.class);
+ try {
+ when(dataSource.getConnection()).thenReturn(connection);
+ when(connection.createStatement()).thenReturn(statement);
+ when(statement.executeQuery(PLUGIN_STATUS)).thenReturn(resultSet);
+ when(statement.executeQuery(MEMBER_COUNT)).thenReturn(resultSet);
+ when(statement.executeQuery(GROUP_NAME)).thenReturn(resultSet);
+ when(statement.executeQuery(SINGLE_PRIMARY)).thenReturn(resultSet);
+ when(resultSet.next()).thenReturn(true, false, true, false, true, false, true, false);
+ when(resultSet.getString("PLUGIN_STATUS")).thenReturn("ACTIVE");
+ when(resultSet.getInt(1)).thenReturn(3);
+ when(resultSet.getString("VARIABLE_VALUE")).thenReturn("group_name", "ON");
+ } catch (final SQLException ex) {
+ throw new ShardingSphereException(ex);
+ }
+ Map<String, DataSource> dataSourceMap = mock(HashMap.class);
+ when(dataSourceMap.get(null)).thenReturn(dataSource);
+ try {
+ mgrHaType.getProps().setProperty("groupName", "group_name");
+ mgrHaType.checkHAConfig(dataSourceMap, "ha_db");
+ } catch (final SQLException ex) {
+ throw new ShardingSphereException(ex);
+ }
+ }
+
+ @Test
+ public void updatePrimaryDataSource() {
+ List<DataSource> dataSources = new LinkedList<>();
+ List<Connection> connections = new LinkedList<>();
+ List<Statement> statements = new LinkedList<>();
+ List<ResultSet> resultSets = new LinkedList<>();
+ List<DatabaseMetaData> databaseMetaData = new LinkedList<>();
+ for (int i = 0; i < 3; i++) {
+ dataSources.add(mock(DataSource.class));
+ connections.add(mock(Connection.class));
+ statements.add(mock(Statement.class));
+ resultSets.add(mock(ResultSet.class));
+ databaseMetaData.add(mock(DatabaseMetaData.class));
+ }
+ String sql = "SELECT MEMBER_HOST, MEMBER_PORT FROM performance_schema.replication_group_members WHERE MEMBER_ID = "
+ + "(SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME = 'group_replication_primary_member')";
+ try {
+ for (int i = 0; i < 3; i++) {
+ when(dataSources.get(i).getConnection()).thenReturn(connections.get(i));
+ when(connections.get(i).createStatement()).thenReturn(statements.get(i));
+ when(statements.get(i).executeQuery(sql)).thenReturn(resultSets.get(i));
+ when(resultSets.get(i).next()).thenReturn(true, false);
+ when(resultSets.get(i).getString("MEMBER_HOST")).thenReturn("127.0.0.1");
+ when(resultSets.get(i).getString("MEMBER_PORT")).thenReturn(Integer.toString(3306 + i));
+ when(connections.get(i).getMetaData()).thenReturn(databaseMetaData.get(i));
+ when(databaseMetaData.get(i).getURL()).thenReturn("jdbc:mysql://127.0.0.1:" + (3306 + i) + "/ds_0?serverTimezone=UTC&useSSL=false");
+ }
+ } catch (final SQLException ex) {
+ throw new ShardingSphereException(ex);
+ }
+ Map<String, DataSource> dataSourceMap = new HashMap<>();
+ for (int i = 0; i < 3; i++) {
+ dataSourceMap.put("ds_" + i, dataSources.get(i));
+ }
+ mgrHaType.getProps().setProperty("groupName", "group_name");
+ mgrHaType.updatePrimaryDataSource(dataSourceMap, "ha_db", Collections.emptySet(), "group_name", null);
+ assertThat(mgrHaType.getPrimaryDataSource(), is("ds_2"));
+ }
+}