Skip to content

Commit

Permalink
bugfix: fix namingserver changVgroup failed (#6817)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggbocoder authored Sep 6, 2024
1 parent 27973af commit 145b33c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
3 changes: 2 additions & 1 deletion changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6797](https://github.com/apache/incubator-seata/pull/6797)] fall back to any of available cluster address when query cluster address is empty
- [[#6800](https://github.com/apache/incubator-seata/pull/6800)] make exception message generic for all database drivers
- [[#6759](https://github.com/apache/incubator-seata/pull/6759)] fix the error of active refresh failure of cross-database table metadata
- [[#6812](https://github.com/apache/incubator-seata/pull/6812)] change group and node offline status are not pushed in real time
- [[#6812](https://github.com/apache/incubator-seata/pull/6812)] bugfix: change group and node offline status are not pushed in real time
- [[#6817](https://github.com/apache/incubator-seata/pull/6817)] bugfix: fix namingserver changVgroup failed
- [[#6820](https://github.com/apache/incubator-seata/pull/6820)] Fix file path error in the Dockerfile


Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- [[#6800](https://github.com/apache/incubator-seata/pull/6800)] 使异常消息对所有数据库驱动程序通用
- [[#6812](https://github.com/apache/incubator-seata/pull/6812)] 修复切换事务分组和节点下线时namingserver没有实时感知和推送的bug
- [[#6759](https://github.com/apache/incubator-seata/pull/6759)] 修复跨库表主动刷新`tableMeta`的异常问题
- [[#6817](https://github.com/apache/incubator-seata/pull/6817)] 修复namingserver切换事务分组失效的问题
- [[#6820](https://github.com/apache/incubator-seata/pull/6820)] 修复Dockerfile得文件结构错误
-
### optimize:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.seata.common.metadata.Cluster;
Expand Down Expand Up @@ -56,7 +57,11 @@ public ClusterBO getCluster(String clusterName) {
}

public void removeOldCluster(String clusterName) {
clusterMap.keySet().forEach(currentClusterName -> {
Set<String> clusterSet = clusterMap.keySet();
if (clusterSet.size() <= 1) {
return;
}
clusterSet.forEach(currentClusterName -> {
if (!StringUtils.equals(currentClusterName, clusterName)) {
clusterMap.remove(currentClusterName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,28 +205,29 @@ public Result<String> removeGroup(Unit unit, String vGroup, String clusterName,
return new Result<>("200", "remove group in old cluster successfully!");
}

public void addGroup(String namespace, String clusterName, String unitName, String vGroup) {
public boolean addGroup(String namespace, String clusterName, String unitName, String vGroup) {
try {
ClusterBO clusterBO = vGroupMap.get(vGroup, k -> new ConcurrentHashMap<>())
.computeIfAbsent(namespace, k -> new NamespaceBO()).getCluster(clusterName);
if (clusterBO != null && !clusterBO.getUnitNames().contains(unitName)) {
clusterBO.addUnit(unitName);
if (clusterBO != null /**&& !clusterBO.getUnitNames().contains(unitName)**/) {
boolean needNotify = !clusterBO.getUnitNames().contains(unitName);
NamespaceBO namespaceBO = vGroupMap.getIfPresent(vGroup).get(namespace);
namespaceBO.removeOldCluster(clusterName);
applicationContext.publishEvent(new ClusterChangeEvent(this, vGroup, System.currentTimeMillis()));
if (needNotify) {
clusterBO.addUnit(unitName);
}
return needNotify;
}
} catch (Exception e) {
LOGGER.error("change vGroup mapping failed:{}", vGroup, e);
}
return false;
}

public void notifyClusterChange(String vGroup, String namespace, String clusterName, String unitName, long term) {

Optional.ofNullable(vGroupMap.asMap().get(vGroup)).flatMap(map -> Optional.ofNullable(map.get(namespace)).flatMap(namespaceBO -> Optional.ofNullable(namespaceBO.getCluster(clusterName)))).ifPresent(clusterBO -> {
// Set<String> units = clusterBO.getUnitNames();
// if (!CollectionUtils.isEmpty(units)) {
applicationContext.publishEvent(new ClusterChangeEvent(this, vGroup, term));
// }
});
}

Expand All @@ -248,8 +249,8 @@ public boolean registerInstance(NamingServerNode node, String namespace, String
vGroups.forEach((k, v) -> {
// In non-raft mode, a unit is one-to-one with a node, and the unitName is stored on the node.
// In raft mode, the unitName is equal to the raft-group, so the node's unitName cannot be used.
addGroup(namespace, clusterName, StringUtils.isBlank(v) ? unitName : v, k);
if (hasChanged) {
boolean changed = addGroup(namespace, clusterName, StringUtils.isBlank(v) ? unitName : v, k);
if (hasChanged || changed) {
notifyClusterChange(k, namespace, clusterName, unitName, node.getTerm());
}
});
Expand Down Expand Up @@ -360,6 +361,7 @@ public void instanceHeartBeatCheck() {
}

public Result<String> changeGroup(String namespace, String vGroup, String clusterName, String unitName) {
long changeTime = System.currentTimeMillis();
ConcurrentMap<String, NamespaceBO> namespaceMap = new ConcurrentHashMap<>(vGroupMap.get(vGroup));
Set<String> currentNamespaces = namespaceMap.keySet();
Map<String, Set<String>> namespaceClusters = new HashMap<>();
Expand All @@ -384,7 +386,7 @@ public Result<String> changeGroup(String namespace, String vGroup, String cluste
String unit = optionalEntry.get().getKey();
Unit unitData = optionalEntry.get().getValue();
result.set(removeGroup(unitData, vGroup, cluster, oldNamespace, unitName));
notifyClusterChange(vGroup, namespace, cluster, unit, -1);
notifyClusterChange(vGroup, namespace, cluster, unit, changeTime);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ public Result<?> addVGroup(@RequestParam String vGroup, @RequestParam String uni
result.setCode("500");
result.setMessage("add vGroup failed!");
}
// push the newest mapping relationship
vGroupMappingStoreManager.notifyMapping();
return result;
}

Expand All @@ -93,8 +91,6 @@ public Result<?> removeVGroup(@RequestParam String vGroup) {
result.setCode("500");
result.setMessage("remove vGroup failed!");
}
// push the newest mapping relationship
vGroupMappingStoreManager.notifyMapping();
return result;
}

Expand Down

0 comments on commit 145b33c

Please sign in to comment.