Piqi-RPC. Exposing Erlang services via JSON, XML and Google Protocol Buffers over HTTP. Friday, March 25, 2011

Similar documents
Protocol Buffers, grpc

Googles Approach for Distributed Systems. Slides partially based upon Majd F. Sakr, Vinay Kolar, Mohammad Hammoud and Google PrototBuf Tutorial

Semesterprojekt Implementierung eines Brettspiels (inklusive computergesteuerter Spieler) Remote Procedure Calls

CSCI-1680 RPC and Data Representation. Rodrigo Fonseca

CSCI-1680 RPC and Data Representation John Jannotti

CSCI-1680 RPC and Data Representation. Rodrigo Fonseca

Thrift specification - Remote Procedure Call

External data representation

Google Protocol Buffers for Embedded IoT

Transitioning from C# to Scala Using Apache Thrift. Twitter Finagle

erlocator Location, location, location.

JSON is a light-weight alternative to XML for data-interchange JSON = JavaScript Object Notation

CSCI-GA Scripting Languages

Distributed Systems. 03. Remote Procedure Calls. Paul Krzyzanowski. Rutgers University. Fall 2017

Distributed Programming and Remote Procedure Calls (RPC): Apache Thrift. George Porter CSE 124 February 19, 2015

Composer Help. Web Request Common Block

JSON as an XML Alternative. JSON is a light-weight alternative to XML for datainterchange

Chapter 4 Communication

CS 417 9/18/17. Paul Krzyzanowski 1. Socket-based communication. Distributed Systems 03. Remote Procedure Calls. Sample SMTP Interaction

Erlang 101. Google Doc

TH IRD EDITION. Python Cookbook. David Beazley and Brian K. Jones. O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Tokyo

Distributed Systems 8. Remote Procedure Calls

Operating Systems. 18. Remote Procedure Calls. Paul Krzyzanowski. Rutgers University. Spring /20/ Paul Krzyzanowski

Microservices with Node.js

Cross-Platform Data Models and API Using grpc

Apache Thrift Introduction & Tutorial

JSON and COBOL. Tom Ross Captain COBOL GSE Nordic Reykjavik June 3, 2016

grpc - A solution for RPCs by Google Distributed Systems Seminar at Charles University in Prague, Nov 2016 Jan Tattermusch - grpc Software Engineer

Writing REST APIs with OpenAPI and Swagger Ada

Apache Avro. Sharing and managing data efficiently

Avro Specification

s Protocol Buffer Knight76 at gmail.com

Introduction to RPC, Apache Thrift Workshop. Tomasz Powchowicz

Avro Specification

Developing a Web Server Platform with SAPI support for AJAX RPC using JSON

Comparative Survey of Object Serialization Techniques and the Programming Supports

spindle Documentation

Java vs JavaScript. For Enterprise Web Applications. Chris Bailey IBM Runtime Technologies IBM Corporation

Foundations of Python

Comp215: Structured Data Semantics

CS 378 Big Data Programming

By Lucas Marshall. All materials Copyright Developer Shed, Inc. except where otherwise noted.

REST. Web-based APIs

416 Distributed Systems. RPC Day 2 Jan 11, 2017

Teach your (micro)services speak Protocol Buffers with grpc.

Elliotte Rusty Harold August From XML to Flat Buffers: Markup in the Twenty-teens

Haskell in the corporate environment. Jeff Polakow October 17, 2008


CS558 Programming Languages

Smashing Node.JS: JavaScript Everywhere

TRAINING GUIDE. Lucity Web Services APIs

Erlang and Thrift for Web Development

REST Web Services Objektumorientált szoftvertervezés Object-oriented software design

Notes. Submit homework on Blackboard The first homework deadline is the end of Sunday, Feb 11 th. Final slides have 'Spring 2018' in chapter title

CSE 544 Principles of Database Management Systems. Lecture 4: Data Models a Never-Ending Story

Introduction to Programming Using Java (98-388)

B4M36DS2, BE4M36DS2: Database Systems 2

Working with Database. Client-server sides AJAX JSON Data formats Working with JSON data Request Response Bytes Database

Controller/server communication

416 Distributed Systems. RPC Day 2 Jan 12, 2018

Programming Language Basics

Programming in Scala Second Edition

C 1. Recap: Finger Table. CSE 486/586 Distributed Systems Remote Procedure Call. Chord: Node Joins and Leaves. Recall? Socket API

CS 498RK FALL RESTFUL APIs

vrealize Log Insight Developer Resources

Short Notes of CS201

