Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Thrift client for multiple threads?

Tags:

I have a working Thrift client in the below snippet.

TTransport transport = new THttpClient(new Uri("http://localhost:8080/api/"));
TProtocol protocol = new TBinaryProtocol(transport);
TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "UserService");
UserService.Client userServiceClient = new UserService.Client(mp);
System.out.println(userServiceClient.getUserById(100));

When running the client within multi-threaded environment

threads[i] = new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println(userServiceClient.getUserById(someId));
    }
}

I got an exception: out of sequence response

org.apache.thrift.TApplicationException: getUserById failed: out of sequence response
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:76)

I guess the reason is that Thrift generated Client is not thread safe. But if I want multi-clients to call the same method getUserById() simultaneously, how can I make it?

like image 527
Rocherlee Avatar asked Jun 07 '16 08:06

Rocherlee


People also ask

Is thrift client thread safe?

I guess the reason is that Thrift generated Client is not thread safe. But if I want multi-clients to call the same method getUserById() simultaneously, how can I make it? Yes there is a connection pool for THttpClient but it doesn't fix the issue.

Can multiple threads write to the same file?

There are several patterns on how to allow multiple threads to write to the same file. the ReaderWriterLock class is invented for this purpose. Another classic is using semaphors and the lock statement to lock a shared resource.

What is multi threaded client?

In a multithreaded application, multiple service-dispatched threads are available in the same server, which means that fewer servers need to be started for that application. The following diagram shows how a server process can dispatch multiple threads to different clients simultaneously.

How can multiple threads share a resource?

In a multithreaded server, it is possible for shared resources to be accessed concurrently. In addition to scope object attributes, shared resources include in-memory data (such as instance or class variables) and external objects such as files, database connections, and network connections.


1 Answers

Thrift clients are not designed to be shared across threads. If you need multiple client threads, set up one Thrift client per thread.

But if I want multi-clients to call the same method getUserById() simultaneously, how can I make it?

We don't know much about the context, so I have to guess a bit. If the issue is that there are a lot of such calls coming in at a time, a possible solution could be to group calls to save roundtrip time:

service wtf {
  list<string>  getUsersById( 1 : list<int> userIds)
}

That's just a short idea. Maybe you want to return list<user_data_struct> instead. For practical reasons I would also recommend to wrap the returned list into a struct, so the whole thing becomes extensible.

like image 61
JensG Avatar answered Nov 01 '22 12:11

JensG