Skip to content
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

add tap control for same sync task #267

Merged
merged 2 commits into from
Aug 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class NacosSyncToNacosServiceImpl implements SyncService {

private final Map<String, Set<String>> sourceInstanceSnapshot = new ConcurrentHashMap<>();

private final Map<String, Integer> syncTaskTap = new ConcurrentHashMap<>();

@Autowired
private MetricsManager metricsManager;

Expand Down Expand Up @@ -181,18 +183,26 @@ public boolean sync(TaskDO taskDO) {

private void doSync(String taskId, TaskDO taskDO, NamingService sourceNamingService,
NamingService destNamingService) throws NacosException {
// 只同步healthy为true的实例
List<Instance> sourceInstances = sourceNamingService.getAllInstances(taskDO.getServiceName(),
getGroupNameOrDefault(taskDO.getGroupName()), new ArrayList<>(), true);
// 先删除不存在的
this.removeInvalidInstance(taskDO, destNamingService, sourceInstances);
// 如果同步实例已经为空代表该服务所有实例已经下线,清除本地持有快照
if (sourceInstances.isEmpty()) {
sourceInstanceSnapshot.remove(taskId);
if (syncTaskTap.putIfAbsent(taskId, 1) != null) {
log.info("任务Id:{}上一个同步任务尚未结束", taskId);
return;
}
// 同步实例
this.syncNewInstance(taskDO, destNamingService, sourceInstances);
try {
// 直接从本地保存的serviceInfoMap中取订阅的服务实例
List<Instance> sourceInstances = sourceNamingService.getAllInstances(taskDO.getServiceName(),
getGroupNameOrDefault(taskDO.getGroupName()), new ArrayList<>(), true);
// 先删除不存在的
this.removeInvalidInstance(taskDO, destNamingService, sourceInstances);
// 如果同步实例已经为空代表该服务所有实例已经下线,清除本地持有快照
if (sourceInstances.isEmpty()) {
sourceInstanceSnapshot.remove(taskId);
return;
}
// 同步实例
this.syncNewInstance(taskDO, destNamingService, sourceInstances);
} finally {
syncTaskTap.remove(taskId);
}
}

private void syncNewInstance(TaskDO taskDO, NamingService destNamingService,
Expand Down