Skip to content

Thrift proxy

Regunath B edited this page Sep 13, 2013 · 7 revisions

The Thrift proxy leverages the Phantom extensibility support as described in Phantom architecture. A simpler example for Thrift proxy is available on the Getting Started and Examples page. The Thrift proxy requires following capabilities for it to behave as a totally transparent proxy for clients:

  • TCP socket listener - that can receive Thrift requests. This is available as TCPNettyServer in the runtime module.
  • Thrift Codecs & Request processing - processes Thrift commands and relays it to the target server. This is available in the channel-handler-thrift module
  • Resilience and fault tolerance integration - to support timeouts, synchronous and asynchronous invocation and fallbacks. This is available in the task-thrift module.

Follow the instructions below to configure your Thrift proxy:

  • Phantom uses Java Reflection to determine method signatures of the target Thrift service. It follows the code generation scheme provided by Thrift 0.9 IDL compiler. Note: The dependency on Thrift 0.9 is only compile time. The target service may run on any Thrift version (versions 0.2 through 0.9). Follow these steps to expose your Thrift interface:
    • Generate Java sources by running the Thrift 0.9 IDL compiler on the target service's Thrift IDL file.
    • Add the Java sources or compiled binaries archive(.jar) to your Maven module's dependency. For example see ArithmeticService source added to the sample-thrift-proxy module source.
  • Create a file by name spring-proxy-listener-config.xml and configure the TCP socket listener, the Channel pipeline factory, Thrift proxy handler, and the Phantom registry and repository beans (or) simply copy this file from sample-thrift-proxy Maven module. Note: the path src/main/resources/external must be preserved as the location for this file. The following bean configuration is worth noting:
<bean id="thriftChannelHandler" class="com.flipkart.phantom.runtime.impl.server.netty.handler.thrift.ThriftChannelHandler" scope="prototype">    
    <property name="repository" ref="thriftProxyRepository" />
    <property name="thriftProxy" value="thrift.ArithmeticService" />
</bean>

Notice here that the fully qualified name of the target Thrift service i.e. thrift.ArithmeticService is configured. Phantom inspects and stores method signatures of this interface during startup.

  • Create a file by name spring-proxy-handler-config.xml in the same location and configure the proxy beans like:
<bean id="sampleThriftProxy" class="com.flipkart.phantom.thrift.impl.proxy.DefaultThriftProxy">
    <property name="thriftServiceClass" value="thrift.ArithmeticService"/>
    <property name="thriftServer" value="localhost" />
    <property name="thriftPort" value="8082" />
    <property name="thriftTimeoutMillis" value="1000" />
    <property name="proxyExecutorTimeout" value="500"/>
    <property name="proxyThreadPoolSize" value="50"/>
</bean>

The following configuration options and features are worth noting:

  • The fully qualified name of the target Thrift service.
  • The host address where the target Thrift service is deployed. May also point to the load balancer fronting a cluster of servers.
  • The port where the target Thrift service is running
  • The Thrift service call timeout i.e. socket timeout in executing the call
  • The execution timeout i.e. the maximum total latency that is acceptable after which the call fails
  • The proxy's thread pool size which controls maximum concurrent executions and after which calls are rejected