alibaba / alibaba/Sentinel

sentinel自行持久化后的一个bug,会导致新增的各类规则把持久数据源加载出来的规则冲掉

Open
#1,868 5 comments 0 reactions 0 assignees View on GitHub
area/dashboard
Dominant language
Java
Stars
23.1k
Forks
8.1k
PR merge metrics
No merged PRs in 30d

Description

用nacos做的持久化,至于如何持久化的,网上一堆教程,用的push模式

但是当sentinel重启后,com.alibaba.csp.sentinel.dashboard.repository.rule下的InMemoryRuleRepositoryAdapter中的save方法

```java
@Override
public T save(T entity) {
if (entity.getId() == null) {
entity.setId(nextId());
}
T processedEntity = preProcess(entity);
if (processedEntity != null) {
allRules.put(processedEntity.getId(), processedEntity);
machineRules.computeIfAbsent(MachineInfo.of(processedEntity.getApp(), processedEntity.getIp(),
processedEntity.getPort()), e -> new ConcurrentHashMap<>(32))
.put(processedEntity.getId(), processedEntity);
appRules.computeIfAbsent(processedEntity.getApp(), v -> new ConcurrentHashMap<>(32))
.put(processedEntity.getId(), processedEntity);
}

return processedEntity;
}
```

给entity赋值的时候,这里用的nextId(),而这个nextId的逻辑是下面这里:

```java
@Component
public class InMemFlowRuleStore extends InMemoryRuleRepositoryAdapter {

private static AtomicLong ids = new AtomicLong(0);

@Override
protected long nextId() {
return ids.incrementAndGet();
}

@Override
protected FlowRuleEntity preProcess(FlowRuleEntity entity) {
if (entity != null && entity.isClusterMode()) {
ClusterFlowConfig config = entity.getClusterConfig();
if (config == null) {
config = new ClusterFlowConfig();
entity.setClusterConfig(config);
}
// Set cluster rule id.
config.setFlowId(entity.getId());
}
return entity;
}
```

所以会导致id直接从1开始赋新的值,而allRules这个Map又会以这个id为key进行规则的存储,导致sentinel重启后,新增的各类规则会逐步将原有规则冲掉了,需要自己修改一下这里的id生成逻辑

Contributor guide

Open the contributing guide

Research direction

Start by reading com.alibaba.csp.sentinel.dashboard.repository.rule.InMemoryRuleRepositoryAdapter.save and the InMemFlowRuleStore.nextId implementation. Reproduce the restart scenario with Nacos push-mode persistence and observe how newly assigned IDs interact with allRules. Done means newly saved rules no longer overwrite rules loaded from persistent storage after a restart.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.