From 615c960887e5c6b4c43eb721e62dfcce4c7c8ffb Mon Sep 17 00:00:00 2001 From: zhangliang Date: Wed, 27 Sep 2023 16:16:38 +0800 Subject: [PATCH] Remove ResourceMetaData.close --- .../metadata/ShardingSphereMetaData.java | 3 +- .../database/resource/ResourceMetaData.java | 12 +----- .../metadata/ShardingSphereMetaDataTest.java | 7 +++- .../resource/ShardingSphereResourceTest.java | 38 ------------------- .../manager/switcher/SwitchingResource.java | 3 +- .../switcher/SwitchingResourceTest.java | 11 ++++-- 6 files changed, 17 insertions(+), 57 deletions(-) delete mode 100644 infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java index 6e96d54075deb..648bb580df4c5 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java @@ -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; @@ -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())); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java index c92a0f6aa0995..2d0fd6f44a5f1 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java @@ -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; @@ -112,13 +111,4 @@ public DatabaseType getStorageType(final String dataSourceName) { public Collection getNotExistedDataSources(final Collection 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(); - } } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java index 28980753c5663..9220183637953 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java @@ -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; @@ -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; @@ -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"); } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java deleted file mode 100644 index bd313fe248d25..0000000000000 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ShardingSphereResourceTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.metadata.database.resource; - -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; - -class ShardingSphereResourceTest { - - @Test - void assertClose() { - MockedDataSource dataSource = new MockedDataSource(); - new ResourceMetaData("sharding_db", Collections.singletonMap("foo_ds", dataSource)).close(dataSource); - Awaitility.await().atMost(1L, TimeUnit.MINUTES).pollInterval(10L, TimeUnit.MILLISECONDS).until(dataSource::isClosed); - assertTrue(dataSource.isClosed()); - } -} diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResource.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResource.java index 096615fe80b8a..2c2459e07e6cb 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResource.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResource.java @@ -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; @@ -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()); } } diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResourceTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResourceTest.java index 5d91cb5872d81..59453e4936992 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResourceTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResourceTest.java @@ -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 { @@ -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()); } }