vrealize Log Insight Developer Resources Update 1 Modified on 03 SEP 2017 vrealize Log Insight 4.0

ArcGIS for Server Administration API for C# Developers. Philip

CS201 - Introduction to Programming Glossary By

Python INTRODUCTION: Understanding the Open source Installation of python in Linux/windows. Understanding Interpreters * ipython.

EPL660: Information Retrieval and Search Engines Lab 3

RMI & RPC. CS 475, Spring 2019 Concurrent & Distributed Systems

CSE 544 Principles of Database Management Systems. Fall 2016 Lecture 4 Data models A Never-Ending Story

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

CSE 451 Midterm 1. Name:

CSC Web Technologies, Spring Web Data Exchange Formats

JHDF5 (HDF5 for Java) 14.12

PROCE55 Mobile: Web API App. Web API.

A JSON Data Processing Language. Audrey Copeland, Walter Meyer, Taimur Samee, Rizwan Syed

The NetRexx Interpreter

Simple Object Access Protocol (SOAP) Reference: 1. Web Services, Gustavo Alonso et. al., Springer

ReJSON = { "activity": "new trick" } Itamar

plisp: A Friendly Lisp IDE for Beginners Rajesh Jayaprakash Tata Consultancy Services Chennai, India

DS 2009: middleware. David Evans

Lecture 15: Frameworks for Application-layer Communications

Lecture 15: Frameworks for Application-layer Communications

Armide Documentation. Release Kyle Mayes

Requests Mock Documentation

Rudy: a small web server. Johan Montelius. October 2, 2016

Symbolic Computation and Common Lisp

Global Servers. The new masters

Web Robots Platform. Web Robots Chrome Extension. Web Robots Portal. Web Robots Cloud

Announcements. Lab 3 is due on Wednesday by 11:59 PM

Integrated Software Environment. Part 2

Remote Procedure Calls

Lecture 8: February 19

CSE 115. Introduction to Computer Science I

The current topic: Python. Announcements. Python. Python

Lesson 14 SOA with REST (Part I)

Monitor your containers with the Elastic Stack. Monica Sarbu

Transcription:

Piqi-RPC Exposing Erlang services via JSON, XML and Google Protocol Buffers over HTTP 1

Anton Lavrik http://piqi.org http://www.alertlogic.com 2

Overview Call Erlang functions using HTTP POST : Server = Erlang Client = anything else that talks over HTTP, e.g. CURL, JavaScript, PHP,... No state is maintained between the calls Input and output parameters can be represented in JSON, XML or Protocol Buffers There is Piqi-RPC command-line client -- interprets command-line arguments as input parameters 3

Inspiration: solving a real problem Make it easy to connect Erlang services with other programming languages and environments: internal and public APIs O&M and development tools (Erlang shell and rpc:call are not enough) If you have to do it manually: manual parsing of XML, JSON -- not an easy task in Erlang adding command-line interface, other formats make it practically impossible to manage 4

Inspiration: the Piqi project Most of parts were already implemented as a part of the Piqi project: type-based converter between JSON, XML, Protocol Buffers data serialization system for Erlang compatible with Protocol Buffers All I had to do is to put these things together and communication layer on top of HTTP in reality, turned out to be a lot of work and a lot of fun! 5

Layer I: Piqi serialization & data conversion 6

What is type-based data serialization 1. Describe your data using a high-level data definition language (Piqi) 2. Use Interface compiler (piqic) to generate serialization and deserialization functions for the target programming language 3. Use the generated functions to encode and decode program data to/from serialization format Examples: Cross-language serialization systems: Protocol Buffers, ASN.1 Many middleware and RPC systems: Apache Thrift, CORBA, UBF,... 7

Demo #1: using Piqi for JSON, XML and Protobuf serialization and deserialization in Erlang program examples/erlang/io_json_xml_pb.erl 8

The Piqi project 9

Piqi - data definition language primitive types: int, bool, float, string, binary, verbatim text user-defined types: record, variant (aka tagged unions), enum, list, alias no tuples, atoms, function closures, polymorphism includes and imports type extensions functions the Piqi language itself is defined in the self-specification 10

Example: person.piqi - Piqi type definition module 11

% definition of a record type.record [.name person % record name.field [.name name % field name.type string % field type ].field [.name id.type int ].field [.name email.type string.optional % field is optional ].field [.name phone.type phone-number.repeated % field can be repeated 0 or more times ] ] % (continued on the next slide...) 12

