Optimize:optimize transfer (#636)

pull/642/head
lingxiao,wu 2 years ago committed by GitHub
parent 3306ef56c8
commit a0737092e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,3 +17,4 @@
- [feat:report the labels when using RestTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/629) - [feat:report the labels when using RestTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/629)
- [Bugfix: fix throw npe when router context is null](https://github.com/Tencent/spring-cloud-tencent/pull/635) - [Bugfix: fix throw npe when router context is null](https://github.com/Tencent/spring-cloud-tencent/pull/635)
- [fix:cancel reporting useless metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/639) - [fix:cancel reporting useless metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/639)
- [Optimize:optimize transfer](https://github.com/Tencent/spring-cloud-tencent/pull/636)

@ -20,11 +20,8 @@ package com.tencent.cloud.metadata.core;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.MetadataContextHolder;
@ -36,7 +33,6 @@ import reactor.core.publisher.Mono;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter; import org.springframework.web.server.WebFilter;
@ -45,8 +41,6 @@ import org.springframework.web.server.WebFilterChain;
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_DISPOSABLE_METADATA; import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_DISPOSABLE_METADATA;
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA; import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA;
import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS;
import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS_KV;
/** /**
* Filter used for storing the metadata from upstream temporarily when web application is * Filter used for storing the metadata from upstream temporarily when web application is
@ -85,39 +79,13 @@ public class DecodeTransferMetadataReactiveFilter implements WebFilter, Ordered
MetadataConstant.HeaderName.METADATA_CONTEXT, MetadataConstant.HeaderName.METADATA_CONTEXT,
MetadataContextHolder.get()); MetadataContextHolder.get());
setCompleteTransHeaderIntoMC(serverHttpRequest); TransHeadersTransfer.transfer(serverHttpRequest);
return webFilterChain.filter(serverWebExchange) return webFilterChain.filter(serverWebExchange)
.doOnError(throwable -> LOG.error("handle metadata[{}] error.", .doOnError(throwable -> LOG.error("handle metadata[{}] error.",
MetadataContextHolder.get(), throwable)) MetadataContextHolder.get(), throwable))
.doFinally((type) -> MetadataContextHolder.remove()); .doFinally((type) -> MetadataContextHolder.remove());
} }
/**
* According to ServerHttpRequest and trans-headers(key list in string type) in metadata, build
* the complete headers(key-value list in map type) into metadata.
*/
private void setCompleteTransHeaderIntoMC(ServerHttpRequest serverHttpRequest) {
// transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}}
Map<String, String> transHeaderMetadata = MetadataContextHolder.get()
.getFragmentContext(FRAGMENT_RAW_TRANSHEADERS);
if (!CollectionUtils.isEmpty(transHeaderMetadata)) {
String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse("");
String[] transHeaderArray = transHeaders.split(",");
HttpHeaders headers = serverHttpRequest.getHeaders();
Set<String> headerKeys = headers.keySet();
for (String httpHeader : headerKeys) {
Arrays.stream(transHeaderArray).forEach(transHeader -> {
if (transHeader.equals(httpHeader)) {
List<String> list = headers.get(httpHeader);
String httpHeaderValue = JacksonUtils.serialize2Json(list);
MetadataContextHolder.get()
.putContext(FRAGMENT_RAW_TRANSHEADERS_KV, httpHeader, httpHeaderValue);
}
});
}
}
}
private Map<String, String> getIntervalMetadata(ServerHttpRequest serverHttpRequest, String headerName) { private Map<String, String> getIntervalMetadata(ServerHttpRequest serverHttpRequest, String headerName) {
HttpHeaders httpHeaders = serverHttpRequest.getHeaders(); HttpHeaders httpHeaders = serverHttpRequest.getHeaders();
String customMetadataStr = httpHeaders.getFirst(headerName); String customMetadataStr = httpHeaders.getFirst(headerName);

@ -21,8 +21,6 @@ package com.tencent.cloud.metadata.core;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -39,15 +37,12 @@ import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.filter.OncePerRequestFilter;
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_DISPOSABLE_METADATA; import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_DISPOSABLE_METADATA;
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA; import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA;
import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS;
import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS_KV;
/** /**
* Filter used for storing the metadata from upstream temporarily when web application is * Filter used for storing the metadata from upstream temporarily when web application is
@ -76,33 +71,13 @@ public class DecodeTransferMetadataServletFilter extends OncePerRequestFilter {
MetadataContextHolder.init(mergedTransitiveMetadata, mergedDisposableMetadata); MetadataContextHolder.init(mergedTransitiveMetadata, mergedDisposableMetadata);
setCompleteTransHeaderIntoMC(httpServletRequest); TransHeadersTransfer.transfer(httpServletRequest);
filterChain.doFilter(httpServletRequest, httpServletResponse); try {
} filterChain.doFilter(httpServletRequest, httpServletResponse);
}
/** finally {
* According to HttpServletRequest and trans-headers(key list in string type) in metadata, build // Clean up ThreadLocal.
* the complete headers(key-value list in map type) into metadata. MetadataContextHolder.remove();
*/
private void setCompleteTransHeaderIntoMC(HttpServletRequest httpServletRequest) {
// transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}}
Map<String, String> transHeaderMetadata = MetadataContextHolder.get()
.getFragmentContext(FRAGMENT_RAW_TRANSHEADERS);
if (!CollectionUtils.isEmpty(transHeaderMetadata)) {
String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse("");
String[] transHeaderArray = transHeaders.split(",");
Enumeration<String> httpHeaders = httpServletRequest.getHeaderNames();
while (httpHeaders.hasMoreElements()) {
String httpHeader = httpHeaders.nextElement();
Arrays.stream(transHeaderArray).forEach(transHeader -> {
if (transHeader.equals(httpHeader)) {
String httpHeaderValue = httpServletRequest.getHeader(httpHeader);
// for example, {"trans-headers-kv" : {"header1":"v1","header2":"v2"...}}
MetadataContextHolder.get()
.putContext(FRAGMENT_RAW_TRANSHEADERS_KV, httpHeader, httpHeaderValue);
}
});
}
} }
} }

@ -19,7 +19,6 @@
package com.tencent.cloud.metadata.core; package com.tencent.cloud.metadata.core;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
@ -59,21 +58,14 @@ public class EncodeTransferMedataFeignInterceptor implements RequestInterceptor,
public void apply(RequestTemplate requestTemplate) { public void apply(RequestTemplate requestTemplate) {
// get metadata of current thread // get metadata of current thread
MetadataContext metadataContext = MetadataContextHolder.get(); MetadataContext metadataContext = MetadataContextHolder.get();
Map<String, String> customMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); Map<String, String> customMetadata = metadataContext.getCustomMetadata();
Map<String, String> disposableMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE); Map<String, String> disposableMetadata = metadataContext.getDisposableMetadata();
Map<String, String> transHeaders = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_RAW_TRANSHEADERS_KV); Map<String, String> transHeaders = metadataContext.getTransHeaders();
// Clean up one-time metadata coming from upstream .
Map<String, String> newestCustomMetadata = new HashMap<>();
customMetadata.forEach((key, value) -> {
if (!disposableMetadata.containsKey(key)) {
newestCustomMetadata.put(key, value);
}
});
this.buildMetadataHeader(requestTemplate, disposableMetadata, CUSTOM_DISPOSABLE_METADATA); this.buildMetadataHeader(requestTemplate, disposableMetadata, CUSTOM_DISPOSABLE_METADATA);
// process custom metadata // process custom metadata
this.buildMetadataHeader(requestTemplate, newestCustomMetadata, CUSTOM_METADATA); this.buildMetadataHeader(requestTemplate, customMetadata, CUSTOM_METADATA);
// set headers that need to be transmitted from the upstream // set headers that need to be transmitted from the upstream
this.buildTransmittedHeader(requestTemplate, transHeaders); this.buildTransmittedHeader(requestTemplate, transHeaders);

@ -21,7 +21,6 @@ package com.tencent.cloud.metadata.core;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
@ -59,21 +58,15 @@ public class EncodeTransferMedataRestTemplateInterceptor implements ClientHttpRe
@NonNull ClientHttpRequestExecution clientHttpRequestExecution) throws IOException { @NonNull ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
// get metadata of current thread // get metadata of current thread
MetadataContext metadataContext = MetadataContextHolder.get(); MetadataContext metadataContext = MetadataContextHolder.get();
Map<String, String> customMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); Map<String, String> customMetadata = metadataContext.getCustomMetadata();
Map<String, String> disposableMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE); Map<String, String> disposableMetadata = metadataContext.getDisposableMetadata();
Map<String, String> transHeaders = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_RAW_TRANSHEADERS_KV); Map<String, String> transHeaders = metadataContext.getTransHeaders();
Map<String, String> newestCustomMetadata = new HashMap<>();
customMetadata.forEach((key, value) -> {
if (!disposableMetadata.containsKey(key)) {
newestCustomMetadata.put(key, value);
}
});
// build custom disposable metadata request header // build custom disposable metadata request header
this.buildMetadataHeader(httpRequest, disposableMetadata, CUSTOM_DISPOSABLE_METADATA); this.buildMetadataHeader(httpRequest, disposableMetadata, CUSTOM_DISPOSABLE_METADATA);
// build custom metadata request header // build custom metadata request header
this.buildMetadataHeader(httpRequest, newestCustomMetadata, CUSTOM_METADATA); this.buildMetadataHeader(httpRequest, customMetadata, CUSTOM_METADATA);
// set headers that need to be transmitted from the upstream // set headers that need to be transmitted from the upstream
this.buildTransmittedHeader(httpRequest, transHeaders); this.buildTransmittedHeader(httpRequest, transHeaders);

