上周五做大流程调试的时候,线下test环境QA发现andriod端一访问就crash的问题。QA在群里@all,crash已经阻塞测试,于是我就去排查。Andrioa端同学说crash的原因是因为后端传过来的字段不符合约定。原本约定的rightlabels字段是一维数组:
"rightlabels":[]
传来过的确实二维数组:
"rightlabels":[[]]
这个rightlabels是我组装传过去的,没错。但我不会组装二维的list,怎么就出现了二维的list呢?最后发现我写的java代码在异常情况下会传:
"rightlabels":[{}]
一个一维的list里面放了一个空对象过去。即使这样也不应该出现list里面套list啊。从日志来看,确实是我这边传了"rightlabels":[{}]
过去导致的crash,但不解的是为什么会变成二维度的list。联系调用方,他们说自己对这个字段只做了透传,不做任何二次封装什么的,我的服务调用方是PHP编写的,我感觉可能是一个跨语言解析上的错误。
为了复现这个情况,我本地搭建了thrift环境,php编写client端,java编写server端 (注:下面的代码不是工程的全部,是部分主要代码)。
Java Server端:
package com.meituan.thrift.demo;
import org.apache.thrift.TException;
public class HelloWorldImpl implements HelloWorldService.Iface{
public HelloWorldImpl() {
}
public String sayHello(String username) throws TException {
System.out.println(username);
return "{\"rightlabels\":[{}]}";
}
}
package com.meituan.thrift.demo;
import com.sun.tools.javac.util.Pair;
import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import java.io.IOException;
public class HelloServer {
public final static int SERVER_PORT = 7099;
private static String SERVER_IP = "127.0.0.1";
public void startServer() {
try{
System.out.println("HelloWorld Server Start ...");
TServerSocket serverSocket = new TServerSocket(SERVER_PORT);
TServer.Args args = new TServer.Args(serverSocket);
TProcessor processor = new HelloWorldService.Processor(new HelloWorldImpl());
TBinaryProtocol.Factory portFactory = new TBinaryProtocol.Factory(true,true);
args.processor(processor);
args.protocolFactory(portFactory);
TServer server = new TSimpleServer(args);
server.serve();
} catch(Exception e) {
System.out.println("Server Start Error...");
e.printStackTrace();
}
}
public static void main(String[] args) {
HelloServer server = new HelloServer();
server.startServer();
}
}
Php Client端:
<?php
require_once __DIR__.'/thrift/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
$GEN_DIR = realpath(dirname(__FILE__)).'/gen-php';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__ . '/thrift/lib/php/lib');
$loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('com', $GEN_DIR);
$loader->register();
try {
$socket = new Thrift\Transport\TSocket('localhost', 7099);
$transport = new Thrift\Transport\TBufferedTransport($socket, 1024, 1024);
$protocol = new Thrift\Protocol\TBinaryProtocolAccelerated($transport);
$client = new com\meituan\thrift\demo\HelloWorldServiceClient($protocol);
$transport->open();
$result = $client->sayHello("TaoMin HelloWorld ");
$resultObj = json_decode($result, true);
if ( isset($resultObj['rightlabels']) ) {
var_dump($resultObj);
}
var_dump(json_encode($resultObj));
} catch (TException $tx) {
print 'TException: '.$tx->getMessage()."\n";
}
Java传过去的是:"{\"rightlabels\":[{}]}"
。最后发现,是因为php先json_decode传过来的json,然后再json_encode,这个过程中,Java传来的空对象被php解析成了空数组,然后再encode就变成了图上红框看到的那样了。
一句话总结:跨语言配合需谨慎....
end ~