Skip to content

Commit

Permalink
Merge master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojinchao95 committed Mar 18, 2024
2 parents 2882964 + bd2977d commit 9cc4863
Show file tree
Hide file tree
Showing 26 changed files with 204 additions and 388 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
package org.apache.shardingsphere.infra.metadata;

import lombok.Getter;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.temporary.TemporaryConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute;
import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute;
import org.apache.shardingsphere.infra.rule.scope.GlobalRule;

import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -95,7 +97,7 @@ public ShardingSphereDatabase getDatabase(final String databaseName) {
public void addDatabase(final String databaseName, final DatabaseType protocolType, final ConfigurationProperties props) {
ShardingSphereDatabase database = ShardingSphereDatabase.create(databaseName, protocolType, props);
databases.put(database.getName().toLowerCase(), database);
globalRuleMetaData.getAttributes(ResourceHeldRuleAttribute.class).forEach(each -> each.addResource(database));
globalRuleMetaData.getRules().forEach(each -> ((GlobalRule) each).refresh(databases));
}

/**
Expand All @@ -107,9 +109,14 @@ public void dropDatabase(final String databaseName) {
cleanResources(databases.remove(databaseName.toLowerCase()));
}

@SneakyThrows(Exception.class)
private void cleanResources(final ShardingSphereDatabase database) {
globalRuleMetaData.getAttributes(ResourceHeldRuleAttribute.class).forEach(each -> each.closeStaleResource(database.getName()));
database.getRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class).forEach(each -> each.closeStaleResource(database.getName()));
globalRuleMetaData.getRules().forEach(each -> ((GlobalRule) each).refresh(databases));
for (ShardingSphereRule each : database.getRuleMetaData().getRules()) {
if (each instanceof AutoCloseable) {
((AutoCloseable) each).close();
}
}
database.getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class).forEach(StaticDataSourceRuleAttribute::cleanStorageNodeDataSources);
Optional.ofNullable(database.getResourceMetaData())
.ifPresent(optional -> optional.getStorageUnits().values().forEach(each -> new DataSourcePoolDestroyer(each.getDataSource()).asyncDestroy()));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@

package org.apache.shardingsphere.infra.rule.scope;

import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;

import java.util.Map;

/**
* Global rule.
*/
public interface GlobalRule extends ShardingSphereRule {

/**
* Refresh rule when databases changed.
*
* @param databases changed databases
*/
default void refresh(final Map<String, ShardingSphereDatabase> databases) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
Expand Down Expand Up @@ -59,9 +59,7 @@ class ShardingSphereMetaDataTest {

@Test
void assertAddDatabase() {
ResourceHeldRuleAttribute<?> globalResourceHeldRuleAttribute = mock(ResourceHeldRuleAttribute.class);
ShardingSphereRule globalRule = mock(ShardingSphereRule.class);
when(globalRule.getAttributes()).thenReturn(new RuleAttributes(globalResourceHeldRuleAttribute));
GlobalRule globalRule = mock(GlobalRule.class);
ShardingSphereDatabase database = mockDatabase(mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new MockedDataSource(), globalRule);
DatabaseType databaseType = mock(DatabaseType.class);
ConfigurationProperties configProps = new ConfigurationProperties(new Properties());
Expand All @@ -70,34 +68,28 @@ void assertAddDatabase() {
ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(globalRule)), configProps);
metaData.addDatabase("foo_db", databaseType, configProps);
assertThat(metaData.getDatabases(), is(databases));
verify(globalResourceHeldRuleAttribute).addResource(database);
verify(globalRule).refresh(databases);
}

@Test
void assertDropDatabase() {
ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS);
GlobalRule globalRule = mock(GlobalRule.class);
MockedDataSource dataSource = new MockedDataSource();
ResourceHeldRuleAttribute<?> databaseResourceHeldRuleAttribute = mock(ResourceHeldRuleAttribute.class, RETURNS_DEEP_STUBS);
ResourceHeldRuleAttribute<?> globalResourceHeldRuleAttribute = mock(ResourceHeldRuleAttribute.class);
ShardingSphereRule databaseRule = mock(ShardingSphereRule.class);
when(databaseRule.getAttributes()).thenReturn(new RuleAttributes(databaseResourceHeldRuleAttribute));
ShardingSphereRule globalRule = mock(ShardingSphereRule.class);
when(globalRule.getAttributes()).thenReturn(new RuleAttributes(globalResourceHeldRuleAttribute));
when(databaseRule.getAttributes()).thenReturn(new RuleAttributes());
ShardingSphereMetaData metaData = new ShardingSphereMetaData(new HashMap<>(Collections.singletonMap("foo_db", mockDatabase(resourceMetaData, dataSource, databaseRule))),
mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(globalRule)), new ConfigurationProperties(new Properties()));
metaData.dropDatabase("foo_db");
assertTrue(metaData.getDatabases().isEmpty());
Awaitility.await().pollDelay(10L, TimeUnit.MILLISECONDS).until(dataSource::isClosed);
assertTrue(dataSource.isClosed());
verify(databaseResourceHeldRuleAttribute).closeStaleResource("foo_db");
verify(globalResourceHeldRuleAttribute).closeStaleResource("foo_db");
verify(globalRule).refresh(metaData.getDatabases());
}

