Skip to content

Commit f55af1e

Browse files
authored
fix: delay init uri with service name. (#659)
1 parent ee34933 commit f55af1e

File tree

3 files changed

+87
-18
lines changed

3 files changed

+87
-18
lines changed

polaris-plugins/polaris-plugins-observability/event-tsf/src/main/java/com/tencent/polaris/plugins/event/tsf/TsfEventReporter.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import com.tencent.polaris.api.utils.ThreadPoolUtils;
3939
import com.tencent.polaris.client.util.NamedThreadFactory;
4040
import com.tencent.polaris.logging.LoggerFactory;
41+
import com.tencent.polaris.metadata.core.constant.MetadataConstants;
42+
import com.tencent.polaris.metadata.core.manager.CalleeMetadataContainerGroup;
4143
import com.tencent.polaris.plugins.event.tsf.report.CloudEvent;
4244
import com.tencent.polaris.plugins.event.tsf.report.Event;
4345
import com.tencent.polaris.plugins.event.tsf.report.EventResponse;
@@ -83,6 +85,8 @@ public class TsfEventReporter implements EventReporter, PluginConfigProvider {
8385

8486
private volatile boolean init = true;
8587

88+
private volatile boolean eventUriInit = false;
89+
8690
private TsfEventReporterConfig tsfEventReporterConfig;
8791

8892
private URI v1EventUri;
@@ -117,6 +121,7 @@ public boolean reportEvent(BaseEvent baseEvent) {
117121

118122
private boolean reportV1Event(FlowEvent flowEvent) {
119123
try {
124+
initEventUri();
120125
if (v1EventUri == null) {
121126
LOG.warn("build v1 event request url fail, can not sent event.");
122127
return false;
@@ -134,11 +139,11 @@ private boolean reportV1Event(FlowEvent flowEvent) {
134139
List<TsfEventDataPair> dimensions = new ArrayList<>();
135140
dimensions.add(new TsfEventDataPair(APP_ID_KEY, tsfEventReporterConfig.getAppId()));
136141
dimensions.add(new TsfEventDataPair(NAMESPACE_ID_KEY, tsfEventReporterConfig.getTsfNamespaceId()));
137-
dimensions.add(new TsfEventDataPair(SERVICE_NAME, tsfEventReporterConfig.getServiceName()));
142+
dimensions.add(new TsfEventDataPair(SERVICE_NAME, getLocalServiceName()));
138143
eventData.setDimensions(dimensions);
139144

140145
List<TsfEventDataPair> additionalMsg = new ArrayList<>();
141-
additionalMsg.add(new TsfEventDataPair(UPSTREAM_SERVICE_KEY, tsfEventReporterConfig.getServiceName()));
146+
additionalMsg.add(new TsfEventDataPair(UPSTREAM_SERVICE_KEY, getLocalServiceName()));
142147
additionalMsg.add(new TsfEventDataPair(UPSTREAM_NAMESPACE_ID_KEY, tsfEventReporterConfig.getTsfNamespaceId()));
143148
additionalMsg.add(new TsfEventDataPair(DOWNSTREAM_SERVICE_KEY, flowEvent.getService()));
144149
additionalMsg.add(new TsfEventDataPair(DOWNSTREAM_NAMESPACE_ID_KEY, flowEvent.getNamespace()));
@@ -193,7 +198,7 @@ private boolean reportReportEvent(FlowEvent flowEvent) {
193198

194199
cloudEvent.putDimension(APP_ID_KEY, tsfEventReporterConfig.getAppId());
195200
cloudEvent.putDimension(NAMESPACE_ID_KEY, tsfEventReporterConfig.getTsfNamespaceId());
196-
cloudEvent.putDimension(SERVICE_NAME, tsfEventReporterConfig.getServiceName());
201+
cloudEvent.putDimension(SERVICE_NAME, getLocalServiceName());
197202
cloudEvent.putDimension(APPLICATION_ID, tsfEventReporterConfig.getApplicationId());
198203

199204
cloudEvent.putExtensionMsg(UPSTREAM_SERVICE_KEY, flowEvent.getSourceService());
@@ -208,7 +213,7 @@ private boolean reportReportEvent(FlowEvent flowEvent) {
208213

209214
String uniqueId = tsfEventReporterConfig.getInstanceId()
210215
+ "#" + tsfEventReporterConfig.getTsfNamespaceId()
211-
+ "#" + tsfEventReporterConfig.getServiceName() + "#" + ruleId;
216+
+ "#" + getLocalServiceName() + "#" + ruleId;
212217
cloudEvent.setId(uniqueId);
213218
cloudEvent.setObject(uniqueId);
214219

@@ -269,18 +274,7 @@ public void postContextInit(Extensions ctx) throws PolarisException {
269274
if (!init) {
270275
init = true;
271276
try {
272-
String v1Path = String.format("/v1/event/%s/%s",
273-
URLEncoder.encode(tsfEventReporterConfig.getServiceName(), "UTF-8"),
274-
URLEncoder.encode(tsfEventReporterConfig.getInstanceId(), "UTF-8"));
275-
v1EventUri = new URIBuilder()
276-
.setScheme("http")
277-
.setHost(IPAddressUtils.getIpCompatible(tsfEventReporterConfig.getEventMasterIp()))
278-
.setPort(tsfEventReporterConfig.getEventMasterPort())
279-
.setPath(v1Path)
280-
.setParameter("token", tsfEventReporterConfig.getToken())
281-
.build();
282277
v1EventExecutors.scheduleWithFixedDelay(new TsfV1EventTask(), 1000, 1000, TimeUnit.MILLISECONDS);
283-
LOG.info("Tsf v1 event reporter init with uri: {}", v1EventUri);
284278

285279
this.reportEventUri = new URIBuilder()
286280
.setScheme("http")
@@ -292,9 +286,41 @@ public void postContextInit(Extensions ctx) throws PolarisException {
292286
reportEventExecutors.scheduleWithFixedDelay(new TsfReportEventTask(), 1000, 1000, TimeUnit.MILLISECONDS);
293287
LOG.info("Tsf report event reporter init with uri: {}", reportEventUri);
294288
LOG.info("Tsf event reporter starts reporting task.");
289+
} catch (URISyntaxException e) {
290+
LOG.error("Build event request url fail.", e);
291+
}
292+
}
293+
}
294+
}
295+
}
296+
297+
private String getLocalServiceName() {
298+
return CalleeMetadataContainerGroup.getStaticApplicationMetadataContainer().getRawMetadataStringValue(MetadataConstants.LOCAL_SERVICE);
299+
}
300+
301+
/**
302+
* delay init event uri, as service name may not be ready at bootstrap stage.
303+
*/
304+
private void initEventUri() {
305+
if (!eventUriInit) {
306+
synchronized (this) {
307+
if (!eventUriInit) {
308+
try {
309+
String v1Path = String.format("/v1/event/%s/%s",
310+
URLEncoder.encode(getLocalServiceName(), "UTF-8"),
311+
URLEncoder.encode(tsfEventReporterConfig.getInstanceId(), "UTF-8"));
312+
v1EventUri = new URIBuilder()
313+
.setScheme("http")
314+
.setHost(IPAddressUtils.getIpCompatible(tsfEventReporterConfig.getEventMasterIp()))
315+
.setPort(tsfEventReporterConfig.getEventMasterPort())
316+
.setPath(v1Path)
317+
.setParameter("token", tsfEventReporterConfig.getToken())
318+
.build();
319+
LOG.info("Tsf v1 event reporter init with uri: {}", v1EventUri);
295320
} catch (URISyntaxException | UnsupportedEncodingException e) {
296321
LOG.error("Build event request url fail.", e);
297322
}
323+
eventUriInit = true;
298324
}
299325
}
300326
}
@@ -331,6 +357,7 @@ private void postV1Event(TsfGenericEvent genericEvent) {
331357
StringEntity postBody = null;
332358
RequestConfig config = RequestConfig.custom().setConnectTimeout(2000).setConnectionRequestTimeout(10000).setSocketTimeout(10000).build();
333359
try (CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build()) {
360+
initEventUri();
334361
HttpPost httpPost = new HttpPost(v1EventUri);
335362
postBody = new StringEntity(gson.toJson(genericEvent));
336363
httpPost.setEntity(postBody);

polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/flow/QuotaFlow.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,10 @@ public void init(Extensions extensions) throws PolarisException {
9898
if (CollectionUtils.isNotEmpty(metadata) && metadata.containsKey(TsfRateLimitConstants.RATE_LIMIT_MASTER_IP_KEY)) {
9999
String rateLimitMasterIp = metadata.get(TsfRateLimitConstants.RATE_LIMIT_MASTER_IP_KEY);
100100
String rateLimitMasterPort = metadata.get(TsfRateLimitConstants.RATE_LIMIT_MASTER_PORT_KEY);
101-
String serviceName = metadata.get(TsfRateLimitConstants.SERVICE_NAME_KEY);
102101
String instanceId = metadata.get(TsfRateLimitConstants.INSTANCE_ID_KEY);
103102
String token = metadata.get(TsfRateLimitConstants.TOKEN_KEY);
104-
if (!StringUtils.isAnyEmpty(rateLimitMasterIp, rateLimitMasterPort, serviceName, instanceId, token)) {
105-
TsfRateLimitMasterUtils.setUri(rateLimitMasterIp, rateLimitMasterPort, serviceName, instanceId, token);
103+
if (!StringUtils.isAnyEmpty(rateLimitMasterIp, rateLimitMasterPort, instanceId, token)) {
104+
TsfRateLimitMasterUtils.setRateLimitConfig(rateLimitConfig);
106105
lock = new ReentrantLock();
107106
}
108107
}

polaris-ratelimit/polaris-ratelimit-client/src/main/java/com/tencent/polaris/ratelimit/client/sync/tsf/TsfRateLimitMasterUtils.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919

2020
import com.google.gson.Gson;
2121
import com.google.gson.GsonBuilder;
22+
import com.tencent.polaris.api.config.provider.RateLimitConfig;
2223
import com.tencent.polaris.client.util.EscapeNonAsciiWriter;
2324
import com.tencent.polaris.logging.LoggerFactory;
25+
import com.tencent.polaris.metadata.core.constant.MetadataConstants;
26+
import com.tencent.polaris.metadata.core.manager.CalleeMetadataContainerGroup;
2427
import org.apache.commons.codec.Charsets;
2528
import org.apache.http.Header;
2629
import org.apache.http.HttpEntity;
@@ -57,14 +60,53 @@ public class TsfRateLimitMasterUtils {
5760

5861
private static final CloseableHttpClient httpClient;
5962

63+
private static RateLimitConfig rateLimitConfig;
64+
6065
private static URI uri = null;
6166

67+
private static volatile boolean uriInit = false;
68+
69+
private static final Object lock = new Object();
70+
6271
static {
6372
RequestConfig config = RequestConfig.custom().setConnectTimeout(500).setConnectionRequestTimeout(2000)
6473
.setSocketTimeout(1000).build();
6574
httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
6675
}
6776

77+
public static void setRateLimitConfig(RateLimitConfig rateLimitConfig) {
78+
TsfRateLimitMasterUtils.rateLimitConfig = rateLimitConfig;
79+
}
80+
81+
/**
82+
* delay init uri, as service name may not be ready at bootstrap stage.
83+
*/
84+
public static void initUri() {
85+
if (!uriInit) {
86+
synchronized (lock) {
87+
if (!uriInit) {
88+
if (rateLimitConfig == null) {
89+
LOG.warn("rate limit config is null.");
90+
uriInit = true;
91+
return;
92+
}
93+
94+
Map<String, String> metadata = rateLimitConfig.getMetadata();
95+
String rateLimitMasterIp = metadata.get(TsfRateLimitConstants.RATE_LIMIT_MASTER_IP_KEY);
96+
String rateLimitMasterPort = metadata.get(TsfRateLimitConstants.RATE_LIMIT_MASTER_PORT_KEY);
97+
String serviceName = CalleeMetadataContainerGroup.getStaticApplicationMetadataContainer().
98+
getRawMetadataStringValue(MetadataConstants.LOCAL_SERVICE);
99+
String instanceId = metadata.get(TsfRateLimitConstants.INSTANCE_ID_KEY);
100+
String token = metadata.get(TsfRateLimitConstants.TOKEN_KEY);
101+
102+
setUri(rateLimitMasterIp, rateLimitMasterPort, serviceName, instanceId, token);
103+
LOG.info("Tsf ratelimit master uri init: {}", uri);
104+
uriInit = true;
105+
}
106+
}
107+
}
108+
}
109+
68110
public static void setUri(String rateLimitMasterIp, String rateLimitMasterPort, String serviceName, String instanceId, String token) {
69111
try {
70112
int port = Integer.parseInt(rateLimitMasterPort);
@@ -81,6 +123,7 @@ public static void setUri(String rateLimitMasterIp, String rateLimitMasterPort,
81123
}
82124

83125
public static Map<String, Integer> report(String ruleId, long pass, long block) {
126+
initUri();
84127
if (uri == null) {
85128
LOG.warn("url is not set.");
86129
}

0 commit comments

Comments
 (0)