taming pythons with zookeeper

40
June 29, 2013 Taming Pythons with ZooKeeper Wednesday, 3 July 13

Upload: jyrki-pulliainen

Post on 10-May-2015

874 views

Category:

Technology


2 download

DESCRIPTION

Concurrency is hard. Consistency in distributed systems is hard. And then the whole thing should be highly-available and error resilient. Fear not, there are good news: There exists an awesome tool called ZooKeeper to help you with this. There even exists a plethora of Python libraries for it, but how to know what to use and how? This talk will walk you through ZooKeeper and how to use it with Python. We’ll be focusing on what I think is the most prominient ZooKeeper library out there for Python: Kazoo. You’ll see how to do things in ZooKeeper and how to implement them using Kazoo. We’ll also peek in to the recipes Kazoo offers, and if we have enough time, touch a real life application we’ve used Kazoo and ZooKeeper to build at Spotify.

TRANSCRIPT

Page 1: Taming Pythons with ZooKeeper

June 29, 2013

Taming Pythons with ZooKeeper

Wednesday, 3 July 13

Page 2: Taming Pythons with ZooKeeper

$ whoami

Wednesday, 3 July 13

Page 3: Taming Pythons with ZooKeeper

Jyrki Pulliainen

@[email protected]

Wednesday, 3 July 13

Page 4: Taming Pythons with ZooKeeper

Wednesday, 3 July 13

Page 5: Taming Pythons with ZooKeeper

ZooKeeper?

Wednesday, 3 July 13

- This talk is about ZooKeeper.- Preaching Java software in Python conference. I can think of more healthier things to do too.

- On side note, our Product Owner is really damn good at this game

Page 6: Taming Pythons with ZooKeeper

ZooKeeper!

Wednesday, 3 July 13

- Apache project, Yahoo 2007- Consistency & Partition tolerance- Filesystem like, actually can be viewed as a trie, store data in directories too- In memory, limits the dataset you can store. Maximum zookeeper node, znode, data size 1M

Page 7: Taming Pythons with ZooKeeper

The Tao of ZooKeeper

7

Wednesday, 3 July 13

Orderly, Reliable, Efficient, Timely, Contention free, ambition free

Page 8: Taming Pythons with ZooKeeper

Wednesday, 3 July 13

Page 9: Taming Pythons with ZooKeeper

June 29, 2013

One library for the enterpriseCurator, from Netflix

Wednesday, 3 July 13

Page 10: Taming Pythons with ZooKeeper

June 29, 2013

Seven for the pythonistasgevent-zookeeperzkpythonzc.zkpykeeper

twitter’s zookeeper libraryzooptxzookeeper

Wednesday, 3 July 13

- gevent-zookeeper -> Spotify- zkpython segfaults- Others have somewhat OK implementations, but lack core features

Page 11: Taming Pythons with ZooKeeper

One Library to rule them all*

* unless you are running twisted

Wednesday, 3 July 13

- txzookeeper still valid for twisted

Page 12: Taming Pythons with ZooKeeper

Kazoo

Wednesday, 3 July 13

- Origins from the Nimbus project- Ben Bangert as the Sauron of ZooKeeper- Not Frodo, that bastard wanted to destroy the perfectly good ring.- All Python, including the protocol. No more segfaults!

Page 13: Taming Pythons with ZooKeeper

CRUD

ZooKeeper CRUD

Let’s talk about KaZoo

13

Wednesday, 3 July 13

Create, Read, Update, DeleteAll basic operations available as async too, but we’ll focus on the synchronous use

Page 14: Taming Pythons with ZooKeeper

First we need to connect!

Text

from kazoo.client import KazooClient

zk = KazooClient(hosts='127.0.0.1:2181')zk.start()# ...zk.stop()

TextText

Wednesday, 3 July 13

Easy to connect to one host, multiple host, with namespace....ZooKeeper supports connection namespacing!Bonus: get notified when the connection state changes

Page 15: Taming Pythons with ZooKeeper

First we need to connect!

Text

from kazoo.client import KazooClient

zk = KazooClient(hosts='127.0.0.1:2181')zk.start()# ...zk.stop()

TextTextzk = KazooClient(hosts='127.0.0.1:2181,127.0.0.2:2181')

Wednesday, 3 July 13

Easy to connect to one host, multiple host, with namespace....ZooKeeper supports connection namespacing!Bonus: get notified when the connection state changes

Page 16: Taming Pythons with ZooKeeper

First we need to connect!

Text

from kazoo.client import KazooClient

zk = KazooClient(hosts='127.0.0.1:2181')zk.start()# ...zk.stop()