@ -20,11 +20,7 @@ package com.tencent.cloud.metadata.core;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.constant.MetadataConstant;
import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContext;
@ -35,7 +31,6 @@ import reactor.core.publisher.Mono;
import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
@ -43,8 +38,6 @@ import org.springframework.web.server.ServerWebExchange;
import static com.tencent.cloud.common.constant.ContextConstant.UTF_8; import static com.tencent.cloud.common.constant.ContextConstant.UTF_8;
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_DISPOSABLE_METADATA; import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_DISPOSABLE_METADATA;
import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA; import static com.tencent.cloud.common.constant.MetadataConstant.HeaderName.CUSTOM_METADATA;
import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS;
import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS_KV;
import static org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter.LOAD_BALANCER_CLIENT_FILTER_ORDER; import static org.springframework.cloud.gateway.filter.ReactiveLoadBalancerClientFilter.LOAD_BALANCER_CLIENT_FILTER_ORDER;
/** /**
@ -72,51 +65,16 @@ public class EncodeTransferMedataScgFilter implements GlobalFilter, Ordered {
metadataContext = MetadataContextHolder.get(); metadataContext = MetadataContextHolder.get();
} }
Map<String, String> customMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); Map<String, String> customMetadata = metadataContext.getCustomMetadata();
Map<String, String> disposableMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE); Map<String, String> disposableMetadata = metadataContext.getDisposableMetadata();
// Clean upstream disposable metadata. this.buildMetadataHeader(builder, customMetadata, CUSTOM_METADATA);
Map<String, String> newestCustomMetadata = new HashMap<>();
customMetadata.forEach((key, value) -> {
if (!disposableMetadata.containsKey(key)) {
newestCustomMetadata.put(key, value);
}
});
this.buildMetadataHeader(builder, newestCustomMetadata, CUSTOM_METADATA);
this.buildMetadataHeader(builder, disposableMetadata, CUSTOM_DISPOSABLE_METADATA); this.buildMetadataHeader(builder, disposableMetadata, CUSTOM_DISPOSABLE_METADATA);
setCompleteTransHeaderIntoMC(exchange.getRequest()); TransHeadersTransfer.transfer(exchange.getRequest());
return chain.filter(exchange.mutate().request(builder.build()).build()); return chain.filter(exchange.mutate().request(builder.build()).build());
} }
/**
* According to ServerHttpRequest and trans-headers(key list in string type) in metadata, build
* the complete headers(key-value list in map type) into metadata.
*/
private void setCompleteTransHeaderIntoMC(ServerHttpRequest serverHttpRequest) {
// transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}}
Map<String, String> transHeaderMetadata = MetadataContextHolder.get()
.getFragmentContext(FRAGMENT_RAW_TRANSHEADERS);
if (!CollectionUtils.isEmpty(transHeaderMetadata)) {
String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse("");
String[] transHeaderArray = transHeaders.split(",");
HttpHeaders headers = serverHttpRequest.getHeaders();
Set<String> headerKeys = headers.keySet();
for (String httpHeader : headerKeys) {
Arrays.stream(transHeaderArray).forEach(transHeader -> {
if (transHeader.equals(httpHeader)) {
List<String> list = headers.get(httpHeader);
String httpHeaderValue = JacksonUtils.serialize2Json(list);
// for example, {"trans-headers-kv" : {"header1":"v1","header2":"v2"...}}
MetadataContextHolder.get()
.putContext(FRAGMENT_RAW_TRANSHEADERS_KV, httpHeader, httpHeaderValue);
}
});
}
}
}
/** /**
* Set metadata into the request header for {@link ServerHttpRequest.Builder} . * Set metadata into the request header for {@link ServerHttpRequest.Builder} .
* @param builder instance of {@link ServerHttpRequest.Builder} * @param builder instance of {@link ServerHttpRequest.Builder}

@ -0,0 +1,99 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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.tencent.cloud.metadata.core;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.util.JacksonUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.CollectionUtils;
/**
* According to request and trans-headers(key list in string type) in metadata, build
* the complete headers(key-value list in map type) into metadata.
*
* @author lingxiao.wlx
*/
public final class TransHeadersTransfer {
private TransHeadersTransfer() {
}
/**
* According to {@link HttpServletRequest} and trans-headers(key list in string type) in metadata, build
* the complete headers(key-value list in map type) into metadata.
*
* @param httpServletRequest httpServletRequest
*/
public static void transfer(HttpServletRequest httpServletRequest) {
// transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}}
Map<String, String> transHeaderMetadata = MetadataContextHolder.get().getTransHeaders();
if (!CollectionUtils.isEmpty(transHeaderMetadata)) {
String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse("");
String[] transHeaderArray = transHeaders.split(",");
Enumeration<String> httpHeaders = httpServletRequest.getHeaderNames();
while (httpHeaders.hasMoreElements()) {
String httpHeader = httpHeaders.nextElement();
Arrays.stream(transHeaderArray).forEach(transHeader -> {
if (transHeader.equals(httpHeader)) {
String httpHeaderValue = httpServletRequest.getHeader(httpHeader);
// for example, {"trans-headers-kv" : {"header1":"v1","header2":"v2"...}}
MetadataContextHolder.get().setTransHeadersKV(httpHeader, httpHeaderValue);
}
});
}
}
}
/**
* According to {@link ServerHttpRequest} and trans-headers(key list in string type) in metadata, build
* the complete headers(key-value list in map type) into metadata.
*
* @param serverHttpRequest serverHttpRequest
*/
public static void transfer(ServerHttpRequest serverHttpRequest) {
// transHeaderMetadata: for example, {"trans-headers" : {"header1,header2,header3":""}}
Map<String, String> transHeaderMetadata = MetadataContextHolder.get().getTransHeaders();
if (!CollectionUtils.isEmpty(transHeaderMetadata)) {
String transHeaders = transHeaderMetadata.keySet().stream().findFirst().orElse("");
String[] transHeaderArray = transHeaders.split(",");
HttpHeaders headers = serverHttpRequest.getHeaders();
Set<String> headerKeys = headers.keySet();
for (String httpHeader : headerKeys) {
Arrays.stream(transHeaderArray).forEach(transHeader -> {
if (transHeader.equals(httpHeader)) {
List<String> list = headers.get(httpHeader);
String httpHeaderValue = JacksonUtils.serialize2Json(list);
// for example, {"trans-headers-kv" : {"header1":"v1","header2":"v2"...}}
MetadataContextHolder.get().setTransHeadersKV(httpHeader, httpHeaderValue);
}
});
}
}
}
}

