Behind the Curtain: Thrifty Development

As we move to systems that scale even better and faster than our current servers, we have begun to move towards using Apache Thrift as our communication layer more and more. ?Today I’m tinkering with it to try and build out our existing API, replacing with a more RESTful interface.

As a somewhat less-used architecture, there’s not a lot out there about Thrift. ?In particular, not very much about implementing their PHP Client is available out there, even the wiki page for PHP examples is conspicuously empty. ?There is even less in regards to the latest 0.9.0 release, which is already over a year old at this point.

There are a couple of tutorials. ?One of the main ones being referenced is?http://chanian.com/2010/05/13/thrift-tutorial-a-php-client/, which is already 3 years old. ?It uses 0.2.0, which has a vastly different directory structure than the current one. ?Still, it allowed me to figure out at least that the library files are all housed in the lib folder of the Thrift tarball.

The second tutorial I came across was?http://sleeplesscodingnights.blogspot.com/2012/11/installing-thrift-090-for-java-server.html, which does deal with Thrift 0.9.0 and implementing a Java Server with a PHP Client. ?However, at Step 7, I found myself getting frustrated with the many files that were required in the Thrift generated classes.

My particular classes required a few more than the ones specified in the tutorial. ?Over and over the php interpreter would fail on one class, and I would diligently find it in the lib/php/lib/Thrift/ folder and add another require_once statement. ?After about 5 such classes, I decided there must be a better way and started poking around the tarball to see what else I could find that could guide me in making my simple RESTful client work.

I find myself digging into the tutorials under tutorial/php to glean what I can about the way in which the dependencies must be loaded, and have found that Thrift PHP now uses a ClassLoader to load in the classes, as shown by the php example included with the 0.9.0 release.

//This line is the path to the ClassLoader.
//Replace it with the full path to wherever you put the php libraries
require_once __DIR__.'/../../lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
//This is the path to your generated php files,
    which were originally put in a gen-php folder.
//Replace it with the full path to wherever you put the generated files
$GEN_DIR = realpath(dirname(__FILE__).'/..').'/gen-php';
$loader = new ThriftClassLoader();
//This registers the base 'Thrift' namespace of all the Thrift library files
//Replace the second parameter with the full path to the parent folder of your Thrift library folders
$loader->registerNamespace('Thrift', __DIR__ . '/../../lib/php/lib');
//This is unnecessary for your project, 
    it is a tutorial folder of generated classes under the 'shared' namespace
$loader->registerDefinition('shared', $GEN_DIR);
//This is the way in which your generated files are loaded.
//Replace 'tutorial' with your namespace
$loader->registerDefinition('tutorial', $GEN_DIR);
$loader->register();

Once that was completed, all I had to change was the Protocol since Thrift’s default Server implementations use TFramedTransport and the Thrift example uses TBufferedTransport.

Overall, this took about 3 hours to finally completely piece together. ?Hopefully this will save people a bit of time in implementing their own PHP Thrift Client.

If you want to try out some Thrifty development of your own, our Mule Server project may be a good starting point. ?It incorporates a fast in memory cache with a Thrift Service, and the generic example is for quickly retrieving and caching authentication requests. ?https://github.com/active911/mule-server

Leave a Reply

Your email address will not be published. Required fields are marked *