Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Thriftify your django app for

No description
by

Péter Neumark

on 10 October 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Thriftify your django app for

Thriftify your django app for
fun and proft!

Also in the package
Getting familiar
How on earth do you use it?
Basics
Is
Apache Thrift
a good RPC protocol?
Conclusion
Why use anything but REST?
Transports, Protocols and Servers and Clients
get_interface
RemoteException
What makes a good RPC protocol?
thrift
file
generated
code
thrift
compiler
REST and JSON is
self-explanatory
.
All you need is
curl
,
baby! Documentation
is for
pussies.
RESTifarianism
REST+JSON is good for document-like resources
It is not an RPC protocol
data center 1
data center 2
HTTP
requests
db
HTTP
response
No schema
JSON limitations
Handling Failure
Complex to parse
- No list of mandatory / optional fields
- No expected type or default value
for fields
HTTP is for CRUD
- No support for:
binary data, non utf-8 strings,
bigints, high-precision floats
- Inefficient wire format
- HTTP status codes not helpful
- Errors which may occur during call
unknown.
- HTTP methods only map well to CRUD
operations.

- No automatic validation of input
- parsing and converting to native
type must be done manually.
efficient wire format
safe, simple conversion to native data types
clear semantics
efficient wire format
well-defined messages
automatic serialization
application
code
struct
UserProfile {
1: i32 uid,
2: string name,
3: string blurb
}
service
UserStorage {
void store(1: UserProfile user),
UserProfile retrieve(1: i32 uid)
}

# UserProfile was a struct in the thrift file.
up = UserProfile(uid=1, name="Test User", blurb="Thrift is great")

# Talk to a server via TCP sockets, using a binary protocol
transport = TSocket.TSocket("localhost", 9090)
transport.open()
protocol = TBinaryProtocol.TBinaryProtocol(transport)

# service_client has the same methods as the thrift UserStorage service.
service_client = UserStorage.Client(protocol)
service_client.store(up)
# call into generated method stub

# Retrieve something as well
up2 = service_client.retrieve(2)
Protocol
Transport
native
object
byte-
stream
Protocol
Transport
byte-
stream
RPC server
def retrieve(self, user_id, op_data):
django_user = django.contrib.auth.models.User.objects.get(id=user_id)
return UserProfile(uid=django_user.id, name=django_user.username,
blurb = " ".join([django_user.first_name, django_user.last_name])

def store(self, up, op_data):
User.objects.create(username=up.name, ...)
client
explicitly declared in IDL
several to choose from
no manual work = less room
for security holes
method
call
HTTP
Client
Server
stub
method
( )
native
object
remote
method
( )
service method
instances of python class
generated from thrift struct
What about django models?
thrift
IDL
generated
code
thrift
compiler
application
code
CrudsHandler
method
call
HTTP
Django
model
Call semantics
- Source code does not look
like a function call.
- Low level handling of HTTP
artifacts (content type, auth,
network errors, etc).
Transports
TCP, HTTP, Memory, file
Framed, ZLib
Protocols
Binary, Compact, Dense
JSON,
Servers
can event based / multithreaded
(language and transport dependant)
Clients
synchronous (async for some languages)
(language dependant, generated)
django
thriftify
struct
UserRow {
1: optional i64 id,
2: optional string username,
3: optional string first_name,
4: optional string last_name,
5: optional string email,
6: optional string password,
7: optional bool is_staff,
8: optional bool is_active,
9: optional bool is_superuser,
10: optional string last_login,
11: optional string date_joined
}

service
UserCruds extends thriftify_cruds.BaseCruds {
UserRow create_row (1:UserRow row)
throws (
1:thriftify_shared.RemoteException ex1,
2:thriftify_cruds.UnauthorizedException ex2,
3:thriftify_cruds.DbIntegrityException ex3),
....
}
MultiUrlEndpoint
Use REST for public APIs and document storage
Use Thrift for internal RPC
op_id assigned to each request
Django-thriftify will soon be open source
making this much much easier!
Full transcript