Skip to content

Commit

Permalink
feat:optimize thread local (sofastack#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
leizhiyuan authored Jun 11, 2020
1 parent 5c1e4a2 commit 8735b43
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.alipay.common.tracer.core.context.trace.SofaTraceContext;
import com.alipay.common.tracer.core.holder.SofaTraceContextHolder;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.sofa.rpc.context.RpcInvokeContext;
import com.alipay.sofa.rpc.context.RpcRunningState;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
import com.alipay.sofa.rpc.core.request.SofaRequest;
Expand All @@ -39,8 +38,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.alipay.sofa.rpc.server.triple.TripleContants.SOFA_REQUEST_KEY;

/**
* 服务端收请求Header的拦截器
* <p>
Expand All @@ -65,13 +62,13 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<Re
final Throwable[] throwable = { null };
SofaRequest sofaRequest = new SofaRequest();
TripleTracerAdapter.serverReceived(sofaRequest, serverServiceDefinition, call, requestHeaders);

SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();

Context ctxWithSpan = Context.current()
.withValue(TracingContextKey.getKey(), serverSpan)
.withValue(TracingContextKey.getSpanContextKey(), serverSpan.context());
.withValue(TracingContextKey.getSpanContextKey(), serverSpan.context())
.withValue(TracingContextKey.getKeySofaRequest(), sofaRequest);

//这里和下面不在一个线程
if (RpcRunningState.isDebugMode()) {
Expand Down Expand Up @@ -142,7 +139,6 @@ public void onHalfClose() {
}
// 服务端收到所有信息
TripleTracerAdapter.serverReceived(sofaRequest, serverServiceDefinition, call, requestHeaders);
RpcInvokeContext.getContext().put(SOFA_REQUEST_KEY, sofaRequest);
try {
super.onHalfClose();
} catch (Throwable t) {
Expand All @@ -160,7 +156,6 @@ private StatusRuntimeException fromThrowable(Throwable t) {
return new StatusRuntimeException(Status.UNKNOWN, trailers);
}
};

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import com.alipay.sofa.rpc.codec.SerializerFactory;
import com.alipay.sofa.rpc.common.utils.ClassUtils;
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.context.RpcInvokeContext;
import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.tracer.sofatracer.TracingContextKey;
import com.alipay.sofa.rpc.transport.ByteArrayWrapperByteBuf;
import com.google.protobuf.ByteString;
import com.google.protobuf.ProtocolStringList;
import io.grpc.Context;
import io.grpc.stub.StreamObserver;
import triple.GenericServiceGrpc;
import triple.Request;
Expand Down Expand Up @@ -57,7 +58,9 @@ public GenericServiceImpl(ProviderConfig providerConfig) {

@Override
public void generic(Request request, StreamObserver<Response> responseObserver) {
SofaRequest sofaRequest = (SofaRequest) RpcInvokeContext.getContext().get(SOFA_REQUEST_KEY);

SofaRequest sofaRequest = TracingContextKey.getKeySofaRequest().get(Context.current());

String methodName = sofaRequest.getMethodName();
Class[] argTypes = getArgTypes(request);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@
*/
package com.alipay.sofa.rpc.tracer.sofatracer;

import com.alipay.sofa.rpc.core.request.SofaRequest;
import io.grpc.Context;
import io.opentracing.Span;
import io.opentracing.SpanContext;

public class TracingContextKey {

public static final String KEY_NAME = "io.opentracing.active-span";
public static final String KEY_CONTEXT_NAME = "io.opentracing.active-span-context";
private static final Context.Key<Span> key = Context.key(KEY_NAME);
private static final Context.Key<SpanContext> keyContext = Context.key(KEY_CONTEXT_NAME);
public static final String KEY_NAME = "io.opentracing.active-span";
public static final String KEY_CONTEXT_NAME = "io.opentracing.active-span-context";
private static final Context.Key<Span> key = Context.key(KEY_NAME);
private static final Context.Key<SpanContext> keyContext = Context.key(KEY_CONTEXT_NAME);
public static final String KEY_SOFA_REQUEST_NAME = "io.opentracing.sofa-request";
private static final Context.Key<SofaRequest> keySofaRequest = Context.key(KEY_SOFA_REQUEST_NAME);

/**
* Retrieves the active span.
Expand Down Expand Up @@ -57,4 +60,8 @@ public static Context.Key<SpanContext> getSpanContextKey() {
public static SpanContext activeSpanContext() {
return keyContext.get();
}

public static Context.Key<SofaRequest> getKeySofaRequest() {
return keySofaRequest;
}
}

0 comments on commit 8735b43

Please sign in to comment.