From e765db8acc9ee509fdc458f2d85ef985e2f2583d Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 29 Sep 2023 02:43:08 +0800 Subject: [PATCH 1/8] Remove StorageUnitMetaData.storageNodes --- .../resource/unit/NewStorageUnitMetaData.java | 46 +++++++++++++++++++ .../resource/unit/StorageUnitMetaData.java | 13 +++--- .../context/ConfigurationContextManager.java | 4 +- .../switcher/NewResourceSwitchManager.java | 7 ++- .../switcher/ResourceSwitchManager.java | 8 ++-- .../mode/manager/ContextManagerTest.java | 6 ++- .../ConfigurationChangedSubscriberTest.java | 2 +- 7 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/NewStorageUnitMetaData.java diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/NewStorageUnitMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/NewStorageUnitMetaData.java new file mode 100644 index 0000000000000..a9ea079772d4d --- /dev/null +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/NewStorageUnitMetaData.java @@ -0,0 +1,46 @@ +/* + * 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.unit; + +import lombok.Getter; +import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; + +import javax.sql.DataSource; + +/** + * Storage unit meta data. + */ +@Getter +public final class NewStorageUnitMetaData { + + private final StorageNode storageNode; + + private final DataSourcePoolProperties dataSourcePoolProperties; + + private final DataSource dataSource; + + private final StorageUnit storageUnit; + + public NewStorageUnitMetaData(final String databaseName, final StorageNode storageNode, final DataSourcePoolProperties dataSourcePoolProperties, final DataSource dataSource) { + this.storageNode = storageNode; + this.dataSourcePoolProperties = dataSourcePoolProperties; + this.dataSource = dataSource; + storageUnit = new StorageUnit(databaseName, dataSource, dataSourcePoolProperties, storageNode); + } +} diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java index aabe73da4906d..18be1da3d08ef 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java @@ -34,23 +34,24 @@ @Getter public final class StorageUnitMetaData { - // TODO zhangliang: should refactor - private final Map storageNodes; - private final Map dataSourcePoolPropertiesMap; private final Map storageUnits; - // TODO zhangliang: should refactor private final Map dataSources; + private final Map metaDataMap; + public StorageUnitMetaData(final String databaseName, final Map storageNodes, final Map dataSourcePoolPropertiesMap, final Map dataSources) { - this.storageNodes = storageNodes; + metaDataMap = new LinkedHashMap<>(); + for (Entry entry : storageNodes.entrySet()) { + metaDataMap.put(entry.getKey(), new NewStorageUnitMetaData(databaseName, entry.getValue(), dataSourcePoolPropertiesMap.get(entry.getKey()), dataSources.get(entry.getValue().getName()))); + } this.dataSourcePoolPropertiesMap = dataSourcePoolPropertiesMap; storageUnits = storageNodes.entrySet().stream().collect( Collectors.toMap(Entry::getKey, entry -> new StorageUnit(databaseName, dataSources.get(entry.getValue().getName()), dataSourcePoolPropertiesMap.get(entry.getKey()), entry.getValue()), - (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(this.storageNodes.size(), 1F))); + (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(storageNodes.size(), 1F))); this.dataSources = storageUnits.entrySet().stream().collect( Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(storageUnits.size(), 1F))); } 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 2528f26dfc0ce..2994c577d8335 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 @@ -365,7 +365,9 @@ private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData re private StorageResource getMergedStorageResource(final ResourceMetaData currentResourceMetaData, final SwitchingResource switchingResource) { Map storageNodeDataSources = currentResourceMetaData.getDataSources(); - Map storageUnitNodeMap = currentResourceMetaData.getStorageUnitMetaData().getStorageNodes(); + Map storageUnitNodeMap = new LinkedHashMap<>( + currentResourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getDataSources().isEmpty()) { storageNodeDataSources.putAll(switchingResource.getNewStorageResource().getDataSources()); } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java index c39f88e79667d..d4b2dbe4a3590 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java @@ -120,8 +120,11 @@ public SwitchingResource unregisterStorageUnit(final ResourceMetaData resourceMe } private StorageResource getToBeRemovedStaleStorageResource(final ResourceMetaData resourceMetaData, final String storageUnitName) { - StorageNode storageNode = resourceMetaData.getStorageUnitMetaData().getStorageNodes().remove(storageUnitName); - Map reservedStorageUintNodeMap = resourceMetaData.getStorageUnitMetaData().getStorageNodes(); + Map metaDataMap = new HashMap<>( + resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode()))); + StorageNode storageNode = metaDataMap.remove(storageUnitName); + Map reservedStorageUintNodeMap = resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode())); Map storageNodes = new LinkedHashMap<>(1, 1F); if (reservedStorageUintNodeMap.values().stream().noneMatch(each -> each.equals(storageNode))) { storageNodes.put(storageNode.getName(), resourceMetaData.getDataSources().get(storageNode.getName())); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java index 8a0c7da949d34..8f8794c5684c4 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java @@ -112,10 +112,12 @@ private Map getNewStorageNodes(final ResourceMetaDa } private Map getNewStorageUnitNodeMap(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMap) { - Map result = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getStorageNodes()); + Map result = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); result.keySet().removeAll(getToBeDeletedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap.keySet()).keySet()); result.putAll(getChangedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap)); - result.putAll(getToBeAddedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageNodes(), toBeChangedStorageUnitNodeMap)); + result.putAll(getToBeAddedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), toBeChangedStorageUnitNodeMap)); return result; } @@ -164,7 +166,7 @@ private StorageResource getToBeRemovedStaleDataSources(final ResourceMetaData re private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMap, final Map storageUnitDataSourcePoolProps) { Map storageNodes = new LinkedHashMap<>(resourceMetaData.getDataSources().size(), 1F); - Map storageUnitNodeMap = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getStorageNodes().size(), 1F); + Map storageUnitNodeMap = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().size(), 1F); storageNodes.putAll(getToBeChangedDataSources(resourceMetaData.getDataSources(), StorageUnitNodeMapUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps))); storageUnitNodeMap.putAll(getChangedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap)); return new StorageResource(storageNodes, storageUnitNodeMap); diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java index 983f0c3a8f116..f1c1ddbebd192 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java @@ -31,6 +31,7 @@ import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeUtils; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; @@ -257,11 +258,14 @@ private ResourceMetaData createOriginalResource() { Map storageNodeDataSourceMap = StorageNodeUtils.getStorageNodeDataSources(originalDataSources); Map storageUnits = new LinkedHashMap<>(2, 1F); Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSources(originalDataSources); + Map metaDataMap = new LinkedHashMap<>(2, 1F); for (Entry entry : storageUnitNodeMap.entrySet()) { storageUnits.put(entry.getKey(), new StorageUnit("foo_db", storageNodeDataSourceMap.get(entry.getValue().getName()), mock(DataSourcePoolProperties.class), entry.getValue())); + metaDataMap.put(entry.getKey(), new NewStorageUnitMetaData( + "foo_db", storageUnitNodeMap.get(entry.getKey()), mock(DataSourcePoolProperties.class), storageNodeDataSourceMap.get(entry.getValue().getName()))); } when(result.getStorageUnitMetaData().getStorageUnits()).thenReturn(storageUnits); - when(result.getStorageUnitMetaData().getStorageNodes()).thenReturn(storageUnitNodeMap); + when(result.getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); when(result.getDataSources()).thenReturn(storageNodeDataSourceMap); return result; } diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java index b2dd14aa6926a..b103a79268d64 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java @@ -113,7 +113,7 @@ private ContextManagerBuilderParameter createContextManagerBuilderParameter() { private Map createDatabases() { when(database.getName()).thenReturn("db"); ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); - when(resourceMetaData.getStorageUnitMetaData().getStorageNodes()).thenReturn(Collections.emptyMap()); + when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.emptyMap()); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(database.getSchemas()).thenReturn(Collections.singletonMap("foo_schema", new ShardingSphereSchema())); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); From 78698392189913209073238de6b21d3f609209c5 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 29 Sep 2023 12:06:54 +0800 Subject: [PATCH 2/8] Remove StorageUnitMetaData.dataSourcePoolPropertiesMap --- .../database/resource/unit/StorageUnitMetaData.java | 3 --- .../manager/context/ConfigurationContextManager.java | 3 ++- .../manager/switcher/NewResourceSwitchManager.java | 11 +++++++---- .../mode/manager/switcher/ResourceSwitchManager.java | 9 ++++++--- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java index 18be1da3d08ef..75bfe46bf3a20 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java @@ -34,8 +34,6 @@ @Getter public final class StorageUnitMetaData { - private final Map dataSourcePoolPropertiesMap; - private final Map storageUnits; private final Map dataSources; @@ -48,7 +46,6 @@ public StorageUnitMetaData(final String databaseName, final Map entry : storageNodes.entrySet()) { metaDataMap.put(entry.getKey(), new NewStorageUnitMetaData(databaseName, entry.getValue(), dataSourcePoolPropertiesMap.get(entry.getKey()), dataSources.get(entry.getValue().getName()))); } - this.dataSourcePoolPropertiesMap = dataSourcePoolPropertiesMap; storageUnits = storageNodes.entrySet().stream().collect( Collectors.toMap(Entry::getKey, entry -> new StorageUnit(databaseName, dataSources.get(entry.getValue().getName()), dataSourcePoolPropertiesMap.get(entry.getKey()), entry.getValue()), (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(storageNodes.size(), 1F))); 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 2994c577d8335..3cec415a40fa5 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 @@ -358,7 +358,8 @@ private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData re final Collection toBeCreatedRuleConfigs) { StorageResource storageResource = getMergedStorageResource(resourceMetaData, switchingResource); Map propsMap = null == switchingResource - ? resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap() + ? resourceMetaData.getStorageUnitMetaData().getMetaDataMap() + .entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)) : switchingResource.getMergedDataSourcePoolPropertiesMap(); return new DataSourceProvidedDatabaseConfiguration(storageResource, toBeCreatedRuleConfigs, propsMap); } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java index d4b2dbe4a3590..12c60dcba1db3 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java @@ -48,7 +48,8 @@ public final class NewResourceSwitchManager { * @return created switching resource */ public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMetaData, final Map storageUnitDataSourcePoolProps) { - Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); + Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap() + .entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedPropsMap.putAll(storageUnitDataSourcePoolProps); Map toBeCreatedStorageUintNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(storageUnitDataSourcePoolProps); return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedStorageUintNodeMap, storageUnitDataSourcePoolProps), @@ -76,7 +77,8 @@ private StorageResource getRegisterNewStorageResource(final ResourceMetaData res * @return created switching resource */ public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaData, final Map propsMap) { - Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); + Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap() + .entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedDataSourcePoolPropertiesMap.putAll(propsMap); Map toBeAlteredStorageUintNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(mergedDataSourcePoolPropertiesMap); return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredStorageUintNodeMap, mergedDataSourcePoolPropertiesMap), @@ -112,9 +114,10 @@ private StorageResource getStaleStorageResource(final ResourceMetaData resourceM * @return created switching resource */ public SwitchingResource unregisterStorageUnit(final ResourceMetaData resourceMetaData, final String storageUnitName) { - Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); + Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap() + .entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedDataSourcePoolPropertiesMap.keySet().removeIf(each -> each.equals(storageUnitName)); - resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap().remove(storageUnitName); + resourceMetaData.getStorageUnitMetaData().getMetaDataMap().remove(storageUnitName); return new SwitchingResource(resourceMetaData, new StorageResource(Collections.emptyMap(), Collections.emptyMap()), getToBeRemovedStaleStorageResource(resourceMetaData, storageUnitName), mergedDataSourcePoolPropertiesMap); } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java index 8f8794c5684c4..23f1f280dba5c 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java @@ -49,7 +49,8 @@ public final class ResourceSwitchManager { * @return created switching resource */ public SwitchingResource create(final ResourceMetaData resourceMetaData, final Map toBeChangedPropsMap) { - Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); + Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedPropsMap.putAll(toBeChangedPropsMap); Map toBeChangedStorageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(toBeChangedPropsMap); return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedStorageUnitNodeMap, toBeChangedPropsMap), @@ -64,7 +65,8 @@ public SwitchingResource create(final ResourceMetaData resourceMetaData, final M * @return created switching resource */ public SwitchingResource createByDropResource(final ResourceMetaData resourceMetaData, final Map toBeDeletedPropsMap) { - Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); + Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedDataSourcePoolPropertiesMap.keySet().removeIf(toBeDeletedPropsMap::containsKey); Map toRemovedStorageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(toBeDeletedPropsMap); return new SwitchingResource(resourceMetaData, new StorageResource(Collections.emptyMap(), Collections.emptyMap()), @@ -79,7 +81,8 @@ public SwitchingResource createByDropResource(final ResourceMetaData resourceMet * @return created switching resource */ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMetaData resourceMetaData, final Map toBeChangedPropsMap) { - Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); + Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedDataSourcePoolPropertiesMap.keySet().removeIf(each -> !toBeChangedPropsMap.containsKey(each)); mergedDataSourcePoolPropertiesMap.putAll(toBeChangedPropsMap); Map toBeChangedStorageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(toBeChangedPropsMap); From 1abeb4a922b6150cd0f3358e79bd383d8f1ebd41 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 29 Sep 2023 15:32:17 +0800 Subject: [PATCH 3/8] Remove StorageUnitMetaData.dataSourceMap --- .../database/ShardingSphereDatabase.java | 9 +++-- .../resource/unit/StorageUnitMetaData.java | 4 --- .../builder/GenericSchemaBuilderMaterial.java | 6 ++-- .../single/route/SingleSQLRouterTest.java | 11 +++--- .../ShowUnloadedSingleTableExecutor.java | 7 +++- .../LoadSingleTableStatementUpdater.java | 5 ++- .../transaction/rule/TransactionRuleTest.java | 34 ++++++++++--------- .../context/ConfigurationContextManager.java | 8 +++-- .../metadata/MetaDataContextsFactory.java | 4 ++- .../metadata/NewMetaDataContextsFactory.java | 8 +++-- .../mode/manager/ContextManagerTest.java | 10 +++--- proxy/backend/core/pom.xml | 4 +++ .../rule/NewRuleDefinitionBackendHandler.java | 9 +++-- .../rule/RuleDefinitionBackendHandler.java | 9 +++-- ...mlDatabaseConfigurationImportExecutor.java | 30 ++++++++++------ .../updatable/ImportMetaDataUpdaterTest.java | 15 ++++---- ...registerStorageUnitBackendHandlerTest.java | 24 ++++++------- .../version/ShardingSphereProxyVersion.java | 10 ++++-- .../ShardingSphereProxyVersionTest.java | 12 ++++--- 19 files changed, 139 insertions(+), 80 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java index 51cb72ca3f60d..f3b94298e40e3 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java @@ -25,9 +25,9 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; -import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource; import org.apache.shardingsphere.infra.instance.InstanceContext; 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.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilder; import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial; @@ -38,12 +38,15 @@ import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule; import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; +import javax.sql.DataSource; import java.sql.SQLException; import java.util.Collection; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Map; +import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; /** * ShardingSphere database. @@ -195,7 +198,9 @@ public synchronized void reloadRules(final Class r Collection databaseRules = new LinkedList<>(ruleMetaData.getRules()); toBeReloadedRules.stream().findFirst().ifPresent(optional -> { databaseRules.removeAll(toBeReloadedRules); - databaseRules.add(((MutableDataNodeRule) optional).reloadRule(ruleConfig, name, resourceMetaData.getStorageUnitMetaData().getDataSources(), databaseRules)); + Map dataSources = resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + databaseRules.add(((MutableDataNodeRule) optional).reloadRule(ruleConfig, name, dataSources, databaseRules)); }); ruleMetaData.getRules().clear(); ruleMetaData.getRules().addAll(databaseRules); diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java index 75bfe46bf3a20..4f4f36f8bca9b 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java @@ -36,8 +36,6 @@ public final class StorageUnitMetaData { private final Map storageUnits; - private final Map dataSources; - private final Map metaDataMap; public StorageUnitMetaData(final String databaseName, final Map storageNodes, final Map dataSourcePoolPropertiesMap, @@ -49,7 +47,5 @@ public StorageUnitMetaData(final String databaseName, final Map new StorageUnit(databaseName, dataSources.get(entry.getValue().getName()), dataSourcePoolPropertiesMap.get(entry.getKey()), entry.getValue()), (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(storageNodes.size(), 1F))); - this.dataSources = storageUnits.entrySet().stream().collect( - Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(storageUnits.size(), 1F))); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java index 7db610c192e3b..7f9f787464f77 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java @@ -53,8 +53,10 @@ public final class GenericSchemaBuilderMaterial { public GenericSchemaBuilderMaterial(final DatabaseType protocolType, final StorageUnitMetaData storageUnitMetaData, final Collection rules, final ConfigurationProperties props, final String defaultSchemaName) { this(protocolType, storageUnitMetaData.getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageType(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), - storageUnitMetaData.getDataSources(), rules, props, defaultSchemaName); + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageType(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), + storageUnitMetaData.getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), + rules, props, defaultSchemaName); } /** diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java index 6f5c1863eac6f..6450b42deb7c8 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java @@ -22,8 +22,11 @@ import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.datanode.DataNode; +import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; 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.NewStorageUnitMetaData; 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.database.schema.model.ShardingSphereSchema; @@ -146,10 +149,10 @@ private DataNode createDataNode(final String dataSourceName) { private ShardingSphereDatabase mockDatabaseWithMultipleResources() { ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - Map dataSourceMap = new HashMap<>(2, 1F); - dataSourceMap.put("ds_0", new MockedDataSource()); - dataSourceMap.put("ds_1", new MockedDataSource()); - when(result.getResourceMetaData().getStorageUnitMetaData().getDataSources()).thenReturn(dataSourceMap); + Map metaDataMap = new HashMap<>(2, 1F); + metaDataMap.put("ds_0", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + metaDataMap.put("ds_1", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + when(result.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); when(result.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); ShardingSphereSchema schema = mock(ShardingSphereSchema.class); when(schema.containsTable("t_order")).thenReturn(true); diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTableExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTableExecutor.java index 8f4897a99ee43..c22d06e587ce1 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTableExecutor.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTableExecutor.java @@ -30,9 +30,12 @@ import javax.sql.DataSource; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Map; +import java.util.Map.Entry; import java.util.Optional; +import java.util.stream.Collectors; /** * Show unloaded single table executor. @@ -61,7 +64,9 @@ public Collection getRows(final ShardingSphereDatabase private Map> getActualDataNodes(final ShardingSphereDatabase database) { ResourceMetaData resourceMetaData = database.getResourceMetaData(); Map aggregateDataSourceMap = SingleTableLoadUtils.getAggregatedDataSourceMap( - resourceMetaData.getStorageUnitMetaData().getDataSources(), database.getRuleMetaData().getRules()); + resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), + database.getRuleMetaData().getRules()); Collection excludedTables = SingleTableLoadUtils.getExcludedTables(database.getRuleMetaData().getRules()); return SingleTableDataNodeLoader.load(database.getName(), database.getProtocolType(), aggregateDataSourceMap, excludedTables); } diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java index 63a134642e384..c86ccf435bcf8 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java @@ -41,6 +41,7 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Map; +import java.util.Map.Entry; import java.util.stream.Collectors; /** @@ -113,7 +114,9 @@ private void checkActualTableExist(final ShardingSphereDatabase database, final } ResourceMetaData resourceMetaData = database.getResourceMetaData(); Map aggregateDataSourceMap = SingleTableLoadUtils.getAggregatedDataSourceMap( - resourceMetaData.getStorageUnitMetaData().getDataSources(), database.getRuleMetaData().getRules()); + resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), + database.getRuleMetaData().getRules()); Map>> actualTableNodes = new LinkedHashMap<>(); for (String each : requiredDataSources) { Map> schemaTableNames = SingleTableDataNodeLoader.loadSchemaTableNames(database.getName(), database.getProtocolType(), aggregateDataSourceMap.get(each), each); 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 3165f0a0a6bb9..9e8bb9f4fa5de 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 @@ -17,17 +17,19 @@ package org.apache.shardingsphere.transaction.rule; +import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; 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.NewStorageUnitMetaData; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; import org.apache.shardingsphere.transaction.api.TransactionType; import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration; import org.apache.shardingsphere.transaction.core.fixture.ShardingSphereTransactionManagerFixture; import org.junit.jupiter.api.Test; -import javax.sql.DataSource; import java.util.Collections; -import java.util.LinkedHashMap; +import java.util.HashMap; import java.util.Map; import static org.hamcrest.CoreMatchers.instanceOf; @@ -60,14 +62,14 @@ void assertAddResource() { assertThat(actual.getDatabases().size(), is(2)); assertTrue(actual.getDatabases().containsKey(SHARDING_DB_1)); ResourceMetaData resourceMetaData1 = actual.getDatabases().get(SHARDING_DB_1).getResourceMetaData(); - assertThat(resourceMetaData1.getStorageUnitMetaData().getDataSources().size(), is(2)); - assertTrue(resourceMetaData1.getStorageUnitMetaData().getDataSources().containsKey("ds_0")); - assertTrue(resourceMetaData1.getStorageUnitMetaData().getDataSources().containsKey("ds_1")); + assertThat(resourceMetaData1.getStorageUnitMetaData().getMetaDataMap().size(), is(2)); + assertTrue(resourceMetaData1.getStorageUnitMetaData().getMetaDataMap().containsKey("ds_0")); + assertTrue(resourceMetaData1.getStorageUnitMetaData().getMetaDataMap().containsKey("ds_1")); assertTrue(actual.getDatabases().containsKey(SHARDING_DB_2)); ResourceMetaData resourceMetaData2 = actual.getDatabases().get(SHARDING_DB_2).getResourceMetaData(); - assertThat(resourceMetaData2.getStorageUnitMetaData().getDataSources().size(), is(2)); - assertTrue(resourceMetaData2.getStorageUnitMetaData().getDataSources().containsKey("ds_0")); - assertTrue(resourceMetaData2.getStorageUnitMetaData().getDataSources().containsKey("ds_1")); + assertThat(resourceMetaData2.getStorageUnitMetaData().getMetaDataMap().size(), is(2)); + assertTrue(resourceMetaData2.getStorageUnitMetaData().getMetaDataMap().containsKey("ds_0")); + assertTrue(resourceMetaData2.getStorageUnitMetaData().getMetaDataMap().containsKey("ds_1")); assertThat(actual.getResource().getTransactionManager(TransactionType.XA), instanceOf(ShardingSphereTransactionManagerFixture.class)); } @@ -97,10 +99,10 @@ private ShardingSphereDatabase createDatabase() { private ResourceMetaData createResourceMetaData() { ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); - Map dataSourceMap = new LinkedHashMap<>(2, 1F); - dataSourceMap.put("ds_0", new MockedDataSource()); - dataSourceMap.put("ds_1", new MockedDataSource()); - when(result.getStorageUnitMetaData().getDataSources()).thenReturn(dataSourceMap); + Map metaDataMap = new HashMap<>(2, 1F); + metaDataMap.put("ds_0", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + metaDataMap.put("ds_1", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + when(result.getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); return result; } @@ -114,10 +116,10 @@ private ShardingSphereDatabase createAddDatabase() { private ResourceMetaData createAddResourceMetaData() { ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); - Map dataSourceMap = new LinkedHashMap<>(2, 1F); - dataSourceMap.put("ds_0", new MockedDataSource()); - dataSourceMap.put("ds_1", new MockedDataSource()); - when(result.getStorageUnitMetaData().getDataSources()).thenReturn(dataSourceMap); + Map metaDataMap = new HashMap<>(2, 1F); + metaDataMap.put("ds_0", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + metaDataMap.put("ds_1", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + when(result.getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); return result; } 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 3cec415a40fa5..46d138e001f23 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 @@ -175,7 +175,9 @@ public synchronized void alterRuleConfiguration(final String databaseName, final Collection rules = new LinkedList<>(database.getRuleMetaData().getRules()); rules.removeIf(each -> each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass())); rules.addAll(DatabaseRulesBuilder.build(databaseName, - database.getResourceMetaData().getStorageUnitMetaData().getDataSources(), database.getRuleMetaData().getRules(), ruleConfig, instanceContext)); + database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), + database.getRuleMetaData().getRules(), ruleConfig, instanceContext)); refreshMetadata(databaseName, database, rules); } catch (final SQLException ex) { log.error("Alter database: {} rule configurations failed", databaseName, ex); @@ -195,7 +197,9 @@ public synchronized void dropRuleConfiguration(final String databaseName, final rules.removeIf(each -> each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass())); if (isNotEmptyConfig(ruleConfig)) { rules.addAll(DatabaseRulesBuilder.build(databaseName, - database.getResourceMetaData().getStorageUnitMetaData().getDataSources(), database.getRuleMetaData().getRules(), ruleConfig, instanceContext)); + database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), + database.getRuleMetaData().getRules(), ruleConfig, instanceContext)); } refreshMetadata(databaseName, database, rules); } catch (final SQLException ex) { diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java index 7a7a2d8e0c311..79efada097c8d 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java @@ -46,6 +46,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -146,7 +147,8 @@ private static void persistDatabaseConfigurations(final MetaDataContexts metadat for (Entry entry : param.getDatabaseConfigs().entrySet()) { String databaseName = entry.getKey(); metadataContexts.getPersistService().persistConfigurations(entry.getKey(), entry.getValue(), - metadataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getDataSources(), + metadataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, each -> each.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), metadataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getRules()); } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/NewMetaDataContextsFactory.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/NewMetaDataContextsFactory.java index 199911a512d2c..ce6f65cce7c55 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/NewMetaDataContextsFactory.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/NewMetaDataContextsFactory.java @@ -26,8 +26,6 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration; -import org.apache.shardingsphere.infra.state.datasource.DataSourceState; -import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaData; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; @@ -35,6 +33,8 @@ import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder; +import org.apache.shardingsphere.infra.state.datasource.DataSourceState; +import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import org.apache.shardingsphere.metadata.factory.ExternalMetaDataFactory; import org.apache.shardingsphere.metadata.factory.NewInternalMetaDataFactory; import org.apache.shardingsphere.metadata.persist.NewMetaDataPersistService; @@ -46,6 +46,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -148,7 +149,8 @@ private static void persistDatabaseConfigurations(final MetaDataContexts metadat for (Entry entry : param.getDatabaseConfigs().entrySet()) { String databaseName = entry.getKey(); metadataContexts.getPersistService().persistConfigurations(entry.getKey(), entry.getValue(), - metadataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getDataSources(), + metadataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, each -> each.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), metadataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getRules()); } } diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java index f1c1ddbebd192..2386abdbb2fdb 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java @@ -109,6 +109,9 @@ private ShardingSphereDatabase mockDatabase() { StorageUnit storageUnit = mock(StorageUnit.class); when(storageUnit.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); when(result.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); + Map metaDataMap = Collections.singletonMap("foo_ds", + new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + when(result.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); return result; } @@ -250,11 +253,9 @@ void assertAlterDataSourceConfiguration() { } private ResourceMetaData createOriginalResource() { - ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); Map originalDataSources = new LinkedHashMap<>(2, 1F); originalDataSources.put("ds_1", new MockedDataSource()); originalDataSources.put("ds_2", new MockedDataSource()); - when(result.getStorageUnitMetaData().getDataSources()).thenReturn(originalDataSources); Map storageNodeDataSourceMap = StorageNodeUtils.getStorageNodeDataSources(originalDataSources); Map storageUnits = new LinkedHashMap<>(2, 1F); Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSources(originalDataSources); @@ -264,6 +265,7 @@ private ResourceMetaData createOriginalResource() { metaDataMap.put(entry.getKey(), new NewStorageUnitMetaData( "foo_db", storageUnitNodeMap.get(entry.getKey()), mock(DataSourcePoolProperties.class), storageNodeDataSourceMap.get(entry.getValue().getName()))); } + ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); when(result.getStorageUnitMetaData().getStorageUnits()).thenReturn(storageUnits); when(result.getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); when(result.getDataSources()).thenReturn(storageNodeDataSourceMap); @@ -304,14 +306,12 @@ void assertReloadSchema() { @Test void assertReloadTable() { - when(metaDataContexts.getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnitMetaData().getDataSources()) - .thenReturn(Collections.singletonMap("foo_ds", new MockedDataSource())); DatabaseMetaDataPersistService databaseMetaDataPersistService = mock(DatabaseMetaDataPersistService.class, RETURNS_DEEP_STUBS); MetaDataPersistService persistService = mock(MetaDataPersistService.class); when(persistService.getDatabaseMetaDataService()).thenReturn(databaseMetaDataPersistService); when(metaDataContexts.getPersistService()).thenReturn(persistService); contextManager.reloadTable("foo_db", "foo_schema", "foo_table"); - assertTrue(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnitMetaData().getDataSources().containsKey("foo_ds")); + assertTrue(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().containsKey("foo_ds")); } private Map createProperties(final String username, final String password) { diff --git a/proxy/backend/core/pom.xml b/proxy/backend/core/pom.xml index 6f208a63187b9..1f2ae164d00b0 100644 --- a/proxy/backend/core/pom.xml +++ b/proxy/backend/core/pom.xml @@ -246,5 +246,9 @@ org.awaitility awaitility + + mysql + mysql-connector-java + diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/NewRuleDefinitionBackendHandler.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/NewRuleDefinitionBackendHandler.java index 24ad12ffa15c5..7e74c242bcde9 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/NewRuleDefinitionBackendHandler.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/NewRuleDefinitionBackendHandler.java @@ -25,11 +25,11 @@ import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator; import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; +import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException; import org.apache.shardingsphere.infra.instance.mode.ModeContextManager; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion; import org.apache.shardingsphere.infra.rule.identifier.type.StaticDataSourceContainedRule; -import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlRuleConfigurationSwapperEngine; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; @@ -46,7 +46,10 @@ import java.util.Collection; import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map.Entry; import java.util.Optional; +import java.util.stream.Collectors; /** * TODO Rename to RuleDefinitionBackendHandler when metadata structure adjustment completed. #25485 @@ -130,7 +133,9 @@ private Collection processAlter(final ShardingSphereDatabase da private RuleConfiguration decorateRuleConfiguration(final ShardingSphereDatabase database, final RuleConfiguration ruleConfig) { Optional decorator = TypedSPILoader.findService(RuleConfigurationDecorator.class, ruleConfig.getClass()); return decorator.map(optional -> optional.decorate(database.getName(), - database.getResourceMetaData().getStorageUnitMetaData().getDataSources(), database.getRuleMetaData().getRules(), ruleConfig)).orElse(ruleConfig); + database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), + database.getRuleMetaData().getRules(), ruleConfig)).orElse(ruleConfig); } @SuppressWarnings({"rawtypes", "unchecked"}) diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/RuleDefinitionBackendHandler.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/RuleDefinitionBackendHandler.java index 8181ed052dfa2..561d0ce961ad7 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/RuleDefinitionBackendHandler.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/RuleDefinitionBackendHandler.java @@ -24,9 +24,9 @@ import org.apache.shardingsphere.distsql.parser.statement.rdl.RuleDefinitionStatement; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.config.rule.decorator.RuleConfigurationDecorator; +import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.rule.identifier.type.StaticDataSourceContainedRule; -import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.handler.distsql.rdl.RDLBackendHandler; @@ -38,8 +38,11 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; import java.util.Collection; +import java.util.LinkedHashMap; import java.util.LinkedList; +import java.util.Map.Entry; import java.util.Optional; +import java.util.stream.Collectors; /** * Rule definition backend handler. @@ -102,7 +105,9 @@ private Collection processSQLStatement(final ShardingSphereDa private RuleConfiguration decorateRuleConfiguration(final ShardingSphereDatabase database, final RuleConfiguration ruleConfig) { Optional decorator = TypedSPILoader.findService(RuleConfigurationDecorator.class, ruleConfig.getClass()); return decorator.map(optional -> optional.decorate(database.getName(), - database.getResourceMetaData().getStorageUnitMetaData().getDataSources(), database.getRuleMetaData().getRules(), ruleConfig)).orElse(ruleConfig); + database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), + database.getRuleMetaData().getRules(), ruleConfig)).orElse(ruleConfig); } @SuppressWarnings({"rawtypes", "unchecked"}) diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java index 2c1748977a1b7..ac0f3f150703d 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java @@ -21,7 +21,6 @@ import org.apache.shardingsphere.broadcast.rule.BroadcastRule; import org.apache.shardingsphere.broadcast.yaml.config.YamlBroadcastRuleConfiguration; import org.apache.shardingsphere.broadcast.yaml.swapper.YamlBroadcastRuleConfigurationSwapper; -import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.DistSQLException; import org.apache.shardingsphere.distsql.handler.exception.datasource.MissingRequiredDataSourcesException; import org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException; import org.apache.shardingsphere.distsql.handler.validate.DataSourcePoolPropertiesValidateHandler; @@ -37,13 +36,17 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration; import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator; -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.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException; +import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.DistSQLException; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; -import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; -import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException; import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration; import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration; import org.apache.shardingsphere.mask.rule.MaskRule; @@ -77,7 +80,6 @@ import org.apache.shardingsphere.single.yaml.config.pojo.YamlSingleRuleConfiguration; import org.apache.shardingsphere.single.yaml.config.swapper.YamlSingleRuleConfigurationSwapper; -import javax.sql.DataSource; import java.sql.SQLException; import java.util.Collection; import java.util.Collections; @@ -158,9 +160,12 @@ private void addResources(final String databaseName, final Map dataSource = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData() - .getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getDataSources(); - propsMap.forEach((key, value) -> dataSource.put(key, DataSourcePoolCreator.create(value))); + Map metaDataMap = ProxyContext.getInstance().getContextManager() + .getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(); + Map toBeAddedStorageNode = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(propsMap); + for (Entry entry : propsMap.entrySet()) { + metaDataMap.put(entry.getKey(), new NewStorageUnitMetaData(databaseName, toBeAddedStorageNode.get(entry.getKey()), entry.getValue(), DataSourcePoolCreator.create(entry.getValue()))); + } } private void addRules(final String databaseName, final Collection yamlRuleConfigs) { @@ -279,13 +284,18 @@ private void addMaskRuleConfiguration(final MaskRuleConfiguration maskRuleConfig private void addBroadcastRuleConfiguration(final BroadcastRuleConfiguration broadcastRuleConfig, final Collection allRuleConfigs, final ShardingSphereDatabase database) { allRuleConfigs.add(broadcastRuleConfig); - database.getRuleMetaData().getRules().add(new BroadcastRule(broadcastRuleConfig, database.getName(), database.getResourceMetaData().getStorageUnitMetaData().getDataSources())); + database.getRuleMetaData().getRules().add(new BroadcastRule(broadcastRuleConfig, database.getName(), + database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)))); } private void addSingleRuleConfiguration(final SingleRuleConfiguration broadcastRuleConfig, final Collection allRuleConfigs, final ShardingSphereDatabase database) { allRuleConfigs.add(broadcastRuleConfig); database.getRuleMetaData().getRules().add( - new SingleRule(broadcastRuleConfig, database.getName(), database.getResourceMetaData().getStorageUnitMetaData().getDataSources(), database.getRuleMetaData().getRules())); + new SingleRule(broadcastRuleConfig, database.getName(), + database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), + database.getRuleMetaData().getRules())); } private void dropDatabase(final String databaseName) { diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataUpdaterTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataUpdaterTest.java index 7ef71d693daac..2bd2642b379a8 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataUpdaterTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataUpdaterTest.java @@ -22,8 +22,11 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereIndex; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; @@ -41,7 +44,6 @@ import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; -import javax.sql.DataSource; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -108,17 +110,18 @@ private ContextManager mockContextManager(final String feature) { if (null != feature) { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(new ShardingSphereSchema(createTableMap(), Collections.emptyMap())); - when(database.getResourceMetaData().getStorageUnitMetaData().getDataSources()).thenReturn(createDataSourceMap()); + Map metaDataMap = createStorageUnitMetaDataMap(); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); when(result.getMetaDataContexts().getMetaData().getDatabases()).thenReturn(Collections.singletonMap(feature, database)); when(result.getMetaDataContexts().getMetaData().getDatabase(feature)).thenReturn(database); } return result; } - private Map createDataSourceMap() { - Map result = new LinkedHashMap<>(2, 1F); - result.put("ds_0", new MockedDataSource()); - result.put("ds_1", new MockedDataSource()); + private Map createStorageUnitMetaDataMap() { + Map result = new LinkedHashMap<>(2, 1F); + result.put("ds_0", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); + result.put("ds_1", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); return result; } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java index 23bb236e926c2..d027cc176af0a 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java @@ -21,10 +21,13 @@ import org.apache.shardingsphere.distsql.handler.exception.storageunit.StorageUnitInUsedException; import org.apache.shardingsphere.distsql.parser.statement.rdl.drop.UnregisterStorageUnitStatement; import org.apache.shardingsphere.infra.datanode.DataNode; +import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.DistSQLException; import org.apache.shardingsphere.infra.instance.mode.ModeContextManager; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; 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.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.mode.manager.ContextManager; @@ -34,6 +37,7 @@ import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; import org.apache.shardingsphere.shadow.rule.ShadowRule; import org.apache.shardingsphere.single.rule.SingleRule; +import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; import org.junit.jupiter.api.BeforeEach; @@ -44,7 +48,6 @@ import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; -import javax.sql.DataSource; import java.sql.SQLException; import java.util.Collections; @@ -67,9 +70,6 @@ class UnregisterStorageUnitBackendHandlerTest { @Mock private ResourceMetaData resourceMetaData; - @Mock - private DataSource dataSource; - @Mock private ShadowRule shadowRule; @@ -86,7 +86,8 @@ class UnregisterStorageUnitBackendHandlerTest { @BeforeEach void setUp() { resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); - when(resourceMetaData.getStorageUnitMetaData().getDataSources()).thenReturn(Collections.singletonMap("foo_ds", dataSource)); + NewStorageUnitMetaData storageUnitMetaData = new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource()); + when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); contextManager = mockContextManager(); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); @@ -106,7 +107,7 @@ private ContextManager mockContextManager() { @Test void assertExecute() throws SQLException { StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getDataSource()).thenReturn(dataSource); + when(storageUnit.getDataSource()).thenReturn(new MockedDataSource()); when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db")).thenReturn(database); @@ -117,9 +118,8 @@ void assertExecute() throws SQLException { @Test void assertStorageUnitNameNotExistedExecute() { - when(ProxyContext.getInstance().getDatabase("foo_db").getResourceMetaData().getStorageUnitMetaData().getDataSources()).thenReturn(Collections.emptyMap()); - assertThrows(MissingRequiredStorageUnitsException.class, - () -> handler.execute("foo_db", new UnregisterStorageUnitStatement(Collections.singleton("foo_ds"), false))); + when(ProxyContext.getInstance().getDatabase("foo_db").getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.emptyMap()); + assertThrows(MissingRequiredStorageUnitsException.class, () -> handler.execute("foo_db", new UnregisterStorageUnitStatement(Collections.singleton("foo_ds"), false))); } @Test @@ -127,7 +127,7 @@ void assertStorageUnitNameInUseExecute() { when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(shadowRule))); when(shadowRule.getDataSourceMapper()).thenReturn(Collections.singletonMap("", Collections.singleton("foo_ds"))); StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getDataSource()).thenReturn(dataSource); + when(storageUnit.getDataSource()).thenReturn(new MockedDataSource()); when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db")).thenReturn(database); @@ -142,7 +142,7 @@ void assertStorageUnitNameInUseWithoutIgnoreSingleTables() { when(dataNode.getDataSourceName()).thenReturn("foo_ds"); when(singleRule.getAllDataNodes()).thenReturn(Collections.singletonMap("", Collections.singleton(dataNode))); StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getDataSource()).thenReturn(dataSource); + when(storageUnit.getDataSource()).thenReturn(new MockedDataSource()); when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db")).thenReturn(database); @@ -156,7 +156,7 @@ void assertStorageUnitNameInUseIgnoreSingleTables() throws SQLException { when(dataNode.getDataSourceName()).thenReturn("foo_ds"); when(singleRule.getAllDataNodes()).thenReturn(Collections.singletonMap("", Collections.singleton(dataNode))); StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getDataSource()).thenReturn(dataSource); + when(storageUnit.getDataSource()).thenReturn(new MockedDataSource()); when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db")).thenReturn(database); diff --git a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java index 4781eb78d3326..017f878109af4 100644 --- a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java +++ b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java @@ -25,15 +25,18 @@ import org.apache.shardingsphere.db.protocol.constant.DatabaseProtocolServerInfo; import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; -import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; +import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.proxy.database.DatabaseServerInfo; import javax.sql.DataSource; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.Map.Entry; import java.util.Optional; +import java.util.stream.Collectors; /** * ShardingSphere-Proxy version. @@ -75,7 +78,8 @@ private static void setDatabaseVersion(final ShardingSphereDatabase database) { private static Optional findDataSourceByProtocolType(final String databaseName, final ResourceMetaData resourceMetaData, final DatabaseType protocolType) { Optional dataSourceName = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet() .stream().filter(entry -> entry.getValue().getStorageType().equals(protocolType)).map(Entry::getKey).findFirst(); - return dataSourceName.flatMap(optional -> Optional.ofNullable( - DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, resourceMetaData.getStorageUnitMetaData().getDataSources()).get(optional))); + Map dataSources = resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + return dataSourceName.flatMap(optional -> Optional.ofNullable(DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, dataSources).get(optional))); } } diff --git a/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java b/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java index e368386eb4136..26a73589af23c 100644 --- a/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java +++ b/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java @@ -19,11 +19,15 @@ import org.apache.shardingsphere.db.protocol.constant.DatabaseProtocolServerInfo; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; 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.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; +import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; import org.junit.jupiter.api.Test; @@ -79,18 +83,18 @@ private ShardingSphereDatabase mockDatabase(final String databaseProductName, fi private ResourceMetaData mockResourceMetaData(final String databaseProductName, final String databaseProductVersion) throws SQLException { ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); DataSource dataSource = createDataSource(databaseProductName, databaseProductVersion); - when(result.getStorageUnitMetaData().getDataSources()).thenReturn(Collections.singletonMap("foo_ds", dataSource)); + NewStorageUnitMetaData storageUnitMetaData = new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), dataSource); + when(result.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); return result; } private DataSource createDataSource(final String databaseProductName, final String databaseProductVersion) throws SQLException { - DataSource result = mock(DataSource.class); Connection connection = mock(Connection.class); DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class); when(databaseMetaData.getDatabaseProductName()).thenReturn(databaseProductName); when(databaseMetaData.getDatabaseProductVersion()).thenReturn(databaseProductVersion); when(connection.getMetaData()).thenReturn(databaseMetaData); - when(result.getConnection()).thenReturn(connection); - return result; + when(connection.getMetaData().getURL()).thenReturn("jdbc:mock://127.0.0.1/foo_ds"); + return new MockedDataSource(connection); } } From f28ce1084c6b9655427aaefa926b213443c7b110 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 29 Sep 2023 16:57:52 +0800 Subject: [PATCH 4/8] Remove StorageUnitMetaData.storageUnits --- .../impl/proxy/ProxyMetaDataInfoExporter.java | 2 +- .../proxy/ProxyMetaDataInfoExporterTest.java | 4 +-- .../CreateEncryptRuleStatementUpdater.java | 2 +- ...eadwriteSplittingRuleStatementChecker.java | 4 +-- ...riteSplittingRuleStatementUpdaterTest.java | 4 +-- .../ShardingStatisticsTableCollector.java | 10 +++---- .../metadata/ShardingSphereMetaData.java | 2 +- .../database/ShardingSphereDatabase.java | 4 +-- .../database/resource/ResourceMetaData.java | 14 ++++----- .../resource/unit/StorageUnitMetaData.java | 6 ---- .../builder/GenericSchemaBuilderMaterial.java | 4 +-- ...ShardingSphereTableDataCollectorUtils.java | 6 ++-- .../metadata/ShardingSphereMetaDataTest.java | 9 +++--- .../driver/DriverExecutionPrepareEngine.java | 2 +- .../infra/rewrite/SQLRewriteEntry.java | 5 +++- .../infra/rewrite/SQLRewriteEntryTest.java | 13 ++++++--- .../engine/impl/AllSQLRouteExecutor.java | 2 +- .../engine/impl/PartialSQLRouteExecutor.java | 12 ++++---- .../route/engine/AllSQLRouteExecutorTest.java | 2 +- .../impl/PartialSQLRouteExecutorTest.java | 10 +++---- .../data/pipeline/cdc/api/impl/CDCJobAPI.java | 4 +-- .../migration/api/impl/MigrationJobAPI.java | 4 +-- .../core/rule/GlobalClockRule.java | 4 +-- .../factory/ExternalMetaDataFactoryTest.java | 6 ++-- .../single/route/SingleSQLRouter.java | 4 +-- .../single/route/SingleSQLRouterTest.java | 5 ++-- ...ingleTableStorageUnitStatementUpdater.java | 2 +- .../transaction/rule/TransactionRule.java | 5 ++-- .../builder/TransactionRuleBuilderTest.java | 2 +- .../mode/manager/ContextManager.java | 12 +++++--- .../context/ConfigurationContextManager.java | 12 ++++---- .../switcher/ResourceSwitchManager.java | 29 +++++++++---------- .../mode/manager/ContextManagerTest.java | 16 ++++------ .../ConfigurationChangedSubscriberTest.java | 2 +- ...ResourceMetaDataChangedSubscriberTest.java | 2 +- .../datasource/JDBCBackendDataSource.java | 2 +- .../AbstractDatabaseMetaDataExecutor.java | 8 ++--- .../queryable/ExportStorageNodesExecutor.java | 4 +-- .../unit/AlterStorageUnitBackendHandler.java | 10 +++---- .../UnregisterStorageUnitBackendHandler.java | 6 ++-- .../ShowRulesUsedStorageUnitExecutor.java | 2 +- .../storage/unit/ShowStorageUnitExecutor.java | 19 ++++++------ .../proxy/backend/util/ExportUtils.java | 6 ++-- ...mlDatabaseConfigurationImportExecutor.java | 4 +-- .../datasource/JDBCBackendDataSourceTest.java | 2 +- ...portDatabaseConfigurationExecutorTest.java | 16 ++++++---- .../queryable/ExportMetaDataExecutorTest.java | 22 +++++++------- .../ExportStorageNodesExecutorTest.java | 28 +++++++++++++----- .../AlterStorageUnitBackendHandlerTest.java | 14 ++++----- ...registerStorageUnitBackendHandlerTest.java | 25 ++++++++-------- .../SelectInformationSchemataExecutor.java | 2 +- .../version/ShardingSphereProxyVersion.java | 4 +-- .../FrontDatabaseProtocolTypeFactory.java | 2 +- .../MySQLMultiStatementsHandlerTest.java | 13 ++++----- .../OpenGaussComBatchBindExecutorTest.java | 8 ++--- ...dBatchedStatementsCommandExecutorTest.java | 8 ++--- ...stgreSQLBatchedStatementsExecutorTest.java | 8 ++--- .../PostgreSQLComDescribeExecutorTest.java | 10 +++---- .../test/it/rewrite/engine/SQLRewriterIT.java | 13 +++++---- 59 files changed, 240 insertions(+), 222 deletions(-) diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporter.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporter.java index 19bd583fe30c0..bb59d70b0720d 100644 --- a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporter.java +++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporter.java @@ -53,6 +53,6 @@ public Optional export(final String pluginTyp } private int getStorageUnitCount(final MetaDataContexts metaDataContexts) { - return metaDataContexts.getMetaData().getDatabases().values().stream().map(each -> each.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().size()).reduce(0, Integer::sum); + return metaDataContexts.getMetaData().getDatabases().values().stream().map(each -> each.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().size()).reduce(0, Integer::sum); } } diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java index 48130603b17e8..fc33163d59747 100644 --- a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java +++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/proxy/ProxyMetaDataInfoExporterTest.java @@ -25,7 +25,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; @@ -75,7 +75,7 @@ void assertExportWithContextManager() { private ContextManager mockContextManager() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("ds_0", mock(StorageUnit.class))); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("ds_0", mock(NewStorageUnitMetaData.class))); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class); when(metaData.getDatabases()).thenReturn(Collections.singletonMap("sharding_db", database)); diff --git a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleStatementUpdater.java b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleStatementUpdater.java index fd89993ba7801..bcb0f68f0bf8e 100644 --- a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleStatementUpdater.java +++ b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleStatementUpdater.java @@ -114,7 +114,7 @@ private void checkToBeCreatedEncryptors(final CreateEncryptRuleStatement sqlStat } private void checkDataSources(final ShardingSphereDatabase database) { - ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().isEmpty(), () -> new EmptyStorageUnitException(database.getName())); + ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().isEmpty(), () -> new EmptyStorageUnitException(database.getName())); } @Override diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java index 2ade1dc59738e..16db29ea71662 100644 --- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java +++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java @@ -126,8 +126,8 @@ private static Collection getDuplicated(final Collection require private static void checkDuplicateRuleNamesWithExistsDataSources(final ShardingSphereDatabase database, final Collection segments) { Collection currentRuleNames = new HashSet<>(); ResourceMetaData resourceMetaData = database.getResourceMetaData(); - if (null != resourceMetaData && null != resourceMetaData.getStorageUnitMetaData().getStorageUnits()) { - currentRuleNames.addAll(resourceMetaData.getStorageUnitMetaData().getStorageUnits().keySet()); + if (null != resourceMetaData && null != resourceMetaData.getStorageUnitMetaData().getMetaDataMap()) { + currentRuleNames.addAll(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().keySet()); } currentRuleNames.addAll(getLogicDataSources(database)); Collection toBeCreatedRuleNames = segments.stream().map(ReadwriteSplittingRuleSegment::getName).filter(currentRuleNames::contains).collect(Collectors.toList()); diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java index e78281d115f30..7d4943e637916 100644 --- a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java +++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java @@ -74,13 +74,13 @@ void before() { @Test void assertCheckSQLStatementWithDuplicateRuleNames() { - when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.emptyMap()); + when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.emptyMap()); assertThrows(DuplicateRuleException.class, () -> updater.checkSQLStatement(database, createSQLStatement("TEST"), createCurrentRuleConfiguration())); } @Test void assertCheckSQLStatementWithDuplicateResource() { - when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("write_ds", null)); + when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("write_ds", null)); assertThrows(InvalidRuleConfigurationException.class, () -> updater.checkSQLStatement(database, createSQLStatement("write_ds", "TEST"), createCurrentRuleConfiguration())); } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollector.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollector.java index 09fa3c5268507..7c1639db66d35 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollector.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollector.java @@ -23,7 +23,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData; @@ -81,18 +81,18 @@ private void collectForShardingStatisticTable(final ShardingSphereDatabase datab row.add(each.getLogicTable()); row.add(dataNode.getDataSourceName()); row.add(dataNode.getTableName()); - addTableRowsAndDataLength(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits(), dataNode, row); + addTableRowsAndDataLength(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(), dataNode, row); tableData.getRows().add(new ShardingSphereRowData(row)); } } } - private void addTableRowsAndDataLength(final Map storageUnits, final DataNode dataNode, final List row) throws SQLException { - DatabaseType databaseType = storageUnits.get(dataNode.getDataSourceName()).getStorageType(); + private void addTableRowsAndDataLength(final Map metaDataMap, final DataNode dataNode, final List row) throws SQLException { + DatabaseType databaseType = metaDataMap.get(dataNode.getDataSourceName()).getStorageUnit().getStorageType(); Optional dialectCollector = DatabaseTypedSPILoader.findService(DialectShardingStatisticsTableCollector.class, databaseType); boolean isAppended = false; if (dialectCollector.isPresent()) { - try (Connection connection = storageUnits.get(dataNode.getDataSourceName()).getDataSource().getConnection()) { + try (Connection connection = metaDataMap.get(dataNode.getDataSourceName()).getDataSource().getConnection()) { isAppended = dialectCollector.get().appendRow(connection, dataNode, row); } } 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 648bb580df4c5..6fe38c9890c41 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 @@ -113,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 -> new DataSourcePoolDestroyer(each.getDataSource()).asyncDestroy())); + .ifPresent(optional -> optional.getStorageUnitMetaData().getMetaDataMap().values().forEach(each -> new DataSourcePoolDestroyer(each.getDataSource()).asyncDestroy())); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java index f3b94298e40e3..b98d04ca9bd1b 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java @@ -175,7 +175,7 @@ public void dropSchema(final String schemaName) { * @return is completed or not */ public boolean isComplete() { - return !ruleMetaData.getRules().isEmpty() && !resourceMetaData.getStorageUnitMetaData().getStorageUnits().isEmpty(); + return !ruleMetaData.getRules().isEmpty() && !resourceMetaData.getStorageUnitMetaData().getMetaDataMap().isEmpty(); } /** @@ -184,7 +184,7 @@ public boolean isComplete() { * @return contains data source or not */ public boolean containsDataSource() { - return !resourceMetaData.getStorageUnitMetaData().getStorageUnits().isEmpty(); + return !resourceMetaData.getStorageUnitMetaData().getMetaDataMap().isEmpty(); } /** 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 2f5e5dfda4f50..9be01787019b9 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 @@ -25,7 +25,7 @@ import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeUtils; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; @@ -68,7 +68,7 @@ public ResourceMetaData(final String databaseName, final Map getAllInstanceDataSourceNames() { Collection result = new LinkedList<>(); - for (Entry entry : storageUnitMetaData.getStorageUnits().entrySet()) { + for (Entry entry : storageUnitMetaData.getMetaDataMap().entrySet()) { if (!isExisted(entry.getKey(), result)) { result.add(entry.getKey()); } @@ -77,8 +77,8 @@ public Collection getAllInstanceDataSourceNames() { } private boolean isExisted(final String dataSourceName, final Collection existedDataSourceNames) { - return existedDataSourceNames.stream().anyMatch(each -> storageUnitMetaData.getStorageUnits().get(dataSourceName).getConnectionProperties() - .isInSameDatabaseInstance(storageUnitMetaData.getStorageUnits().get(each).getConnectionProperties())); + return existedDataSourceNames.stream().anyMatch(each -> storageUnitMetaData.getMetaDataMap().get(dataSourceName).getStorageUnit().getConnectionProperties() + .isInSameDatabaseInstance(storageUnitMetaData.getMetaDataMap().get(each).getStorageUnit().getConnectionProperties())); } /** @@ -88,7 +88,7 @@ private boolean isExisted(final String dataSourceName, final Collection * @return connection properties */ public ConnectionProperties getConnectionProperties(final String dataSourceName) { - return storageUnitMetaData.getStorageUnits().get(dataSourceName).getConnectionProperties(); + return storageUnitMetaData.getMetaDataMap().get(dataSourceName).getStorageUnit().getConnectionProperties(); } /** @@ -98,7 +98,7 @@ public ConnectionProperties getConnectionProperties(final String dataSourceName) * @return storage type */ public DatabaseType getStorageType(final String dataSourceName) { - return storageUnitMetaData.getStorageUnits().get(dataSourceName).getStorageType(); + return storageUnitMetaData.getMetaDataMap().get(dataSourceName).getStorageUnit().getStorageType(); } /** @@ -108,6 +108,6 @@ public DatabaseType getStorageType(final String dataSourceName) { * @return not existed resource names */ public Collection getNotExistedDataSources(final Collection resourceNames) { - return resourceNames.stream().filter(each -> !storageUnitMetaData.getStorageUnits().containsKey(each)).collect(Collectors.toSet()); + return resourceNames.stream().filter(each -> !storageUnitMetaData.getMetaDataMap().containsKey(each)).collect(Collectors.toSet()); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java index 4f4f36f8bca9b..ad0dd43fae270 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java @@ -26,7 +26,6 @@ import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; -import java.util.stream.Collectors; /** * Storage unit meta data. @@ -34,8 +33,6 @@ @Getter public final class StorageUnitMetaData { - private final Map storageUnits; - private final Map metaDataMap; public StorageUnitMetaData(final String databaseName, final Map storageNodes, final Map dataSourcePoolPropertiesMap, @@ -44,8 +41,5 @@ public StorageUnitMetaData(final String databaseName, final Map entry : storageNodes.entrySet()) { metaDataMap.put(entry.getKey(), new NewStorageUnitMetaData(databaseName, entry.getValue(), dataSourcePoolPropertiesMap.get(entry.getKey()), dataSources.get(entry.getValue().getName()))); } - storageUnits = storageNodes.entrySet().stream().collect( - Collectors.toMap(Entry::getKey, entry -> new StorageUnit(databaseName, dataSources.get(entry.getValue().getName()), dataSourcePoolPropertiesMap.get(entry.getKey()), entry.getValue()), - (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(storageNodes.size(), 1F))); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java index 7f9f787464f77..5303dcf574e81 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java @@ -52,8 +52,8 @@ public final class GenericSchemaBuilderMaterial { public GenericSchemaBuilderMaterial(final DatabaseType protocolType, final StorageUnitMetaData storageUnitMetaData, final Collection rules, final ConfigurationProperties props, final String defaultSchemaName) { - this(protocolType, storageUnitMetaData.getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageType(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), + this(protocolType, storageUnitMetaData.getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageUnit().getStorageType(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), storageUnitMetaData.getMetaDataMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), rules, props, defaultSchemaName); diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/ShardingSphereTableDataCollectorUtils.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/ShardingSphereTableDataCollectorUtils.java index c45a1830d2963..037b5583d1eb7 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/ShardingSphereTableDataCollectorUtils.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/ShardingSphereTableDataCollectorUtils.java @@ -20,7 +20,7 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData; @@ -57,7 +57,7 @@ public static Collection collectRowData(final ShardingSph return Collections.emptyList(); } Collection result = new LinkedList<>(); - for (StorageUnit each : database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().values()) { + for (NewStorageUnitMetaData each : database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().values()) { try ( Connection connection = each.getDataSource().getConnection(); Statement statement = connection.createStatement(); @@ -69,7 +69,7 @@ public static Collection collectRowData(final ShardingSph } private static boolean isDifferentProtocolAndStorageType(final ShardingSphereDatabase database) { - return !database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().values().stream().allMatch(each -> each.getStorageType().equals(database.getProtocolType())); + return !database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().values().stream().allMatch(each -> each.getStorageUnit().getStorageType().equals(database.getProtocolType())); } private static Collection getRows(final ShardingSphereTable table, final Collection selectedColumnNames, final ResultSet resultSet) throws SQLException { 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 d9c1147adbdb9..4aa84ca8dbf60 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 @@ -19,9 +19,11 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.rule.identifier.type.ResourceHeldRule; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; @@ -88,9 +90,8 @@ private ShardingSphereDatabase mockDatabase(final ResourceMetaData resourceMetaD ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); when(result.getName()).thenReturn("foo_db"); when(result.getResourceMetaData()).thenReturn(resourceMetaData); - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getDataSource()).thenReturn(dataSource); - when(result.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_db", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), dataSource); + when(result.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("foo_db", storageUnitMetaData)); when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(databaseResourceHeldRule))); return result; } diff --git a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/DriverExecutionPrepareEngine.java b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/DriverExecutionPrepareEngine.java index d7520e3065b64..8e607ca3bb175 100644 --- a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/DriverExecutionPrepareEngine.java +++ b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/driver/DriverExecutionPrepareEngine.java @@ -97,7 +97,7 @@ protected List> group(final String dataSourceName, final int c @SuppressWarnings("unchecked") private ExecutionGroup createExecutionGroup(final String dataSourceName, final List sqlUnits, final C connection, final ConnectionMode connectionMode) throws SQLException { List inputs = new LinkedList<>(); - DatabaseType databaseType = storageUnitMetaData.getStorageUnits().get(dataSourceName).getStorageType(); + DatabaseType databaseType = storageUnitMetaData.getMetaDataMap().get(dataSourceName).getStorageUnit().getStorageType(); for (SQLUnit each : sqlUnits) { inputs.add((T) sqlExecutionUnitBuilder.build(new ExecutionUnit(dataSourceName, each), statementManager, connection, connectionMode, option, databaseType)); } diff --git a/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java b/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java index a6be57c329ff3..15681b968ba5c 100644 --- a/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java +++ b/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java @@ -35,9 +35,11 @@ import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader; import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.stream.Collectors; /** * SQL rewrite entry. @@ -77,7 +79,8 @@ public SQLRewriteResult rewrite(final String sql, final List params, fin SQLRewriteContext sqlRewriteContext = createSQLRewriteContext(sql, params, sqlStatementContext, routeContext, connectionContext, hintValueContext); SQLTranslatorRule rule = globalRuleMetaData.getSingleRule(SQLTranslatorRule.class); DatabaseType protocolType = database.getProtocolType(); - Map storageUnits = database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits(); + Map storageUnits = database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageUnit(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); return routeContext.getRouteUnits().isEmpty() ? new GenericSQLRewriteEngine(rule, protocolType, storageUnits).rewrite(sqlRewriteContext) : new RouteSQLRewriteEngine(rule, protocolType, storageUnits).rewrite(sqlRewriteContext, routeContext); diff --git a/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntryTest.java b/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntryTest.java index 20a3f9f7d88ea..0e40c97e1d0c8 100644 --- a/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntryTest.java +++ b/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntryTest.java @@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; 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.database.schema.model.ShardingSphereSchema; @@ -83,15 +84,19 @@ void assertRewriteForRouteSQLRewriteResult() { } private ResourceMetaData mockResourceMetaData() { - Map storageUnits = new LinkedHashMap<>(2, 1F); StorageUnit storageUnit1 = mock(StorageUnit.class); when(storageUnit1.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "H2")); StorageUnit storageUnit2 = mock(StorageUnit.class); when(storageUnit2.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL")); - storageUnits.put("ds_0", storageUnit1); - storageUnits.put("ds_1", storageUnit2); + Map metaDataMap = new LinkedHashMap<>(2, 1F); + NewStorageUnitMetaData storageUnitMetaData1 = mock(NewStorageUnitMetaData.class); + when(storageUnitMetaData1.getStorageUnit()).thenReturn(storageUnit1); + NewStorageUnitMetaData storageUnitMetaData2 = mock(NewStorageUnitMetaData.class); + when(storageUnitMetaData2.getStorageUnit()).thenReturn(storageUnit2); + metaDataMap.put("ds_0", storageUnitMetaData1); + metaDataMap.put("ds_1", storageUnitMetaData2); ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); - when(result.getStorageUnitMetaData().getStorageUnits()).thenReturn(storageUnits); + when(result.getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); return result; } } diff --git a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java index cc22cbd17fcf5..72979865f6783 100644 --- a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java +++ b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java @@ -36,7 +36,7 @@ public final class AllSQLRouteExecutor implements SQLRouteExecutor { @Override public RouteContext route(final ConnectionContext connectionContext, final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database) { RouteContext result = new RouteContext(); - for (String each : database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().keySet()) { + for (String each : database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().keySet()) { result.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())); } return result; diff --git a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java index 97570a05fd505..2ea31c3580c11 100644 --- a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java +++ b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java @@ -22,7 +22,7 @@ import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.hint.SQLHintDataSourceNotExistsException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.route.SQLRouter; import org.apache.shardingsphere.infra.route.context.RouteContext; @@ -59,7 +59,7 @@ public PartialSQLRouteExecutor(final Collection rules, final @SuppressWarnings({"unchecked", "rawtypes"}) public RouteContext route(final ConnectionContext connectionContext, final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database) { RouteContext result = new RouteContext(); - Optional dataSourceName = findDataSourceByHint(queryContext.getHintValueContext(), database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()); + Optional dataSourceName = findDataSourceByHint(queryContext.getHintValueContext(), database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()); if (dataSourceName.isPresent()) { result.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName.get(), dataSourceName.get()), Collections.emptyList())); return result; @@ -71,16 +71,16 @@ public RouteContext route(final ConnectionContext connectionContext, final Query entry.getValue().decorateRouteContext(result, queryContext, database, entry.getKey(), props, connectionContext); } } - if (result.getRouteUnits().isEmpty() && 1 == database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().size()) { - String singleDataSourceName = database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().keySet().iterator().next(); + if (result.getRouteUnits().isEmpty() && 1 == database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().size()) { + String singleDataSourceName = database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().keySet().iterator().next(); result.getRouteUnits().add(new RouteUnit(new RouteMapper(singleDataSourceName, singleDataSourceName), Collections.emptyList())); } return result; } - private Optional findDataSourceByHint(final HintValueContext hintValueContext, final Map storageUnits) { + private Optional findDataSourceByHint(final HintValueContext hintValueContext, final Map metaDataMap) { Optional result = HintManager.isInstantiated() && HintManager.getDataSourceName().isPresent() ? HintManager.getDataSourceName() : hintValueContext.findHintDataSourceName(); - if (result.isPresent() && !storageUnits.containsKey(result.get())) { + if (result.isPresent() && !metaDataMap.containsKey(result.get())) { throw new SQLHintDataSourceNotExistsException(result.get()); } return result; diff --git a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/AllSQLRouteExecutorTest.java b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/AllSQLRouteExecutorTest.java index cce673b123904..e5743ab64323e 100644 --- a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/AllSQLRouteExecutorTest.java +++ b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/AllSQLRouteExecutorTest.java @@ -42,7 +42,7 @@ class AllSQLRouteExecutorTest { void assertRouteSuccess() { String name = "test"; ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().keySet()).thenReturn(Stream.of(name).collect(Collectors.toSet())); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().keySet()).thenReturn(Stream.of(name).collect(Collectors.toSet())); AllSQLRouteExecutor allSQLRouteExecutor = new AllSQLRouteExecutor(); RouteContext actual = allSQLRouteExecutor.route(new ConnectionContext(), mock(QueryContext.class), mock(RuleMetaData.class), database); assertThat(actual.getRouteUnits().size(), is(1)); diff --git a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutorTest.java b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutorTest.java index 58f70706b2d26..8c56d1b616807 100644 --- a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutorTest.java +++ b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutorTest.java @@ -23,7 +23,7 @@ import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.hint.SQLHintDataSourceNotExistsException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.session.connection.ConnectionContext; @@ -65,10 +65,10 @@ class PartialSQLRouteExecutorTest { @BeforeEach void setup() { - Map storageUnits = new HashMap<>(); - storageUnits.put("ds_0", null); - storageUnits.put("ds_1", null); - when(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(storageUnits); + Map metaDataMap = new HashMap<>(); + metaDataMap.put("ds_0", null); + metaDataMap.put("ds_1", null); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); } @Test diff --git a/kernel/data-pipeline/scenario/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/api/impl/CDCJobAPI.java b/kernel/data-pipeline/scenario/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/api/impl/CDCJobAPI.java index ec651c8b4b80c..2bcadf7dd73c5 100644 --- a/kernel/data-pipeline/scenario/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/api/impl/CDCJobAPI.java +++ b/kernel/data-pipeline/scenario/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/api/impl/CDCJobAPI.java @@ -78,7 +78,7 @@ import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration; import org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper; @@ -160,7 +160,7 @@ private YamlCDCJobConfiguration getYamlCDCJobConfiguration(final StreamDataParam private ShardingSpherePipelineDataSourceConfiguration getDataSourceConfiguration(final ShardingSphereDatabase database) { Map> dataSourcePoolProps = new HashMap<>(); - for (Entry entry : database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().entrySet()) { + for (Entry entry : database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet()) { dataSourcePoolProps.put(entry.getKey(), dataSourceConfigSwapper.swapToMap(entry.getValue().getDataSourcePoolProperties())); } YamlRootConfiguration targetRootConfig = new YamlRootConfiguration(); diff --git a/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/api/impl/MigrationJobAPI.java b/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/api/impl/MigrationJobAPI.java index 9181d6397e78a..582f9f6d93ab7 100644 --- a/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/api/impl/MigrationJobAPI.java +++ b/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/api/impl/MigrationJobAPI.java @@ -85,7 +85,7 @@ import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.infra.util.json.JsonUtils; import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration; @@ -191,7 +191,7 @@ private YamlPipelineDataSourceConfiguration buildYamlPipelineDataSourceConfigura private PipelineDataSourceConfiguration buildTargetPipelineDataSourceConfiguration(final ShardingSphereDatabase targetDatabase) { Map> targetPoolProps = new HashMap<>(); YamlDataSourceConfigurationSwapper dataSourceConfigSwapper = new YamlDataSourceConfigurationSwapper(); - for (Entry entry : targetDatabase.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().entrySet()) { + for (Entry entry : targetDatabase.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet()) { targetPoolProps.put(entry.getKey(), dataSourceConfigSwapper.swapToMap(entry.getValue().getDataSourcePoolProperties())); } YamlRootConfiguration targetRootConfig = buildYamlRootConfiguration(targetDatabase.getName(), targetPoolProps, targetDatabase.getRuleMetaData().getConfigurations()); diff --git a/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/rule/GlobalClockRule.java b/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/rule/GlobalClockRule.java index 8312d63bd65e9..11c91a00409ab 100644 --- a/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/rule/GlobalClockRule.java +++ b/kernel/global-clock/core/src/main/java/org/apache/shardingsphere/globalclock/core/rule/GlobalClockRule.java @@ -23,7 +23,6 @@ import org.apache.shardingsphere.infra.database.DatabaseTypeEngine; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.rule.identifier.scope.GlobalRule; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.transaction.spi.TransactionHook; @@ -61,7 +60,8 @@ private Properties createProperties(final Map da } private Optional findStorageType(final Collection databases) { - return databases.stream().flatMap(each -> each.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().values().stream()).findFirst().map(StorageUnit::getStorageType); + return databases.stream() + .flatMap(each -> each.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().values().stream()).findFirst().map(each -> each.getStorageUnit().getStorageType()); } /** diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactoryTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactoryTest.java index f5a5b39bce5b4..110bf526f13b9 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactoryTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/factory/ExternalMetaDataFactoryTest.java @@ -41,7 +41,7 @@ void assertCreateSingleDatabase() throws SQLException { DatabaseConfiguration databaseConfig = new DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(), Collections.emptyList()); ShardingSphereDatabase actual = ExternalMetaDataFactory.create("foo_db", databaseConfig, new ConfigurationProperties(new Properties()), mock(InstanceContext.class)); assertThat(actual.getName(), is("foo_db")); - assertTrue(actual.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().isEmpty()); + assertTrue(actual.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().isEmpty()); } @Test @@ -50,7 +50,7 @@ void assertCreateDatabaseMap() throws SQLException { Map actual = ExternalMetaDataFactory.create( Collections.singletonMap("foo_db", databaseConfig), new ConfigurationProperties(new Properties()), mock(InstanceContext.class)); assertTrue(actual.containsKey("foo_db")); - assertTrue(actual.get("foo_db").getResourceMetaData().getStorageUnitMetaData().getStorageUnits().isEmpty()); + assertTrue(actual.get("foo_db").getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().isEmpty()); } @Test @@ -59,6 +59,6 @@ void assertCreateDatabaseMapWhenConfigUppercaseDatabaseName() throws SQLExceptio Map actual = ExternalMetaDataFactory.create( Collections.singletonMap("FOO_DB", databaseConfig), new ConfigurationProperties(new Properties()), mock(InstanceContext.class)); assertTrue(actual.containsKey("foo_db")); - assertTrue(actual.get("foo_db").getResourceMetaData().getStorageUnitMetaData().getStorageUnits().isEmpty()); + assertTrue(actual.get("foo_db").getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().isEmpty()); } } diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java index c3dbc63496ea2..e818800e42cb5 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java @@ -47,7 +47,7 @@ public final class SingleSQLRouter implements SQLRouter { @Override public RouteContext createRouteContext(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ShardingSphereDatabase database, final SingleRule rule, final ConfigurationProperties props, final ConnectionContext connectionContext) { - if (1 == database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().size()) { + if (1 == database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().size()) { return createSingleDataSourceRouteContext(rule, database, queryContext); } RouteContext result = new RouteContext(); @@ -82,7 +82,7 @@ public void decorateRouteContext(final RouteContext routeContext, final QueryCon private RouteContext createSingleDataSourceRouteContext(final SingleRule rule, final ShardingSphereDatabase database, final QueryContext queryContext) { String logicDataSource = rule.getDataSourceNames().iterator().next(); - String actualDataSource = database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().keySet().iterator().next(); + String actualDataSource = database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().keySet().iterator().next(); RouteContext result = new RouteContext(); result.getRouteUnits().add(new RouteUnit(new RouteMapper(logicDataSource, actualDataSource), createTableMappers(queryContext.getSqlStatementContext().getTablesContext().getTableNames()))); return result; diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java index 6450b42deb7c8..ac631ea82d724 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java @@ -27,7 +27,6 @@ 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.NewStorageUnitMetaData; -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.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.route.SQLRouter; @@ -86,7 +85,7 @@ void assertCreateRouteContextWithSingleDataSource() throws SQLException { private ShardingSphereDatabase mockSingleDatabase() { ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", mock(StorageUnit.class))); + when(result.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", mock(NewStorageUnitMetaData.class))); return result; } @@ -108,7 +107,7 @@ void assertCreateRouteContextWithReadwriteSplittingDataSource() throws SQLExcept private ShardingSphereDatabase mockReadwriteSplittingDatabase() { ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(result.getName()).thenReturn(" db_schema"); - when(result.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("write_ds", mock(StorageUnit.class))); + when(result.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("write_ds", mock(NewStorageUnitMetaData.class))); return result; } diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitStatementUpdater.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitStatementUpdater.java index f353939f28650..4ecf23f65e5b8 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitStatementUpdater.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitStatementUpdater.java @@ -40,7 +40,7 @@ public void checkSQLStatement(final ShardingSphereDatabase database, final SetDe private void checkStorageUnitExist(final ShardingSphereDatabase database, final SetDefaultSingleTableStorageUnitStatement sqlStatement) { if (!Strings.isNullOrEmpty(sqlStatement.getDefaultStorageUnit())) { - Collection storageUnitNames = database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().keySet(); + Collection storageUnitNames = database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().keySet(); ShardingSpherePreconditions.checkState(storageUnitNames.contains(sqlStatement.getDefaultStorageUnit()), () -> new MissingRequiredStorageUnitsException(database.getName(), Collections.singleton(sqlStatement.getDefaultStorageUnit()))); } 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 ce567379c569a..1f23ac6395e8d 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 @@ -71,8 +71,9 @@ private synchronized ShardingSphereTransactionManagerEngine createTransactionMan Map databaseTypes = new LinkedHashMap<>(databases.size(), 1F); for (Entry entry : databases.entrySet()) { ShardingSphereDatabase database = entry.getValue(); - database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().forEach((key, value) -> dataSourceMap.put(database.getName() + "." + key, value.getDataSource())); - database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().forEach((key, value) -> databaseTypes.put(database.getName() + "." + key, value.getStorageType())); + database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().forEach((key, value) -> dataSourceMap.put(database.getName() + "." + key, value.getDataSource())); + database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap() + .forEach((key, value) -> databaseTypes.put(database.getName() + "." + key, value.getStorageUnit().getStorageType())); } if (dataSourceMap.isEmpty()) { return new ShardingSphereTransactionManagerEngine(defaultType); 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 61385c7450192..e14d8de11fde8 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 @@ -49,7 +49,7 @@ void assertBuild() { 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().getStorageUnitMetaData().getStorageUnits().size(), is(2)); + assertThat(rule.getDatabases().get("logic_db").getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().size(), is(2)); } private Map createDataSourceMap() { diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java index 71033b341b284..a49ed247c9b39 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java @@ -47,8 +47,11 @@ import java.sql.SQLException; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; /** * Context manager. @@ -105,7 +108,8 @@ public synchronized void renewMetaDataContexts(final MetaDataContexts metaDataCo * @return storage units */ public Map getStorageUnits(final String databaseName) { - return metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getStorageUnits(); + return metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageUnit(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); } /** @@ -182,8 +186,8 @@ private ShardingSphereSchema loadSchema(final String databaseName, final String ShardingSphereDatabase database = metaDataContexts.get().getMetaData().getDatabase(databaseName); database.reloadRules(MutableDataNodeRule.class); GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(database.getProtocolType(), - Collections.singletonMap(dataSourceName, database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().get(dataSourceName).getStorageType()), - Collections.singletonMap(dataSourceName, database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().get(dataSourceName).getDataSource()), + Collections.singletonMap(dataSourceName, database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().get(dataSourceName).getStorageUnit().getStorageType()), + Collections.singletonMap(dataSourceName, database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().get(dataSourceName).getDataSource()), database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName); ShardingSphereSchema result = GenericSchemaBuilder.build(material).get(schemaName); result.getViews().putAll(metaDataContexts.get().getPersistService().getDatabaseMetaDataService().getViewMetaDataPersistService().load(database.getName(), schemaName)); @@ -218,7 +222,7 @@ public void reloadTable(final String databaseName, final String schemaName, fina */ public void reloadTable(final String databaseName, final String schemaName, final String dataSourceName, final String tableName) { ShardingSphereDatabase database = metaDataContexts.get().getMetaData().getDatabase(databaseName); - StorageUnit storageUnit = database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().get(dataSourceName); + StorageUnit storageUnit = database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().get(dataSourceName).getStorageUnit(); GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(database.getProtocolType(), Collections.singletonMap(dataSourceName, storageUnit.getStorageType()), Collections.singletonMap(dataSourceName, storageUnit.getDataSource()), database.getRuleMetaData().getRules(), metaDataContexts.get().getMetaData().getProps(), schemaName); try { 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 46d138e001f23..a9cf6d9c37fa8 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 @@ -32,7 +32,7 @@ import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.SchemaManager; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; @@ -279,8 +279,8 @@ public void alterSchemaMetaData(final String databaseName, final ShardingSphereD */ public Map renewDatabase(final ShardingSphereDatabase database, final SwitchingResource resource) { Map newStorageNodes = getNewStorageNodes(database.getResourceMetaData().getDataSources(), resource); - Map newStorageUnitNodeMap = getNewStorageUnitNodeMap(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits(), resource); - Map propsMap = database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().entrySet().stream() + Map newStorageUnitNodeMap = getNewStorageUnitNodeMap(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(), resource); + Map propsMap = database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> currentValue, LinkedHashMap::new)); return Collections.singletonMap(database.getName().toLowerCase(), new ShardingSphereDatabase(database.getName(), database.getProtocolType(), new ResourceMetaData(database.getName(), newStorageNodes, newStorageUnitNodeMap, propsMap), database.getRuleMetaData(), database.getSchemas())); @@ -296,9 +296,9 @@ private Map getNewStorageNodes(final Map getNewStorageUnitNodeMap(final Map currentStorageUnits, final SwitchingResource resource) { - Map result = new LinkedHashMap<>(currentStorageUnits.size(), 1F); - for (Entry entry : currentStorageUnits.entrySet()) { + private Map getNewStorageUnitNodeMap(final Map currentStorageUnitMetaDataMap, final SwitchingResource resource) { + Map result = new LinkedHashMap<>(currentStorageUnitMetaDataMap.size(), 1F); + for (Entry entry : currentStorageUnitMetaDataMap.entrySet()) { if (!resource.getStaleStorageResource().getStorageUnitNodeMap().containsKey(entry.getKey())) { result.put(entry.getKey(), entry.getValue().getStorageNode()); } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java index 23f1f280dba5c..476ccadf5731f 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java @@ -24,7 +24,7 @@ import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; import javax.sql.DataSource; @@ -90,7 +90,7 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet Collection toBeChangedStorageNodeNames = toBeChangedStorageUnitNodeMap.values().stream().map(StorageNode::getName).collect(Collectors.toSet()); staleStorageResource.getDataSources().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSources(), toBeChangedStorageNodeNames)); staleStorageResource.getStorageUnitNodeMap().putAll( - getToBeDeletedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap.keySet())); + getToBeDeletedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getMetaDataMap(), toBeChangedStorageUnitNodeMap.keySet())); return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedStorageUnitNodeMap, toBeChangedPropsMap), staleStorageResource, mergedDataSourcePoolPropertiesMap); } @@ -117,8 +117,8 @@ private Map getNewStorageNodes(final ResourceMetaDa private Map getNewStorageUnitNodeMap(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMap) { Map result = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); - result.keySet().removeAll(getToBeDeletedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap.keySet()).keySet()); - result.putAll(getChangedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap)); + result.keySet().removeAll(getToBeDeletedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getMetaDataMap(), toBeChangedStorageUnitNodeMap.keySet()).keySet()); + result.putAll(getChangedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getMetaDataMap(), toBeChangedStorageUnitNodeMap)); result.putAll(getToBeAddedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), toBeChangedStorageUnitNodeMap)); return result; @@ -153,7 +153,7 @@ private Map getToBeAddedDataSources(final Map toRemovedStorageUnitNodeMap) { - Map reservedStorageUnitNodeMap = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet().stream() + Map reservedStorageUnitNodeMap = resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() .filter(entry -> !toRemovedStorageUnitNodeMap.containsKey(entry.getKey())) .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode())); Collection toBeRemovedStorageNodeNames = toRemovedStorageUnitNodeMap.values().stream().map(StorageNode::getName).collect(Collectors.toSet()); @@ -161,7 +161,7 @@ private StorageResource getToBeRemovedStaleDataSources(final ResourceMetaData re Map staleStorageNodes = resourceMetaData.getDataSources().entrySet().stream() .filter(entry -> toBeRemovedStorageNodeNames.contains(entry.getKey()) && !inUsedDataSourceNames.contains(entry.getKey())) .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); - Map staleStorageUnitNodeMap = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet().stream() + Map staleStorageUnitNodeMap = resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() .filter(entry -> !reservedStorageUnitNodeMap.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode())); return new StorageResource(staleStorageNodes, staleStorageUnitNodeMap); } @@ -171,7 +171,7 @@ private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaD Map storageNodes = new LinkedHashMap<>(resourceMetaData.getDataSources().size(), 1F); Map storageUnitNodeMap = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().size(), 1F); storageNodes.putAll(getToBeChangedDataSources(resourceMetaData.getDataSources(), StorageUnitNodeMapUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps))); - storageUnitNodeMap.putAll(getChangedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap)); + storageUnitNodeMap.putAll(getChangedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getMetaDataMap(), toBeChangedStorageUnitNodeMap)); return new StorageResource(storageNodes, storageUnitNodeMap); } @@ -198,10 +198,9 @@ private Map getToBeDeletedDataSources(final Map getToBeDeletedStorageUnitNodeMap(final Map storageUnits, - final Collection toBeChangedStorageUnitNames) { - Map result = new LinkedHashMap<>(storageUnits.size(), 1F); - for (Entry entry : storageUnits.entrySet()) { + private Map getToBeDeletedStorageUnitNodeMap(final Map storageUnitMetaDataMap, final Collection toBeChangedStorageUnitNames) { + Map result = new LinkedHashMap<>(storageUnitMetaDataMap.size(), 1F); + for (Entry entry : storageUnitMetaDataMap.entrySet()) { if (!toBeChangedStorageUnitNames.contains(entry.getKey())) { result.put(entry.getKey(), entry.getValue().getStorageNode()); } @@ -209,14 +208,14 @@ private Map getToBeDeletedStorageUnitNodeMap(final Map getChangedStorageUnitNodeMap(final Map storageUnits, final Map toBeChangedStorageUnitNodeMap) { - return toBeChangedStorageUnitNodeMap.entrySet().stream().filter(entry -> isModifiedStorageUnitNodeMap(storageUnits, entry.getKey(), entry.getValue())) + private Map getChangedStorageUnitNodeMap(final Map storageUnitMetaDataMap, final Map toBeChangedStorageUnitNodeMap) { + return toBeChangedStorageUnitNodeMap.entrySet().stream().filter(entry -> isModifiedStorageUnitNodeMap(storageUnitMetaDataMap, entry.getKey(), entry.getValue())) .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); } - private boolean isModifiedStorageUnitNodeMap(final Map originalStorageUnits, + private boolean isModifiedStorageUnitNodeMap(final Map originalstorageUnitMetaDataMap, final String dataSourceName, final StorageNode storageNode) { - return originalStorageUnits.containsKey(dataSourceName) && !storageNode.equals(originalStorageUnits.get(dataSourceName).getStorageNode()); + return originalstorageUnitMetaDataMap.containsKey(dataSourceName) && !storageNode.equals(originalstorageUnitMetaDataMap.get(dataSourceName).getStorageNode()); } private Map getToBeAddedStorageUnitNodeMap(final Map storageUnitNodeMap, final Map toBeChangedStorageUnitNodeMap) { diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java index 2386abdbb2fdb..20d309e66766a 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java @@ -32,7 +32,6 @@ import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeUtils; import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; @@ -106,9 +105,9 @@ private ShardingSphereDatabase mockDatabase() { when(mutableDataNodeRule.findTableDataNode("foo_schema", "foo_tbl")).thenReturn(Optional.of(mock(DataNode.class))); when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(mutableDataNodeRule))); when(result.getSchemas()).thenReturn(new HashMap<>(Collections.singletonMap("foo_schema", new ShardingSphereSchema()))); - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); - when(result.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class, RETURNS_DEEP_STUBS); + when(storageUnitMetaData.getStorageUnit().getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + when(result.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); Map metaDataMap = Collections.singletonMap("foo_ds", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource())); when(result.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); @@ -229,7 +228,7 @@ void assertAlterRuleConfiguration() { when(resourceMetaData.getDataSources()).thenReturn(StorageNodeUtils.getStorageNodeDataSources(dataSources)); StorageUnitMetaData storageUnitMetaData = mock(StorageUnitMetaData.class); when(resourceMetaData.getStorageUnitMetaData()).thenReturn(storageUnitMetaData); - when(storageUnitMetaData.getStorageUnits()).thenReturn(Collections.emptyMap()); + when(storageUnitMetaData.getMetaDataMap()).thenReturn(Collections.emptyMap()); ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), resourceMetaData, mock(RuleMetaData.class), Collections.emptyMap()); when(metaDataContexts.getMetaData().getDatabase("foo_db")).thenReturn(database); @@ -247,7 +246,7 @@ void assertAlterDataSourceConfiguration() { when(metaDataContexts.getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.emptyList())); contextManager.getConfigurationContextManager().alterDataSourceUnitsConfiguration("foo_db", Collections.singletonMap("foo_ds", new DataSourcePoolProperties(MockedDataSource.class.getName(), createProperties("test", "test")))); - assertThat(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnitMetaData().getStorageUnits().size(), is(3)); + assertThat(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().size(), is(3)); assertAlteredDataSource((MockedDataSource) contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db") .getResourceMetaData().getDataSources().get(new StorageNodeName("foo_ds"))); } @@ -257,16 +256,13 @@ private ResourceMetaData createOriginalResource() { originalDataSources.put("ds_1", new MockedDataSource()); originalDataSources.put("ds_2", new MockedDataSource()); Map storageNodeDataSourceMap = StorageNodeUtils.getStorageNodeDataSources(originalDataSources); - Map storageUnits = new LinkedHashMap<>(2, 1F); Map storageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSources(originalDataSources); Map metaDataMap = new LinkedHashMap<>(2, 1F); for (Entry entry : storageUnitNodeMap.entrySet()) { - storageUnits.put(entry.getKey(), new StorageUnit("foo_db", storageNodeDataSourceMap.get(entry.getValue().getName()), mock(DataSourcePoolProperties.class), entry.getValue())); metaDataMap.put(entry.getKey(), new NewStorageUnitMetaData( "foo_db", storageUnitNodeMap.get(entry.getKey()), mock(DataSourcePoolProperties.class), storageNodeDataSourceMap.get(entry.getValue().getName()))); } ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); - when(result.getStorageUnitMetaData().getStorageUnits()).thenReturn(storageUnits); when(result.getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); when(result.getDataSources()).thenReturn(storageNodeDataSourceMap); return result; @@ -294,8 +290,6 @@ void assertAlterProperties() { @Test void assertReloadSchema() { when(metaDataContexts.getMetaData().getDatabase("foo_db").getName()).thenReturn("foo_db"); - when(metaDataContexts.getMetaData().getDatabase("foo_db").getResourceMetaData() - .getStorageUnitMetaData().getStorageUnits().get("foo_ds").getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); DatabaseMetaDataPersistService databaseMetaDataPersistService = mock(DatabaseMetaDataPersistService.class, RETURNS_DEEP_STUBS); MetaDataPersistService persistService = mock(MetaDataPersistService.class); when(persistService.getDatabaseMetaDataService()).thenReturn(databaseMetaDataPersistService); diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java index b103a79268d64..2168bd2468e53 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java @@ -134,7 +134,7 @@ void assertRenewForRuleConfigurationsChanged() { @Test void assertRenewForDataSourceChanged() { subscriber.renew(new DataSourceUnitsChangedEvent("db", "0", createChangedDataSourcePoolPropertiesMap())); - assertTrue(contextManager.getMetaDataContexts().getMetaData().getDatabase("db").getResourceMetaData().getStorageUnitMetaData().getStorageUnits().containsKey("ds_2")); + assertTrue(contextManager.getMetaDataContexts().getMetaData().getDatabase("db").getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().containsKey("ds_2")); } private Map createChangedDataSourcePoolPropertiesMap() { diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ResourceMetaDataChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ResourceMetaDataChangedSubscriberTest.java index 7e32bcbf0733b..a86985b96d5ff 100644 --- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ResourceMetaDataChangedSubscriberTest.java +++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ResourceMetaDataChangedSubscriberTest.java @@ -114,7 +114,7 @@ void assertRenewForDatabaseAdded() { when(persistService.getDataSourceUnitService().load("db_added")).thenReturn(createDataSourcePoolPropertiesMap()); when(persistService.getDatabaseRulePersistService().load("db_added")).thenReturn(Collections.emptyList()); subscriber.renew(new DatabaseAddedEvent("db_added")); - assertNotNull(contextManager.getMetaDataContexts().getMetaData().getDatabase("db_added").getResourceMetaData().getStorageUnitMetaData().getStorageUnits()); + assertNotNull(contextManager.getMetaDataContexts().getMetaData().getDatabase("db_added").getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()); } private Map createDataSourcePoolPropertiesMap() { 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 eaa76e016a8d6..ad781d0cf92ac 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 @@ -69,7 +69,7 @@ public List getConnections(final String databaseName, final String d public List getConnections(final String databaseName, final String dataSourceName, final int connectionSize, final ConnectionMode connectionMode, final TransactionType transactionType) throws SQLException { DataSource dataSource = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData() - .getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getStorageUnits().get(dataSourceName).getDataSource(); + .getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().get(dataSourceName).getDataSource(); if (dataSourceName.contains(".")) { String dataSourceStr = dataSourceName.split("\\.")[0]; if (GlobalDataSourceRegistry.getInstance().getCachedDataSources().containsKey(dataSourceStr)) { diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/AbstractDatabaseMetaDataExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/AbstractDatabaseMetaDataExecutor.java index 7c716f6de1157..2291b340b737f 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/AbstractDatabaseMetaDataExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/AbstractDatabaseMetaDataExecutor.java @@ -31,7 +31,7 @@ import org.apache.shardingsphere.infra.merge.result.MergedResult; import org.apache.shardingsphere.infra.merge.result.impl.transparent.TransparentMergedResult; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.user.Grantee; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; @@ -160,12 +160,12 @@ protected Collection getDatabaseNames(final ConnectionSession connection @Override protected void processMetaData(final String databaseName, final Consumer callback) throws SQLException { ResourceMetaData resourceMetaData = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(); - Optional storageUnit = resourceMetaData.getStorageUnitMetaData().getStorageUnits().values().stream().findFirst(); - if (!storageUnit.isPresent()) { + Optional storageUnitMetaData = resourceMetaData.getStorageUnitMetaData().getMetaDataMap().values().stream().findFirst(); + if (!storageUnitMetaData.isPresent()) { return; } try ( - Connection connection = storageUnit.get().getDataSource().getConnection(); + Connection connection = storageUnitMetaData.get().getDataSource().getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { for (int i = 0; i < parameters.size(); i++) { preparedStatement.setObject(i + 1, parameters.get(i)); diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutor.java index 5f3f89c994e79..9f9d61ee2f65f 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutor.java @@ -25,7 +25,7 @@ import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.util.json.JsonUtils; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.distsql.export.ExportedStorageNode; @@ -87,7 +87,7 @@ private Map> getAllStorageNodes(final Sh private Map> generateDatabaseExportStorageNodesData(final ShardingSphereDatabase database) { Map storageNodes = new LinkedHashMap<>(); - for (Entry entry : database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().entrySet()) { + for (Entry entry : database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet()) { ConnectionProperties connectionProps = database.getResourceMetaData().getConnectionProperties(entry.getKey()); String databaseInstanceIp = getDatabaseInstanceIp(connectionProps); if (storageNodes.containsKey(databaseInstanceIp)) { diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/AlterStorageUnitBackendHandler.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/AlterStorageUnitBackendHandler.java index b66b9458e2e47..c3bd0c2ed2552 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/AlterStorageUnitBackendHandler.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/AlterStorageUnitBackendHandler.java @@ -34,7 +34,7 @@ import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.exception.core.external.ShardingSphereExternalException; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader; import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader; @@ -101,15 +101,15 @@ private Collection getDuplicatedStorageUnitNames(final Collection storageUnitNames) { - Map storageUnits = ProxyContext.getInstance().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getStorageUnits(); - Collection notExistedStorageUnitNames = storageUnitNames.stream().filter(each -> !storageUnits.containsKey(each)).collect(Collectors.toList()); + Map metaDataMap = ProxyContext.getInstance().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(); + Collection notExistedStorageUnitNames = storageUnitNames.stream().filter(each -> !metaDataMap.containsKey(each)).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(notExistedStorageUnitNames.isEmpty(), () -> new MissingRequiredStorageUnitsException(databaseName, notExistedStorageUnitNames)); } private void checkDatabase(final String databaseName, final AlterStorageUnitStatement sqlStatement) { - Map storageUnits = ProxyContext.getInstance().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getStorageUnits(); + Map metaDataMap = ProxyContext.getInstance().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(); Collection invalidStorageUnitNames = sqlStatement.getStorageUnits().stream().collect(Collectors.toMap(DataSourceSegment::getName, each -> each)).entrySet().stream() - .filter(each -> !isIdenticalDatabase(each.getValue(), storageUnits.get(each.getKey()).getDataSource())).map(Entry::getKey).collect(Collectors.toSet()); + .filter(each -> !isIdenticalDatabase(each.getValue(), metaDataMap.get(each.getKey()).getDataSource())).map(Entry::getKey).collect(Collectors.toSet()); ShardingSpherePreconditions.checkState(invalidStorageUnitNames.isEmpty(), () -> new InvalidStorageUnitsException(Collections.singleton(String.format("Cannot alter the database of %s", invalidStorageUnitNames)))); } diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandler.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandler.java index aedfcff6ed293..80df2fc3e17dd 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandler.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandler.java @@ -25,7 +25,7 @@ import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.exception.core.external.server.ShardingSphereServerException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader; import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader; @@ -71,7 +71,7 @@ public void checkSQLStatement(final String databaseName, final UnregisterStorage } private void checkExisted(final String databaseName, final Collection storageUnitNames) { - Map storageUnits = ProxyContext.getInstance().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getStorageUnits(); + Map storageUnits = ProxyContext.getInstance().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(); Collection notExistedStorageUnits = storageUnitNames.stream().filter(each -> !storageUnits.containsKey(each)).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(notExistedStorageUnits.isEmpty(), () -> new MissingRequiredStorageUnitsException(databaseName, notExistedStorageUnits)); } @@ -79,7 +79,7 @@ private void checkExisted(final String databaseName, final Collection st private void checkInUsed(final String databaseName, final UnregisterStorageUnitStatement sqlStatement) { ShardingSphereDatabase database = ProxyContext.getInstance().getDatabase(databaseName); Map> inUsedStorageUnits = StorageUnitUtils.getInUsedStorageUnits( - database.getRuleMetaData(), database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().size()); + database.getRuleMetaData(), database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().size()); Collection inUsedStorageUnitNames = inUsedStorageUnits.keySet(); inUsedStorageUnitNames.retainAll(sqlStatement.getStorageUnitNames()); if (!inUsedStorageUnitNames.isEmpty()) { diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowRulesUsedStorageUnitExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowRulesUsedStorageUnitExecutor.java index 143d26aaf52d3..38aa774bcb5dd 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowRulesUsedStorageUnitExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowRulesUsedStorageUnitExecutor.java @@ -62,7 +62,7 @@ public final class ShowRulesUsedStorageUnitExecutor implements RQLExecutor getRows(final ShardingSphereDatabase database, final ShowRulesUsedStorageUnitStatement sqlStatement) { Collection result = new LinkedList<>(); String resourceName = sqlStatement.getStorageUnitName().orElse(null); - if (database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().containsKey(resourceName)) { + if (database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().containsKey(resourceName)) { result.addAll(getShardingData(database)); result.addAll(getReadwriteSplittingData(database, resourceName)); result.addAll(getEncryptData(database)); diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/storage/unit/ShowStorageUnitExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/storage/unit/ShowStorageUnitExecutor.java index d795896420582..725603ef8f6d5 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/storage/unit/ShowStorageUnitExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/storage/unit/ShowStorageUnitExecutor.java @@ -29,7 +29,7 @@ import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.util.json.JsonUtils; import org.apache.shardingsphere.proxy.backend.util.StorageUnitUtils; @@ -90,23 +90,24 @@ private Map getCustomProps(final Map customProps } private Map getDataSourcePoolPropertiesMap(final ShardingSphereDatabase database, final ShowStorageUnitsStatement sqlStatement) { - Map result = new LinkedHashMap<>(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().size(), 1F); - Map propsMap = database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().entrySet().stream() + Map result = new LinkedHashMap<>(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().size(), 1F); + Map propsMap = database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> currentValue, LinkedHashMap::new)); - Map storageUnits = database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits(); + Map metaDataMap = database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(); Optional usageCount = sqlStatement.getUsageCount(); if (usageCount.isPresent()) { Map> inUsedStorageUnits = StorageUnitUtils.getInUsedStorageUnits( - database.getRuleMetaData(), database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().size()); - for (Entry entry : database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().entrySet()) { + database.getRuleMetaData(), database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().size()); + for (Entry entry : database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet()) { Integer currentUsageCount = inUsedStorageUnits.containsKey(entry.getKey()) ? inUsedStorageUnits.get(entry.getKey()).size() : 0; if (usageCount.get().equals(currentUsageCount)) { - result.put(entry.getKey(), getDataSourcePoolProperties(propsMap, entry.getKey(), storageUnits.get(entry.getKey()).getStorageType(), entry.getValue().getDataSource())); + result.put(entry.getKey(), + getDataSourcePoolProperties(propsMap, entry.getKey(), metaDataMap.get(entry.getKey()).getStorageUnit().getStorageType(), entry.getValue().getDataSource())); } } } else { - for (Entry entry : storageUnits.entrySet()) { - result.put(entry.getKey(), getDataSourcePoolProperties(propsMap, entry.getKey(), storageUnits.get(entry.getKey()).getStorageType(), entry.getValue().getDataSource())); + for (Entry entry : metaDataMap.entrySet()) { + result.put(entry.getKey(), getDataSourcePoolProperties(propsMap, entry.getKey(), metaDataMap.get(entry.getKey()).getStorageUnit().getStorageType(), entry.getValue().getDataSource())); } } return result; diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/ExportUtils.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/ExportUtils.java index 30898e073fafd..70a638d47a9ab 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/ExportUtils.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/ExportUtils.java @@ -23,7 +23,7 @@ import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader; import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper; @@ -84,11 +84,11 @@ private static void appendDatabaseName(final String databaseName, final StringBu } private static void appendDataSourceConfigurations(final ShardingSphereDatabase database, final StringBuilder stringBuilder) { - if (database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().isEmpty()) { + if (database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().isEmpty()) { return; } stringBuilder.append("dataSources:").append(System.lineSeparator()); - for (Entry entry : database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().entrySet()) { + for (Entry entry : database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet()) { appendDataSourceConfiguration(entry.getKey(), entry.getValue().getDataSourcePoolProperties(), stringBuilder); } } diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java index ac0f3f150703d..add3f8d268b76 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java @@ -132,7 +132,7 @@ public void importDatabaseConfiguration(final YamlProxyDatabaseConfiguration yam private void checkDatabase(final String databaseName) { ShardingSpherePreconditions.checkNotNull(databaseName, () -> new UnsupportedSQLOperationException("Property `databaseName` in imported config is required")); if (ProxyContext.getInstance().databaseExists(databaseName)) { - ShardingSpherePreconditions.checkState(ProxyContext.getInstance().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getStorageUnits().isEmpty(), + ShardingSpherePreconditions.checkState(ProxyContext.getInstance().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().isEmpty(), () -> new UnsupportedSQLOperationException(String.format("Database `%s` exists and is not empty,overwrite is not supported", databaseName))); } } @@ -252,7 +252,7 @@ private void addShardingRuleConfiguration(final ShardingRuleConfiguration shardi InstanceContext instanceContext = ProxyContext.getInstance().getContextManager().getInstanceContext(); shardingRuleConfigImportChecker.check(database, shardingRuleConfig); allRuleConfigs.add(shardingRuleConfig); - database.getRuleMetaData().getRules().add(new ShardingRule(shardingRuleConfig, database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().keySet(), instanceContext)); + database.getRuleMetaData().getRules().add(new ShardingRule(shardingRuleConfig, database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().keySet(), instanceContext)); } private void addReadwriteSplittingRuleConfiguration(final ReadwriteSplittingRuleConfiguration readwriteSplittingRuleConfig, 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 03ca65319fefd..f4c0b6501e3d2 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 @@ -96,7 +96,7 @@ private Map createDatabases() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getProtocolType()).thenReturn(databaseType); for (Entry entry : mockDataSources(2).entrySet()) { - when(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().get(entry.getKey()).getDataSource()).thenReturn(entry.getValue()); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().get(entry.getKey()).getDataSource()).thenReturn(entry.getValue()); } return Collections.singletonMap("schema", database); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportDatabaseConfigurationExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportDatabaseConfigurationExecutorTest.java index a26662361f982..cf8199123d6cd 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportDatabaseConfigurationExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportDatabaseConfigurationExecutorTest.java @@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; @@ -70,8 +71,8 @@ void assertGetColumns() { @Test void assertExecute() { when(database.getName()).thenReturn("normal_db"); - Map storageUnits = createStorageUnits(); - when(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(storageUnits); + Map metaDataMap = createStorageUnitMetaDataMap(); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.singleton(createShardingRuleConfiguration())); Collection actual = new ExportDatabaseConfigurationExecutor().getRows(database, new ExportDatabaseConfigurationStatement(mock(DatabaseSegment.class), null)); assertThat(actual.size(), is(1)); @@ -79,14 +80,17 @@ void assertExecute() { assertThat(row.getCell(1), is(loadExpectedRow())); } - private Map createStorageUnits() { + private Map createStorageUnitMetaDataMap() { Map propsMap = createDataSourceMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - Map result = new LinkedHashMap<>(); + Map result = new LinkedHashMap<>(); for (Entry entry : propsMap.entrySet()) { StorageUnit storageUnit = mock(StorageUnit.class); when(storageUnit.getDataSourcePoolProperties()).thenReturn(entry.getValue()); - result.put(entry.getKey(), storageUnit); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class, RETURNS_DEEP_STUBS); + when(storageUnitMetaData.getStorageUnit()).thenReturn(storageUnit); + when(storageUnitMetaData.getDataSourcePoolProperties()).thenReturn(entry.getValue()); + result.put(entry.getKey(), storageUnitMetaData); } return result; } @@ -94,7 +98,7 @@ private Map createStorageUnits() { @Test void assertExecuteWithEmptyDatabase() { when(database.getName()).thenReturn("empty_db"); - when(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.emptyMap()); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.emptyMap()); when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList()); ExportDatabaseConfigurationStatement sqlStatement = new ExportDatabaseConfigurationStatement(new DatabaseSegment(0, 0, new IdentifierValue("empty_db")), null); Collection actual = new ExportDatabaseConfigurationExecutor().getRows(database, sqlStatement); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java index 57179cdd5bcae..2a24cf8c69e1c 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java @@ -27,8 +27,8 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; -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.instance.ComputeNodeInstance; import org.apache.shardingsphere.infra.instance.InstanceContext; import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; @@ -38,10 +38,10 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.manager.standalone.workerid.generator.StandaloneWorkerIdGenerator; @@ -111,7 +111,7 @@ void assertExecuteWithEmptyMetaData() { when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); when(ProxyContext.getInstance().getAllDatabaseNames()).thenReturn(Collections.singleton("empty_metadata")); when(database.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singleton("empty_metadata")); - when(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.emptyMap()); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.emptyMap()); when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList()); ExportMetaDataStatement sqlStatement = new ExportMetaDataStatement(null); Collection actual = new ExportMetaDataExecutor().getRows(contextManager.getMetaDataContexts().getMetaData(), sqlStatement); @@ -134,8 +134,8 @@ private ContextManager mockEmptyContextManager() { void assertExecute() { when(database.getName()).thenReturn("normal_db"); when(database.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singleton("empty_metadata")); - Map storageUnits = createStorageUnits(); - when(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(storageUnits); + Map metaDataMap = createStorageUnitMetaDataMap(); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList()); ContextManager contextManager = mockContextManager(); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); @@ -147,14 +147,14 @@ void assertExecute() { assertThat(row.getCell(3).toString(), is(loadExpectedRow())); } - private Map createStorageUnits() { + private Map createStorageUnitMetaDataMap() { Map propsMap = createDataSourceMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - Map result = new LinkedHashMap<>(); + Map result = new LinkedHashMap<>(); for (Entry entry : propsMap.entrySet()) { - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getDataSourcePoolProperties()).thenReturn(entry.getValue()); - result.put(entry.getKey(), storageUnit); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class); + when(storageUnitMetaData.getDataSourcePoolProperties()).thenReturn(entry.getValue()); + result.put(entry.getKey(), storageUnitMetaData); } return result; } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java index c27871548eaa3..0d3f50a134d36 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java @@ -29,6 +29,7 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; @@ -54,6 +55,7 @@ import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; +import javax.sql.DataSource; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; @@ -128,8 +130,8 @@ private ContextManager mockEmptyContextManager() { @Test void assertExecute() { when(database.getName()).thenReturn("normal_db"); - Map storageUnits = createStorageUnits(); - when(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(storageUnits); + Map metaDataMap = createStorageUnitMetaDataMap(); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.singleton(createShardingRuleConfiguration())); ContextManager contextManager = mockContextManager(); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); @@ -142,8 +144,8 @@ void assertExecute() { @Test void assertExecuteWithDatabaseName() { when(database.getName()).thenReturn("normal_db"); - Map storageUnits = createStorageUnits(); - when(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(storageUnits); + Map metaDataMap = createStorageUnitMetaDataMap(); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.singleton(createShardingRuleConfiguration())); ContextManager contextManager = mockContextManager(); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); @@ -163,10 +165,20 @@ private ContextManager mockContextManager() { return result; } - private Map createStorageUnits() { - Map result = new LinkedHashMap<>(2, 1F); - result.put("ds_0", createStorageUnit("demo_ds_0")); - result.put("ds_1", createStorageUnit("demo_ds_1")); + private Map createStorageUnitMetaDataMap() { + NewStorageUnitMetaData storageUnitMetaData1 = mock(NewStorageUnitMetaData.class); + StorageUnit storageUnit1 = createStorageUnit("demo_ds_0"); + when(storageUnitMetaData1.getStorageUnit()).thenReturn(storageUnit1); + DataSource dataSource1 = storageUnit1.getDataSource(); + when(storageUnitMetaData1.getDataSource()).thenReturn(dataSource1); + NewStorageUnitMetaData storageUnitMetaData2 = mock(NewStorageUnitMetaData.class); + StorageUnit storageUnit2 = createStorageUnit("demo_ds_1"); + when(storageUnitMetaData2.getStorageUnit()).thenReturn(storageUnit2); + DataSource dataSource2 = storageUnit2.getDataSource(); + when(storageUnitMetaData2.getDataSource()).thenReturn(dataSource2); + Map result = new LinkedHashMap<>(2, 1F); + result.put("ds_0", storageUnitMetaData1); + result.put("ds_1", storageUnitMetaData2); return result; } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/AlterStorageUnitBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/AlterStorageUnitBackendHandlerTest.java index 524c1943b2dfe..45d66bf43b6b1 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/AlterStorageUnitBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/AlterStorageUnitBackendHandlerTest.java @@ -29,7 +29,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; @@ -80,9 +80,9 @@ void assertExecute() { when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); when(ProxyContext.getInstance().getDatabase("foo_db")).thenReturn(database); ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getDataSource()).thenReturn(mockHikariDataSource("ds_0")); - when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("ds_0", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class); + when(storageUnitMetaData.getDataSource()).thenReturn(mockHikariDataSource("ds_0")); + when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("ds_0", storageUnitMetaData)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); assertThat(handler.execute("foo_db", createAlterStorageUnitStatement("ds_0")), instanceOf(UpdateResponseHeader.class)); } @@ -107,9 +107,9 @@ void assertExecuteWithAlterDatabase() { when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); when(ProxyContext.getInstance().getDatabase("foo_db")).thenReturn(database); ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getDataSource()).thenReturn(mockHikariDataSource("ds_1")); - when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("ds_0", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class); + when(storageUnitMetaData.getDataSource()).thenReturn(mockHikariDataSource("ds_1")); + when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("ds_0", storageUnitMetaData)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); assertThrows(InvalidStorageUnitsException.class, () -> handler.execute("foo_db", createAlterStorageUnitStatement("ds_0"))); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java index d027cc176af0a..69af68c851adb 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java @@ -28,7 +28,6 @@ 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.NewStorageUnitMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.metadata.MetaDataContexts; @@ -106,9 +105,9 @@ private ContextManager mockContextManager() { @Test void assertExecute() throws SQLException { - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getDataSource()).thenReturn(new MockedDataSource()); - when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class); + when(storageUnitMetaData.getDataSource()).thenReturn(new MockedDataSource()); + when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db")).thenReturn(database); UnregisterStorageUnitStatement unregisterStorageUnitStatement = new UnregisterStorageUnitStatement(Collections.singleton("foo_ds"), false); @@ -126,9 +125,9 @@ void assertStorageUnitNameNotExistedExecute() { void assertStorageUnitNameInUseExecute() { when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(shadowRule))); when(shadowRule.getDataSourceMapper()).thenReturn(Collections.singletonMap("", Collections.singleton("foo_ds"))); - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getDataSource()).thenReturn(new MockedDataSource()); - when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class); + when(storageUnitMetaData.getDataSource()).thenReturn(new MockedDataSource()); + when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db")).thenReturn(database); assertThrows(StorageUnitInUsedException.class, @@ -141,9 +140,9 @@ void assertStorageUnitNameInUseWithoutIgnoreSingleTables() { DataNode dataNode = mock(DataNode.class); when(dataNode.getDataSourceName()).thenReturn("foo_ds"); when(singleRule.getAllDataNodes()).thenReturn(Collections.singletonMap("", Collections.singleton(dataNode))); - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getDataSource()).thenReturn(new MockedDataSource()); - when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class); + when(storageUnitMetaData.getDataSource()).thenReturn(new MockedDataSource()); + when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db")).thenReturn(database); assertThrows(StorageUnitInUsedException.class, () -> handler.execute("foo_db", new UnregisterStorageUnitStatement(Collections.singleton("foo_ds"), false))); @@ -155,9 +154,9 @@ void assertStorageUnitNameInUseIgnoreSingleTables() throws SQLException { DataNode dataNode = mock(DataNode.class); when(dataNode.getDataSourceName()).thenReturn("foo_ds"); when(singleRule.getAllDataNodes()).thenReturn(Collections.singletonMap("", Collections.singleton(dataNode))); - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getDataSource()).thenReturn(new MockedDataSource()); - when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class); + when(storageUnitMetaData.getDataSource()).thenReturn(new MockedDataSource()); + when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db")).thenReturn(database); UnregisterStorageUnitStatement unregisterStorageUnitStatement = new UnregisterStorageUnitStatement(Collections.singleton("foo_ds"), true); diff --git a/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/information/SelectInformationSchemataExecutor.java b/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/information/SelectInformationSchemataExecutor.java index ad3aa0e4c2a39..fb52e3c1892e1 100644 --- a/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/information/SelectInformationSchemataExecutor.java +++ b/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/information/SelectInformationSchemataExecutor.java @@ -95,7 +95,7 @@ protected Collection getDatabaseNames(final ConnectionSession connection @Override protected void preProcess(final String databaseName, final Map rows, final Map alias) { ResourceMetaData resourceMetaData = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(); - Collection catalogs = resourceMetaData.getStorageUnitMetaData().getStorageUnits().keySet() + Collection catalogs = resourceMetaData.getStorageUnitMetaData().getMetaDataMap().keySet() .stream().map(each -> resourceMetaData.getConnectionProperties(each).getCatalog()).collect(Collectors.toSet()); schemaNameAlias = alias.getOrDefault(SCHEMA_NAME, ""); String rowValue = rows.getOrDefault(schemaNameAlias, "").toString(); diff --git a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java index 017f878109af4..2d745e21811df 100644 --- a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java +++ b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersion.java @@ -76,8 +76,8 @@ private static void setDatabaseVersion(final ShardingSphereDatabase database) { } private static Optional findDataSourceByProtocolType(final String databaseName, final ResourceMetaData resourceMetaData, final DatabaseType protocolType) { - Optional dataSourceName = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet() - .stream().filter(entry -> entry.getValue().getStorageType().equals(protocolType)).map(Entry::getKey).findFirst(); + Optional dataSourceName = resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet() + .stream().filter(entry -> entry.getValue().getStorageUnit().getStorageType().equals(protocolType)).map(Entry::getKey).findFirst(); Map dataSources = resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); return dataSourceName.flatMap(optional -> Optional.ofNullable(DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, dataSources).get(optional))); diff --git a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactory.java b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactory.java index b6c1589fe84d8..7aae335bac2f9 100644 --- a/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactory.java +++ b/proxy/frontend/core/src/main/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactory.java @@ -51,7 +51,7 @@ public static DatabaseType getDatabaseType() { return TypedSPILoader.getService(DatabaseType.class, DEFAULT_FRONTEND_DATABASE_PROTOCOL_TYPE); } Optional database = metaDataContexts.getMetaData().getDatabases().values().stream().filter(ShardingSphereDatabase::containsDataSource).findFirst(); - return database.isPresent() ? database.get().getResourceMetaData().getStorageUnitMetaData().getStorageUnits().values().iterator().next().getStorageType() + return database.isPresent() ? database.get().getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().values().iterator().next().getStorageUnit().getStorageType() : TypedSPILoader.getService(DatabaseType.class, DEFAULT_FRONTEND_DATABASE_PROTOCOL_TYPE); } diff --git a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandlerTest.java b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandlerTest.java index af9db53e69108..5f2b906666c59 100644 --- a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandlerTest.java +++ b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandlerTest.java @@ -21,7 +21,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode; import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.StatementOption; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; @@ -104,12 +104,11 @@ private ConnectionSession mockConnectionSession() throws SQLException { private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - when(result.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getAllInstanceDataSourceNames()) - .thenReturn(Collections.singletonList("foo_ds")); - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); - when(result.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnitMetaData().getStorageUnits()) - .thenReturn(Collections.singletonMap("foo_ds", storageUnit)); + when(result.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singletonList("foo_ds")); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class, RETURNS_DEEP_STUBS); + when(storageUnitMetaData.getStorageUnit().getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + when(result.getMetaDataContexts().getMetaData().getDatabase("foo_db").getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()) + .thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); when(result.getMetaDataContexts().getMetaData().getDatabase("foo_db").getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL")); when(result.getMetaDataContexts().getMetaData().getDatabase("foo_db").getRuleMetaData()) .thenReturn(new RuleMetaData(Collections.emptyList())); diff --git a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java index 72a103934adcb..f9eaf2c296826 100644 --- a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java +++ b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java @@ -30,7 +30,7 @@ import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.StatementOption; import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine; @@ -140,9 +140,9 @@ private ContextManager mockContextManager() { private ShardingSphereDatabase mockDatabase() { ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(result.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singleton("foo_ds")); - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "openGauss")); - when(result.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class, RETURNS_DEEP_STUBS); + when(storageUnitMetaData.getStorageUnit().getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "openGauss")); + when(result.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.emptyList())); when(result.containsSchema("public")).thenReturn(true); when(result.getSchema("public").containsTable("bmsql")).thenReturn(true); diff --git a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutorTest.java b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutorTest.java index 4efc882c107ff..c0e6b196116a5 100644 --- a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutorTest.java +++ b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutorTest.java @@ -35,7 +35,7 @@ import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.StatementOption; import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine; @@ -158,9 +158,9 @@ private ContextManager mockContextManager() { when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singletonList("foo_ds")); - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")); - when(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class, RETURNS_DEEP_STUBS); + when(storageUnitMetaData.getStorageUnit().getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("foo_ds", storageUnitMetaData)); when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.emptyList())); when(database.containsSchema("public")).thenReturn(true); when(database.getSchema("public").containsTable("t_order")).thenReturn(true); diff --git a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutorTest.java b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutorTest.java index d6949b21877b2..57fba5cc5dde8 100644 --- a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutorTest.java +++ b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutorTest.java @@ -28,7 +28,7 @@ import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.StatementOption; import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; import org.apache.shardingsphere.infra.session.connection.ConnectionContext; @@ -133,9 +133,9 @@ private ContextManager mockContextManager() { when(result.getMetaDataContexts().getMetaData().getProps().getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY)).thenReturn(1); when(result.getMetaDataContexts().getMetaData().getProps().getValue(ConfigurationPropertyKey.SQL_SHOW)).thenReturn(false); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")); - when(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("ds_0", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class, RETURNS_DEEP_STUBS); + when(storageUnitMetaData.getStorageUnit().getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")); + when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("ds_0", storageUnitMetaData)); when(database.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singletonList("ds_0")); when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.emptyList())); when(database.containsSchema("public")).thenReturn(true); diff --git a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutorTest.java b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutorTest.java index ce9e9cc467c28..0a1dd82995b3c 100644 --- a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutorTest.java +++ b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/describe/PostgreSQLComDescribeExecutorTest.java @@ -36,7 +36,7 @@ import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode; import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; @@ -400,10 +400,10 @@ private ContextManager mockContextManager() { when(schema.getTable(TABLE_NAME)).thenReturn(table); when(schema.getAllColumnNames(TABLE_NAME)).thenReturn(Arrays.asList("id", "k", "c", "pad")); when(result.getMetaDataContexts().getMetaData().getDatabase(DATABASE_NAME).getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")); - StorageUnit storageUnit = mock(StorageUnit.class); - when(storageUnit.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")); - when(result.getMetaDataContexts().getMetaData().getDatabase(DATABASE_NAME).getResourceMetaData().getStorageUnitMetaData().getStorageUnits()) - .thenReturn(Collections.singletonMap("ds_0", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class, RETURNS_DEEP_STUBS); + when(storageUnitMetaData.getStorageUnit().getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")); + when(result.getMetaDataContexts().getMetaData().getDatabase(DATABASE_NAME).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()) + .thenReturn(Collections.singletonMap("ds_0", storageUnitMetaData)); when(result.getMetaDataContexts().getMetaData().containsDatabase(DATABASE_NAME)).thenReturn(true); when(result.getMetaDataContexts().getMetaData().getDatabase(DATABASE_NAME).containsSchema("public")).thenReturn(true); when(result.getMetaDataContexts().getMetaData().getDatabase(DATABASE_NAME).getSchema("public").containsTable(TABLE_NAME)).thenReturn(true); diff --git a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java index 2f1135a635948..7944e4c9644ae 100644 --- a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java +++ b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java @@ -33,6 +33,7 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; 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.database.schema.model.ShardingSphereSchema; @@ -120,9 +121,9 @@ private Collection createSQLRewriteUnits(final SQLRewriteEngineT new YamlDataSourceConfigurationSwapper().swapToDataSources(rootConfig.getDataSources()), new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(rootConfig.getRules())); mockDataSource(databaseConfig.getDataSources()); DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, testParams.getDatabaseType()); - Map storageUnits = createStorageUnits(databaseConfig, databaseType); + Map metaDataMap = createStorageUnitMetaDataMap(databaseConfig, databaseType); ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); - when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(storageUnits); + when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap); String schemaName = new DatabaseTypeRegistry(databaseType).getDefaultSchemaName(DefaultDatabase.LOGIC_NAME); SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(TypedSPILoader.getService(DatabaseType.class, testParams.getDatabaseType()), sqlParserRule.getSqlStatementCache(), sqlParserRule.getParseTreeCache(), sqlParserRule.isSqlCommentParseEnabled()); @@ -170,12 +171,14 @@ private Collection createGlobalRules() { return result; } - private Map createStorageUnits(final DatabaseConfiguration databaseConfig, final DatabaseType databaseType) { - Map result = new LinkedHashMap<>(databaseConfig.getDataSources().size(), 1F); + private Map createStorageUnitMetaDataMap(final DatabaseConfiguration databaseConfig, final DatabaseType databaseType) { + Map result = new LinkedHashMap<>(databaseConfig.getDataSources().size(), 1F); for (Entry entry : databaseConfig.getDataSources().entrySet()) { StorageUnit storageUnit = mock(StorageUnit.class); when(storageUnit.getStorageType()).thenReturn(databaseType); - result.put(entry.getKey(), storageUnit); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class); + when(storageUnitMetaData.getStorageUnit()).thenReturn(storageUnit); + result.put(entry.getKey(), storageUnitMetaData); } return result; } From 754d5b1b8e9eeccdc38f09013f0ce97eaac60369 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sun, 1 Oct 2023 09:47:25 +0800 Subject: [PATCH 5/8] Fix CI --- .../impl/jdbc/JDBCMetaDataInfoExporter.java | 2 +- .../ShadowRuleConfigurationChecker.java | 6 ++--- .../infra/database/DatabaseTypeEngine.java | 2 +- .../database/resource/ResourceMetaData.java | 7 +++--- .../kernel/KernelProcessorTest.java | 4 ++- .../infra/rewrite/SQLRewriteEntry.java | 11 +++----- .../engine/GenericSQLRewriteEngine.java | 6 ++--- .../rewrite/engine/RouteSQLRewriteEngine.java | 6 ++--- .../engine/GenericSQLRewriteEngineTest.java | 14 ++++++----- .../engine/RouteSQLRewriteEngineTest.java | 20 +++++++-------- .../DriverDatabaseConnectionManager.java | 8 +++--- .../datasource/ShardingSphereDataSource.java | 10 ++++---- .../DriverDatabaseConnectionManagerTest.java | 25 +++++++++++-------- .../ShardingSphereConnectionTest.java | 8 +++++- .../ShardingSphereDataSourceTest.java | 10 ++++---- .../mode/manager/ContextManager.java | 13 ++++------ .../switcher/NewResourceSwitchManager.java | 12 ++++----- .../switcher/ResourceSwitchManager.java | 7 +++--- .../mode/manager/ContextManagerTest.java | 2 +- proxy/backend/core/pom.xml | 4 --- .../RefreshTableMetaDataUpdater.java | 10 ++++---- .../RegisterStorageUnitBackendHandler.java | 2 +- .../UnregisterStorageUnitBackendHandler.java | 4 +-- ...mportDatabaseConfigurationUpdaterTest.java | 9 ++++++- .../RefreshTableMetaDataUpdaterTest.java | 2 +- ...RegisterStorageUnitBackendHandlerTest.java | 2 +- .../import/config-duplicated-logic-table.yaml | 4 +-- .../import/config-empty-database-name.yaml | 4 +-- .../resources/conf/import/config-encrypt.yaml | 4 +-- .../conf/import/config-invalid-algorithm.yaml | 4 +-- .../resources/conf/import/config-mask.yaml | 4 +-- .../import/config-readwrite-splitting.yaml | 6 ++--- .../resources/conf/import/config-shadow.yaml | 4 +-- .../conf/import/config-sharding.yaml | 4 +-- .../autocommit/MySQLAutoCommitTestCase.java | 7 +++--- 35 files changed, 130 insertions(+), 117 deletions(-) diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCMetaDataInfoExporter.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCMetaDataInfoExporter.java index ed249b6955f7e..d4759dace3fc1 100644 --- a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCMetaDataInfoExporter.java +++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/exporter/impl/jdbc/JDBCMetaDataInfoExporter.java @@ -59,7 +59,7 @@ public Optional export(final String pluginTyp ShardingSphereDataSource dataSource = (ShardingSphereDataSource) entry.getValue(); String databaseName = AgentReflectionUtils.getFieldValue(dataSource, "databaseName"); ContextManager contextManager = AgentReflectionUtils.getFieldValue(dataSource, "contextManager"); - result.addMetric(Arrays.asList(databaseName, "storage_unit_count"), contextManager.getStorageUnits(databaseName).size()); + result.addMetric(Arrays.asList(databaseName, "storage_unit_count"), contextManager.getStorageUnitMetaDataMap(databaseName).size()); } return Optional.of(result); } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java index 3269b0f3c9be9..87bb6e41ba7a0 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java @@ -57,11 +57,11 @@ public void check(final String databaseName, final ShadowRuleConfiguration confi } private void checkDataSources(final Map shadowDataSources, final Map dataSourceMap, final String databaseName) { - Set dataSource = dataSourceMap.keySet(); + Set dataSourceName = dataSourceMap.keySet(); for (Entry entry : shadowDataSources.entrySet()) { - ShardingSpherePreconditions.checkState(dataSource.contains(entry.getValue().getProductionDataSourceName()), + ShardingSpherePreconditions.checkState(dataSourceName.contains(entry.getValue().getProductionDataSourceName()), () -> new MissingRequiredShadowConfigurationException("ProductionDataSourceName", databaseName)); - ShardingSpherePreconditions.checkState(dataSource.contains(entry.getValue().getShadowDataSourceName()), + ShardingSpherePreconditions.checkState(dataSourceName.contains(entry.getValue().getShadowDataSourceName()), () -> new MissingRequiredShadowConfigurationException("ShadowDataSourceName", databaseName)); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java index e4393472a9f4e..15ebbdf3ce7d9 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/database/DatabaseTypeEngine.java @@ -24,9 +24,9 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory; -import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import javax.sql.DataSource; import java.sql.Connection; 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 9be01787019b9..49ef85f8f46e9 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 @@ -25,7 +25,6 @@ import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeUtils; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils; @@ -68,9 +67,9 @@ public ResourceMetaData(final String databaseName, final Map getAllInstanceDataSourceNames() { Collection result = new LinkedList<>(); - for (Entry entry : storageUnitMetaData.getMetaDataMap().entrySet()) { - if (!isExisted(entry.getKey(), result)) { - result.add(entry.getKey()); + for (String each : storageUnitMetaData.getMetaDataMap().keySet()) { + if (!isExisted(each, result)) { + result.add(each); } } return result; diff --git a/infra/context/src/test/java/org/apache/shardingsphere/infra/connection/kernel/KernelProcessorTest.java b/infra/context/src/test/java/org/apache/shardingsphere/infra/connection/kernel/KernelProcessorTest.java index 0ce60c3e3b333..3364a65077a7a 100644 --- a/infra/context/src/test/java/org/apache/shardingsphere/infra/connection/kernel/KernelProcessorTest.java +++ b/infra/context/src/test/java/org/apache/shardingsphere/infra/connection/kernel/KernelProcessorTest.java @@ -53,8 +53,10 @@ void assertGenerateExecutionContext() { SQLStatementContext sqlStatementContext = mock(CommonSQLStatementContext.class); when(sqlStatementContext.getSqlStatement()).thenReturn(mock(SelectStatement.class)); QueryContext queryContext = new QueryContext(sqlStatementContext, "SELECT * FROM tbl", Collections.emptyList()); + ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); + when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.emptyMap()); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), - mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new RuleMetaData(mockShardingSphereRule()), Collections.emptyMap()); + resourceMetaData, new RuleMetaData(mockShardingSphereRule()), Collections.emptyMap()); ConfigurationProperties props = new ConfigurationProperties(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.SQL_SHOW.getKey(), Boolean.TRUE.toString()))); ExecutionContext actual = new KernelProcessor().generateExecutionContext(queryContext, database, new RuleMetaData(mockShardingSphereRule()), props, mock(ConnectionContext.class)); diff --git a/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java b/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java index 15681b968ba5c..0381bdf236174 100644 --- a/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java +++ b/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java @@ -22,7 +22,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext; import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContextDecorator; @@ -35,11 +35,9 @@ import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader; import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.stream.Collectors; /** * SQL rewrite entry. @@ -79,11 +77,10 @@ public SQLRewriteResult rewrite(final String sql, final List params, fin SQLRewriteContext sqlRewriteContext = createSQLRewriteContext(sql, params, sqlStatementContext, routeContext, connectionContext, hintValueContext); SQLTranslatorRule rule = globalRuleMetaData.getSingleRule(SQLTranslatorRule.class); DatabaseType protocolType = database.getProtocolType(); - Map storageUnits = database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageUnit(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + Map storageUnitMetaDataMap = database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(); return routeContext.getRouteUnits().isEmpty() - ? new GenericSQLRewriteEngine(rule, protocolType, storageUnits).rewrite(sqlRewriteContext) - : new RouteSQLRewriteEngine(rule, protocolType, storageUnits).rewrite(sqlRewriteContext, routeContext); + ? new GenericSQLRewriteEngine(rule, protocolType, storageUnitMetaDataMap).rewrite(sqlRewriteContext) + : new RouteSQLRewriteEngine(rule, protocolType, storageUnitMetaDataMap).rewrite(sqlRewriteContext, routeContext); } private SQLRewriteContext createSQLRewriteContext(final String sql, final List params, final SQLStatementContext sqlStatementContext, diff --git a/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/engine/GenericSQLRewriteEngine.java b/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/engine/GenericSQLRewriteEngine.java index 66ad3447f2d5a..fed6844ab0908 100644 --- a/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/engine/GenericSQLRewriteEngine.java +++ b/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/engine/GenericSQLRewriteEngine.java @@ -19,7 +19,7 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext; import org.apache.shardingsphere.infra.rewrite.engine.result.GenericSQLRewriteResult; import org.apache.shardingsphere.infra.rewrite.engine.result.SQLRewriteUnit; @@ -38,7 +38,7 @@ public final class GenericSQLRewriteEngine { private final DatabaseType protocolType; - private final Map storageUnits; + private final Map storageUnitMetaDataMap; /** * Rewrite SQL and parameters. @@ -49,7 +49,7 @@ public final class GenericSQLRewriteEngine { public GenericSQLRewriteResult rewrite(final SQLRewriteContext sqlRewriteContext) { String sql = translatorRule.translate( new DefaultSQLBuilder(sqlRewriteContext).toSQL(), sqlRewriteContext.getSqlStatementContext().getSqlStatement(), protocolType, - storageUnits.isEmpty() ? protocolType : storageUnits.values().iterator().next().getStorageType()); + storageUnitMetaDataMap.isEmpty() ? protocolType : storageUnitMetaDataMap.values().iterator().next().getStorageUnit().getStorageType()); return new GenericSQLRewriteResult(new SQLRewriteUnit(sql, sqlRewriteContext.getParameterBuilder().getParameters())); } } diff --git a/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngine.java b/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngine.java index 6b839bdc0fc13..2f74bd8543787 100644 --- a/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngine.java +++ b/infra/rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngine.java @@ -22,7 +22,7 @@ import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.datanode.DataNode; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext; import org.apache.shardingsphere.infra.rewrite.engine.result.RouteSQLRewriteResult; import org.apache.shardingsphere.infra.rewrite.engine.result.SQLRewriteUnit; @@ -54,7 +54,7 @@ public final class RouteSQLRewriteEngine { private final DatabaseType protocolType; - private final Map storageUnits; + private final Map storageUnitMetaDataMap; /** * Rewrite SQL and parameters. @@ -157,7 +157,7 @@ private boolean isInSameDataNode(final Collection dataNodes, final Rou private Map translate(final SQLStatement sqlStatement, final Map sqlRewriteUnits) { Map result = new LinkedHashMap<>(sqlRewriteUnits.size(), 1F); for (Entry entry : sqlRewriteUnits.entrySet()) { - DatabaseType storageType = storageUnits.get(entry.getKey().getDataSourceMapper().getActualName()).getStorageType(); + DatabaseType storageType = storageUnitMetaDataMap.get(entry.getKey().getDataSourceMapper().getActualName()).getStorageUnit().getStorageType(); String sql = translatorRule.translate(entry.getValue().getSql(), sqlStatement, protocolType, storageType); SQLRewriteUnit sqlRewriteUnit = new SQLRewriteUnit(sql, entry.getValue().getParameters()); result.put(entry.getKey(), sqlRewriteUnit); diff --git a/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/GenericSQLRewriteEngineTest.java b/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/GenericSQLRewriteEngineTest.java index 77eb054a6ce04..5eede96db7390 100644 --- a/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/GenericSQLRewriteEngineTest.java +++ b/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/GenericSQLRewriteEngineTest.java @@ -22,7 +22,7 @@ import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext; import org.apache.shardingsphere.infra.rewrite.engine.result.GenericSQLRewriteResult; @@ -32,9 +32,11 @@ import org.junit.jupiter.api.Test; import java.util.Collections; +import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -44,7 +46,7 @@ class GenericSQLRewriteEngineTest { void assertRewrite() { DatabaseType databaseType = mock(DatabaseType.class); SQLTranslatorRule rule = new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()); - GenericSQLRewriteResult actual = new GenericSQLRewriteEngine(rule, databaseType, Collections.singletonMap("ds_0", mockStorageUnit(databaseType))) + GenericSQLRewriteResult actual = new GenericSQLRewriteEngine(rule, databaseType, mockStorageUnitMetaData(databaseType)) .rewrite(new SQLRewriteContext(mockDatabase(), mock(CommonSQLStatementContext.class), "SELECT 1", Collections.emptyList(), mock(ConnectionContext.class), new HintValueContext())); assertThat(actual.getSqlRewriteUnit().getSql(), is("SELECT 1")); @@ -61,10 +63,10 @@ void assertRewriteStorageTypeIsEmpty() { assertThat(actual.getSqlRewriteUnit().getParameters(), is(Collections.emptyList())); } - private StorageUnit mockStorageUnit(final DatabaseType databaseType) { - StorageUnit result = mock(StorageUnit.class); - when(result.getStorageType()).thenReturn(databaseType); - return result; + private Map mockStorageUnitMetaData(final DatabaseType databaseType) { + NewStorageUnitMetaData result = mock(NewStorageUnitMetaData.class, RETURNS_DEEP_STUBS); + when(result.getStorageUnit().getStorageType()).thenReturn(databaseType); + return Collections.singletonMap("ds_0", result); } private ShardingSphereDatabase mockDatabase() { diff --git a/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngineTest.java b/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngineTest.java index abdd7eb2e5b08..d442e855a7136 100644 --- a/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngineTest.java +++ b/infra/rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngineTest.java @@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.hint.HintValueContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext; import org.apache.shardingsphere.infra.rewrite.engine.result.RouteSQLRewriteResult; @@ -60,7 +60,7 @@ void assertRewriteWithStandardParameterBuilder() { routeContext.getRouteUnits().add(routeUnit); DatabaseType databaseType = mock(DatabaseType.class); RouteSQLRewriteResult actual = new RouteSQLRewriteEngine( - new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), databaseType, mockStorageUnits(databaseType)).rewrite(sqlRewriteContext, routeContext); + new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), databaseType, mockStorageUnitMetaData(databaseType)).rewrite(sqlRewriteContext, routeContext); assertThat(actual.getSqlRewriteUnits().size(), is(1)); assertThat(actual.getSqlRewriteUnits().get(routeUnit).getSql(), is("SELECT ?")); assertThat(actual.getSqlRewriteUnits().get(routeUnit).getParameters(), is(Collections.singletonList(1))); @@ -79,7 +79,7 @@ void assertRewriteWithStandardParameterBuilderWhenNeedAggregateRewrite() { routeContext.getRouteUnits().add(secondRouteUnit); DatabaseType databaseType = mock(DatabaseType.class); RouteSQLRewriteResult actual = new RouteSQLRewriteEngine( - new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), databaseType, mockStorageUnits(databaseType)).rewrite(sqlRewriteContext, routeContext); + new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), databaseType, mockStorageUnitMetaData(databaseType)).rewrite(sqlRewriteContext, routeContext); assertThat(actual.getSqlRewriteUnits().size(), is(1)); assertThat(actual.getSqlRewriteUnits().get(firstRouteUnit).getSql(), is("SELECT ? UNION ALL SELECT ?")); assertThat(actual.getSqlRewriteUnits().get(firstRouteUnit).getParameters(), is(Arrays.asList(1, 1))); @@ -99,7 +99,7 @@ void assertRewriteWithGroupedParameterBuilderForBroadcast() { routeContext.getRouteUnits().add(routeUnit); DatabaseType databaseType = mock(DatabaseType.class); RouteSQLRewriteResult actual = new RouteSQLRewriteEngine( - new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), databaseType, mockStorageUnits(databaseType)).rewrite(sqlRewriteContext, routeContext); + new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), databaseType, mockStorageUnitMetaData(databaseType)).rewrite(sqlRewriteContext, routeContext); assertThat(actual.getSqlRewriteUnits().size(), is(1)); assertThat(actual.getSqlRewriteUnits().get(routeUnit).getSql(), is("INSERT INTO tbl VALUES (?)")); assertThat(actual.getSqlRewriteUnits().get(routeUnit).getParameters(), is(Collections.singletonList(1))); @@ -121,7 +121,7 @@ void assertRewriteWithGroupedParameterBuilderForRouteWithSameDataNode() { routeContext.getOriginalDataNodes().add(Collections.singletonList(new DataNode("ds.tbl_0"))); DatabaseType databaseType = mock(DatabaseType.class); RouteSQLRewriteResult actual = new RouteSQLRewriteEngine( - new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), databaseType, mockStorageUnits(databaseType)).rewrite(sqlRewriteContext, routeContext); + new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), databaseType, mockStorageUnitMetaData(databaseType)).rewrite(sqlRewriteContext, routeContext); assertThat(actual.getSqlRewriteUnits().size(), is(1)); assertThat(actual.getSqlRewriteUnits().get(routeUnit).getSql(), is("INSERT INTO tbl VALUES (?)")); assertThat(actual.getSqlRewriteUnits().get(routeUnit).getParameters(), is(Collections.singletonList(1))); @@ -142,7 +142,7 @@ void assertRewriteWithGroupedParameterBuilderForRouteWithEmptyDataNode() { routeContext.getOriginalDataNodes().add(Collections.emptyList()); DatabaseType databaseType = mock(DatabaseType.class); RouteSQLRewriteResult actual = new RouteSQLRewriteEngine( - new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), databaseType, mockStorageUnits(databaseType)).rewrite(sqlRewriteContext, routeContext); + new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), databaseType, mockStorageUnitMetaData(databaseType)).rewrite(sqlRewriteContext, routeContext); assertThat(actual.getSqlRewriteUnits().size(), is(1)); assertThat(actual.getSqlRewriteUnits().get(routeUnit).getSql(), is("INSERT INTO tbl VALUES (?)")); assertThat(actual.getSqlRewriteUnits().get(routeUnit).getParameters(), is(Collections.singletonList(1))); @@ -163,7 +163,7 @@ void assertRewriteWithGroupedParameterBuilderForRouteWithNotSameDataNode() { routeContext.getOriginalDataNodes().add(Collections.singletonList(new DataNode("ds_1.tbl_1"))); DatabaseType databaseType = mock(DatabaseType.class); RouteSQLRewriteResult actual = new RouteSQLRewriteEngine( - new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), databaseType, mockStorageUnits(databaseType)).rewrite(sqlRewriteContext, routeContext); + new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), databaseType, mockStorageUnitMetaData(databaseType)).rewrite(sqlRewriteContext, routeContext); assertThat(actual.getSqlRewriteUnits().size(), is(1)); assertThat(actual.getSqlRewriteUnits().get(routeUnit).getSql(), is("INSERT INTO tbl VALUES (?)")); assertTrue(actual.getSqlRewriteUnits().get(routeUnit).getParameters().isEmpty()); @@ -176,9 +176,9 @@ private ShardingSphereDatabase mockDatabase() { return result; } - private Map mockStorageUnits(final DatabaseType databaseType) { - StorageUnit result = mock(StorageUnit.class); - when(result.getStorageType()).thenReturn(databaseType); + private Map mockStorageUnitMetaData(final DatabaseType databaseType) { + NewStorageUnitMetaData result = mock(NewStorageUnitMetaData.class, RETURNS_DEEP_STUBS); + when(result.getStorageUnit().getStorageType()).thenReturn(databaseType); return Collections.singletonMap("ds_0", result); } } diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java index 78d476d131e3e..ea8fa3130d6a7 100644 --- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java +++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManager.java @@ -33,7 +33,7 @@ import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; import org.apache.shardingsphere.infra.instance.metadata.InstanceType; import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser; import org.apache.shardingsphere.infra.session.connection.ConnectionContext; import org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext; @@ -89,7 +89,7 @@ public final class DriverDatabaseConnectionManager implements DatabaseConnection private final String databaseName; public DriverDatabaseConnectionManager(final String databaseName, final ContextManager contextManager) { - for (Entry entry : contextManager.getStorageUnits(databaseName).entrySet()) { + for (Entry entry : contextManager.getStorageUnitMetaDataMap(databaseName).entrySet()) { DataSource dataSource = entry.getValue().getDataSource(); String cacheKey = getKey(databaseName, entry.getKey()); dataSourceMap.put(cacheKey, dataSource); @@ -341,7 +341,9 @@ public List getConnections(final String dataSourceName, final int co private List getConnections(final String currentDatabaseName, final String dataSourceName, final int connectionOffset, final int connectionSize, final ConnectionMode connectionMode) throws SQLException { String cacheKey = getKey(currentDatabaseName, dataSourceName); - DataSource dataSource = databaseName.equals(currentDatabaseName) ? dataSourceMap.get(cacheKey) : contextManager.getStorageUnits(currentDatabaseName).get(dataSourceName).getDataSource(); + DataSource dataSource = databaseName.equals(currentDatabaseName) + ? dataSourceMap.get(cacheKey) + : contextManager.getStorageUnitMetaDataMap(currentDatabaseName).get(dataSourceName).getDataSource(); Preconditions.checkNotNull(dataSource, "Missing the data source name: '%s'", dataSourceName); Collection connections; synchronized (cachedConnections) { diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java index 5deeda8268c62..390da85e974aa 100644 --- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java +++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java @@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaDataBuilder; import org.apache.shardingsphere.infra.instance.metadata.InstanceType; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.mode.manager.ContextManager; @@ -102,7 +102,7 @@ public Connection getConnection(final String username, final String password) { @Override public void close() throws SQLException { contextManagerDestroyedCallback(databaseName); - for (StorageUnit each : contextManager.getStorageUnits(databaseName).values()) { + for (NewStorageUnitMetaData each : contextManager.getStorageUnitMetaDataMap(databaseName).values()) { close(each.getDataSource()); } contextManager.close(); @@ -133,13 +133,13 @@ private void contextManagerDestroyedCallback(final String databaseName) { @Override public int getLoginTimeout() throws SQLException { - Map storageUnits = contextManager.getStorageUnits(databaseName); - return storageUnits.isEmpty() ? 0 : storageUnits.values().iterator().next().getDataSource().getLoginTimeout(); + Map metaDataMap = contextManager.getStorageUnitMetaDataMap(databaseName); + return metaDataMap.isEmpty() ? 0 : metaDataMap.values().iterator().next().getDataSource().getLoginTimeout(); } @Override public void setLoginTimeout(final int seconds) throws SQLException { - for (StorageUnit each : contextManager.getStorageUnits(databaseName).values()) { + for (NewStorageUnitMetaData each : contextManager.getStorageUnitMetaDataMap(databaseName).values()) { each.getDataSource().setLoginTimeout(seconds); } } diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java index a424a2e930417..432cfa3e3078d 100644 --- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java +++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java @@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode; import org.apache.shardingsphere.infra.instance.metadata.InstanceType; import org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; 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; @@ -75,8 +76,8 @@ void setUp() throws SQLException { private ContextManager mockContextManager() throws SQLException { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - Map storageUnits = mockStorageUnits(); - when(result.getStorageUnits(DefaultDatabase.LOGIC_NAME)).thenReturn(storageUnits); + Map metaDataMap = mockStorageUnitMetaDataMap(); + when(result.getStorageUnitMetaDataMap(DefaultDatabase.LOGIC_NAME)).thenReturn(metaDataMap); MetaDataPersistService persistService = mockMetaDataPersistService(); when(result.getMetaDataContexts().getPersistService()).thenReturn(persistService); when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn( @@ -88,16 +89,20 @@ private ContextManager mockContextManager() throws SQLException { return result; } - private Map mockStorageUnits() throws SQLException { - Map result = new HashMap<>(2, 1F); - StorageUnit validStorageUnit = mock(StorageUnit.class); - when(validStorageUnit.getDataSource()).thenReturn(new MockedDataSource()); - result.put("ds", validStorageUnit); - StorageUnit invalidStorageUnit = mock(StorageUnit.class); + private Map mockStorageUnitMetaDataMap() throws SQLException { + Map result = new HashMap<>(2, 1F); + result.put("ds", mockStorageUnitMetaData(new MockedDataSource())); DataSource invalidDataSource = mock(DataSource.class); when(invalidDataSource.getConnection()).thenThrow(new SQLException()); - when(invalidStorageUnit.getDataSource()).thenReturn(invalidDataSource); - result.put("invalid_ds", invalidStorageUnit); + result.put("invalid_ds", mockStorageUnitMetaData(invalidDataSource)); + return result; + } + + private NewStorageUnitMetaData mockStorageUnitMetaData(final DataSource dataSource) { + NewStorageUnitMetaData result = mock(NewStorageUnitMetaData.class, RETURNS_DEEP_STUBS); + when(result.getDataSource()).thenReturn(dataSource); + StorageUnit storageUnit = mock(StorageUnit.class); + when(result.getStorageUnit()).thenReturn(storageUnit); return result; } diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java index bfa180781bc34..25fbddc4ec64c 100644 --- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java +++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnectionTest.java @@ -20,6 +20,7 @@ import lombok.SneakyThrows; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.session.connection.ConnectionContext; @@ -33,6 +34,7 @@ import org.junit.jupiter.api.Test; import org.mockito.internal.configuration.plugins.Plugins; +import javax.sql.DataSource; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.SQLException; @@ -251,7 +253,11 @@ private ContextManager mockContextManager() { private ContextManager mockContextManager(final StorageUnit storageUnit) { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - when(result.getStorageUnits(DefaultDatabase.LOGIC_NAME)).thenReturn(Collections.singletonMap("ds", storageUnit)); + NewStorageUnitMetaData storageUnitMetaData = mock(NewStorageUnitMetaData.class); + when(storageUnitMetaData.getStorageUnit()).thenReturn(storageUnit); + DataSource dataSource = storageUnit.getDataSource(); + when(storageUnitMetaData.getDataSource()).thenReturn(dataSource); + when(result.getStorageUnitMetaDataMap(DefaultDatabase.LOGIC_NAME)).thenReturn(Collections.singletonMap("ds", storageUnitMetaData)); when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Arrays.asList(mockTransactionRule(), mock(TrafficRule.class)))); return result; } diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java index de2c40f748f8d..dd106df7f8a44 100644 --- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java +++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java @@ -59,7 +59,7 @@ void assertNewConstructorWithModeConfigurationOnly() throws Exception { assertNotNull(contextManager.getMetaDataContexts().getMetaData().getDatabase(DefaultDatabase.LOGIC_NAME)); assertThat(contextManager.getClusterStateContext().getCurrentState(), is(ClusterState.OK)); assertThat(contextManager.getInstanceContext().getInstance().getState().getCurrentState(), is(InstanceState.OK)); - assertTrue(contextManager.getStorageUnits(DefaultDatabase.LOGIC_NAME).isEmpty()); + assertTrue(contextManager.getStorageUnitMetaDataMap(DefaultDatabase.LOGIC_NAME).isEmpty()); } } @@ -72,8 +72,8 @@ void assertNewConstructorWithAllArguments() throws Exception { assertNotNull(contextManager.getMetaDataContexts().getMetaData().getDatabase(DefaultDatabase.LOGIC_NAME)); assertThat(contextManager.getClusterStateContext().getCurrentState(), is(ClusterState.OK)); assertThat(contextManager.getInstanceContext().getInstance().getState().getCurrentState(), is(InstanceState.OK)); - assertThat(contextManager.getStorageUnits(DefaultDatabase.LOGIC_NAME).size(), is(1)); - assertThat(contextManager.getStorageUnits(DefaultDatabase.LOGIC_NAME).get("ds").getDataSource().getConnection().getMetaData().getURL(), is("jdbc:mock://127.0.0.1/foo_ds")); + assertThat(contextManager.getStorageUnitMetaDataMap(DefaultDatabase.LOGIC_NAME).size(), is(1)); + assertThat(contextManager.getStorageUnitMetaDataMap(DefaultDatabase.LOGIC_NAME).get("ds").getDataSource().getConnection().getMetaData().getURL(), is("jdbc:mock://127.0.0.1/foo_ds")); } } @@ -106,7 +106,7 @@ private ShardingSphereDataSource createShardingSphereDataSource(final DataSource @Test void assertEmptyDataSourceMap() throws Exception { try (ShardingSphereDataSource actual = new ShardingSphereDataSource(DefaultDatabase.LOGIC_NAME, null)) { - assertTrue(getContextManager(actual).getStorageUnits(DefaultDatabase.LOGIC_NAME).isEmpty()); + assertTrue(getContextManager(actual).getStorageUnitMetaDataMap(DefaultDatabase.LOGIC_NAME).isEmpty()); assertThat(actual.getLoginTimeout(), is(0)); } } @@ -114,7 +114,7 @@ void assertEmptyDataSourceMap() throws Exception { @Test void assertNotEmptyDataSourceMap() throws Exception { try (ShardingSphereDataSource actual = createShardingSphereDataSource(createHikariDataSource())) { - assertThat(getContextManager(actual).getStorageUnits(DefaultDatabase.LOGIC_NAME).size(), is(1)); + assertThat(getContextManager(actual).getStorageUnitMetaDataMap(DefaultDatabase.LOGIC_NAME).size(), is(1)); assertThat(actual.getLoginTimeout(), is(15)); } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java index a49ed247c9b39..971db5f1d399a 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; 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.database.schema.SchemaManager; @@ -47,11 +48,8 @@ import java.sql.SQLException; import java.util.Collections; -import java.util.LinkedHashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Collectors; /** * Context manager. @@ -102,14 +100,13 @@ public synchronized void renewMetaDataContexts(final MetaDataContexts metaDataCo } /** - * Get storage units. + * Get storage unit meta data map. * * @param databaseName database name - * @return storage units + * @return storage unit meta data map */ - public Map getStorageUnits(final String databaseName) { - return metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageUnit(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + public Map getStorageUnitMetaDataMap(final String databaseName) { + return metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(); } /** diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java index 12c60dcba1db3..c76b2ee80736b 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java @@ -28,7 +28,6 @@ import javax.sql.DataSource; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; @@ -48,7 +47,7 @@ public final class NewResourceSwitchManager { * @return created switching resource */ public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMetaData, final Map storageUnitDataSourcePoolProps) { - Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap() + Map mergedPropsMap = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap() .entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedPropsMap.putAll(storageUnitDataSourcePoolProps); Map toBeCreatedStorageUintNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(storageUnitDataSourcePoolProps); @@ -77,7 +76,7 @@ private StorageResource getRegisterNewStorageResource(final ResourceMetaData res * @return created switching resource */ public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaData, final Map propsMap) { - Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap() + Map mergedDataSourcePoolPropertiesMap = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap() .entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedDataSourcePoolPropertiesMap.putAll(propsMap); Map toBeAlteredStorageUintNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(mergedDataSourcePoolPropertiesMap); @@ -114,7 +113,7 @@ private StorageResource getStaleStorageResource(final ResourceMetaData resourceM * @return created switching resource */ public SwitchingResource unregisterStorageUnit(final ResourceMetaData resourceMetaData, final String storageUnitName) { - Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap() + Map mergedDataSourcePoolPropertiesMap = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap() .entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedDataSourcePoolPropertiesMap.keySet().removeIf(each -> each.equals(storageUnitName)); resourceMetaData.getStorageUnitMetaData().getMetaDataMap().remove(storageUnitName); @@ -123,8 +122,9 @@ public SwitchingResource unregisterStorageUnit(final ResourceMetaData resourceMe } private StorageResource getToBeRemovedStaleStorageResource(final ResourceMetaData resourceMetaData, final String storageUnitName) { - Map metaDataMap = new HashMap<>( - resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode()))); + Map metaDataMap = new LinkedHashMap<>( + resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); StorageNode storageNode = metaDataMap.remove(storageUnitName); Map reservedStorageUintNodeMap = resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode())); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java index 476ccadf5731f..a9a9b19b89c38 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java @@ -30,7 +30,6 @@ import javax.sql.DataSource; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; @@ -49,7 +48,7 @@ public final class ResourceSwitchManager { * @return created switching resource */ public SwitchingResource create(final ResourceMetaData resourceMetaData, final Map toBeChangedPropsMap) { - Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + Map mergedPropsMap = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedPropsMap.putAll(toBeChangedPropsMap); Map toBeChangedStorageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(toBeChangedPropsMap); @@ -65,7 +64,7 @@ public SwitchingResource create(final ResourceMetaData resourceMetaData, final M * @return created switching resource */ public SwitchingResource createByDropResource(final ResourceMetaData resourceMetaData, final Map toBeDeletedPropsMap) { - Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + Map mergedDataSourcePoolPropertiesMap = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedDataSourcePoolPropertiesMap.keySet().removeIf(toBeDeletedPropsMap::containsKey); Map toRemovedStorageUnitNodeMap = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(toBeDeletedPropsMap); @@ -81,7 +80,7 @@ public SwitchingResource createByDropResource(final ResourceMetaData resourceMet * @return created switching resource */ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMetaData resourceMetaData, final Map toBeChangedPropsMap) { - Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() + Map mergedDataSourcePoolPropertiesMap = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); mergedDataSourcePoolPropertiesMap.keySet().removeIf(each -> !toBeChangedPropsMap.containsKey(each)); mergedDataSourcePoolPropertiesMap.putAll(toBeChangedPropsMap); diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java index 20d309e66766a..78941a7331cd4 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java @@ -120,7 +120,7 @@ void assertGetDataSourceMap() { ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), resourceMetaData, mock(RuleMetaData.class), Collections.emptyMap()); when(metaDataContexts.getMetaData().getDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(database); - assertThat(contextManager.getStorageUnits(DefaultDatabase.LOGIC_NAME).size(), is(1)); + assertThat(contextManager.getStorageUnitMetaDataMap(DefaultDatabase.LOGIC_NAME).size(), is(1)); } @Test diff --git a/proxy/backend/core/pom.xml b/proxy/backend/core/pom.xml index 1f2ae164d00b0..6f208a63187b9 100644 --- a/proxy/backend/core/pom.xml +++ b/proxy/backend/core/pom.xml @@ -246,9 +246,5 @@ org.awaitility awaitility - - mysql - mysql-connector-java - diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/RefreshTableMetaDataUpdater.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/RefreshTableMetaDataUpdater.java index 19d9e27649171..7abd6c9f33916 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/RefreshTableMetaDataUpdater.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/RefreshTableMetaDataUpdater.java @@ -25,7 +25,7 @@ import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.NoDatabaseSelectedException; import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.UnknownDatabaseException; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable.updater.ConnectionSessionRequiredRALUpdater; @@ -43,7 +43,7 @@ public final class RefreshTableMetaDataUpdater implements ConnectionSessionRequi public void executeUpdate(final ConnectionSession connectionSession, final RefreshTableMetaDataStatement sqlStatement) { String databaseName = getDatabaseName(connectionSession); ContextManager contextManager = ProxyContext.getInstance().getContextManager(); - checkStorageUnits(databaseName, contextManager.getStorageUnits(databaseName), sqlStatement); + checkStorageUnitMetaData(databaseName, contextManager.getStorageUnitMetaDataMap(databaseName), sqlStatement); String schemaName = getSchemaName(databaseName, sqlStatement, connectionSession); if (sqlStatement.getStorageUnitName().isPresent()) { if (sqlStatement.getTableName().isPresent()) { @@ -60,12 +60,12 @@ public void executeUpdate(final ConnectionSession connectionSession, final Refre } } - private void checkStorageUnits(final String databaseName, final Map storageUnits, final RefreshTableMetaDataStatement sqlStatement) { - ShardingSpherePreconditions.checkState(!storageUnits.isEmpty(), () -> new EmptyStorageUnitException(databaseName)); + private void checkStorageUnitMetaData(final String databaseName, final Map storageUnitMetaDataMap, final RefreshTableMetaDataStatement sqlStatement) { + ShardingSpherePreconditions.checkState(!storageUnitMetaDataMap.isEmpty(), () -> new EmptyStorageUnitException(databaseName)); if (sqlStatement.getStorageUnitName().isPresent()) { String storageUnitName = sqlStatement.getStorageUnitName().get(); ShardingSpherePreconditions.checkState( - storageUnits.containsKey(storageUnitName), () -> new MissingRequiredStorageUnitsException(databaseName, Collections.singleton(storageUnitName))); + storageUnitMetaDataMap.containsKey(storageUnitName), () -> new MissingRequiredStorageUnitsException(databaseName, Collections.singleton(storageUnitName))); } } diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/RegisterStorageUnitBackendHandler.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/RegisterStorageUnitBackendHandler.java index 6aade316f1eb8..37c284f792c98 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/RegisterStorageUnitBackendHandler.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/RegisterStorageUnitBackendHandler.java @@ -112,7 +112,7 @@ private void checkDuplicatedLogicalDataSourceNames(final String databaseName, fi } private Collection getCurrentStorageUnitNames(final String databaseName) { - return ProxyContext.getInstance().getContextManager().getStorageUnits(databaseName).keySet(); + return ProxyContext.getInstance().getContextManager().getStorageUnitMetaDataMap(databaseName).keySet(); } private Collection getLogicalDataSourceNames(final String databaseName) { diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandler.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandler.java index 80df2fc3e17dd..f2da5e6dbaf8f 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandler.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandler.java @@ -71,8 +71,8 @@ public void checkSQLStatement(final String databaseName, final UnregisterStorage } private void checkExisted(final String databaseName, final Collection storageUnitNames) { - Map storageUnits = ProxyContext.getInstance().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(); - Collection notExistedStorageUnits = storageUnitNames.stream().filter(each -> !storageUnits.containsKey(each)).collect(Collectors.toList()); + Map metaDataMap = ProxyContext.getInstance().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(); + Collection notExistedStorageUnits = storageUnitNames.stream().filter(each -> !metaDataMap.containsKey(each)).collect(Collectors.toList()); ShardingSpherePreconditions.checkState(notExistedStorageUnits.isEmpty(), () -> new MissingRequiredStorageUnitsException(databaseName, notExistedStorageUnits)); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdaterTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdaterTest.java index 7cf3dd367146a..719aa2ca260d1 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdaterTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdaterTest.java @@ -25,18 +25,20 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule; -import org.apache.shardingsphere.infra.exception.core.external.sql.type.generic.UnsupportedSQLOperationException; import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.util.YamlDatabaseConfigurationImportExecutor; +import org.apache.shardingsphere.test.fixture.jdbc.MockedDriver; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.internal.configuration.plugins.Plugins; @@ -59,6 +61,11 @@ class ImportDatabaseConfigurationUpdaterTest { private ImportDatabaseConfigurationUpdater importDatabaseConfigUpdater; + @BeforeAll + static void setUp() throws ClassNotFoundException { + Class.forName(MockedDriver.class.getName()); + } + @Test void assertImportDatabaseExecutorForSharding() throws SQLException { assertExecute("sharding_db", "/conf/import/config-sharding.yaml"); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/RefreshTableMetaDataUpdaterTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/RefreshTableMetaDataUpdaterTest.java index 0e0866a33f340..d973dc48cf057 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/RefreshTableMetaDataUpdaterTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/RefreshTableMetaDataUpdaterTest.java @@ -69,7 +69,7 @@ void assertUnknownDatabaseException() { @Test void assertEmptyResource() { ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); - when(contextManager.getStorageUnits("foo_db")).thenReturn(Collections.emptyMap()); + when(contextManager.getStorageUnitMetaDataMap("foo_db")).thenReturn(Collections.emptyMap()); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); when(ProxyContext.getInstance().databaseExists("foo_db")).thenReturn(true); UpdatableRALBackendHandler backendHandler = new UpdatableRALBackendHandler<>(new RefreshTableMetaDataStatement(), mockConnectionSession("foo_db")); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/RegisterStorageUnitBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/RegisterStorageUnitBackendHandlerTest.java index 1fb0d6d283787..9dbc9bbbed82d 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/RegisterStorageUnitBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/RegisterStorageUnitBackendHandlerTest.java @@ -97,7 +97,7 @@ void assertExecuteWithDuplicateStorageUnitNamesInStatement() { @Test void assertExecuteWithDuplicateStorageUnitNamesWithResourceMetaData() { ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); - when(contextManager.getStorageUnits("foo_db").keySet()).thenReturn(Collections.singleton("ds_0")); + when(contextManager.getStorageUnitMetaDataMap("foo_db").keySet()).thenReturn(Collections.singleton("ds_0")); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); assertThrows(DuplicateStorageUnitException.class, () -> handler.execute("foo_db", createRegisterStorageUnitStatement())); } diff --git a/proxy/backend/core/src/test/resources/conf/import/config-duplicated-logic-table.yaml b/proxy/backend/core/src/test/resources/conf/import/config-duplicated-logic-table.yaml index e1b64b99b38ef..446c64bc569af 100644 --- a/proxy/backend/core/src/test/resources/conf/import/config-duplicated-logic-table.yaml +++ b/proxy/backend/core/src/test/resources/conf/import/config-duplicated-logic-table.yaml @@ -19,7 +19,7 @@ databaseName: sharding_db dataSources: ds_0: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_0 username: root password: connectionTimeoutMilliseconds: 30000 @@ -28,7 +28,7 @@ dataSources: maxPoolSize: 50 minPoolSize: 1 ds_1: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_1 username: root password: connectionTimeoutMilliseconds: 30000 diff --git a/proxy/backend/core/src/test/resources/conf/import/config-empty-database-name.yaml b/proxy/backend/core/src/test/resources/conf/import/config-empty-database-name.yaml index 90fe05ebcf327..c48a6296b3da3 100644 --- a/proxy/backend/core/src/test/resources/conf/import/config-empty-database-name.yaml +++ b/proxy/backend/core/src/test/resources/conf/import/config-empty-database-name.yaml @@ -17,7 +17,7 @@ dataSources: ds_0: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_0 username: root password: connectionTimeoutMilliseconds: 30000 @@ -26,7 +26,7 @@ dataSources: maxPoolSize: 50 minPoolSize: 1 ds_1: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_1 username: root password: connectionTimeoutMilliseconds: 30000 diff --git a/proxy/backend/core/src/test/resources/conf/import/config-encrypt.yaml b/proxy/backend/core/src/test/resources/conf/import/config-encrypt.yaml index 167fea1656858..c6b0fdd565e1b 100644 --- a/proxy/backend/core/src/test/resources/conf/import/config-encrypt.yaml +++ b/proxy/backend/core/src/test/resources/conf/import/config-encrypt.yaml @@ -19,7 +19,7 @@ databaseName: encrypt_db dataSources: ds_0: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_0 username: root password: connectionTimeoutMilliseconds: 30000 @@ -28,7 +28,7 @@ dataSources: maxPoolSize: 50 minPoolSize: 1 ds_1: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_1 username: root password: connectionTimeoutMilliseconds: 30000 diff --git a/proxy/backend/core/src/test/resources/conf/import/config-invalid-algorithm.yaml b/proxy/backend/core/src/test/resources/conf/import/config-invalid-algorithm.yaml index 18f7c5f058120..887b2d93a6ae6 100644 --- a/proxy/backend/core/src/test/resources/conf/import/config-invalid-algorithm.yaml +++ b/proxy/backend/core/src/test/resources/conf/import/config-invalid-algorithm.yaml @@ -19,7 +19,7 @@ databaseName: sharding_db dataSources: ds_0: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_0 username: root password: connectionTimeoutMilliseconds: 30000 @@ -28,7 +28,7 @@ dataSources: maxPoolSize: 50 minPoolSize: 1 ds_1: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_1 username: root password: connectionTimeoutMilliseconds: 30000 diff --git a/proxy/backend/core/src/test/resources/conf/import/config-mask.yaml b/proxy/backend/core/src/test/resources/conf/import/config-mask.yaml index 3c9986f6712ec..6479e7c954aa6 100644 --- a/proxy/backend/core/src/test/resources/conf/import/config-mask.yaml +++ b/proxy/backend/core/src/test/resources/conf/import/config-mask.yaml @@ -19,7 +19,7 @@ databaseName: mask_db dataSources: ds_0: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_0 username: root password: connectionTimeoutMilliseconds: 30000 @@ -28,7 +28,7 @@ dataSources: maxPoolSize: 50 minPoolSize: 1 ds_1: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_1 username: root password: connectionTimeoutMilliseconds: 30000 diff --git a/proxy/backend/core/src/test/resources/conf/import/config-readwrite-splitting.yaml b/proxy/backend/core/src/test/resources/conf/import/config-readwrite-splitting.yaml index 5d80acaff67c8..4b09a4e1d1834 100644 --- a/proxy/backend/core/src/test/resources/conf/import/config-readwrite-splitting.yaml +++ b/proxy/backend/core/src/test/resources/conf/import/config-readwrite-splitting.yaml @@ -19,7 +19,7 @@ databaseName: readwrite_splitting_db dataSources: write_ds: - url: jdbc:mysql://127.0.0.1:3306/demo_write_ds?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_write_ds username: root password: connectionTimeoutMilliseconds: 30000 @@ -28,7 +28,7 @@ dataSources: maxPoolSize: 50 minPoolSize: 1 read_ds_0: - url: jdbc:mysql://127.0.0.1:3306/demo_read_ds_0?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_read_ds_0 username: root password: connectionTimeoutMilliseconds: 30000 @@ -37,7 +37,7 @@ dataSources: maxPoolSize: 50 minPoolSize: 1 read_ds_1: - url: jdbc:mysql://127.0.0.1:3306/demo_read_ds_1?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_read_ds_1 username: root password: connectionTimeoutMilliseconds: 30000 diff --git a/proxy/backend/core/src/test/resources/conf/import/config-shadow.yaml b/proxy/backend/core/src/test/resources/conf/import/config-shadow.yaml index 19e94dca27783..ec52ad40e70cc 100644 --- a/proxy/backend/core/src/test/resources/conf/import/config-shadow.yaml +++ b/proxy/backend/core/src/test/resources/conf/import/config-shadow.yaml @@ -19,7 +19,7 @@ databaseName: shadow_db dataSources: ds: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_0 username: root password: connectionTimeoutMilliseconds: 30000 @@ -28,7 +28,7 @@ dataSources: maxPoolSize: 50 minPoolSize: 1 shadow_ds: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_1 username: root password: connectionTimeoutMilliseconds: 30000 diff --git a/proxy/backend/core/src/test/resources/conf/import/config-sharding.yaml b/proxy/backend/core/src/test/resources/conf/import/config-sharding.yaml index f195f58ce36eb..77cb41c42d3d7 100644 --- a/proxy/backend/core/src/test/resources/conf/import/config-sharding.yaml +++ b/proxy/backend/core/src/test/resources/conf/import/config-sharding.yaml @@ -19,7 +19,7 @@ databaseName: sharding_db dataSources: ds_0: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_0 username: root password: connectionTimeoutMilliseconds: 30000 @@ -28,7 +28,7 @@ dataSources: maxPoolSize: 50 minPoolSize: 1 ds_1: - url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false + url: jdbc:mock://127.0.0.1/demo_ds_1 username: root password: connectionTimeoutMilliseconds: 30000 diff --git a/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java b/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java index c83052b34933f..3fcaac3d2b940 100644 --- a/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java +++ b/test/e2e/operation/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java @@ -50,7 +50,9 @@ public void executeTest(final TransactionContainerComposer containerComposer) th private void assertAutoCommit() throws SQLException { // TODO Currently XA transaction does not support two transactions in the same thread at the same time - try (Connection connection1 = getDataSource().getConnection(); Connection connection2 = getDataSource().getConnection()) { + try ( + Connection connection1 = getDataSource().getConnection(); + Connection connection2 = getDataSource().getConnection()) { executeWithLog(connection1, "set session transaction isolation level read committed;"); executeWithLog(connection2, "set session transaction isolation level read committed;"); connection1.setAutoCommit(false); @@ -58,8 +60,7 @@ private void assertAutoCommit() throws SQLException { executeWithLog(connection1, "insert into account(id, balance, transaction_id) values(1, 100, 1);"); assertFalse(executeQueryWithLog(connection2, "select * from account;").next()); connection1.commit(); - Awaitility.await().atMost(1L, TimeUnit.SECONDS).pollDelay(200L, TimeUnit.MILLISECONDS).until( - () -> executeQueryWithLog(connection2, "select * from account;").next()); + Awaitility.await().atMost(1L, TimeUnit.SECONDS).pollDelay(200L, TimeUnit.MILLISECONDS).until(() -> executeQueryWithLog(connection2, "select * from account;").next()); assertTrue(executeQueryWithLog(connection2, "select * from account;").next()); } } From 64d646329c6a0c8f38bf28e8c88d0d67425d21ed Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sun, 1 Oct 2023 10:00:08 +0800 Subject: [PATCH 6/8] Code format --- .../database/schema/builder/GenericSchemaBuilderMaterial.java | 2 +- .../mode/manager/context/ConfigurationContextManager.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java index 5303dcf574e81..b10c21987f065 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderMaterial.java @@ -53,7 +53,7 @@ public final class GenericSchemaBuilderMaterial { public GenericSchemaBuilderMaterial(final DatabaseType protocolType, final StorageUnitMetaData storageUnitMetaData, final Collection rules, final ConfigurationProperties props, final String defaultSchemaName) { this(protocolType, storageUnitMetaData.getMetaDataMap().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageUnit().getStorageType(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageUnit().getStorageType(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), storageUnitMetaData.getMetaDataMap().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), rules, props, defaultSchemaName); 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 a9cf6d9c37fa8..949bdd5db3011 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 @@ -363,7 +363,7 @@ private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData re StorageResource storageResource = getMergedStorageResource(resourceMetaData, switchingResource); Map propsMap = null == switchingResource ? resourceMetaData.getStorageUnitMetaData().getMetaDataMap() - .entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)) + .entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)) : switchingResource.getMergedDataSourcePoolPropertiesMap(); return new DataSourceProvidedDatabaseConfiguration(storageResource, toBeCreatedRuleConfigs, propsMap); } From f2711a42264cd5ccd7603e6fca69f3e275b4c760 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sun, 1 Oct 2023 23:45:22 +0800 Subject: [PATCH 7/8] Try to fix IT --- .../database/resource/unit/StorageUnitMetaData.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java index ad0dd43fae270..f94b9468f128c 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.infra.metadata.database.resource.unit; import lombok.Getter; +import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource; 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.node.StorageNodeName; @@ -39,7 +40,11 @@ public StorageUnitMetaData(final String databaseName, final Map dataSources) { metaDataMap = new LinkedHashMap<>(); for (Entry entry : storageNodes.entrySet()) { - metaDataMap.put(entry.getKey(), new NewStorageUnitMetaData(databaseName, entry.getValue(), dataSourcePoolPropertiesMap.get(entry.getKey()), dataSources.get(entry.getValue().getName()))); + DataSource dataSource = dataSources.get(entry.getValue().getName()); + if (!(dataSource instanceof CatalogSwitchableDataSource)) { + dataSource = new CatalogSwitchableDataSource(dataSource, entry.getValue().getCatalog(), entry.getValue().getUrl()); + } + metaDataMap.put(entry.getKey(), new NewStorageUnitMetaData(databaseName, entry.getValue(), dataSourcePoolPropertiesMap.get(entry.getKey()), dataSource)); } } } From 19477b202716fec669d72e5cc6441a56ae161e80 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Mon, 2 Oct 2023 00:00:25 +0800 Subject: [PATCH 8/8] Try to fix IT --- .../infra/metadata/database/resource/unit/StorageUnit.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java index fd59f0d651c7e..d6417aaf5a372 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java @@ -68,6 +68,6 @@ private ConnectionProperties createConnectionProperties(final boolean isDataSour } Map standardProps = DataSourcePoolPropertiesCreator.create(dataSource).getConnectionPropertySynonyms().getStandardProperties(); ConnectionPropertiesParser parser = DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class, storageType); - return parser.parse(standardProps.get("url").toString(), standardProps.get("username").toString(), storageNode.getCatalog()); + return parser.parse(standardProps.get("url").toString(), standardProps.getOrDefault("username", "").toString(), storageNode.getCatalog()); } }