Skip to content

fix: incorrect cross-cluster call warning when clusterName is not set#4287

Merged
uuuyuqi merged 1 commit into
alibaba:2025.1.xfrom
daguimu:fix/incorrect-cross-cluster-call-log-issue4020
Apr 2, 2026
Merged

fix: incorrect cross-cluster call warning when clusterName is not set#4287
uuuyuqi merged 1 commit into
alibaba:2025.1.xfrom
daguimu:fix/incorrect-cross-cluster-call-log-issue4020

Conversation

@daguimu

@daguimu daguimu commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Describe what this PR does / why we need it

In NacosLoadBalancer.getInstanceResponse(), the "A cross-cluster call occurs" warning log is incorrectly placed in the outer else block, which triggers when clusterName is blank (null or empty). Since PR #3771 removed the default value for clusterName, this warning fires on every load balancing request when users don't explicitly configure a cluster name.

The warning should only appear when clusterName IS configured but no same-cluster instances are found — that is the actual cross-cluster call scenario.

Does this pull request fix one issue?

Fixes #4020

Describe how you did it

Moved the else block with the warning log from the outer if (StringUtils.isNotBlank(clusterName)) into the inner if (!CollectionUtils.isEmpty(sameClusterInstances)).

Before (incorrect):

if (StringUtils.isNotBlank(clusterName)) {
    // filter same cluster instances
    if (!CollectionUtils.isEmpty(sameClusterInstances)) {
        instancesToChoose = sameClusterInstances;
    }
}
else {
    log.warn("A cross-cluster call occurs..."); // triggers when clusterName is BLANK
}

After (correct):

if (StringUtils.isNotBlank(clusterName)) {
    // filter same cluster instances
    if (!CollectionUtils.isEmpty(sameClusterInstances)) {
        instancesToChoose = sameClusterInstances;
    }
    else {
        log.warn("A cross-cluster call occurs..."); // triggers when clusterName is SET but no match
    }
}

Describe how to verify it

  1. Start a Spring Cloud Alibaba application with Nacos discovery without setting spring.cloud.nacos.discovery.cluster-name
  2. Make load-balanced requests
  3. Verify no "A cross-cluster call occurs" warning in logs

Tests Added

Change Point Test
Blank clusterName should not warn blankClusterNameShouldNotWarnCrossCluster()
Empty clusterName should not warn emptyClusterNameShouldNotWarnCrossCluster()
Same cluster instances selected correctly sameClusterShouldSelectSameClusterInstance()
No same-cluster instances still returns response noSameClusterInstancesShouldStillReturnResponse()
Empty instances returns EmptyResponse emptyServiceInstancesShouldReturnEmptyResponse()

Special notes for reviews

PR #4021 proposed the same fix for the 2021.x branch. This PR applies it to the current 2025.1.x branch with comprehensive unit tests.

@uuuyuqi

uuuyuqi commented Mar 27, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the fix. One follow-up concern: this warning is still emitted from the request path in NacosLoadBalancer#getInstanceResponse(). That means if a service stays in a real cross-cluster fallback state for some time, the log can still be printed once per request and flood the logs under traffic.

Would you consider adding some form of throttling here, for example:

  • log at most once per time window per (serviceId, clusterName), or
  • log only on state transition (entering cross-cluster fallback), and suppress repeats until recovery

The current fix removes the false positives, which is great, but rate-limiting the warning would make the behavior much safer in sustained fallback scenarios.

@daguimu daguimu force-pushed the fix/incorrect-cross-cluster-call-log-issue4020 branch from a5e0e0e to 6b69710 Compare March 27, 2026 13:44
@daguimu

daguimu commented Mar 27, 2026

Copy link
Copy Markdown
Contributor Author

@uuuyuqi Good point! I've updated this PR to include log throttling — the cross-cluster warning now logs at most once per 5 minutes per (serviceId, clusterName) using a ConcurrentHashMap<String, Long> timestamp tracker. This prevents log flooding under sustained cross-cluster fallback traffic while still surfacing the warning when it first occurs.

Added test: crossClusterWarnShouldBeThrottled() — verifies the throttle state is tracked correctly after repeated cross-cluster calls.

@daguimu daguimu force-pushed the fix/incorrect-cross-cluster-call-log-issue4020 branch 2 times, most recently from ab76748 to e0d2315 Compare March 27, 2026 15:16
Move the "A cross-cluster call occurs" warning from the outer else
block (which triggers when clusterName is blank) into the inner else
block (which triggers when clusterName IS set but no same-cluster
instances are found).

Additionally, throttle the cross-cluster warning to log at most once
per 5 minutes per (serviceId, clusterName) to prevent log flooding
under sustained cross-cluster fallback traffic.

Fixes alibaba#4020
@daguimu daguimu force-pushed the fix/incorrect-cross-cluster-call-log-issue4020 branch from e0d2315 to 1ddb22a Compare April 1, 2026 14:34

@uuuyuqi uuuyuqi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@uuuyuqi uuuyuqi merged commit b3161a2 into alibaba:2025.1.x Apr 2, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The log was incorrectly printed: "A cross-cluster call occurs"

2 participants