Project: Peers

Peers is a distributed programming toolkit designed for building Peer-to-Peer systems. The toolkit is a hybrid language project which offers low level primitives for managing concurrency and communication. Using these primitives, complex P2P protocols and complete systems can be designed and implemented in Python, while maintaining high performance for critical operations.

Introduction

The global scale of P2P systems makes protocol design a hard problem. At a basic level, P2P protocols have to intrinsically manage a high level of concurrency, loose timing and unpredictability of network conditions or peer behavior. Failures and node turnover are not rare occurrences, they are the norm. Cooperation cannot be assumed, protocols must be designed to tolerate anti-social or malicious behavior and adapt to observed behavior.

The situation is even more complicated when considering the huge gap from design to implementation of P2P protocols. The vast majority of protocol proposals are designed around an abstract model which bears little resemblance to implementation reality. The main problem here is the lack of a suitable programming environment, where ideas and protocols can be expressed and implemented in a simple high-level language.

These are not just theoretical observations. Very few academic protocol proposalss are seen or heard of in the wild. Contrast this situation with the ubiquitousness, robustness and unprecedented scalability of BitTorrent. BitTorrent is not an academic protocol, it is an organically evolved hack. The protocol evolved bottom-up as a program written in Python.

The choice of language and approach to protocol design ensured that the code is the design, easy to understand and reason about, yet always runnable. The protocol deals intrinsically with a high-level of concurrency and makes no assumptions about peer behavior other than what can be observed. In a sense, BitTorrent shows the benefit of using dynamic languages in P2P system development, as they inherently support bottom-up development.

The Peers Programming Environment

Peers is designed to support bottom-up P2P system development. The approach is Code as Design. We are not interested in theoretical protocol and system design, geared towards simulation with implementation as an afterthought. Instead, peers provides a distributed programming environment for designing protocols and systems that can be used in the real-world, while maintaining ease of understanding and the ability to simulate.

As such, the primary programming language for developing protocols and systems with Peers is Python. All the concurrency and communication primitives are provided by a small and fast user-space kernel written in C++.

On top of the kernel, Peers provide a library for programming P2P protocols and applications. The main idea is that messages are treated as calls. Peers provides the suitable abstractions for implementing P2P protocols in this way. The concurrency inherent in P2P protocols can be explicitly managed with higher order functions, while maintaining low-level network efficiency.

The Peers Kernel

The Peers kernel provides the primitives for communication and concurrency management. The kernel is event-driven, with the low level events being file-descriptor conditions and delayed, context-switched execution of code. Delayed execution provides the raw primitive for managing user-space concurrency with continuations.

At the same time, we recognize the necessity of mixing user-space concurrency with low level concurrency using native threads. Thus, Peers is designed to be thread-safe from the ground up, allowing the application programmer to freely mix native threads with events. Nonetheless, low level events are not expressive enough for easily programming P2P systems.

At the fundamental level, a P2P system is a message-passing distributed system, and hence the environment should offer suitable communication abstractions. Peers provides them as a lightweight distributed object system. The system is designed to specifically address the needs of P2P programming. It provides both synchronous and asynchronous call semantics, asynchronous events, reverse channels for performing back-calls to the peer, and the ability to interleave raw data.

Synchronous calls denote messages that context switch to a remote process while blocking the current thread in the local process waiting for a response. Asynchronous calls are calls with continuation: the caller supplies a continuation function to receive the result (or synchronization signal in the case of a void call) and an exception handler function. Asynchronous calls can be naturally nested and connected in pipes, in effect performing remote calls with continuation and distributed recursion. Events are equivalent to asynchronous calls that do not return a value. Raw data messages provide a mechanism for minimum overhead asynchronous data transport. Data can be freely interleaved with calls and events, while maintaining a well-defined order of delivery.

The kernel API is exported to Python with Boost.Python bindings, as a python-land module. Distributed object interfaces are specified in a domain specific language we call XX. The specification is compiled to C++ code and Python bindings by the Peers rpc compiler. The generated code provides implementation stubs and proxies, and handles the message marshaling and dispatch. As a result, a protocol can be written entirely in python without having to worry about bit-banging performance; all the network I/O and dispatch is handled by native code.

Browsing the Wiki

This source wiki provides documentation and a guide to the Peers code base. We recommend that you start with the programmer guide, which describes the high level design of Peers, and the tutorial, which provides a hands-on introduction to building peers applications.

Meta-Info

Peers is developed in the Viral Communications research group at the MIT Media Laboratory. The primary Peers hackers are vyzo (aka Dimitris Vyzovitis, direct flames to vyzo-at-media.mit.edu) and imirkin (aka Ilia Mirkin, flames to imirkin-at-mit.edu).

If you would like to be involved, we take patches and we give credit. The project mailing list is peers-at-media.mit.edu. You can subscribe at the Media Lab list server.