use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.
the class DoubleWriteServiceRemovalToV2Task method run.
@Override
public void run() {
try {
InstanceOperatorClientImpl instanceOperator = ApplicationUtils.getBean(InstanceOperatorClientImpl.class);
ClientServiceIndexesManager clientServiceIndexesManager = ApplicationUtils.getBean(ClientServiceIndexesManager.class);
ServiceStorage serviceStorage = ApplicationUtils.getBean(ServiceStorage.class);
ServiceInfo serviceInfo = serviceStorage.getPushData(service);
for (Instance instance : serviceInfo.getHosts()) {
instanceOperator.removeInstance(service.getNamespace(), service.getName(), instance);
}
int count = 0;
while (!clientServiceIndexesManager.getAllClientsRegisteredService(service).isEmpty() && count < MAX_WAIT_TIMES) {
TimeUnit.MILLISECONDS.sleep(100);
count += 1;
}
clientServiceIndexesManager.removePublisherIndexesByEmptyService(service);
ServiceManager.getInstance().removeSingleton(service);
serviceStorage.removeData(service);
NotifyCenter.publishEvent(new MetadataEvent.ServiceMetadataEvent(service, true));
} catch (Exception e) {
if (Loggers.SRV_LOG.isDebugEnabled()) {
Loggers.SRV_LOG.debug("Double write removal of {} from 1 to 2 failed", service, e);
}
ServiceChangeV1Task retryTask = new ServiceChangeV1Task(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral(), DoubleWriteContent.BOTH, DoubleWriteAction.REMOVE);
retryTask.setTaskInterval(INTERVAL);
String taskKey = ServiceChangeV1Task.getKey(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral());
ApplicationUtils.getBean(DoubleWriteDelayTaskEngine.class).addTask(taskKey, retryTask);
}
}
use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.
the class HealthCheckCommonV2 method checkFailNow.
/**
* Health check fail, set instance unhealthy directly.
*
* @param task health check task
* @param service service
* @param msg message
*/
public void checkFailNow(HealthCheckTaskV2 task, Service service, String msg) {
try {
HealthCheckInstancePublishInfo instance = (HealthCheckInstancePublishInfo) task.getClient().getInstancePublishInfo(service);
if (null == instance) {
return;
}
try {
if (instance.isHealthy()) {
String serviceName = service.getGroupedServiceName();
String clusterName = instance.getCluster();
if (switchDomain.isHealthCheckEnabled(serviceName) && !task.isCancelled() && distroMapper.responsible(task.getClient().getResponsibleId())) {
healthStatusSynchronizer.instanceHealthStatusChange(false, task.getClient(), service, instance);
Loggers.EVT_LOG.info("serviceName: {} {POS} {IP-DISABLED} invalid: {}:{}@{}, region: {}, msg: {}", serviceName, instance.getIp(), instance.getPort(), clusterName, UtilsAndCommons.LOCALHOST_SITE, msg);
}
}
} finally {
instance.resetOkCount();
instance.finishCheck();
}
} catch (Throwable t) {
Loggers.SRV_LOG.error("[CHECK-FAIL] error when close check task.", t);
}
}
use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.
the class HealthCheckCommonV2 method checkFail.
/**
* Health check fail, when instance check failed count more than max failed time, set unhealthy.
*
* @param task health check task
* @param service service
* @param msg message
*/
public void checkFail(HealthCheckTaskV2 task, Service service, String msg) {
try {
HealthCheckInstancePublishInfo instance = (HealthCheckInstancePublishInfo) task.getClient().getInstancePublishInfo(service);
if (instance == null) {
return;
}
try {
if (instance.isHealthy()) {
String serviceName = service.getGroupedServiceName();
String clusterName = instance.getCluster();
if (instance.getFailCount().incrementAndGet() >= switchDomain.getCheckTimes()) {
if (switchDomain.isHealthCheckEnabled(serviceName) && !task.isCancelled() && distroMapper.responsible(task.getClient().getResponsibleId())) {
healthStatusSynchronizer.instanceHealthStatusChange(false, task.getClient(), service, instance);
Loggers.EVT_LOG.info("serviceName: {} {POS} {IP-DISABLED} invalid: {}:{}@{}, region: {}, msg: {}", serviceName, instance.getIp(), instance.getPort(), clusterName, UtilsAndCommons.LOCALHOST_SITE, msg);
}
} else {
Loggers.EVT_LOG.info("serviceName: {} {OTHER} {IP-DISABLED} pre-invalid: {}:{}@{} in {}, msg: {}", serviceName, instance.getIp(), instance.getPort(), clusterName, instance.getFailCount(), msg);
}
}
} finally {
instance.resetOkCount();
instance.finishCheck();
}
} catch (Throwable t) {
Loggers.SRV_LOG.error("[CHECK-FAIL] error when close check task.", t);
}
}
use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.
the class HealthCheckCommonV2 method checkOk.
/**
* Health check pass.
*
* @param task health check task
* @param service service
* @param msg message
*/
public void checkOk(HealthCheckTaskV2 task, Service service, String msg) {
try {
HealthCheckInstancePublishInfo instance = (HealthCheckInstancePublishInfo) task.getClient().getInstancePublishInfo(service);
if (instance == null) {
return;
}
try {
if (!instance.isHealthy()) {
String serviceName = service.getGroupedServiceName();
String clusterName = instance.getCluster();
if (instance.getOkCount().incrementAndGet() >= switchDomain.getCheckTimes()) {
if (switchDomain.isHealthCheckEnabled(serviceName) && !task.isCancelled() && distroMapper.responsible(task.getClient().getResponsibleId())) {
healthStatusSynchronizer.instanceHealthStatusChange(true, task.getClient(), service, instance);
Loggers.EVT_LOG.info("serviceName: {} {POS} {IP-ENABLED} valid: {}:{}@{}, region: {}, msg: {}", serviceName, instance.getIp(), instance.getPort(), clusterName, UtilsAndCommons.LOCALHOST_SITE, msg);
}
} else {
Loggers.EVT_LOG.info("serviceName: {} {OTHER} {IP-ENABLED} pre-valid: {}:{}@{} in {}, msg: {}", serviceName, instance.getIp(), instance.getPort(), clusterName, instance.getOkCount(), msg);
}
}
} finally {
instance.resetFailCount();
instance.finishCheck();
}
} catch (Throwable t) {
Loggers.SRV_LOG.error("[CHECK-OK] error when close check task.", t);
}
}
use of com.alibaba.nacos.naming.core.v2.pojo.Service in project nacos by alibaba.
the class ServiceStorageTest method testRemoveData.
@Test
public void testRemoveData() throws NoSuchFieldException, IllegalAccessException {
serviceStorage.removeData(SERVICE);
Field serviceClusterIndex = ServiceStorage.class.getDeclaredField("serviceClusterIndex");
serviceClusterIndex.setAccessible(true);
ConcurrentMap<Service, Set<String>> serviceSetConcurrentMap = (ConcurrentMap<Service, Set<String>>) serviceClusterIndex.get(serviceStorage);
Field serviceDataIndexes = ServiceStorage.class.getDeclaredField("serviceDataIndexes");
serviceDataIndexes.setAccessible(true);
ConcurrentMap<Service, ServiceInfo> infoConcurrentMap = (ConcurrentMap<Service, ServiceInfo>) serviceDataIndexes.get(serviceStorage);
Assert.assertEquals(serviceSetConcurrentMap.size(), 0);
Assert.assertEquals(infoConcurrentMap.size(), 0);
}
Aggregations