% (example continued).record [.name phone-number.field [.name number.type string ].field [.name type.type phone-type.optional.default.home % default value for an optional field ] ] % definition of an enum type.enum [.name phone-type.option [.name mobile ].option [.name home ].option [.name work ] ] 13

Example: person.piqi in Piqi-light syntax type person = { - name :: string() - id :: int()? email :: string() * phone :: phone-number() } type phone-number = { - number :: string()? type :: phone-type() =.home } type phone-type = mobile home work generated by piqi light person.piqi command 14

Example: person.piqi definitions mapped to Erlang 15

-record(person, { name :: string() binary(), id :: integer(), email :: string() binary(), phone = [] :: [phone_number()] }). -record(phone_number, { number :: string() binary(), type :: phone_type() }). -type(phone_type() :: mobile home work ). -type(person() :: #person{}). -type(phone_number() :: #phone_number{}). <mod>_piqi.hrl generated by piqic erang person.piqi command 16

Example: person.piqi.proto (Protobuf definition) message person { required string name = 1; required sint32 id = 2; optional string email = 3; repeated phone_number phone = 4; } message phone_number { required string number = 1; optional phone_type type = 2 [default = home]; } enum phone_type { mobile = 1; home = 2; work = 3; } generated by piqi to-proto person.piqi command 17

Piq - data representation language Syntax is designed to be convenient for viewing and editing data: lighter than JSON: no quotes around field names, no, separators less parenthesis than in S-expressions kind-of like Erlang proplists, but no need to wrap variants in tuples comments verbatim text literals repeated fields, like in Protocol Buffers and XML 18

Example: person.piq (corresponds to the previously defined person data type) :person/person [.name "J. Random Hacker".id 0.email "j.r.hacker@example.com".phone [.number "(111) 123 45 67" % NOTE: phone type is "home" by default ].phone [.number "(222) 123 45 67".mobile ].phone [.number "(333) 123 45 67".work ] ] 19

Example: person.piq.json { } "name": "J. Random Hacker", "id": 0, "email": "j.r.hacker@example.com", "phone": [ { "number": "(111) 123 45 67", "type": "home" }, { "number": "(222) 123 45 67", "type": "mobile" }, { "number": "(333) 123 45 67", "type": "work" } ] generated by piqi convert -t json person.piq command 20

Example: person.piq.xml <?xml version="1.0" encoding="utf-8"?> <person> <name>j. Random Hacker</name> <id>0</id> <email>j.r.hacker@example.com</email> <phone> <number>(111) 123 45 67</number> <type>home</type> </phone> <phone> <number>(222) 123 45 67</number> <type>mobile</type> </phone> <phone> <number>(333) 123 45 67</number> <type>work</type> </phone> </person> generated by piqi convert -t xml person.piq command 21

XML and JSON mapping details JSON & XML: UTF-8 only Floating point NaN, Infinity and -Infinity are represented as strings Base64 for binaries XML (in addition to the above): No support for DTD, Schemas, namespaces and attributes Whitespace is significant in text nodes 22

Example: piqi getopt piqi getopt -t (json xml piq) --piqtype person/person -- \ --name "J. Random Hacker" \ --id 0 \ --email "j.r.hacker@example.com" \ --phone [ --number "(111) 123 45 67" ] \ --phone [ \ --number "(222) 123 45 67" \ --mobile \ ] \ --phone [ \ --number "(333) 123 45 67" \ --work \ ] 23

Example: piqi getopt (short names) piqi getopt -t (json xml piq) --piqtype person/person -- \ "J. Random Hacker" \ -i 0 \ -e "j.r.hacker@example.com" \ -p [ "(111) 123 45 67" ] \ -p [ \ "(222) 123 45 67" \ -m \ ] \ -p [ \ "(333) 123 45 67" \ -w \ ] 24

Layer II: Piqi-RPC & HTTP 25

Data serialization workflow (from a previous slide) 1. Describe your data using in Piqi 2. Use Interface compiler (piqic erlang) to generate serialization and deserialization functions for Erlang 3. Use the generated functions to encode and decode program data to/from serialization format 26

Piqi-RPC workflow 1. Describe your data and functions in Piqi 2. Use Interface compiler (piqic piqic-erlang-rpc) to generate serialization and deserialization functions and server stubs for Erlang. 3. Use the generated functions to encode and decode program data to/from serialization format 3. Implement server callback functions 4. Run piqi_rpc:start(), piqi_rpc:add_service(..., <URL path> ) Your services are up and running at http://.../<url path>/<func-name> 27

HTTP layer Built using Webmachine -- great library for writing well-behaving HTTP servers POST for making calls with input parameters in the body Use any of these media types in Content-Type, Accept HTTP headers: application/json, application/xml", "application/x-protobuf Successful calls return 200 OK or 204 No Content Unsuccessful call return ~10 different meaningful HTTP status codes along with error description, e.g. 400 Bad Request, 404 Not Found, 415 Unsupported media type, 411 Length required, 500 Internal Server Error, 503 Service Unavailable, etc. 28

Piqi function definition example: addressbook.piqi.include [.module person ].function [.name add-person.input person.error string ].function [.name get-person.input [.field [.name id.type int ] ].output person.error string ] 29

Generated Erlang types and signatures % from addressbook_piqi.hrl: -type(add_person_input() :: person()). -record(get_person_input, { id :: integer() }). -type(get_person_output() :: person()). -type(get_person_error() :: string() binary()). -type(get_person_input() :: #get_person_input{}). % from addressbook_piqi_impl.hrl: -spec add_person/1 :: (add_person_input()) -> ok. -spec get_person/1 :: (get_person_input()) -> {ok, get_person_output()} {error, get_person_error()}..hrl files generated by piqic-erang-rpc addressbook.piqi 30

Example: piqi call - Piqi-RPC command-line client $ piqi call -t json http://localhost:8888/addressbook/add-person -- \ --name "J. Random Hacker" \ --id 0 \ --email "j.r.hacker@example.com" \ --phone [ --number "(111) 123 45 67" ] \ --phone [ \ --number "(222) 123 45 67" \ --mobile \ ] \ $ piqi call -t json http://localhost:8888/addressbook/get-person -- 0 { "name": "J. Random Hacker", "id": 0, "email": "j.r.hacker@example.com", "phone": [ { "number": "(111) 123 45 67", "type": "home" }, { "number": "(222) 123 45 67", "type": "mobile" }, { "number": "(333) 123 45 67", "type": "work" } ] } 31

Example: piqi call (getting command-line help) $ piqi call http://localhost:8888/addressbook -h Piqi-RPC functions (use -p flag for more details): add-person -- <person>, which is a combination of: --name <string> --id <int> --email <string> (optional) --phone <phone-number> (repeated) get-person -- <input>, which is: --id <int> 32

Demo #2: simple address book Piqi-RPC service piqi-rpc/examples/addressbook/ 33

Implementation details: OCaml part ~95% OCaml, ~5% Erlang OCaml part: two standalone natively compiled executables piqi -- Piq and Piqi language implementations and tools including typebased data converter piqic -- Piqi compiler & basic Erlang compiler backend that generates type definitions and Protocol Buffers codecs (piqic erlang) OCaml type-based converter communicates with Erlang using Erlang port interface over Unix pipe (they actually use Piqi-RPC/Protobuf!) 34

Implementation details: Erlang part piqi Erlang application Runtime support library for data serialization (all formats) piqi_tool.erl -- wrapper of OCaml piqi server Erlang port piqi_rpc Erlang application Piqi compiler backend for stubs generation (piqic-erlang-rpc) Runtime support library for generated stubs Webmachine behavior implementing HTTP layer 35

Initial Erlang serialization benchmarks Input data: addressbook.piq (238 bytes in Protobuf format) Serialization / deserialization rate is in objects per second per CPU core on 2.4 GHz Core 2 Duo No performance optimizations has been done so far Read rate Write rate Protobuf JSON XML 25000 50000 4500 4500 (pretty-printed) 3000 5500 36

Project details 100% open-source licensed under Apache v2, code is on GitHub Easy to build and deploy non-erlang self-contained dependencies Erlang part can be customized without hacking into OCaml OCaml part is fairly stable Tested on Linux, Mac OS X, Solaris, 32- and 64-bit Documentation, examples, tests 37

Future plans Piqi: embedded documentation in Piqi specifications performance & multi-core scalability coordinated upgrades for generated Piqi Erlang code Piqi-RPC: tweaks, usability improvements, etc asynchronous messages 38

Demo #3: querying erlang:process_info() using Piqi-RPC piqi-rpc/examples/process_info/ 39

Why OCaml? Static type checker Mutability: mostly for record fields and global variables Raw speed, native code, self-contained executables Great libraries for language and tool development: utf8 lexing, parsing, pretty-printing, XML, JSON Sequential, single-threaded: no need for concurrency, fault-tolerance, etc. 40

Thank you! Homepage: http://piqi.org/doc/piqi-rpc Source code: http://github/alavrik/piqi @alavrik 41