-
Notifications
You must be signed in to change notification settings - Fork 8.9k
optimize: Add some checks to RegistryFactory #7345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
a45a9cb
a82d97f
01f78c0
64b02eb
7db9f0b
91fee68
672c597
13c17a7
b8ade86
566e134
eb5a1ae
4c4997a
9c9b0ed
5a869c9
8d19432
3d8271a
ce7d31d
a4ca41e
9943658
ebb972c
4659493
dc4fd59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,10 @@ | |
| package org.apache.seata.discovery.registry; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| import org.apache.seata.common.ConfigurationKeys; | ||
| import org.apache.seata.common.Constants; | ||
|
|
@@ -31,7 +33,6 @@ | |
|
|
||
| /** | ||
| * The type multiple Registry factory. | ||
| * | ||
| */ | ||
| public class MultiRegistryFactory { | ||
|
|
||
|
|
@@ -48,26 +49,36 @@ public static List<RegistryService> getInstances() { | |
|
|
||
| private static List<RegistryService> buildRegistryServices() { | ||
| List<RegistryService> registryServices = new ArrayList<>(); | ||
| String registryTypeNamesStr = | ||
| ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig(ConfigurationKeys.FILE_ROOT_REGISTRY | ||
| + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE); | ||
| Set<String> processedRegistryTypes = new HashSet<>(); | ||
| String registryTypeNamesStr = ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig( | ||
| ConfigurationKeys.FILE_ROOT_REGISTRY + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE); | ||
|
|
||
| if (StringUtils.isBlank(registryTypeNamesStr)) { | ||
| registryTypeNamesStr = RegistryType.File.name(); | ||
| } | ||
| String[] registryTypeNames = registryTypeNamesStr.split(Constants.REGISTRY_TYPE_SPLIT_CHAR); | ||
|
|
||
| if (registryTypeNames.length > 1) { | ||
| LOGGER.info("use multi registry center type: {}", registryTypeNamesStr); | ||
| } | ||
|
|
||
| for (String registryTypeName : registryTypeNames) { | ||
| RegistryType registryType; | ||
| try { | ||
| registryType = RegistryType.getType(registryTypeName); | ||
| } catch (Exception exx) { | ||
| throw new NotSupportYetException("not support registry type: " + registryTypeName); | ||
| } | ||
|
|
||
| if (processedRegistryTypes.contains(registryType.name())) { | ||
| LOGGER.warn("The duplicate registration center type '{}' was found in the configuration and has been skipped.", registryType.name()); | ||
| continue; | ||
|
||
| } | ||
|
|
||
| RegistryService registryService = EnhancedServiceLoader | ||
| .load(RegistryProvider.class, Objects.requireNonNull(registryType).name()).provide(); | ||
| .load(RegistryProvider.class, Objects.requireNonNull(registryType).name()).provide(); | ||
| registryServices.add(registryService); | ||
| processedRegistryTypes.add(registryType.name()); | ||
| } | ||
| return registryServices; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,14 +20,14 @@ | |
|
|
||
| import org.apache.seata.common.exception.NotSupportYetException; | ||
| import org.apache.seata.common.loader.EnhancedServiceLoader; | ||
| import org.apache.seata.common.util.StringUtils; | ||
| import org.apache.seata.config.ConfigurationFactory; | ||
| import org.apache.seata.config.ConfigurationKeys; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * The type Registry factory. | ||
| * | ||
| */ | ||
| public class RegistryFactory { | ||
|
|
||
|
|
@@ -45,8 +45,12 @@ public static RegistryService getInstance() { | |
| private static RegistryService buildRegistryService() { | ||
| RegistryType registryType; | ||
| String registryTypeName = ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig( | ||
| ConfigurationKeys.FILE_ROOT_REGISTRY + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR | ||
| + ConfigurationKeys.FILE_ROOT_TYPE); | ||
| ConfigurationKeys.FILE_ROOT_REGISTRY + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE); | ||
|
|
||
| if (StringUtils.isBlank(registryTypeName)) { | ||
| registryTypeName = RegistryType.File.name(); | ||
| } | ||
|
|
||
| LOGGER.info("use registry center type: {}", registryTypeName); | ||
| try { | ||
|
||
| registryType = RegistryType.getType(registryTypeName); | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it’s been changed to a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * 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.seata.discovery.registry; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.seata.common.ConfigurationKeys; | ||
| import org.apache.seata.common.Constants; | ||
| import org.apache.seata.common.exception.NotSupportYetException; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * The type Multi registry factory test. | ||
| */ | ||
| public class MultiRegistryFactoryTest { | ||
|
|
||
| private static final String REGISTRY_TYPE_KEY = | ||
| ConfigurationKeys.FILE_ROOT_REGISTRY + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE; | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| System.clearProperty(REGISTRY_TYPE_KEY); | ||
| } | ||
|
|
||
| /** | ||
| * Test getInstances with default config. | ||
| */ | ||
| @Test | ||
| public void testGetInstancesWithDefaultConfig() { | ||
| // Set "registry.type = file" as default config | ||
| System.setProperty(REGISTRY_TYPE_KEY, RegistryType.File.name()); | ||
|
|
||
| List<RegistryService> instances = MultiRegistryFactory.getInstances(); | ||
| Assertions.assertNotNull(instances); | ||
|
|
||
| for (RegistryService service : instances) { | ||
| Assertions.assertNotNull(service); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test buildRegistryServices with multiple tests of the same registry type. | ||
| */ | ||
| @Test | ||
| public void testGetInstancesWithMultiRegistryTypes() throws Throwable { | ||
| // Set up multiple registration center configurations | ||
| System.setProperty(REGISTRY_TYPE_KEY, RegistryType.File.name() + Constants.REGISTRY_TYPE_SPLIT_CHAR + RegistryType.File.name()); | ||
|
|
||
| List<RegistryService> instances = invokeBuildRegistryServices(); | ||
| Assertions.assertNotNull(instances); | ||
| Assertions.assertEquals(1, instances.size()); | ||
| } | ||
|
|
||
| /** | ||
| * Test buildRegistryServices with blank registry type. | ||
| */ | ||
| @Test | ||
| public void testGetInstancesWithBlankRegistryType() throws Throwable { | ||
| System.setProperty(REGISTRY_TYPE_KEY, ""); | ||
|
|
||
| List<RegistryService> instances = invokeBuildRegistryServices(); | ||
| Assertions.assertNotNull(instances); | ||
| } | ||
|
|
||
| /** | ||
| * Test buildRegistryServices with invalid registry type. | ||
| */ | ||
| @Test | ||
| public void testGetInstancesWithInvalidRegistryType() { | ||
| System.setProperty(REGISTRY_TYPE_KEY, "InvalidRegistryType"); | ||
|
|
||
| Assertions.assertThrows(NotSupportYetException.class, MultiRegistryFactoryTest::invokeBuildRegistryServices); | ||
| } | ||
|
|
||
| /** | ||
| * Use reflection to call the buildRegistryServices method | ||
| */ | ||
| private static List<RegistryService> invokeBuildRegistryServices() throws Throwable { | ||
| Method buildMethod = MultiRegistryFactory.class.getDeclaredMethod("buildRegistryServices"); | ||
| buildMethod.setAccessible(true); | ||
|
|
||
| try { | ||
| return (List<RegistryService>) buildMethod.invoke(null); | ||
| } catch (InvocationTargetException e) { | ||
| throw e.getTargetException(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * 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.seata.discovery.registry; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
|
|
||
| import org.apache.seata.common.ConfigurationKeys; | ||
| import org.apache.seata.common.exception.NotSupportYetException; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * The type Registry factory test. | ||
| */ | ||
| public class RegistryFactoryTest { | ||
|
|
||
| private static final String REGISTRY_TYPE_KEY = | ||
| ConfigurationKeys.FILE_ROOT_REGISTRY + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE; | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| System.clearProperty(REGISTRY_TYPE_KEY); | ||
| } | ||
|
|
||
| /** | ||
| * Test getInstance with default config. | ||
| */ | ||
| @Test | ||
| public void testGetInstanceWithDefaultConfig() { | ||
| System.setProperty(REGISTRY_TYPE_KEY, RegistryType.File.name()); | ||
|
|
||
| RegistryService instance = RegistryFactory.getInstance(); | ||
| Assertions.assertNotNull(instance); | ||
| } | ||
|
|
||
| /** | ||
| * Test buildRegistryService with invalid registry type. | ||
| */ | ||
| @Test | ||
| public void testGetInstanceOfInvalidRegistryType() { | ||
| System.setProperty(REGISTRY_TYPE_KEY, "InvalidRegistryType"); | ||
|
|
||
| Assertions.assertThrows(NotSupportYetException.class, RegistryFactoryTest::invokeBuildRegistryService); | ||
| } | ||
|
|
||
| /** | ||
| * Test buildRegistryService with blank registry type. | ||
| */ | ||
| @Test | ||
| public void testGetInstancesWithBlankRegistryType() throws Throwable { | ||
| System.setProperty(REGISTRY_TYPE_KEY, ""); | ||
|
|
||
| RegistryService instance = invokeBuildRegistryService(); | ||
| Assertions.assertEquals(instance.getClass(), FileRegistryServiceImpl.class); | ||
| } | ||
|
|
||
| /** | ||
| * Use reflection to call the buildRegistryServices method | ||
|
||
| */ | ||
| private static RegistryService invokeBuildRegistryService() throws Throwable { | ||
| Method buildMethod = RegistryFactory.class.getDeclaredMethod("buildRegistryService"); | ||
|
|
||
| buildMethod.setAccessible(true); | ||
| try { | ||
| return (RegistryService) buildMethod.invoke(null); | ||
| } catch (InvocationTargetException e) { | ||
| throw e.getTargetException(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not convert this directly to a TreeSet? This would allow for case-insensitivity and also handle deduplication
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
Also, it would be great to add a test case that covers this scenario.