Text

zk = KazooClient(hosts='127.0.0.1:2181/namespace,127.0.0.2:2181')

Textzk = KazooClient(hosts='127.0.0.1:2181,127.0.0.2:2181')

Wednesday, 3 July 13

Easy to connect to one host, multiple host, with namespace....ZooKeeper supports connection namespacing!Bonus: get notified when the connection state changes

Page 17: Taming Pythons with ZooKeeper

Create

zk.create('/europython', b'2013')

Wednesday, 3 July 13

Adding nodes easy, helpers exist for recursive creationEphemeral ZK feature, session + heartbeats, can’t have children!Incremental: guaranteed ever increasing 10 digit number in node name

Page 18: Taming Pythons with ZooKeeper

Create

zk.create('/europython', b'2013')

zk.create('/europython/jyrki', ephemeral=True)

Wednesday, 3 July 13

Adding nodes easy, helpers exist for recursive creationEphemeral ZK feature, session + heartbeats, can’t have children!Incremental: guaranteed ever increasing 10 digit number in node name

Page 19: Taming Pythons with ZooKeeper

Create

zk.create('/europython', b'2013')

zk.create('/europython/jyrki', ephemeral=True)

zk.create('/europython/sequential', sequence=True)

Wednesday, 3 July 13

Adding nodes easy, helpers exist for recursive creationEphemeral ZK feature, session + heartbeats, can’t have children!Incremental: guaranteed ever increasing 10 digit number in node name

Page 20: Taming Pythons with ZooKeeper

Read

Text

zk.get('/europython')

Wednesday, 3 July 13

Page 21: Taming Pythons with ZooKeeper

Read

Text

zk.get('/europython')

zk.exists('/europython/jyrki')

Wednesday, 3 July 13

Page 22: Taming Pythons with ZooKeeper

Read

Text

zk.get('/europython')

zk.exists('/europython/jyrki')

zk.get_children('/europython')

Wednesday, 3 July 13

Page 23: Taming Pythons with ZooKeeper

Update & Delete

zk.set_data('/europython/jyrki', b'nervous')

Wednesday, 3 July 13

Page 24: Taming Pythons with ZooKeeper

Update & Delete

zk.set_data('/europython/jyrki', b'nervous')

zk.delete('/europython/jyrki')

Wednesday, 3 July 13

Page 25: Taming Pythons with ZooKeeper

Wednesday, 3 July 13

Page 26: Taming Pythons with ZooKeeper

Distributed locks?

Barrier?

Semaphores?

Counters?

Elections?

Wednesday, 3 July 13

Page 27: Taming Pythons with ZooKeeper

Textfrom kazoo.recipe import <your-favourite-thing>

Wednesday, 3 July 13

Of course it does not have everything from curator

Page 28: Taming Pythons with ZooKeeper

Want to know when things change?

Wednesday, 3 July 13

Page 29: Taming Pythons with ZooKeeper

watchers

Wednesday, 3 July 13

Page 30: Taming Pythons with ZooKeeper

Text

zk.exists('/europython/wine', watch=callback)

zk.get('/europython/wine', watch=callback)

zk.get_children('/europython/dinners', watch=callback)

Wednesday, 3 July 13

Page 31: Taming Pythons with ZooKeeper

from kazoo.recipe.watchers import DataWatch, ChildWatch

@DataWatch('/path/to/node')def data_callback(data, stat): # ... do_something

@ChildWatch('/path/to/node')def child_callback(children): # ... do_something

Wednesday, 3 July 13

Page 32: Taming Pythons with ZooKeeper

TESTS

Wednesday, 3 July 13

Page 33: Taming Pythons with ZooKeeper

What if something goes

WRONG?

Wednesday, 3 July 13

Page 34: Taming Pythons with ZooKeeper

sys.exit()Wednesday, 3 July 13

Page 35: Taming Pythons with ZooKeeper

Stand back!

It’s time for real life example

Wednesday, 3 July 13

Page 36: Taming Pythons with ZooKeeper

Wednesday, 3 July 13

Page 37: Taming Pythons with ZooKeeper

SEARCHED FOR THE NEWEST JUSTIN BIEBER

Encryption keys were not available to play it.

Wednesday, 3 July 13

Page 38: Taming Pythons with ZooKeeper

SUMMARY

Wednesday, 3 July 13

Page 39: Taming Pythons with ZooKeeper

Guess what, we’re hiring

Wednesday, 3 July 13

Page 40: Taming Pythons with ZooKeeper

Thank You!

[email protected]

@nailor

Wednesday, 3 July 13