未验证 提交 1b02f0fd 编写于 作者: L Liang Zhang 提交者: GitHub

Remove MasterSlaveDataSourceFactory and related API (#5459)

* for code style

* deprecate OrchestrationMasterSlaveDataSourceFactory

* remove OrchestrationMasterSlaveDataSourceFactory

* remove YamlOrchestrationMasterSlaveDataSourceFactory

* remove YamlProxyRuleConfiguration.masterSlaveRule

* remove YamlMasterSlaveDataSourceFactory

* refactor ShadowDataSourceFactory

* refactor SpringBootConfiguration

* fix example

* remove MasterSlaveDataSourceFactoryTest

* refactor MasterSlaveDataSourceTest

* remove MasterSlaveDataSourceFactory
上级 e71ba102
......@@ -24,7 +24,6 @@ import org.apache.shardingsphere.example.type.RegistryCenterType;
import org.apache.shardingsphere.example.type.ShardingType;
import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractDataSourceAdapter;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.yaml.YamlOrchestrationEncryptDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.yaml.YamlOrchestrationMasterSlaveDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.yaml.YamlOrchestrationShardingDataSourceFactory;
import javax.sql.DataSource;
......@@ -66,7 +65,7 @@ public class YamlConfigurationExampleMain {
return YamlOrchestrationShardingDataSourceFactory.createDataSource(getFile(yamlFilePath));
case MASTER_SLAVE:
yamlFilePath = String.format("/META-INF/%s/%s/master-slave.yaml", registryCenterType.name().toLowerCase(), loadConfigFromRegCenter ? "cloud" : "local");
return YamlOrchestrationMasterSlaveDataSourceFactory.createDataSource(getFile(yamlFilePath));
return YamlOrchestrationShardingDataSourceFactory.createDataSource(getFile(yamlFilePath));
case ENCRYPT:
yamlFilePath = String.format("/META-INF/%s/%s/encrypt.yaml", registryCenterType.name().toLowerCase(), loadConfigFromRegCenter ? "cloud" : "local");
return YamlOrchestrationEncryptDataSourceFactory.createDataSource(getFile(yamlFilePath));
......
......@@ -20,7 +20,7 @@ package org.apache.shardingsphere.example.orchestration.raw.jdbc.config.cloud;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.OrchestrationMasterSlaveDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.OrchestrationShardingDataSourceFactory;
import javax.sql.DataSource;
import java.sql.SQLException;
......@@ -36,7 +36,6 @@ public final class CloudMasterSlaveConfiguration implements ExampleConfiguration
@Override
public DataSource getDataSource() throws SQLException {
return OrchestrationMasterSlaveDataSourceFactory.createDataSource(new OrchestrationConfiguration(centerConfigurationMap));
return OrchestrationShardingDataSourceFactory.createDataSource(new OrchestrationConfiguration(centerConfigurationMap));
}
}
......@@ -22,11 +22,12 @@ import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.OrchestrationMasterSlaveDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.OrchestrationShardingDataSourceFactory;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
......@@ -43,7 +44,7 @@ public final class LocalMasterSlaveConfiguration implements ExampleConfiguration
public DataSource getDataSource() throws SQLException {
MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration("demo_ds_master_slave", "demo_ds_master", Arrays.asList("demo_ds_slave_0", "demo_ds_slave_1"));
OrchestrationConfiguration orchestrationConfig = new OrchestrationConfiguration(centerConfigurationMap);
return OrchestrationMasterSlaveDataSourceFactory.createDataSource(createDataSourceMap(), masterSlaveRuleConfig, new Properties(), orchestrationConfig);
return OrchestrationShardingDataSourceFactory.createDataSource(createDataSourceMap(), Collections.singleton(masterSlaveRuleConfig), new Properties(), orchestrationConfig);
}
private Map<String, DataSource> createDataSourceMap() {
......
......@@ -17,14 +17,15 @@
package org.apache.shardingsphere.example.sharding.raw.jdbc.config;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
......@@ -34,7 +35,7 @@ public final class MasterSlaveConfiguration implements ExampleConfiguration {
@Override
public DataSource getDataSource() throws SQLException {
MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration("demo_ds_master_slave", "demo_ds_master", Arrays.asList("demo_ds_slave_0", "demo_ds_slave_1"));
return MasterSlaveDataSourceFactory.createDataSource(createDataSourceMap(), masterSlaveRuleConfig, new Properties());
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), Collections.singleton(masterSlaveRuleConfig), new Properties());
}
private Map<String, DataSource> createDataSourceMap() {
......
/*
* 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.shardingjdbc.api;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.core.rule.MasterSlaveRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
/**
* Master-slave data source factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MasterSlaveDataSourceFactory {
/**
* Create master-slave data source.
*
* @param dataSourceMap data source map
* @param masterSlaveRuleConfig master-slave rule configuration
* @param props props
* @return master-slave data source
* @throws SQLException SQL exception
*
* @deprecated instead of ShardingDataSourceFactory
*/
@Deprecated
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final Properties props) throws SQLException {
return new MasterSlaveDataSource(dataSourceMap, new MasterSlaveRule(masterSlaveRuleConfig), props);
}
}
......@@ -92,7 +92,7 @@ public final class ShadowDataSourceFactory {
} else if (shadowRule.isSharding()) {
return ShardingDataSourceFactory.createDataSource(dataSources, Collections.singletonList(shadowRule.getShardingRuleConfig()), props);
} else if (shadowRule.isMasterSlave()) {
return MasterSlaveDataSourceFactory.createDataSource(dataSources, shadowRule.getMasterSlaveRuleConfig(), props);
return ShardingDataSourceFactory.createDataSource(dataSources, Collections.singletonList(shadowRule.getMasterSlaveRuleConfig()), props);
} else {
return dataSources.values().iterator().next();
}
......
/*
* 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.shardingjdbc.api.yaml;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.core.yaml.config.masterslave.YamlRootMasterSlaveConfiguration;
import org.apache.shardingsphere.underlying.common.yaml.engine.YamlEngine;
import org.apache.shardingsphere.core.yaml.swapper.MasterSlaveRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;
/**
* Master-slave data source factory for YAML.
*
* @deprecated instead of ShardingDataSourceFactory
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Deprecated
public final class YamlMasterSlaveDataSourceFactory {
/**
* Create master-slave data source.
*
* @param yamlFile YAML file for master-slave rule configuration with data sources
* @return master-slave data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final File yamlFile) throws SQLException, IOException {
YamlRootMasterSlaveConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootMasterSlaveConfiguration.class);
return MasterSlaveDataSourceFactory.createDataSource(config.getDataSources(), new MasterSlaveRuleConfigurationYamlSwapper().swap(config.getMasterSlaveRule()), config.getProps());
}
/**
* Create master-slave data source.
*
* @param yamlBytes YAML bytes for master-slave rule configuration with data sources
* @return master-slave data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final byte[] yamlBytes) throws SQLException, IOException {
YamlRootMasterSlaveConfiguration config = YamlEngine.unmarshal(yamlBytes, YamlRootMasterSlaveConfiguration.class);
return MasterSlaveDataSourceFactory.createDataSource(config.getDataSources(), new MasterSlaveRuleConfigurationYamlSwapper().swap(config.getMasterSlaveRule()), config.getProps());
}
/**
* Create master-slave data source.
*
* @param dataSourceMap data source map
* @param yamlFile YAML file for master-slave rule configuration without data sources
* @return master-slave data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final File yamlFile) throws SQLException, IOException {
YamlRootMasterSlaveConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootMasterSlaveConfiguration.class);
return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new MasterSlaveRuleConfigurationYamlSwapper().swap(config.getMasterSlaveRule()), config.getProps());
}
/**
* Create master-slave data source.
*
* @param dataSourceMap data source map
* @param yamlBytes YAML bytes for master-slave rule configuration without data sources
* @return master-slave data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlBytes) throws SQLException, IOException {
YamlRootMasterSlaveConfiguration config = YamlEngine.unmarshal(yamlBytes, YamlRootMasterSlaveConfiguration.class);
return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new MasterSlaveRuleConfigurationYamlSwapper().swap(config.getMasterSlaveRule()), config.getProps());
}
}
/*
* 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.shardingjdbc.api;
import org.apache.shardingsphere.api.config.masterslave.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.shardingjdbc.fixture.TestDataSource;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource;
import org.junit.Test;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
public final class MasterSlaveDataSourceFactoryTest {
@Test
public void assertCreateDataSourceForSingleSlave() throws SQLException {
Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
dataSourceMap.put("master_ds", new TestDataSource("master_ds"));
dataSourceMap.put("slave_ds", new TestDataSource("slave_ds"));
Properties properties = new Properties();
properties.setProperty("sql.show", "true");
assertThat(MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new MasterSlaveRuleConfiguration("logic_ds", "master_ds", Collections.singletonList("slave_ds"),
new LoadBalanceStrategyConfiguration("ROUND_ROBIN")), properties), instanceOf(MasterSlaveDataSource.class));
}
@Test
public void assertCreateDataSourceForMultipleSlaves() throws SQLException {
Map<String, DataSource> dataSourceMap = new HashMap<>(3, 1);
dataSourceMap.put("master_ds", new TestDataSource("master_ds"));
dataSourceMap.put("slave_ds_0", new TestDataSource("slave_ds_0"));
dataSourceMap.put("slave_ds_1", new TestDataSource("slave_ds_1"));
Properties properties = new Properties();
properties.setProperty("sql.show", "true");
assertThat(MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new MasterSlaveRuleConfiguration("logic_ds", "master_ds", Arrays.asList("slave_ds_0", "slave_ds_1"),
new LoadBalanceStrategyConfiguration("ROUND_ROBIN")), properties), instanceOf(MasterSlaveDataSource.class));
}
}
......@@ -20,36 +20,24 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource;
import org.apache.shardingsphere.api.config.masterslave.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.hint.HintManager;
import org.apache.shardingsphere.masterslave.route.engine.impl.MasterVisitedManager;
import org.apache.shardingsphere.core.rule.MasterSlaveRule;
import org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
import org.apache.shardingsphere.masterslave.route.engine.impl.MasterVisitedManager;
import org.apache.shardingsphere.shardingjdbc.fixture.TestDataSource;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.MasterSlaveConnection;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.apache.shardingsphere.underlying.common.database.type.dialect.H2DatabaseType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public final class MasterSlaveDataSourceTest {
......@@ -78,58 +66,6 @@ public final class MasterSlaveDataSourceTest {
TransactionTypeHolder.clear();
}
@Test(expected = IllegalStateException.class)
public void assertGetDatabaseProductNameWhenDataBaseProductNameDifferent() throws SQLException {
DataSource masterDataSource = mock(DataSource.class);
DataSource slaveDataSource = mock(DataSource.class);
Connection masterConnection = mockConnection("jdbc:mysql://localhost:3306/test_ds");
final Connection slaveConnection = mockConnection("jdbc:h2:mem:test_ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL");
Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
dataSourceMap.put("masterDataSource", masterDataSource);
dataSourceMap.put("slaveDataSource", slaveDataSource);
when(masterDataSource.getConnection()).thenReturn(masterConnection);
when(slaveDataSource.getConnection()).thenReturn(slaveConnection);
MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration(
"ds", "masterDataSource", Collections.singletonList("slaveDataSource"), new LoadBalanceStrategyConfiguration("ROUND_ROBIN"));
try {
MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveRuleConfig, new Properties());
} finally {
verify(masterConnection).close();
verify(slaveConnection).close();
}
}
@Test
public void assertGetDatabaseProductName() throws SQLException {
DataSource masterDataSource = mock(DataSource.class);
DataSource slaveDataSource1 = mock(DataSource.class);
DataSource slaveDataSource2 = mock(DataSource.class);
Connection masterConnection = mockConnection("jdbc:h2:mem:test_ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL");
Connection slaveConnection1 = mockConnection("jdbc:h2:mem:test_ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL");
Connection slaveConnection2 = mockConnection("jdbc:h2:mem:test_ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL");
when(masterDataSource.getConnection()).thenReturn(masterConnection);
when(slaveDataSource1.getConnection()).thenReturn(slaveConnection1);
when(slaveDataSource2.getConnection()).thenReturn(slaveConnection2);
Map<String, DataSource> dataSourceMap = new LinkedHashMap<>(3, 1);
dataSourceMap.put("masterDataSource", masterDataSource);
dataSourceMap.put("slaveDataSource1", slaveDataSource1);
dataSourceMap.put("slaveDataSource2", slaveDataSource2);
assertThat(((MasterSlaveDataSource) MasterSlaveDataSourceFactory.createDataSource(dataSourceMap,
new MasterSlaveRuleConfiguration("ds", "masterDataSource", Arrays.asList("slaveDataSource1", "slaveDataSource2"), new LoadBalanceStrategyConfiguration("ROUND_ROBIN")),
new Properties())).getDatabaseType(), instanceOf(H2DatabaseType.class));
verify(slaveConnection1, times(3)).close();
verify(slaveConnection2, times(3)).close();
}
private Connection mockConnection(final String url) throws SQLException {
Connection result = mock(Connection.class);
DatabaseMetaData metaData = mock(DatabaseMetaData.class);
when(metaData.getTables(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(mock(ResultSet.class));
when(result.getMetaData()).thenReturn(metaData);
when(metaData.getURL()).thenReturn(url);
return result;
}
@Test
public void assertGetConnection() {
assertThat(masterSlaveDataSource.getConnection(), instanceOf(MasterSlaveConnection.class));
......
......@@ -18,11 +18,8 @@
package org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource;
import com.google.common.base.Joiner;
import org.apache.shardingsphere.api.config.masterslave.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.ShardingConnection;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.fixture.XAShardingTransactionManagerFixture;
import org.apache.shardingsphere.transaction.core.TransactionType;
......@@ -74,22 +71,6 @@ public final class ShardingDataSourceTest {
assertDatabaseProductName(dataSourceMap, dataSource1.getConnection(), dataSource2.getConnection());
}
@Test(expected = IllegalStateException.class)
public void assertGetDatabaseProductNameWhenDataBaseProductNameDifferentForMasterSlave() throws SQLException {
DataSource dataSource1 = mockDataSource(DatabaseTypes.getActualDatabaseType("MySQL"));
DataSource masterDataSource = mockDataSource(DatabaseTypes.getActualDatabaseType("H2"));
DataSource slaveDataSource = mockDataSource(DatabaseTypes.getActualDatabaseType("H2"));
Map<String, DataSource> masterSlaveDataSourceMap = new HashMap<>(2, 1);
masterSlaveDataSourceMap.put("masterDataSource", masterDataSource);
masterSlaveDataSourceMap.put("slaveDataSource", slaveDataSource);
MasterSlaveDataSource dataSource2 = (MasterSlaveDataSource) MasterSlaveDataSourceFactory.createDataSource(masterSlaveDataSourceMap,
new MasterSlaveRuleConfiguration("ds", "masterDataSource", Collections.singletonList("slaveDataSource"), new LoadBalanceStrategyConfiguration("ROUND_ROBIN")), new Properties());
Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
dataSourceMap.put("ds1", dataSource1);
dataSourceMap.put("ds2", dataSource2);
assertDatabaseProductName(dataSourceMap, dataSource1.getConnection(), masterDataSource.getConnection(), slaveDataSource.getConnection());
}
@Test
public void assertGetDatabaseProductName() throws SQLException {
DataSource dataSource1 = mockDataSource(DatabaseTypes.getActualDatabaseType("H2"));
......
/*
* 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.shardingjdbc.orchestration.api;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.core.rule.MasterSlaveRule;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.datasource.OrchestrationMasterSlaveDataSource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
/**
* Orchestration sharding data source factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class OrchestrationMasterSlaveDataSourceFactory {
/**
* Create master-slave data source.
*
* @param dataSourceMap data source map
* @param masterSlaveRuleConfig master-slave rule configuration
* @param props properties
* @param orchestrationConfig orchestration configuration
* @return master-slave data source
* @throws SQLException SQL exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig,
final Properties props, final OrchestrationConfiguration orchestrationConfig) throws SQLException {
if (null == masterSlaveRuleConfig || null == masterSlaveRuleConfig.getMasterDataSourceName()) {
return createDataSource(orchestrationConfig);
}
MasterSlaveDataSource masterSlaveDataSource = new MasterSlaveDataSource(dataSourceMap, new MasterSlaveRule(masterSlaveRuleConfig), props);
return new OrchestrationMasterSlaveDataSource(masterSlaveDataSource, orchestrationConfig);
}
/**
* Create master-slave data source.
*
* @param orchestrationConfig orchestration configuration
* @return master-slave data source
* @throws SQLException SQL exception
*/
public static DataSource createDataSource(final OrchestrationConfiguration orchestrationConfig) throws SQLException {
return new OrchestrationMasterSlaveDataSource(orchestrationConfig);
}
}
/*
* 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.shardingjdbc.orchestration.api.yaml;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.core.rule.MasterSlaveRule;
import org.apache.shardingsphere.core.yaml.config.masterslave.YamlMasterSlaveRuleConfiguration;
import org.apache.shardingsphere.core.yaml.swapper.MasterSlaveRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import org.apache.shardingsphere.orchestration.center.yaml.config.YamlCenterRepositoryConfiguration;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.datasource.OrchestrationMasterSlaveDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.util.YamlCenterRepositoryConfigurationSwapperUtil;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.yaml.YamlOrchestrationMasterSlaveRuleConfiguration;
import org.apache.shardingsphere.underlying.common.yaml.engine.YamlEngine;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
/**
* Orchestration master-slave data source factory for YAML.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class YamlOrchestrationMasterSlaveDataSourceFactory {
private static final MasterSlaveRuleConfigurationYamlSwapper MASTER_SLAVE_RULE_SWAPPER = new MasterSlaveRuleConfigurationYamlSwapper();
/**
* Create master-slave data source.
*
* @param yamlFile YAML file for master-slave rule configuration with data sources
* @return master-slave data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final File yamlFile) throws SQLException, IOException {
YamlOrchestrationMasterSlaveRuleConfiguration config = unmarshal(yamlFile);
return createDataSource(config.getDataSources(), config.getMasterSlaveRule(), config.getProps(), config.getOrchestration());
}
/**
* Create master-slave data source.
*
* @param dataSourceMap data source map
* @param yamlFile YAML file for master-slave rule configuration without data sources
* @return master-slave data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final File yamlFile) throws SQLException, IOException {
YamlOrchestrationMasterSlaveRuleConfiguration config = unmarshal(yamlFile);
return createDataSource(dataSourceMap, config.getMasterSlaveRule(), config.getProps(), config.getOrchestration());
}
/**
* Create master-slave data source.
*
* @param yamlBytes YAML bytes for master-slave rule configuration with data sources
* @return master-slave data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final byte[] yamlBytes) throws SQLException, IOException {
YamlOrchestrationMasterSlaveRuleConfiguration config = unmarshal(yamlBytes);
return createDataSource(config.getDataSources(), config.getMasterSlaveRule(), config.getProps(), config.getOrchestration());
}
/**
* Create master-slave data source.
*
* @param dataSourceMap data source map
* @param yamlBytes YAML bytes for master-slave rule configuration without data sources
* @return master-slave data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlBytes) throws SQLException, IOException {
YamlOrchestrationMasterSlaveRuleConfiguration config = unmarshal(yamlBytes);
return createDataSource(dataSourceMap, config.getMasterSlaveRule(), config.getProps(), config.getOrchestration());
}
private static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final YamlMasterSlaveRuleConfiguration yamlMasterSlaveRuleConfiguration,
final Properties props, final Map<String, YamlCenterRepositoryConfiguration> yamlInstanceConfigurationMap) throws SQLException {
if (null == yamlMasterSlaveRuleConfiguration) {
return new OrchestrationMasterSlaveDataSource(new OrchestrationConfiguration(YamlCenterRepositoryConfigurationSwapperUtil.marshal(yamlInstanceConfigurationMap)));
} else {
MasterSlaveDataSource masterSlaveDataSource = new MasterSlaveDataSource(dataSourceMap, new MasterSlaveRule(MASTER_SLAVE_RULE_SWAPPER.swap(yamlMasterSlaveRuleConfiguration)), props);
return new OrchestrationMasterSlaveDataSource(masterSlaveDataSource, new OrchestrationConfiguration(YamlCenterRepositoryConfigurationSwapperUtil.marshal(yamlInstanceConfigurationMap)));
}
}
private static YamlOrchestrationMasterSlaveRuleConfiguration unmarshal(final File yamlFile) throws IOException {
return YamlEngine.unmarshal(yamlFile, YamlOrchestrationMasterSlaveRuleConfiguration.class);
}
private static YamlOrchestrationMasterSlaveRuleConfiguration unmarshal(final byte[] yamlBytes) throws IOException {
return YamlEngine.unmarshal(yamlBytes, YamlOrchestrationMasterSlaveRuleConfiguration.class);
}
}
......@@ -21,8 +21,8 @@ import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.yaml.AbstractYamlDataSourceTest;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.yaml.YamlOrchestrationMasterSlaveDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.datasource.OrchestrationMasterSlaveDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.yaml.YamlOrchestrationShardingDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.datasource.OrchestrationShardingDataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
......@@ -58,9 +58,9 @@ public final class YamlOrchestrationMasterSlaveIntegrateTest extends AbstractYam
File yamlFile = new File(YamlOrchestrationMasterSlaveIntegrateTest.class.getResource(filePath).toURI());
DataSource dataSource;
if (hasDataSource) {
dataSource = YamlOrchestrationMasterSlaveDataSourceFactory.createDataSource(yamlFile);
dataSource = YamlOrchestrationShardingDataSourceFactory.createDataSource(yamlFile);
} else {
dataSource = YamlOrchestrationMasterSlaveDataSourceFactory.createDataSource(
dataSource = YamlOrchestrationShardingDataSourceFactory.createDataSource(
Maps.asMap(Sets.newHashSet("db_master", "db_slave_0", "db_slave_1"), AbstractYamlDataSourceTest::createDataSource), yamlFile);
}
try (Connection connection = dataSource.getConnection();
......@@ -69,6 +69,6 @@ public final class YamlOrchestrationMasterSlaveIntegrateTest extends AbstractYam
statement.executeQuery("SELECT * FROM t_order_item");
statement.executeQuery("SELECT * FROM t_config");
}
((OrchestrationMasterSlaveDataSource) dataSource).close();
((OrchestrationShardingDataSource) dataSource).close();
}
}
......@@ -21,6 +21,8 @@ import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.api.config.masterslave.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.core.rule.MasterSlaveRule;
import org.apache.shardingsphere.core.yaml.config.masterslave.YamlRootMasterSlaveConfiguration;
import org.apache.shardingsphere.core.yaml.swapper.MasterSlaveRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import org.apache.shardingsphere.orchestration.core.common.CenterType;
......@@ -30,15 +32,16 @@ import org.apache.shardingsphere.orchestration.core.common.event.PropertiesChang
import org.apache.shardingsphere.orchestration.core.registrycenter.event.CircuitStateChangedEvent;
import org.apache.shardingsphere.orchestration.core.registrycenter.event.DisabledStateChangedEvent;
import org.apache.shardingsphere.orchestration.core.registrycenter.schema.OrchestrationShardingSchema;
import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlMasterSlaveDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.circuit.connection.CircuitBreakerConnection;
import org.apache.shardingsphere.underlying.common.config.DataSourceConfiguration;
import org.apache.shardingsphere.underlying.common.database.DefaultSchema;
import org.apache.shardingsphere.underlying.common.exception.ShardingSphereException;
import org.apache.shardingsphere.underlying.common.yaml.engine.YamlEngine;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
......@@ -64,7 +67,12 @@ public final class OrchestrationMasterSlaveDataSourceTest {
private static MasterSlaveDataSource getMasterSlaveDataSource() throws IOException, SQLException, URISyntaxException {
File yamlFile = new File(OrchestrationMasterSlaveDataSource.class.getResource("/yaml/unit/masterSlave.yaml").toURI());
return (MasterSlaveDataSource) YamlMasterSlaveDataSourceFactory.createDataSource(yamlFile);
return (MasterSlaveDataSource) createDataSource(yamlFile);
}
private static DataSource createDataSource(final File yamlFile) throws SQLException, IOException {
YamlRootMasterSlaveConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootMasterSlaveConfiguration.class);
return new MasterSlaveDataSource(config.getDataSources(), new MasterSlaveRule(new MasterSlaveRuleConfigurationYamlSwapper().swap(config.getMasterSlaveRule())), config.getProps());
}
private static OrchestrationConfiguration getOrchestrationConfiguration() {
......
......@@ -35,12 +35,13 @@ dataSources:
password:
maxTotal: 100
masterSlaveRule:
name: db_ms
masterDataSourceName: db_master
slaveDataSourceNames:
- db_slave_0
- db_slave_1
masterSlaveRules:
db_ms:
name: db_ms
masterDataSourceName: db_master
slaveDataSourceNames:
- db_slave_0
- db_slave_1
orchestration:
test_ms_registry_name:
......
......@@ -35,12 +35,13 @@ dataSources:
password:
maxTotal: 100
masterSlaveRule:
name: db_ms
masterDataSourceName: db_master
slaveDataSourceNames:
- db_slave_0
- db_slave_1
masterSlaveRules:
db_ms:
name: db_ms
masterDataSourceName: db_master
slaveDataSourceNames:
- db_slave_0
- db_slave_1
orchestration:
test_ms_registry_name:
......
......@@ -46,14 +46,6 @@ public final class YamlProxyRuleConfiguration implements YamlConfiguration {
private Map<String, YamlMasterSlaveRuleConfiguration> masterSlaveRules = new LinkedHashMap<>();
/**
* Master slave rule.
*
* @deprecated master slave rule
*/
@Deprecated
private YamlMasterSlaveRuleConfiguration masterSlaveRule;
private YamlEncryptRuleConfiguration encryptRule;
private YamlShadowRuleConfiguration shadowRule;
......
......@@ -62,7 +62,6 @@ public final class ShardingConfigurationLoaderTest {
assertDataSourceParameter(actual.getDataSources().get("ds_0"), "jdbc:mysql://127.0.0.1:3306/ds_0");
assertDataSourceParameter(actual.getDataSources().get("ds_1"), "jdbc:mysql://127.0.0.1:3306/ds_1");
assertShardingRuleConfiguration(actual.getShardingRule());
assertNull(actual.getMasterSlaveRule());
assertNull(actual.getEncryptRule());
}
......@@ -105,7 +104,6 @@ public final class ShardingConfigurationLoaderTest {
assertNotNull(actual.getDataSource());
assertDataSourceParameter(actual.getDataSources().get("dataSource"), "jdbc:mysql://127.0.0.1:3306/encrypt_ds");
assertNull(actual.getShardingRule());
assertNull(actual.getMasterSlaveRule());
assertEncryptRuleConfiguration(actual.getEncryptRule());
}
......
......@@ -25,7 +25,6 @@ import org.apache.shardingsphere.core.yaml.swapper.ShadowRuleConfigurationYamlSw
import org.apache.shardingsphere.core.yaml.swapper.root.RuleRootConfigurationsYamlSwapper;
import org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.ShadowDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.spring.boot.common.SpringBootPropertiesConfigurationProperties;
......@@ -112,7 +111,7 @@ public class SpringBootConfiguration implements EnvironmentAware {
@Bean
@Conditional(MasterSlaveRuleCondition.class)
public DataSource masterSlaveDataSource() throws SQLException {
return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new MasterSlaveRuleConfigurationYamlSwapper().swap(masterSlaveRule), props.getProps());
return ShardingDataSourceFactory.createDataSource(dataSourceMap, Collections.singleton(new MasterSlaveRuleConfigurationYamlSwapper().swap(masterSlaveRule)), props.getProps());
}
/**
......
......@@ -18,7 +18,7 @@
package org.apache.shardingsphere.shardingjdbc.spring.boot.type;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......@@ -44,8 +44,8 @@ public class SpringBootMasterSlaveTest {
@Test
public void assertWithMasterSlaveDataSource() {
assertTrue(dataSource instanceof MasterSlaveDataSource);
for (DataSource each : ((MasterSlaveDataSource) dataSource).getDataSourceMap().values()) {
assertTrue(dataSource instanceof ShardingDataSource);
for (DataSource each : ((ShardingDataSource) dataSource).getDataSourceMap().values()) {
assertThat(((BasicDataSource) each).getMaxTotal(), is(100));
}
}
......
......@@ -33,10 +33,10 @@ public final class GroupedParameterBuilder implements ParameterBuilder {
@Getter
private final List<StandardParameterBuilder> parameterBuilders;
@Getter
private final List<Object> onDuplicateKeyUpdateAddedParameters = new LinkedList<>();
@Setter
private String derivedColumnName;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册