[BUG] Nacos instance updates can emit added, deleted, and updated events together
- Dominant language
- Java
- Stars
- 8.8k
- Forks
- 3.1k
- Avg merge
- 7d 1h
- Merged PRs (30d)
- 85
Description
### Search before asking
- [x] I had searched in the [issues](https://github.com/apache/shenyu/issues) and found no similar issues.
### Apache ShenYu Component
shenyu-registry
### What happened
`NacosInstanceRegisterRepository.compareInstances()` computes added and deleted instances with full `Instance.equals()` comparisons, then computes updated instances by matching `instanceId` and checking `!currentInstance.equals(previousInstance)`.
Current code in `shenyu-registry/shenyu-registry-nacos/src/main/java/org/apache/shenyu/registry/nacos/NacosInstanceRegisterRepository.java`:
```java
Set addedInstances = currentInstances.stream()
.filter(item -> !previousInstances.contains(item))
.collect(Collectors.toSet());
Set deletedInstances = previousInstances.stream()
.filter(item -> !currentInstances.contains(item))
.collect(Collectors.toSet());
Set updatedInstances = currentInstances.stream()
.filter(currentInstance -> Objects.nonNull(currentInstance.getInstanceId())
&& previousInstances.stream().anyMatch(previousInstance -> StringUtils.isNotBlank(previousInstance.getInstanceId())
&& currentInstance.getInstanceId().equals(previousInstance.getInstanceId())
&& !currentInstance.equals(previousInstance)))
.collect(Collectors.toSet());
```
ShenYu currently uses Nacos client `2.2.4`. In that version, `com.alibaba.nacos.api.naming.pojo.Instance.equals()` compares `toString()` values, so changes to health, enabled flag, weight, metadata, and other fields make two objects unequal even when they represent the same `instanceId`.
As a result, a property-only update for the same Nacos instance can be emitted as:
1. `ADDED`, because the changed current `Instance` is not contained in `previousInstances`.
2. `DELETED`, because the old `Instance` is not contained in `currentInstances`.
3. `UPDATED`, because the same `instanceId` exists and `equals()` is false.
Downstream discovery consumers can receive contradictory events for one logical update.
### What you expected to happen
For the same Nacos `instanceId`, a property-only change should emit one `UPDATED` event. Added and deleted detection should compare stable instance identity, not full mutable instance state.
### How to reproduce
1. Use Nacos discovery with ShenYu.
2. Register one instance and let `NacosInstanceRegisterRepository` store it in `watcherInstanceRegisterMap`.
3. Change only a mutable property of the same Nacos instance, such as weight or metadata, while keeping the same `instanceId`.
4. Trigger the Nacos listener and inspect events from `compareInstances()`.
### Debug logs
_No response_
### Environment
Current master branch.
### Are you willing to submit PR?
- [ ] Yes I am willing to submit a PR!
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in shenyu-registry/shenyu-registry-nacos/src/main/java/org/apache/shenyu/registry/nacos/NacosInstanceRegisterRepository.java, focusing on compareInstances() and the Nacos Instance identity behavior described in the issue. Verify the listener path that consumes its event sets. Done means a property-only change for one instanceId produces only UPDATED, while genuine additions and removals still produce ADDED or DELETED.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend, distributed-systems
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 74/100