From c69667d0b0a681fd53121b3a306124875d46e5d8 Mon Sep 17 00:00:00 2001 From: pader Date: Sun, 29 Nov 2020 22:23:30 +0800 Subject: [PATCH 1/6] Add the function of automatically clearing empty services --- .../nacos/naming/core/v2/ServiceManager.java | 5 ++ .../v2/cleaner/EmptyServiceAutoCleanerV2.java | 48 +++++++++++++++++++ .../core/v2/event/service/ServiceEvent.java | 13 +++++ .../v2/index/ClientServiceIndexesManager.java | 20 +++++--- .../core/v2/metadata/ExpiredMetadataInfo.java | 12 +++-- .../v2/metadata/NamingMetadataManager.java | 12 +++++ 6 files changed, 99 insertions(+), 11 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/ServiceManager.java index 84ac7355e6f..11d1d2e8953 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/ServiceManager.java @@ -88,6 +88,10 @@ public Optional getSingletonIfExist(Service service) { return Optional.ofNullable(singletonRepository.get(service)); } + public Set getAllNamespaces() { + return namespaceSingletonMaps.keySet(); + } + /** * Remove singleton service. * @@ -95,6 +99,7 @@ public Optional getSingletonIfExist(Service service) { * @return removed service */ public Service removeSingleton(Service service) { + namespaceSingletonMaps.get(service.getNamespace()).remove(service); return singletonRepository.remove(service); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java index a024558abe8..471bc1a3fb1 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java @@ -16,6 +16,19 @@ package com.alibaba.nacos.naming.core.v2.cleaner; +import com.alibaba.nacos.common.notify.NotifyCenter; +import com.alibaba.nacos.naming.core.v2.ServiceManager; +import com.alibaba.nacos.naming.core.v2.event.service.ServiceEvent; +import com.alibaba.nacos.naming.core.v2.index.ClientServiceIndexesManager; +import com.alibaba.nacos.naming.core.v2.pojo.Service; +import com.alibaba.nacos.naming.misc.GlobalExecutor; +import com.alibaba.nacos.naming.misc.Loggers; + +import java.util.Collection; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; + /** * Empty service auto cleaner for v2.x. * @@ -25,6 +38,16 @@ public class EmptyServiceAutoCleanerV2 extends AbstractNamingCleaner { private static final String EMPTY_SERVICE = "emptyService"; + private final ClientServiceIndexesManager clientServiceIndexesManager; + + public EmptyServiceAutoCleanerV2(ClientServiceIndexesManager clientServiceIndexesManager) { + this.clientServiceIndexesManager = clientServiceIndexesManager; + // TODO get internal from config + GlobalExecutor.scheduleExpiredClientCleaner(this, TimeUnit.SECONDS.toMillis(30), TimeUnit.SECONDS.toMillis(20), + TimeUnit.MILLISECONDS); + + } + @Override public String getType() { return EMPTY_SERVICE; @@ -32,6 +55,31 @@ public String getType() { @Override public void doClean() { + ServiceManager serviceManager = ServiceManager.getInstance(); + // Parallel flow opening threshold + int parallelSize = 100; + + for (String each : serviceManager.getAllNamespaces()) { + Set services = serviceManager.getSingletons(each); + Stream stream = services.size() > parallelSize ? services.parallelStream() : services.stream(); + stream.forEach(this::cleanEmptyService); + } + } + + private void cleanEmptyService(Service service) { + Loggers.SRV_LOG.warn("namespace : {}, [{}] services are automatically cleaned", service.getNamespace(), + service.getGroupedServiceName()); + Collection registeredService = clientServiceIndexesManager.getAllClientsRegisteredService(service); + if (isTimeExpired(service) && registeredService.isEmpty()) { + clientServiceIndexesManager.removePublisherIndexes(service); + ServiceManager.getInstance().removeSingleton(service); + NotifyCenter.publishEvent(new ServiceEvent.ServiceRemovedEvent(service)); + } + } + private boolean isTimeExpired(Service service) { + long currentTimeMillis = System.currentTimeMillis(); + // TODO get expired time from config + return currentTimeMillis - service.getLastUpdatedTime() >= TimeUnit.MINUTES.toMillis(1); } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/service/ServiceEvent.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/service/ServiceEvent.java index 419a0832ead..913e3987bd2 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/service/ServiceEvent.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/service/ServiceEvent.java @@ -56,4 +56,17 @@ public ServiceChangedEvent(Service service, boolean incrementRevision) { } } } + + /** + * Service data changed event. + */ + public static class ServiceRemovedEvent extends ServiceEvent { + + private static final long serialVersionUID = 2123694271992630822L; + + + public ServiceRemovedEvent(Service service) { + super(service); + } + } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ClientServiceIndexesManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ClientServiceIndexesManager.java index 73e0e9b5740..ec6d5e7cf1b 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ClientServiceIndexesManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ClientServiceIndexesManager.java @@ -58,6 +58,14 @@ public Collection getAllClientsSubscribeService(Service service) { return subscriberIndexes.containsKey(service) ? subscriberIndexes.get(service) : new ConcurrentHashSet<>(); } + public void removePublisherIndexes(Service service) { + if (publisherIndexes.get(service).isEmpty()) { + + publisherIndexes.remove(service); + } + } + + @Override public List> subscribeTypes() { List> result = new LinkedList<>(); @@ -66,6 +74,7 @@ public List> subscribeTypes() { result.add(ClientOperationEvent.ClientSubscribeServiceEvent.class); result.add(ClientOperationEvent.ClientUnsubscribeServiceEvent.class); result.add(ClientEvent.ClientDisconnectEvent.class); + result.add(ServiceEvent.ServiceRemovedEvent.class); return result; } @@ -73,11 +82,12 @@ public List> subscribeTypes() { public void onEvent(Event event) { if (event instanceof ClientEvent.ClientDisconnectEvent) { handleClientDisconnect((ClientEvent.ClientDisconnectEvent) event); - } else { + } else if (event instanceof ClientOperationEvent) { handleClientOperation((ClientOperationEvent) event); } } + private void handleClientDisconnect(ClientEvent.ClientDisconnectEvent event) { Client client = event.getClient(); for (Service each : client.getAllSubscribeService()) { @@ -103,9 +113,7 @@ private void handleClientOperation(ClientOperationEvent event) { } private void addPublisherIndexes(Service service, String clientId) { - if (!publisherIndexes.containsKey(service)) { - publisherIndexes.putIfAbsent(service, new ConcurrentHashSet<>()); - } + publisherIndexes.computeIfAbsent(service, (key) -> new ConcurrentHashSet<>()); publisherIndexes.get(service).add(clientId); NotifyCenter.publishEvent(new ServiceEvent.ServiceChangedEvent(service, true)); } @@ -122,9 +130,7 @@ private void removePublisherIndexes(Service service, String clientId) { } private void addSubscriberIndexes(Service service, String clientId) { - if (!subscriberIndexes.containsKey(service)) { - subscriberIndexes.putIfAbsent(service, new ConcurrentHashSet<>()); - } + subscriberIndexes.computeIfAbsent(service, (key) -> new ConcurrentHashSet<>()); subscriberIndexes.get(service).add(clientId); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/ExpiredMetadataInfo.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/ExpiredMetadataInfo.java index 6fd833ade00..6c7ffac604c 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/ExpiredMetadataInfo.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/ExpiredMetadataInfo.java @@ -37,18 +37,22 @@ public class ExpiredMetadataInfo { private final long createTime; - private ExpiredMetadataInfo(Service service, String instanceId) { + private ExpiredMetadataInfo(Service service, String instanceId, Long createTime) { this.service = service; this.instanceId = instanceId; - createTime = System.currentTimeMillis(); + this.createTime = createTime; + } + + public static ExpiredMetadataInfo newExpiredServiceMetadata(Service service, Long createTime) { + return new ExpiredMetadataInfo(service, null, createTime); } public static ExpiredMetadataInfo newExpiredServiceMetadata(Service service) { - return new ExpiredMetadataInfo(service, null); + return new ExpiredMetadataInfo(service, null, System.currentTimeMillis()); } public static ExpiredMetadataInfo newExpiredInstanceMetadata(Service service, String instanceId) { - return new ExpiredMetadataInfo(service, instanceId); + return new ExpiredMetadataInfo(service, instanceId, System.currentTimeMillis()); } public Service getService() { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java index f52775b93af..d3612d0a33a 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java @@ -22,6 +22,7 @@ import com.alibaba.nacos.common.utils.ConcurrentHashSet; import com.alibaba.nacos.naming.core.v2.event.client.ClientEvent; import com.alibaba.nacos.naming.core.v2.event.metadata.MetadataEvent; +import com.alibaba.nacos.naming.core.v2.event.service.ServiceEvent; import com.alibaba.nacos.naming.core.v2.pojo.Service; import org.springframework.stereotype.Component; @@ -157,6 +158,7 @@ public List> subscribeTypes() { result.add(MetadataEvent.InstanceMetadataEvent.class); result.add(MetadataEvent.ServiceMetadataEvent.class); result.add(ClientEvent.ClientDisconnectEvent.class); + result.add(ServiceEvent.ServiceRemovedEvent.class); return result; } @@ -166,11 +168,21 @@ public void onEvent(Event event) { handleInstanceMetadataEvent((MetadataEvent.InstanceMetadataEvent) event); } else if (event instanceof MetadataEvent.ServiceMetadataEvent) { handleServiceMetadataEvent((MetadataEvent.ServiceMetadataEvent) event); + } else if (event instanceof ServiceEvent.ServiceRemovedEvent) { + handleServiceRemovedEvent((ServiceEvent.ServiceRemovedEvent) event); } else { handleClientDisconnectEvent((ClientEvent.ClientDisconnectEvent) event); } } + private void handleServiceRemovedEvent(ServiceEvent.ServiceRemovedEvent event) { + Service service = event.getService(); + if (!containServiceMetadata(service)) { + return; + } + updateExpiredInfo(true, ExpiredMetadataInfo.newExpiredServiceMetadata(service, Long.MAX_VALUE)); + } + private void handleClientDisconnectEvent(ClientEvent.ClientDisconnectEvent event) { for (Service each : event.getClient().getAllPublishedService()) { String instanceId = event.getClient().getInstancePublishInfo(each).getIp(); From ac9b04bfa58118e08fb060afd7a5008dc674a9ab Mon Sep 17 00:00:00 2001 From: pader Date: Sun, 29 Nov 2020 23:03:31 +0800 Subject: [PATCH 2/6] Fix checkstyle issue --- .../core/v2/cleaner/EmptyServiceAutoCleanerV2.java | 2 +- .../naming/core/v2/event/service/ServiceEvent.java | 1 - .../core/v2/index/ClientServiceIndexesManager.java | 10 +++++----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java index 471bc1a3fb1..61761485caf 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java @@ -71,7 +71,7 @@ private void cleanEmptyService(Service service) { service.getGroupedServiceName()); Collection registeredService = clientServiceIndexesManager.getAllClientsRegisteredService(service); if (isTimeExpired(service) && registeredService.isEmpty()) { - clientServiceIndexesManager.removePublisherIndexes(service); + clientServiceIndexesManager.removePublisherIndexesByEmptyService(service); ServiceManager.getInstance().removeSingleton(service); NotifyCenter.publishEvent(new ServiceEvent.ServiceRemovedEvent(service)); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/service/ServiceEvent.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/service/ServiceEvent.java index 913e3987bd2..7d7e6f806c4 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/service/ServiceEvent.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/service/ServiceEvent.java @@ -64,7 +64,6 @@ public static class ServiceRemovedEvent extends ServiceEvent { private static final long serialVersionUID = 2123694271992630822L; - public ServiceRemovedEvent(Service service) { super(service); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ClientServiceIndexesManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ClientServiceIndexesManager.java index ec6d5e7cf1b..45993263c8b 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ClientServiceIndexesManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/index/ClientServiceIndexesManager.java @@ -58,14 +58,16 @@ public Collection getAllClientsSubscribeService(Service service) { return subscriberIndexes.containsKey(service) ? subscriberIndexes.get(service) : new ConcurrentHashSet<>(); } - public void removePublisherIndexes(Service service) { + /** + * Clear the service index without instances. + * @param service The service of the Nacos. + */ + public void removePublisherIndexesByEmptyService(Service service) { if (publisherIndexes.get(service).isEmpty()) { - publisherIndexes.remove(service); } } - @Override public List> subscribeTypes() { List> result = new LinkedList<>(); @@ -74,7 +76,6 @@ public List> subscribeTypes() { result.add(ClientOperationEvent.ClientSubscribeServiceEvent.class); result.add(ClientOperationEvent.ClientUnsubscribeServiceEvent.class); result.add(ClientEvent.ClientDisconnectEvent.class); - result.add(ServiceEvent.ServiceRemovedEvent.class); return result; } @@ -87,7 +88,6 @@ public void onEvent(Event event) { } } - private void handleClientDisconnect(ClientEvent.ClientDisconnectEvent event) { Client client = event.getClient(); for (Service each : client.getAllSubscribeService()) { From a5a389ae2e8d65da7dffd16e2ee818569f892dc8 Mon Sep 17 00:00:00 2001 From: pader Date: Mon, 30 Nov 2020 14:48:04 +0800 Subject: [PATCH 3/6] Delete the remove event of the service --- .../core/v2/cleaner/EmptyServiceAutoCleanerV2.java | 4 ++-- .../naming/core/v2/event/service/ServiceEvent.java | 11 ----------- .../core/v2/metadata/ExpiredMetadataInfo.java | 12 ++++-------- .../core/v2/metadata/NamingMetadataManager.java | 14 +------------- 4 files changed, 7 insertions(+), 34 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java index 61761485caf..b67359b8dab 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java @@ -18,7 +18,7 @@ import com.alibaba.nacos.common.notify.NotifyCenter; import com.alibaba.nacos.naming.core.v2.ServiceManager; -import com.alibaba.nacos.naming.core.v2.event.service.ServiceEvent; +import com.alibaba.nacos.naming.core.v2.event.metadata.MetadataEvent; import com.alibaba.nacos.naming.core.v2.index.ClientServiceIndexesManager; import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.misc.GlobalExecutor; @@ -73,7 +73,7 @@ private void cleanEmptyService(Service service) { if (isTimeExpired(service) && registeredService.isEmpty()) { clientServiceIndexesManager.removePublisherIndexesByEmptyService(service); ServiceManager.getInstance().removeSingleton(service); - NotifyCenter.publishEvent(new ServiceEvent.ServiceRemovedEvent(service)); + NotifyCenter.publishEvent(new MetadataEvent.ServiceMetadataEvent(service, true)); } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/service/ServiceEvent.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/service/ServiceEvent.java index 7d7e6f806c4..1323f9b4891 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/service/ServiceEvent.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/event/service/ServiceEvent.java @@ -57,15 +57,4 @@ public ServiceChangedEvent(Service service, boolean incrementRevision) { } } - /** - * Service data changed event. - */ - public static class ServiceRemovedEvent extends ServiceEvent { - - private static final long serialVersionUID = 2123694271992630822L; - - public ServiceRemovedEvent(Service service) { - super(service); - } - } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/ExpiredMetadataInfo.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/ExpiredMetadataInfo.java index 6c7ffac604c..9e274851739 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/ExpiredMetadataInfo.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/ExpiredMetadataInfo.java @@ -37,22 +37,18 @@ public class ExpiredMetadataInfo { private final long createTime; - private ExpiredMetadataInfo(Service service, String instanceId, Long createTime) { + private ExpiredMetadataInfo(Service service, String instanceId) { this.service = service; this.instanceId = instanceId; - this.createTime = createTime; - } - - public static ExpiredMetadataInfo newExpiredServiceMetadata(Service service, Long createTime) { - return new ExpiredMetadataInfo(service, null, createTime); + this.createTime = System.currentTimeMillis(); } public static ExpiredMetadataInfo newExpiredServiceMetadata(Service service) { - return new ExpiredMetadataInfo(service, null, System.currentTimeMillis()); + return new ExpiredMetadataInfo(service, null); } public static ExpiredMetadataInfo newExpiredInstanceMetadata(Service service, String instanceId) { - return new ExpiredMetadataInfo(service, instanceId, System.currentTimeMillis()); + return new ExpiredMetadataInfo(service, instanceId); } public Service getService() { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java index d3612d0a33a..39a2bcadf41 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java @@ -22,7 +22,6 @@ import com.alibaba.nacos.common.utils.ConcurrentHashSet; import com.alibaba.nacos.naming.core.v2.event.client.ClientEvent; import com.alibaba.nacos.naming.core.v2.event.metadata.MetadataEvent; -import com.alibaba.nacos.naming.core.v2.event.service.ServiceEvent; import com.alibaba.nacos.naming.core.v2.pojo.Service; import org.springframework.stereotype.Component; @@ -158,7 +157,6 @@ public List> subscribeTypes() { result.add(MetadataEvent.InstanceMetadataEvent.class); result.add(MetadataEvent.ServiceMetadataEvent.class); result.add(ClientEvent.ClientDisconnectEvent.class); - result.add(ServiceEvent.ServiceRemovedEvent.class); return result; } @@ -168,21 +166,11 @@ public void onEvent(Event event) { handleInstanceMetadataEvent((MetadataEvent.InstanceMetadataEvent) event); } else if (event instanceof MetadataEvent.ServiceMetadataEvent) { handleServiceMetadataEvent((MetadataEvent.ServiceMetadataEvent) event); - } else if (event instanceof ServiceEvent.ServiceRemovedEvent) { - handleServiceRemovedEvent((ServiceEvent.ServiceRemovedEvent) event); - } else { + } else { handleClientDisconnectEvent((ClientEvent.ClientDisconnectEvent) event); } } - private void handleServiceRemovedEvent(ServiceEvent.ServiceRemovedEvent event) { - Service service = event.getService(); - if (!containServiceMetadata(service)) { - return; - } - updateExpiredInfo(true, ExpiredMetadataInfo.newExpiredServiceMetadata(service, Long.MAX_VALUE)); - } - private void handleClientDisconnectEvent(ClientEvent.ClientDisconnectEvent event) { for (Service each : event.getClient().getAllPublishedService()) { String instanceId = event.getClient().getInstancePublishInfo(each).getIp(); From 4508982b07b3581349d1addde55b080676a91b9a Mon Sep 17 00:00:00 2001 From: pader Date: Mon, 30 Nov 2020 15:18:21 +0800 Subject: [PATCH 4/6] Fix checkstyle issue --- .../nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java | 2 +- .../nacos/naming/core/v2/metadata/NamingMetadataManager.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java index b67359b8dab..b7859af3f81 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java @@ -70,7 +70,7 @@ private void cleanEmptyService(Service service) { Loggers.SRV_LOG.warn("namespace : {}, [{}] services are automatically cleaned", service.getNamespace(), service.getGroupedServiceName()); Collection registeredService = clientServiceIndexesManager.getAllClientsRegisteredService(service); - if (isTimeExpired(service) && registeredService.isEmpty()) { + if (registeredService.isEmpty() && isTimeExpired(service)) { clientServiceIndexesManager.removePublisherIndexesByEmptyService(service); ServiceManager.getInstance().removeSingleton(service); NotifyCenter.publishEvent(new MetadataEvent.ServiceMetadataEvent(service, true)); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java index 39a2bcadf41..f52775b93af 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/metadata/NamingMetadataManager.java @@ -166,7 +166,7 @@ public void onEvent(Event event) { handleInstanceMetadataEvent((MetadataEvent.InstanceMetadataEvent) event); } else if (event instanceof MetadataEvent.ServiceMetadataEvent) { handleServiceMetadataEvent((MetadataEvent.ServiceMetadataEvent) event); - } else { + } else { handleClientDisconnectEvent((ClientEvent.ClientDisconnectEvent) event); } } From 12a6686ce4723d2ac81a39bc73b815bc4d565017 Mon Sep 17 00:00:00 2001 From: pader Date: Mon, 30 Nov 2020 22:00:08 +0800 Subject: [PATCH 5/6] Add some task configurations for cleaning --- .../v2/cleaner/EmptyServiceAutoCleanerV2.java | 9 ++-- .../v2/cleaner/ExpiredMetadataCleaner.java | 8 ++-- .../com/alibaba/nacos/sys/env/Constants.java | 16 ++++++++ .../com/alibaba/nacos/sys/env/EnvUtil.java | 41 +++++++++++++++++++ 4 files changed, 65 insertions(+), 9 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java index b7859af3f81..fd47df3bd9f 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java @@ -23,6 +23,7 @@ import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.misc.GlobalExecutor; import com.alibaba.nacos.naming.misc.Loggers; +import com.alibaba.nacos.sys.env.EnvUtil; import java.util.Collection; import java.util.Set; @@ -42,9 +43,8 @@ public class EmptyServiceAutoCleanerV2 extends AbstractNamingCleaner { public EmptyServiceAutoCleanerV2(ClientServiceIndexesManager clientServiceIndexesManager) { this.clientServiceIndexesManager = clientServiceIndexesManager; - // TODO get internal from config - GlobalExecutor.scheduleExpiredClientCleaner(this, TimeUnit.SECONDS.toMillis(30), TimeUnit.SECONDS.toMillis(20), - TimeUnit.MILLISECONDS); + GlobalExecutor.scheduleExpiredClientCleaner(this, TimeUnit.SECONDS.toMillis(30), + EnvUtil.getEmptyServiceCleanInterval(), TimeUnit.MILLISECONDS); } @@ -79,7 +79,6 @@ private void cleanEmptyService(Service service) { private boolean isTimeExpired(Service service) { long currentTimeMillis = System.currentTimeMillis(); - // TODO get expired time from config - return currentTimeMillis - service.getLastUpdatedTime() >= TimeUnit.MINUTES.toMillis(1); + return currentTimeMillis - service.getLastUpdatedTime() >= EnvUtil.getEmptyServiceExpiredTime(); } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/ExpiredMetadataCleaner.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/ExpiredMetadataCleaner.java index c6b436848b1..cdd9b37cc9e 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/ExpiredMetadataCleaner.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/ExpiredMetadataCleaner.java @@ -21,6 +21,7 @@ import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService; import com.alibaba.nacos.naming.misc.GlobalExecutor; import com.alibaba.nacos.naming.misc.Loggers; +import com.alibaba.nacos.sys.env.EnvUtil; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; @@ -43,8 +44,8 @@ public ExpiredMetadataCleaner(NamingMetadataManager metadataManager, NamingMetadataOperateService metadataOperateService) { this.metadataManager = metadataManager; this.metadataOperateService = metadataOperateService; - // TODO get internal from config - GlobalExecutor.scheduleExpiredClientCleaner(this, 5000, 5000, TimeUnit.MILLISECONDS); + GlobalExecutor.scheduleExpiredClientCleaner(this, 5000, EnvUtil.getExpiredMetadataCleanInterval(), + TimeUnit.MILLISECONDS); } @Override @@ -56,8 +57,7 @@ public String getType() { public void doClean() { long currentTime = System.currentTimeMillis(); for (ExpiredMetadataInfo each : metadataManager.getExpiredMetadataInfos()) { - // TODO get expired time from config - if (currentTime - each.getCreateTime() > TimeUnit.MINUTES.toMillis(1)) { + if (currentTime - each.getCreateTime() > EnvUtil.getExpiredMetadataExpiredTime()) { removeExpiredMetadata(each); } } diff --git a/sys/src/main/java/com/alibaba/nacos/sys/env/Constants.java b/sys/src/main/java/com/alibaba/nacos/sys/env/Constants.java index 20addac2c70..e294ee25eeb 100644 --- a/sys/src/main/java/com/alibaba/nacos/sys/env/Constants.java +++ b/sys/src/main/java/com/alibaba/nacos/sys/env/Constants.java @@ -48,7 +48,23 @@ public interface Constants { * the root context path. */ String ROOT_WEB_CONTEXT_PATH = "/"; + /** + * Time interval to clear empty services, unit: millisecond. default: 60000 ms. + */ + String EMPTY_SERVICE_CLEAN_INTERVAL = "empty.service.clean.interval"; + /** + * Expiration time of empty service, unit: millisecond. default: 60000 ms. + */ + String EMPTY_SERVICE_EXPIRED_TIME = "empty.service.expired.time"; + /** + * Time interval to clear expired metadata, unit: millisecond. default: 5000 ms. + */ + String EXPIRED_METADATA_CLEAN_INTERVAL = "expired.metadata.clean.interval"; + /** + * Expiration time of expired metadata, unit: millisecond. default: 60000 ms. + */ + String EXPIRED_METADATA_EXPIRED_TIME = "expired.metadata.expired.time"; String NACOS_SERVER_IP = "nacos.server.ip"; String USE_ONLY_SITE_INTERFACES = "nacos.inetutils.use-only-site-local-interfaces"; diff --git a/sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java b/sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java index 8fcb97b4171..57e553bb711 100644 --- a/sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java +++ b/sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java @@ -42,6 +42,11 @@ import java.util.List; import java.util.Map; +import static com.alibaba.nacos.sys.env.Constants.EMPTY_SERVICE_CLEAN_INTERVAL; +import static com.alibaba.nacos.sys.env.Constants.EMPTY_SERVICE_EXPIRED_TIME; +import static com.alibaba.nacos.sys.env.Constants.EXPIRED_METADATA_CLEAN_INTERVAL; +import static com.alibaba.nacos.sys.env.Constants.EXPIRED_METADATA_EXPIRED_TIME; + /** * Its own configuration information manipulation tool class. * @@ -72,6 +77,14 @@ public class EnvUtil { private static String contextPath = null; + private static Long emptyServiceCleanInterval; + + private static Long emptyServiceExpiredTime; + + private static Long expiredMetadataCleanInterval; + + private static Long expiredMetadataExpiredTime; + @JustForTest private static String confPath = ""; @@ -123,6 +136,34 @@ public static String resolveRequiredPlaceholders(String text) throws IllegalArgu return environment.resolveRequiredPlaceholders(text); } + public static Long getEmptyServiceCleanInterval() { + if (Objects.isNull(emptyServiceCleanInterval)) { + emptyServiceCleanInterval = getProperty(EMPTY_SERVICE_CLEAN_INTERVAL, Long.class, 60000L); + } + return emptyServiceCleanInterval; + } + + public static Long getEmptyServiceExpiredTime() { + if (Objects.isNull(emptyServiceExpiredTime)) { + emptyServiceExpiredTime = getProperty(EMPTY_SERVICE_EXPIRED_TIME, Long.class, 60000L); + } + return emptyServiceExpiredTime; + } + + public static Long getExpiredMetadataCleanInterval() { + if (Objects.isNull(expiredMetadataCleanInterval)) { + expiredMetadataCleanInterval = getProperty(EXPIRED_METADATA_CLEAN_INTERVAL, Long.class, 5000L); + } + return expiredMetadataCleanInterval; + } + + public static Long getExpiredMetadataExpiredTime() { + if (Objects.isNull(expiredMetadataExpiredTime)) { + expiredMetadataExpiredTime = getProperty(EXPIRED_METADATA_EXPIRED_TIME, Long.class, 60000L); + } + return expiredMetadataExpiredTime; + } + public static List getPropertyList(String key) { List valueList = new ArrayList<>(); From 99fdf8a7cb33f2d075db69d64ad1878614ebe915 Mon Sep 17 00:00:00 2001 From: pader Date: Tue, 1 Dec 2020 11:20:59 +0800 Subject: [PATCH 6/6] Adjust the configuration to the Naming module --- .../v2/cleaner/EmptyServiceAutoCleanerV2.java | 6 +-- .../v2/cleaner/ExpiredMetadataCleaner.java | 6 +-- .../nacos/naming/misc/GlobalConfig.java | 25 ++++++++++- .../alibaba/nacos/naming/utils/Constants.java | 19 +++++++++ .../com/alibaba/nacos/sys/env/Constants.java | 16 -------- .../com/alibaba/nacos/sys/env/EnvUtil.java | 41 ------------------- 6 files changed, 49 insertions(+), 64 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java index fd47df3bd9f..2fb61158af2 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/EmptyServiceAutoCleanerV2.java @@ -21,9 +21,9 @@ import com.alibaba.nacos.naming.core.v2.event.metadata.MetadataEvent; import com.alibaba.nacos.naming.core.v2.index.ClientServiceIndexesManager; import com.alibaba.nacos.naming.core.v2.pojo.Service; +import com.alibaba.nacos.naming.misc.GlobalConfig; import com.alibaba.nacos.naming.misc.GlobalExecutor; import com.alibaba.nacos.naming.misc.Loggers; -import com.alibaba.nacos.sys.env.EnvUtil; import java.util.Collection; import java.util.Set; @@ -44,7 +44,7 @@ public class EmptyServiceAutoCleanerV2 extends AbstractNamingCleaner { public EmptyServiceAutoCleanerV2(ClientServiceIndexesManager clientServiceIndexesManager) { this.clientServiceIndexesManager = clientServiceIndexesManager; GlobalExecutor.scheduleExpiredClientCleaner(this, TimeUnit.SECONDS.toMillis(30), - EnvUtil.getEmptyServiceCleanInterval(), TimeUnit.MILLISECONDS); + GlobalConfig.getEmptyServiceCleanInterval(), TimeUnit.MILLISECONDS); } @@ -79,6 +79,6 @@ private void cleanEmptyService(Service service) { private boolean isTimeExpired(Service service) { long currentTimeMillis = System.currentTimeMillis(); - return currentTimeMillis - service.getLastUpdatedTime() >= EnvUtil.getEmptyServiceExpiredTime(); + return currentTimeMillis - service.getLastUpdatedTime() >= GlobalConfig.getEmptyServiceExpiredTime(); } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/ExpiredMetadataCleaner.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/ExpiredMetadataCleaner.java index cdd9b37cc9e..36c3514cb2c 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/ExpiredMetadataCleaner.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/cleaner/ExpiredMetadataCleaner.java @@ -19,9 +19,9 @@ import com.alibaba.nacos.naming.core.v2.metadata.ExpiredMetadataInfo; import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager; import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService; +import com.alibaba.nacos.naming.misc.GlobalConfig; import com.alibaba.nacos.naming.misc.GlobalExecutor; import com.alibaba.nacos.naming.misc.Loggers; -import com.alibaba.nacos.sys.env.EnvUtil; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; @@ -44,7 +44,7 @@ public ExpiredMetadataCleaner(NamingMetadataManager metadataManager, NamingMetadataOperateService metadataOperateService) { this.metadataManager = metadataManager; this.metadataOperateService = metadataOperateService; - GlobalExecutor.scheduleExpiredClientCleaner(this, 5000, EnvUtil.getExpiredMetadataCleanInterval(), + GlobalExecutor.scheduleExpiredClientCleaner(this, 5000, GlobalConfig.getExpiredMetadataCleanInterval(), TimeUnit.MILLISECONDS); } @@ -57,7 +57,7 @@ public String getType() { public void doClean() { long currentTime = System.currentTimeMillis(); for (ExpiredMetadataInfo each : metadataManager.getExpiredMetadataInfos()) { - if (currentTime - each.getCreateTime() > EnvUtil.getExpiredMetadataExpiredTime()) { + if (currentTime - each.getCreateTime() > GlobalConfig.getExpiredMetadataExpiredTime()) { removeExpiredMetadata(each); } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/misc/GlobalConfig.java b/naming/src/main/java/com/alibaba/nacos/naming/misc/GlobalConfig.java index ef70b4b8d72..6886f4d134d 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/misc/GlobalConfig.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/misc/GlobalConfig.java @@ -17,11 +17,17 @@ package com.alibaba.nacos.naming.misc; import com.alibaba.nacos.core.distributed.distro.DistroConfig; +import com.alibaba.nacos.sys.env.EnvUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; +import static com.alibaba.nacos.naming.utils.Constants.EMPTY_SERVICE_CLEAN_INTERVAL; +import static com.alibaba.nacos.naming.utils.Constants.EMPTY_SERVICE_EXPIRED_TIME; +import static com.alibaba.nacos.naming.utils.Constants.EXPIRED_METADATA_CLEAN_INTERVAL; +import static com.alibaba.nacos.naming.utils.Constants.EXPIRED_METADATA_EXPIRED_TIME; + /** * Stores some configurations for Distro protocol. * @@ -48,7 +54,7 @@ public class GlobalConfig { @Value("${nacos.naming.expireInstance:true}") private boolean expireInstance = true; - @Value("${nacos.naming.distro.loadDataRetryDelayMillis:30000}") + @Value("${nacos.naming.clean.loadDataRetryDelayMillis:60000L}") private long loadDataRetryDelayMillis = 30000; public GlobalConfig(DistroConfig distroConfig) { @@ -91,6 +97,23 @@ public long getLoadDataRetryDelayMillis() { return loadDataRetryDelayMillis; } + public static Long getEmptyServiceCleanInterval() { + return EnvUtil.getProperty(EMPTY_SERVICE_CLEAN_INTERVAL, Long.class, 60000L); + + } + + public static Long getEmptyServiceExpiredTime() { + return EnvUtil.getProperty(EMPTY_SERVICE_EXPIRED_TIME, Long.class, 60000L); + } + + public static Long getExpiredMetadataCleanInterval() { + return EnvUtil.getProperty(EXPIRED_METADATA_CLEAN_INTERVAL, Long.class, 5000L); + } + + public static Long getExpiredMetadataExpiredTime() { + return EnvUtil.getProperty(EXPIRED_METADATA_EXPIRED_TIME, Long.class, 60000L); + } + @Override public String toString() { return "GlobalConfig{" + "taskDispatchPeriod=" + taskDispatchPeriod + ", batchSyncKeyCount=" + batchSyncKeyCount diff --git a/naming/src/main/java/com/alibaba/nacos/naming/utils/Constants.java b/naming/src/main/java/com/alibaba/nacos/naming/utils/Constants.java index 6e5b90b3c7b..c28940a7c59 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/utils/Constants.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/utils/Constants.java @@ -32,4 +32,23 @@ public final class Constants { public static final String NACOS_NAMING_USE_NEW_RAFT_FIRST = "nacos.naming.use-new-raft.first"; + /** + * Time interval to clear empty services, unit: millisecond. default: 60000 ms. + */ + public static final String EMPTY_SERVICE_CLEAN_INTERVAL = "nacos.naming.clean.empty-service.interval"; + + /** + * Expiration time of empty service, unit: millisecond. default: 60000 ms. + */ + public static final String EMPTY_SERVICE_EXPIRED_TIME = "nacos.naming.clean.empty-service.expired-time"; + + /** + * Time interval to clear expired metadata, unit: millisecond. default: 5000 ms. + */ + public static final String EXPIRED_METADATA_CLEAN_INTERVAL = "nacos.naming.clean.expired-metadata.interval"; + + /** + * Expiration time of expired metadata, unit: millisecond. default: 60000 ms. + */ + public static final String EXPIRED_METADATA_EXPIRED_TIME = "nacos.naming.clean.expired-metadata.expired-time"; } diff --git a/sys/src/main/java/com/alibaba/nacos/sys/env/Constants.java b/sys/src/main/java/com/alibaba/nacos/sys/env/Constants.java index e294ee25eeb..20addac2c70 100644 --- a/sys/src/main/java/com/alibaba/nacos/sys/env/Constants.java +++ b/sys/src/main/java/com/alibaba/nacos/sys/env/Constants.java @@ -48,23 +48,7 @@ public interface Constants { * the root context path. */ String ROOT_WEB_CONTEXT_PATH = "/"; - /** - * Time interval to clear empty services, unit: millisecond. default: 60000 ms. - */ - String EMPTY_SERVICE_CLEAN_INTERVAL = "empty.service.clean.interval"; - /** - * Expiration time of empty service, unit: millisecond. default: 60000 ms. - */ - String EMPTY_SERVICE_EXPIRED_TIME = "empty.service.expired.time"; - /** - * Time interval to clear expired metadata, unit: millisecond. default: 5000 ms. - */ - String EXPIRED_METADATA_CLEAN_INTERVAL = "expired.metadata.clean.interval"; - /** - * Expiration time of expired metadata, unit: millisecond. default: 60000 ms. - */ - String EXPIRED_METADATA_EXPIRED_TIME = "expired.metadata.expired.time"; String NACOS_SERVER_IP = "nacos.server.ip"; String USE_ONLY_SITE_INTERFACES = "nacos.inetutils.use-only-site-local-interfaces"; diff --git a/sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java b/sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java index 57e553bb711..8fcb97b4171 100644 --- a/sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java +++ b/sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java @@ -42,11 +42,6 @@ import java.util.List; import java.util.Map; -import static com.alibaba.nacos.sys.env.Constants.EMPTY_SERVICE_CLEAN_INTERVAL; -import static com.alibaba.nacos.sys.env.Constants.EMPTY_SERVICE_EXPIRED_TIME; -import static com.alibaba.nacos.sys.env.Constants.EXPIRED_METADATA_CLEAN_INTERVAL; -import static com.alibaba.nacos.sys.env.Constants.EXPIRED_METADATA_EXPIRED_TIME; - /** * Its own configuration information manipulation tool class. * @@ -77,14 +72,6 @@ public class EnvUtil { private static String contextPath = null; - private static Long emptyServiceCleanInterval; - - private static Long emptyServiceExpiredTime; - - private static Long expiredMetadataCleanInterval; - - private static Long expiredMetadataExpiredTime; - @JustForTest private static String confPath = ""; @@ -136,34 +123,6 @@ public static String resolveRequiredPlaceholders(String text) throws IllegalArgu return environment.resolveRequiredPlaceholders(text); } - public static Long getEmptyServiceCleanInterval() { - if (Objects.isNull(emptyServiceCleanInterval)) { - emptyServiceCleanInterval = getProperty(EMPTY_SERVICE_CLEAN_INTERVAL, Long.class, 60000L); - } - return emptyServiceCleanInterval; - } - - public static Long getEmptyServiceExpiredTime() { - if (Objects.isNull(emptyServiceExpiredTime)) { - emptyServiceExpiredTime = getProperty(EMPTY_SERVICE_EXPIRED_TIME, Long.class, 60000L); - } - return emptyServiceExpiredTime; - } - - public static Long getExpiredMetadataCleanInterval() { - if (Objects.isNull(expiredMetadataCleanInterval)) { - expiredMetadataCleanInterval = getProperty(EXPIRED_METADATA_CLEAN_INTERVAL, Long.class, 5000L); - } - return expiredMetadataCleanInterval; - } - - public static Long getExpiredMetadataExpiredTime() { - if (Objects.isNull(expiredMetadataExpiredTime)) { - expiredMetadataExpiredTime = getProperty(EXPIRED_METADATA_EXPIRED_TIME, Long.class, 60000L); - } - return expiredMetadataExpiredTime; - } - public static List getPropertyList(String key) { List valueList = new ArrayList<>();