Data pass-through

更新时间:
复制 MD 格式

The data pass-through feature allows applications to store data in the invocation context. Any application in the call chain can then access this data.

You can pass data through the trace by adding it to the request and response of a call. You can then retrieve this data from the trace. The following code shows an example of how to do this:

RpcInvokeContext.getContext().putRequestBaggage("key_request","value_request");
RpcInvokeContext.getContext().putResponseBaggage("key_response","value_response");

String requestValue=RpcInvokeContext.getContext().getRequestBaggage("key_request");
String responseValue=RpcInvokeContext.getContext().getResponseBaggage("key_response");

Usage example

For example, in a call chain from service A to B to C, the request baggage set by A is passed to B and C. The response baggage from C and B is then passed back to A.

Caller A settings:

// Set the request baggage value before making the call.
RpcInvokeContext context =RpcInvokeContext.getContext();
context.putRequestBaggage("reqBaggageB","a2bbb");
// Make the call.
String result = service.hello();
// Get the response baggage value.
context.getResponseBaggage("respBaggageB");

Service B code:

public String hello(){
    // Get the request baggage value.
    RpcInvokeContext context =RpcInvokeContext.getContext();
    String reqBaggage = context.getRequestBaggage("reqBaggageB");
    doSomething();
    // Pass a value through the response baggage.
    context.putResponseBaggage("respBaggageB","b2aaa");
    return result;
}

If you start a subthread, you must set the context for the subthread:

CountDownLatch latch =new CountDownLatch(1);
finalRpcInvokeContext parentContext =RpcInvokeContext.peekContext();
Thread thread =new Thread(new Runnable(){
    public void run(){
          try{
                RpcInvokeContext.setContext(parentContext);
                // Call a remote service.
                xxxService.sayHello();
                latch.countDown();
          }finally{
               RpcInvokeContext.removeContext();
          }
    }
},"new-thread");
thread.start();

// You cannot get the response baggage data at this point.
latch.await(); // Wait
// After the call returns, you can get the response baggage value.

Comparison with SOFATracer

SOFATracer is an open source distributed tracing system from Ant Group. The Remote Procedure Call (RPC) framework has integrated SOFATracer, and this feature is enabled by default.

The differences between the data pass-through feature in the RPC framework and SOFATracer are as follows:

  • The RPC data pass-through feature is designed for business use cases and supports bidirectional data transfer. A caller can pass data to a service, and the service can return data to the caller. In contrast, SOFATracer is designed for middleware to pass data that is transparent to business logic, and it only supports unidirectional data transfer.

  • With the RPC framework, you can choose not to pass data through the entire call chain. In contrast, SOFATracer propagates all data throughout the chain. Passing large amounts of data with SOFATracer may affect downstream services.

Both approaches have advantages and disadvantages. Use the RPC data pass-through feature when you need to pass business-related data.