Skip to content

Commit

Permalink
[ISSUE #10374] Support listener to get changed instances (#10905)
Browse files Browse the repository at this point in the history
* For #10374, Support listener to get changed instances

* Update AbstractNamingChangeListener,NamingChaneEventTest

* Update InstancesDiffTest

* Rename NamingChangeEventTest
  • Loading branch information
ldyedu authored Aug 7, 2023
1 parent 1f5dbf0 commit c026d2f
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.backups.FailoverReactor;
import com.alibaba.nacos.client.naming.event.InstancesChangeEvent;
import com.alibaba.nacos.client.naming.event.InstancesDiff;
import com.alibaba.nacos.common.lifecycle.Closeable;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.utils.ConvertUtils;
Expand Down Expand Up @@ -160,16 +161,17 @@ public ServiceInfo processServiceInfo(ServiceInfo serviceInfo) {
return oldService;
}
serviceInfoMap.put(serviceInfo.getKey(), serviceInfo);
boolean changed = isChangedServiceInfo(oldService, serviceInfo);
InstancesDiff diff = getServiceInfoDiff(oldService, serviceInfo);
if (StringUtils.isBlank(serviceInfo.getJsonFromServer())) {
serviceInfo.setJsonFromServer(JacksonUtils.toJson(serviceInfo));
}
MetricsMonitor.getServiceInfoMapSizeMonitor().set(serviceInfoMap.size());
if (changed) {
if (diff.hasDifferent()) {
NAMING_LOGGER.info("current ips:({}) service: {} -> {}", serviceInfo.ipCount(), serviceInfo.getKey(),
JacksonUtils.toJson(serviceInfo.getHosts()));

NotifyCenter.publishEvent(new InstancesChangeEvent(notifierEventScope, serviceInfo.getName(), serviceInfo.getGroupName(),
serviceInfo.getClusters(), serviceInfo.getHosts()));
serviceInfo.getClusters(), serviceInfo.getHosts(), diff));
DiskCache.write(serviceInfo, cacheDir);
}
return serviceInfo;
Expand All @@ -179,18 +181,20 @@ private boolean isEmptyOrErrorPush(ServiceInfo serviceInfo) {
return null == serviceInfo.getHosts() || (pushEmptyProtection && !serviceInfo.validate());
}

private boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newService) {
private InstancesDiff getServiceInfoDiff(ServiceInfo oldService, ServiceInfo newService) {
InstancesDiff instancesDiff = new InstancesDiff();
if (null == oldService) {
NAMING_LOGGER.info("init new ips({}) service: {} -> {}", newService.ipCount(), newService.getKey(),
JacksonUtils.toJson(newService.getHosts()));
return true;
instancesDiff.setAddedInstances(newService.getHosts());
return instancesDiff;
}
if (oldService.getLastRefTime() > newService.getLastRefTime()) {
NAMING_LOGGER.warn("out of date data received, old-t: {}, new-t: {}", oldService.getLastRefTime(),
newService.getLastRefTime());
return false;
return instancesDiff;
}
boolean changed = false;

Map<String, Instance> oldHostMap = new HashMap<>(oldService.getHosts().size());
for (Instance host : oldService.getHosts()) {
oldHostMap.put(host.toInetAddr(), host);
Expand Down Expand Up @@ -231,23 +235,23 @@ private boolean isChangedServiceInfo(ServiceInfo oldService, ServiceInfo newServ
}

if (newHosts.size() > 0) {
changed = true;
NAMING_LOGGER.info("new ips({}) service: {} -> {}", newHosts.size(), newService.getKey(),
JacksonUtils.toJson(newHosts));
instancesDiff.setAddedInstances(newHosts);
}

if (remvHosts.size() > 0) {
changed = true;
NAMING_LOGGER.info("removed ips({}) service: {} -> {}", remvHosts.size(), newService.getKey(),
JacksonUtils.toJson(remvHosts));
instancesDiff.setRemovedInstances(remvHosts);
}

if (modHosts.size() > 0) {
changed = true;
NAMING_LOGGER.info("modified ips({}) service: {} -> {}", modHosts.size(), newService.getKey(),
JacksonUtils.toJson(modHosts));
instancesDiff.setModifiedInstances(modHosts);
}
return changed;
return instancesDiff;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ public class InstancesChangeEvent extends Event {
private final String clusters;

private final List<Instance> hosts;

private InstancesDiff instancesDiff;

public InstancesChangeEvent(String eventScope, String serviceName, String groupName, String clusters, List<Instance> hosts) {
public InstancesChangeEvent(String eventScope, String serviceName, String groupName, String clusters, List<Instance> hosts, InstancesDiff diff) {
this.eventScope = eventScope;
this.serviceName = serviceName;
this.groupName = groupName;
this.clusters = clusters;
this.hosts = hosts;
this.instancesDiff = diff;
}

public String getServiceName() {
Expand All @@ -64,7 +67,11 @@ public String getClusters() {
public List<Instance> getHosts() {
return hosts;
}


public InstancesDiff getInstancesDiff() {
return instancesDiff;
}

@Override
public String scope() {
return this.eventScope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import com.alibaba.nacos.api.naming.listener.AbstractEventListener;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.client.naming.listener.NamingChangeEvent;
import com.alibaba.nacos.common.JustForTest;
import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.listener.Subscriber;
Expand Down Expand Up @@ -130,8 +130,8 @@ public void onEvent(InstancesChangeEvent event) {

private com.alibaba.nacos.api.naming.listener.Event transferToNamingEvent(
InstancesChangeEvent instancesChangeEvent) {
return new NamingEvent(instancesChangeEvent.getServiceName(), instancesChangeEvent.getGroupName(),
instancesChangeEvent.getClusters(), instancesChangeEvent.getHosts());
return new NamingChangeEvent(instancesChangeEvent.getServiceName(), instancesChangeEvent.getGroupName(),
instancesChangeEvent.getClusters(), instancesChangeEvent.getHosts(), instancesChangeEvent.getInstancesDiff());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 1999-2023 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.client.naming.event;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.common.utils.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* The differences in instances compared to the last callback.
*
* @author lideyou
*/
public class InstancesDiff {
private final List<Instance> addedInstances = new ArrayList<>();

private final List<Instance> removedInstances = new ArrayList<>();

private final List<Instance> modifiedInstances = new ArrayList<>();

public List<Instance> getAddedInstances() {
return addedInstances;
}

public void setAddedInstances(Collection<Instance> addedInstances) {
this.addedInstances.clear();
if (CollectionUtils.isNotEmpty(addedInstances)) {
this.addedInstances.addAll(addedInstances);
}
}

public List<Instance> getRemovedInstances() {
return removedInstances;
}

public void setRemovedInstances(Collection<Instance> removedInstances) {
this.removedInstances.clear();
if (CollectionUtils.isNotEmpty(removedInstances)) {
this.removedInstances.addAll(removedInstances);
}
}

public List<Instance> getModifiedInstances() {
return modifiedInstances;
}

public void setModifiedInstances(Collection<Instance> modifiedInstances) {
this.modifiedInstances.clear();
if (CollectionUtils.isNotEmpty(modifiedInstances)) {
this.modifiedInstances.addAll(modifiedInstances);
}
}

/**
* Check if any instances have changed.
* @return true if there are instances that have changed
*/
public boolean hasDifferent() {
return CollectionUtils.isNotEmpty(this.addedInstances)
|| CollectionUtils.isNotEmpty(this.removedInstances)
|| CollectionUtils.isNotEmpty(this.modifiedInstances);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 1999-2023 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.client.naming.listener;

import com.alibaba.nacos.api.naming.listener.AbstractEventListener;
import com.alibaba.nacos.api.naming.listener.Event;

/**
* Listener for NamingChangeEvent.
*
* @author lideyou
*/
public abstract class AbstractNamingChangeListener extends AbstractEventListener {
@Override
public final void onEvent(Event event) {
if (event instanceof NamingChangeEvent) {
onChange((NamingChangeEvent) event);
}
}

/**
* Callback when instances have changed.
* @param event NamingChangeEvent
*/
public abstract void onChange(NamingChangeEvent event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 1999-2023 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.client.naming.listener;

import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.naming.event.InstancesDiff;
import com.alibaba.nacos.common.utils.CollectionUtils;

import java.util.List;

/**
* Naming Event with instance change information.
*
* @author lideyou
*/
public class NamingChangeEvent extends NamingEvent {
private final InstancesDiff instancesDiff;

public NamingChangeEvent(String serviceName, List<Instance> instances, InstancesDiff instancesDiff) {
super(serviceName, instances);
this.instancesDiff = instancesDiff;
}

public NamingChangeEvent(String serviceName, String groupName, String clusters, List<Instance> instances, InstancesDiff instancesDiff) {
super(serviceName, groupName, clusters, instances);
this.instancesDiff = instancesDiff;
}

public boolean isAdded() {
return CollectionUtils.isNotEmpty(this.instancesDiff.getAddedInstances());
}

public boolean isRemoved() {
return CollectionUtils.isNotEmpty(this.instancesDiff.getRemovedInstances());
}

public boolean isModified() {
return CollectionUtils.isNotEmpty(this.instancesDiff.getModifiedInstances());
}

public List<Instance> getAddedInstances() {
return this.instancesDiff.getAddedInstances();
}

public List<Instance> getRemovedInstances() {
return this.instancesDiff.getRemovedInstances();
}

public List<Instance> getModifiedInstances() {
return this.instancesDiff.getModifiedInstances();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,21 @@ public void testGetServiceName() {
List<Instance> hosts = new ArrayList<>();
Instance ins = new Instance();
hosts.add(ins);
InstancesChangeEvent event = new InstancesChangeEvent(eventScope, serviceName, groupName, clusters, hosts);
InstancesDiff diff = new InstancesDiff();
diff.setAddedInstances(hosts);
InstancesChangeEvent event = new InstancesChangeEvent(eventScope, serviceName, groupName, clusters, hosts, diff);
Assert.assertEquals(eventScope, event.scope());
Assert.assertEquals(serviceName, event.getServiceName());
Assert.assertEquals(clusters, event.getClusters());
Assert.assertEquals(groupName, event.getGroupName());
List<Instance> hosts1 = event.getHosts();
Assert.assertEquals(hosts.size(), hosts1.size());
Assert.assertEquals(hosts.get(0), hosts1.get(0));
InstancesDiff diff1 = event.getInstancesDiff();
Assert.assertTrue(diff1.hasDifferent());
Assert.assertEquals(diff.getAddedInstances().size(), diff1.getAddedInstances().size());
Assert.assertEquals(diff.getAddedInstances().get(0), diff.getAddedInstances().get(0));
Assert.assertEquals(diff.getRemovedInstances().size(), diff1.getRemovedInstances().size());
Assert.assertEquals(diff.getModifiedInstances().size(), diff1.getModifiedInstances().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public void testRegisterListener() {
List<Instance> hosts = new ArrayList<>();
Instance ins = new Instance();
hosts.add(ins);
InstancesChangeEvent event = new InstancesChangeEvent(eventScope, name, group, clusters, hosts);
InstancesDiff diff = new InstancesDiff();
diff.setAddedInstances(hosts);
InstancesChangeEvent event = new InstancesChangeEvent(eventScope, name, group, clusters, hosts, diff);
Assert.assertEquals(true, instancesChangeNotifier.scopeMatches(event));
}

Expand Down
Loading

0 comments on commit c026d2f

Please sign in to comment.