60
CLOJURE AT DATASTAX: THE LONG ROAD FROM PYTHON TO CLOJURE Nick Bailey @nickmbailey

Clojure at DataStax: The Long Road From Python to Clojure

Embed Size (px)

Citation preview

Page 1: Clojure at DataStax: The Long Road From Python to Clojure

CLOJURE AT DATASTAX: THE LONG ROAD FROM PYTHON TO CLOJURENick Bailey @nickmbailey

Page 2: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

1 Introduction

2 Starting with Python

3 Clojure Enters the Ring

4 Clojure Everywhere!

5 Takeaways

2

Page 3: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 3

1. Introduction

Page 4: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Me• DataStax • Clojure • Apache Cassandra • Austin

4

Page 5: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

DataStax• DSE (distributed database) • Java

• OpsCenter • Python • Clojure

5

Page 6: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

This talk• DataStax Clojure experience report • Python shop interested in Clojure? • Have a large python application you wish was written in Clojure?

6

Page 7: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 7

2. Starting With Python

Page 8: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 8

A long time ago in an galaxy far far away…

Page 9: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 9

A long time ago in an office not far away…

(5 years ago)

Page 10: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Building a Database Monitoring Tool

10

Page 11: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Building a Database Monitoring Tool

11

Page 12: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Building a Database Monitoring Tool

12

Page 13: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 13

Ok… but why?

Page 14: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Why Twisted?

14

• 2010 • 10ish person startup • 1 front-end dev • 1 back-end dev

Page 15: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Product Complexity Grows

15

• Single process isn’t enough • Need an agent to run on the cluster

Page 16: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Product Growth

16

Page 17: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Things go wrong

17

• The JVM doesn’t play nice • Python may not be the best choice for monitoring a JVM… • … but there is definitely a JVM available…

Page 18: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 18

3. Clojure Enters the Ring

Page 19: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 19

3. Clojure Enters the Ring (pun intended)

Page 20: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 20

(still 5 years ago)

Page 21: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Replacement Needed

21

?

?

?

?

Page 22: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Responsibilities

22

• Gather and report metric data

Page 23: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Requirements

23

• Need to interact heavily with a JVM based database • Maintainability • Developer happiness

Page 24: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Contenders

24

• Java • Clojure • Scala • Jython

Page 25: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Why Clojure

25

• Isolated piece of the architecture • Immutability was a good fit • Great interoperability • An experiment worth taking a risk on

Page 26: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

New Architecture

26

Page 27: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 27

Experiment Results…

Page 28: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Smashing Success!

28

• Clojure fit all of our needs • Great recruitment tool • Clojure inspires passion • Picked up by new developers

Page 29: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

On Learning Clojure

29

“…the most important transition will be opening your soul to Truth and Beauty, if you do so, Clojure will come naturally…”

—Philip Doctor

Page 30: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

A Note on Hiring

30

• Datastax has a distributed engineering organization • Our clojure engineers span the globe

Page 31: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

The rough edges

31

• Deployed on 1.2, immediate painful migration • JMX library unmaintained • Less mature libraries

Page 32: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 32

Ok… why didn’t you just rewrite it all?

Page 33: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Hindsight is 20/20…

33

• Clojure was definitely considered an experiment/risk • Still a tiny startup • Bulk of code and functionality in the Twisted code

Page 34: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 34

4. Clojure Everywhere!

Page 35: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 35

Back to the future!

(ish)

Page 36: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

What’s changed?

36

• 2 person team to 20 person team • Clojure codebase has grown significantly • JVM usage has increased across the company • Sorry Python, Clojure is the favorite child

Page 37: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Time to Reevaluate

37

?

Page 38: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Reevaluating Twisted

38

• Scalability requirements have grown • GIL rears it’s ugly head • Overlapping responsibilities

Page 39: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Reevaluating Twisted

39

• Need a path the JVM • Need to address scalability concerns

Page 40: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Possible Solutions

40

• Rewrite completely • Multiple processes • Jython?

Page 41: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Rewrite completely

41

• Benefit of hindsight when starting from the ground up • 4+ years invested • Pause feature development • Inevitable rewrite bugs introduced

Page 42: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Multiple Processes

42

• Less time intensive than a re-write • Can gradually move things out of python • Multi-processing is difficult • Untangling Twisted is extremely difficult • Adding IPC • Additional customer burden

Page 43: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Port to Jython

43

• Theoretically completely behind the scenes • Shortest path to being completely on the JVM • No out of the box support for Twisted • Introduction of a “third language” • The road less traveled

Page 44: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 44

Jython Wins!

Page 45: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

The Present

45

Page 46: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Migrating to Jython1. Make Twisted run on Jython 2. Replace all broken python libraries 3. Fix all the Jython* bugs you found 4. Write Jython <-> Clojure translation layer

46

Page 47: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

1. Make Twisted run on Jython

47

• Upgrade Twisted • Start application on twisted • Get a stack trace and fix the issue • Rinse • Repeat

Page 48: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

2. Replace all broken python libraries

48

• C extensions won’t run on Jython • SSL, LDAP, sqlite, crypto • Jython doesn’t support python sub processing

Page 49: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

3. Fix all the Jython* bugs you found

49

• Brace yourselves…

Page 50: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

3. Fix all the Jython* bugs you found

50

• Socket leaks • Deadlocks (including during garbage collection) • Incorrect socket implementations • Lots of SSL bugs • *And also 1 bug in Netty

Page 51: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 51

If you thought stack traces and heap dumps were bad in Clojure…

Page 52: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Write Jython <-> Clojure translation layer

52

• The fun part • Clojure: “What’s a PyFunction object!?” • Jython: “Shut up, what’s an IPersistentVector!?” • Luckily, everyone speaks Java • https://gist.github.com/nickmbailey/3f11300e944e17d92edd6d42da785ab3

Page 53: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

The Present

53

Page 54: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Success?

54

• There were some bumps a long the road • Took a lot longer than hoped • It was definitely the right choice

Page 55: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Over the Rainbow

55

Page 56: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 56

Takeaways

Page 57: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Should I use Clojure?

57

• Yes, open yourself to Truth and Beauty

Page 58: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Should I not use Python?

58

• No one said that! • But if you are mainly interacting with the JVM… probably not.

Page 59: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved.

Should I migrate my Python app to Clojure via Jython?

59

• Maybe? • Do you need to be on the JVM? • How much C code will you need to replace? • Are you brave enough to venture into the Jython codebase itself?

Page 60: Clojure at DataStax: The Long Road From Python to Clojure

© 2016 DataStax, All Rights Reserved. 60

Questions? [email protected]