yanghao605 opened a new issue, #4035: URL: https://github.com/apache/servicecomb-java-chassis/issues/4035
问题背景:  消费端的Handler链如上 其中cse-demo-handler是自定义的handler,代码如下: ``` public class CseHandler implements Handler { private static final Logger LOGGER = LoggerFactory.getLogger(CseHandler.class); @Override public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception { LOGGER.info("CseHandler access, operation: {}", invocation.getMicroserviceQualifiedName()); TransferContextHelper.injectCommonContext(invocation, InvocationContext::addContext); invocation.next(asyncResp); } } ``` 大体能力就是基于自定义的Handler往Invocation的Context中塞入一些自定义的上下文 然后再高并发场景下,出现了最后发送的请求中上下文不全,之前设置的一些key-value丢失了,检测方法是自定一个HttpClientFilter,在发送请求之前检查Invocation的上下文  问题分析: 当前分析问题应该是出在tracing-consumer上的ZipkinConsumerDelegate中的injector实现上, ``` class ZipkinConsumerDelegate implements ZipkinTracingDelegate { private final HttpClientHandler<HttpClientRequest, HttpClientResponse> handler; private final HttpTracing httpTracing; private final Injector<Invocation> injector; private final HttpClientResponseWrapper responseWrapper; private final HttpClientRequestWrapper requestWrapper; ZipkinConsumerDelegate(HttpTracing httpTracing) { this.httpTracing = httpTracing; // 这里的HttpTracing是CSE中的一个Bean对象,是多线程共用的 this.injector = httpTracing.tracing().propagation().injector(injector()); this.handler = HttpClientHandler.create(httpTracing); this.responseWrapper = new HttpClientResponseWrapper(); this.requestWrapper = new HttpClientRequestWrapper(); } @Override public Span createSpan(Invocation invocation) { return handler.handleSend(requestWrapper.invocation(invocation)); } @Override public void onResponse(Span span, Response response, Throwable error) { handler.handleReceive(responseWrapper.response(response).throwable(error).request(requestWrapper), span); } @Override public String name() { return "Zipkin consumer"; } @Override public Tracing tracer() { return httpTracing.tracing(); } private Setter<Invocation, String> injector() { // 这里会对Invocation的Context进行修改 return (invocation, key, value) -> invocation.getContext().put(key, value); } } ``` 当前看应该是由于多线程共用了同一个httpTracing,从而使用了HttpTracing中的injector方法,向Invocation的context中塞入相关字段,由于Invocation的context是hashMap的,线程不安全,从而导致context中数据丢失 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
