#502 Fix data inconsistent bug

This commit is contained in:
nkorange 2019-03-26 14:07:06 +08:00
parent e2a720640a
commit 14ecd9c230
4 changed files with 76 additions and 51 deletions

View File

@ -138,6 +138,16 @@ public interface NamingService {
*/ */
void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException; void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException;
/**
* deregister instance with full instance information
*
* @param serviceName name of service
* @param groupName group of service
* @param instance instance information
* @throws NacosException
*/
void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException;
/** /**
* get all instances of a service * get all instances of a service
* *

View File

@ -316,8 +316,18 @@ public class NacosNamingService implements NamingService {
@Override @Override
public void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException { public void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException {
beatReactor.removeBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), ip, port); Instance instance = new Instance();
serverProxy.deregisterService(NamingUtils.getGroupedName(serviceName, groupName), ip, port, clusterName); instance.setIp(ip);
instance.setPort(port);
instance.setClusterName(clusterName);
deregisterInstance(serviceName, groupName, instance);
}
@Override
public void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException {
beatReactor.removeBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), instance.getIp(), instance.getPort());
serverProxy.deregisterService(NamingUtils.getGroupedName(serviceName, groupName), instance);
} }
@Override @Override

View File

@ -187,17 +187,18 @@ public class NamingProxy {
} }
public void deregisterService(String serviceName, String ip, int port, String clusterName) throws NacosException { public void deregisterService(String serviceName, Instance instance) throws NacosException {
NAMING_LOGGER.info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}:{}@{}", NAMING_LOGGER.info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}",
namespaceId, serviceName, ip, port, clusterName); namespaceId, serviceName, instance);
final Map<String, String> params = new HashMap<String, String>(8); final Map<String, String> params = new HashMap<String, String>(8);
params.put(CommonParams.NAMESPACE_ID, namespaceId); params.put(CommonParams.NAMESPACE_ID, namespaceId);
params.put("ip", ip);
params.put("port", String.valueOf(port));
params.put(CommonParams.SERVICE_NAME, serviceName); params.put(CommonParams.SERVICE_NAME, serviceName);
params.put(CommonParams.CLUSTER_NAME, clusterName); params.put(CommonParams.CLUSTER_NAME, instance.getClusterName());
params.put("ip", instance.getIp());
params.put("port", String.valueOf(instance.getPort()));
params.put("ephemeral", String.valueOf(instance.isEphemeral()));
reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.DELETE); reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.DELETE);
} }

View File

@ -100,9 +100,9 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
public volatile Notifier notifier = new Notifier(); public volatile Notifier notifier = new Notifier();
private volatile Map<String, CopyOnWriteArrayList<RecordListener>> listeners = new ConcurrentHashMap<>(); private Map<String, CopyOnWriteArrayList<RecordListener>> listeners = new ConcurrentHashMap<>();
private volatile Map<String, String> syncChecksumTasks = new ConcurrentHashMap<>(16); private Map<String, String> syncChecksumTasks = new ConcurrentHashMap<>(16);
@PostConstruct @PostConstruct
public void init() throws Exception { public void init() throws Exception {
@ -200,52 +200,56 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
syncChecksumTasks.put(server, "1"); syncChecksumTasks.put(server, "1");
List<String> toUpdateKeys = new ArrayList<>(); try {
List<String> toRemoveKeys = new ArrayList<>();
for (Map.Entry<String, String> entry : checksumMap.entrySet()) { List<String> toUpdateKeys = new ArrayList<>();
if (distroMapper.responsible(KeyBuilder.getServiceName(entry.getKey()))) { List<String> toRemoveKeys = new ArrayList<>();
// this key should not be sent from remote server: for (Map.Entry<String, String> entry : checksumMap.entrySet()) {
Loggers.EPHEMERAL.error("receive responsible key timestamp of " + entry.getKey() + " from " + server); if (distroMapper.responsible(KeyBuilder.getServiceName(entry.getKey()))) {
// abort the procedure: // this key should not be sent from remote server:
Loggers.EPHEMERAL.error("receive responsible key timestamp of " + entry.getKey() + " from " + server);
// abort the procedure:
return;
}
if (!dataStore.contains(entry.getKey()) ||
dataStore.get(entry.getKey()).value == null ||
!dataStore.get(entry.getKey()).value.getChecksum().equals(entry.getValue())) {
toUpdateKeys.add(entry.getKey());
}
}
for (String key : dataStore.keys()) {
if (!server.equals(distroMapper.mapSrv(KeyBuilder.getServiceName(key)))) {
continue;
}
if (!checksumMap.containsKey(key)) {
toRemoveKeys.add(key);
}
}
Loggers.EPHEMERAL.info("to remove keys: {}, to update keys: {}, source: {}", toRemoveKeys, toUpdateKeys, server);
for (String key : toRemoveKeys) {
onRemove(key);
}
if (toUpdateKeys.isEmpty()) {
return; return;
} }
if (!dataStore.contains(entry.getKey()) ||
dataStore.get(entry.getKey()).value == null || try {
!dataStore.get(entry.getKey()).value.getChecksum().equals(entry.getValue())) { byte[] result = NamingProxy.getData(toUpdateKeys, server);
toUpdateKeys.add(entry.getKey()); processData(result);
} catch (Exception e) {
Loggers.EPHEMERAL.error("get data from " + server + " failed!", e);
} }
} finally {
// Remove this 'in process' flag:
syncChecksumTasks.remove(server);
} }
for (String key : dataStore.keys()) {
if (!server.equals(distroMapper.mapSrv(KeyBuilder.getServiceName(key)))) {
continue;
}
if (!checksumMap.containsKey(key)) {
toRemoveKeys.add(key);
}
}
Loggers.EPHEMERAL.info("to remove keys: {}, to update keys: {}, source: {}", toRemoveKeys, toUpdateKeys, server);
for (String key : toRemoveKeys) {
onRemove(key);
}
if (toUpdateKeys.isEmpty()) {
return;
}
try {
byte[] result = NamingProxy.getData(toUpdateKeys, server);
processData(result);
} catch (Exception e) {
Loggers.EPHEMERAL.error("get data from " + server + " failed!", e);
}
// Remove this 'in process' flag:
syncChecksumTasks.remove(server);
} }
public boolean syncAllDataFromRemote(Server server) { public boolean syncAllDataFromRemote(Server server) {