Skip to content

Commit

Permalink
[ISSUE #10380] Support for fuzzy watch capability for Nacos registrat…
Browse files Browse the repository at this point in the history
…ion center. (#11200)

* add nacos fuzzy watch in client side

* add fuzzy watch request handler, client operation and watch event

* add fuzzy watch index manager support, add service change event in nacos naming module

* add nacos server side notify fuzzy watch logic

* add nacos naming fuzzy watch example

* fix format

* Optimizing Abstract Classes

* alter class name and fix bug

* remove redundant file import by repeat merge

* fix bug
  • Loading branch information
Chionanthus authored Oct 20, 2023
1 parent cafdb91 commit 4be6f3e
Show file tree
Hide file tree
Showing 58 changed files with 1,665 additions and 333 deletions.
10 changes: 5 additions & 5 deletions api/src/main/java/com/alibaba/nacos/api/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,20 @@ public static class Naming {
}

/**
* The constants in Watch Pattern Match Rule directory.
* The constants in fuzzy watch pattern match rule directory.
*/
public static class WatchMatchRule {
public static class FuzzyWatchMatchRule {

public static final String MATCH_ALL = "MATCH_ALL";

public static final String MATCH_PREFIX = "MATCH_PREFIX";

}

/**
* The constants in Watch Notify Event directory.
* The constants in fuzzy watch event type directory.
*/
public static class WatchEventType {
public static class ServiceChangedType {

public static final String ADD_SERVICE = "ADD_SERVICE";

Expand Down
32 changes: 16 additions & 16 deletions api/src/main/java/com/alibaba/nacos/api/naming/NamingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.alibaba.nacos.api.naming;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.listener.AbstractWatchEventListener;
import com.alibaba.nacos.api.naming.listener.AbstractFuzzyWatchEventListener;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
Expand Down Expand Up @@ -534,44 +534,44 @@ void unsubscribe(String serviceName, String groupName, List<String> clusters, Ev
throws NacosException;

/**
* Watch a range of services by match rule to receive notify events of matched services alteration.
* Watch a range of services by rule to receive notify events of matched services alteration.
*
* @param fixedGroupName fixed group name for watch
* @param fixedGroupName fixed group name for fuzzy watch
* @param listener event listener
* @throws NacosException nacos exception
*/
void fuzzyWatch(String fixedGroupName, AbstractWatchEventListener listener) throws NacosException;
void fuzzyWatch(String fixedGroupName, AbstractFuzzyWatchEventListener listener) throws NacosException;

/**
* Watch a range of services by match rule to receive notify events of matched services alteration.
* Watch a range of services by rule to receive notify events of matched services alteration.
*
* @param serviceNamePattern service name pattern for watch
* @param fixedGroupName fixed group name for watch
* @param serviceNamePattern service name pattern for fuzzy watch
* @param fixedGroupName fixed group name for fuzzy watch
* @param listener event listener
* @throws NacosException nacos exception
*/
void fuzzyWatch(String serviceNamePattern, String fixedGroupName,
AbstractWatchEventListener listener) throws NacosException;
AbstractFuzzyWatchEventListener listener) throws NacosException;

/**
* Cancel watch event listener of a pattern.
* Cancel fuzzy watch, and remove event listener of a pattern.
*
* @param fixedGroupName fixed group name for watch
* @param fixedGroupName fixed group name for fuzzy watch
* @param listener event listener
* @throws NacosException nacos exception
*/
void cancelFuzzyWatch(String fixedGroupName, AbstractWatchEventListener listener) throws NacosException;
void cancelFuzzyWatch(String fixedGroupName, AbstractFuzzyWatchEventListener listener) throws NacosException;

/**
* Cancel watch event listener of a pattern.
* Cancel fuzzy watch, and remove event listener of a pattern.
*
* @param serviceNamePattern service name pattern for watch
* @param fixedGroupName fixed group name for watch
* @param serviceNamePattern service name pattern for fuzzy watch
* @param fixedGroupName fixed group name for fuzzy watch
* @param listener event listener
* @throws NacosException nacos exception
*/
void cancelFuzzyWatch(String serviceNamePattern, String fixedGroupName, AbstractWatchEventListener listener) throws NacosException;
void cancelFuzzyWatch(String serviceNamePattern, String fixedGroupName, AbstractFuzzyWatchEventListener listener) throws NacosException;

/**
* Get all service names from server.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import java.util.concurrent.Executor;

/**
* Abstract watch event listener, to support handle event by user custom executor.
* Abstract fuzzy watch event listener, to support handle event by user custom executor.
*
* @author tanyongquan
*/
public abstract class AbstractWatchEventListener implements WatchListener {
public abstract class AbstractFuzzyWatchEventListener implements FuzzyWatchListener {

String uuid;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
package com.alibaba.nacos.api.naming.listener;

/**
* Watch Listener.
* Fuzzy Watch Listener.
*
* @author tanyongquan
*/
public interface WatchListener {
public interface FuzzyWatchListener {

/**
* callback event.
*
* @param event event
*/
void onEvent(WatchNotifyEvent event);
void onEvent(FuzzyWatchNotifyEvent event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
import com.alibaba.nacos.api.naming.pojo.Service;

/**
* Watch Notify Event.
* Fuzzy Watch Notify Event.
*
* @author tanyongquan
*/
public class WatchNotifyEvent implements Event {
public class FuzzyWatchNotifyEvent implements Event {

private Service service;

private String changeType;

public WatchNotifyEvent() {
public FuzzyWatchNotifyEvent() {
}

public WatchNotifyEvent(Service service, String changeType) {
public FuzzyWatchNotifyEvent(Service service, String changeType) {
this.service = service;
this.changeType = changeType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.api.naming.pojo;

import com.alibaba.nacos.api.naming.utils.NamingUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -101,6 +103,10 @@ public void setGroupName(String groupName) {
this.groupName = groupName;
}

public String getGroupedServiceName() {
return NamingUtils.getGroupedName(name, groupName);
}

public Map<String, String> getMetadata() {
return metadata;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public class NamingRemoteConstants {

public static final String DE_REGISTER_INSTANCE = "deregisterInstance";

public static final String WATCH_SERVICE = "watchService";
public static final String FUZZY_WATCH_SERVICE = "fuzzyWatchService";

public static final String CANCEL_WATCH_SERVICE = "cancelWatchService";
public static final String CANCEL_FUZZY_WATCH_SERVICE = "cancelFuzzyWatchService";

public static final String QUERY_SERVICE = "queryService";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,20 @@
import static com.alibaba.nacos.api.common.Constants.Naming.NAMING_MODULE;

/**
* Abstract watch notify request, including basic watch notify information.
* Abstract fuzzy watch notify request, including basic fuzzy watch notify information.
*
* @author tanyongquan
*/
public abstract class AbstractWatchNotifyRequest extends ServerRequest {
public abstract class AbstractFuzzyWatchNotifyRequest extends ServerRequest {
private String namespace;

private String pattern;

private String serviceChangedType;

public AbstractWatchNotifyRequest(){
public AbstractFuzzyWatchNotifyRequest(){
}

public AbstractWatchNotifyRequest(String namespace, String pattern, String serviceChangedType) {
public AbstractFuzzyWatchNotifyRequest(String namespace, String serviceChangedType) {
this.namespace = namespace;
this.pattern = pattern;
this.serviceChangedType = serviceChangedType;
}

Expand All @@ -57,14 +54,6 @@ public void setServiceChangedType(String serviceChangedType) {
this.serviceChangedType = serviceChangedType;
}

public String getPattern() {
return pattern;
}

public void setPattern(String pattern) {
this.pattern = pattern;
}

@Override
public String getModule() {
return NAMING_MODULE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
package com.alibaba.nacos.api.naming.remote.request;

/**
* Nacos watch notify service change request, use it when one of the services changes.
* Nacos fuzzy watch notify service change request, use it when one of the services changes.
*
* @author tanyongquan
*/
public class WatchNotifyChangeRequest extends AbstractWatchNotifyRequest {
public class FuzzyWatchNotifyChangeRequest extends AbstractFuzzyWatchNotifyRequest {

String serviceName;

String groupName;

public WatchNotifyChangeRequest() {
public FuzzyWatchNotifyChangeRequest() {
}

public WatchNotifyChangeRequest(String namespace, String serviceName,
public FuzzyWatchNotifyChangeRequest(String namespace, String serviceName,
String groupName, String serviceChangedType) {
super(namespace, "", serviceChangedType);
super(namespace, serviceChangedType);
this.serviceName = serviceName;
this.groupName = groupName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,39 @@
import java.util.HashSet;

/**
* Nacos watch initial notify request, use it when init a watch request, push service by batch.
* Nacos fuzzy watch initial notify request, use it when init a watch request, push service by batch.
*
* @author tanyongquan
*/
public class WatchNotifyInitRequest extends AbstractWatchNotifyRequest {
public class FuzzyWatchNotifyInitRequest extends AbstractFuzzyWatchNotifyRequest {

private String pattern;

private Collection<String> servicesName;

public WatchNotifyInitRequest() {
public FuzzyWatchNotifyInitRequest() {
}

private WatchNotifyInitRequest(String namespace, String pattern, String serviceChangedType, Collection<String> servicesName) {
super(namespace, pattern, serviceChangedType);
private FuzzyWatchNotifyInitRequest(String namespace, String pattern, String serviceChangedType, Collection<String> servicesName) {
super(namespace, serviceChangedType);
this.servicesName = servicesName;
this.pattern = pattern;
}

public static FuzzyWatchNotifyInitRequest buildInitRequest(String namespace, String pattern, Collection<String> servicesName) {
return new FuzzyWatchNotifyInitRequest(namespace, pattern, Constants.ServiceChangedType.WATCH_INITIAL_MATCH, servicesName);
}

public static FuzzyWatchNotifyInitRequest buildInitFinishRequest(String namespace, String pattern) {
return new FuzzyWatchNotifyInitRequest(namespace, pattern, Constants.ServiceChangedType.FINISH_WATCH_INIT, new HashSet<>(1));
}

public static WatchNotifyInitRequest buildInitRequest(String namespace, String pattern, Collection<String> servicesName) {
return new WatchNotifyInitRequest(namespace, pattern, Constants.WatchEventType.WATCH_INITIAL_MATCH, servicesName);
public String getPattern() {
return pattern;
}

public static WatchNotifyInitRequest buildInitFinishRequest(String namespace, String pattern) {
return new WatchNotifyInitRequest(namespace, pattern, Constants.WatchEventType.FINISH_WATCH_INIT, new HashSet<>(1));
public void setPattern(String pattern) {
this.pattern = pattern;
}

public Collection<String> getServicesName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
package com.alibaba.nacos.api.naming.remote.request;

/**
* Nacos naming watch service request.
* Nacos naming fuzzy watch service request.
*
* @author tanyongquan
*/
public class WatchServiceRequest extends AbstractNamingRequest {
public class FuzzyWatchRequest extends AbstractNamingRequest {

private String type;

public WatchServiceRequest() {
public FuzzyWatchRequest() {
}

public WatchServiceRequest(String namespace, String serviceNamePattern, String groupNamePattern, String type) {
public FuzzyWatchRequest(String namespace, String serviceNamePattern, String groupNamePattern, String type) {
super(namespace, serviceNamePattern, groupNamePattern);
this.type = type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@
import com.alibaba.nacos.api.remote.response.ResponseCode;

/**
* Nacos naming watch service response.
* Nacos naming fuzzy watch service response.
*
* @author tanyongquan
*/
public class WatchServiceResponse extends Response {
public class FuzzyWatchResponse extends Response {

private String type;

public WatchServiceResponse(){
public FuzzyWatchResponse(){
}

public WatchServiceResponse(String type) {
public FuzzyWatchResponse(String type) {
this.type = type;
}

public static WatchServiceResponse buildSuccessResponse(String type) {
return new WatchServiceResponse(type);
public static FuzzyWatchResponse buildSuccessResponse(String type) {
return new FuzzyWatchResponse(type);
}

/**
Expand All @@ -45,8 +45,8 @@ public static WatchServiceResponse buildSuccessResponse(String type) {
* @param message error message
* @return fail response
*/
public static WatchServiceResponse buildFailResponse(String message) {
WatchServiceResponse result = new WatchServiceResponse();
public static FuzzyWatchResponse buildFailResponse(String message) {
FuzzyWatchResponse result = new FuzzyWatchResponse();
result.setErrorInfo(ResponseCode.FAIL.getCode(), message);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import com.alibaba.nacos.api.remote.response.Response;

/**
* Response for notify watcher.
* Response for notify fuzzy watcher.
*
* @author tanyongquan
*/
public class NotifyWatcherResponse extends Response {
public class NotifyFuzzyWatcherResponse extends Response {

}
Loading

0 comments on commit 4be6f3e

Please sign in to comment.