-
Notifications
You must be signed in to change notification settings - Fork 8.5k
fix: incorrect cross-cluster call warning when clusterName is not set #4287
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
Merged
uuuyuqi
merged 1 commit into
alibaba:2025.1.x
from
daguimu:fix/incorrect-cross-cluster-call-log-issue4020
Apr 2, 2026
Merged
Changes from all commits
Commits
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 hidden or 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
229 changes: 229 additions & 0 deletions
229
...-discovery/src/test/java/com/alibaba/cloud/nacos/loadbalancer/NacosLoadBalancerTests.java
This file contains hidden or 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,229 @@ | ||
| /* | ||
| * Copyright 2013-present the original author or authors. | ||
| * | ||
| * 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 | ||
| * | ||
| * https://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 com.alibaba.cloud.nacos.loadbalancer; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import ch.qos.logback.classic.Level; | ||
| import ch.qos.logback.classic.spi.ILoggingEvent; | ||
| import ch.qos.logback.core.read.ListAppender; | ||
| import com.alibaba.cloud.nacos.NacosDiscoveryProperties; | ||
| import com.alibaba.cloud.nacos.util.InetIPv6Utils; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import org.springframework.beans.factory.ObjectProvider; | ||
| import org.springframework.cloud.client.DefaultServiceInstance; | ||
| import org.springframework.cloud.client.ServiceInstance; | ||
| import org.springframework.cloud.client.loadbalancer.DefaultResponse; | ||
| import org.springframework.cloud.client.loadbalancer.EmptyResponse; | ||
| import org.springframework.cloud.client.loadbalancer.Request; | ||
| import org.springframework.cloud.client.loadbalancer.Response; | ||
| import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * Tests for {@link NacosLoadBalancer} cross-cluster call log logic. | ||
| * | ||
| * @author daguimu | ||
| */ | ||
| @ExtendWith(MockitoExtension.class) | ||
| public class NacosLoadBalancerTests { | ||
|
|
||
| @Mock | ||
| private ObjectProvider<ServiceInstanceListSupplier> supplierProvider; | ||
|
|
||
| @Mock | ||
| private InetIPv6Utils inetIPv6Utils; | ||
|
|
||
| @Mock | ||
| private LoadBalancerAlgorithm defaultAlgorithm; | ||
|
|
||
| private NacosDiscoveryProperties nacosDiscoveryProperties; | ||
|
|
||
| private NacosLoadBalancer nacosLoadBalancer; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| nacosDiscoveryProperties = new NacosDiscoveryProperties(); | ||
|
|
||
| Map<String, LoadBalancerAlgorithm> algorithmMap = new HashMap<>(); | ||
| algorithmMap.put(LoadBalancerAlgorithm.DEFAULT_SERVICE_ID, defaultAlgorithm); | ||
|
|
||
| nacosLoadBalancer = new NacosLoadBalancer( | ||
| supplierProvider, | ||
| "test-service", | ||
| nacosDiscoveryProperties, | ||
| inetIPv6Utils, | ||
| Collections.emptyList(), | ||
| algorithmMap); | ||
| } | ||
|
|
||
| @Test | ||
| public void blankClusterNameShouldNotWarnCrossCluster() { | ||
| nacosDiscoveryProperties.setClusterName(null); | ||
| List<ServiceInstance> instances = createInstances("192.168.1.1", "clusterA"); | ||
|
|
||
| when(defaultAlgorithm.getInstance(any(), any())).thenReturn(instances.get(0)); | ||
|
|
||
| Response<ServiceInstance> response = invokeGetInstanceResponse(instances); | ||
|
|
||
| assertThat(response).isInstanceOf(DefaultResponse.class); | ||
| assertThat(response.hasServer()).isTrue(); | ||
| assertThat(getLastWarnLogTime()).isEqualTo(0L); | ||
| } | ||
|
|
||
| @Test | ||
| public void emptyClusterNameShouldNotWarnCrossCluster() { | ||
| nacosDiscoveryProperties.setClusterName(""); | ||
| List<ServiceInstance> instances = createInstances("192.168.1.1", "clusterA"); | ||
|
|
||
| when(defaultAlgorithm.getInstance(any(), any())).thenReturn(instances.get(0)); | ||
|
|
||
| Response<ServiceInstance> response = invokeGetInstanceResponse(instances); | ||
|
|
||
| assertThat(response).isInstanceOf(DefaultResponse.class); | ||
| assertThat(response.hasServer()).isTrue(); | ||
| assertThat(getLastWarnLogTime()).isEqualTo(0L); | ||
| } | ||
|
|
||
| @Test | ||
| public void sameClusterShouldSelectSameClusterInstance() { | ||
| nacosDiscoveryProperties.setClusterName("clusterA"); | ||
|
|
||
| List<ServiceInstance> instances = new ArrayList<>(); | ||
| instances.add(createInstance("192.168.1.1", "clusterA")); | ||
| instances.add(createInstance("192.168.1.2", "clusterB")); | ||
|
|
||
| ServiceInstance clusterAInstance = instances.get(0); | ||
| when(defaultAlgorithm.getInstance(any(), any())).thenReturn(clusterAInstance); | ||
|
|
||
| Response<ServiceInstance> response = invokeGetInstanceResponse(instances); | ||
|
|
||
| assertThat(response).isInstanceOf(DefaultResponse.class); | ||
| assertThat(response.hasServer()).isTrue(); | ||
| assertThat(getLastWarnLogTime()).isEqualTo(0L); | ||
| } | ||
|
|
||
| @Test | ||
| public void noSameClusterInstancesShouldStillReturnResponse() { | ||
| nacosDiscoveryProperties.setClusterName("clusterC"); | ||
|
|
||
| List<ServiceInstance> instances = new ArrayList<>(); | ||
| instances.add(createInstance("192.168.1.1", "clusterA")); | ||
| instances.add(createInstance("192.168.1.2", "clusterB")); | ||
|
|
||
| when(defaultAlgorithm.getInstance(any(), any())).thenReturn(instances.get(0)); | ||
|
|
||
| Response<ServiceInstance> response = invokeGetInstanceResponse(instances); | ||
|
|
||
| assertThat(response).isInstanceOf(DefaultResponse.class); | ||
| assertThat(response.hasServer()).isTrue(); | ||
| assertThat(getLastWarnLogTime()).isGreaterThan(0L); | ||
| } | ||
|
|
||
| @Test | ||
| public void crossClusterWarnShouldBeThrottled() { | ||
| nacosDiscoveryProperties.setClusterName("clusterC"); | ||
|
|
||
| List<ServiceInstance> instances = new ArrayList<>(); | ||
| instances.add(createInstance("192.168.1.1", "clusterA")); | ||
|
|
||
| when(defaultAlgorithm.getInstance(any(), any())).thenReturn(instances.get(0)); | ||
|
|
||
| // Attach a ListAppender to capture log output | ||
| ch.qos.logback.classic.Logger nacosLogger = (ch.qos.logback.classic.Logger) | ||
| org.slf4j.LoggerFactory.getLogger(NacosLoadBalancer.class); | ||
| ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); | ||
| listAppender.start(); | ||
| nacosLogger.addAppender(listAppender); | ||
|
|
||
| try { | ||
| for (int i = 0; i < 10; i++) { | ||
| Response<ServiceInstance> response = invokeGetInstanceResponse(instances); | ||
| assertThat(response).isInstanceOf(DefaultResponse.class); | ||
| assertThat(response.hasServer()).isTrue(); | ||
| } | ||
|
|
||
| // Warning should be emitted exactly once despite 10 invocations | ||
| long crossClusterWarnCount = listAppender.list.stream() | ||
| .filter(e -> e.getLevel() == Level.WARN) | ||
| .filter(e -> e.getFormattedMessage().contains("cross-cluster call occurs")) | ||
| .count(); | ||
| assertThat(crossClusterWarnCount).isEqualTo(1); | ||
| assertThat(getLastWarnLogTime()).isGreaterThan(0L); | ||
| } | ||
| finally { | ||
| nacosLogger.detachAppender(listAppender); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void emptyServiceInstancesShouldReturnEmptyResponse() { | ||
| Response<ServiceInstance> response = invokeGetInstanceResponse(Collections.emptyList()); | ||
|
|
||
| assertThat(response).isInstanceOf(EmptyResponse.class); | ||
| } | ||
|
|
||
| private Response<ServiceInstance> invokeGetInstanceResponse(List<ServiceInstance> instances) { | ||
| try { | ||
| java.lang.reflect.Method method = NacosLoadBalancer.class.getDeclaredMethod( | ||
| "getInstanceResponse", Request.class, List.class); | ||
| method.setAccessible(true); | ||
| @SuppressWarnings("unchecked") | ||
| Response<ServiceInstance> result = (Response<ServiceInstance>) method.invoke( | ||
| nacosLoadBalancer, (Request<?>) null, instances); | ||
| return result; | ||
| } | ||
| catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private long getLastWarnLogTime() { | ||
| try { | ||
| java.lang.reflect.Field field = NacosLoadBalancer.class.getDeclaredField("lastWarnLogTime"); | ||
| field.setAccessible(true); | ||
| return (long) field.get(nacosLoadBalancer); | ||
| } | ||
| catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private List<ServiceInstance> createInstances(String host, String cluster) { | ||
| List<ServiceInstance> instances = new ArrayList<>(); | ||
| instances.add(createInstance(host, cluster)); | ||
| return instances; | ||
| } | ||
|
|
||
| private ServiceInstance createInstance(String host, String cluster) { | ||
| Map<String, String> metadata = new HashMap<>(); | ||
| metadata.put("nacos.cluster", cluster); | ||
| return new DefaultServiceInstance("instance-" + host, "test-service", host, 8080, false, metadata); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.