Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add client labels collect and attach labels to conn #11844

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 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 @@ -33,6 +33,8 @@ public class Constants {

public static final String APPNAME = "AppName";

public static final String CLIENT_VERSION_KEY = "ClientVersion";

public static final String UNKNOWN_APP = "UnknownApp";

public static final String DEFAULT_DOMAINNAME = "commonconfig.config-host.taobao.com";
Expand Down Expand Up @@ -223,6 +225,36 @@ public class Constants {

public static final int DEFAULT_REDO_THREAD_COUNT = 1;

public static final String APP_CONN_LABELS_PREFIX = "nacos.app.conn.labels";

public static final String GRAY = "gray";

public static final String DOT = ".";

public static final String WEIGHT = "weight";

public static final String PROPERTIES_KEY = "properties";

public static final String JVM_KEY = "jvm";

public static final String ENV_KEY = "env";

public static final String APP_CONN_LABELS_PROPERTIES_WEIGHT_KEY = APP_CONN_LABELS_PREFIX + DOT + WEIGHT + DOT + PROPERTIES_KEY;

public static final int APP_CONN_LABELS_PROPERTIES_DEFAULT_WEIGHT = 3;

public static final String APP_CONN_LABELS_JVM_WEIGHT_KEY = APP_CONN_LABELS_PREFIX + DOT + WEIGHT + DOT + JVM_KEY;

public static final int APP_CONN_LABELS_JVM_DEFAULT_WEIGHT = 2;

public static final String APP_CONN_LABELS_ENV_WEIGHT_KEY = APP_CONN_LABELS_PREFIX + DOT + WEIGHT + DOT + ENV_KEY;

public static final int APP_CONN_LABELS_ENV_DEFAULT_WEIGHT = 1;

public static final String APP_CONN_PREFIX = "app_";

public static final String CONFIG_GRAY = "nacos.config" + DOT + GRAY;

/**
* The constants in config directory.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ private RpcClient ensureRpcClient(String taskId) throws NacosException {
Map<String, String> newLabels = new HashMap<>(labels);
newLabels.put("taskId", taskId);
RpcClient rpcClient = RpcClientFactory.createClient(uuid + "_config-" + taskId, getConnectionType(),
newLabels, RpcClientTlsConfig.properties(this.properties));
newLabels, this.properties, RpcClientTlsConfig.properties(this.properties));
if (rpcClient.isWaitInitiated()) {
initRpcClientHandler(rpcClient);
rpcClient.setTenant(getTenant());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public void before() {
rpcClientFactoryMockedStatic.when(
() -> RpcClientFactory.createClient(anyString(), any(ConnectionType.class), any(Map.class),
any(RpcClientTlsConfig.class))).thenReturn(rpcClient);
rpcClientFactoryMockedStatic.when(
() -> RpcClientFactory.createClient(anyString(), any(ConnectionType.class), any(Map.class),
any(Properties.class), any(RpcClientTlsConfig.class))).thenReturn(rpcClient);
localConfigInfoProcessorMockedStatic = Mockito.mockStatic(LocalConfigInfoProcessor.class);
Properties properties = new Properties();
properties.put(PropertyKeyConst.NAMESPACE, TEST_NAMESPACE);
Expand Down Expand Up @@ -555,7 +558,7 @@ public void receiveConfigInfo(String configInfo) {
Mockito.when(rpcClientInner.isWaitInitiated()).thenReturn(true, false);
rpcClientFactoryMockedStatic.when(
() -> RpcClientFactory.createClient(anyString(), any(ConnectionType.class), any(Map.class),
any(RpcClientTlsConfig.class))).thenReturn(rpcClientInner);
any(Properties.class), any(RpcClientTlsConfig.class))).thenReturn(rpcClientInner);
// mock listen and remove listen request
Mockito.when(rpcClientInner.request(any(ConfigBatchListenRequest.class), anyLong()))
.thenReturn(response, response);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.common.labels;

import java.util.Map;
import java.util.Properties;

/**
* LabelsCollector.
*
* @author rong
* @date 2024/2/4
*/
public interface LabelsCollector {

/**
* init labels.
*
* @param properties Properties
* @date 2024/2/4
* @description init labels
*/
void init(Properties properties);

/**
* getLabels.
*
* @return Map
* @date 2024/2/4
* @description get all labels
*/
Map<String, String> getLabels();

/**
* getOrder.
*
* @return the order value
* @date 2024/2/4
* @description get order value of labels in case of multiple labels
*/
int getOrder();

/**
* get collector name.
*
* @return name of collector
* @date 2024/2/4
* @description name of collector
*/
String getName();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.common.labels;

import java.util.Map;
import java.util.Properties;

/**
* LabelsCollectorManager.
*
* @author rong
* @date 2024/2/4
*/
public interface LabelsCollectorManager {

/**
* get all labels collected.
*
* @date 2024/2/4
* @description
* @return all labels
*/
Map<String, String> getAllLabels();

/**
* refresh all labels.
*
* @date 2024/3/7
* @param properties Properties.
* @return all labels.
*/
Map<String, String> refreshAllLabels(Properties properties);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.common.labels.impl;

import com.alibaba.nacos.common.labels.LabelsCollector;

import java.util.HashMap;
import java.util.Map;

/**
* AbstractLabelsCollector.
*
* @author rong
*/
public abstract class AbstractLabelsCollector implements LabelsCollector {

protected Map<String, String> labels = new HashMap<>(2);

private static final int DEFAULT_INITIAL_ORDER = 100;

@Override
public Map<String, String> getLabels() {
return labels;
}

@Override
public int getOrder() {
return DEFAULT_INITIAL_ORDER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.common.labels.impl;

import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.common.labels.LabelsCollector;
import com.alibaba.nacos.common.labels.impl.utils.ConfigGetterManager;
import com.alibaba.nacos.common.utils.ConnLabelsUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Properties;

/**
* DefaultLabelsCollector.
*
* @author rong
*/
public class DefaultLabelsCollector extends AbstractLabelsCollector implements LabelsCollector {

protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultLabelsCollector.class);

private final String customName = "defaultLabelsCollector";

/**
* init labels.
*
* @date 2024/2/4
*@description will init from properties, JVM OPTIONS, ENV by order of <tt>properties > JVM OPTIONS > ENV</tt> by default.
* which will use the next level value when the current level value isn't setup (you also can set the level by env).
* <p>eg: if the value of "nacos.app.conn.labels"(properties' key) is "k1=v1,k2=v2"(properties' value), the result will be
* a Map with value{k1=v1,k2=v2}.</p>
* @param properties Properties
*/
@Override
public void init(Properties properties) {
ConfigGetterManager configGetterManager = new ConfigGetterManager(properties);
labels.putAll(ConnLabelsUtils.parseRawLabels(configGetterManager.getConfig(Constants.APP_CONN_LABELS_PREFIX)));

String grayLabelValue = configGetterManager.getConfig(Constants.CONFIG_GRAY);
if (StringUtils.isNotBlank(grayLabelValue)) {
labels.put(Constants.GRAY, grayLabelValue);
}
for (Map.Entry<String, String> entry : labels.entrySet()) {
LOGGER.info("init labels: {}={}", entry.getKey(), entry.getValue());
}
}

@Override
public String getName() {
return customName;
}
}
Loading
Loading