48
Battlefield report: Bittorrent protocol implementation Analysis of using Erlang and Haskell Jesper Louis Andersen [email protected] Sep 27, 2010

Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Battlefield report: Bittorrent protocolimplementation

Analysis of using Erlang and Haskell

Jesper Louis [email protected]

Sep 27, 2010

Page 2: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Overview

Goal: Tell a story. Give insight.

Priming: What is it, really?

Actors! You have hundreds of independent processes ...

War diary: Musings over the implementations.

Page 3: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Overview

Goal: Tell a story. Give insight.

Priming: What is it, really?

Actors! You have hundreds of independent processes ...

War diary: Musings over the implementations.

Page 4: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Overview

Goal: Tell a story. Give insight.

Priming: What is it, really?

Actors! You have hundreds of independent processes ...

War diary: Musings over the implementations.

Page 5: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Overview

Goal: Tell a story. Give insight.

Priming: What is it, really?

Actors! You have hundreds of independent processes ...

War diary: Musings over the implementations.

Page 6: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

History

Etorrent - A bittorrent client implemented in Erlang

I Erlang/OTP implementation

I Initial Checkin, 27th Dec 2006

I Had first working version around early 2008

I 5 KSLOCs

Combinatorrent - A bittorrent client in Haskell

I GHC (Glasgow Haskell Compiler) implementation

I Initial checkin: 16th Nov 2009

I First working version less than 2.5 months after

I Implements an actor-like model on top of STM (SoftwareTransactional Memory)

I 4.1 KSLOCs

Page 7: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Ackowledgements

This is joint work; try to make it easy to contribute:

Etorrent: Tuncer Ayaz, Magnus Klaar

Combinatorrent: Alex Mason, Andrea Vezzozi, “Astro”, BenEdwards, John Gunderman, Roman Cheplyaka, ThomasChristensen

Page 8: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Why?

Several reasons:

I “To fully understand a programming language, you mustimplement something non-trivial with it.” – Jespers Law

I A prioriI A posteriori

I Gauge the effectiveness of modern functional programminglanguages for real-world problems.

I BitTorrent is a good “Problem Set”

Page 9: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Why?

Several reasons:

I “To fully understand a programming language, you mustimplement something non-trivial with it.” – Jespers Law

I A prioriI A posteriori

I Gauge the effectiveness of modern functional programminglanguages for real-world problems.

I BitTorrent is a good “Problem Set”

Page 10: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Why?

Several reasons:

I “To fully understand a programming language, you mustimplement something non-trivial with it.” – Jespers Law

I A prioriI A posteriori

I Gauge the effectiveness of modern functional programminglanguages for real-world problems.

I BitTorrent is a good “Problem Set”

Page 11: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Why?

Several reasons:

I “To fully understand a programming language, you mustimplement something non-trivial with it.” – Jespers Law

I A prioriI A posteriori

I Gauge the effectiveness of modern functional programminglanguages for real-world problems.

I BitTorrent is a good “Problem Set”

Page 12: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

KSLOCs

wgo combinatorrent etorrent bittornado rtorrent ktorrent transmission deluge

020

000

4000

060

000

8000

0

Page 13: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

KSLOCs

wgo combinatorrent bittornado rtorrent ktorrent transmission deluge Vuze

0e+

001e

+05

2e+

053e

+05

4e+

05

Page 14: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

HTTP vs BitTorrent

BitTorrent is about Content distribution. Some key differences:

HTTP

I Simple

I Stateless

I One-to-many

I “Serial”

I Upstream bandwidthheavy

BitTorrent

I Complex

I Stateful

I Peer-2-Peer

I “Concurrent”

I Upstream bandwidthscales proportionallywith number ofconsumers

In BitTorrent everything is sacrificed for the last point.

Page 15: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Key concepts

One: A stream of bytes is split into pieces and exchanged amongpeers with a message-passing protocol.

Page 16: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Two: Swarm intelligence

Beehives, Ant colonies, wasps.

Each client acts independently with a 10 second memory, onlyevaluates downstream bandwidth; unless it is seeding.

Mantra: Be friendly to your established friends, but be optimisticabout gaining new onesMimics human interaction.

Page 17: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Two: Swarm intelligence

Beehives, Ant colonies, wasps.

Each client acts independently with a 10 second memory, onlyevaluates downstream bandwidth; unless it is seeding.

Mantra: Be friendly to your established friends, but be optimisticabout gaining new onesMimics human interaction.

Page 18: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Actor models

“Island model”

