SlideShare a Scribd company logo
맛보기




      Apache Thrift

            김용환
      knight76.tistory.com
서론
Thrift
•  a software framework for scalable cross language
  service development. It combines software stack
  with a code generation engine to build services that
  work efficiently and seamlessly
• Provides cross-language RPC and serialization library
• 지원 언어 :
  C++ ,C# ,Cocoa ,Erlang ,Haskell ,Java ,Ocaml ,Perl ,P
  HP ,Python ,Ruby ,Smalltalk
• Champion
    – Doug Cutting (하둡)
• Apache license
Powered by Thrift
Support Protocol
• 많은 곳에서 통신 규약으로 사용하는 중
장점
• 버전닝 지원
• 여러 언어에서 사용 가능하며 언어에 맞도록 소스가 생성되고, 언어
  간의 Serialization 가능
• Sync, Async API 제공
• XML 설정이 필요 없음
• Layer에 맞는 Interface를 사용 및 Customizing 구축 가능
• RPC 기능 제공 (Google Protocol Buffer에는 없는 기능)
   – 서버 기능 좋음
      •   서블릿 제공(org.apache.thrift.server.TServlet)
      •   멀티쓰레드 지원 (org.apache.thrift.server.ThreadPoolServer : worker thread 지정)
      •   Async 지원 (org.apache.thrift.server. TNonblockingServer : single threaded)
      •   Multi-thread Half-Sync/Half-Async지원 : org.apache.thrift.server. THsHaServer
• Exception을 제공 (Google Protocol Buffer에는 없는 기능)
• Set, Map 지원 (Google Protocol Buffer에는 없는 기능)
단점 ?
• 자바로 코드가 생성될 때, Slf4j를 기본적으로 가지고
  가며, 내부 코드는 모두 thrift lib가 필요함
• C++의 Server threading part는 Boost에 의존을 가짐
• 자바 쪽 이클립스 플러그인 없음
• Thrift lib의 api가 자주 바뀌어서, 버전 업마다 소스를
  좀 보고 코딩해야 함. (인터넷 예제가 거의 없음)
• XML Serialization/Deserialization 기능 없음
• 문서가 확실히 적음
• 생성된 코드가 Google Protocol Buffer에 비해서 보기
  는 편하지는 않음 (특히 C++)
download
• http://thrift.apache.org/download/
설치 및 사용
• 원래는 configure/make를 필요 (리눅스/윈도우)
• 윈도우는 그냥 실행하면 됨 (여기서는 윈도우 버전으로
  만 테스트)
(소스 컴파일시)
         Basic requirements
• A relatively POSIX-compliant *NIX system
  – Cygwin or MinGW can be used on Windows
• g++ 3.3.5+
• boost 1.33.1+ (1.34.0 for building all
  tests)
• Runtime libraries for lex and yacc might
  be needed for the compiler.
(소스 컴파일시)
             Language requirements
•   C++
     – Boost 1.33.1+
     – libevent (optional, to build the nonblocking server)
     – zlib (optional)
•   Java
     –   Java 1.5+
     –   Apache Ant
     –   Apache Ivy (recommended)
     –   Apache Commons Lang (recommended)
     –   SLF4J
•   C#: Mono 1.2.4+ (and pkg-config to detect it) or Visual Studio 2005+
•   Python 2.4+ (including header files for extension modules)
•   PHP 5.0+ (optionally including header files for extension modules)
•   Ruby 1.8+ (including header files for extension modules)
•   Erlang R12 (R11 works but not recommended)
•   Perl 5
     – Bit::Vector
     – Class::Accessor
History
• Originally developed by Facebook
• Donated to apache
  apache incubator
• Now 상위 프로젝트
  http://thrift.apache.org/
Interface definition


                             Code
  Interface                                            컴파일
                           generator
    생성                                                  코드
                             실행



.thrift 파일 작성
                 실행
                 thrift -gen java -gen py foo.thrift
예제
Thrift파일
                                                     샘플 #1
service HelloWorld {
              oneway void hello(1:string name)
}


C:thrift>thrift-0.7.0.exe --gen java HelloWorld.thrift
  Generated Code
public class HelloWorld {
  public interface Iface {
     public void hello(String name) throws org.apache.thrift.TException;
   }

 public interface AsyncIface {
    public void hello(String name, org.apache.thrift.async.AsyncMethodCallback<AsyncClient.hello_call> resultHandler) throws
org.apache.thrift.TException;
  }

