Skip to content

Commit

Permalink
Use ShardingSphereSchema.name instead of SchemaContext.name (#7674)
Browse files Browse the repository at this point in the history
* Add ShardingSphereSchema.name

* Use ShardingSphereSchema.name instead of SchemaContext.name

* Use RETURNS_DEEP_STUBS ti simply mock

* Use RETURNS_DEEP_STUBS ti simply mock

* Use RETURNS_DEEP_STUBS ti simply mock

* Use RETURNS_DEEP_STUBS ti simply mock

* Use RETURNS_DEEP_STUBS ti simply mock

* Remove SchemaContext.name

* Remove SchemaContext.name

* Add javadoc
  • Loading branch information
terrymanu committed Sep 30, 2020
1 parent 8ecc928 commit 6ee78a6
Show file tree
Hide file tree
Showing 27 changed files with 159 additions and 198 deletions.
Expand Up @@ -211,7 +211,7 @@ public synchronized void renew(final MetaDataChangedEvent event) {
String schemaName = entry.getKey();
SchemaContext oldSchemaContext = entry.getValue();
SchemaContext newSchemaContext = event.getSchemaNames().contains(schemaName)
? new SchemaContext(oldSchemaContext.getName(), getChangedShardingSphereSchema(oldSchemaContext.getSchema(), event.getRuleSchemaMetaData(), schemaName),
? new SchemaContext(getChangedShardingSphereSchema(oldSchemaContext.getSchema(), event.getRuleSchemaMetaData(), schemaName),
oldSchemaContext.getRuntimeContext()) : oldSchemaContext;
newSchemaContexts.put(schemaName, newSchemaContext);
}
Expand Down Expand Up @@ -292,21 +292,21 @@ private Map<String, SchemaContext> getChangedSchemaContexts(final ConfigurationP
Map<String, SchemaContext> result = new HashMap<>(schemaContexts.getSchemaContextMap().size());
for (Entry<String, SchemaContext> entry : schemaContexts.getSchemaContextMap().entrySet()) {
RuntimeContext runtimeContext = entry.getValue().getRuntimeContext();
result.put(entry.getKey(), new SchemaContext(entry.getValue().getName(), entry.getValue().getSchema(), new RuntimeContext(runtimeContext.getCachedDatabaseMetaData(),
result.put(entry.getKey(), new SchemaContext(entry.getValue().getSchema(), new RuntimeContext(runtimeContext.getCachedDatabaseMetaData(),
new ExecutorKernel(props.<Integer>getValue(ConfigurationPropertyKey.EXECUTOR_SIZE)), runtimeContext.getSqlParserEngine())));
}
return result;
}

private ShardingSphereSchema getChangedShardingSphereSchema(final ShardingSphereSchema oldShardingSphereSchema, final RuleSchemaMetaData newRuleSchemaMetaData, final String schemaName) {
ShardingSphereMetaData metaData = new ShardingSphereMetaData(oldShardingSphereSchema.getMetaData().getDataSourceMetaDatas(), newRuleSchemaMetaData, schemaName);
return new ShardingSphereSchema(oldShardingSphereSchema.getConfigurations(), oldShardingSphereSchema.getRules(), oldShardingSphereSchema.getDataSources(), metaData);
return new ShardingSphereSchema(schemaName, oldShardingSphereSchema.getConfigurations(), oldShardingSphereSchema.getRules(), oldShardingSphereSchema.getDataSources(), metaData);
}

private SchemaContext getChangedSchemaContext(final SchemaContext oldSchemaContext, final Collection<RuleConfiguration> ruleConfigs) throws SQLException {
ShardingSphereSchema oldSchema = oldSchemaContext.getSchema();
SchemaContextsBuilder builder = new SchemaContextsBuilder(schemaContexts.getDatabaseType(), Collections.singletonMap(oldSchemaContext.getName(), oldSchema.getDataSources()),
Collections.singletonMap(oldSchemaContext.getName(), ruleConfigs), schemaContexts.getAuthentication(), schemaContexts.getProps().getProps());
SchemaContextsBuilder builder = new SchemaContextsBuilder(schemaContexts.getDatabaseType(), Collections.singletonMap(oldSchemaContext.getSchema().getName(), oldSchema.getDataSources()),
Collections.singletonMap(oldSchemaContext.getSchema().getName(), ruleConfigs), schemaContexts.getAuthentication(), schemaContexts.getProps().getProps());
return builder.build().getSchemaContextMap().values().iterator().next();
}

Expand All @@ -315,11 +315,11 @@ private SchemaContext getChangedSchemaContext(final SchemaContext oldSchemaConte
Map<String, DataSource> modifiedDataSources = getModifiedDataSources(oldSchemaContext, newDataSourceConfigs);
oldSchemaContext.getSchema().closeDataSources(deletedDataSources);
oldSchemaContext.getSchema().closeDataSources(modifiedDataSources.keySet());
Map<String, Map<String, DataSource>> dataSourcesMap = Collections.singletonMap(oldSchemaContext.getName(),
Map<String, Map<String, DataSource>> dataSourcesMap = Collections.singletonMap(oldSchemaContext.getSchema().getName(),
getNewDataSources(oldSchemaContext.getSchema().getDataSources(), getAddedDataSources(oldSchemaContext, newDataSourceConfigs), modifiedDataSources, deletedDataSources));
return new SchemaContextsBuilder(schemaContexts.getDatabaseType(), dataSourcesMap,
Collections.singletonMap(oldSchemaContext.getName(), oldSchemaContext.getSchema().getConfigurations()), schemaContexts.getAuthentication(),
schemaContexts.getProps().getProps()).build().getSchemaContextMap().get(oldSchemaContext.getName());
Collections.singletonMap(oldSchemaContext.getSchema().getName(), oldSchemaContext.getSchema().getConfigurations()), schemaContexts.getAuthentication(),
schemaContexts.getProps().getProps()).build().getSchemaContextMap().get(oldSchemaContext.getSchema().getName());
}

private Map<String, DataSource> getNewDataSources(final Map<String, DataSource> oldDataSources,
Expand Down
Expand Up @@ -36,9 +36,9 @@
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.context.schema.SchemaContext;
import org.apache.shardingsphere.infra.context.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.context.schema.impl.StandardSchemaContexts;
import org.apache.shardingsphere.infra.context.schema.runtime.RuntimeContext;
import org.apache.shardingsphere.infra.context.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
Expand Down Expand Up @@ -75,6 +75,10 @@
@RunWith(MockitoJUnitRunner.class)
public final class GovernanceSchemaContextsTest {

private final Authentication authentication = new Authentication();

private final ConfigurationProperties props = new ConfigurationProperties(new Properties());

@Mock
private DatabaseType databaseType;

Expand All @@ -93,10 +97,6 @@ public final class GovernanceSchemaContextsTest {
@Mock
private PrimaryReplicaReplicationRule primaryReplicaReplicationRule;

private Authentication authentication = new Authentication();

private ConfigurationProperties configurationProperties = new ConfigurationProperties(new Properties());

private GovernanceSchemaContexts governanceSchemaContexts;

@Before
Expand All @@ -106,16 +106,16 @@ public void setUp() {
when(governanceFacade.getRegistryCenter()).thenReturn(registryCenter);
when(governanceFacade.getConfigCenter()).thenReturn(configCenter);
when(registryCenter.loadDisabledDataSources("schema")).thenReturn(Collections.singletonList("schema.ds_1"));
governanceSchemaContexts = new GovernanceSchemaContexts(new StandardSchemaContexts(getSchemaContextMap(), authentication, configurationProperties, databaseType), governanceFacade);
governanceSchemaContexts = new GovernanceSchemaContexts(new StandardSchemaContexts(createSchemaContextMap(), authentication, props, databaseType), governanceFacade);
}

@SneakyThrows
private Map<String, SchemaContext> getSchemaContextMap() {
private Map<String, SchemaContext> createSchemaContextMap() {
ShardingSphereSchema shardingSphereSchema = mock(ShardingSphereSchema.class);
ShardingSphereMetaData shardingSphereMetaData = mock(ShardingSphereMetaData.class);
RuntimeContext runtimeContext = mock(RuntimeContext.class);
when(schemaContext.getName()).thenReturn("schema");
when(schemaContext.getSchema()).thenReturn(shardingSphereSchema);
when(schemaContext.getSchema().getName()).thenReturn("schema");
when(schemaContext.getRuntimeContext()).thenReturn(runtimeContext);
when(shardingSphereSchema.getMetaData()).thenReturn(shardingSphereMetaData);
when(shardingSphereSchema.getRules()).thenReturn(Collections.singletonList(primaryReplicaReplicationRule));
Expand Down Expand Up @@ -144,7 +144,7 @@ public void assertGetAuthentication() {

@Test
public void assertGetProps() {
assertThat(governanceSchemaContexts.getProps(), is(configurationProperties));
assertThat(governanceSchemaContexts.getProps(), is(props));
}

@Test
Expand Down
Expand Up @@ -28,8 +28,6 @@
@Getter
public final class SchemaContext {

private final String name;

private final ShardingSphereSchema schema;

private final RuntimeContext runtimeContext;
Expand Down
Expand Up @@ -99,7 +99,7 @@ private SchemaContext createSchemaContext(final String schemaName) throws SQLExc
Map<String, DataSource> dataSources = this.dataSources.get(schemaName);
RuntimeContext runtimeContext = new RuntimeContext(createCachedDatabaseMetaData(dataSources).orElse(null),
executorKernel, ShardingSphereSQLParserEngineFactory.getSQLParserEngine(DatabaseTypes.getTrunkDatabaseTypeName(databaseType)));
return new SchemaContext(schemaName, createShardingSphereSchema(schemaName), runtimeContext);
return new SchemaContext(createShardingSphereSchema(schemaName), runtimeContext);
}

private Optional<CachedDatabaseMetaData> createCachedDatabaseMetaData(final Map<String, DataSource> dataSources) throws SQLException {
Expand All @@ -115,7 +115,7 @@ private ShardingSphereSchema createShardingSphereSchema(final String schemaName)
Map<String, DataSource> dataSources = this.dataSources.get(schemaName);
Collection<RuleConfiguration> ruleConfigs = this.ruleConfigs.get(schemaName);
Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(ruleConfigs, dataSources.keySet());
return new ShardingSphereSchema(ruleConfigs, rules, dataSources, createMetaData(schemaName, dataSources, rules));
return new ShardingSphereSchema(schemaName, ruleConfigs, rules, dataSources, createMetaData(schemaName, dataSources, rules));
}

private ShardingSphereMetaData createMetaData(final String schemaName, final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> rules) throws SQLException {
Expand Down
Expand Up @@ -35,6 +35,8 @@
@Getter
public final class ShardingSphereSchema {

private final String name;

private final Collection<RuleConfiguration> configurations;

private final Collection<ShardingSphereRule> rules;
Expand Down
Expand Up @@ -22,34 +22,34 @@
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.junit.Test;
import org.mockito.Mockito;

import javax.sql.DataSource;
import java.util.Collections;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

public final class SchemaContextTest {

@Test
public void assertIsComplete() {
ShardingSphereSchema schema = new ShardingSphereSchema(Collections.singleton(Mockito.mock(RuleConfiguration.class)),
Collections.singleton(Mockito.mock(ShardingSphereRule.class)), Collections.singletonMap("ds", Mockito.mock(DataSource.class)), Mockito.mock(ShardingSphereMetaData.class));
assertTrue(new SchemaContext("name", schema, Mockito.mock(RuntimeContext.class)).isComplete());
ShardingSphereSchema schema = new ShardingSphereSchema("name", Collections.singleton(mock(RuleConfiguration.class)),
Collections.singleton(mock(ShardingSphereRule.class)), Collections.singletonMap("ds", mock(DataSource.class)), mock(ShardingSphereMetaData.class));
assertTrue(new SchemaContext(schema, mock(RuntimeContext.class)).isComplete());
}

@Test
public void assertIsNotCompleteWithoutRule() {
ShardingSphereSchema schema = new ShardingSphereSchema(Collections.emptyList(),
Collections.emptyList(), Collections.singletonMap("ds", Mockito.mock(DataSource.class)), Mockito.mock(ShardingSphereMetaData.class));
assertFalse(new SchemaContext("name", schema, Mockito.mock(RuntimeContext.class)).isComplete());
ShardingSphereSchema schema = new ShardingSphereSchema("name", Collections.emptyList(),
Collections.emptyList(), Collections.singletonMap("ds", mock(DataSource.class)), mock(ShardingSphereMetaData.class));
assertFalse(new SchemaContext(schema, mock(RuntimeContext.class)).isComplete());
}

@Test
public void assertIsNotCompleteWithoutDataSource() {
ShardingSphereSchema schema = new ShardingSphereSchema(Collections.singleton(Mockito.mock(RuleConfiguration.class)),
Collections.singleton(Mockito.mock(ShardingSphereRule.class)), Collections.emptyMap(), Mockito.mock(ShardingSphereMetaData.class));
assertFalse(new SchemaContext("name", schema, Mockito.mock(RuntimeContext.class)).isComplete());
ShardingSphereSchema schema = new ShardingSphereSchema("name", Collections.singleton(mock(RuleConfiguration.class)),
Collections.singleton(mock(ShardingSphereRule.class)), Collections.emptyMap(), mock(ShardingSphereMetaData.class));
assertFalse(new SchemaContext(schema, mock(RuntimeContext.class)).isComplete());
}
}
Expand Up @@ -25,6 +25,8 @@
import org.mockito.junit.MockitoJUnitRunner;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -47,8 +49,8 @@ public final class ShardingSphereSchemaTest {
private DataSource dataSource2;

@Test
public void assertCloseDataSources() throws Exception {
new ShardingSphereSchema(Collections.emptyList(), Collections.emptyList(), createDataSources(), mock(ShardingSphereMetaData.class)).closeDataSources(Arrays.asList("ds_0", "ds_2"));
public void assertCloseDataSources() throws SQLException, IOException {
new ShardingSphereSchema("name", Collections.emptyList(), Collections.emptyList(), createDataSources(), mock(ShardingSphereMetaData.class)).closeDataSources(Arrays.asList("ds_0", "ds_2"));
verify(dataSource0).close();
verify(dataSource1, times(0)).close();
}
Expand Down
Expand Up @@ -51,7 +51,7 @@ public void assertClose() {
RuntimeContext runtimeContext = mock(RuntimeContext.class, RETURNS_DEEP_STUBS);
ExecutorKernel executorKernel = mock(ExecutorKernel.class);
when(runtimeContext.getExecutorKernel()).thenReturn(executorKernel);
SchemaContext schemaContext = new SchemaContext("logic_db", mock(ShardingSphereSchema.class), runtimeContext);
SchemaContext schemaContext = new SchemaContext(mock(ShardingSphereSchema.class), runtimeContext);
StandardSchemaContexts standardSchemaContexts = new StandardSchemaContexts(
Collections.singletonMap("logic_db", schemaContext), new Authentication(), new ConfigurationProperties(new Properties()), mock(DatabaseType.class));
standardSchemaContexts.close();
Expand Down

0 comments on commit 6ee78a6

Please sign in to comment.