Skip to content

Commit

Permalink
Config fuzzy watch support (#11856)
Browse files Browse the repository at this point in the history
Nacos config supports fuzzy watch. by [email protected]
  • Loading branch information
stone-98 authored Dec 27, 2024
1 parent 8478fd7 commit 17bd04e
Show file tree
Hide file tree
Showing 32 changed files with 4,270 additions and 69 deletions.
16 changes: 16 additions & 0 deletions api/src/main/java/com/alibaba/nacos/api/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public class Constants {

public static final Integer CLUSTER_GRPC_PORT_DEFAULT_OFFSET = 1001;

public static final String NAMESPACE_ID_SPLITTER = ">>";

public static final String DATA_ID_SPLITTER = "@@";

/**
* second.
*/
Expand Down Expand Up @@ -208,6 +212,8 @@ public class Constants {

public static final String ALL_PATTERN = "*";

public static final String FUZZY_LISTEN_PATTERN_WILDCARD = "*";

public static final String COLON = ":";

public static final String LINE_BREAK = "\n";
Expand All @@ -231,6 +237,16 @@ public class Constants {

public static final int DEFAULT_REDO_THREAD_COUNT = 1;

public static class ConfigChangeType {

public static final String ADD_CONFIG = "ADD_CONFIG";

public static final String DELETE_CONFIG = "DELETE_CONFIG";

public static final String FINISH_LISTEN_INIT = "FINISH_LISTEN_INIT";

public static final String LISTEN_INIT = "LISTEN_INIT";
}
public static final String APP_CONN_LABELS_KEY = "nacos.app.conn.labels";

public static final String DOT = ".";
Expand Down
86 changes: 81 additions & 5 deletions api/src/main/java/com/alibaba/nacos/api/config/ConfigService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
package com.alibaba.nacos.api.config;

import com.alibaba.nacos.api.config.filter.IConfigFilter;
import com.alibaba.nacos.api.config.listener.AbstractFuzzyListenListener;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;

/**
* Config Service Interface.
*
Expand Down Expand Up @@ -59,8 +63,8 @@ String getConfigAndSignListener(String dataId, String group, long timeoutMs, Lis
/**
* Add a listener to the configuration, after the server modified the configuration, the client will use the
* incoming listener callback. Recommended asynchronous processing, the application can implement the getExecutor
* method in the ManagerListener, provide a thread pool of execution. If not provided, use the main thread callback, May
* block other configurations or be blocked by other configurations.
* method in the ManagerListener, provide a thread pool of execution. If not provided, use the main thread callback,
* May block other configurations or be blocked by other configurations.
*
* @param dataId dataId
* @param group group
Expand All @@ -69,6 +73,78 @@ String getConfigAndSignListener(String dataId, String group, long timeoutMs, Lis
*/
void addListener(String dataId, String group, Listener listener) throws NacosException;

/**
* Add a fuzzy listener to the configuration. After the server modifies the configuration matching the specified
* fixed group name, the client will utilize the incoming fuzzy listener callback. Fuzzy listeners allow for
* pattern-based subscription to configurations, where the fixed group name represents the group and dataId patterns
* specified for subscription.
*
* @param fixedGroupName The fixed group name representing the group and dataId patterns to subscribe to.
* @param listener The fuzzy listener to be added.
* @throws NacosException NacosException
*/
void addFuzzyListener(String fixedGroupName, AbstractFuzzyListenListener listener) throws NacosException;

/**
* Add a fuzzy listener to the configuration. After the server modifies the configuration matching the specified
* dataId pattern and fixed group name, the client will utilize the incoming fuzzy listener callback. Fuzzy
* listeners allow for pattern-based subscription to configurations.
*
* @param dataIdPattern The pattern to match dataIds for subscription.
* @param fixedGroupName The fixed group name representing the group and dataId patterns to subscribe to.
* @param listener The fuzzy listener to be added.
* @throws NacosException NacosException
*/
void addFuzzyListener(String dataIdPattern, String fixedGroupName, AbstractFuzzyListenListener listener)
throws NacosException;

/**
* Add a fuzzy listener to the configuration and retrieve all configs that match the specified fixed group name.
* Fuzzy listeners allow for pattern-based subscription to configs, where the fixed group name represents the group
* and dataId patterns specified for subscription.
*
* @param fixedGroupName The fixed group name representing the group and dataId patterns to subscribe to.
* @param listener The fuzzy listener to be added.
* @return CompletableFuture containing collection of configs that match the specified fixed group name.
* @throws NacosException NacosException
*/
CompletableFuture<Collection<String>> addFuzzyListenerAndGetConfigs(String fixedGroupName,
AbstractFuzzyListenListener listener) throws NacosException;

/**
* Add a fuzzy listener to the configuration and retrieve all configs that match the specified dataId pattern and
* fixed group name. Fuzzy listeners allow for pattern-based subscription to configs.
*
* @param dataIdPattern The pattern to match dataIds for subscription.
* @param fixedGroupName The fixed group name representing the group and dataId patterns to subscribe to.
* @param listener The fuzzy listener to be added.
* @return CompletableFuture containing collection of configs that match the specified dataId pattern and fixed
* group name.
* @throws NacosException NacosException
*/
CompletableFuture<Collection<String>> addFuzzyListenerAndGetConfigs(String dataIdPattern, String fixedGroupName,
AbstractFuzzyListenListener listener) throws NacosException;

/**
* Cancel fuzzy listen and remove the event listener for a specified fixed group name.
*
* @param fixedGroupName The fixed group name for fuzzy watch.
* @param listener The event listener to be removed.
* @throws NacosException If an error occurs during the cancellation process.
*/
void cancelFuzzyListen(String fixedGroupName, AbstractFuzzyListenListener listener) throws NacosException;

/**
* Cancel fuzzy listen and remove the event listener for a specified service name pattern and fixed group name.
*
* @param dataIdPatter The pattern to match dataId for fuzzy watch.
* @param fixedGroupName The fixed group name for fuzzy watch.
* @param listener The event listener to be removed.
* @throws NacosException If an error occurs during the cancellation process.
*/
void cancelFuzzyListen(String dataIdPatter, String fixedGroupName, AbstractFuzzyListenListener listener)
throws NacosException;

/**
* Publish config.
*
Expand Down Expand Up @@ -144,10 +220,10 @@ boolean publishConfigCas(String dataId, String group, String content, String cas
* @return whether health
*/
String getServerStatus();

/**
* add config filter.
* It is recommended to use {@link com.alibaba.nacos.api.config.filter.AbstractConfigFilter} to expand the filter.
* add config filter. It is recommended to use {@link com.alibaba.nacos.api.config.filter.AbstractConfigFilter} to
* expand the filter.
*
* @param configFilter filter
* @since 2.3.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.api.config.listener;

import java.util.Objects;

/**
* AbstractFuzzyListenListener is an abstract class that provides basic functionality for listening to fuzzy
* configuration changes in Nacos.
*
* @author stone-98
* @date 2024/3/4
*/
public abstract class AbstractFuzzyListenListener extends AbstractListener {

/**
* Unique identifier for the listener.
*/
private String uuid;

/**
* Get the UUID (Unique Identifier) of the listener.
*
* @return The UUID of the listener
*/
public String getUuid() {
return uuid;
}

/**
* Set the UUID (Unique Identifier) of the listener.
*
* @param uuid The UUID to be set
*/
public void setUuid(String uuid) {
this.uuid = uuid;
}

/**
* Callback method invoked when a fuzzy configuration change event occurs.
*
* @param event The fuzzy configuration change event
*/
public abstract void onEvent(FuzzyListenConfigChangeEvent event);

/**
* Receive the configuration information. This method is overridden but does nothing in this abstract class.
*
* @param configInfo The configuration information
*/
@Override
public void receiveConfigInfo(String configInfo) {
// Do nothing by default
}

/**
* Compute the hash code for this listener based on its UUID.
*
* @return The hash code value for this listener
*/
@Override
public int hashCode() {
return Objects.hashCode(uuid);
}

/**
* Compare this listener to the specified object for equality. Two listeners are considered equal if they have the
* same UUID.
*
* @param o The object to compare to
* @return true if the specified object is equal to this listener, false otherwise
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AbstractFuzzyListenListener that = (AbstractFuzzyListenListener) o;
return Objects.equals(uuid, that.uuid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.api.config.listener;

/**
* Represents a fuzzy listening configuration change event.
*
* <p>This event indicates that a change has occurred in a configuration that matches a fuzzy listening pattern.
*
* @author stone-98
* @date 2024/3/12
*/
public class FuzzyListenConfigChangeEvent {

/**
* The group of the configuration that has changed.
*/
private String group;

/**
* The data ID of the configuration that has changed.
*/
private String dataId;

/**
* The type of change that has occurred (e.g., "ADD_CONFIG", "DELETE_CONFIG").
*/
private String type;

/**
* Constructs an empty FuzzyListenConfigChangeEvent.
*/
public FuzzyListenConfigChangeEvent() {
}

/**
* Constructs a FuzzyListenConfigChangeEvent with the specified parameters.
*
* @param group The group of the configuration that has changed
* @param dataId The data ID of the configuration that has changed
* @param type The type of change that has occurred
*/
public FuzzyListenConfigChangeEvent(String group, String dataId, String type) {
this.group = group;
this.dataId = dataId;
this.type = type;
}

/**
* Constructs and returns a new FuzzyListenConfigChangeEvent with the specified parameters.
*
* @param group The group of the configuration that has changed
* @param dataId The data ID of the configuration that has changed
* @param type The type of change that has occurred
* @return A new FuzzyListenConfigChangeEvent instance
*/
public static FuzzyListenConfigChangeEvent build(String group, String dataId, String type) {
return new FuzzyListenConfigChangeEvent(group, dataId, type);
}

public String getGroup() {
return group;
}

public void setGroup(String group) {
this.group = group;
}

public String getDataId() {
return dataId;
}

public void setDataId(String dataId) {
this.dataId = dataId;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

/**
* Returns a string representation of the FuzzyListenConfigChangeEvent.
*
* @return A string representation of the event
*/
@Override
public String toString() {
return "FuzzyListenConfigChangeEvent{" + "group='" + group + '\'' + ", dataId='" + dataId + '\'' + ", type='"
+ type + '\'' + '}';
}
}
Loading

0 comments on commit 17bd04e

Please sign in to comment.