Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@

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;

/**
* Database configuration.
Expand All @@ -44,6 +47,16 @@ public interface DatabaseConfiguration {
*/
Map<String, StorageUnit> getStorageUnits();

/**
* Get storage unit configurations.
*
* @return storage unit configurations
*/
default Map<String, StorageUnitConfiguration> getStorageUnitConfigurations() {
return getStorageUnits().entrySet().stream().collect(Collectors.toMap(
Entry::getKey, entry -> new StorageUnitConfiguration(entry.getValue().getDataSourcePoolProperties()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}

/**
* Get data sources.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.infra.config.database;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;

/**
* Storage unit configuration.
*/
@RequiredArgsConstructor
@Getter
public final class StorageUnitConfiguration {

private final DataSourcePoolProperties dataSourcePoolProperties;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@

import lombok.Getter;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
import org.apache.shardingsphere.infra.config.database.StorageUnitConfiguration;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration;
import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
Expand All @@ -48,15 +47,18 @@ public final class DataSourceGeneratedDatabaseConfiguration implements DatabaseC

private final Map<StorageNode, DataSource> dataSources;

public DataSourceGeneratedDatabaseConfiguration(final Map<String, DataSourceConfiguration> dataSourceConfigs, final Collection<RuleConfiguration> ruleConfigs,
private final Map<String, StorageUnitConfiguration> storageUnitConfigurations;

public DataSourceGeneratedDatabaseConfiguration(final Map<String, StorageUnitConfiguration> storageUnitConfigs, final Collection<RuleConfiguration> ruleConfigs,
final boolean isInstanceConnectionEnabled) {
ruleConfigurations = ruleConfigs;
Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap = dataSourceConfigs.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap = storageUnitConfigs.entrySet().stream().collect(Collectors.toMap(
Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
Map<String, StorageNode> storageUnitNodeMap = StorageUnitNodeMapCreator.create(dataSourcePoolPropertiesMap, isInstanceConnectionEnabled);
Map<StorageNode, DataSource> storageNodeDataSources = createStorageNodeDataSourceMap(dataSourcePoolPropertiesMap, storageUnitNodeMap);
storageUnits = createStorageUnits(dataSourcePoolPropertiesMap, storageUnitNodeMap, storageNodeDataSources);
dataSources = storageNodeDataSources;
storageUnitConfigurations = storageUnitConfigs;
}

private Map<StorageNode, DataSource> createStorageNodeDataSourceMap(final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap, final Map<String, StorageNode> storageUnitNodeMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.infra.yaml.config.swapper.resource;

import com.google.common.base.Preconditions;
import org.apache.shardingsphere.infra.config.database.StorageUnitConfiguration;
import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
Expand Down Expand Up @@ -83,6 +84,16 @@ public DataSourcePoolProperties swapToDataSourcePoolProperties(final Map<String,
return new DataSourcePoolProperties(yamlConfig.get(DATA_SOURCE_CLASS_NAME_KEY).toString(), getProperties(yamlConfig));
}

/**
* Swap to storage unit configuration.
*
* @param yamlConfig YAML configuration
* @return storage unit configuration
*/
public StorageUnitConfiguration swapToStorageUnitConfiguration(final Map<String, Object> yamlConfig) {
return new StorageUnitConfiguration(swapToDataSourcePoolProperties(yamlConfig));
}

@SuppressWarnings({"rawtypes", "unchecked"})
private Map<String, Object> getProperties(final Map<String, Object> yamlConfig) {
Map<String, Object> result = new HashMap<>(yamlConfig);
Expand All @@ -107,4 +118,14 @@ public Map<String, Object> swapToMap(final DataSourcePoolProperties props) {
result.put(DATA_SOURCE_CLASS_NAME_KEY, props.getPoolClassName());
return result;
}

/**
* Swap to map from storage unit configuration.
*
* @param storageUnitConfig storage unit configuration
* @return data source map
*/
public Map<String, Object> swapToMap(final StorageUnitConfiguration storageUnitConfig) {
return swapToMap(storageUnitConfig.getDataSourcePoolProperties());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.infra.config.database;

import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

class StorageUnitConfigurationTest {

@Test
void assertDataSourcePoolProperties() {
DataSourcePoolProperties expected = new DataSourcePoolProperties("foo_class", Collections.emptyMap());
assertThat(new StorageUnitConfiguration(expected).getDataSourcePoolProperties(), is(expected));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
package org.apache.shardingsphere.infra.config.database.impl;

import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.config.database.StorageUnitConfiguration;
import org.apache.shardingsphere.infra.datasource.pool.config.ConnectionConfiguration;
import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration;
import org.apache.shardingsphere.infra.datasource.pool.config.PoolConfiguration;
import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator;
import org.apache.shardingsphere.infra.fixture.FixtureRuleConfiguration;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
Expand Down Expand Up @@ -91,8 +93,23 @@ void assertNewWithException() {
}

private DataSourceGeneratedDatabaseConfiguration createDatabaseConfiguration(final String dataSourceClassName) {
DataSourceConfiguration dataSourceConfig = new DataSourceConfiguration(new ConnectionConfiguration(dataSourceClassName, null, "jdbc:mock://127.0.0.1/foo_db", "root", ""),
StorageUnitConfiguration storageUnitConfig = new StorageUnitConfiguration(DataSourcePoolPropertiesCreator.create(createDataSourceConfiguration(dataSourceClassName)));
return new DataSourceGeneratedDatabaseConfiguration(Collections.singletonMap("foo_db", storageUnitConfig), Collections.singleton(new FixtureRuleConfiguration("foo_rule")), true);
}

private DataSourceConfiguration createDataSourceConfiguration(final String dataSourceClassName) {
return new DataSourceConfiguration(new ConnectionConfiguration(dataSourceClassName, null, "jdbc:mock://127.0.0.1/foo_db", "root", ""),
new PoolConfiguration(2000L, 1000L, 1000L, 2, 1, false, new Properties()));
return new DataSourceGeneratedDatabaseConfiguration(Collections.singletonMap("foo_db", dataSourceConfig), Collections.singleton(new FixtureRuleConfiguration("foo_rule")), true);
}

@Test
void assertNewWithStorageUnitConfigurations() {
DataSourceConfiguration dataSourceConfig = createDataSourceConfiguration(MockedDataSource.class.getName());
StorageUnitConfiguration storageUnitConfig = new StorageUnitConfiguration(DataSourcePoolPropertiesCreator.create(dataSourceConfig));
DataSourceGeneratedDatabaseConfiguration actual = new DataSourceGeneratedDatabaseConfiguration(
Collections.singletonMap("foo_db", storageUnitConfig), Collections.singleton(new FixtureRuleConfiguration("foo_rule")), true);
assertThat(actual.getStorageUnitConfigurations().get("foo_db"), is(storageUnitConfig));
assertStorageUnits(actual.getStorageUnits().get("foo_db"));
assertDataSources((MockedDataSource) actual.getDataSources().get(new StorageNode("foo_db")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void assertNewWithDataSources() {
Collections.singletonMap("foo_ds", new MockedDataSource()), Collections.singleton(new FixtureRuleConfiguration("foo_rule")));
assertRuleConfigurations(actual);
assertStorageUnits(actual.getStorageUnits().get("foo_ds"));
assertThat(actual.getStorageUnitConfigurations().get("foo_ds").getDataSourcePoolProperties(), is(actual.getStorageUnits().get("foo_ds").getDataSourcePoolProperties()));
assertDataSources((MockedDataSource) actual.getDataSources().get(new StorageNode("foo_ds")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.infra.yaml.config.swapper.resource;

import org.apache.shardingsphere.infra.config.database.StorageUnitConfiguration;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
import org.apache.shardingsphere.test.infra.fixture.jdbc.MockedDataSource;
Expand Down Expand Up @@ -143,4 +144,18 @@ private Map<String, Object> createYamlConfiguration() {
result.put("customPoolProps", customProps);
return result;
}

@Test
void assertSwapToStorageUnitConfiguration() {
StorageUnitConfiguration actual = swapper.swapToStorageUnitConfiguration(createPropertyMap("foo_ds"));
assertThat(actual.getDataSourcePoolProperties().getPoolClassName(), is(MockedDataSource.class.getName()));
assertThat(actual.getDataSourcePoolProperties().getAllLocalProperties().get("url"), is("jdbc:mock://127.0.0.1/foo_ds"));
}

@Test
void assertSwapStorageUnitConfigurationToMap() {
DataSourcePoolProperties props = new DataSourcePoolProperties(MockedDataSource.class.getName(), createProperties());
Map<String, Object> actual = swapper.swapToMap(new StorageUnitConfiguration(props));
assertThat(actual, is(swapper.swapToMap(props)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.apache.shardingsphere.elasticjob.lite.lifecycle.api.JobConfigurationAPI;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
Expand Down Expand Up @@ -107,6 +108,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
Expand Down Expand Up @@ -151,7 +153,7 @@ private CDCJobAPI createJobAPI() throws ReflectiveOperationException {
when(PipelineJobIdUtils.parseContextKey(anyString())).thenReturn(new PipelineContextKey("foo_db", InstanceType.PROXY));
when(PipelineAPIFactory.getPipelineGovernanceFacade(any())).thenReturn(mock(PipelineGovernanceFacade.class, RETURNS_DEEP_STUBS));
YamlDataSourceConfigurationSwapper dataSourceSwapper = mock(YamlDataSourceConfigurationSwapper.class);
when(dataSourceSwapper.swapToMap(any())).thenReturn(createStandardDataSourceProperties());
when(dataSourceSwapper.swapToMap(nullable(DataSourcePoolProperties.class))).thenReturn(createStandardDataSourceProperties());
CDCJobAPI result = new CDCJobAPI();
Plugins.getMemberAccessor().set(CDCJobAPI.class.getDeclaredField("dataSourceConfigSwapper"), result, dataSourceSwapper);
Plugins.getMemberAccessor().set(CDCJobAPI.class.getDeclaredField("ruleConfigSwapperEngine"), result, mock(YamlRuleConfigurationSwapperEngine.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
import org.apache.shardingsphere.infra.config.database.StorageUnitConfiguration;
import org.apache.shardingsphere.infra.config.database.impl.DataSourceGeneratedDatabaseConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationPropertyKey;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.database.DatabaseTypeEngine;
import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration;
import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
import org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaData;
Expand Down Expand Up @@ -90,9 +90,9 @@ private Map<String, DatabaseConfiguration> createEffectiveDatabaseConfigurations
private DatabaseConfiguration createEffectiveDatabaseConfiguration(final String databaseName, final Map<String, DatabaseConfiguration> databaseConfigs,
final boolean isInstanceConnectionEnabled) {
closeGeneratedDataSources(databaseName, databaseConfigs);
Map<String, DataSourceConfiguration> dataSources = persistFacade.loadDataSourceConfigurations(databaseName);
Map<String, StorageUnitConfiguration> storageUnitConfigs = persistFacade.loadStorageUnitConfigurations(databaseName);
Collection<RuleConfiguration> databaseRuleConfigs = persistFacade.getDatabaseRuleService().load(databaseName);
return new DataSourceGeneratedDatabaseConfiguration(dataSources, databaseRuleConfigs, isInstanceConnectionEnabled);
return new DataSourceGeneratedDatabaseConfiguration(storageUnitConfigs, databaseRuleConfigs, isInstanceConnectionEnabled);
}

private void closeGeneratedDataSources(final String databaseName, final Map<String, ? extends DatabaseConfiguration> databaseConfigs) {
Expand Down
Loading
Loading