Monday, March 12, 2007

Paxos

Paxos is the most efficient distributed consensus algorithm for asynchronous systems with non-Byzantine faults. There is no deterministic algorithm for always achieving consensus successfully in an asynchronous distributed system, but Paxos comes pretty close to doing so in reality for non-Byzantine faults.

The best reference on Paxos is a paper by Butler Lampson titled "How to build a highly available system using consensus".

Paxos' creator Leslie Lamport has written two papers, "The Part-time Parliament" which is the main detailed paper, and "paxos made simple" which presents the same thing simplified. However, Lampson's paper provides a very good context for Paxos: Why is consensus needed when designing an HA service ? What makes Paxos a really good consensus algorithm for asynchronous unreliable systems ? How to use Paxos in a _real_ distributed replicated (HA) service. Google's chubby lock service is a real life application of Paxos for solving a real problem and gives interesting perspectives on where consensus fits in an HA service implementation.

For instance, Lampson shows how an HA service need not use consensus for each and every consensus decision. Instead, it can use consensus once to elect a lease-based primary or grant a lock which can then be used. Chubby also uses Paxos to provide a lock service rather than let clients use Paxos for every consensus decision. This helps performance since the lock owner or primary can pretty much do what it wants without having to consult other hosts. Another real world benefit is when modifying existing non-HA code to be HA. Its easier to modify existing code to convert it to a distributed system using locks, than make it use consensus for every majority decision.

Lampson's paper and the paper on chubby lock service claim that Paxos consensus algorithm and the consensus algorithm used in Viewstamped replication which was published around the same time are equivalent. Viewstamped replication was used by Ori and Liskov to build a replication file system where the primary election happens through consensus. "Viewstamping" is used to make sure that even after a new primary is elected (and view changed), events pending with the older primary are well known to predate those seen by the new primary. The concept is similar to "round number" in Paxos where each view is like a round number and viewstamps are monotonically increasing like Paxos round numbers.

There was an MIT project by Barbara Liskov in 1990 called Argus which tried to create a programming model for writing applications which would then be automatically made highly available (HA) using viewstamped replication based system. Argus staff (Sanjay Ghemawat) joined Google, so mapreduce is probably inspired to some degree by Argus. Mapreduce does not have Argus style fault tolerance, it has basic fault tolerance implemented by the master.

Safety property of Consensus:

All non-failed processes agree on a value v that is proposed by at least one process (v must be proposed by some process, otherwise the system can e.g., trivially agree on value 0 all the time).

The basic idea behind Paxos:

Consensus is easy if there are no faults. For example, two phase commit: one process decides the outcome and asks all processes to prepare. If all processes agree, it issues a commit and job is done. If leader fails, the outcome is not clear. Another example: majority-based consensus: there is no leader. All processes choose a value and broadcast. If a majority agrees on the same value, there is consensus. But if there is no majority, or if some processes crash so that nobody knows if consensus was reached, this algorithm fails.

Paxox's basic idea comes from this non-fault-tolerant majority consensus algorithm. To make it fault-tolerant, Paxos repeats majority consensus until there is consensus. However, the challenge comes from the asynchronous nature of the problem due to which its difficult to know when the current voting round has failed to reach consensus and hence a new round needs to be started. In practice, some process will start a new round after waiting for a certain time if consensus has not reached till then (Paxos is protected from multiple processes starting a round since all round numbers are ordered, so one of them will take precedence over the other thus effectively killing that other round). But the decision about the perceived failure of the previous round can be false as its made by some process based on messages it receives from other processes (after consensus is reached, typically the result is broadcasted for everyone to know though this is not needed for correctness). So if some messages are lost, its decision can be incorrect. Thus there could be a processe A which has witnessed that there has been a consensus on some value but process B doesn't know it. So B start s another round possibly using a different value. So if B's new request to A is lost, A will wrongly stick to its older consensus value while the new consensus could be on a different value. So for Paxos to work, each new round should use a value for which there has possibly been a consensus in a previous round. The tricky part of Paxos is to ensure this property.

Paxos ensures this by having the leader process of a round ask other processes for their past values and decide for which value there could possibly have been a consensus. Two key details that make the algorithm work are:
  1. Each process can only be participating in one round at a time.
  2. Round numbers are monotonically increasing so knowing which round is "previous" is trivial.

References:
  • Butler W. Lampson, How to Build a Highly Available System Using Consensus. 10th International Workshop on Distributed Algorithms (WDAG 1996)
  • Leslie Lamport, The Part-time Parliament, ACM Transactions on Computer Systems, May 1998
  • Leslie Lamport, Paxos made simple, http://research.microsoft.com/users/lamport/pubs/paxos-simple.pdf

Future Reading:
  • Jean-Philippe Martin, Lorenzo Alvisi, Fast Byzantine Paxos, http://www.cs.utexas.edu/users/lorenzo/papers/fab.pdf

Sunday, March 11, 2007

LBFS: A low bandwidth file system

