-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
optimize: redis registry push empty protection optimize #6164
Merged
Merged
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
85f0b0e
redis registry push empty protection optimize
laywin 9911bb4
add change log
laywin 5ed822a
add unit test
laywin 3449120
add test case
funky-eyes d98ad94
Merge branch '2.x' into redis_push_empty_protection
funky-eyes 8be6b42
Merge branch 'redis_push_empty_protection' of github.com:laywin/seata…
funky-eyes 7ae2bd5
add test case
funky-eyes 8e6d283
add test case
funky-eyes 88ff43b
add test case
funky-eyes 3846568
add test case
funky-eyes 6f04d1d
add test case
funky-eyes 56780ae
add test case
funky-eyes 776497d
add test case
funky-eyes becbea9
test
funky-eyes 5749426
test
funky-eyes d423833
test
funky-eyes 85a4b72
optimize
laywin 2cc328b
1. replace ConcurrentHashMap.computeIfAbsent to CollectionUtils.compu…
laywin 0424055
fix: check style
laywin c845405
1. optimize unit test
laywin 912c0e5
Merge branch '2.x' into redis_push_empty_protection
funky-eyes d51cba5
optimize unit test
laywin c31f3f0
Update discovery/seata-discovery-redis/src/test/java/io/seata/discove…
funky-eyes 30e68c6
Merge branch '2.x' of github.com:seata/seata into redis_push_empty_pr…
funky-eyes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...y-redis/src/test/java/io/seata/discovery/registry/redis/RedisRegisterServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright 1999-2019 Seata.io Group. | ||
* | ||
* Licensed 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. | ||
*/ | ||
funky-eyes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
package io.seata.discovery.registry.redis; | ||
|
||
import com.github.microwww.redis.RedisServer; | ||
import io.seata.common.util.NetUtil; | ||
import io.seata.config.Configuration; | ||
import io.seata.config.ConfigurationFactory; | ||
import org.junit.jupiter.api.*; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
import org.mockito.internal.util.collections.Sets; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.net.InetSocketAddress; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.*; | ||
funky-eyes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @author laywin | ||
*/ | ||
public class RedisRegisterServiceImplTest { | ||
|
||
Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
private static RedisRegistryServiceImpl redisRegistryService; | ||
|
||
private static RedisServer server; | ||
|
||
|
||
@BeforeAll | ||
public static void init() throws IOException { | ||
System.setProperty("config.type", "file"); | ||
System.setProperty("config.file.name", "file.conf"); | ||
System.setProperty("txServiceGroup", "default_tx_group"); | ||
System.setProperty("service.vgroupMapping.default_tx_group", "default"); | ||
System.setProperty("registry.redis.serverAddr", "127.0.0.1:6789"); | ||
System.setProperty("registry.redis.cluster", "default"); | ||
RedisServer server = new RedisServer(); | ||
server.listener("127.0.0.1", 6789); | ||
redisRegistryService = RedisRegistryServiceImpl.getInstance(); | ||
} | ||
|
||
@Test | ||
public void testFlow() { | ||
|
||
redisRegistryService.register(new InetSocketAddress(NetUtil.getLocalIp(), 8091)); | ||
|
||
redisRegistryService.unregister(new InetSocketAddress(NetUtil.getLocalIp(), 8091)); | ||
|
||
Assertions.assertTrue(redisRegistryService.lookup("default_tx_group").size() > 0); | ||
funky-eyes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Test | ||
public void testRemoveServerAddressByPushEmptyProtection() | ||
throws NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
|
||
MockedStatic<ConfigurationFactory> configurationFactoryMockedStatic = mockStatic(ConfigurationFactory.class); | ||
Configuration configuration = mock(Configuration.class); | ||
when(configuration.getConfig(anyString())).thenReturn("cluster"); | ||
|
||
configurationFactoryMockedStatic.when(ConfigurationFactory::getInstance).thenReturn(configuration); | ||
|
||
Field field = RedisRegistryServiceImpl.class.getDeclaredField("CLUSTER_ADDRESS_MAP"); | ||
field.setAccessible(true); | ||
|
||
ConcurrentMap<String, Set<InetSocketAddress>> CLUSTER_ADDRESS_MAP = (ConcurrentMap<String, Set<InetSocketAddress>>)field.get(null); | ||
CLUSTER_ADDRESS_MAP.put("cluster", Sets.newSet(NetUtil.toInetSocketAddress("127.0.0.1:8091"))); | ||
|
||
Method method = RedisRegistryServiceImpl.class.getDeclaredMethod("removeServerAddressByPushEmptyProtection", String.class, String.class); | ||
method.setAccessible(true); | ||
method.invoke(redisRegistryService, "cluster", "127.0.0.1:8091"); | ||
|
||
// test the push empty protection situation | ||
Assertions.assertEquals(1, CLUSTER_ADDRESS_MAP.get("cluster").size()); | ||
|
||
|
||
when(configuration.getConfig(anyString())).thenReturn("mycluster"); | ||
|
||
method.invoke(redisRegistryService, "cluster", "127.0.0.1:8091"); | ||
configurationFactoryMockedStatic.close(); | ||
|
||
// test the normal remove situation | ||
Assertions.assertEquals(0, CLUSTER_ADDRESS_MAP.get("cluster").size()); | ||
} | ||
|
||
@AfterAll | ||
public static void afterAll() { | ||
if (server != null) { | ||
try { | ||
server.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
请考虑并发性问题
Please consider concurrency issues