@Test
void assertContainsDatabase() {
ResourceHeldRuleAttribute<?> globalResourceHeldRuleAttribute = mock(ResourceHeldRuleAttribute.class);
ShardingSphereRule globalRule = mock(ShardingSphereRule.class);
when(globalRule.getAttributes()).thenReturn(new RuleAttributes(globalResourceHeldRuleAttribute));
ShardingSphereDatabase database = mockDatabase(mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new MockedDataSource(), globalRule);
Map<String, ShardingSphereDatabase> databases = new HashMap<>(Collections.singletonMap("foo_db", database));
ConfigurationProperties configProps = new ConfigurationProperties(new Properties());
Expand All @@ -107,9 +99,7 @@ void assertContainsDatabase() {

@Test
void assertGetDatabase() {
ResourceHeldRuleAttribute<?> globalResourceHeldRuleAttribute = mock(ResourceHeldRuleAttribute.class);
ShardingSphereRule globalRule = mock(ShardingSphereRule.class);
when(globalRule.getAttributes()).thenReturn(new RuleAttributes(globalResourceHeldRuleAttribute));
ShardingSphereDatabase database = mockDatabase(mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new MockedDataSource(), globalRule);
Map<String, ShardingSphereDatabase> databases = new HashMap<>(Collections.singletonMap("foo_db", database));
ConfigurationProperties configProps = new ConfigurationProperties(new Properties());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.traffic.rule.builder.DefaultTrafficRuleConfigurationBuilder;
import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -99,15 +96,10 @@ void assertSetSchema() throws SQLException {
}
}

@SuppressWarnings("unchecked")
private Connection createConnectionAdaptor() {
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
TransactionRule transactionRule = mock(TransactionRule.class);
ResourceHeldRuleAttribute<ShardingSphereTransactionManagerEngine> resourceHeldRuleAttribute = mock(ResourceHeldRuleAttribute.class);
when(resourceHeldRuleAttribute.getResource()).thenReturn(mock(ShardingSphereTransactionManagerEngine.class));
when(transactionRule.getAttributes()).thenReturn(new RuleAttributes(resourceHeldRuleAttribute));
when(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(
new RuleMetaData(Arrays.asList(transactionRule, new TrafficRule(new DefaultTrafficRuleConfigurationBuilder().build()))));
new RuleMetaData(Arrays.asList(mock(TransactionRule.class, RETURNS_DEEP_STUBS), new TrafficRule(new DefaultTrafficRuleConfigurationBuilder().build()))));
return new ShardingSphereConnection(DefaultDatabase.LOGIC_NAME, contextManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute;
import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -76,19 +73,14 @@ void setUp() throws SQLException {
databaseConnectionManager = new DriverDatabaseConnectionManager(DefaultDatabase.LOGIC_NAME, mockContextManager());
}

@SuppressWarnings("unchecked")
private ContextManager mockContextManager() throws SQLException {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
Map<String, StorageUnit> storageUnits = mockStorageUnits();
when(result.getStorageUnits(DefaultDatabase.LOGIC_NAME)).thenReturn(storageUnits);
MetaDataPersistService persistService = mockMetaDataPersistService();
when(result.getMetaDataContexts().getPersistService()).thenReturn(persistService);
TransactionRule transactionRule = mock(TransactionRule.class);
ResourceHeldRuleAttribute<ShardingSphereTransactionManagerEngine> resourceHeldRuleAttribute = mock(ResourceHeldRuleAttribute.class);
when(resourceHeldRuleAttribute.getResource()).thenReturn(mock(ShardingSphereTransactionManagerEngine.class));
when(transactionRule.getAttributes()).thenReturn(new RuleAttributes(resourceHeldRuleAttribute));
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(
new RuleMetaData(Arrays.asList(transactionRule, mock(TrafficRule.class, RETURNS_DEEP_STUBS))));
new RuleMetaData(Arrays.asList(mock(TransactionRule.class, RETURNS_DEEP_STUBS), mock(TrafficRule.class, RETURNS_DEEP_STUBS))));
when(result.getInstanceContext().getAllClusterInstances(InstanceType.PROXY, Arrays.asList("OLTP", "OLAP"))).thenReturn(
Collections.singletonMap("foo_id", new ProxyInstanceMetaData("foo_id", "127.0.0.1@3307", "foo_version")));
Map<String, DataSource> trafficDataSourceMap = mockTrafficDataSourceMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@

import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.traffic.rule.TrafficRule;
import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.Test;

Expand All @@ -42,14 +39,10 @@ class UnsupportedOperationConnectionTest {

private final ShardingSphereConnection shardingSphereConnection;

@SuppressWarnings("unchecked")
UnsupportedOperationConnectionTest() {
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
TransactionRule transactionRule = mock(TransactionRule.class);
ResourceHeldRuleAttribute<ShardingSphereTransactionManagerEngine> resourceHeldRuleAttribute = mock(ResourceHeldRuleAttribute.class);
when(resourceHeldRuleAttribute.getResource()).thenReturn(mock(ShardingSphereTransactionManagerEngine.class));
when(transactionRule.getAttributes()).thenReturn(new RuleAttributes(resourceHeldRuleAttribute));
when(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Arrays.asList(transactionRule, mock(TrafficRule.class))));
when(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(
new RuleMetaData(Arrays.asList(mock(TransactionRule.class, RETURNS_DEEP_STUBS), mock(TrafficRule.class))));
shardingSphereConnection = new ShardingSphereConnection(DefaultDatabase.LOGIC_NAME, contextManager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.state.cluster.ClusterStateContext;
import org.apache.shardingsphere.infra.state.instance.InstanceStateContext;
Expand Down Expand Up @@ -60,14 +58,11 @@ class DriverStateContextTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ContextManager contextManager;

@SuppressWarnings("unchecked")
@BeforeEach
void setUp() {
Map<String, ShardingSphereDatabase> databases = mockDatabases();
TransactionRule transactionRule = mock(TransactionRule.class);
ResourceHeldRuleAttribute<ShardingSphereTransactionManagerEngine> resourceHeldRuleAttribute = mock(ResourceHeldRuleAttribute.class);
when(resourceHeldRuleAttribute.getResource()).thenReturn(mock(ShardingSphereTransactionManagerEngine.class));
when(transactionRule.getAttributes()).thenReturn(new RuleAttributes(resourceHeldRuleAttribute));
when(transactionRule.getResource()).thenReturn(mock(ShardingSphereTransactionManagerEngine.class));
TrafficRule trafficRule = mock(TrafficRule.class);
RuleMetaData globalRuleMetaData = new RuleMetaData(Arrays.asList(transactionRule, trafficRule));
MetaDataContexts metaDataContexts = new MetaDataContexts(
Expand Down
Loading

0 comments on commit 9cc4863

Please sign in to comment.