 public static class Client extends org.apache.thrift.TServiceClient implements Iface {
  …
  }

  public static class AsyncClient extends org.apache.thrift.async.TAsyncClient implements AsyncIface {
  …
  }

 public static class Processor<I extends Iface> extends org.apache.thrift.TBaseProcessor implements org.apache.thrift.TProcessor          {
  }

 public static class hello_args implements org.apache.thrift.TBase<hello_args, hello_args._Fields>, java.io.Serializable, Cloneable   {
  …
  }
Server Code : 사용자는 generated code를 상속하고.Thrift API를 이용해서 포트매핑
public class HelloWorldServer {

          public static void main(String[] argss) {
                      try {
                                  Factory portFactory = new TBinaryProtocol.Factory(true, true);
                                  TServerSocket serverTransport = new TServerSocket(8000);
                                  HelloWorld.Processor<HelloWorld.Iface> processor =
                                             new HelloWorld.Processor<HelloWorld.Iface>(new
HelloWorldImpl());
                                  Args args = new Args(serverTransport);
                                  args.processor(processor);
                                  args.protocolFactory(portFactory);
                                  TServer server = new TThreadPoolServer(args);
                                  System.out.println("Starting server on port 8000 ...");
                                  server.serve();
                      } catch (TTransportException e) {
                                  e.printStackTrace();
                      }
          }
}

class HelloWorldImpl implements HelloWorld.Iface {
           @Override
           public void hello(String name) throws TException {
                      long time = System.currentTimeMillis();
                      System.out.println("Current time : " + time);
                      System.out.println("Hello " + name);
           }
}
Client Code



public class HelloWorldClient {
            public static void main(String[] args) {
                        TTransport transport;
                        try {
                                    transport = new TSocket("localhost", 8000);
                                    TProtocol protocol = new TBinaryProtocol(transport);
                                    HelloWorld.Client client = new HelloWorld.Client(protocol);
                                    transport.open();
                                    client.hello("World!!!");
                                    transport.close();
                        } catch (Exception e) {
                                    e.printStackTrace();
                        }
            }
}




                                                실행결과

Starting server on port 8000 ...
Current time : 1317014309989
Hello World!!!
Client/Server 의 pom.xml
  • Slf4j와 thrift library가 있어야 컴파일 됨

<dependencies>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.6.2</version>
</dependency>

<dependency>
  <groupId>org.apache.thrift</groupId>
  <artifactId>libthrift</artifactId>
  <version>0.7.0</version>
</dependency>

</dependencies>
생성된 소스
• C++




• Java
샘플 #2


exception MathException {
  1 : string message
}

service CalculatorServicef {
  i32 add (1 : i32 a , 2 : i32 b )
  i32 subtract (1 : i32 a , 2 : i32 b )
  double divide (1 : double a , 2 : double b ) throws (1 : MathException me)
}
생성된 소스
자바
클래스
샘플 #3
namespace cpp thrift.example
namespace java thrift.example

enum TweetType {
TWEET,
RETWEET = 2,
DM = 0xa,
REPLY
}
struct Location {
1: required double latitude;
2: required double longitude;
}
struct Tweet {
1: required i32 userId;
2: required string userName;
3: required string text;
4: optional Location loc;
5: optional TweetType tweetType = TweetType.TWEET;
16: optional string language = "english";
}
typedef list<Tweet> TweetList
struct TweetSearchResult {
1: TweetList tweets;
}
const i32 MAX_RESULTS = 100;
service Twitter {
void ping(),
bool postTweet(1:Tweet tweet);
TweetSearchResult searchTweets(1:string query);
oneway void zip()
}
생성 파일




• 상수는 Constants클래스로.
• Enum은 org.apache.thrift.TEnum 을 상속
  받은 java enum으로 변경
Type
Types
• C/C++ style typedefs.
  typedef i32 MyInteger
  Typedef StrutType SStruct

• Enum
  enum TweetType {
    TWEET,
    RETWEET = 2,
    DM = 0xa,
    REPLY
  }

