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 e9ed36bdab21d..46c5b927469fa 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 @@ -18,6 +18,7 @@ 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; @@ -25,8 +26,9 @@ 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; @@ -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)); } /** @@ -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())); diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/attribute/resoure/ResourceHeldRuleAttribute.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/attribute/resoure/ResourceHeldRuleAttribute.java deleted file mode 100644 index ca66fdc423b3c..0000000000000 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/attribute/resoure/ResourceHeldRuleAttribute.java +++ /dev/null @@ -1,55 +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.rule.attribute.resoure; - -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.rule.attribute.RuleAttribute; - -/** - * Resource held rule attribute. - * - * @param type of resource - */ -public interface ResourceHeldRuleAttribute extends RuleAttribute { - - /** - * Get resource. - * - * @return got resource - */ - T getResource(); - - /** - * Add resource. - * - * @param database database - */ - void addResource(ShardingSphereDatabase database); - - /** - * Close stale resource with database name. - * - * @param databaseName database name - */ - void closeStaleResource(String databaseName); - - /** - * Close stale resource. - */ - void closeStaleResource(); -} diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/scope/GlobalRule.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/scope/GlobalRule.java index fbe266c21440d..f18df6a6cd592 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/scope/GlobalRule.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/rule/scope/GlobalRule.java @@ -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 databases) { + } } 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 ff29ba8424e06..97f7c6e79267c 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 @@ -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; @@ -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()); @@ -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 databases = new HashMap<>(Collections.singletonMap("foo_db", database)); ConfigurationProperties configProps = new ConfigurationProperties(new Properties()); @@ -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 databases = new HashMap<>(Collections.singletonMap("foo_db", database)); ConfigurationProperties configProps = new ConfigurationProperties(new Properties()); diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ConnectionAdapterTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ConnectionAdapterTest.java index 923c9d4b8be35..69c972de9b33a 100644 --- a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ConnectionAdapterTest.java +++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/ConnectionAdapterTest.java @@ -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; @@ -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 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); } } diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java index 1b4ce67dddc67..51a976565081a 100644 --- a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java +++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java @@ -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; @@ -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 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 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 trafficDataSourceMap = mockTrafficDataSourceMap(); diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java index 268d50fab9de6..cb742ff816802 100644 --- a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java +++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationConnectionTest.java @@ -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; @@ -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 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); } diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java index 741c1664c928f..df7af7923912b 100644 --- a/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java +++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/state/DriverStateContextTest.java @@ -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; @@ -60,14 +58,11 @@ class DriverStateContextTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ContextManager contextManager; - @SuppressWarnings("unchecked") @BeforeEach void setUp() { Map databases = mockDatabases(); TransactionRule transactionRule = mock(TransactionRule.class); - ResourceHeldRuleAttribute 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( diff --git a/jdbc/src/test/java/org/apache/shardingsphere/driver/state/ok/OKDriverStateTest.java b/jdbc/src/test/java/org/apache/shardingsphere/driver/state/ok/OKDriverStateTest.java index 4f817db2a188b..3092f3404169c 100644 --- a/jdbc/src/test/java/org/apache/shardingsphere/driver/state/ok/OKDriverStateTest.java +++ b/jdbc/src/test/java/org/apache/shardingsphere/driver/state/ok/OKDriverStateTest.java @@ -20,11 +20,8 @@ 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.transaction.ShardingSphereTransactionManagerEngine; import org.apache.shardingsphere.transaction.rule.TransactionRule; import org.junit.jupiter.api.Test; @@ -39,15 +36,11 @@ class OKDriverStateTest { - @SuppressWarnings("unchecked") @Test void assertGetConnection() { ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); - TransactionRule transactionRule = mock(TransactionRule.class); - ResourceHeldRuleAttribute 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)))); Connection actual = new OKDriverState().getConnection(DefaultDatabase.LOGIC_NAME, contextManager); assertThat(actual, instanceOf(ShardingSphereConnection.class)); } diff --git a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/ConnectionTransaction.java b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/ConnectionTransaction.java index bda76f0cb5852..f0d2e4fdd5ec6 100644 --- a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/ConnectionTransaction.java +++ b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/ConnectionTransaction.java @@ -19,7 +19,6 @@ import lombok.Getter; import lombok.Setter; -import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute; import org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext; import org.apache.shardingsphere.transaction.api.TransactionType; import org.apache.shardingsphere.transaction.rule.TransactionRule; @@ -49,7 +48,7 @@ public ConnectionTransaction(final TransactionRule rule) { public ConnectionTransaction(final TransactionType transactionType, final TransactionRule rule) { this.transactionType = transactionType; - transactionManager = ((ShardingSphereTransactionManagerEngine) rule.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).getResource()).getTransactionManager(transactionType); + transactionManager = rule.getResource().getTransactionManager(transactionType); } /** diff --git a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionResourceHeldRuleAttribute.java b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionResourceHeldRuleAttribute.java deleted file mode 100644 index 081eb93422caa..0000000000000 --- a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionResourceHeldRuleAttribute.java +++ /dev/null @@ -1,128 +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.transaction.rule; - -import lombok.extern.slf4j.Slf4j; -import org.apache.shardingsphere.infra.database.core.type.DatabaseType; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute; -import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine; -import org.apache.shardingsphere.transaction.api.TransactionType; - -import javax.sql.DataSource; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.concurrent.atomic.AtomicReference; - -/** - * Transaction resource held rule attribute. - */ -@Slf4j -public final class TransactionResourceHeldRuleAttribute implements ResourceHeldRuleAttribute { - - private final TransactionType defaultType; - - private final String providerType; - - private final Map databases; - - private final AtomicReference resource; - - public TransactionResourceHeldRuleAttribute(final TransactionType defaultType, final String providerType, final Map databases) { - this.defaultType = defaultType; - this.providerType = providerType; - this.databases = databases; - resource = new AtomicReference<>(createTransactionManagerEngine(this.databases)); - } - - @Override - public ShardingSphereTransactionManagerEngine getResource() { - return resource.get(); - } - - @Override - public synchronized void addResource(final ShardingSphereDatabase database) { - // TODO process null when for information_schema - if (null == database) { - return; - } - databases.put(database.getName(), database); - rebuildEngine(); - } - - @Override - public synchronized void closeStaleResource(final String databaseName) { - if (!databases.containsKey(databaseName.toLowerCase())) { - return; - } - databases.remove(databaseName); - rebuildEngine(); - } - - @Override - public synchronized void closeStaleResource() { - databases.clear(); - closeEngine(); - } - - private void rebuildEngine() { - ShardingSphereTransactionManagerEngine previousEngine = resource.get(); - if (null != previousEngine) { - closeEngine(previousEngine); - } - resource.set(createTransactionManagerEngine(databases)); - } - - private synchronized ShardingSphereTransactionManagerEngine createTransactionManagerEngine(final Map databases) { - if (databases.isEmpty()) { - return new ShardingSphereTransactionManagerEngine(defaultType); - } - Map dataSourceMap = new LinkedHashMap<>(databases.size(), 1F); - Map databaseTypes = new LinkedHashMap<>(databases.size(), 1F); - for (Entry entry : databases.entrySet()) { - ShardingSphereDatabase database = entry.getValue(); - database.getResourceMetaData().getStorageUnits().forEach((key, value) -> dataSourceMap.put(database.getName() + "." + key, value.getDataSource())); - database.getResourceMetaData().getStorageUnits().forEach((key, value) -> databaseTypes.put(database.getName() + "." + key, value.getStorageType())); - } - if (dataSourceMap.isEmpty()) { - return new ShardingSphereTransactionManagerEngine(defaultType); - } - ShardingSphereTransactionManagerEngine result = new ShardingSphereTransactionManagerEngine(defaultType); - result.init(databaseTypes, dataSourceMap, providerType); - return result; - } - - private void closeEngine() { - ShardingSphereTransactionManagerEngine engine = resource.get(); - if (null != engine) { - closeEngine(engine); - resource.set(new ShardingSphereTransactionManagerEngine(defaultType)); - } - } - - private void closeEngine(final ShardingSphereTransactionManagerEngine engine) { - try { - engine.close(); - // CHECKSTYLE:OFF - } catch (final RuntimeException ex) { - // CHECKSTYLE:ON - log.error("Close transaction engine failed", ex); - } - } -} diff --git a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionRule.java b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionRule.java index aa37f248cc7ce..848cc099e5053 100644 --- a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionRule.java +++ b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/rule/TransactionRule.java @@ -18,21 +18,28 @@ package org.apache.shardingsphere.transaction.rule; import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.rule.scope.GlobalRule; import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes; +import org.apache.shardingsphere.infra.rule.scope.GlobalRule; +import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine; import org.apache.shardingsphere.transaction.api.TransactionType; import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration; +import javax.sql.DataSource; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; -import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicReference; /** * Transaction rule. */ @Getter -public final class TransactionRule implements GlobalRule { +@Slf4j +public final class TransactionRule implements GlobalRule, AutoCloseable { private final TransactionRuleConfiguration configuration; @@ -42,7 +49,7 @@ public final class TransactionRule implements GlobalRule { private final Properties props; - private final Map databases; + private final AtomicReference resource; private final RuleAttributes attributes; @@ -51,7 +58,62 @@ public TransactionRule(final TransactionRuleConfiguration ruleConfig, final Map< defaultType = TransactionType.valueOf(ruleConfig.getDefaultType().toUpperCase()); providerType = ruleConfig.getProviderType(); props = ruleConfig.getProps(); - this.databases = new ConcurrentHashMap<>(databases); - attributes = new RuleAttributes(new TransactionResourceHeldRuleAttribute(defaultType, providerType, this.databases)); + resource = new AtomicReference<>(createTransactionManagerEngine(databases)); + attributes = new RuleAttributes(); + } + + private synchronized ShardingSphereTransactionManagerEngine createTransactionManagerEngine(final Map databases) { + if (databases.isEmpty()) { + return new ShardingSphereTransactionManagerEngine(defaultType); + } + Map databaseTypes = new LinkedHashMap<>(databases.size(), 1F); + Map dataSourceMap = new LinkedHashMap<>(databases.size(), 1F); + for (Entry entry : databases.entrySet()) { + ShardingSphereDatabase database = entry.getValue(); + database.getResourceMetaData().getStorageUnits().forEach((key, value) -> { + databaseTypes.put(database.getName() + "." + key, value.getStorageType()); + dataSourceMap.put(database.getName() + "." + key, value.getDataSource()); + }); + } + ShardingSphereTransactionManagerEngine result = new ShardingSphereTransactionManagerEngine(defaultType); + result.init(databaseTypes, dataSourceMap, providerType); + return result; + } + + /** + * Get resource. + * + * @return got resource + */ + public ShardingSphereTransactionManagerEngine getResource() { + return resource.get(); + } + + @Override + public void refresh(final Map databases) { + ShardingSphereTransactionManagerEngine previousEngine = resource.get(); + if (null != previousEngine) { + close(previousEngine); + } + resource.set(createTransactionManagerEngine(databases)); + } + + @Override + public void close() { + ShardingSphereTransactionManagerEngine engine = resource.get(); + if (null != engine) { + close(engine); + resource.set(new ShardingSphereTransactionManagerEngine(defaultType)); + } + } + + private void close(final ShardingSphereTransactionManagerEngine engine) { + try { + engine.close(); + // CHECKSTYLE:OFF + } catch (final RuntimeException ex) { + // CHECKSTYLE:ON + log.error("Close transaction engine failed.", ex); + } } } diff --git a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/TransactionRuleTest.java b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/TransactionRuleTest.java index 9f92fc32e5557..e9887b4204685 100644 --- a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/TransactionRuleTest.java +++ b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/TransactionRuleTest.java @@ -22,9 +22,7 @@ import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; 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.rule.attribute.resoure.ResourceHeldRuleAttribute; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; -import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine; import org.apache.shardingsphere.transaction.api.TransactionType; import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration; import org.apache.shardingsphere.transaction.core.fixture.ShardingSphereTransactionManagerFixture; @@ -35,10 +33,7 @@ import java.util.Map; import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -51,48 +46,24 @@ class TransactionRuleTest { @Test void assertInitTransactionRuleWithMultiDatabaseType() { - TransactionRule actual = new TransactionRule(createTransactionRuleConfiguration(), Collections.singletonMap(SHARDING_DB_1, createDatabase())); - assertNotNull(actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).getResource()); - assertThat(((ShardingSphereTransactionManagerEngine) actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).getResource()).getTransactionManager(TransactionType.XA), - instanceOf(ShardingSphereTransactionManagerFixture.class)); + try (TransactionRule actual = new TransactionRule(createTransactionRuleConfiguration(), Collections.singletonMap(SHARDING_DB_1, createDatabase()))) { + assertThat(actual.getResource().getTransactionManager(TransactionType.XA), instanceOf(ShardingSphereTransactionManagerFixture.class)); + } } @Test void assertAddResource() { - TransactionRule actual = new TransactionRule(createTransactionRuleConfiguration(), Collections.singletonMap(SHARDING_DB_1, createDatabase())); - actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).addResource(createAddDatabase()); - assertNotNull(actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).getResource()); - assertThat(actual.getDatabases().size(), is(2)); - assertTrue(actual.getDatabases().containsKey(SHARDING_DB_1)); - ResourceMetaData resourceMetaData1 = actual.getDatabases().get(SHARDING_DB_1).getResourceMetaData(); - assertThat(resourceMetaData1.getStorageUnits().size(), is(2)); - assertTrue(resourceMetaData1.getStorageUnits().containsKey("ds_0")); - assertTrue(resourceMetaData1.getStorageUnits().containsKey("ds_1")); - assertTrue(actual.getDatabases().containsKey(SHARDING_DB_2)); - ResourceMetaData resourceMetaData2 = actual.getDatabases().get(SHARDING_DB_2).getResourceMetaData(); - assertThat(resourceMetaData2.getStorageUnits().size(), is(2)); - assertTrue(resourceMetaData2.getStorageUnits().containsKey("ds_0")); - assertTrue(resourceMetaData2.getStorageUnits().containsKey("ds_1")); - assertThat(((ShardingSphereTransactionManagerEngine) actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).getResource()).getTransactionManager(TransactionType.XA), - instanceOf(ShardingSphereTransactionManagerFixture.class)); - } - - @Test - void assertCloseStaleResource() { - TransactionRule actual = new TransactionRule(createTransactionRuleConfiguration(), Collections.singletonMap(SHARDING_DB_1, createDatabase())); - actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).closeStaleResource(SHARDING_DB_1); - assertTrue(actual.getDatabases().isEmpty()); - assertThat(((ShardingSphereTransactionManagerEngine) actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).getResource()).getTransactionManager(TransactionType.XA), - instanceOf(ShardingSphereTransactionManagerFixture.class)); + try (TransactionRule actual = new TransactionRule(createTransactionRuleConfiguration(), Collections.singletonMap(SHARDING_DB_1, createDatabase()))) { + actual.refresh(Collections.singletonMap(SHARDING_DB_2, createAddDatabase())); + assertThat(actual.getResource().getTransactionManager(TransactionType.XA), instanceOf(ShardingSphereTransactionManagerFixture.class)); + } } @Test - void assertCloseAllStaleResources() { + void assertClose() { TransactionRule actual = new TransactionRule(createTransactionRuleConfiguration(), Collections.singletonMap(SHARDING_DB_1, createDatabase())); - actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).closeStaleResource(); - assertTrue(actual.getDatabases().isEmpty()); - assertThat(((ShardingSphereTransactionManagerEngine) actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).getResource()).getTransactionManager(TransactionType.XA), - instanceOf(ShardingSphereTransactionManagerFixture.class)); + actual.close(); + assertThat(actual.getResource().getTransactionManager(TransactionType.XA), instanceOf(ShardingSphereTransactionManagerFixture.class)); } private ShardingSphereDatabase createDatabase() { diff --git a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/builder/TransactionRuleBuilderTest.java b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/builder/TransactionRuleBuilderTest.java index 6c84909724721..953c72ad87604 100644 --- a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/builder/TransactionRuleBuilderTest.java +++ b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/builder/TransactionRuleBuilderTest.java @@ -35,8 +35,6 @@ import java.util.Map; import java.util.Properties; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.mock; @@ -47,9 +45,9 @@ void assertBuild() { TransactionRuleConfiguration ruleConfig = new TransactionRuleConfiguration("LOCAL", "provider", new Properties()); ShardingSphereDatabase database = new ShardingSphereDatabase("logic_db", null, new ResourceMetaData(createDataSourceMap()), new RuleMetaData(Collections.singletonList(mock(ShardingSphereRule.class))), Collections.singletonMap("test", mock(ShardingSphereSchema.class))); - TransactionRule rule = new TransactionRuleBuilder().build(ruleConfig, Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ConfigurationProperties.class)); - assertNotNull(rule.getConfiguration()); - assertThat(rule.getDatabases().get("logic_db").getResourceMetaData().getStorageUnits().size(), is(2)); + try (TransactionRule rule = new TransactionRuleBuilder().build(ruleConfig, Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ConfigurationProperties.class))) { + assertNotNull(rule.getConfiguration()); + } } private Map createDataSourceMap() { diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java index 6880e17ef09ac..cf8165df2cab7 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.mode.manager.context; import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration; import org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration; @@ -37,8 +38,6 @@ import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRulesBuilder; import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder; -import org.apache.shardingsphere.infra.rule.scope.DatabaseRule; -import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute; import org.apache.shardingsphere.metadata.factory.ExternalMetaDataFactory; import org.apache.shardingsphere.metadata.factory.InternalMetaDataFactory; import org.apache.shardingsphere.metadata.persist.MetaDataBasedPersistService; @@ -76,11 +75,9 @@ public final class ConfigurationContextManager { * @param databaseName database name * @param propsMap data source pool properties map */ - @SuppressWarnings("rawtypes") public synchronized void registerStorageUnit(final String databaseName, final Map propsMap) { try { - Collection staleResourceHeldRuleAttributes = getStaleResourceHeldRules(databaseName); - staleResourceHeldRuleAttributes.forEach(ResourceHeldRuleAttribute::closeStaleResource); + closeStaleRules(databaseName); SwitchingResource switchingResource = new ResourceSwitchManager().registerStorageUnit(metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(), propsMap); buildNewMetaDataContext(databaseName, switchingResource); @@ -95,11 +92,9 @@ public synchronized void registerStorageUnit(final String databaseName, final Ma * @param databaseName database name * @param propsMap data source pool properties map */ - @SuppressWarnings("rawtypes") public synchronized void alterStorageUnit(final String databaseName, final Map propsMap) { try { - Collection staleResourceHeldRuleAttributes = getStaleResourceHeldRules(databaseName); - staleResourceHeldRuleAttributes.forEach(ResourceHeldRuleAttribute::closeStaleResource); + closeStaleRules(databaseName); SwitchingResource switchingResource = new ResourceSwitchManager().alterStorageUnit(metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(), propsMap); buildNewMetaDataContext(databaseName, switchingResource); @@ -114,11 +109,9 @@ public synchronized void alterStorageUnit(final String databaseName, final Map staleResourceHeldRuleAttributes = getStaleResourceHeldRules(databaseName); - staleResourceHeldRuleAttributes.forEach(ResourceHeldRuleAttribute::closeStaleResource); + closeStaleRules(databaseName); SwitchingResource switchingResource = new ResourceSwitchManager().unregisterStorageUnit(metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(), Collections.singletonList(storageUnitName)); buildNewMetaDataContext(databaseName, switchingResource); @@ -147,12 +140,10 @@ private void buildNewMetaDataContext(final String databaseName, final SwitchingR * @param databaseName database name * @param ruleConfigs rule configurations */ - @SuppressWarnings("rawtypes") public synchronized void alterRuleConfiguration(final String databaseName, final Collection ruleConfigs) { try { - Collection staleResourceHeldRuleAttributes = getStaleResourceHeldRules(databaseName); // TODO consider rename this method to alterDatabaseRuleConfiguration - staleResourceHeldRuleAttributes.stream().filter(DatabaseRule.class::isInstance).forEach(ResourceHeldRuleAttribute::closeStaleResource); + closeStaleRules(databaseName); MetaDataContexts reloadMetaDataContexts = createMetaDataContextsWhenRuleChanged(databaseName, false, null, ruleConfigs); alterSchemaMetaData(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.get().getMetaData().getDatabase(databaseName)); metaDataContexts.set(reloadMetaDataContexts); @@ -225,6 +216,34 @@ private MetaDataContexts createMetaDataContextsByAlterRule(final String database metaDataContexts.get().getMetaData().getGlobalRuleMetaData(), metaDataContexts.get().getMetaData().getProps())); } + /** + * Alter data source units configuration. + * + * @param databaseName database name + * @param propsMap altered data source pool properties map + */ + public synchronized void alterDataSourceUnitsConfiguration(final String databaseName, final Map propsMap) { + try { + closeStaleRules(databaseName); + SwitchingResource switchingResource = + new ResourceSwitchManager().createByAlterDataSourcePoolProperties(metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(), propsMap); + metaDataContexts.get().getMetaData().getDatabases().putAll(renewDatabase(metaDataContexts.get().getMetaData().getDatabase(databaseName), switchingResource)); + // TODO Remove this logic when issue #22887 are finished. + MetaDataContexts reloadMetaDataContexts = createMetaDataContexts(databaseName, false, switchingResource, null); + reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().forEach((schemaName, schema) -> reloadMetaDataContexts.getPersistService().getDatabaseMetaDataService() + .persist(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), schemaName, schema)); + Optional.ofNullable(reloadMetaDataContexts.getStatistics().getDatabaseData().get(databaseName)) + .ifPresent(optional -> optional.getSchemaData().forEach((schemaName, schemaData) -> reloadMetaDataContexts.getPersistService().getShardingSphereDataPersistService() + .persist(databaseName, schemaName, schemaData, metaDataContexts.get().getMetaData().getDatabases()))); + alterSchemaMetaData(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.get().getMetaData().getDatabase(databaseName)); + metaDataContexts.set(reloadMetaDataContexts); + metaDataContexts.get().getMetaData().getDatabases().putAll(newShardingSphereDatabase(metaDataContexts.get().getMetaData().getDatabase(databaseName))); + switchingResource.closeStaleDataSources(); + } catch (final SQLException ex) { + log.error("Alter database: {} data source configuration failed", databaseName, ex); + } + } + /** * Alter schema meta data. * @@ -273,11 +292,13 @@ private Map getNewStorageUnits(final Map getStaleResourceHeldRules(final String databaseName) { - Collection result = metaDataContexts.get().getMetaData().getDatabase(databaseName).getRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class); - result.addAll(metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class)); - return result; + @SneakyThrows(Exception.class) + private void closeStaleRules(final String databaseName) { + for (ShardingSphereRule each : metaDataContexts.get().getMetaData().getDatabase(databaseName).getRuleMetaData().getRules()) { + if (each instanceof AutoCloseable) { + ((AutoCloseable) each).close(); + } + } } /** @@ -391,7 +412,7 @@ public synchronized void alterGlobalRuleConfiguration(final Collection removeSingleGlobalRule(final RuleConfigur return result; } + @SneakyThrows(Exception.class) + private void closeStaleGlobalRules() { + for (ShardingSphereRule each : metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getRules()) { + if (each instanceof AutoCloseable) { + ((AutoCloseable) each).close(); + } + } + } + /** * Alter properties. * diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java index 5b1d28f98eabf..dc0e794aa5063 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java @@ -18,21 +18,23 @@ package org.apache.shardingsphere.mode.metadata; import lombok.Getter; +import lombok.SneakyThrows; import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData; import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData; import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder; -import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.metadata.persist.MetaDataBasedPersistService; +import java.util.Collection; +import java.util.LinkedList; import java.util.Map.Entry; import java.util.Optional; @@ -96,12 +98,20 @@ private void useLoadedToReplaceInitBySchemaData(final ShardingSphereSchemaData i } } + @SneakyThrows(Exception.class) @Override public void close() { persistService.getRepository().close(); - metaData.getGlobalRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class).forEach(ResourceHeldRuleAttribute::closeStaleResource); - for (ShardingSphereDatabase each : metaData.getDatabases().values()) { - each.getRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class).forEach(ResourceHeldRuleAttribute::closeStaleResource); + for (ShardingSphereRule each : getAllRules()) { + if (each instanceof AutoCloseable) { + ((AutoCloseable) each).close(); + } } } + + private Collection getAllRules() { + Collection result = new LinkedList<>(metaData.getGlobalRuleMetaData().getRules()); + metaData.getDatabases().values().stream().map(each -> each.getRuleMetaData().getRules()).forEach(result::addAll); + return result; + } } diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java index fd52bc64702a5..05a40ef3dba0b 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java @@ -86,6 +86,7 @@ void setUp() throws SQLException { when(metaDataPersistService.getDatabaseMetaDataService()).thenReturn(databaseMetaDataPersistService); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + when(database.getRuleMetaData().getRules()).thenReturn(Collections.emptyList()); when(ExternalMetaDataFactory.create(anyMap(), any(), any())).thenReturn(new HashMap<>(Collections.singletonMap("foo_db", database))); when(GlobalRulesBuilder.buildRules(anyCollection(), anyMap(), any(ConfigurationProperties.class))).thenReturn(Collections.singleton(new MockedRule())); } diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneModeContextManager.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneModeContextManager.java index bf09b056735ce..e1b0ad6cce644 100644 --- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneModeContextManager.java +++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneModeContextManager.java @@ -31,7 +31,7 @@ import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute; import org.apache.shardingsphere.infra.rule.attribute.metadata.MetaDataHeldRuleAttribute; -import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute; +import org.apache.shardingsphere.infra.rule.scope.GlobalRule; import org.apache.shardingsphere.infra.spi.type.ordered.cache.OrderedServicesCache; import org.apache.shardingsphere.metadata.persist.service.config.global.GlobalPersistService; import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataBasedPersistService; @@ -211,8 +211,8 @@ public void registerStorageUnits(final String databaseName, final Map each.addResource(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName))); + contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules() + .forEach(each -> ((GlobalRule) each).refresh(contextManager.getMetaDataContexts().getMetaData().getDatabases())); contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getSchemas() .forEach((schemaName, schema) -> contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService() .persist(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(), schemaName, schema)); @@ -226,8 +226,8 @@ public void alterStorageUnits(final String databaseName, final Map each.addResource(contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName))); + contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getRules() + .forEach(each -> ((GlobalRule) each).refresh(contextManager.getMetaDataContexts().getMetaData().getDatabases())); contextManager.getMetaDataContexts().getPersistService().getDataSourceUnitService().append( contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getName(), toBeUpdatedProps); switchingResource.closeStaleDataSources(); diff --git a/pom.xml b/pom.xml index 4c8e62768a780..1ad4dc4d774c4 100644 --- a/pom.xml +++ b/pom.xml @@ -100,7 +100,7 @@ 1.70 5.6.0 - 3.9.1 + 3.9.2 0.12.0 0.7.7 4.5.1 diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSource.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSource.java index 0028e662a6e68..76f0cc66b8620 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSource.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSource.java @@ -21,10 +21,8 @@ import org.apache.shardingsphere.infra.database.core.GlobalDataSourceRegistry; import org.apache.shardingsphere.infra.exception.OverallConnectionNotEnoughException; import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode; -import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute; import org.apache.shardingsphere.proxy.backend.connector.BackendDataSource; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; -import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine; import org.apache.shardingsphere.transaction.api.TransactionType; import org.apache.shardingsphere.transaction.rule.TransactionRule; import org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager; @@ -108,8 +106,7 @@ private List createConnections(final String databaseName, final Stri private Connection createConnection(final String databaseName, final String dataSourceName, final DataSource dataSource, final TransactionType transactionType) throws SQLException { TransactionRule transactionRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TransactionRule.class); - ShardingSphereTransactionManager transactionManager = ((ShardingSphereTransactionManagerEngine) transactionRule.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).getResource()) - .getTransactionManager(transactionType); + ShardingSphereTransactionManager transactionManager = transactionRule.getResource().getTransactionManager(transactionType); Connection result = isInTransaction(transactionManager) ? transactionManager.getConnection(databaseName, dataSourceName) : dataSource.getConnection(); if (dataSourceName.contains(".")) { String catalog = dataSourceName.split("\\.")[1]; diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/BackendTransactionManager.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/BackendTransactionManager.java index 86238dd4ce4de..0fdbf1bc674f7 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/BackendTransactionManager.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/BackendTransactionManager.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.proxy.backend.connector.jdbc.transaction; -import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute; import org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager; @@ -56,7 +55,7 @@ public BackendTransactionManager(final ProxyDatabaseConnectionManager databaseCo transactionType = connection.getConnectionSession().getTransactionStatus().getTransactionType(); localTransactionManager = new LocalTransactionManager(databaseConnectionManager); TransactionRule transactionRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TransactionRule.class); - ShardingSphereTransactionManagerEngine engine = (ShardingSphereTransactionManagerEngine) transactionRule.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).getResource(); + ShardingSphereTransactionManagerEngine engine = transactionRule.getResource(); shardingSphereTransactionManager = null == engine ? null : engine.getTransactionManager(transactionType); transactionHooks = ShardingSphereServiceLoader.getServiceInstances(TransactionHook.class); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSourceTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSourceTest.java index 19e6bfafbd190..2befa7cc8198c 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSourceTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSourceTest.java @@ -26,8 +26,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.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; @@ -36,7 +34,6 @@ import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; -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; @@ -87,31 +84,22 @@ void setUp() { private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), - new ShardingSphereMetaData(createDatabases(), mock(ResourceMetaData.class), mockGlobalRuleMetaData(), new ConfigurationProperties(new Properties()))); + MetaDataContexts metaDataContexts = new MetaDataContexts(mock(MetaDataPersistService.class), new ShardingSphereMetaData( + createDatabases(), mock(ResourceMetaData.class), + new RuleMetaData(Collections.singleton(mock(TransactionRule.class, RETURNS_DEEP_STUBS))), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; } private Map createDatabases() { - DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE"); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getProtocolType()).thenReturn(databaseType); + when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); for (Entry entry : mockDataSources(2).entrySet()) { when(database.getResourceMetaData().getStorageUnits().get(entry.getKey()).getDataSource()).thenReturn(entry.getValue()); } return Collections.singletonMap("schema", database); } - @SuppressWarnings("unchecked") - private RuleMetaData mockGlobalRuleMetaData() { - TransactionRule transactionRule = mock(TransactionRule.class); - ResourceHeldRuleAttribute resourceHeldRuleAttribute = mock(ResourceHeldRuleAttribute.class); - when(resourceHeldRuleAttribute.getResource()).thenReturn(mock(ShardingSphereTransactionManagerEngine.class)); - when(transactionRule.getAttributes()).thenReturn(new RuleAttributes(resourceHeldRuleAttribute)); - return new RuleMetaData(Collections.singleton(transactionRule)); - } - private Map mockDataSources(final int size) { Map result = new HashMap<>(size, 1F); for (int i = 0; i < size; i++) { diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/BackendTransactionManagerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/BackendTransactionManagerTest.java index 73da06c9c801a..9f3e235f574e0 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/BackendTransactionManagerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/BackendTransactionManagerTest.java @@ -18,11 +18,9 @@ package org.apache.shardingsphere.proxy.backend.connector.jdbc.transaction; import lombok.SneakyThrows; -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.infra.session.connection.ConnectionContext; import org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; @@ -193,14 +191,11 @@ private ContextManager mockContextManager() { return result; } - @SuppressWarnings("unchecked") private RuleMetaData mockGlobalRuleMetaData() { ShardingSphereTransactionManagerEngine transactionManagerEngine = mock(ShardingSphereTransactionManagerEngine.class); when(transactionManagerEngine.getTransactionManager(TransactionType.XA)).thenReturn(shardingSphereTransactionManager); TransactionRule transactionRule = mock(TransactionRule.class); - ResourceHeldRuleAttribute resourceHeldRuleAttribute = mock(ResourceHeldRuleAttribute.class); - when(resourceHeldRuleAttribute.getResource()).thenReturn(transactionManagerEngine); - when(transactionRule.getAttributes()).thenReturn(new RuleAttributes(resourceHeldRuleAttribute)); + when(transactionRule.getResource()).thenReturn(transactionManagerEngine); return new RuleMetaData(Collections.singleton(transactionRule)); } } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandlerFactoryTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandlerFactoryTest.java index 8edf97ec0a0e0..27ffa5bdfd7d4 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandlerFactoryTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandlerFactoryTest.java @@ -18,15 +18,13 @@ package org.apache.shardingsphere.proxy.backend.handler.transaction; import lombok.SneakyThrows; -import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes; -import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute; -import org.apache.shardingsphere.infra.session.query.QueryContext; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; +import org.apache.shardingsphere.infra.session.query.QueryContext; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager; import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnector; import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnectorFactory; +import org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager; import org.apache.shardingsphere.proxy.backend.connector.jdbc.transaction.BackendTransactionManager; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler; @@ -36,7 +34,6 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.TCLStatement; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; -import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine; import org.apache.shardingsphere.transaction.core.TransactionOperationType; import org.apache.shardingsphere.transaction.rule.TransactionRule; import org.hamcrest.Matcher; @@ -95,14 +92,9 @@ void assertTransactionBackendHandlerReturnedWhenTCLStatementInstanceOfRollbackSt assertFieldOfInstance(getBackendTransactionManager(transactionBackendHandler), "connection", is(databaseConnectionManager)); } - @SuppressWarnings("unchecked") private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - TransactionRule transactionRule = mock(TransactionRule.class); - ResourceHeldRuleAttribute 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(Collections.singleton(transactionRule))); + when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(mock(TransactionRule.class)))); return result; } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandlerTest.java index 51928977dfec5..9b80a1d0aa853 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/transaction/TransactionBackendHandlerTest.java @@ -18,8 +18,6 @@ package org.apache.shardingsphere.proxy.backend.handler.transaction; 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.proxy.backend.connector.ProxyDatabaseConnectionManager; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; @@ -28,7 +26,6 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.TCLStatement; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; -import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine; import org.apache.shardingsphere.transaction.core.TransactionOperationType; import org.apache.shardingsphere.transaction.rule.TransactionRule; import org.junit.jupiter.api.Test; @@ -59,14 +56,9 @@ void assertExecute() throws SQLException { assertThat(new TransactionBackendHandler(mock(TCLStatement.class), TransactionOperationType.BEGIN, connectionSession).execute(), instanceOf(UpdateResponseHeader.class)); } - @SuppressWarnings("unchecked") private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - TransactionRule transactionRule = mock(TransactionRule.class); - ResourceHeldRuleAttribute 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(Collections.singleton(transactionRule))); + when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(mock(TransactionRule.class)))); return result; } } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSessionTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSessionTest.java index 9919f43b55f86..845ae3c9ad87f 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSessionTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/session/ConnectionSessionTest.java @@ -17,18 +17,15 @@ package org.apache.shardingsphere.proxy.backend.session; -import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes; -import org.apache.shardingsphere.infra.rule.attribute.resoure.ResourceHeldRuleAttribute; -import org.apache.shardingsphere.infra.session.query.QueryContext; import org.apache.shardingsphere.infra.database.mysql.type.MySQLDatabaseType; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; +import org.apache.shardingsphere.infra.session.query.QueryContext; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager; import org.apache.shardingsphere.proxy.backend.connector.jdbc.transaction.BackendTransactionManager; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; -import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine; import org.apache.shardingsphere.transaction.api.TransactionType; import org.apache.shardingsphere.transaction.exception.SwitchTypeInTransactionException; import org.apache.shardingsphere.transaction.rule.TransactionRule; @@ -93,14 +90,9 @@ void assertSwitchSchemaWhileBegin() { assertThat(connectionSession.getDefaultDatabaseName(), is("newDB")); } - @SuppressWarnings("unchecked") private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - TransactionRule transactionRule = mock(TransactionRule.class); - ResourceHeldRuleAttribute 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(Collections.singleton(transactionRule))); + when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(mock(TransactionRule.class)))); return result; }