使用 thrift 遇到的一个 bug

org.apache.thrift.protocol.TProtocolException: Required field 'statusCode' was not found in serialized data! Struct: com.meituan.itc.udm.mustang.thrift.JudgeResponse$JudgeResponseStandardScheme@689774fc
	at com.meituan.itc.udm.mustang.thrift.JudgeResponse$JudgeResponseStandardScheme.read(JudgeResponse.java:789)
	at com.meituan.itc.udm.mustang.thrift.JudgeResponse$JudgeResponseStandardScheme.read(JudgeResponse.java:692)
	at com.meituan.itc.udm.mustang.thrift.JudgeResponse.read(JudgeResponse.java:603)
	at com.meituan.itc.udm.mustang.thrift.MustangService$judgeUserCondition_result$judgeUserCondition_resultStandardScheme.read(MustangService.java:1119)
	at com.meituan.itc.udm.mustang.thrift.MustangService$judgeUserCondition_result$judgeUserCondition_resultStandardScheme.read(MustangService.java:1104)
	at com.meituan.itc.udm.mustang.thrift.MustangService$judgeUserCondition_result.read(MustangService.java:1055)
	at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78)
	at com.meituan.itc.udm.mustang.thrift.MustangService$Client.recv_judgeUserCondition(MustangService.java:96)
	at com.meituan.itc.udm.mustang.thrift.MustangService$Client.judgeUserCondition(MustangService.java:83)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.meituan.service.mobile.mtthrift.client.invoker.MTThriftMethodInterceptor.syncRpcInvoke(MTThriftMethodInterceptor.java:417)
	at com.meituan.service.mobile.mtthrift.client.invoker.MTThriftMethodInterceptor.getRpcResult(MTThriftMethodInterceptor.java:360)
	at com.meituan.service.mobile.mtthrift.client.invoker.MTThriftMethodInterceptor.doInvoke(MTThriftMethodInterceptor.java:259)
	at com.meituan.dorado.rpc.handler.filter.InvokeChainBuilder$1.handle(InvokeChainBuilder.java:40)
	at com.meituan.trip.resilience.hystrix.extension.HystrixMTThriftInvokerFilter.filter(HystrixMTThriftInvokerFilter.java:80)
	at com.meituan.dorado.rpc.handler.filter.InvokeChainBuilder$3.handle(InvokeChainBuilder.java:87)
	at com.meituan.service.mobile.mtthrift.client.invoker.MTThriftMethodInterceptor.invoke(MTThriftMethodInterceptor.java:127)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
	at com.sun.proxy.$Proxy123.judgeUserCondition(Unknown Source)
	at com.meituan.fe.evolve.activity.test.T.testPersona(T.java:44)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

在 thrift 中,对于参数和返回值为 struct 的,编解码都是相应的 struct 对应的类。

在这里插入图片描述
而 struct.validate() 只检查引用类型。

 public void validate() throws org.apache.thrift.TException {
    // check for required fields
    // alas, we cannot check 'statusCode' because it's a primitive and you chose the non-beans generator.
    if (judgeResults == null) {
      throw new org.apache.thrift.protocol.TProtocolException("Required field 'judgeResults' was not present! Struct: " + toString());
    }
  }

com.meituan.itc.udm.mustang.thrift.JudgeResponse.JudgeResponseStandardScheme#write

没有对 statusCode 的检查。

读取 TType.MAP 过后直接读取到 TType.STOP(对应于oprot.writeFieldStop())

statusCode 编号为 -1 ?

说明,它的 thrift 文件 statusCode 索引为0。

使用 thrift 遇到的一个 bug_第1张图片
编号为 0代表字段的结束

namespace java com.meituan.model

struct Pair {
        0: required string key;
        1: required string value;
}

这时候 key 的编号为-1,value 的编号为1。

JudgeResponse 的写入在服务端,调用的是如下的 write 方法,对于 statusCode 一定是写入的。


那么该 bug 又是怎么出现的呢?

tcpdump 抓包使用 thrift 遇到的一个 bug_第2张图片
0c success 的类型 STRUCT

00 00 success 的索引 0

0d judgeResults 的类型 MAP

00 01 judgeResults 的索引 1

00 00 字段结束标记

确实没有 statusCode 属性。
根源:

使用 thrift 遇到的一个 bug_第3张图片
而最新版本加入了 statusCode 字段,而且索引设置为0=。=

你可能感兴趣的:(thrift)