LBFS is a client server based network file system that is optimized for low bandwidth links. Main novelty is identifying duplicates in data (this technique came to be known as "content-defined chunking (CDC) after this paper was published) and not transmitting these duplicates between the client and the server.

The full LBFS uses a lot of other existing bandwidth optimization techniques and could potentially benefit from lot of techniques for bandwidth reduction especially over wide-area networks:
  • Aggressive client caching, write behind cache
  • Lease based server callbacks to tell clients to tell clients when files have been changed on the server
  • Batching file system operations as in NFSv4
  • Pipelining of RPC calls (would work even with NFSv3).
  • CODA disconnected operations
  • Operation-based updates ("Operation based update propagation in a mobile file system, Usenix 1999)
Identifying duplicates (Content-defined chunking):

LBFS identifies duplicates within files and across files. Also, it assumes that modifications to a file leave most of its contents unchanged, it optimizes that commonality as well, i.e., identifying duplicates across multiple versions of a file.

The technique of splitting a file into smaller units (chunks) for purposes of comparing for identifying duplicates is called chunking. A simple chunking algorithm is to chunk a given file into chunks of equal sizes. This technique however may do a poor job of identifying duplicates. Some advanced techniques compute "overlapping chunks", i.e., chunks that are not disjoint to not catch any possible duplicates. However, recording these chunks by their offset within the file doesn't work as it requires re-chunking when the file is modified and offsets change. In short following are the goals of a chunking algorithm:
  1. In a given static state of files, should be able to identify most number of duplicates
  2. For dynamic files, changes to a file, e.g., inserting and deleting some data in the middle should not affect existing chunks - so only minimum number of chunks should need to be recalculated.
  3. Minimum overhead in space and time.
The second goal cannot be accomplished by offset based chunking. Rather, as data is inserted say into the middle of a file, chunks that don't get changed should shift in the file and not need to be recalculated. This can be achieved by a chunking scheme based on the contents of a file. LBFS defines chunk limits, or "breakpoints", based on the actual contents of the file at the breakpoint. So as the content and hence the breakpoint shifts, so does the chunk. Breakpoints are selected using Rabin fingerprints on overlapping 48-byte regions. A Rabin fingerprint is the polynomial representation of the data module a pre-determined irreducible polynomial. When the low-order 13 bits of a region's fingerprint match a magic value, the region is identified as a breakpoint. Given the randomness of SHA-1, matching 13-bits generates a breakpoint every 8KB on an average over random data. However, pathological cases are possible (e.g., all
zeros), so LBFS restricts chunk size between the interval [2KB, 64KB].

Architecture:

The key component of LBFS on both client and server side is a chunk database which maps SHA-1 hashes of chunks to triples. This mapping must be updated whenever a file is modified.

LBFS is implemented as a modification to the NFSv3 protocol. At the client side, an LBFS driver uses SunRPC calls to communicate with the server and uses a local chunk database. At the server side, LBFS
driver is implemented as a stackable file system: it pretends to be an NFS server to the remote client, but locally acts as an NFS client to a local NFS server. By working on top of an NFS server, it doesn't
have to reinvent the wheel for a lot of basic operations. E.g., the LBFS server implements atomic commit (described below) using 4 new rpc calls added to the NFS server. It also maintains a chunk database.

File consistency:

LBFS uses a close-to-open consistency model like AFS. In addition, it doesn't clear cached contents of a file from the client when the file is closed. This is useful for the case when the file is reopened in the near future. File's contents are written atomically to the server upon close to avoid a mishmash of updates from multiple clients. For this purpose, LBFS adds a few rpcs to NFS. These RPC calls are implemented in a way such that the chances of identifying duplicates are not reduced.

Closest prior arts:

"A protocol independent technique for eliminating redundant network traffic":
This paper implements two chunk indexes on the client side and server side at the network protocol layer behaving as cooperative caches, so would work for most kind of traffic. But its too generic and doesn't solve the problems you'd need to solve for a file system, e.g., data at server could have changed, or data at client could have changed as the result of a write, etc.

Rsync
Tries basic overlapping-chunk comparison for similar looking files.

Interesting design points:
  • LBFS is built on top of NFS as an extension to NFSv3. In this way, it bets a bunch of implementation of an network file system for free, including access control. NFS also seems easily extensible (of course requiring source code changes) by adding new RPCs as needed.
  • LBFS server is essentially like a stackable file system or filter driver, it pretends to be an NFS server to the LBFS client but locally behaves like a client to the actual NFS server. In this way, it can work on top of any file system for which an NFS server has been written. However, since LBFS server runs in user space, it could have as well been implemented using system calls - that way also it would work for any file system. However, there are key reasons for using an NFS interface to the server than system calls:
    • With system calls, renaming a file changes the identity of the file (its name), whereas with NFS, the file handle doesn't change, so less changes to chunk database.
    • Grossly simplifies the implementation since the LBFS protocol is NFS-based.
    • LBFS gets access control for free by reusing NFS server's access control mechanism.
  • LBFS doesn't bother too much about keeping the chunk database consistent with file contents as its difficult to ensure this during crashes and concurrent updates. Instead, it recomputes the hash every time it gets a hit in the chunk database. This was an interesting design decision since it reduced the complexity considerably in exchange for a slight cost to performance.
Discussion:
  • Although LBFS CDC has signifcant overlap with techniques for de-duplicating data, it is applied to a very different scenario. Data de-duplication is often feasible only for static data as for non-static data, removing duplicates often costs in terms of seek time when doing sequential IO. With LBFS, data is non de-duplicated at server side, its only the client-server communication that enable de-duplication. If client's chunk database may be accommodated in its memory, there won't be such a performance hit.
  • In LBFS, modification to a file does not invalidate existing chunks which are not affected by the write. However, LBFS still incurs an extra cost with each update for updating the chunk database to update the mapping from SHA-1 hash to .

Referrences:

Athicha Muthitacharoen, Benjie Chen, and David Mazières. A Low-bandwidth Network File System. Proc. 18th Symposium on Operating Systems Principles. Banff, Canada. October, 2001.