@ -124,8 +124,7 @@ public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceI
PolarisRouterContext routerContext = new PolarisRouterContext(); PolarisRouterContext routerContext = new PolarisRouterContext();
routerContext.putLabels(RouterConstant.TRANSITIVE_LABELS, MetadataContextHolder.get() routerContext.putLabels(RouterConstant.TRANSITIVE_LABELS, MetadataContextHolder.get().getTransitiveMetadata());
.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE));
Map<String, String> labelHeaderValuesMap = new HashMap<>(); Map<String, String> labelHeaderValuesMap = new HashMap<>();
try { try {

@ -109,8 +109,7 @@ public class RouterLabelFeignInterceptor implements RequestInterceptor, Ordered
} }
// labels from downstream // labels from downstream
Map<String, String> transitiveLabels = MetadataContextHolder.get() Map<String, String> transitiveLabels = MetadataContextHolder.get().getTransitiveMetadata();
.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
labels.putAll(transitiveLabels); labels.putAll(transitiveLabels);
// pass label by header // pass label by header

@ -139,8 +139,7 @@ public class RouterLabelRestTemplateInterceptor implements ClientHttpRequestInte
} }
// labels from downstream // labels from downstream
Map<String, String> transitiveLabels = MetadataContextHolder.get() Map<String, String> transitiveLabels = MetadataContextHolder.get().getTransitiveMetadata();
.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
labels.putAll(transitiveLabels); labels.putAll(transitiveLabels);
// pass label by header // pass label by header