I Cheap processes (green, userland based)

I Fast CTX switch

I Process Isolation, message pass is persistent or a copy

Page 19: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Actor models

“Island model”

I Cheap processes (green, userland based)

I Fast CTX switch

I Process Isolation, message pass is persistent or a copy

Page 20: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Communication (Link)

Peer #1 Peer #2

P1

Socket

P2 P3

Tracker

PeerMgr ChokeMgr

Status

PeerP

Peer SendQueue

Peer_Receiver

Peer Sender

PeerP

Peer SendQueue

Peer_Receiver

Peer Sender

Main

FSConsole

Timer

PieceMgr

Listener

HTTP

Page 21: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Process Hierarchy (Location)

S0

S1 Main Timer Console PeerMgr ChokeMgr

S2 FS Tracker Status PieceMgr

SPeer1 SPeer2

P1Receiver P1SendQ P1PeerP P1Sender P2Receiver P2SendQ P2PeerP P2Sender

Page 22: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Bigraphs

Bigraph = Hypergraph + Tree

Do not confuse with bipartite graphs.

Hypergraph is the link-graphTree is the location-graph

Page 23: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Robustness

Robustness is key to good programming:

I Semantics (segfault, Null, of-by-one, ...)

I Proactive: HaskellI Type system

I Reactive: ErlangI Crashes, restartsI SupervisorsI Redundancy

Ideas from both areas are needed in robust software!

Page 24: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Process Hierarchy (Location)

S0

S1 Main Timer Console PeerMgr ChokeMgr

S2 FS Tracker Status PieceMgr

SPeer1 SPeer2

P1Receiver P1SendQ P1PeerP P1Sender P2Receiver P2SendQ P2PeerP P2Sender

Page 25: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Strings in Haskell and Erlang

I Single linked lists of runes

I Simple

I Unicode is trivial

I List operations are string operations

I It is fairly fast

I Extremely memory heavy (16+ bytes per char in Erlang!)

Solution: Use ByteString for binary data in Haskell, binaries/iolistsin Erlang.

Page 26: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Strings in Haskell and Erlang

I Single linked lists of runes

I Simple

I Unicode is trivial

I List operations are string operations

I It is fairly fast

I Extremely memory heavy (16+ bytes per char in Erlang!)

Solution: Use ByteString for binary data in Haskell, binaries/iolistsin Erlang.

Page 27: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Strings in Haskell and Erlang

I Single linked lists of runes

I Simple

I Unicode is trivial

I List operations are string operations

I It is fairly fast

I Extremely memory heavy (16+ bytes per char in Erlang!)

Solution: Use ByteString for binary data in Haskell, binaries/iolistsin Erlang.

Page 28: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Strings in Haskell and Erlang

I Single linked lists of runes

I Simple

I Unicode is trivial

I List operations are string operations

I It is fairly fast

I Extremely memory heavy (16+ bytes per char in Erlang!)

Solution: Use ByteString for binary data in Haskell, binaries/iolistsin Erlang.

Page 29: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Some cool things in Haskell

I Haskell is king of abstraction (sans Proof assistants)

I Type system is expressive almost to the point of program proof

I Strong Type Zoo

I Combinators run at full speed in Haskell

I Close to being clay: you can model actors easily

I Excellent community - vibrant; practitioners and academics.

I QuickCheck - (John Hughes, Wednesday)

Page 30: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Some cool things in Haskell

I Haskell is king of abstraction (sans Proof assistants)

I Type system is expressive almost to the point of program proof

I Strong Type Zoo

I Combinators run at full speed in Haskell

I Close to being clay: you can model actors easily

I Excellent community - vibrant; practitioners and academics.

I QuickCheck - (John Hughes, Wednesday)

Page 31: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Some cool things in Haskell

I Haskell is king of abstraction (sans Proof assistants)

I Type system is expressive almost to the point of program proof

I Strong Type Zoo

I Combinators run at full speed in Haskell

I Close to being clay: you can model actors easily

I Excellent community - vibrant; practitioners and academics.

I QuickCheck - (John Hughes, Wednesday)

Page 32: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Some cool things in Haskell

I Haskell is king of abstraction (sans Proof assistants)

I Type system is expressive almost to the point of program proof

I Strong Type Zoo

I Combinators run at full speed in Haskell

I Close to being clay: you can model actors easily

I Excellent community - vibrant; practitioners and academics.

I QuickCheck - (John Hughes, Wednesday)

Page 33: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

The bad in Haskell

I Lazy evaluation - space leaks

