blob: 082be625c11d90b8ca8972c5755534ca24506061 [file] [log] [blame]
Mark Slee24b49d32007-03-21 01:24:00 +00001%-----------------------------------------------------------------------------
2%
3% Thrift whitepaper
4%
5% Name: thrift.tex
6%
7% Authors: Mark Slee (mcslee@facebook.com)
8%
9% Created: 05 March 2007
10%
David Reisse9aab102008-04-03 20:16:53 +000011% You will need a copy of sigplanconf.cls to format this document.
12% It is available at <http://www.sigplan.org/authorInformation.htm>.
13%
Mark Slee24b49d32007-03-21 01:24:00 +000014%-----------------------------------------------------------------------------
15
16
17\documentclass[nocopyrightspace,blockstyle]{sigplanconf}
18
19\usepackage{amssymb}
20\usepackage{amsfonts}
21\usepackage{amsmath}
Marc Slemko10b3bdb2007-04-01 09:14:05 +000022\usepackage{url}
Mark Slee24b49d32007-03-21 01:24:00 +000023
24\begin{document}
25
David Reiss0c90f6f2008-02-06 22:18:40 +000026% \conferenceinfo{WXYZ '05}{date, City.}
Mark Slee24b49d32007-03-21 01:24:00 +000027% \copyrightyear{2007}
David Reiss0c90f6f2008-02-06 22:18:40 +000028% \copyrightdata{[to be supplied]}
Mark Slee24b49d32007-03-21 01:24:00 +000029
30% \titlebanner{banner above paper title} % These are ignored unless
31% \preprintfooter{short description of paper} % 'preprint' option specified.
32
33\title{Thrift: Scalable Cross-Language Services Implementation}
34\subtitle{}
35
36\authorinfo{Mark Slee, Aditya Agarwal and Marc Kwiatkowski}
37 {Facebook, 156 University Ave, Palo Alto, CA}
38 {\{mcslee,aditya,marc\}@facebook.com}
39
40\maketitle
41
42\begin{abstract}
43Thrift is a software library and set of code-generation tools developed at
44Facebook to expedite development and implementation of efficient and scalable
45backend services. Its primary goal is to enable efficient and reliable
46communication across programming languages by abstracting the portions of each
47language that tend to require the most customization into a common library
48that is implemented in each language. Specifically, Thrift allows developers to
Mark Sleeafaf2762007-04-03 19:47:04 +000049define datatypes and service interfaces in a single language-neutral file
Mark Slee24b49d32007-03-21 01:24:00 +000050and generate all the necessary code to build RPC clients and servers.
51
52This paper details the motivations and design choices we made in Thrift, as
53well as some of the more interesting implementation details. It is not
54intended to be taken as research, but rather it is an exposition on what we did
55and why.
56\end{abstract}
57
58% \category{D.3.3}{Programming Languages}{Language constructs and features}
59
60%\terms
61%Languages, serialization, remote procedure call
62
63%\keywords
64%Data description language, interface definition language, remote procedure call
65
66\section{Introduction}
67As Facebook's traffic and network structure have scaled, the resource
David Reiss0c90f6f2008-02-06 22:18:40 +000068demands of many operations on the site (i.e. search,
Mark Slee24b49d32007-03-21 01:24:00 +000069ad selection and delivery, event logging) have presented technical requirements
70drastically outside the scope of the LAMP framework. In our implementation of
71these services, various programming languages have been selected to
72optimize for the right combination of performance, ease and speed of
73development, availability of existing libraries, etc. By and large,
74Facebook's engineering culture has tended towards choosing the best
Mark Sleeafaf2762007-04-03 19:47:04 +000075tools and implementations available over standardizing on any one
Mark Slee24b49d32007-03-21 01:24:00 +000076programming language and begrudgingly accepting its inherent limitations.
77
78Given this design choice, we were presented with the challenge of building
79a transparent, high-performance bridge across many programming languages.
80We found that most available solutions were either too limited, did not offer
Mark Sleeafaf2762007-04-03 19:47:04 +000081sufficient datatype freedom, or suffered from subpar performance.
Mark Slee24b49d32007-03-21 01:24:00 +000082\footnote{See Appendix A for a discussion of alternative systems.}
83
84The solution that we have implemented combines a language-neutral software
85stack implemented across numerous programming languages and an associated code
86generation engine that transforms a simple interface and data definition
87language into client and server remote procedure call libraries.
88Choosing static code generation over a dynamic system allows us to create
Mark Sleeafaf2762007-04-03 19:47:04 +000089validated code that can be run without the need for
90any advanced introspective run-time type checking. It is also designed to
Mark Slee24b49d32007-03-21 01:24:00 +000091be as simple as possible for the developer, who can typically define all
92the necessary data structures and interfaces for a complex service in a single
93short file.
94
95Surprised that a robust open solution to these relatively common problems
96did not yet exist, we committed early on to making the Thrift implementation
97open source.
98
99In evaluating the challenges of cross-language interaction in a networked
100environment, some key components were identified:
101
102\textit{Types.} A common type system must exist across programming languages
Mark Sleeafaf2762007-04-03 19:47:04 +0000103without requiring that the application developer use custom Thrift datatypes
Mark Slee24b49d32007-03-21 01:24:00 +0000104or write their own serialization code. That is,
105a C++ programmer should be able to transparently exchange a strongly typed
106STL map for a dynamic Python dictionary. Neither
107programmer should be forced to write any code below the application layer
David Reiss0c90f6f2008-02-06 22:18:40 +0000108to achieve this. Section 2 details the Thrift type system.
Mark Slee24b49d32007-03-21 01:24:00 +0000109
110\textit{Transport.} Each language must have a common interface to
111bidirectional raw data transport. The specifics of how a given
112transport is implemented should not matter to the service developer.
113The same application code should be able to run against TCP stream sockets,
114raw data in memory, or files on disk. Section 3 details the Thrift Transport
115layer.
116
Mark Sleeafaf2762007-04-03 19:47:04 +0000117\textit{Protocol.} Datatypes must have some way of using the Transport
Mark Slee24b49d32007-03-21 01:24:00 +0000118layer to encode and decode themselves. Again, the application
119developer need not be concerned by this layer. Whether the service uses
120an XML or binary protocol is immaterial to the application code.
121All that matters is that the data can be read and written in a consistent,
122deterministic matter. Section 4 details the Thrift Protocol layer.
123
Mark Sleeafaf2762007-04-03 19:47:04 +0000124\textit{Versioning.} For robust services, the involved datatypes must
Mark Slee24b49d32007-03-21 01:24:00 +0000125provide a mechanism for versioning themselves. Specifically,
126it should be possible to add or remove fields in an object or alter the
127argument list of a function without any interruption in service (or,
128worse yet, nasty segmentation faults). Section 5 details Thrift's versioning
129system.
130
131\textit{Processors.} Finally, we generate code capable of processing data
Aditya Agarwalaf524ee2007-03-31 08:28:06 +0000132streams to accomplish remote procedure calls. Section 6 details the generated
Mark Slee24b49d32007-03-21 01:24:00 +0000133code and TProcessor paradigm.
134
135Section 7 discusses implementation details, and Section 8 describes
136our conclusions.
137
138\section{Types}
139
140The goal of the Thrift type system is to enable programmers to develop using
141completely natively defined types, no matter what programming language they
142use. By design, the Thrift type system does not introduce any special dynamic
143types or wrapper objects. It also does not require that the developer write
Mark Sleeafaf2762007-04-03 19:47:04 +0000144any code for object serialization or transport. The Thrift IDL (Interface
145Definition Language) file is
Mark Slee24b49d32007-03-21 01:24:00 +0000146logically a way for developers to annotate their data structures with the
147minimal amount of extra information necessary to tell a code generator
148how to safely transport the objects across languages.
149
150\subsection{Base Types}
151
152The type system rests upon a few base types. In considering which types to
153support, we aimed for clarity and simplicity over abundance, focusing
154on the key types available in all programming languages, ommitting any
155niche types available only in specific languages.
156
157The base types supported by Thrift are:
158\begin{itemize}
159\item \texttt{bool} A boolean value, true or false
160\item \texttt{byte} A signed byte
161\item \texttt{i16} A 16-bit signed integer
162\item \texttt{i32} A 32-bit signed integer
163\item \texttt{i64} A 64-bit signed integer
164\item \texttt{double} A 64-bit floating point number
165\item \texttt{string} An encoding-agnostic text or binary string
166\end{itemize}
167
168Of particular note is the absence of unsigned integer types. Because these
169types have no direct translation to native primitive types in many languages,
170the advantages they afford are lost. Further, there is no way to prevent the
171application developer in a language like Python from assigning a negative value
172to an integer variable, leading to unpredictable behavior. From a design
173standpoint, we observed that unsigned integers were very rarely, if ever, used
174for arithmetic purposes, but in practice were much more often used as keys or
175identifiers. In this case, the sign is irrelevant. Signed integers serve this
176same purpose and can be safely cast to their unsigned counterparts (most
177commonly in C++) when absolutely necessary.
178
Mark Slee24b49d32007-03-21 01:24:00 +0000179\subsection{Structs}
180
Aditya Agarwalaf524ee2007-03-31 08:28:06 +0000181A Thrift struct defines a common object to be used across languages. A struct
Mark Slee24b49d32007-03-21 01:24:00 +0000182is essentially equivalent to a class in object oriented programming
183languages. A struct has a set of strongly typed fields, each with a unique
184name identifier. The basic syntax for defining a Thrift struct looks very
185similar to a C struct definition. Fields may be annotated with an integer field
186identifier (unique to the scope of that struct) and optional default values.
187Field identifiers will be automatically assigned if omitted, though they are
188strongly encouraged for versioning reasons discussed later.
189
Mark Sleeafaf2762007-04-03 19:47:04 +0000190\subsection{Containers}
191
192Thrift containers are strongly typed containers that map to the most commonly
193used containers in common programming languages. They are annotated using
194the C++ template (or Java Generics) style. There are three types available:
195\begin{itemize}
196\item \texttt{list<type>} An ordered list of elements. Translates directly into
197an STL \texttt{vector}, Java \texttt{ArrayList}, or native array in scripting languages. May
198contain duplicates.
199\item \texttt{set<type>} An unordered set of unique elements. Translates into
200an STL \texttt{set}, Java \texttt{HashSet}, \texttt{set} in Python, or native
David Reiss0c90f6f2008-02-06 22:18:40 +0000201dictionary in PHP/Ruby.
Mark Sleeafaf2762007-04-03 19:47:04 +0000202\item \texttt{map<type1,type2>} A map of strictly unique keys to values
Mark Slee09bfd612007-04-04 19:56:41 +0000203Translates into an STL \texttt{map}, Java \texttt{HashMap}, PHP associative
Mark Sleeafaf2762007-04-03 19:47:04 +0000204array, or Python/Ruby dictionary.
205\end{itemize}
206
207While defaults are provided, the type mappings are not explicitly fixed. Custom
208code generator directives have been added to substitute custom types in
209destination languages (i.e.
210\texttt{hash\_map} or Google's sparse hash map can be used in C++). The
211only requirement is that the custom types support all the necessary iteration
212primitives. Container elements may be of any valid Thrift type, including other
213containers or structs.
214
Mark Slee24b49d32007-03-21 01:24:00 +0000215\begin{verbatim}
216struct Example {
217 1:i32 number=10,
218 2:i64 bigNumber,
219 3:double decimals,
220 4:string name="thrifty"
221}\end{verbatim}
222
223In the target language, each definition generates a type with two methods,
224\texttt{read} and \texttt{write}, which perform serialization and transport
225of the objects using a Thrift TProtocol object.
226
227\subsection{Exceptions}
228
229Exceptions are syntactically and functionally equivalent to structs except
230that they are declared using the \texttt{exception} keyword instead of the
231\texttt{struct} keyword.
232
233The generated objects inherit from an exception base class as appropriate
Mark Sleeafaf2762007-04-03 19:47:04 +0000234in each target programming language, in order to seamlessly
235integrate with native exception handling in any given
Mark Slee24b49d32007-03-21 01:24:00 +0000236language. Again, the design emphasis is on making the code familiar to the
237application developer.
238
239\subsection{Services}
240
241Services are defined using Thrift types. Definition of a service is
Mark Sleeafaf2762007-04-03 19:47:04 +0000242semantically equivalent to defining an interface (or a pure virtual abstract
243class) in object oriented
Mark Slee24b49d32007-03-21 01:24:00 +0000244programming. The Thrift compiler generates fully functional client and
245server stubs that implement the interface. Services are defined as follows:
246
247\begin{verbatim}
248service <name> {
249 <returntype> <name>(<arguments>)
250 [throws (<exceptions>)]
251 ...
252}\end{verbatim}
253
254An example:
255
256\begin{verbatim}
257service StringCache {
258 void set(1:i32 key, 2:string value),
259 string get(1:i32 key) throws (1:KeyNotFound knf),
David Reiss0c90f6f2008-02-06 22:18:40 +0000260 void delete(1:i32 key)
Mark Slee24b49d32007-03-21 01:24:00 +0000261}
262\end{verbatim}
263
264Note that \texttt{void} is a valid type for a function return, in addition to
265all other defined Thrift types. Additionally, an \texttt{async} modifier
Mark Sleeafaf2762007-04-03 19:47:04 +0000266keyword may be added to a \texttt{void} function, which will generate code that does
Mark Slee24b49d32007-03-21 01:24:00 +0000267not wait for a response from the server. Note that a pure \texttt{void}
268function will return a response to the client which guarantees that the
269operation has completed on the server side. With \texttt{async} method calls
Mark Sleeafaf2762007-04-03 19:47:04 +0000270the client will only be guaranteed that the request succeeded at the
Mark Slee24b49d32007-03-21 01:24:00 +0000271transport layer. (In many transport scenarios this is inherently unreliable
272due to the Byzantine Generals' Problem. Therefore, application developers
Mark Sleeafaf2762007-04-03 19:47:04 +0000273should take care only to use the async optimization in cases where dropped
Mark Slee24b49d32007-03-21 01:24:00 +0000274method calls are acceptable or the transport is known to be reliable.)
275
Mark Sleeafaf2762007-04-03 19:47:04 +0000276Also of note is the fact that argument lists and exception lists for functions
277are implemented as Thrift structs. All three constructs are identical in both
278notation and behavior.
Mark Slee24b49d32007-03-21 01:24:00 +0000279
280\section{Transport}
281
282The transport layer is used by the generated code to facilitate data transfer.
283
284\subsection{Interface}
285
Mark Sleeafaf2762007-04-03 19:47:04 +0000286A key design choice in the implementation of Thrift was to decouple the
Mark Slee24b49d32007-03-21 01:24:00 +0000287transport layer from the code generation layer. Though Thrift is typically
288used on top of the TCP/IP stack with streaming sockets as the base layer of
David Reiss0c90f6f2008-02-06 22:18:40 +0000289communication, there was no compelling reason to build that constraint into
Mark Slee24b49d32007-03-21 01:24:00 +0000290the system. The performance tradeoff incurred by an abstracted I/O layer
291(roughly one virtual method lookup / function call per operation) was
292immaterial compared to the cost of actual I/O operations (typically invoking
293system calls).
294
Aditya Agarwalaf524ee2007-03-31 08:28:06 +0000295Fundamentally, generated Thrift code only needs to know how to read and
Mark Sleeafaf2762007-04-03 19:47:04 +0000296write data. The origin and destination of the data are irrelevant; it may be a
297socket, a segment of shared memory, or a file on the local disk. The Thrift
298transport interface supports the following methods:
Mark Slee24b49d32007-03-21 01:24:00 +0000299
300\begin{itemize}
Mark Sleeafaf2762007-04-03 19:47:04 +0000301\item \texttt{open} Opens the tranpsort
302\item \texttt{close} Closes the tranport
303\item \texttt{isOpen} Indicates whether the transport is open
304\item \texttt{read} Reads from the transport
305\item \texttt{write} Writes to the transport
306\item \texttt{flush} Forces any pending writes
Mark Slee24b49d32007-03-21 01:24:00 +0000307\end{itemize}
308
309There are a few additional methods not documented here which are used to aid
Mark Sleeafaf2762007-04-03 19:47:04 +0000310in batching reads and optionally signaling the completion of a read or
311write operation from the generated code.
Mark Slee24b49d32007-03-21 01:24:00 +0000312
313In addition to the above
Mark Sleec11045a2007-04-01 23:14:38 +0000314\texttt{TTransport} interface, there is a\\
315\texttt{TServerTransport} interface
Mark Slee24b49d32007-03-21 01:24:00 +0000316used to accept or create primitive transport objects. Its interface is as
317follows:
318
319\begin{itemize}
Mark Sleeafaf2762007-04-03 19:47:04 +0000320\item \texttt{open} Opens the transport
321\item \texttt{listen} Begins listening for connections
322\item \texttt{accept} Returns a new client transport
323\item \texttt{close} Closes the transport
Mark Slee24b49d32007-03-21 01:24:00 +0000324\end{itemize}
325
326\subsection{Implementation}
327
328The transport interface is designed for simple implementation in any
329programming language. New transport mechanisms can be easily defined as needed
330by application developers.
331
332\subsubsection{TSocket}
333
334The \texttt{TSocket} class is implemented across all target languages. It
335provides a common, simple interface to a TCP/IP stream socket.
336
337\subsubsection{TFileTransport}
338
339The \texttt{TFileTransport} is an abstraction of an on-disk file to a data
Mark Sleeafaf2762007-04-03 19:47:04 +0000340stream. It can be used to write out a set of incoming Thrift requests to a file
341on disk. The on-disk data can then be replayed from the log, either for
342post-processing or for reproduction and/or simulation of past events.
Mark Slee24b49d32007-03-21 01:24:00 +0000343
344\subsubsection{Utilities}
345
346The Transport interface is designed to support easy extension using common
Mark Sleeafaf2762007-04-03 19:47:04 +0000347OOP techniques, such as composition. Some simple utilites include the
348\texttt{TBufferedTransport}, which buffers the writes and reads on an
349underlying transport, the \texttt{TFramedTransport}, which transmits data with frame
350size headers for chunking optimization or nonblocking operation, and the
351\texttt{TMemoryBuffer}, which allows reading and writing directly from the heap
352or stack memory owned by the process.
Mark Slee24b49d32007-03-21 01:24:00 +0000353
354\section{Protocol}
355
356A second major abstraction in Thrift is the separation of data structure from
357transport representation. Thrift enforces a certain messaging structure when
358transporting data, but it is agnostic to the protocol encoding in use. That is,
Mark Sleeafaf2762007-04-03 19:47:04 +0000359it does not matter whether data is encoded as XML, human-readable ASCII, or a
360dense binary format as long as the data supports a fixed set of operations
361that allow it to be deterministically read and written by generated code.
Mark Slee24b49d32007-03-21 01:24:00 +0000362
363\subsection{Interface}
364
365The Thrift Protocol interface is very straightforward. It fundamentally
366supports two things: 1) bidirectional sequenced messaging, and
3672) encoding of base types, containers, and structs.
368
369\begin{verbatim}
370writeMessageBegin(name, type, seq)
371writeMessageEnd()
372writeStructBegin(name)
373writeStructEnd()
374writeFieldBegin(name, type, id)
375writeFieldEnd()
376writeFieldStop()
377writeMapBegin(ktype, vtype, size)
378writeMapEnd()
379writeListBegin(etype, size)
380writeListEnd()
381writeSetBegin(etype, size)
382writeSetEnd()
383writeBool(bool)
384writeByte(byte)
385writeI16(i16)
386writeI32(i32)
387writeI64(i64)
388writeDouble(double)
389writeString(string)
390
391name, type, seq = readMessageBegin()
392 readMessageEnd()
393name = readStructBegin()
394 readStructEnd()
395name, type, id = readFieldBegin()
396 readFieldEnd()
397k, v, size = readMapBegin()
398 readMapEnd()
399etype, size = readListBegin()
400 readListEnd()
401etype, size = readSetBegin()
402 readSetEnd()
403bool = readBool()
404byte = readByte()
405i16 = readI16()
406i32 = readI32()
407i64 = readI64()
408double = readDouble()
409string = readString()
410\end{verbatim}
411
Mark Sleeafaf2762007-04-03 19:47:04 +0000412Note that every \texttt{write} function has exactly one \texttt{read} counterpart, with
413the exception of \texttt{writeFieldStop()}. This is a special method
Mark Slee24b49d32007-03-21 01:24:00 +0000414that signals the end of a struct. The procedure for reading a struct is to
Mark Sleeafaf2762007-04-03 19:47:04 +0000415\texttt{readFieldBegin()} until the stop field is encountered, and then to
Mark Slee24b49d32007-03-21 01:24:00 +0000416\texttt{readStructEnd()}. The
Mark Sleeafaf2762007-04-03 19:47:04 +0000417generated code relies upon this call sequence to ensure that everything written by
Mark Slee24b49d32007-03-21 01:24:00 +0000418a protocol encoder can be read by a matching protocol decoder. Further note
419that this set of functions is by design more robust than necessary.
420For example, \texttt{writeStructEnd()} is not strictly necessary, as the end of
421a struct may be implied by the stop field. This method is a convenience for
Mark Sleeafaf2762007-04-03 19:47:04 +0000422verbose protocols in which it is cleaner to separate these calls (e.g. a closing
Mark Slee24b49d32007-03-21 01:24:00 +0000423\texttt{</struct>} tag in XML).
424
425\subsection{Structure}
426
427Thrift structures are designed to support encoding into a streaming
Mark Sleeafaf2762007-04-03 19:47:04 +0000428protocol. The implementation should never need to frame or compute the
Mark Slee24b49d32007-03-21 01:24:00 +0000429entire data length of a structure prior to encoding it. This is critical to
430performance in many scenarios. Consider a long list of relatively large
Mark Sleeafaf2762007-04-03 19:47:04 +0000431strings. If the protocol interface required reading or writing a list to be an
432atomic operation, then the implementation would need to perform a linear pass over the
Mark Slee24b49d32007-03-21 01:24:00 +0000433entire list before encoding any data. However, if the list can be written
434as iteration is performed, the corresponding read may begin in parallel,
Aditya Agarwalaf524ee2007-03-31 08:28:06 +0000435theoretically offering an end-to-end speedup of $(kN - C)$, where $N$ is the size
Mark Slee24b49d32007-03-21 01:24:00 +0000436of the list, $k$ the cost factor associated with serializing a single
437element, and $C$ is fixed offset for the delay between data being written
438and becoming available to read.
439
440Similarly, structs do not encode their data lengths a priori. Instead, they are
441encoded as a sequence of fields, with each field having a type specifier and a
Mark Sleeafaf2762007-04-03 19:47:04 +0000442unique field identifier. Note that the inclusion of type specifiers allows
Mark Slee24b49d32007-03-21 01:24:00 +0000443the protocol to be safely parsed and decoded without any generated code
444or access to the original IDL file. Structs are terminated by a field header
445with a special \texttt{STOP} type. Because all the basic types can be read
Mark Sleeafaf2762007-04-03 19:47:04 +0000446deterministically, all structs (even those containing other structs) can be
Mark Slee24b49d32007-03-21 01:24:00 +0000447read deterministically. The Thrift protocol is self-delimiting without any
448framing and regardless of the encoding format.
449
450In situations where streaming is unnecessary or framing is advantageous, it
451can be very simply added into the transport layer, using the
452\texttt{TFramedTransport} abstraction.
453
454\subsection{Implementation}
455
456Facebook has implemented and deployed a space-efficient binary protocol which
457is used by most backend services. Essentially, it writes all data
458in a flat binary format. Integer types are converted to network byte order,
459strings are prepended with their byte length, and all message and field headers
460are written using the primitive integer serialization constructs. String names
461for fields are omitted - when using generated code, field identifiers are
462sufficient.
463
464We decided against some extreme storage optimizations (i.e. packing
465small integers into ASCII or using a 7-bit continuation format) for the sake
466of simplicity and clarity in the code. These alterations can easily be made
Mark Sleeafaf2762007-04-03 19:47:04 +0000467if and when we encounter a performance-critical use case that demands them.
Mark Slee24b49d32007-03-21 01:24:00 +0000468
469\section{Versioning}
470
471Thrift is robust in the face of versioning and data definition changes. This
Mark Sleeafaf2762007-04-03 19:47:04 +0000472is critical to enable staged rollouts of changes to deployed services. The
473system must be able to support reading of old data from log files, as well as
474requests from out-of-date clients to new servers, and vice versa.
Mark Slee24b49d32007-03-21 01:24:00 +0000475
476\subsection{Field Identifiers}
477
478Versioning in Thrift is implemented via field identifiers. The field header
479for every member of a struct in Thrift is encoded with a unique field
480identifier. The combination of this field identifier and its type specifier
481is used to uniquely identify the field. The Thrift definition language
482supports automatic assignment of field identifiers, but it is good
483programming practice to always explicitly specify field identifiers.
484Identifiers are specified as follows:
485
486\begin{verbatim}
487struct Example {
488 1:i32 number=10,
489 2:i64 bigNumber,
490 3:double decimals,
491 4:string name="thrifty"
492}\end{verbatim}
493
Mark Sleeafaf2762007-04-03 19:47:04 +0000494To avoid conflicts between manually and automatically assigned identifiers,
495fields with identifiers omitted are assigned identifiers
Mark Slee24b49d32007-03-21 01:24:00 +0000496decrementing from -1, and the language only supports the manual assignment of
497positive identifiers.
498
499When data is being deserialized, the generated code can use these identifiers
500to properly identify the field and determine whether it aligns with a field in
501its definition file. If a field identifier is not recognized, the generated
502code can use the type specifier to skip the unknown field without any error.
Mark Sleeafaf2762007-04-03 19:47:04 +0000503Again, this is possible due to the fact that all datatypes are self
Mark Slee24b49d32007-03-21 01:24:00 +0000504delimiting.
505
506Field identifiers can (and should) also be specified in function argument
507lists. In fact, argument lists are not only represented as structs on the
508backend, but actually share the same code in the compiler frontend. This
509allows for version-safe modification of method parameters
510
511\begin{verbatim}
512service StringCache {
513 void set(1:i32 key, 2:string value),
514 string get(1:i32 key) throws (1:KeyNotFound knf),
David Reiss0c90f6f2008-02-06 22:18:40 +0000515 void delete(1:i32 key)
Mark Slee24b49d32007-03-21 01:24:00 +0000516}
517\end{verbatim}
518
519The syntax for specifying field identifiers was chosen to echo their structure.
520Structs can be thought of as a dictionary where the identifiers are keys, and
Mark Sleeafaf2762007-04-03 19:47:04 +0000521the values are strongly-typed named fields.
Mark Slee24b49d32007-03-21 01:24:00 +0000522
523Field identifiers internally use the \texttt{i16} Thrift type. Note, however,
524that the \texttt{TProtocol} abstraction may encode identifiers in any format.
525
526\subsection{Isset}
527
528When an unexpected field is encountered, it can be safely ignored and
529discarded. When an expected field is not found, there must be some way to
530signal to the developer that it was not present. This is implemented via an
Mark Sleeafaf2762007-04-03 19:47:04 +0000531inner \texttt{isset} structure inside the defined objects. (Isset functionality
532is implicit with a \texttt{null} value in PHP, \texttt{None} in Python
Mark Slee24b49d32007-03-21 01:24:00 +0000533and \texttt{nil} in Ruby.) Essentially,
534the inner \texttt{isset} object of each Thrift struct contains a boolean value
535for each field which denotes whether or not that field is present in the
536struct. When a reader receives a struct, it should check for a field being set
537before operating directly on it.
538
539\begin{verbatim}
540class Example {
541 public:
542 Example() :
543 number(10),
544 bigNumber(0),
545 decimals(0),
David Reiss0c90f6f2008-02-06 22:18:40 +0000546 name("thrifty") {}
Mark Slee24b49d32007-03-21 01:24:00 +0000547
548 int32_t number;
549 int64_t bigNumber;
550 double decimals;
551 std::string name;
552
553 struct __isset {
554 __isset() :
555 number(false),
556 bigNumber(false),
557 decimals(false),
558 name(false) {}
559 bool number;
560 bool bigNumber;
561 bool decimals;
562 bool name;
563 } __isset;
564...
565}
David Reiss0c90f6f2008-02-06 22:18:40 +0000566\end{verbatim}
Mark Slee24b49d32007-03-21 01:24:00 +0000567
568\subsection{Case Analysis}
569
570There are four cases in which version mismatches may occur.
571
572\begin{enumerate}
573\item \textit{Added field, old client, new server.} In this case, the old
574client does not send the new field. The new server recognizes that the field
Mark Sleeafaf2762007-04-03 19:47:04 +0000575is not set, and implements default behavior for out-of-date requests.
Mark Slee24b49d32007-03-21 01:24:00 +0000576\item \textit{Removed field, old client, new server.} In this case, the old
577client sends the removed field. The new server simply ignores it.
578\item \textit{Added field, new client, old server.} The new client sends a
579field that the old server does not recognize. The old server simply ignores
580it and processes as normal.
581\item \textit{Removed field, new client, old server.} This is the most
582dangerous case, as the old server is unlikely to have suitable default
583behavior implemented for the missing field. It is recommended that in this
584situation the new server be rolled out prior to the new clients.
585\end{enumerate}
586
587\subsection{Protocol/Transport Versioning}
588The \texttt{TProtocol} abstractions are also designed to give protocol
589implementations the freedom to version themselves in whatever manner they
590see fit. Specifically, any protocol implementation is free to send whatever
591it likes in the \texttt{writeMessageBegin()} call. It is entirely up to the
592implementor how to handle versioning at the protocol level. The key point is
593that protocol encoding changes are safely isolated from interface definition
594version changes.
595
596Note that the exact same is true of the \texttt{TTransport} interface. For
597example, if we wished to add some new checksumming or error detection to the
598\texttt{TFileTransport}, we could simply add a version header into the
599data it writes to the file in such a way that it would still accept old
Mark Sleeafaf2762007-04-03 19:47:04 +0000600log files without the given header.
Mark Slee24b49d32007-03-21 01:24:00 +0000601
602\section{RPC Implementation}
603
604\subsection{TProcessor}
605
606The last core interface in the Thrift design is the \texttt{TProcessor},
607perhaps the most simple of the constructs. The interface is as follows:
608
609\begin{verbatim}
610interface TProcessor {
611 bool process(TProtocol in, TProtocol out)
612 throws TException
613}
614\end{verbatim}
615
616The key design idea here is that the complex systems we build can fundamentally
617be broken down into agents or services that operate on inputs and outputs. In
618most cases, there is actually just one input and output (an RPC client) that
619needs handling.
620
621\subsection{Generated Code}
622
623When a service is defined, we generate a
624\texttt{TProcessor} instance capable of handling RPC requests to that service,
625using a few helpers. The fundamental structure (illustrated in pseudo-C++) is
626as follows:
627
628\begin{verbatim}
629Service.thrift
630 => Service.cpp
631 interface ServiceIf
632 class ServiceClient : virtual ServiceIf
633 TProtocol in
634 TProtocol out
635 class ServiceProcessor : TProcessor
636 ServiceIf handler
637
638ServiceHandler.cpp
639 class ServiceHandler : virtual ServiceIf
640
641TServer.cpp
642 TServer(TProcessor processor,
643 TServerTransport transport,
644 TTransportFactory tfactory,
645 TProtocolFactory pfactory)
646 serve()
647\end{verbatim}
648
Mark Sleec11045a2007-04-01 23:14:38 +0000649From the Thrift definition file, we generate the virtual service interface.
Mark Slee24b49d32007-03-21 01:24:00 +0000650A client class is generated, which implements the interface and
651uses two \texttt{TProtocol} instances to perform the I/O operations. The
652generated processor implements the \texttt{TProcessor} interface. The generated
653code has all the logic to handle RPC invocations via the \texttt{process()}
Mark Sleeafaf2762007-04-03 19:47:04 +0000654call, and takes as a parameter an instance of the service interface, as
Mark Slee24b49d32007-03-21 01:24:00 +0000655implemented by the application developer.
656
Mark Sleeafaf2762007-04-03 19:47:04 +0000657The user provides an implementation of the application interface in separate,
658non-generated source code.
Mark Slee24b49d32007-03-21 01:24:00 +0000659
660\subsection{TServer}
661
662Finally, the Thrift core libraries provide a \texttt{TServer} abstraction.
663The \texttt{TServer} object generally works as follows.
664
665\begin{itemize}
666\item Use the \texttt{TServerTransport} to get a \texttt{TTransport}
667\item Use the \texttt{TTransportFactory} to optionally convert the primitive
668transport into a suitable application transport (typically the
669\texttt{TBufferedTransportFactory} is used here)
670\item Use the \texttt{TProtocolFactory} to create an input and output protocol
671for the \texttt{TTransport}
672\item Invoke the \texttt{process()} method of the \texttt{TProcessor} object
673\end{itemize}
674
675The layers are appropriately separated such that the server code needs to know
676nothing about any of the transports, encodings, or applications in play. The
677server encapsulates the logic around connection handling, threading, etc.
678while the processor deals with RPC. The only code written by the application
Mark Sleec11045a2007-04-01 23:14:38 +0000679developer lives in the definitional Thrift file and the interface
Mark Slee24b49d32007-03-21 01:24:00 +0000680implementation.
681
682Facebook has deployed multiple \texttt{TServer} implementations, including
683the single-threaded \texttt{TSimpleServer}, thread-per-connection
684\texttt{TThreadedServer}, and thread-pooling \texttt{TThreadPoolServer}.
685
686The \texttt{TProcessor} interface is very general by design. There is no
687requirement that a \texttt{TServer} take a generated \texttt{TProcessor}
688object. Thrift allows the application developer to easily write any type of
689server that operates on \texttt{TProtocol} objects (for instance, a server
690could simply stream a certain type of object without any actual RPC method
691invocation).
692
693\section{Implementation Details}
694\subsection{Target Languages}
695Thrift currently supports five target languages: C++, Java, Python, Ruby, and
696PHP. At Facebook, we have deployed servers predominantly in C++, Java, and
697Python. Thrift services implemented in PHP have also been embedded into the
698Apache web server, providing transparent backend access to many of our
699frontend constructs using a \texttt{THttpClient} implementation of the
700\texttt{TTransport} interface.
701
702Though Thrift was explicitly designed to be much more efficient and robust
703than typical web technologies, as we were designing our XML-based REST web
704services API we noticed that Thrift could be easily used to define our
705service interface. Though we do not currently employ SOAP envelopes (in the
Mark Sleeafaf2762007-04-03 19:47:04 +0000706authors' opinions there is already far too much repetitive enterprise Java
Mark Slee24b49d32007-03-21 01:24:00 +0000707software to do that sort of thing), we were able to quickly extend Thrift to
708generate XML Schema Definition files for our service, as well as a framework
709for versioning different implementations of our web service. Though public
710web services are admittedly tangential to Thrift's core use case and design,
711Thrift facilitated rapid iteration and affords us the ability to quickly
712migrate our entire XML-based web service onto a higher performance system
Mark Sleeafaf2762007-04-03 19:47:04 +0000713should the need arise.
Mark Slee24b49d32007-03-21 01:24:00 +0000714
715\subsection{Generated Structs}
716We made a conscious decision to make our generated structs as transparent as
717possible. All fields are publicly accessible; there are no \texttt{set()} and
718\texttt{get()} methods. Similarly, use of the \texttt{isset} object is not
719enforced. We do not include any \texttt{FieldNotSetException} construct.
720Developers have the option to use these fields to write more robust code, but
721the system is robust to the developer ignoring the \texttt{isset} construct
722entirely and will provide suitable default behavior in all cases.
723
Mark Sleeafaf2762007-04-03 19:47:04 +0000724This choice was motivated by the desire to ease application development. Our stated
Mark Slee24b49d32007-03-21 01:24:00 +0000725goal is not to make developers learn a rich new library in their language of
726choice, but rather to generate code that allow them to work with the constructs
727that are most familiar in each language.
728
729We also made the \texttt{read()} and \texttt{write()} methods of the generated
Mark Sleeafaf2762007-04-03 19:47:04 +0000730objects public so that the objects can be used outside of the context
Mark Slee24b49d32007-03-21 01:24:00 +0000731of RPC clients and servers. Thrift is a useful tool simply for generating
732objects that are easily serializable across programming languages.
733
734\subsection{RPC Method Identification}
735Method calls in RPC are implemented by sending the method name as a string. One
736issue with this approach is that longer method names require more bandwidth.
737We experimented with using fixed-size hashes to identify methods, but in the
738end concluded that the savings were not worth the headaches incurred. Reliably
739dealing with conflicts across versions of an interface definition file is
740impossible without a meta-storage system (i.e. to generate non-conflicting
741hashes for the current version of a file, we would have to know about all
742conflicts that ever existed in any previous version of the file).
743
744We wanted to avoid too many unnecessary string comparisons upon
745method invocation. To deal with this, we generate maps from strings to function
746pointers, so that invocation is effectively accomplished via a constant-time
747hash lookup in the common case. This requires the use of a couple interesting
748code constructs. Because Java does not have function pointers, process
749functions are all private member classes implementing a common interface.
750
751\begin{verbatim}
752private class ping implements ProcessFunction {
753 public void process(int seqid,
754 TProtocol iprot,
755 TProtocol oprot)
756 throws TException
757 { ...}
758}
759
760HashMap<String,ProcessFunction> processMap_ =
761 new HashMap<String,ProcessFunction>();
762\end{verbatim}
763
764In C++, we use a relatively esoteric language construct: member function
765pointers.
766
767\begin{verbatim}
768std::map<std::string,
769 void (ExampleServiceProcessor::*)(int32_t,
770 facebook::thrift::protocol::TProtocol*,
771 facebook::thrift::protocol::TProtocol*)>
772 processMap_;
773\end{verbatim}
774
775Using these techniques, the cost of string processing is minimized, and we
776reap the benefit of being able to easily debug corrupt or misunderstood data by
Mark Sleeafaf2762007-04-03 19:47:04 +0000777inspecting it for known string method names.
Mark Slee24b49d32007-03-21 01:24:00 +0000778
779\subsection{Servers and Multithreading}
Mark Sleeafaf2762007-04-03 19:47:04 +0000780Thrift services require basic multithreading to handle simultaneous
Mark Sleec11045a2007-04-01 23:14:38 +0000781requests from multiple clients. For the Python and Java implementations of
Mark Sleeafaf2762007-04-03 19:47:04 +0000782Thrift server logic, the standard threading libraries distributed with the
783languages provide adequate support. For the C++ implementation, no standard multithread runtime
David Reiss0c90f6f2008-02-06 22:18:40 +0000784library exists. Specifically, robust, lightweight, and portable
Mark Sleeafaf2762007-04-03 19:47:04 +0000785thread manager and timer class implementations do not exist. We investigated
David Reiss0c90f6f2008-02-06 22:18:40 +0000786existing implementations, namely \texttt{boost::thread},
Mark Sleec11045a2007-04-01 23:14:38 +0000787\texttt{boost::threadpool}, \texttt{ACE\_Thread\_Manager} and
David Reiss0c90f6f2008-02-06 22:18:40 +0000788\texttt{ACE\_Timer}.
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000789
Mark Sleec11045a2007-04-01 23:14:38 +0000790While \texttt{boost::threads}\cite{boost.threads} provides clean,
791lightweight and robust implementations of multi-thread primitives (mutexes,
792conditions, threads) it does not provide a thread manager or timer
David Reiss0c90f6f2008-02-06 22:18:40 +0000793implementation.
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000794
Mark Sleec11045a2007-04-01 23:14:38 +0000795\texttt{boost::threadpool}\cite{boost.threadpool} also looked promising but
796was not far enough along for our purposes. We wanted to limit the dependency on
Mark Sleeafaf2762007-04-03 19:47:04 +0000797third-party libraries as much as possible. Because\\
Mark Sleec11045a2007-04-01 23:14:38 +0000798\texttt{boost::threadpool} is
799not a pure template library and requires runtime libraries and because it is
Mark Sleeafaf2762007-04-03 19:47:04 +0000800not yet part of the official Boost distribution we felt it was not ready for
Mark Sleec11045a2007-04-01 23:14:38 +0000801use in Thrift. As \texttt{boost::threadpool} evolves and especially if it is
Mark Sleeafaf2762007-04-03 19:47:04 +0000802added to the Boost distribution we may reconsider our decision to not use it.
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000803
804ACE has both a thread manager and timer class in addition to multi-thread
Mark Sleeafaf2762007-04-03 19:47:04 +0000805primitives. The biggest problem with ACE is that it is ACE. Unlike Boost, ACE
Mark Sleec11045a2007-04-01 23:14:38 +0000806API quality is poor. Everything in ACE has large numbers of dependencies on
David Reiss0c90f6f2008-02-06 22:18:40 +0000807everything else in ACE - thus forcing developers to throw out standard
Mark Sleeafaf2762007-04-03 19:47:04 +0000808classes, such as STL collections, in favor of ACE's homebrewed implementations. In
809addition, unlike Boost, ACE implementations demonstrate little understanding
Mark Sleec11045a2007-04-01 23:14:38 +0000810of the power and pitfalls of C++ programming and take no advantage of modern
811templating techniques to ensure compile time safety and reasonable compiler
Mark Sleeafaf2762007-04-03 19:47:04 +0000812error messages. For all these reasons, ACE was rejected. Instead, we chose
813to implement our own library, described in the following sections.
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000814
815\subsection{Thread Primitives}
816
Mark Sleec11045a2007-04-01 23:14:38 +0000817The Thrift thread libraries are implemented in the namespace\\
818\texttt{facebook::thrift::concurrency} and have three components:
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000819\begin{itemize}
Mark Sleec11045a2007-04-01 23:14:38 +0000820\item primitives
821\item thread pool manager
822\item timer manager
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000823\end{itemize}
824
Mark Sleec11045a2007-04-01 23:14:38 +0000825As mentioned above, we were hesitant to introduce any additional dependencies
David Reiss0c90f6f2008-02-06 22:18:40 +0000826on Thrift. We decided to use \texttt{boost::shared\_ptr} because it is so
Mark Sleeafaf2762007-04-03 19:47:04 +0000827useful for multithreaded application, it requires no link-time or
828runtime libraries (i.e. it is a pure template library) and it is due
829to become part of the C++0x standard.
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000830
Mark Sleec11045a2007-04-01 23:14:38 +0000831We implement standard \texttt{Mutex} and \texttt{Condition} classes, and a
David Reiss0c90f6f2008-02-06 22:18:40 +0000832 \texttt{Monitor} class. The latter is simply a combination of a mutex and
Mark Sleeafaf2762007-04-03 19:47:04 +0000833condition variable and is analogous to the \texttt{Monitor} implementation provided for
David Reiss0c90f6f2008-02-06 22:18:40 +0000834the Java \texttt{Object} class. This is also sometimes referred to as a barrier. We
Mark Sleec11045a2007-04-01 23:14:38 +0000835provide a \texttt{Synchronized} guard class to allow Java-like synchronized blocks.
David Reiss0c90f6f2008-02-06 22:18:40 +0000836This is just a bit of syntactic sugar, but, like its Java counterpart, clearly
Mark Sleeafaf2762007-04-03 19:47:04 +0000837delimits critical sections of code. Unlike its Java counterpart, we still
Mark Sleec11045a2007-04-01 23:14:38 +0000838have the ability to programmatically lock, unlock, block, and signal monitors.
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000839
840\begin{verbatim}
Mark Sleec11045a2007-04-01 23:14:38 +0000841void run() {
842 {Synchronized s(manager->monitor);
843 if (manager->state == TimerManager::STARTING) {
844 manager->state = TimerManager::STARTED;
845 manager->monitor.notifyAll();
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000846 }
Mark Sleec11045a2007-04-01 23:14:38 +0000847 }
848}
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000849\end{verbatim}
850
Mark Sleec11045a2007-04-01 23:14:38 +0000851We again borrowed from Java the distinction between a thread and a runnable
852class. A \texttt{Thread} is the actual schedulable object. The
David Reiss0c90f6f2008-02-06 22:18:40 +0000853\texttt{Runnable} is the logic to execute within the thread.
854The \texttt{Thread} implementation deals with all the platform-specific thread
Mark Sleeafaf2762007-04-03 19:47:04 +0000855creation and destruction issues, while the \texttt{Runnable} implementation deals
Mark Sleec11045a2007-04-01 23:14:38 +0000856with the application-specific per-thread logic. The benefit of this approach
David Reiss0c90f6f2008-02-06 22:18:40 +0000857is that developers can easily subclass the Runnable class without pulling in
Aditya Agarwal8c5daa42007-04-05 01:17:52 +0000858platform-specific super-classes.
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000859
860\subsection{Thread, Runnable, and shared\_ptr}
Mark Sleec11045a2007-04-01 23:14:38 +0000861We use \texttt{boost::shared\_ptr} throughout the \texttt{ThreadManager} and
862\texttt{TimerManager} implementations to guarantee cleanup of dead objects that can
863be accessed by multiple threads. For \texttt{Thread} class implementations,
864\texttt{boost::shared\_ptr} usage requires particular attention to make sure
865\texttt{Thread} objects are neither leaked nor dereferenced prematurely while
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000866creating and shutting down threads.
867
Mark Sleec11045a2007-04-01 23:14:38 +0000868Thread creation requires calling into a C library. (In our case the POSIX
Mark Slee09bfd612007-04-04 19:56:41 +0000869thread library, \texttt{libpthread}, but the same would be true for WIN32 threads).
Mark Sleeafaf2762007-04-03 19:47:04 +0000870Typically, the OS makes few, if any, guarantees about when \texttt{ThreadMain}, a C thread's entry-point function, will be called. Therefore, it is
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000871possible that our thread create call,
Mark Sleec11045a2007-04-01 23:14:38 +0000872\texttt{ThreadFactory::newThread()} could return to the caller
873well before that time. To ensure that the returned \texttt{Thread} object is not
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000874prematurely cleaned up if the caller gives up its reference prior to the
Mark Sleec11045a2007-04-01 23:14:38 +0000875\texttt{ThreadMain} call, the \texttt{Thread} object makes a weak referenence to
876itself in its \texttt{start} method.
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000877
Mark Sleec11045a2007-04-01 23:14:38 +0000878With the weak reference in hand the \texttt{ThreadMain} function can attempt to get
879a strong reference before entering the \texttt{Runnable::run} method of the
Mark Sleeafaf2762007-04-03 19:47:04 +0000880\texttt{Runnable} object bound to the \texttt{Thread}. If no strong references to the
David Reiss0c90f6f2008-02-06 22:18:40 +0000881thread are obtained between exiting \texttt{Thread::start} and entering \texttt{ThreadMain}, the weak reference returns \texttt{null} and the function
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000882exits immediately.
883
Mark Sleec11045a2007-04-01 23:14:38 +0000884The need for the \texttt{Thread} to make a weak reference to itself has a
885significant impact on the API. Since references are managed through the
886\texttt{boost::shared\_ptr} templates, the \texttt{Thread} object must have a reference
887to itself wrapped by the same \texttt{boost::shared\_ptr} envelope that is returned
Mark Sleeafaf2762007-04-03 19:47:04 +0000888to the caller. This necessitated the use of the factory pattern.
889\texttt{ThreadFactory} creates the raw \texttt{Thread} object and a
890\texttt{boost::shared\_ptr} wrapper, and calls a private helper method of the class
891implementing the \texttt{Thread} interface (in this case, \texttt{PosixThread::weakRef})
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000892 to allow it to make add weak reference to itself through the
Mark Sleec11045a2007-04-01 23:14:38 +0000893 \texttt{boost::shared\_ptr} envelope.
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000894
Mark Sleec11045a2007-04-01 23:14:38 +0000895\texttt{Thread} and \texttt{Runnable} objects reference each other. A \texttt{Runnable}
Mark Sleeafaf2762007-04-03 19:47:04 +0000896object may need to know about the thread in which it is executing, and a Thread, obviously,
Mark Sleec11045a2007-04-01 23:14:38 +0000897needs to know what \texttt{Runnable} object it is hosting. This interdependency is
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000898further complicated because the lifecycle of each object is independent of the
Mark Slee8142b9d2007-04-03 05:49:12 +0000899other. An application may create a set of \texttt{Runnable} object to be reused in different threads, or it may create and forget a \texttt{Runnable} object
David Reiss0c90f6f2008-02-06 22:18:40 +0000900once a thread has been created and started for it.
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000901
Mark Sleec11045a2007-04-01 23:14:38 +0000902The \texttt{Thread} class takes a \texttt{boost::shared\_ptr} reference to the hosted
Mark Sleeafaf2762007-04-03 19:47:04 +0000903\texttt{Runnable} object in its constructor, while the \texttt{Runnable} class has an
Mark Sleec11045a2007-04-01 23:14:38 +0000904explicit \texttt{thread} method to allow explicit binding of the hosted thread.
Mark Sleeafaf2762007-04-03 19:47:04 +0000905\texttt{ThreadFactory::newThread} binds the objects to each other.
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000906
907\subsection{ThreadManager}
908
David Reiss0c90f6f2008-02-06 22:18:40 +0000909\texttt{ThreadManager} creates a pool of worker threads and
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000910allows applications to schedule tasks for execution as free worker threads
David Reiss0c90f6f2008-02-06 22:18:40 +0000911become available. The \texttt{ThreadManager} does not implement dynamic
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000912thread pool resizing, but provides primitives so that applications can add
David Reiss0c90f6f2008-02-06 22:18:40 +0000913and remove threads based on load. This approach was chosen because
914implementing load metrics and thread pool size is very application
Mark Sleec11045a2007-04-01 23:14:38 +0000915specific. For example some applications may want to adjust pool size based
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000916on running-average of work arrival rates that are measured via polled
Mark Sleec11045a2007-04-01 23:14:38 +0000917samples. Others may simply wish to react immediately to work-queue
918depth high and low water marks. Rather than trying to create a complex
David Reiss0c90f6f2008-02-06 22:18:40 +0000919API abstract enough to capture these different approaches, we
920simply leave it up to the particular application and provide the
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000921primitives to enact the desired policy and sample current status.
922
923\subsection{TimerManager}
924
Mark Sleeafaf2762007-04-03 19:47:04 +0000925\texttt{TimerManager} allows applications to schedule
David Reiss0c90f6f2008-02-06 22:18:40 +0000926 \texttt{Runnable} objects for execution at some point in the future. Its specific task
Mark Sleec11045a2007-04-01 23:14:38 +0000927is to allows applications to sample \texttt{ThreadManager} load at regular
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000928intervals and make changes to the thread pool size based on application policy.
Mark Sleec11045a2007-04-01 23:14:38 +0000929Of course, it can be used to generate any number of timer or alarm events.
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000930
Mark Sleec11045a2007-04-01 23:14:38 +0000931The default implementation of \texttt{TimerManager} uses a single thread to
David Reiss0c90f6f2008-02-06 22:18:40 +0000932execute expired \texttt{Runnable} objects. Thus, if a timer operation needs to
Marc Slemko10b3bdb2007-04-01 09:14:05 +0000933do a large amount of work and especially if it needs to do blocking I/O,
934that should be done in a separate thread.
Mark Slee24b49d32007-03-21 01:24:00 +0000935
936\subsection{Nonblocking Operation}
937Though the Thrift transport interfaces map more directly to a blocking I/O
938model, we have implemented a high performance \texttt{TNonBlockingServer}
Mark Sleeafaf2762007-04-03 19:47:04 +0000939in C++ based on \texttt{libevent} and the \texttt{TFramedTransport}. We
Mark Slee24b49d32007-03-21 01:24:00 +0000940implemented this by moving all I/O into one tight event loop using a
941state machine. Essentially, the event loop reads framed requests into
942\texttt{TMemoryBuffer} objects. Once entire requests are ready, they are
943dispatched to the \texttt{TProcessor} object which can read directly from
944the data in memory.
945
946\subsection{Compiler}
Mark Sleeafaf2762007-04-03 19:47:04 +0000947The Thrift compiler is implemented in C++ using standard \texttt{lex}/\texttt{yacc}
948lexing and parsing. Though it could have been implemented with fewer
949lines of code in another language (i.e. Python Lex-Yacc (PLY) or \texttt{ocamlyacc}), using C++
Mark Slee24b49d32007-03-21 01:24:00 +0000950forces explicit definition of the language constructs. Strongly typing the
951parse tree elements (debatably) makes the code more approachable for new
952developers.
953
954Code generation is done using two passes. The first pass looks only for
955include files and type definitions. Type definitions are not checked during
956this phase, since they may depend upon include files. All included files
957are sequentially scanned in a first pass. Once the include tree has been
Mark Sleeafaf2762007-04-03 19:47:04 +0000958resolved, a second pass over all files is taken that inserts type definitions
Mark Slee24b49d32007-03-21 01:24:00 +0000959into the parse tree and raises an error on any undefined types. The program is
960then generated against the parse tree.
961
962Due to inherent complexities and potential for circular dependencies,
963we explicitly disallow forward declaration. Two Thrift structs cannot
964each contain an instance of the other. (Since we do not allow \texttt{null}
965struct instances in the generated C++ code, this would actually be impossible.)
966
Aditya Agarwalaf524ee2007-03-31 08:28:06 +0000967\subsection{TFileTransport}
David Reiss0c90f6f2008-02-06 22:18:40 +0000968The \texttt{TFileTransport} logs Thrift requests/structs by
969framing incoming data with its length and writing it out to disk.
970Using a framed on-disk format allows for better error checking and
Mark Sleeafaf2762007-04-03 19:47:04 +0000971helps with the processing of a finite number of discrete events. The\\
David Reiss0c90f6f2008-02-06 22:18:40 +0000972\texttt{TFileWriterTransport} uses a system of swapping in-memory buffers
973to ensure good performance while logging large amounts of data.
Mark Sleeafaf2762007-04-03 19:47:04 +0000974A Thrift log file is split up into chunks of a specified size; logged messages
David Reiss0c90f6f2008-02-06 22:18:40 +0000975are not allowed to cross chunk boundaries. A message that would cross a chunk
976boundary will cause padding to be added until the end of the chunk and the
Mark Sleeafaf2762007-04-03 19:47:04 +0000977first byte of the message are aligned to the beginning of the next chunk.
David Reiss0c90f6f2008-02-06 22:18:40 +0000978Partitioning the file into chunks makes it possible to read and interpret data
979from a particular point in the file.
Aditya Agarwalaf524ee2007-03-31 08:28:06 +0000980
Mark Sleec11045a2007-04-01 23:14:38 +0000981\section{Facebook Thrift Services}
Aditya Agarwaladf3e7f2007-03-31 16:56:14 +0000982Thrift has been employed in a large number of applications at Facebook, including
Mark Sleeafaf2762007-04-03 19:47:04 +0000983search, logging, mobile, ads and the developer platform. Two specific usages are discussed below.
Aditya Agarwaladf3e7f2007-03-31 16:56:14 +0000984
985\subsection{Search}
Mark Sleeafaf2762007-04-03 19:47:04 +0000986Thrift is used as the underlying protocol and transport layer for the Facebook Search service.
987The multi-language code generation is well suited for search because it allows for application
Aditya Agarwaladf3e7f2007-03-31 16:56:14 +0000988development in an efficient server side language (C++) and allows the Facebook PHP-based web application
989to make calls to the search service using Thrift PHP libraries. There is also a large
David Reiss0c90f6f2008-02-06 22:18:40 +0000990variety of search stats, deployment and testing functionality that is built on top
Mark Sleeafaf2762007-04-03 19:47:04 +0000991of generated Python code. Additionally, the Thrift log file format is
David Reiss0c90f6f2008-02-06 22:18:40 +0000992used as a redo log for providing real-time search index updates. Thrift has allowed the
993search team to leverage each language for its strengths and to develop code at a rapid pace.
Aditya Agarwaladf3e7f2007-03-31 16:56:14 +0000994
995\subsection{Logging}
996The Thrift \texttt{TFileTransport} functionality is used for structured logging. Each
997service function definition along with its parameters can be considered to be
David Reiss0c90f6f2008-02-06 22:18:40 +0000998a structured log entry identified by the function name. This log can then be used for
Mark Sleeafaf2762007-04-03 19:47:04 +0000999a variety of purposes, including inline and offline processing, stats aggregation and as a redo log.
Aditya Agarwaladf3e7f2007-03-31 16:56:14 +00001000
Mark Slee24b49d32007-03-21 01:24:00 +00001001\section{Conclusions}
1002Thrift has enabled Facebook to build scalable backend
1003services efficiently by enabling engineers to divide and conquer. Application
Mark Sleeafaf2762007-04-03 19:47:04 +00001004developers can focus on application code without worrying about the
Mark Slee24b49d32007-03-21 01:24:00 +00001005sockets layer. We avoid duplicated work by writing buffering and I/O logic
1006in one place, rather than interspersing it in each application.
1007
1008Thrift has been employed in a wide variety of applications at Facebook,
Mark Sleeafaf2762007-04-03 19:47:04 +00001009including search, logging, mobile, ads, and the developer platform. We have
Mark Slee24b49d32007-03-21 01:24:00 +00001010found that the marginal performance cost incurred by an extra layer of
Mark Sleeafaf2762007-04-03 19:47:04 +00001011software abstraction is far eclipsed by the gains in developer efficiency and
Mark Slee24b49d32007-03-21 01:24:00 +00001012systems reliability.
1013
1014\appendix
1015
1016\section{Similar Systems}
1017The following are software systems similar to Thrift. Each is (very!) briefly
1018described:
1019
1020\begin{itemize}
1021\item \textit{SOAP.} XML-based. Designed for web services via HTTP, excessive
1022XML parsing overhead.
1023\item \textit{CORBA.} Relatively comprehensive, debatably overdesigned and
1024heavyweight. Comparably cumbersome software installation.
1025\item \textit{COM.} Embraced mainly in Windows client softare. Not an entirely
1026open solution.
1027\item \textit{Pillar.} Lightweight and high-performance, but missing versioning
1028and abstraction.
1029\item \textit{Protocol Buffers.} Closed-source, owned by Google. Described in
1030Sawzall paper.
1031\end{itemize}
1032
1033\acks
1034
1035Many thanks for feedback on Thrift (and extreme trial by fire) are due to
Aditya Agarwalaf524ee2007-03-31 08:28:06 +00001036Martin Smith, Karl Voskuil and Yishan Wong.
Mark Slee24b49d32007-03-21 01:24:00 +00001037
1038Thrift is a successor to Pillar, a similar system developed
1039by Adam D'Angelo, first while at Caltech and continued later at Facebook.
1040Thrift simply would not have happened without Adam's insights.
1041
Marc Slemko10b3bdb2007-04-01 09:14:05 +00001042\begin{thebibliography}{}
Mark Slee24b49d32007-03-21 01:24:00 +00001043
Marc Slemko10b3bdb2007-04-01 09:14:05 +00001044\bibitem{boost.threads}
1045Kempf, William,
1046``Boost.Threads'',
1047\url{http://www.boost.org/doc/html/threads.html}
Mark Slee24b49d32007-03-21 01:24:00 +00001048
Marc Slemko10b3bdb2007-04-01 09:14:05 +00001049\bibitem{boost.threadpool}
1050Henkel, Philipp,
1051``threadpool'',
1052\url{http://threadpool.sourceforge.net}
1053
1054\end{thebibliography}
Mark Slee24b49d32007-03-21 01:24:00 +00001055
1056\end{document}