Friday 11 October 2013

Apache Thrift Tutorial

1 comment

Introduction

       Apache thrift is a tool developed at Facebook which is used for automatically creating remote servers in Remote Procedure Call (RPC) in different languages. Thrift means economical, since it is saving time and cost for creating remote servers in RPC.  Apache thrift support many different languages . Apache thrift won't create client, you require to create the client program manually. Apache thrift is an Interface Definition language (IDL) used to create services in a different language.

Installation 

On Ubuntu:
       You can install all dependencies by giving only one single command as shown below
sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev 

OR

       If there is an error in above command please check due to which dependency error is occurring.
  • First check whether your g++ version is greater or equal to 4.2 you may check the version with the command given below
    g++ --version   (This should be >= 4.2)
  • Install lex and yacc using command given below
    sudo apt-get install byacc flex
  • Install boost library using command given below
    sudo apt-get install libboost-all-dev
  • Install autoconf which is require for building the source
    sudo apt-get install autoconf
  • Install  automake
    sudo apt-get install automake
  • Install libtool
    sudo apt-get install libtool
  • install libssl-dev
     sudo apt-get install libssl-dev
  • Install pkg-config
    sudo apt-get install pkg-config
  • Install libevent-dev
    sudo apt-get install libevent-dev
  • Install bison
    sudo apt-get install bison

       Now download the apache thrift and extract it. After extracting open terminal and go to the path where thrift is extracted and give the following commands
./configure
make
sudo make install
       You can check whether apache thrift is installed properly or not by giving command below. If you are able to see the version of thrift then your installation is completed successful.
thrift --version

Use

      As explain before we can create source codes for many languages without installation of that language, but to run created source code you will require to install those language. To create remote server you will require to create a file with extension thrift. This thrift file contain thrift interface description language we will explain this in short for detail refer link.

Lets create one example which performs basic arithmetic operations Thrift_IDL.thrift.

#this is comment
#!/usr/local/bin/thrift --gen cpp

namespace cpp Test #this indicates that given thrift is applicable to cpp 

service arithmetic_op  #create class arithmetic_opHandler : virtual public arithmetic_opIf with following public function
{       
  i32 add(1:i32 num1, 2:i32 num2),  #this create function with int32_t add(const int32_t num1, const int32_t num2)
  i32 sub(1:i32 num1, 2:i32 num2),  #this create function with int32_t sub(const int32_t num1, const int32_t num2) 
  i32 mul(1:i32 num1, 2:i32 num2),  #this create function with int32_t mul(const int32_t num1, const int32_t num2) 
  i32 div(1:i32 num1, 2:i32 num2),  #this create function with int32_t div(const int32_t num1, const int32_t num2)
}

service arithmetic_Demo      #this is second service for dispay
{
i32 display(1:i32 num1, 2:i32 num2)
}
To generate server for this file you require to give the command
thrift --gen cpp Thrift_IDL.thrift
       This command will generate one folder with name "gen-cpp" which contain 10 files as shown in the figure below. This number of file and the format of files changes as the language changes, here we are considering the files for cpp.
Apache Thrift
  
       Since we don't define any struct or enum declared in thrift file, the file Thrift_IDL_types.h, Thrift_IDL_types.cpp are only initialized but not used.

Modify Functions declared in Services(.thrift file):
       Functions declared in the services of thrift file are only initialized by thrift. You require to add the contain to perform some action you require.
       We will take example of arithmetic_op service: thrift will create arithmetic_op_server.skeleton.cpp file which contain main function and thrift will create arithmetic_opHandler class which contain constructor and the number of functions declared in the thrift file as shown below.
class arithmetic_opHandler : virtual public arithmetic_opIf {
 public:
  arithmetic_opHandler() {
    // Your initialization goes here
  }

  int32_t add(const int32_t num1, const int32_t num2) {
    // Your implementation goes here
    printf("add\n");
  }

  int32_t sub(const int32_t num1, const int32_t num2) {
    // Your implementation goes here
    printf("sub\n");
  }

  int32_t mul(const int32_t num1, const int32_t num2) {
    // Your implementation goes here
    printf("mul\n");
  }

  int32_t div(const int32_t num1, const int32_t num2) {
    // Your implementation goes here
    printf("div\n");
  }
};
        You can add logic of addition, subtraction, multiplication and division in the functions add(), sub(), mul(), div() respectively. This functions are taking two inputs from client and printing the function name, instead of this you can add the logic of arithmetic operation.
Compiling:
  • g++ -I/usr/local/include/thrift -c arithmetic_Demo.cpp
  • g++ -I/usr/local/include/thrift -c arithmetic_Demo_server.skeleton.cpp
  • g++ -I/usr/local/include/thrift -c arithmetic_op.cpp
  • g++ -I/usr/local/include/thrift -c arithmetic_op_server.skeleton.cpp
  • g++ -I/usr/local/include/thrift -c Thrift_IDL_constants.cpp
  • g++ -I/usr/local/include/thrift -c Thrift_IDL_types.cpp
To compile all files in one stroke you can use the command given below
g++ -I/usr/local/include/thrift -c *.cpp
Here *cpp represent all files with extension .cpp.
Explanation:
      -I: option which is used by gcc compiler to locate the header files. In this compilation we are suggesting the gcc compiler to search for the header files in the "/usr/local/include/thrift/" folder also.
      Since it is only compilation gcc compiler won't link the thrift libraries which eliminates the requirement of including thrift library with -lthrift.
      -c: option in gcc compiler is used to create object files if you don't provide -o followed by your custom name of object file it will create object file with the same name of your c or cpp file with .o extension.

Linking:
       Since there are two services in our example we get two Service_name_server_skeleton.cpp files which contain main function in it. Due to this we require to link this two service_skeleton.cpp files separately, which will create two executables with two different service.
       You can use the command given below for linking the services

Link "arithmetic_op" service:
 
g++ -L/usr/local/lib/ arithmetic_op.o arithmetic_op_server.skeleton.o Thrift_IDL_constants.o Thrift_IDL_types.o -lthrift -o arithmetic_op

Link "arithmetic_Demo" service:

g++ -L/usr/local/lib/ arithmetic_Demo.o arithmetic_Demo_server.skeleton.o Thrift_IDL_constants.o Thrift_IDL_types.o -lthrift -o arithmetic_Demo

Explanation:
-L: This option is used when you want compiler to link with libraries present in directory other than LD_LIBRARY_PATH environment variable. Here we have used to link with thrift library named "libthrift.a" or "libthrift.so". Here .a represent static library where as .so represent dynamic library.

-o: This option is used to give custom name to your executable by default it is a.out

1 comment: