• Stars
    star
    214
  • Rank 184,678 (Top 4 %)
  • Language
    Elixir
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Pure Elixir Thrift Implementation

Elixir Thrift

Hex Version Hex Docs Build Status Coverage Status

This package contains an implementation of Thrift for Elixir. It includes a Thrift IDL parser, an Elixir code generator, and binary framed client and server implementations.

The generated serialization code is highly optimized and has been measured at 10 and 25 times fasterwhy? than the code generated by the Apache Thrift Erlang implementation.

Project Status

Version 2.0 is under actively development and should be released soon. It is a complete rewrite that drops the Apache Thrift dependency and implements everything in pure Elixir.

Getting Started

Until version 2.0 is released, you'll need to track the master branch directly:

{:thrift, github: "pinterest/elixir-thrift"}

This package includes a Mix compiler task that automates Thrift code generation. Prepend :thrift to your project's :compilers list and add a new top-level :thrift configuration key. The only necessary compiler option is :files, which defines the list of Thrift files that should be compiled.

# mix.exs
defmodule MyProject.Mixfile do
  # ...
  def project do
    [
      # ...
      compilers: [:thrift | Mix.compilers],
      thrift: [
        files: Path.wildcard("thrift/**/*.thrift")
      ]
    ]
  end
end

RPC Service Support

We provide full client and server support for Thrift RPC services. The examples below are based on this simplified service definition:

service Service {
  i64 add(1: i64 left, 2: i64 right)
}

You can also check out the full example project for a complete client and server implementation of the sample calculator application.

Clients

You interact with Thrift services using generated, service-specific interface modules. These modules handle type conversions and make calling the service's remote functions easier.

iex> alias Calculator.Generated.Service.Binary.Framed.Client
iex> {:ok, client} = Client.start_link("localhost", 9090, [])
iex> {:ok, result} = Client.add(client, 10, 20)
{:ok, 30}

We generate two versions of each function defined by the Thrift service's interface: one that returns a standard result tuple, and a ! variant that returns a single result value but raises an exception if an error occurs.

@spec add(pid(), integer(), integer(), Client.options()) :: {:ok, integer()} | {:error, any()}
def add(client, left, right, rpc_opts \\ [])

@spec add!(pid(), integer(), integer(), Client.options()) :: integer()
def add!(client, left, right, rpc_opts \\ [])

Servers

In order to start a Thrift server, you will need to provide a callback module that implements the functions described by its service interface. Fortunately, a behaviour module will be automatically generated for you, complete with success typing.

defmodule Calculator.ServiceHandler do
  @behaviour Calculator.Generated.Service.Handler

  @impl true
  def add(left, right) do
    left + right
  end
end

Then provide your handler module when starting the server process:

iex> alias Calculator.Generated.Service.Binary.Framed.Server
iex> {:ok, server} = Server.start_link(Calculator.ServiceHandler, 9090, [])

All RPC calls to the server will be delegated to the handler module. The server provides a supervisor which can be added to your application's supervision tree. It's important to add it to your supervision tree with type :supervisor and not :worker.

defmodule Calculator.Application
  alias Calculator.Generated.Service.Binary.Framed.Server

  def start(_type, _args) do
    children = [
      server_child_spec(9090)
    ]

    opts = [strategy: :one_for_one, name: Calculator.Supervisor]
    Supervisor.start_link(children, opts)
  end

  defp server_child_spec(port) do
    %{
      id: Server,
      start: {Server, :start_link, [Calculator.ServiceHandler, port]},
      type: :supervisor
    }
  end
end

Serialization

A BinaryProtocol module is generated for each Thrift struct, union, and exception type. You can use this interface to easily serialize and deserialize your own types.

iex> alias Calculator.Generated.Vector
iex> data = %Vector{x: 1, y: 2, z: 3}
|> Vector.BinaryProtocol.serialize
|> IO.iodata_to_binary
iex> Vector.BinaryProtocol.deserialize(data)
{%Calculator.Generated.Vector{x: 1.0, y: 2.0, z: 3.0}, ""}

Thrift IDL Parsing

The Thrift.Parser module parses Thrift IDL documents and produces an abstract syntax tree. You can use these features to support additional languages, protocols, and servers.

Thrift.Parser.parse("enum Colors { RED, GREEN, BLUE }")
%Thrift.AST.Schema{constants: %{},
 enums: %{Colors: %Thrift.AST.TEnum{name: :Colors,
    values: [RED: 1, GREEN: 2, BLUE: 3]}}, exceptions: %{}, includes: [],
 namespaces: %{}, services: %{}, structs: %{}, thrift_namespace: nil,
 typedefs: %{}, unions: %{}}

Debugging

In order to debug your Thrift RPC calls, we recommend you use thrift-tools. It is a set of tools to introspect Apache Thrift traffic.

Try something like:

$ pip install thrift-tools
$ sudo thrift-tool --iface eth0 --port 9090 dump --show-all --pretty

FAQ

Why is it faster than the Apache implementation?

The Apache Thrift implementation uses C++ to write Erlang modules that describe Thrift data structures and then uses these descriptions to turn your Thrift data into bytes. It consults these descriptions every time Thrift data is serialized/deserialized. This on-the-fly conversion costs CPU time.

Additionally, this separation of concerns in Apache Thrift prevent the Erlang VM from doing the best job that it can do during serialization.

Our implementation uses Elixir to write Elixir code that's specific to your Thrift structures. This serialization logic is then compiled, and that compiled code is what converts your data to and from serialized bytes. We've spent a lot of time making sure that the generated code takes advantage of several of the optimizations that the Erlang VM provides.

What tradeoffs have you made to get this performance?

Thrift has the following concepts:

  1. Protocols Define a conversion of data into bytes.
  2. Transports Define how bytes move; across a network or in and out of a file.
  3. Processors Encapsulate reading from streams and doing something with the data. Processors are generated by the Thrift compiler.

In Apache Thrift, Protocols and Transports can be mixed and matched. However, our implementation does the mixing and matching for you and generates a combination of (Protocol + Transport + Processor). This means that if you need to support a new Protocol or Transport, you will need to integrate it into this project.

Presently, we implement:

  • Binary Protocol, Framed Client
  • Binary Protocol, Framed Server

We are more than willing to accept contributions that add more!

More Repositories

1

ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
Kotlin
6,192
star
2

gestalt

A set of React UI components that supports Pinterest’s design language
TypeScript
4,240
star
3

PINRemoteImage

A thread safe, performant, feature rich image fetcher
Objective-C
4,009
star
4

PINCache

Fast, non-deadlocking parallel object cache for iOS, tvOS and OS X
Objective-C
2,660
star
5

querybook

Querybook is a Big Data Querying UI, combining collocated table metadata and a simple notebook interface.
TypeScript
1,923
star
6

secor

Secor is a service implementing Kafka log persistence
Java
1,845
star
7

teletraan

Teletraan is Pinterest's deploy system.
Java
1,807
star
8

knox

Knox is a secret management service
Go
1,229
star
9

pinball

Pinball is a scalable workflow manager
JavaScript
1,048
star
10

mysql_utils

Pinterest MySQL Management Tools
Python
883
star
11

snappass

Share passwords securely
Python
837
star
12

elixometer

A light Elixir wrapper around exometer.
Elixir
827
star
13

pymemcache

A comprehensive, fast, pure-Python memcached client.
Python
771
star
14

bonsai

Understand the tree of dependencies inside your webpack bundles, and trim away the excess.
JavaScript
738
star
15

rocksplicator

RocksDB Replication
C++
662
star
16

esprint

Fast eslint runner
JavaScript
661
star
17

bender

An easy-to-use library for creating load testing applications
Go
658
star
18

DoctorK

DoctorK is a service for Kafka cluster auto healing and workload balancing
Java
633
star
19

plank

A tool for generating immutable model objects
Swift
469
star
20

riffed

Provides idiomatic Elixir bindings for Apache Thrift
Elixir
307
star
21

thrift-tools

thrift-tools is a library and a set of tools to introspect Apache Thrift traffic.
Python
233
star
22

widgets

JavaScript widgets, including the Pin It button.
JavaScript
210
star
23

singer

A high-performance, reliable and extensible logging agent for uploading data to Kafka, Pulsar, etc.
Java
178
star
24

terrapin

Serving system for batch generated data sets
Java
176
star
25

git-stacktrace

Easily figure out which git commit caused a given stacktrace
Python
158
star
26

jbender

An easy-to-use library for creating load testing applications.
Java
156
star
27

ptracer

A library for ptrace-based tracing of Python programs
Python
155
star
28

react-pinterest

JavaScript
151
star
29

pinlater

PinLater is a Thrift service to manage scheduling and execution of asynchronous jobs.
Java
135
star
30

memq

MemQ is an efficient, scalable cloud native PubSub system
Java
129
star
31

api-quickstart

Code that makes it easy to get started with the Pinterest API.
Python
122
star
32

it-cpe-cookbooks

A suite of Chef cookbooks that we use to manage our fleet of client devices
Ruby
118
star
33

psc

PubSubClient (PSC)
Java
117
star
34

pinterest-api-demo

JavaScript
106
star
35

PINOperation

Objective-C
104
star
36

orion

Management and automation platform for Stateful Distributed Systems
Java
101
star
37

soundwave

A searchable EC2 Inventory store
Java
96
star
38

PINFuture

An Objective-C future implementation that aims to provide maximal type safety
Objective-C
83
star
39

kingpin

KingPin is the toolset used at Pinterest for service discovery and application configuration.
Python
69
star
40

arcanist-linters

A collection of custom Arcanist linters
PHP
63
star
41

pagerduty-monit

Wrapper scripts to integrate monit and PagerDuty.
Shell
60
star
42

pinrepo

Pinrepo is a highly scalable solution for storing and serving build artifacts such as debian packages, maven jars and pypi packages.
Python
58
star
43

transformer_user_action

Transformer-based Realtime User Action Model for Recommendation at Pinterest
Python
49
star
44

quasar-thrift

A Thrift server that uses Quasar's lightweight threads to handle connections.
Java
47
star
45

pinterest-python-sdk

An SDK that makes it quick and easy to build applications with Pinterest API.
Python
47
star
46

yuvi

Yuvi is an in-memory storage engine for recent time series metrics data.
Java
45
star
47

atg-research

Python
41
star
48

slackminion

A python bot framework for slack
Python
22
star
49

api-description

OpenAPI descriptions for Pinterest's REST API
18
star
50

l10nmessages

L10nMessages is a library that makes internationalization (i18n) and localization (l10n) of Java applications easy and safe.
Java
17
star
51

thriftcheck

A linter for Thrift IDL files
Go
16
star
52

arcanist-owners

An Arcanist extension for displaying file ownership information
PHP
16
star
53

tiered-storage

Pinterest's simplified and efficient Tiered Storage implementation for Kafka
Java
13
star
54

.github

Pinterest's Open Source Project Template
12
star
55

homebrew-tap

macOS Homebrew formulas to install Pinterest open source software
Ruby
12
star
56

pinterest-python-generated-api-client

This is the auto-generated code using OpenAPI generator. Generated code comprises HTTP requests to various v5 API endpoints.
Python
12
star
57

vscode-gestalt

Visual Studio Code extension for Gestalt, Pinterest's design system
TypeScript
9
star
58

wheeljack

Work with interdependent python repositories seemlessly.
Python
8
star
59

ffffound

FFFFOUND Import tool for Pinterest
HTML
8
star
60

vscode-package-watcher

Watch package lock files and suggest to re-run npm or yarn.
TypeScript
6
star
61

graphql-lint-rules

Pinterest GraphQL Lint Rules
TypeScript
6
star
62

ss-gtm-template

This is a repository to implement the Google Tag Manager server-side tag template for Pinterest Conversions API to be deployed into Google Community Template Gallery.
Smarty
5
star
63

pinterest-magento2-extension

PHP
4
star
64

Pinterest-Salesforce-Commerce-Cartridge

JavaScript
4
star
65

figma-calculations

TypeScript
2
star
66

slate

Resource Lifecycle Management framework
Java
1
star