  struct Tweet {
     1: required i32 userId;
     2: optional TweetType tweetType = TweetType.TWEET
  }
기타
• comments
  – /* */
  – //
• namespace
  namespace java com.example.project


• Include
  include "tweet.thrift”
  ...
  struct TweetSearchResult {
  1: list<tweet.Tweet> tweets;
  }
기타
• Constants
  const i32 INT_CONST = 1234;
  const map<string,string> MAP_CONST = {"hello": "world",
  "goodnight": "moon"}

• 안되는 것
  –   cyclic structs
  –   Polymorphism
  –   Overloading
  –   null return
  –   heterogeneous containers : items in a container
      must be of the same type (자바 generic을 생각)
IDL>Java
•   bool: boolean
•   byte: byte
•   i16: short
•   i32: int
•   i64: long
•   double: double
•   string: String
•   list<type>: List<type>
•   set<type>: Set<type>
•   Map<key, value>: Map<key, value>
•   Typedef a b : just B
IDL->C++
•   bool: bool
•   byte: int8_t
•   i16: int16_t
•   i32: int32_t
•   i64: int64_t
•   double: double
•   string: std::string
•   list<t1>: std::vector<t1>
•   set<t1>: std::set<t1>
•   map<t1,t2>: std::map<T1, T2>
Types
• Basic type
   – bool, byte, int(i16, i32, i64), double, string
• Containers
   – list<type>
   – Set<type>
   – Map<keyType, valueType>
• Struct (c와 비슷)
   – Struct안에 struct과 enum 사용가능
   – But, 선언은 {} 밖에서 해야 함. 안에서 선언금지
   – 상속 안됨
• Exception
   – Structs
• Service
   – Services are defined using Thrift types
Stacks
Flow
Thrift Network Stack
LanguageSupport
•   http://wiki.apache.org/thrift/LibraryFeatures?action=show&redirect=Langu
    ageSupport
                                                                     (2011.9.25)
Thrift Network Stack
Transport Layer
• TFileTransport – This transport writes to a file.
• TFramedTransport – This transport is required
  when using a non-blocking server. It sends data
  in frames, where each frame is preceded by a
  length information.
• TMemoryTransport – Uses memory for I/O. The
  Java implementation uses a simple
  ByteArrayOutputStream internally.
• TSocket – Uses blocking socket I/O for transport.
• TZlibTransport – Performs compression using zlib.
  Used in conjunction with another transport. Not
  available in the Java implementation.
Transport interface
•   Open
•   Close
•   Read
•   Write
•   Flush
Server Transport interface
•   Open
•   Listen
•   Accept
•   Close


    • file: read/write to/from a file on disk
    • http: as the name suggests
Thrift Network Stack
Protocol layer
• TBinaryProtocol – A straight-forward binary format
  encoding numeric values as binary. It is faster than
  the text protocol but more difficult to debug.
• TCompactProtocol – Very efficient, dense encoding
  of data.
• TDebugProtocol – Uses a human-readable text
  format to aid in debugging.
• TDenseProtocol – Similar to TCompactProtocol,
  striping off the meta information from what is
  transmitted.
• TJSONProtocol – Uses JSON for encoding of data.
• TSimpleJSONProtocol – A write-only protocol using
  JSON. Suitable for parsing by scripting languages.
Protocol interface
writeMessageBegin(name, type, seq)
writeMessageEnd()
writeStructBegin(name)
writeStructEnd()
writeFieldBegin(name, type, id)
writeFieldEnd()
writeFieldStop()
writeMapBegin(ktype, vtype, size)
writeMapEnd()
writeListBegin(etype, size)
writeListEnd()
writeSetBegin(etype, size)
writeSetEnd()
writeBool(bool)
writeByte(byte)
writeI16(i16)
writeI32(i32)
…
…
..
Json Serialization
    Thrift
struct Job {
  1: i32 num1 = 0,
  2: i32 num2,
}

Generated Java Code by Compiler
public class Job implements org.apache.thrift.TBase<Job, Job._Fields>, java.io.Serializable,
Cloneable {
.....
}

 Java code

Job job;
….

TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory());
String json = serializer.toString( job);
Binary Serialization

 Java code
// serialization code
TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory());
byte[] bytes = serializer.serialize(new Job());

// deserialization code
TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
Job job= new Job();
deserializer.deserialize( job, bytes);
Thrift Network Stack
processor
• Thrift 컴파일러에 의해서 interface에 맞는
  processor가 생성됨
• Processor는 input/ouput stream을 관장하
  는 클래스(encapsulates the ability to read
  data from input streams and write to
  output streams.)
  interface TProcessor {
  bool process(TProtocol in, TProtocol out) throws TException
  }
