Skip to content

Commit

Permalink
Remove impl of GovernanceSchemaContexts
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 3, 2020
1 parent ff9129f commit c4f7b47
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 314 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@
/**
* Governance schema contexts.
*/
public class GovernanceSchemaContexts implements SchemaContexts {
public final class GovernanceSchemaContexts implements SchemaContexts {

private final GovernanceFacade governanceFacade;

private volatile SchemaContexts schemaContexts;

protected GovernanceSchemaContexts(final SchemaContexts schemaContexts, final GovernanceFacade governanceFacade) {
public GovernanceSchemaContexts(final SchemaContexts schemaContexts, final GovernanceFacade governanceFacade) {
this.governanceFacade = governanceFacade;
this.schemaContexts = schemaContexts;
ShardingSphereEventBus.getInstance().register(this);
Expand All @@ -101,7 +101,7 @@ private void persistMetaData() {
}

@Override
public final DatabaseType getDatabaseType() {
public DatabaseType getDatabaseType() {
return schemaContexts.getDatabaseType();
}

Expand All @@ -116,32 +116,32 @@ private DatabaseType getDatabaseType(final Map<String, Map<String, DataSource>>
}

@Override
public final Map<String, SchemaContext> getSchemaContexts() {
public Map<String, SchemaContext> getSchemaContexts() {
return schemaContexts.getSchemaContexts();
}

@Override
public final SchemaContext getDefaultSchemaContext() {
public SchemaContext getDefaultSchemaContext() {
return schemaContexts.getDefaultSchemaContext();
}

@Override
public final Authentication getAuthentication() {
public Authentication getAuthentication() {
return schemaContexts.getAuthentication();
}

@Override
public final ConfigurationProperties getProps() {
public ConfigurationProperties getProps() {
return schemaContexts.getProps();
}

@Override
public final boolean isCircuitBreak() {
public boolean isCircuitBreak() {
return schemaContexts.isCircuitBreak();
}

@Override
public final void close() throws Exception {
public void close() throws Exception {
schemaContexts.close();
governanceFacade.close();
}
Expand Down Expand Up @@ -306,7 +306,7 @@ private SchemaContext getChangedSchemaContext(final SchemaContext oldSchemaConte
return builder.build().getSchemaContexts().values().iterator().next();
}

private SchemaContext getChangedSchemaContext(final SchemaContext oldSchemaContext, final Map<String, DataSourceConfiguration> newDataSources) throws Exception {
private SchemaContext getChangedSchemaContext(final SchemaContext oldSchemaContext, final Map<String, DataSourceConfiguration> newDataSources) throws SQLException {
Collection<String> deletedDataSources = getDeletedDataSources(oldSchemaContext, newDataSources);
Map<String, DataSource> modifiedDataSources = getModifiedDataSources(oldSchemaContext, newDataSources);
oldSchemaContext.getSchema().closeDataSources(deletedDataSources);
Expand Down Expand Up @@ -346,7 +346,7 @@ private Map<String, DataSource> getModifiedDataSources(final SchemaContext oldSc
return DataSourceConverter.getDataSourceMap(modifiedDataSourceConfigs);
}

private synchronized boolean isModifiedDataSource(final Map<String, DataSource> oldDataSources, final String newDataSourceName, final DataSourceConfiguration newDataSourceConfig) {
private boolean isModifiedDataSource(final Map<String, DataSource> oldDataSources, final String newDataSourceName, final DataSourceConfiguration newDataSourceConfig) {
DataSourceConfiguration dataSourceConfig = DataSourceConverter.getDataSourceConfigurationMap(oldDataSources).get(newDataSourceName);
return newDataSourceConfig.equals(dataSourceConfig);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;

Expand Down Expand Up @@ -149,7 +148,7 @@ public void assertIsCircuitBreak() {
@Test
@SneakyThrows(Exception.class)
public void assertSchemaAdd() {
SchemaAddedEvent event = new SchemaAddedEvent("schema_add", getDataSourceConfigurations(), new ArrayList<>());
SchemaAddedEvent event = new SchemaAddedEvent("schema_add", getDataSourceConfigurations(), new LinkedList<>());
governanceSchemaContexts.renew(event);
assertNotNull(governanceSchemaContexts.getSchemaContexts().get("schema_add"));
}
Expand All @@ -160,7 +159,7 @@ private Map<String, DataSourceConfiguration> getDataSourceConfigurations() {
dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
dataSource.setUsername("sa");
dataSource.setPassword("");
Map<String, DataSourceConfiguration> result = new LinkedHashMap<>();
Map<String, DataSourceConfiguration> result = new LinkedHashMap<>(3, 1);
result.put("ds_m", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
result.put("ds_0", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
result.put("ds_1", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
Expand Down Expand Up @@ -193,15 +192,15 @@ public void assertAuthenticationChanged() {

@Test
public void assertMetaDataChanged() {
MetaDataChangedEvent event = new MetaDataChangedEvent(Arrays.asList("schema_changed"), mock(RuleSchemaMetaData.class));
MetaDataChangedEvent event = new MetaDataChangedEvent(Collections.singletonList("schema_changed"), mock(RuleSchemaMetaData.class));
governanceSchemaContexts.renew(event);
assertTrue(governanceSchemaContexts.getSchemaContexts().keySet().contains("schema"));
assertFalse(governanceSchemaContexts.getSchemaContexts().keySet().contains("schema_changed"));
assertTrue(governanceSchemaContexts.getSchemaContexts().containsKey("schema"));
assertFalse(governanceSchemaContexts.getSchemaContexts().containsKey("schema_changed"));
}

@Test
public void assertMetaDataChangedWithExistSchema() {
MetaDataChangedEvent event = new MetaDataChangedEvent(Arrays.asList("schema"), mock(RuleSchemaMetaData.class));
MetaDataChangedEvent event = new MetaDataChangedEvent(Collections.singletonList("schema"), mock(RuleSchemaMetaData.class));
governanceSchemaContexts.renew(event);
assertThat(governanceSchemaContexts.getSchemaContexts().get("schema"), not(schemaContext));
}
Expand All @@ -210,7 +209,7 @@ public void assertMetaDataChangedWithExistSchema() {
@SneakyThrows(Exception.class)
public void assertRuleConfigurationsChanged() {
assertThat(governanceSchemaContexts.getSchemaContexts().get("schema"), is(schemaContext));
RuleConfigurationsChangedEvent event = new RuleConfigurationsChangedEvent("schema", new ArrayList<>());
RuleConfigurationsChangedEvent event = new RuleConfigurationsChangedEvent("schema", new LinkedList<>());
governanceSchemaContexts.renew(event);
assertThat(governanceSchemaContexts.getSchemaContexts().get("schema"), not(schemaContext));
}
Expand All @@ -227,7 +226,7 @@ public void assertDisableStateChanged() {
public void assertDataSourceChanged() {
DataSourceChangedEvent event = new DataSourceChangedEvent("schema", getChangedDataSourceConfigurations());
governanceSchemaContexts.renew(event);
assertTrue(governanceSchemaContexts.getSchemaContexts().get("schema").getSchema().getDataSources().keySet().contains("ds_2"));
assertTrue(governanceSchemaContexts.getSchemaContexts().get("schema").getSchema().getDataSources().containsKey("ds_2"));
}

private Map<String, DataSourceConfiguration> getChangedDataSourceConfigurations() {
Expand All @@ -236,7 +235,7 @@ private Map<String, DataSourceConfiguration> getChangedDataSourceConfigurations(
dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
dataSource.setUsername("sa");
dataSource.setPassword("");
Map<String, DataSourceConfiguration> result = new LinkedHashMap<>();
Map<String, DataSourceConfiguration> result = new LinkedHashMap<>(3, 1);
result.put("ds_m", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
result.put("ds_1", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
result.put("ds_2", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.driver.governance.internal.circuit.datasource.CircuitBreakerDataSource;
import org.apache.shardingsphere.driver.governance.internal.schema.JDBCGovernanceSchemaContexts;
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
import org.apache.shardingsphere.governance.core.config.ConfigCenter;
import org.apache.shardingsphere.governance.core.facade.GovernanceFacade;
import org.apache.shardingsphere.governance.core.schema.GovernanceSchemaContexts;
import org.apache.shardingsphere.governance.repository.api.config.GovernanceConfiguration;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.context.SchemaContexts;
import org.apache.shardingsphere.infra.context.SchemaContextsBuilder;
import org.apache.shardingsphere.infra.database.DefaultSchema;
Expand Down Expand Up @@ -68,14 +68,14 @@ public final class GovernanceShardingSphereDataSource extends AbstractUnsupporte

public GovernanceShardingSphereDataSource(final GovernanceConfiguration governanceConfig) throws SQLException {
GovernanceFacade governanceFacade = createGovernanceFacade(governanceConfig);
schemaContexts = new JDBCGovernanceSchemaContexts(createSchemaContexts(governanceFacade), governanceFacade);
schemaContexts = new GovernanceSchemaContexts(createSchemaContexts(governanceFacade), governanceFacade);
transactionContexts = createTransactionContexts(schemaContexts.getDatabaseType(), schemaContexts.getDefaultSchemaContext().getSchema().getDataSources());
}

public GovernanceShardingSphereDataSource(final Map<String, DataSource> dataSourceMap, final Collection<RuleConfiguration> ruleConfigurations,
final Properties props, final GovernanceConfiguration governanceConfig) throws SQLException {
GovernanceFacade governanceFacade = createGovernanceFacade(governanceConfig);
schemaContexts = new JDBCGovernanceSchemaContexts(createSchemaContexts(dataSourceMap, ruleConfigurations, props), governanceFacade);
schemaContexts = new GovernanceSchemaContexts(createSchemaContexts(dataSourceMap, ruleConfigurations, props), governanceFacade);
transactionContexts = createTransactionContexts(schemaContexts.getDatabaseType(), schemaContexts.getDefaultSchemaContext().getSchema().getDataSources());
uploadLocalConfiguration(governanceFacade);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@
import com.google.common.collect.ImmutableMap;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.apache.shardingsphere.driver.governance.internal.schema.JDBCGovernanceSchemaContexts;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.governance.core.common.event.datasource.DataSourceChangedEvent;
import org.apache.shardingsphere.governance.core.common.event.props.PropertiesChangedEvent;
import org.apache.shardingsphere.governance.core.common.event.rule.RuleConfigurationsChangedEvent;
import org.apache.shardingsphere.governance.core.registry.event.DisabledStateChangedEvent;
import org.apache.shardingsphere.governance.core.registry.schema.GovernanceSchema;
import org.apache.shardingsphere.governance.core.schema.GovernanceSchemaContexts;
import org.apache.shardingsphere.governance.repository.api.config.GovernanceCenterConfiguration;
import org.apache.shardingsphere.governance.repository.api.config.GovernanceConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.context.SchemaContexts;
import org.apache.shardingsphere.infra.database.DefaultSchema;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
Expand Down Expand Up @@ -58,14 +58,14 @@

public final class GovernanceShardingSphereDataSourceTest {

private static JDBCGovernanceSchemaContexts governanceSchemaContexts;
private static GovernanceSchemaContexts governanceSchemaContexts;

@BeforeClass
public static void setUp() throws SQLException, IOException, URISyntaxException {
SchemaContexts schemaContexts = getShardingSphereDataSource().getSchemaContexts();
GovernanceShardingSphereDataSource governanceDataSource = new GovernanceShardingSphereDataSource(schemaContexts.getDefaultSchemaContext().getSchema().getDataSources(),
schemaContexts.getDefaultSchemaContext().getSchema().getConfigurations(), schemaContexts.getProps().getProps(), getGovernanceConfiguration());
governanceSchemaContexts = (JDBCGovernanceSchemaContexts) governanceDataSource.getSchemaContexts();
governanceSchemaContexts = (GovernanceSchemaContexts) governanceDataSource.getSchemaContexts();
}

private static ShardingSphereDataSource getShardingSphereDataSource() throws IOException, SQLException, URISyntaxException {
Expand All @@ -91,12 +91,11 @@ private static GovernanceCenterConfiguration getConfigCenterConfiguration() {

@Test
public void assertInitializeGovernanceShardingSphereDataSource() throws SQLException {
GovernanceShardingSphereDataSource governanceShardingSphereDataSource = new GovernanceShardingSphereDataSource(getGovernanceConfiguration());
assertThat(governanceShardingSphereDataSource.getConnection(), instanceOf(Connection.class));
assertThat(new GovernanceShardingSphereDataSource(getGovernanceConfiguration()).getConnection(), instanceOf(Connection.class));
}

@Test
public void assertRenewRules() throws Exception {
public void assertRenewRules() throws SQLException {
governanceSchemaContexts.renew(new RuleConfigurationsChangedEvent(DefaultSchema.LOGIC_NAME, Arrays.asList(getShardingRuleConfiguration(), getMasterSlaveRuleConfiguration())));
assertThat(((ShardingRule) governanceSchemaContexts.getDefaultSchemaContext().getSchema().getRules().iterator().next()).getTableRules().size(), is(1));
}
Expand Down Expand Up @@ -126,7 +125,7 @@ private Map<String, DataSourceConfiguration> getDataSourceConfigurations() {
dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
dataSource.setUsername("sa");
dataSource.setPassword("");
Map<String, DataSourceConfiguration> result = new LinkedHashMap<>();
Map<String, DataSourceConfiguration> result = new LinkedHashMap<>(3, 1);
result.put("ds_m", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
result.put("ds_s", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
result.put("ds_0", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
Expand Down
Loading

0 comments on commit c4f7b47

Please sign in to comment.