I Heap Profile – Use strictness annotations,

I Peak Mem:

I Productivity:

I CPU/Mb:

I Academic compilers, stability suffer

I Some libraries are extremely complex type-wise

Page 34: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

The bad in Haskell

I Lazy evaluation - space leaksI Heap Profile – Use strictness annotations,

I Peak Mem:

I Productivity:

I CPU/Mb:

I Academic compilers, stability suffer

I Some libraries are extremely complex type-wise

Page 35: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

The bad in Haskell

I Lazy evaluation - space leaksI Heap Profile – Use strictness annotations,

I Peak Mem:

I Productivity:

I CPU/Mb:

I Academic compilers, stability suffer

I Some libraries are extremely complex type-wise

Page 36: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

The bad in Haskell

I Lazy evaluation - space leaksI Heap Profile – Use strictness annotations,

I Peak Mem:

I Productivity:

I CPU/Mb:

I Academic compilers, stability suffer

I Some libraries are extremely complex type-wise

Page 37: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Some cool things in Erlang

I Crash-oriented programming is bliss, an error might not befatal

I OTP - Actor abstraction: Servers, event drivers, finite statemachine, supervision, logging, ...

I Processes are individually garbage collected (isolation)

I Interpreted language, but implementation is heavily optimized

I Again, excellent community!

Page 38: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Some cool things in Erlang

I Crash-oriented programming is bliss, an error might not befatal

I OTP - Actor abstraction: Servers, event drivers, finite statemachine, supervision, logging, ...

I Processes are individually garbage collected (isolation)

I Interpreted language, but implementation is heavily optimized

I Again, excellent community!

Page 39: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Some cool things in Erlang

I Crash-oriented programming is bliss, an error might not befatal

I OTP - Actor abstraction: Servers, event drivers, finite statemachine, supervision, logging, ...

I Processes are individually garbage collected (isolation)

I Interpreted language, but implementation is heavily optimized

I Again, excellent community!

Page 40: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Some cool things in Erlang

I Crash-oriented programming is bliss, an error might not befatal

I OTP - Actor abstraction: Servers, event drivers, finite statemachine, supervision, logging, ...

I Processes are individually garbage collected (isolation)

I Interpreted language, but implementation is heavily optimized

I Again, excellent community!

Page 41: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Some cool things in Erlang

I Crash-oriented programming is bliss, an error might not befatal

I OTP - Actor abstraction: Servers, event drivers, finite statemachine, supervision, logging, ...

I Processes are individually garbage collected (isolation)

I Interpreted language, but implementation is heavily optimized

I Again, excellent community!

Page 42: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

The bad in Erlang

I Not suited for number crunching (have to choose rightalgorithm, data structure)

I No way to do imperative code (Deliberate choice by theErlang developers, have to fake it)

I Dynamic typing (Dialyzer project helps, processes are small(< 500 lines)

Page 43: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

The bad in Erlang

I Not suited for number crunching (have to choose rightalgorithm, data structure)

I No way to do imperative code (Deliberate choice by theErlang developers, have to fake it)

I Dynamic typing (Dialyzer project helps, processes are small(< 500 lines)

Page 44: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

The bad in Erlang

I Not suited for number crunching (have to choose rightalgorithm, data structure)

I No way to do imperative code (Deliberate choice by theErlang developers, have to fake it)

I Dynamic typing (Dialyzer project helps, processes are small(< 500 lines)

Page 45: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

The Ugly

Haskell:

I Take laziness seriously from the start

I Be careful when choosing libraries

Erlang:

I Be careful about messaging large data between processes

I Mnesia has optimistic conflict resolution

Both: Expect to manipulate your process model quite a bit.

Page 46: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

The Ugly

Haskell:

I Take laziness seriously from the start

I Be careful when choosing libraries

Erlang:

I Be careful about messaging large data between processes

I Mnesia has optimistic conflict resolution

Both: Expect to manipulate your process model quite a bit.

Page 47: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

The Ugly

Haskell:

I Take laziness seriously from the start

I Be careful when choosing libraries

Erlang:

I Be careful about messaging large data between processes

I Mnesia has optimistic conflict resolution

Both: Expect to manipulate your process model quite a bit.

Page 48: Battle eld report: Bittorrent protocol implementationgotocon.com/dl/jaoo-aarhus-2010/slides/JesperLouis... · History Etorrent - A bittorrent client implemented in Erlang I Erlang/OTP

Repositories

We use github for all code:

http://www.github.com/jlouis

Look for etorrent and combinatorrent