#502 Fix serialize problem and do some refactoring for health check
This commit is contained in:
parent
e3440fe7f0
commit
caf7213c2d
@ -50,6 +50,7 @@ public class Service {
|
||||
/**
|
||||
* Health check mode.
|
||||
*/
|
||||
@Deprecated
|
||||
private String healthCheckMode;
|
||||
|
||||
/**
|
||||
|
@ -64,3 +64,7 @@ server.tomcat.accesslog.enabled=true
|
||||
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D
|
||||
# default current work dir
|
||||
server.tomcat.basedir=
|
||||
|
||||
nacos.naming.partition.taskDispatchThreadCount=10
|
||||
nacos.naming.partition.taskDispatchPeriod=200
|
||||
nacos.naming.partition.batchSyncKeyCount=1000
|
||||
|
@ -38,3 +38,7 @@ server.tomcat.basedir=
|
||||
#nacos.security.ignore.urls=/**
|
||||
|
||||
nacos.security.ignore.urls=/,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/login,/v1/console/health,/v1/cs/**,/v1/ns/**,/v1/cmdb/**,/actuator/**
|
||||
|
||||
nacos.naming.partition.taskDispatchThreadCount=10
|
||||
nacos.naming.partition.taskDispatchPeriod=200
|
||||
nacos.naming.partition.batchSyncKeyCount=1000
|
||||
|
@ -28,8 +28,10 @@ import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@ -43,6 +45,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Component
|
||||
@DependsOn("serverListManager")
|
||||
public class DataSyncer implements MemberChangeListener {
|
||||
|
||||
@Autowired
|
||||
@ -61,7 +64,8 @@ public class DataSyncer implements MemberChangeListener {
|
||||
|
||||
private List<Member> servers;
|
||||
|
||||
public DataSyncer() {
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
serverListManager.listen(this);
|
||||
startTimedSync();
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
package com.alibaba.nacos.naming.consistency.ephemeral.partition;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Stores some configurations for Partition protocol
|
||||
@ -23,15 +24,16 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
* @author nkorange
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Component
|
||||
public class PartitionConfig {
|
||||
|
||||
@Value("taskDispatchThreadCount")
|
||||
@Value("${nacos.naming.partition.taskDispatchThreadCount}")
|
||||
private int taskDispatchThreadCount = 10;
|
||||
|
||||
@Value("taskDispatchPeriod")
|
||||
@Value("${nacos.naming.partition.taskDispatchPeriod}")
|
||||
private int taskDispatchPeriod = 2000;
|
||||
|
||||
@Value("batchSyncKeyCount")
|
||||
@Value("${nacos.naming.partition.batchSyncKeyCount}")
|
||||
private int batchSyncKeyCount = 1000;
|
||||
|
||||
public int getTaskDispatchThreadCount() {
|
||||
|
@ -135,25 +135,37 @@ public class RaftStore {
|
||||
}
|
||||
|
||||
if (KeyBuilder.matchServiceMetaKey(file.getName())) {
|
||||
try {
|
||||
return JSON.parseObject(json.replace("\\", ""), new TypeReference<Datum<Service>>() {
|
||||
});
|
||||
} catch (Exception e) {
|
||||
Datum<String> datum = JSON.parseObject(json, new TypeReference<Datum<String>>(){});
|
||||
Datum<Service> serviceDatum = new Datum<>();
|
||||
serviceDatum.timestamp.set(datum.timestamp.get());
|
||||
serviceDatum.key = datum.key;
|
||||
serviceDatum.value = JSON.parseObject(datum.value, Service.class);
|
||||
return serviceDatum;
|
||||
}
|
||||
}
|
||||
|
||||
if (KeyBuilder.matchInstanceListKey(file.getName())) {
|
||||
Datum<List<Instance>> datum = JSON.parseObject(json, new TypeReference<Datum<List<Instance>>>() {
|
||||
try {
|
||||
return JSON.parseObject(json, new TypeReference<Datum<Instances>>() {
|
||||
});
|
||||
Map<String, Instance> instanceMap = new HashMap<>(64);
|
||||
if (datum.value == null || datum.value.isEmpty()) {
|
||||
return datum;
|
||||
} catch (Exception e) {
|
||||
Datum<String> datum = JSON.parseObject(json, new TypeReference<Datum<String>>(){});
|
||||
List<Instance> instanceList = JSON.parseObject(datum.value, new TypeReference<List<Instance>>(){});
|
||||
Datum<Instances> instancesDatum = new Datum<>();
|
||||
instancesDatum.key = datum.key;
|
||||
instancesDatum.timestamp.set(datum.timestamp.get());
|
||||
|
||||
Instances instances = new Instances();
|
||||
instances.setInstanceMap(new HashMap<>(16));
|
||||
for (Instance instance : instanceList) {
|
||||
instances.getInstanceMap().put(instance.getDatumKey(), instance);
|
||||
}
|
||||
for (Instance instance : datum.value) {
|
||||
instanceMap.put(instance.getDatumKey(), instance);
|
||||
return instancesDatum;
|
||||
}
|
||||
Datum<Map<String, Instance>> mapDatum = new Datum<>();
|
||||
mapDatum.value = instanceMap;
|
||||
mapDatum.key = datum.key;
|
||||
mapDatum.timestamp.set(datum.timestamp.get());
|
||||
return mapDatum;
|
||||
}
|
||||
|
||||
return JSON.parseObject(json, Datum.class);
|
||||
|
@ -46,7 +46,7 @@ import java.util.*;
|
||||
* @author nkorange
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + UtilsAndCommons.NACOS_NAMING_CATALOG_CONTEXT)
|
||||
@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/catalog")
|
||||
public class CatalogController {
|
||||
|
||||
@Autowired
|
||||
|
@ -23,7 +23,7 @@ import com.alibaba.nacos.naming.core.DistroMapper;
|
||||
import com.alibaba.nacos.naming.core.Instance;
|
||||
import com.alibaba.nacos.naming.core.Service;
|
||||
import com.alibaba.nacos.naming.core.ServiceManager;
|
||||
import com.alibaba.nacos.naming.healthcheck.HealthCheckMode;
|
||||
import com.alibaba.nacos.naming.healthcheck.HealthCheckType;
|
||||
import com.alibaba.nacos.naming.misc.HttpClient;
|
||||
import com.alibaba.nacos.naming.misc.Loggers;
|
||||
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
||||
@ -102,7 +102,7 @@ public class HealthController {
|
||||
} else {
|
||||
Service service = serviceManager.getService(namespaceId, dom);
|
||||
// Only health check "none" need update health status with api
|
||||
if (service.getHealthCheckMode().equals(HealthCheckMode.none.name())) {
|
||||
if (HealthCheckType.NONE.name().equals(service.getClusterMap().get(clusterName).getHealthChecker().getType())) {
|
||||
for (Instance instance : service.allIPs(Lists.newArrayList(clusterName))) {
|
||||
if (instance.getIp().equals(ip) && instance.getPort() == port) {
|
||||
instance.setValid(valid);
|
||||
|
@ -33,6 +33,7 @@ import com.alibaba.nacos.naming.web.NeedAuth;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@ -45,6 +46,7 @@ import java.util.List;
|
||||
* @author nkorange
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/operator")
|
||||
public class OperatorController {
|
||||
|
||||
@Autowired
|
||||
@ -120,7 +122,7 @@ public class OperatorController {
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/metrics")
|
||||
@RequestMapping(value = "/metrics", method = RequestMethod.GET)
|
||||
public JSONObject metrics(HttpServletRequest request) {
|
||||
|
||||
JSONObject result = new JSONObject();
|
||||
|
@ -78,7 +78,6 @@ public class ServiceController {
|
||||
}
|
||||
|
||||
float protectThreshold = NumberUtils.toFloat(WebUtils.optional(request, "protectThreshold", "0"));
|
||||
String healthCheckMode = WebUtils.optional(request, "healthCheckMode", switchDomain.defaultHealthCheckMode);
|
||||
String metadata = WebUtils.optional(request, "metadata", StringUtils.EMPTY);
|
||||
String selector = WebUtils.optional(request, "selector", StringUtils.EMPTY);
|
||||
Map<String, String> metadataMap = new HashMap<>(16);
|
||||
@ -89,7 +88,6 @@ public class ServiceController {
|
||||
Service domObj = new Service();
|
||||
domObj.setName(serviceName);
|
||||
domObj.setProtectThreshold(protectThreshold);
|
||||
domObj.setHealthCheckMode(healthCheckMode.toLowerCase());
|
||||
domObj.setEnabled(true);
|
||||
domObj.setMetadata(metadataMap);
|
||||
domObj.setSelector(parseSelector(selector));
|
||||
@ -142,7 +140,6 @@ public class ServiceController {
|
||||
res.put("name", serviceName);
|
||||
res.put("namespaceId", domain.getNamespaceId());
|
||||
res.put("protectThreshold", domain.getProtectThreshold());
|
||||
res.put("healthCheckMode", domain.getHealthCheckMode());
|
||||
res.put("metadata", domain.getMetadata());
|
||||
res.put("selector", domain.getSelector());
|
||||
|
||||
|
@ -409,10 +409,6 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement
|
||||
resetWeight = vDom.getResetWeight();
|
||||
}
|
||||
|
||||
if (getHealthCheckMode().equals(vDom.getHealthCheckMode())) {
|
||||
Loggers.SRV_LOG.info("[DOM-UPDATE] dom: {}, healthCheckMode: {} -> {}", getName(), getHealthCheckMode(), vDom.getHealthCheckMode());
|
||||
}
|
||||
|
||||
if (enabled != vDom.getEnabled().booleanValue()) {
|
||||
Loggers.SRV_LOG.info("[DOM-UPDATE] dom: {}, enabled: {} -> {}", getName(), enabled, vDom.getEnabled());
|
||||
enabled = vDom.getEnabled();
|
||||
|
@ -60,12 +60,11 @@ public class ClientBeatCheckTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
if (!domain.getHealthCheckMode().equals(HealthCheckMode.client.name()) ||
|
||||
!getDistroMapper().responsible(domain.getName())) {
|
||||
if (!getDistroMapper().responsible(domain.getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<Instance> instances = domain.allIPs();
|
||||
List<Instance> instances = domain.allIPs(true);
|
||||
|
||||
for (Instance instance : instances) {
|
||||
if (System.currentTimeMillis() - instance.getLastBeat() > ClientBeatProcessor.CLIENT_BEAT_TIMEOUT) {
|
||||
|
@ -61,10 +61,6 @@ public class ClientBeatProcessor implements Runnable {
|
||||
|
||||
public void process() {
|
||||
Service service = this.service;
|
||||
if (!service.getHealthCheckMode().equals(HealthCheckMode.client.name())) {
|
||||
return;
|
||||
}
|
||||
|
||||
Loggers.EVT_LOG.debug("[CLIENT-BEAT] processing beat: {}", rsInfo.toString());
|
||||
|
||||
String ip = rsInfo.getIp();
|
||||
|
@ -108,10 +108,6 @@ public class HealthCheckCommon {
|
||||
}, 500, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public boolean isHealthCheckEnabled(Service service) {
|
||||
return service.getHealthCheckMode().equals(HealthCheckMode.server.name());
|
||||
}
|
||||
|
||||
public void reEvaluateCheckRT(long checkRT, HealthCheckTask task, SwitchDomain.HealthParams params) {
|
||||
task.setCheckRTLast(checkRT);
|
||||
|
||||
|
@ -1,36 +0,0 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.nacos.naming.healthcheck;
|
||||
|
||||
/**
|
||||
* Health check mode
|
||||
*
|
||||
* @author nkorange
|
||||
*/
|
||||
public enum HealthCheckMode {
|
||||
/**
|
||||
* Health check sent from server.
|
||||
*/
|
||||
server,
|
||||
/**
|
||||
* Health check sent from client.
|
||||
*/
|
||||
client,
|
||||
/**
|
||||
* Health check disabled.
|
||||
*/
|
||||
none
|
||||
}
|
@ -33,6 +33,9 @@ public class HealthCheckProcessorDelegate implements HealthCheckProcessor {
|
||||
@Autowired
|
||||
private MysqlHealthCheckProcessor mysqlProcessor;
|
||||
|
||||
@Autowired
|
||||
private NoneHealthCheckProcessor noneProcessor;
|
||||
|
||||
@Override
|
||||
public void process(HealthCheckTask task) {
|
||||
|
||||
@ -53,7 +56,7 @@ public class HealthCheckProcessorDelegate implements HealthCheckProcessor {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Unknown check type: " + type);
|
||||
noneProcessor.process(task);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -19,7 +19,6 @@ import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.alibaba.nacos.naming.boot.SpringContext;
|
||||
import com.alibaba.nacos.naming.core.Cluster;
|
||||
import com.alibaba.nacos.naming.core.DistroMapper;
|
||||
import com.alibaba.nacos.naming.core.Service;
|
||||
import com.alibaba.nacos.naming.misc.Loggers;
|
||||
import com.alibaba.nacos.naming.misc.SwitchDomain;
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
@ -70,7 +69,8 @@ public class HealthCheckTask implements Runnable {
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
if (distroMapper.responsible(cluster.getDom().getName())) {
|
||||
if (distroMapper.responsible(cluster.getDom().getName()) &&
|
||||
switchDomain.isHealthCheckEnabled(cluster.getDom().getName())) {
|
||||
healthCheckProcessor.process(this);
|
||||
Loggers.EVT_LOG.debug("[HEALTH-CHECK] schedule health check task: {}", cluster.getDom().getName());
|
||||
}
|
||||
@ -92,7 +92,6 @@ public class HealthCheckTask implements Runnable {
|
||||
this.setCheckRTLastLast(this.getCheckRTLast());
|
||||
|
||||
Cluster cluster = this.getCluster();
|
||||
if ((cluster.getDom()).getHealthCheckMode().equals(HealthCheckMode.server.name())) {
|
||||
Loggers.CHECK_RT.info("{}:{}@{}->normalized: {}, worst: {}, best: {}, last: {}, diff: {}",
|
||||
cluster.getDom().getName(), cluster.getName(), cluster.getHealthChecker().getType(),
|
||||
this.getCheckRTNormalized(), this.getCheckRTWorst(), this.getCheckRTBest(),
|
||||
@ -101,7 +100,6 @@ public class HealthCheckTask implements Runnable {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Cluster getCluster() {
|
||||
return cluster;
|
||||
|
@ -30,5 +30,9 @@ public enum HealthCheckType {
|
||||
/**
|
||||
* MySQL type
|
||||
*/
|
||||
MYSQL
|
||||
MYSQL,
|
||||
/**
|
||||
* No check
|
||||
*/
|
||||
NONE
|
||||
}
|
||||
|
@ -90,9 +90,7 @@ public class HttpHealthCheckProcessor implements HealthCheckProcessor {
|
||||
return;
|
||||
}
|
||||
|
||||
Service service = task.getCluster().getDom();
|
||||
|
||||
if (!switchDomain.isHealthCheckEnabled() || !service.getHealthCheckMode().equals(HealthCheckMode.server.name())) {
|
||||
if (!switchDomain.isHealthCheckEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -96,12 +96,6 @@ public class MysqlHealthCheckProcessor implements HealthCheckProcessor {
|
||||
return;
|
||||
}
|
||||
|
||||
Service service = task.getCluster().getDom();
|
||||
|
||||
if (!healthCheckCommon.isHealthCheckEnabled(service)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Instance ip : ips) {
|
||||
try {
|
||||
|
||||
|
@ -0,0 +1,23 @@
|
||||
package com.alibaba.nacos.naming.healthcheck;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Health checker that does nothing
|
||||
*
|
||||
* @author nkorange
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Component
|
||||
public class NoneHealthCheckProcessor implements HealthCheckProcessor {
|
||||
|
||||
@Override
|
||||
public void process(HealthCheckTask task) {
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "NONE";
|
||||
}
|
||||
}
|
@ -113,10 +113,6 @@ public class TcpSuperSenseProcessor implements HealthCheckProcessor, Runnable {
|
||||
}
|
||||
Service service = task.getCluster().getDom();
|
||||
|
||||
if (!healthCheckCommon.isHealthCheckEnabled(service)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Instance ip : ips) {
|
||||
|
||||
if (ip.isMarked()) {
|
||||
|
@ -17,8 +17,6 @@ package com.alibaba.nacos.naming.misc;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.alibaba.nacos.naming.consistency.DataListener;
|
||||
import com.alibaba.nacos.naming.healthcheck.HealthCheckMode;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
@ -52,8 +50,6 @@ public class SwitchDomain implements DataListener<SwitchDomain> {
|
||||
|
||||
public boolean healthCheckEnabled = true;
|
||||
|
||||
public String defaultHealthCheckMode = HealthCheckMode.client.name();
|
||||
|
||||
public boolean distroEnabled = true;
|
||||
|
||||
public boolean enableStandalone = true;
|
||||
@ -259,14 +255,6 @@ public class SwitchDomain implements DataListener<SwitchDomain> {
|
||||
return healthCheckEnabled || getHealthCheckWhiteList().contains(dom);
|
||||
}
|
||||
|
||||
public String getDefaultHealthCheckMode() {
|
||||
return defaultHealthCheckMode;
|
||||
}
|
||||
|
||||
public void setDefaultHealthCheckMode(String defaultHealthCheckMode) {
|
||||
this.defaultHealthCheckMode = defaultHealthCheckMode;
|
||||
}
|
||||
|
||||
public boolean isDistroEnabled() {
|
||||
return distroEnabled;
|
||||
}
|
||||
|
@ -235,17 +235,6 @@ public class SwitchManager {
|
||||
return;
|
||||
}
|
||||
|
||||
if (entry.equals(SwitchEntry.DEFAULT_HEALTH_CHECK_MODE)) {
|
||||
String defaultHealthCheckMode = value;
|
||||
|
||||
switchDomain.setDefaultHealthCheckMode(defaultHealthCheckMode);
|
||||
if (!debug) {
|
||||
consistencyService.put(UtilsAndCommons.getSwitchDomainKey(), JSON.toJSONString(switchDomain));
|
||||
;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (entry.equals(SwitchEntry.DOM_STATUS_SYNC_PERIOD)) {
|
||||
Long millis = Long.parseLong(value);
|
||||
|
||||
|
@ -15,8 +15,6 @@
|
||||
*/
|
||||
package com.alibaba.nacos.naming.core;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.nacos.naming.healthcheck.HealthCheckMode;
|
||||
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
@ -49,7 +47,6 @@ public class DomainTest {
|
||||
|
||||
Service newDomain = new Service();
|
||||
newDomain.setName("nacos.domain.1");
|
||||
newDomain.setHealthCheckMode(HealthCheckMode.client.name());
|
||||
newDomain.setProtectThreshold(0.7f);
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setName(UtilsAndCommons.DEFAULT_CLUSTER_NAME);
|
||||
@ -58,7 +55,6 @@ public class DomainTest {
|
||||
|
||||
domain.update(newDomain);
|
||||
|
||||
Assert.assertEquals(HealthCheckMode.client.name(), domain.getHealthCheckMode());
|
||||
Assert.assertEquals(0.7f, domain.getProtectThreshold(), 0.0001f);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user