Thrift Network Stack
Server Layer
• TNonblockingServer – A multi-threaded
  server using non-blocking io (Java
  implementation uses NIO channels).
  TFramedTransport must be used with
  this server.
• TSimpleServer – A single-threaded server
  using std blocking io. Useful for testing.
• TThreadPoolServer – A multi-threaded
  server using std blocking io.
기타
기존 IDL를 변경할 때..
• 기존 IDL를 바꿀때마다 고생하지 않아도 되는
  방법
 – Don’t change the numeric tags for any existing
   fields.
 – Any new fields that you add should be optional.
 – Non-required fields can be removed, as long as
   the tag number is not used again in your
   updated message type
    "OBSOLETE_“으로 이름 변경하기
 – Changing a default value is generally OK, as
   long as you remember that default values are
   never sent over the wire.
Thrift API
• http://people.apache.org/~jfarrell/thrift/0.
  7.0/javadoc/
끝

More Related Content

ODP
An introduction to Apache Thrift
PDF
Apache thrift-RPC service cross languages
PPT
Introduction to Thrift
PDF
RESTLess Design with Apache Thrift: Experiences from Apache Airavata
PPTX
Apache Thrift, a brief introduction
PDF
3 apache-avro
PDF
服务框架: Thrift & PasteScript
PPTX
Php’s guts
An introduction to Apache Thrift
Apache thrift-RPC service cross languages
Introduction to Thrift
RESTLess Design with Apache Thrift: Experiences from Apache Airavata
Apache Thrift, a brief introduction
3 apache-avro
服务框架: Thrift & PasteScript
Php’s guts

What's hot (20)

PDF
Apache Thrift : One Stop Solution for Cross Language Communication
PDF
The Parenscript Common Lisp to JavaScript compiler
PDF
Fluentd meetup dive into fluent plugin (outdated)
PDF
PHPCR e API Platform: cosa significa davvero sviluppare un CMF con Symfony
PDF
Apache AVRO (Boston HUG, Jan 19, 2010)
PDF
Fluentd meetup in japan
PDF
JRuby with Java Code in Data Processing World
PDF
NativeBoost
PDF
Fluentd unified logging layer
PPTX
Hack and HHVM
PDF
How DSL works on Ruby
PPTX
Hack Programming Language
PDF
Dive into Fluentd plugin v0.12
PPTX
Php internal architecture
PDF
Fluentd v0.14 Plugin API Details
PDF
DEF CON 27 - workshop - HUGO TROVAO and RUSHIKESH NADEDKAR - scapy dojo v1
PDF
Fluentd and WebHDFS
PDF
Fluentd introduction at ipros
ODP
Developing high-performance network servers in Lisp
PDF
Fluentd v1.0 in a nutshell
Apache Thrift : One Stop Solution for Cross Language Communication
The Parenscript Common Lisp to JavaScript compiler
Fluentd meetup dive into fluent plugin (outdated)
PHPCR e API Platform: cosa significa davvero sviluppare un CMF con Symfony
Apache AVRO (Boston HUG, Jan 19, 2010)
Fluentd meetup in japan
JRuby with Java Code in Data Processing World
NativeBoost
Fluentd unified logging layer
Hack and HHVM
How DSL works on Ruby
Hack Programming Language
Dive into Fluentd plugin v0.12
Php internal architecture
Fluentd v0.14 Plugin API Details
DEF CON 27 - workshop - HUGO TROVAO and RUSHIKESH NADEDKAR - scapy dojo v1
Fluentd and WebHDFS
Fluentd introduction at ipros
Developing high-performance network servers in Lisp
Fluentd v1.0 in a nutshell
Ad

Viewers also liked (14)

PPTX
Mongo DB로 진행하는 CRUD
PPTX
아파치 쓰리프트 (Apache Thrift)
PPT
Building scalable and language independent java services using apache thrift
PDF
Illustration of TextSecure's Protocol Buffer usage
PDF
Acceleration for big data, hadoop and memcached it168文库
PPT
Hive User Meeting March 2010 - Hive Team
PPT
Hive Object Model
PPTX
Facebook thrift
PPTX
Thrift vs Protocol Buffers vs Avro - Biased Comparison
PDF
Spark 2.x Troubleshooting Guide
 
