4

I'm pretty puzzled with this issue. I have an Apache Thrift 0.9.0 client and server. The client code goes like this:

this.transport = new TSocket(this.server, this.port);
final TProtocol protocol = new TBinaryProtocol(this.transport);
this.client = new ZKProtoService.Client(protocol);

This works fine. However, if I try to wrap the transport in a TFramedTransport

this.transport = new TSocket(this.server, this.port);
final TProtocol protocol = new TBinaryProtocol(new TFramedTransport(this.transport));
this.client = new ZKProtoService.Client(protocol);

I get the following obscure (no explanation message whatsoever) exception in the client side. Server side shows no error.

org.apache.thrift.transport.TTransportException
    at org.apache.thrift.transport.TIOStreamTransport.read(TIOStreamTransport.java:132)
    at org.apache.thrift.transport.TTransport.readAll(TTransport.java:84)
    at org.apache.thrift.transport.TFramedTransport.readFrame(TFramedTransport.java:129)
    at org.apache.thrift.transport.TFramedTransport.read(TFramedTransport.java:101)
    at org.apache.thrift.transport.TTransport.readAll(TTransport.java:84)
    at org.apache.thrift.protocol.TBinaryProtocol.readAll(TBinaryProtocol.java:378)
    at org.apache.thrift.protocol.TBinaryProtocol.readI32(TBinaryProtocol.java:297)
    at org.apache.thrift.protocol.TBinaryProtocol.readMessageBegin(TBinaryProtocol.java:204)
    at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:69)
    at com.blablabla.android.core.device.proto.ProtoService$Client.recv_open(ProtoService.java:108)
    at com.blablabla.android.core.device.proto.ProtoService$Client.open(ProtoService.java:95)
    at com.blablabla.simpleprotoclient.proto.ProtoClient.initializeCommunication(ProtoClient.java:411)
    at com.blablabla.simpleprotoclient.proto.ProtoClient.doWork(ProtoClient.java:269)
    at com.blablabla.simpleprotoclient.proto.ProtoClient.run(ProtoClient.java:499)
    at java.lang.Thread.run(Thread.java:724)

It also fails if I use TCompactProtocol instead of TBinaryProtocol.

In the server side I have extended TProcessor with my own class since I need to reuse existing service handler (the service server-side IFace implementation) for this client:

@Override
public boolean process(final TProtocol in, final TProtocol out)
        throws TException {
    final TTransport t = in.getTransport();
    final TSocket socket = (TSocket) t;
    socket.setTimeout(ProtoServer.SOCKET_TIMEOUT);
    final String clientAddress = socket.getSocket().getInetAddress()
            .getHostAddress();
    final int clientPort = socket.getSocket().getPort();
    final String clientRemote = clientAddress + ":" + clientPort;
    ProtoService.Processor<ProtoServiceHandler> processor = PROCESSORS
            .get(clientRemote);
    if (processor == null) {
        final ProtoServiceHandler handler = new ProtoServiceHandler(
                clientRemote);
        processor = new ProtoService.Processor<ProtoServiceHandler>(
                handler);
        PROCESSORS.put(clientRemote, processor);
        HANDLERS.put(clientRemote, handler);
        ProtoClientConnectionChecker.addNewConnection(clientRemote,
                socket);
    }
    return processor.process(in, out);
}

And this is how I start the server side:

TServerTransport serverTransport = new TServerSocket(DEFAULT_CONTROL_PORT);
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(
            serverTransport).processor(new ControlProcessor()));
Thread thControlServer = new Thread(new StartServer("Control", server));
thControlServer.start();

I have some questions:

  • Is it correct to reuse service handler instances or I shouldn't be doing this?
  • Why does it fail when I use TFramedTransport or TCompactProtocol? How to fix this?

Any help on this issue is welcome. Thanks in advance!

m0skit0
  • 25,268
  • 11
  • 79
  • 127

2 Answers2

4

I was having the same problem and finally found the answer. It is possible to set the transport type on the server, though this is not clear from most tutorials and examples I've found on the web. Have a look at all of the methods of the TServer.Args class (or the args classes for other servers, which extend TServer.Args). There are methods inputTransportFactory and outputTransportFactory. You can use new TFramedTransport.Factory() as inputs to each of these methods to declare which transport the server should use. In scala:

  val handler = new ServiceStatusHandler
  val processor = new ServiceStatus.Processor(handler)
  val serverTransport = new TServerSocket(9090)
  val args = new TServer.Args(serverTransport)
    .processor(processor)
    .inputTransportFactory(new TFramedTransport.Factory)
    .outputTransportFactory(new TFramedTransport.Factory)
  val server = new TSimpleServer(args)
  println("Starting the simple server...")
  server.serve()

Note that if you are using a TAsyncClient, you have no choice about the transport that you use. You must use TNonblockingTransport, which has only one standard implementation, TNonblockingSocket, which internally wraps whatever protocol you are using in a framed transport. It doesn't actually wrap your chosen protocol in a TFramedTransport, but it does prepend the length of the frame to the content that it writes, and expects the server to prepend the length of the response as well. This wasn't documented anywhere I found, but if you look at the source code and experiment with different combinations, you will find that with TSimpleServer you must use TFramedTransport to get it to work with an async client.

By the way, it's also worth noting that the docs say that a TNonblockingServer must use TFramedTransport in the outermost later of the transport. However, the examples don't show this being set in TNonblockingServer.Args, yet you still find that you must use TFramedTransport on the client side to successfully execute an rpc on the server. This is because TNonblockingServer.Args has its input and output protocols set to TFramedTransport by default (you can see this using reflection to inspect the fields of the superclass hierarchy or in the source code for the constructor of AbstractNonblockingServerArgs -- you can override the input and output transports, but the server will likely fail for the reasons discussed in the documentation).

jonderry
  • 23,013
  • 32
  • 104
  • 171
  • What else did I say? **Always use the exact same protocol/transport stack on both ends**! – JensG Jul 18 '14 at 15:37
2

When the issue happens with framed, but it works without framed, then you have an incompatible protocol stack on both ends. Choose one of the following:

  • either modify the server code to use framed as well
  • or do not use framed on the client

A good rule of thumb is, to always use the exact same protocol/transport stack on both ends. In the particular case it blows up, because framed adds a four-byte header holding the size of the message that follows. If the server does not use framed, these additional four bytes sent by the client will be interpreted (wrongly) as part of the message.

Altough the sample code in that answer TNonblockingServer in thrift crashes when TFramedTransport opens is for C++, adding framed on the server should be very similar with Java.

PS: Yes, it is perfectly ok to re-use your handler. A typical handler is a stateless thing.

Community
  • 1
  • 1
JensG
  • 13,148
  • 4
  • 45
  • 55
  • 1
    Thanks for the answer. I didn't know you can define server side transport. I don't really know how to do that. [AFAIK](http://people.apache.org/~thejas/thrift-0.9/javadoc/org/apache/thrift/transport/TServerTransport.html) there are only 2 server transport classes while there are [several more](http://people.apache.org/~jfarrell/thrift/0.6.1/javadoc/org/apache/thrift/transport/TTransport.html) at the client side. I've edited my question to include the server side transport part. – m0skit0 Nov 22 '13 at 19:48
  • Hmmm I see I should be using [this server](http://people.apache.org/~jfarrell/thrift/0.6.1/javadoc/org/apache/thrift/server/THsHaServer.html) instead, right? – m0skit0 Nov 22 '13 at 19:58
  • Regarding transports, there are two kinds of them. *Endpoint* transports, which connect to an underlying medium, like stream transport, sockets, and *layered* transports like framed, buffered, etc. The latter are not related to a specific medium, they act merely as a kind of software "filter". At least in theory, layered transports can be stacked in any combination onto an endpoint transport. - Regarding servers, these are just different implementatations with different characteristics. The server itself does not affect protocol or transport. – JensG Nov 22 '13 at 20:06
  • 1
    *"The server itself does not affect protocol or transport."* But [documentation for TNonblockingServer](http://people.apache.org/~jfarrell/thrift/0.6.1/javadoc/org/apache/thrift/server/TNonblockingServer.html) states that *"Clients must also use TFramedTransport."* – m0skit0 Nov 22 '13 at 20:11