Skip to content

Commit

Permalink
feat:update client id generator. (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeBeFreeman authored Dec 2, 2024
1 parent f2ad583 commit 2d65ad3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.tencent.polaris.api.plugin.stat.ReporterMetaInfo;
import com.tencent.polaris.api.plugin.stat.StatReporter;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.api.utils.IPAddressUtils;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.client.flow.AbstractFlow;
import com.tencent.polaris.client.util.NamedThreadFactory;
Expand All @@ -64,6 +65,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
* SDK初始化相关的上下文信息
Expand All @@ -74,6 +76,12 @@ public class SDKContext extends Destroyable implements InitContext, AutoCloseabl

private static final Logger LOG = LoggerFactory.getLogger(SDKContext.class);
private static final String DEFAULT_ADDRESS = "127.0.0.1";

/**
* 客户端ID自增序列
*/
private static final AtomicInteger CLIENT_ID_SEQ = new AtomicInteger(0);

/**
* 配置对象
*/
Expand Down Expand Up @@ -105,7 +113,8 @@ public SDKContext(Configuration configuration, Manager plugins, ValueContext val
this.configuration = configuration;
this.plugins = plugins;
this.valueContext = valueContext;
this.valueContext.setClientId(generateClientId(this.valueContext.getHost()));
this.valueContext.setClientId(generateClientId(getHostNameOrDefaultToHost(this.valueContext.getHost())));
LOG.info("init SDKContext with clientId={}", this.valueContext.getClientId());
List<ServerServiceInfo> services = new ArrayList<>();
//加载系统服务配置
SystemConfig system = configuration.getGlobal().getSystem();
Expand All @@ -130,7 +139,24 @@ public SDKContext(Configuration configuration, Manager plugins, ValueContext val
}

private static String generateClientId(String host) {
return host + "-" + getProcessId("0");
if (!StringUtils.equalsIgnoreCase(host, DEFAULT_ADDRESS)) {
return host + "_" + getProcessId("0") + "_" + CLIENT_ID_SEQ.getAndIncrement();
} else {
return UUID.randomUUID().toString();
}
}

private static String getHostNameOrDefaultToHost(String host) {
try {
String hostName = IPAddressUtils.getHostName();
if (StringUtils.isBlank(hostName)) {
hostName = host;
}
return hostName;
} catch (Throwable throwable) {
LOG.error("fail to get host name", throwable);
return host;
}
}

private static String getProcessId(String fallback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@

package com.tencent.polaris.api.utils;

import com.tencent.polaris.logging.LoggerFactory;
import org.slf4j.Logger;

import java.net.InetAddress;

/**
* @author Haotian Zhang
*/
public class IPAddressUtils {


private static final Logger LOG = LoggerFactory.getLogger(IPAddressUtils.class);

public static String getIpCompatible(String ip) {
if (StringUtils.isEmpty(ip)) {
return ip;
Expand All @@ -31,4 +38,20 @@ public static String getIpCompatible(String ip) {
}
return ip;
}

public static String getHostName() {
try {
String hostname = System.getenv("HOSTNAME");
if (StringUtils.isBlank(hostname)) {
hostname = System.getProperty("HOSTNAME");
}
if (StringUtils.isBlank(hostname)) {
hostname = InetAddress.getLocalHost().getHostName();
}
return hostname;
} catch (Exception e) {
LOG.warn("get host name error", e);
return "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void initActually(InitContext ctx, ServerConnectorConfig connectorConfig
updateServiceExecutor = new ScheduledThreadPoolExecutor(1,
new NamedThreadFactory(getName() + "-update-service"));
updateServiceExecutor.setMaximumPoolSize(1);
clientInstanceId = UUID.randomUUID().toString();
clientInstanceId = ctx.getValueContext().getClientId();
initialized = true;
}

Expand Down

0 comments on commit 2d65ad3

Please sign in to comment.