Skip to content

Commit

Permalink
Remove ResourceMetaData.close (#28600)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Sep 27, 2023
1 parent ecaa6d9 commit be9b6fb
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -112,6 +113,6 @@ private void closeResources(final ShardingSphereDatabase database) {
database.getRuleMetaData().findRules(ResourceHeldRule.class).forEach(each -> each.closeStaleResource(databaseName));
database.getRuleMetaData().findSingleRule(StaticDataSourceContainedRule.class).ifPresent(StaticDataSourceContainedRule::cleanStorageNodeDataSources);
Optional.ofNullable(database.getResourceMetaData())
.ifPresent(optional -> optional.getStorageUnitMetaData().getStorageUnits().values().forEach(each -> database.getResourceMetaData().close(each.getDataSource())));
.ifPresent(optional -> optional.getStorageUnitMetaData().getStorageUnits().values().forEach(each -> new DataSourcePoolDestroyer(each.getDataSource()).asyncDestroy()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
import lombok.Getter;
import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
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;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData;
Expand Down Expand Up @@ -112,13 +111,4 @@ public DatabaseType getStorageType(final String dataSourceName) {
public Collection<String> getNotExistedDataSources(final Collection<String> resourceNames) {
return resourceNames.stream().filter(each -> !storageUnitMetaData.getStorageUnits().containsKey(each)).collect(Collectors.toSet());
}

/**
* Close data source.
*
* @param dataSource data source to be closed
*/
public void close(final DataSource dataSource) {
new DataSourcePoolDestroyer(dataSource).asyncDestroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoSettings;
Expand All @@ -37,6 +38,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -69,14 +71,15 @@ void assertAddDatabase() {
@Test
void assertDropDatabase() {
ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS);
DataSource dataSource = new MockedDataSource();
MockedDataSource dataSource = new MockedDataSource();
ResourceHeldRule<?> databaseResourceHeldRule = mock(ResourceHeldRule.class);
ResourceHeldRule<?> globalResourceHeldRule = mock(ResourceHeldRule.class);
ShardingSphereMetaData metaData = new ShardingSphereMetaData(new HashMap<>(Collections.singletonMap("foo_db", mockDatabase(resourceMetaData, dataSource, databaseResourceHeldRule))),
mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(globalResourceHeldRule)), new ConfigurationProperties(new Properties()));
metaData.dropDatabase("foo_db");
assertTrue(metaData.getDatabases().isEmpty());
verify(resourceMetaData).close(dataSource);
Awaitility.await().pollDelay(10L, TimeUnit.MILLISECONDS).until(dataSource::isClosed);
assertTrue(dataSource.isClosed());
verify(databaseResourceHeldRule).closeStaleResource("foo_db");
verify(globalResourceHeldRule).closeStaleResource("foo_db");
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource;
Expand Down Expand Up @@ -47,6 +48,6 @@ public final class SwitchingResource {
* Close stale data sources.
*/
public void closeStaleDataSources() {
staleStorageResource.getDataSourceMap().values().stream().filter(Objects::nonNull).forEach(resourceMetaData::close);
staleStorageResource.getDataSourceMap().values().stream().filter(Objects::nonNull).forEach(each -> new DataSourcePoolDestroyer(each).asyncDestroy());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@

package org.apache.shardingsphere.mode.manager.switcher;

import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

class SwitchingResourceTest {

Expand All @@ -37,6 +39,7 @@ void assertCloseStaleDataSources() {
StorageResource newStorageResource = new StorageResource(Collections.singletonMap(new StorageNode("new_ds"), new MockedDataSource()), Collections.emptyMap());
StorageResource staleStorageResource = new StorageResource(Collections.singletonMap(new StorageNode("stale_ds"), staleDataSource), Collections.emptyMap());
new SwitchingResource(resourceMetaData, newStorageResource, staleStorageResource, Collections.emptyMap()).closeStaleDataSources();
verify(resourceMetaData).close(staleDataSource);
Awaitility.await().pollDelay(10L, TimeUnit.MILLISECONDS).until(staleDataSource::isClosed);
assertTrue(staleDataSource.isClosed());
}
}

0 comments on commit be9b6fb

Please sign in to comment.