@ -255,8 +255,7 @@ public class PolarisReactiveLoadBalancerClientFilter extends ReactiveLoadBalance
} }
// labels from downstream // labels from downstream
Map<String, String> transitiveLabels = MetadataContextHolder.get() Map<String, String> transitiveLabels = MetadataContextHolder.get().getTransitiveMetadata();
.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
labels.putAll(transitiveLabels); labels.putAll(transitiveLabels);
return labels; return labels;

@ -28,7 +28,7 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/** /**
* test for {@link PolarisRouterContext} * test for {@link PolarisRouterContext}.
* *
*@author lepdou 2022-05-26 *@author lepdou 2022-05-26
*/ */

@ -37,7 +37,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
* test for {@link RouterRuleLabelResolver} * test for {@link RouterRuleLabelResolver}.
*@author lepdou 2022-05-26 *@author lepdou 2022-05-26
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)

@ -30,7 +30,7 @@ import org.junit.Test;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* Test for {@link FeignExpressionLabelUtils} * Test for {@link FeignExpressionLabelUtils}.
*@author lepdou 2022-05-26 *@author lepdou 2022-05-26
*/ */
public class FeignExpressionLabelUtilsTest { public class FeignExpressionLabelUtilsTest {

@ -50,7 +50,7 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
* test for {@link RouterLabelFeignInterceptor} * test for {@link RouterLabelFeignInterceptor}.
* @author lepdou 2022-05-26 * @author lepdou 2022-05-26
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
@ -92,7 +92,7 @@ public class RouterLabelFeignInterceptorTest {
Map<String, String> transitiveLabels = new HashMap<>(); Map<String, String> transitiveLabels = new HashMap<>();
transitiveLabels.put("k1", "v1"); transitiveLabels.put("k1", "v1");
transitiveLabels.put("k2", "v22"); transitiveLabels.put("k2", "v22");
when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)).thenReturn(transitiveLabels); when(metadataContext.getTransitiveMetadata()).thenReturn(transitiveLabels);
// mock MetadataContextHolder#get // mock MetadataContextHolder#get
try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) { try (MockedStatic<MetadataContextHolder> mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class)) {

@ -135,7 +135,7 @@ public class RouterLabelRestTemplateInterceptorTest {
Map<String, String> transitiveLabels = new HashMap<>(); Map<String, String> transitiveLabels = new HashMap<>();
transitiveLabels.put("k1", "v1"); transitiveLabels.put("k1", "v1");
transitiveLabels.put("k2", "v22"); transitiveLabels.put("k2", "v22");
when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)).thenReturn(transitiveLabels); when(metadataContext.getTransitiveMetadata()).thenReturn(transitiveLabels);
routerLabels.putAll(transitiveLabels); routerLabels.putAll(transitiveLabels);
mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext); mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);

@ -59,7 +59,7 @@ import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
* Test for ${@link PolarisReactiveLoadBalancerClientFilter} * Test for ${@link PolarisReactiveLoadBalancerClientFilter}.
*@author lepdou 2022-07-04 *@author lepdou 2022-07-04
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
@ -97,7 +97,7 @@ public class PolarisReactiveLoadBalancerClientFilterTest {
Map<String, String> transitiveLabels = new HashMap<>(); Map<String, String> transitiveLabels = new HashMap<>();
transitiveLabels.put("t1", "v1"); transitiveLabels.put("t1", "v1");
transitiveLabels.put("t2", "v2"); transitiveLabels.put("t2", "v2");
when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)).thenReturn(transitiveLabels); when(metadataContext.getTransitiveMetadata()).thenReturn(transitiveLabels);
mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class); mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class);
mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext); mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);

@ -19,6 +19,7 @@
package com.tencent.cloud.common.metadata; package com.tencent.cloud.common.metadata;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -118,6 +119,63 @@ public class MetadataContext {
} }
public Map<String, String> getDisposableMetadata() {
return this.getFragmentContext(MetadataContext.FRAGMENT_DISPOSABLE);
}
public Map<String, String> getTransitiveMetadata() {
return this.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
}
public Map<String, String> getCustomMetadata() {
Map<String, String> transitiveMetadata = this.getTransitiveMetadata();
Map<String, String> disposableMetadata = this.getDisposableMetadata();
Map<String, String> customMetadata = new HashMap<>();
// Clean up one-time metadata coming from upstream .
transitiveMetadata.forEach((key, value) -> {
if (!disposableMetadata.containsKey(key)) {
customMetadata.put(key, value);
}
});
return Collections.unmodifiableMap(customMetadata);
}
public Map<String, String> getTransHeaders() {
return this.getFragmentContext(MetadataContext.FRAGMENT_RAW_TRANSHEADERS);
}
public Map<String, String> getTransHeadersKV() {
return this.getFragmentContext(MetadataContext.FRAGMENT_RAW_TRANSHEADERS_KV);
}
public Map<String, String> getLoadbalancerMetadata() {
return this.getFragmentContext(FRAGMENT_LOAD_BALANCER);
}
public void setTransitiveMetadata(Map<String, String> transitiveMetadata) {
this.putFragmentContext(FRAGMENT_TRANSITIVE, Collections.unmodifiableMap(transitiveMetadata));
}
public void setDisposableMetadata(Map<String, String> disposableMetadata) {
this.putFragmentContext(FRAGMENT_DISPOSABLE, Collections.unmodifiableMap(disposableMetadata));
}
public void setUpstreamDisposableMetadata(Map<String, String> upstreamDisposableMetadata) {
this.putFragmentContext(FRAGMENT_UPSTREAM_DISPOSABLE, Collections.unmodifiableMap(upstreamDisposableMetadata));
}
public void setTransHeadersKV(String key, String value) {
this.putContext(FRAGMENT_RAW_TRANSHEADERS_KV, key, value);
}
public void setTransHeaders(String key, String value) {
this.putContext(FRAGMENT_RAW_TRANSHEADERS, key, value);
}
public void setLoadbalancer(String key, String value) {
this.putContext(FRAGMENT_LOAD_BALANCER, key, value);
}
public Map<String, String> getFragmentContext(String fragment) { public Map<String, String> getFragmentContext(String fragment) {
Map<String, String> fragmentContext = fragmentContexts.get(fragment); Map<String, String> fragmentContext = fragmentContexts.get(fragment);
if (fragmentContext == null) { if (fragmentContext == null) {

@ -29,8 +29,6 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_DISPOSABLE; import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_DISPOSABLE;
import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_RAW_TRANSHEADERS;
import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_TRANSITIVE;
import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_UPSTREAM_DISPOSABLE; import static com.tencent.cloud.common.metadata.MetadataContext.FRAGMENT_UPSTREAM_DISPOSABLE;
/** /**
@ -67,11 +65,11 @@ public final class MetadataContextHolder {
// init static transitive metadata // init static transitive metadata
MetadataContext metadataContext = new MetadataContext(); MetadataContext metadataContext = new MetadataContext();
metadataContext.putFragmentContext(FRAGMENT_TRANSITIVE, staticMetadataManager.getMergedStaticTransitiveMetadata()); metadataContext.setTransitiveMetadata(staticMetadataManager.getMergedStaticTransitiveMetadata());
metadataContext.putFragmentContext(FRAGMENT_DISPOSABLE, staticMetadataManager.getMergedStaticDisposableMetadata()); metadataContext.setDisposableMetadata(staticMetadataManager.getMergedStaticDisposableMetadata());
if (StringUtils.hasText(staticMetadataManager.getTransHeaderFromEnv())) { if (StringUtils.hasText(staticMetadataManager.getTransHeaderFromEnv())) {
metadataContext.putContext(FRAGMENT_RAW_TRANSHEADERS, staticMetadataManager.getTransHeaderFromEnv(), ""); metadataContext.setTransHeaders(staticMetadataManager.getTransHeaderFromEnv(), "");
} }
METADATA_CONTEXT.set(metadataContext); METADATA_CONTEXT.set(metadataContext);
@ -132,17 +130,17 @@ public final class MetadataContextHolder {
// Save transitive metadata to ThreadLocal. // Save transitive metadata to ThreadLocal.
if (!CollectionUtils.isEmpty(dynamicTransitiveMetadata)) { if (!CollectionUtils.isEmpty(dynamicTransitiveMetadata)) {
Map<String, String> staticTransitiveMetadata = metadataContext.getFragmentContext(FRAGMENT_TRANSITIVE); Map<String, String> staticTransitiveMetadata = metadataContext.getTransitiveMetadata();
Map<String, String> mergedTransitiveMetadata = new HashMap<>(); Map<String, String> mergedTransitiveMetadata = new HashMap<>();
mergedTransitiveMetadata.putAll(staticTransitiveMetadata); mergedTransitiveMetadata.putAll(staticTransitiveMetadata);
mergedTransitiveMetadata.putAll(dynamicTransitiveMetadata); mergedTransitiveMetadata.putAll(dynamicTransitiveMetadata);
metadataContext.putFragmentContext(FRAGMENT_TRANSITIVE, Collections.unmodifiableMap(mergedTransitiveMetadata)); metadataContext.setTransitiveMetadata(Collections.unmodifiableMap(mergedTransitiveMetadata));
Map<String, String> mergedDisposableMetadata = new HashMap<>(dynamicDisposableMetadata); Map<String, String> mergedDisposableMetadata = new HashMap<>(dynamicDisposableMetadata);
metadataContext.putFragmentContext(FRAGMENT_UPSTREAM_DISPOSABLE, Collections.unmodifiableMap(mergedDisposableMetadata)); metadataContext.setUpstreamDisposableMetadata(Collections.unmodifiableMap(mergedDisposableMetadata));
Map<String, String> staticDisposableMetadata = metadataContext.getFragmentContext(FRAGMENT_DISPOSABLE); Map<String, String> staticDisposableMetadata = metadataContext.getDisposableMetadata();
metadataContext.putFragmentContext(FRAGMENT_DISPOSABLE, Collections.unmodifiableMap(staticDisposableMetadata)); metadataContext.setDisposableMetadata(Collections.unmodifiableMap(staticDisposableMetadata));
} }
MetadataContextHolder.set(metadataContext); MetadataContextHolder.set(metadataContext);
} }

@ -45,10 +45,10 @@ public class MetadataContextHolderTest {
customMetadata.put("a", "1"); customMetadata.put("a", "1");
customMetadata.put("b", "2"); customMetadata.put("b", "2");
MetadataContext metadataContext = MetadataContextHolder.get(); MetadataContext metadataContext = MetadataContextHolder.get();
metadataContext.putFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE, customMetadata); metadataContext.setTransitiveMetadata(customMetadata);
MetadataContextHolder.set(metadataContext); MetadataContextHolder.set(metadataContext);
customMetadata = MetadataContextHolder.get().getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); customMetadata = MetadataContextHolder.get().getTransitiveMetadata();
Assertions.assertThat(customMetadata.get("a")).isEqualTo("1"); Assertions.assertThat(customMetadata.get("a")).isEqualTo("1");
Assertions.assertThat(customMetadata.get("b")).isEqualTo("2"); Assertions.assertThat(customMetadata.get("b")).isEqualTo("2");
@ -60,7 +60,7 @@ public class MetadataContextHolderTest {
customMetadata.put("c", "3"); customMetadata.put("c", "3");
MetadataContextHolder.init(customMetadata, new HashMap<>()); MetadataContextHolder.init(customMetadata, new HashMap<>());
metadataContext = MetadataContextHolder.get(); metadataContext = MetadataContextHolder.get();
customMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); customMetadata = metadataContext.getTransitiveMetadata();
Assertions.assertThat(customMetadata.get("a")).isEqualTo("1"); Assertions.assertThat(customMetadata.get("a")).isEqualTo("1");
Assertions.assertThat(customMetadata.get("b")).isEqualTo("22"); Assertions.assertThat(customMetadata.get("b")).isEqualTo("22");
Assertions.assertThat(customMetadata.get("c")).isEqualTo("3"); Assertions.assertThat(customMetadata.get("c")).isEqualTo("3");

@ -22,7 +22,7 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/** /**
* Test for {@link ExpressionParserV1} * Test for {@link ExpressionParserV1}.
* @author lepdou 2022-10-08 * @author lepdou 2022-10-08
*/ */
public class ExpressionParserV1Test { public class ExpressionParserV1Test {

@ -22,7 +22,7 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
/** /**
* Test for {@link ExpressionParserV2} * Test for {@link ExpressionParserV2}.
* @author lepdou 2022-10-08 * @author lepdou 2022-10-08
*/ */
public class ExpressionParserV2Test { public class ExpressionParserV2Test {

@ -55,7 +55,7 @@ public class MetadataBackendController {
// Get Custom Metadata From Context // Get Custom Metadata From Context
MetadataContext context = MetadataContextHolder.get(); MetadataContext context = MetadataContextHolder.get();
Map<String, String> customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); Map<String, String> customMetadataMap = context.getTransitiveMetadata();
customMetadataMap.forEach((key, value) -> { customMetadataMap.forEach((key, value) -> {
LOG.info("Metadata Backend Custom Metadata (Key-Value): {} : {}", key, value); LOG.info("Metadata Backend Custom Metadata (Key-Value): {} : {}", key, value);

@ -69,7 +69,7 @@ public class MetadataFrontendController {
// Get Custom Metadata From Context // Get Custom Metadata From Context
MetadataContext context = MetadataContextHolder.get(); MetadataContext context = MetadataContextHolder.get();
Map<String, String> customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); Map<String, String> customMetadataMap = context.getTransitiveMetadata();
customMetadataMap.forEach((key, value) -> { customMetadataMap.forEach((key, value) -> {
LOG.info("Metadata Middle Custom Metadata (Key-Value): {} : {}", key, value); LOG.info("Metadata Middle Custom Metadata (Key-Value): {} : {}", key, value);
@ -116,7 +116,7 @@ public class MetadataFrontendController {
// Get Custom Metadata From Context // Get Custom Metadata From Context
MetadataContext context = MetadataContextHolder.get(); MetadataContext context = MetadataContextHolder.get();
Map<String, String> customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); Map<String, String> customMetadataMap = context.getTransitiveMetadata();
customMetadataMap.forEach((key, value) -> { customMetadataMap.forEach((key, value) -> {
LOG.info("Metadata Middle Custom Metadata (Key-Value): {} : {}", key, value); LOG.info("Metadata Middle Custom Metadata (Key-Value): {} : {}", key, value);

@ -92,7 +92,7 @@ public class MetadataMiddleController {
// Get Custom Metadata From Context // Get Custom Metadata From Context
MetadataContext context = MetadataContextHolder.get(); MetadataContext context = MetadataContextHolder.get();
Map<String, String> customMetadataMap = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); Map<String, String> customMetadataMap = context.getTransitiveMetadata();
customMetadataMap.forEach((key, value) -> { customMetadataMap.forEach((key, value) -> {
LOG.info("Metadata Middle Custom Metadata (Key-Value): {} : {}", key, value); LOG.info("Metadata Middle Custom Metadata (Key-Value): {} : {}", key, value);

@ -77,13 +77,13 @@ public class TrafficStainingGatewayFilter implements GlobalFilter, Ordered {
metadataContext = MetadataContextHolder.get(); metadataContext = MetadataContextHolder.get();
} }
Map<String, String> oldTransitiveMetadata = metadataContext.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE); Map<String, String> oldTransitiveMetadata = metadataContext.getTransitiveMetadata();
// append new transitive metadata // append new transitive metadata
Map<String, String> newTransitiveMetadata = new HashMap<>(oldTransitiveMetadata); Map<String, String> newTransitiveMetadata = new HashMap<>(oldTransitiveMetadata);
newTransitiveMetadata.putAll(stainedLabels); newTransitiveMetadata.putAll(stainedLabels);
metadataContext.putFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE, newTransitiveMetadata); metadataContext.setTransitiveMetadata(newTransitiveMetadata);
}).build(); }).build();
return chain.filter(exchange.mutate().request(request).build()); return chain.filter(exchange.mutate().request(request).build());

@ -115,8 +115,7 @@ public class EnhancedRestTemplateReporter extends AbstractPolarisReporterAdapter
private void reportResult(URI url, ClientHttpResponse response) { private void reportResult(URI url, ClientHttpResponse response) {
ServiceCallResult resultRequest = createServiceCallResult(url); ServiceCallResult resultRequest = createServiceCallResult(url);
try { try {
Map<String, String> loadBalancerContext = MetadataContextHolder.get() Map<String, String> loadBalancerContext = MetadataContextHolder.get().getLoadbalancerMetadata();
.getFragmentContext(MetadataContext.FRAGMENT_LOAD_BALANCER);
String targetHost = loadBalancerContext.get("host"); String targetHost = loadBalancerContext.get("host");
String targetPort = loadBalancerContext.get("port"); String targetPort = loadBalancerContext.get("port");

@ -17,7 +17,6 @@
package com.tencent.cloud.rpc.enhancement.resttemplate; package com.tencent.cloud.rpc.enhancement.resttemplate;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.MetadataContextHolder;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
@ -37,9 +36,8 @@ public final class LoadBalancerClientAspectUtils {
Object server = joinPoint.getArgs()[0]; Object server = joinPoint.getArgs()[0];
if (server instanceof ServiceInstance) { if (server instanceof ServiceInstance) {
ServiceInstance instance = (ServiceInstance) server; ServiceInstance instance = (ServiceInstance) server;
MetadataContextHolder.get().putContext(MetadataContext.FRAGMENT_LOAD_BALANCER, "host", instance.getHost()); MetadataContextHolder.get().setLoadbalancer("host", instance.getHost());
MetadataContextHolder.get() MetadataContextHolder.get().setLoadbalancer("port", String.valueOf(instance.getPort()));
.putContext(MetadataContext.FRAGMENT_LOAD_BALANCER, "port", String.valueOf(instance.getPort()));
} }
} }
} }

@ -54,7 +54,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** /**
* Test for {@link EnhancedRestTemplateReporter} * Test for {@link EnhancedRestTemplateReporter}.
* @author lepdou 2022-09-06 * @author lepdou 2022-09-06
*/ */
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
@ -82,7 +82,7 @@ public class EnhancedRestTemplateReporterTest {
Map<String, String> loadBalancerContext = new HashMap<>(); Map<String, String> loadBalancerContext = new HashMap<>();
loadBalancerContext.put("host", "1.1.1.1"); loadBalancerContext.put("host", "1.1.1.1");
loadBalancerContext.put("port", "8080"); loadBalancerContext.put("port", "8080");
when(metadataContext.getFragmentContext(MetadataContext.FRAGMENT_LOAD_BALANCER)).thenReturn(loadBalancerContext); when(metadataContext.getLoadbalancerMetadata()).thenReturn(loadBalancerContext);
mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class); mockedMetadataContextHolder = Mockito.mockStatic(MetadataContextHolder.class);
mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext); mockedMetadataContextHolder.when(MetadataContextHolder::get).thenReturn(metadataContext);

Loading…
Cancel
Save