Skip to content

Commit

Permalink
fix:fix reportClient not working bug when using push-gateway strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeBeFreeman committed May 9, 2023
1 parent 5ef3f9d commit 4baf026
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@
import com.tencent.polaris.api.plugin.compose.Extensions;
import com.tencent.polaris.api.plugin.compose.ServerServiceInfo;
import com.tencent.polaris.api.plugin.impl.PluginManager;
import com.tencent.polaris.api.plugin.server.ReportClientRequest;
import com.tencent.polaris.api.plugin.server.ReportClientResponse;
import com.tencent.polaris.api.plugin.server.ServerConnector;
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.StringUtils;
import com.tencent.polaris.client.util.NamedThreadFactory;
import com.tencent.polaris.factory.ConfigAPIFactory;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.logging.LoggerFactory;
Expand All @@ -46,17 +52,22 @@
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;
import java.util.ServiceLoader;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.yaml.snakeyaml.Yaml;

import com.tencent.polaris.version.Version;

/**
* SDK初始化相关的上下文信息
*
Expand Down Expand Up @@ -84,6 +95,8 @@ public class SDKContext extends Destroyable implements InitContext, AutoCloseabl
private final List<Destroyable> destroyHooks = new ArrayList<>();
private final Collection<ServerServiceInfo> serverServices;

private final ScheduledExecutorService reportClientExecutorService;

/**
* 构造器
*
Expand Down Expand Up @@ -116,6 +129,7 @@ public SDKContext(Configuration configuration, Manager plugins, ValueContext val
services.add(new ServerServiceInfo(ClusterType.MONITOR_CLUSTER, monitorCluster));
}
this.serverServices = Collections.unmodifiableCollection(services);
this.reportClientExecutorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("polaris-report-client"));
}

private static String generateClientId(String host) {
Expand Down Expand Up @@ -298,6 +312,7 @@ public synchronized void init() throws PolarisException {
}
extensions.init(configuration, plugins, valueContext);
plugins.postContextInitPlugins(extensions);
reportClient(extensions);
}

private boolean clusterAvailable(ClusterConfig clusterConfig) {
Expand All @@ -313,6 +328,36 @@ private boolean clusterAvailable(ClusterConfig clusterConfig) {
return true;
}

/**
* Report prometheus http server metadata periodic
*
* @param extensions extensions
*/
private void reportClient(Extensions extensions) {
reportClientExecutorService.scheduleAtFixedRate(() -> {
ServerConnector serverConnector = extensions.getServerConnector();
ReportClientRequest reportClientRequest = new ReportClientRequest();
reportClientRequest.setClientHost(extensions.getValueContext().getHost());
reportClientRequest.setVersion(Version.VERSION);
List<StatReporter> statPlugins = extensions.getStatReporters();
List<ReporterMetaInfo> reporterMetaInfos = new ArrayList<>();
for (StatReporter statPlugin : statPlugins) {
ReporterMetaInfo reporterMetaInfo = statPlugin.metaInfo();
if (StringUtils.isNotBlank(reporterMetaInfo.getProtocol())) {
reporterMetaInfos.add(reporterMetaInfo);
}
}
reportClientRequest.setReporterMetaInfos(reporterMetaInfos);

try {
ReportClientResponse reportClientResponse = serverConnector.reportClient(reportClientRequest);
LOG.debug("Report client success, response:{}", reportClientResponse);
} catch (PolarisException e) {
LOG.error("Report client failed.", e);
}
}, 0L, 60L, TimeUnit.SECONDS);
}

public Extensions getExtensions() {
return extensions;
}
Expand All @@ -333,6 +378,9 @@ protected void doDestroy() {
}
}
plugins.destroyPlugins();
if (Objects.nonNull(reportClientExecutorService)) {
reportClientExecutorService.shutdown();
}
}

public ValueContext getValueContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.tencent.polaris.api.plugin.route.LocationLevel;
import com.tencent.polaris.api.plugin.route.ServiceRouter;
import com.tencent.polaris.api.plugin.server.ServerConnector;
import com.tencent.polaris.api.plugin.stat.StatReporter;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.logging.LoggerFactory;
import com.tencent.polaris.specification.api.v1.model.ModelProto;
Expand Down Expand Up @@ -66,6 +67,7 @@ public class Extensions {
private Configuration configuration;
private CircuitBreaker resourceBreaker;

private final List<StatReporter> statReporters = new ArrayList<>();
private Supplier plugins;

//系统服务的路由链
Expand Down Expand Up @@ -161,6 +163,9 @@ public void init(Configuration config, Supplier plugins, ValueContext valueConte
serverConnector = (ServerConnector) plugins.getPlugin(PluginTypes.SERVER_CONNECTOR.getBaseType(),
valueContext.getServerConnectorProtocol());

// 加载监控上报
loadStatReporters(plugins);

initLocation(config, valueContext);
}

Expand Down Expand Up @@ -231,6 +236,15 @@ private void loadOutlierDetector(Configuration config, Supplier plugins) throws
}
}

private void loadStatReporters(Supplier plugins) throws PolarisException {
Collection<Plugin> reporters = plugins.getPlugins(PluginTypes.STAT_REPORTER.getBaseType());
if (CollectionUtils.isNotEmpty(reporters)) {
for (Plugin reporter : reporters) {
statReporters.add((StatReporter) reporter);
}
}
}

public Supplier getPlugins() {
return plugins;
}
Expand All @@ -251,6 +265,10 @@ public List<InstanceCircuitBreaker> getInstanceCircuitBreakers() {
return instanceCircuitBreakers;
}

public List<StatReporter> getStatReporters() {
return statReporters;
}

public List<HealthChecker> getHealthCheckers() {
return healthCheckers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ void initHandle() {
startSchedulePushTask();
} else {
startScheduleAggregationTask();
reportClient(extensions);
}
}
}
Expand Down Expand Up @@ -237,29 +236,6 @@ public void destroy() {
}
}

/**
* Report prometheus http server metadata periodic
*
* @param extensions extensions
*/
private void reportClient(Extensions extensions) {
if (executorService != null) {
executorService.scheduleAtFixedRate(() -> {
ServerConnector serverConnector = extensions.getServerConnector();
ReportClientRequest reportClientRequest = new ReportClientRequest();
reportClientRequest.setClientHost(extensions.getValueContext().getHost());
reportClientRequest.setVersion(Version.VERSION);
reportClientRequest.setReporterMetaInfos(Collections.singletonList(metaInfo()));
try {
ReportClientResponse reportClientResponse = serverConnector.reportClient(reportClientRequest);
LOGGER.debug("Report prometheus http server metadata success, response:{}", reportClientResponse);
} catch (PolarisException e) {
LOGGER.error("Report prometheus http server info exception.", e);
}
}, 0L, 60L, TimeUnit.SECONDS);
}
}

private void startScheduleAggregationTask() {
// If port is -1, then disable prometheus http server
if (config.getPort() == -1) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

<properties>
<!-- Project revision -->
<revision>1.12.3</revision>
<revision>1.12.4-SNAPSHOT</revision>

<timestamp>${maven.build.timestamp}</timestamp>
<skip.maven.deploy>false</skip.maven.deploy>
Expand Down

0 comments on commit 4baf026

Please sign in to comment.