PDF
Apache big data 2016 - Speaking the language of Big Data
PDF
Secured (Kerberos-based) Spark Notebook for Data Science: Spark Summit East t...
PDF
Bulletproof Jobs: Patterns for Large-Scale Spark Processing: Spark Summit Eas...
PDF
What No One Tells You About Writing a Streaming App: Spark Summit East talk b...
Mongo DB로 진행하는 CRUD
아파치 쓰리프트 (Apache Thrift)
Building scalable and language independent java services using apache thrift
Illustration of TextSecure's Protocol Buffer usage
Acceleration for big data, hadoop and memcached it168文库
Hive User Meeting March 2010 - Hive Team
Hive Object Model
Facebook thrift
Thrift vs Protocol Buffers vs Avro - Biased Comparison
Spark 2.x Troubleshooting Guide
 
Apache big data 2016 - Speaking the language of Big Data
Secured (Kerberos-based) Spark Notebook for Data Science: Spark Summit East t...
Bulletproof Jobs: Patterns for Large-Scale Spark Processing: Spark Summit Eas...
What No One Tells You About Writing a Streaming App: Spark Summit East talk b...
Ad

Similar to Apache Thrift (20)

PDF
Introduction to clojure
PDF
Silicon Valley JUG: JVM Mechanics
PPT
Dr archana dhawan bajaj - c# dot net
PDF
JVM Mechanics: When Does the JVM JIT & Deoptimize?
PPTX
introduction to node.js
PDF
Python, do you even async?
PPTX
Sharding and Load Balancing in Scala - Twitter's Finagle
PDF
Solr @ Etsy - Apache Lucene Eurocon
PDF
Original slides from Ryan Dahl's NodeJs intro talk
PDF
Tornado Web Server Internals
PDF
The Swift Compiler and Standard Library
PDF
Fast and Reliable Swift APIs with gRPC
PDF
The State of containerd
PPTX
Server Side Swift
PPTX
Iron Languages - NYC CodeCamp 2/19/2011
PDF
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
PPTX
OpenCL Heterogeneous Parallel Computing
PPTX
ODP
Lambda Chops - Recipes for Simpler, More Expressive Code
PDF
TypeScript for Java Developers
Introduction to clojure
Silicon Valley JUG: JVM Mechanics
Dr archana dhawan bajaj - c# dot net
JVM Mechanics: When Does the JVM JIT & Deoptimize?
introduction to node.js
Python, do you even async?
Sharding and Load Balancing in Scala - Twitter's Finagle
Solr @ Etsy - Apache Lucene Eurocon
Original slides from Ryan Dahl's NodeJs intro talk
Tornado Web Server Internals
The Swift Compiler and Standard Library
Fast and Reliable Swift APIs with gRPC
The State of containerd
Server Side Swift
Iron Languages - NYC CodeCamp 2/19/2011
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
OpenCL Heterogeneous Parallel Computing
Lambda Chops - Recipes for Simpler, More Expressive Code
TypeScript for Java Developers

More from knight1128 (19)

PPTX
Hancom MDS Conference - KAKAO DEVOPS Practice (카카오 스토리의 Devops 사례)
DOCX
Comet
PDF
Apache avro
PPTX
Redis
PPTX
Spring MVC 3 Restful
PPTX
Jersey framework
PPTX
Google Protocol buffer
PPTX
Jdk(java) 7 - 5. invoke-dynamic
PPTX
Jdk(java) 7 - 6 기타기능
PPTX
Jdk 7 4-forkjoin
PPTX
Jdk 7 3-nio2
PPTX
공유 Jdk 7-2-project coin
PPTX
공유 Jdk 7-1-short introduction
DOCX
아마존 Aws 서비스_연구
PPTX
속도체크
PPTX
구글크롬Os
PPTX
하이브리드앱
PPTX
오픈소스를 활용한 Batch_처리_플랫폼_공유
PPTX
Ssl 하드웨어 가속기를 이용한 성능 향상
Hancom MDS Conference - KAKAO DEVOPS Practice (카카오 스토리의 Devops 사례)
Comet
Apache avro
Redis
Spring MVC 3 Restful
Jersey framework
Google Protocol buffer
Jdk(java) 7 - 5. invoke-dynamic
Jdk(java) 7 - 6 기타기능
Jdk 7 4-forkjoin
Jdk 7 3-nio2
공유 Jdk 7-2-project coin
공유 Jdk 7-1-short introduction
아마존 Aws 서비스_연구
속도체크
구글크롬Os
하이브리드앱
오픈소스를 활용한 Batch_처리_플랫폼_공유
Ssl 하드웨어 가속기를 이용한 성능 향상

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Empathic Computing: Creating Shared Understanding
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
KodekX | Application Modernization Development
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPT
Teaching material agriculture food technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Cloud computing and distributed systems.
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Reach Out and Touch Someone: Haptics and Empathic Computing
Empathic Computing: Creating Shared Understanding
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectral efficient network and resource selection model in 5G networks
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KodekX | Application Modernization Development
Mobile App Security Testing_ A Comprehensive Guide.pdf
Teaching material agriculture food technology
Review of recent advances in non-invasive hemoglobin estimation
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...

Apache Thrift

  • 1. 맛보기 Apache Thrift 김용환 knight76.tistory.com
  • 3. Thrift • a software framework for scalable cross language service development. It combines software stack with a code generation engine to build services that work efficiently and seamlessly • Provides cross-language RPC and serialization library • 지원 언어 : C++ ,C# ,Cocoa ,Erlang ,Haskell ,Java ,Ocaml ,Perl ,P HP ,Python ,Ruby ,Smalltalk • Champion – Doug Cutting (하둡) • Apache license
  • 5. Support Protocol • 많은 곳에서 통신 규약으로 사용하는 중
  • 6. 장점 • 버전닝 지원 • 여러 언어에서 사용 가능하며 언어에 맞도록 소스가 생성되고, 언어 간의 Serialization 가능 • Sync, Async API 제공 • XML 설정이 필요 없음 • Layer에 맞는 Interface를 사용 및 Customizing 구축 가능 • RPC 기능 제공 (Google Protocol Buffer에는 없는 기능) – 서버 기능 좋음 • 서블릿 제공(org.apache.thrift.server.TServlet) • 멀티쓰레드 지원 (org.apache.thrift.server.ThreadPoolServer : worker thread 지정) • Async 지원 (org.apache.thrift.server. TNonblockingServer : single threaded) • Multi-thread Half-Sync/Half-Async지원 : org.apache.thrift.server. THsHaServer • Exception을 제공 (Google Protocol Buffer에는 없는 기능) • Set, Map 지원 (Google Protocol Buffer에는 없는 기능)
  • 7. 단점 ? • 자바로 코드가 생성될 때, Slf4j를 기본적으로 가지고 가며, 내부 코드는 모두 thrift lib가 필요함 • C++의 Server threading part는 Boost에 의존을 가짐 • 자바 쪽 이클립스 플러그인 없음 • Thrift lib의 api가 자주 바뀌어서, 버전 업마다 소스를 좀 보고 코딩해야 함. (인터넷 예제가 거의 없음) • XML Serialization/Deserialization 기능 없음 • 문서가 확실히 적음 • 생성된 코드가 Google Protocol Buffer에 비해서 보기 는 편하지는 않음 (특히 C++)
  • 9. 설치 및 사용 • 원래는 configure/make를 필요 (리눅스/윈도우) • 윈도우는 그냥 실행하면 됨 (여기서는 윈도우 버전으로 만 테스트)
  • 10. (소스 컴파일시) Basic requirements • A relatively POSIX-compliant *NIX system – Cygwin or MinGW can be used on Windows • g++ 3.3.5+ • boost 1.33.1+ (1.34.0 for building all tests) • Runtime libraries for lex and yacc might be needed for the compiler.
  • 11. (소스 컴파일시) Language requirements • C++ – Boost 1.33.1+ – libevent (optional, to build the nonblocking server) – zlib (optional) • Java – Java 1.5+ – Apache Ant – Apache Ivy (recommended) – Apache Commons Lang (recommended) – SLF4J • C#: Mono 1.2.4+ (and pkg-config to detect it) or Visual Studio 2005+ • Python 2.4+ (including header files for extension modules) • PHP 5.0+ (optionally including header files for extension modules) • Ruby 1.8+ (including header files for extension modules) • Erlang R12 (R11 works but not recommended) • Perl 5 – Bit::Vector – Class::Accessor
  • 12. History • Originally developed by Facebook • Donated to apache apache incubator • Now 상위 프로젝트 http://thrift.apache.org/
  • 13. Interface definition Code Interface 컴파일 generator 생성 코드 실행 .thrift 파일 작성 실행 thrift -gen java -gen py foo.thrift
  • 15. Thrift파일 샘플 #1 service HelloWorld { oneway void hello(1:string name) } C:thrift>thrift-0.7.0.exe --gen java HelloWorld.thrift Generated Code public class HelloWorld { public interface Iface { public void hello(String name) throws org.apache.thrift.TException; } public interface AsyncIface { public void hello(String name, org.apache.thrift.async.AsyncMethodCallback<AsyncClient.hello_call> resultHandler) throws org.apache.thrift.TException; } public static class Client extends org.apache.thrift.TServiceClient implements Iface { … } public static class AsyncClient extends org.apache.thrift.async.TAsyncClient implements AsyncIface { … } public static class Processor<I extends Iface> extends org.apache.thrift.TBaseProcessor implements org.apache.thrift.TProcessor { } public static class hello_args implements org.apache.thrift.TBase<hello_args, hello_args._Fields>, java.io.Serializable, Cloneable { … }
  • 16. Server Code : 사용자는 generated code를 상속하고.Thrift API를 이용해서 포트매핑 public class HelloWorldServer { public static void main(String[] argss) { try { Factory portFactory = new TBinaryProtocol.Factory(true, true); TServerSocket serverTransport = new TServerSocket(8000); HelloWorld.Processor<HelloWorld.Iface> processor = new HelloWorld.Processor<HelloWorld.Iface>(new HelloWorldImpl()); Args args = new Args(serverTransport); args.processor(processor); args.protocolFactory(portFactory); TServer server = new TThreadPoolServer(args); System.out.println("Starting server on port 8000 ..."); server.serve(); } catch (TTransportException e) { e.printStackTrace(); } } } class HelloWorldImpl implements HelloWorld.Iface { @Override public void hello(String name) throws TException { long time = System.currentTimeMillis(); System.out.println("Current time : " + time); System.out.println("Hello " + name); } }
  • 17. Client Code public class HelloWorldClient { public static void main(String[] args) { TTransport transport; try { transport = new TSocket("localhost", 8000); TProtocol protocol = new TBinaryProtocol(transport); HelloWorld.Client client = new HelloWorld.Client(protocol); transport.open(); client.hello("World!!!"); transport.close(); } catch (Exception e) { e.printStackTrace(); } } } 실행결과 Starting server on port 8000 ... Current time : 1317014309989 Hello World!!!
  • 18. Client/Server 의 pom.xml • Slf4j와 thrift library가 있어야 컴파일 됨 <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.7.0</version> </dependency> </dependencies>
  • 20. 샘플 #2 exception MathException { 1 : string message } service CalculatorServicef { i32 add (1 : i32 a , 2 : i32 b ) i32 subtract (1 : i32 a , 2 : i32 b ) double divide (1 : double a , 2 : double b ) throws (1 : MathException me) }
  • 23. 샘플 #3 namespace cpp thrift.example namespace java thrift.example enum TweetType { TWEET, RETWEET = 2, DM = 0xa, REPLY } struct Location { 1: required double latitude; 2: required double longitude; } struct Tweet { 1: required i32 userId; 2: required string userName; 3: required string text; 4: optional Location loc; 5: optional TweetType tweetType = TweetType.TWEET; 16: optional string language = "english"; } typedef list<Tweet> TweetList struct TweetSearchResult { 1: TweetList tweets; } const i32 MAX_RESULTS = 100; service Twitter { void ping(), bool postTweet(1:Tweet tweet); TweetSearchResult searchTweets(1:string query); oneway void zip() }
  • 24. 생성 파일 • 상수는 Constants클래스로. • Enum은 org.apache.thrift.TEnum 을 상속 받은 java enum으로 변경
  • 25. Type
  • 26. Types • C/C++ style typedefs. typedef i32 MyInteger Typedef StrutType SStruct • Enum enum TweetType { TWEET, RETWEET = 2, DM = 0xa, REPLY } struct Tweet { 1: required i32 userId; 2: optional TweetType tweetType = TweetType.TWEET }
  • 27. 기타 • comments – /* */ – // • namespace namespace java com.example.project • Include include "tweet.thrift” ... struct TweetSearchResult { 1: list<tweet.Tweet> tweets; }
  • 28. 기타 • Constants const i32 INT_CONST = 1234; const map<string,string> MAP_CONST = {"hello": "world", "goodnight": "moon"} • 안되는 것 – cyclic structs – Polymorphism – Overloading – null return – heterogeneous containers : items in a container must be of the same type (자바 generic을 생각)
  • 29. IDL>Java • bool: boolean • byte: byte • i16: short • i32: int • i64: long • double: double • string: String • list<type>: List<type> • set<type>: Set<type> • Map<key, value>: Map<key, value> • Typedef a b : just B
  • 30. IDL->C++ • bool: bool • byte: int8_t • i16: int16_t • i32: int32_t • i64: int64_t • double: double • string: std::string • list<t1>: std::vector<t1> • set<t1>: std::set<t1> • map<t1,t2>: std::map<T1, T2>
  • 31. Types • Basic type – bool, byte, int(i16, i32, i64), double, string • Containers – list<type> – Set<type> – Map<keyType, valueType> • Struct (c와 비슷) – Struct안에 struct과 enum 사용가능 – But, 선언은 {} 밖에서 해야 함. 안에서 선언금지 – 상속 안됨 • Exception – Structs • Service – Services are defined using Thrift types
  • 33. Flow
  • 35. LanguageSupport • http://wiki.apache.org/thrift/LibraryFeatures?action=show&redirect=Langu ageSupport (2011.9.25)
  • 37. Transport Layer • TFileTransport – This transport writes to a file. • TFramedTransport – This transport is required when using a non-blocking server. It sends data in frames, where each frame is preceded by a length information. • TMemoryTransport – Uses memory for I/O. The Java implementation uses a simple ByteArrayOutputStream internally. • TSocket – Uses blocking socket I/O for transport. • TZlibTransport – Performs compression using zlib. Used in conjunction with another transport. Not available in the Java implementation.
  • 38. Transport interface • Open • Close • Read • Write • Flush
  • 39. Server Transport interface • Open • Listen • Accept • Close • file: read/write to/from a file on disk • http: as the name suggests
  • 41. Protocol layer • TBinaryProtocol – A straight-forward binary format encoding numeric values as binary. It is faster than the text protocol but more difficult to debug. • TCompactProtocol – Very efficient, dense encoding of data. • TDebugProtocol – Uses a human-readable text format to aid in debugging. • TDenseProtocol – Similar to TCompactProtocol, striping off the meta information from what is transmitted. • TJSONProtocol – Uses JSON for encoding of data. • TSimpleJSONProtocol – A write-only protocol using JSON. Suitable for parsing by scripting languages.
  • 42. Protocol interface writeMessageBegin(name, type, seq) writeMessageEnd() writeStructBegin(name) writeStructEnd() writeFieldBegin(name, type, id) writeFieldEnd() writeFieldStop() writeMapBegin(ktype, vtype, size) writeMapEnd() writeListBegin(etype, size) writeListEnd() writeSetBegin(etype, size) writeSetEnd() writeBool(bool) writeByte(byte) writeI16(i16) writeI32(i32) … … ..
  • 43. Json Serialization Thrift struct Job { 1: i32 num1 = 0, 2: i32 num2, } Generated Java Code by Compiler public class Job implements org.apache.thrift.TBase<Job, Job._Fields>, java.io.Serializable, Cloneable { ..... } Java code Job job; …. TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory()); String json = serializer.toString( job);
  • 44. Binary Serialization Java code // serialization code TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory()); byte[] bytes = serializer.serialize(new Job()); // deserialization code TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory()); Job job= new Job(); deserializer.deserialize( job, bytes);
  • 46. processor • Thrift 컴파일러에 의해서 interface에 맞는 processor가 생성됨 • Processor는 input/ouput stream을 관장하 는 클래스(encapsulates the ability to read data from input streams and write to output streams.) interface TProcessor { bool process(TProtocol in, TProtocol out) throws TException }
  • 48. Server Layer • TNonblockingServer – A multi-threaded server using non-blocking io (Java implementation uses NIO channels). TFramedTransport must be used with this server. • TSimpleServer – A single-threaded server using std blocking io. Useful for testing. • TThreadPoolServer – A multi-threaded server using std blocking io.
  • 50. 기존 IDL를 변경할 때.. • 기존 IDL를 바꿀때마다 고생하지 않아도 되는 방법 – Don’t change the numeric tags for any existing fields. – Any new fields that you add should be optional. – Non-required fields can be removed, as long as the tag number is not used again in your updated message type "OBSOLETE_“으로 이름 변경하기 – Changing a default value is generally OK, as long as you remember that default values are never sent over the wire.
  • 52.