Monday, October 22, 2007

Bigtable

Disclaimer: I'm a Google employee now but this article was written before I joined Google and only contains information available in the OSDI 2006 paper by Fay Chang et al on Bigtable intermixed with notes on my understanding of Bigtable at that time.

Bigtable is mainly a multi-dimensional sorted map. It is not a relational database, it is not a file system. Like a sorted map it supports lookup and update operations, and given its multi-dimensional nature the creators have also provided an atomic row-update operation. However, the key innovations in Bigtable are its distributed nature, efficient operation, fault recovery, ability to scale to huge datasets, and all this while maintaining a much more structured interface than conventional file systems.

Bigtable does not support a full relational data model; instead, it provides clients with a simple data model that supports dynamic control over data layout and format, and allows clients to reason about the locality properties of the data represented in the underlying storage. Clients can control the locality of their data through careful choices in their schemas. Finally, Bigtable schema parameters let clients dynamically control whether to serve data out of memory or from disk.

Every read or write of data under a single row key is atomic (regardless of the number of different columns being read or written in the row), a design decision that makes it easier for clients to reason about the system's behavior in the presence of concurrent updates to the same row.

The row range for a table is dynamically partitioned. Each row range is called a tablet, which is the unit of distribution and load balancing. As a result, reads of short row ranges are efficient and typically require communication with only a small number of machines. Clients can exploit this property by selecting their row keys so that they get good locality for their data accesses. For example, in Webtable, pages in the same domain are grouped together into contiguous rows by reversing the hostname components of the URLs.

Column families:
  • Basic unit of access control
  • All data stored in a column family is usually of the same type (we compress data in the same column family together).
  • Every column key must belong to some column family: A column family must be created before data can be stored under any column key in that family; after a family has been created, any column key within the family can be used.
  • It is our intent that the number of distinct column families in a table be small (in the hundreds at most), and that families rarely change during operation.
  • Access control and both disk and memory accounting are performed at the column-family level.
Column families are further grouped together into locality groups which support several performance optimizations.

Each cell in a Bigtable can contain multiple versions of the same data; these versions are indexed by timestamp. Bigtable timestamps are 64-bit integers.
  • Different versions of a cell are stored in decreasing timestamp order, so that the most recent versions can be read first.
  • To make the management of versioned data less onerous, we support two per-column-family settings that tell Bigtable to garbage-collect cell versions automatically. The client can specify either that only the last n versions of a cell be kept, or that only new-enough versions be kept.

API:
  • General operations like looking up values, iterating over them, etc. using regexps for identifying rows/columns/families.
  • Single row transactions
  • Sawzall based scripts to work on bigtable servers.
  • Use cells as int counters

Data layout:

Bigtable ultimately is based on GFS. But the actual data layout of bigtable data is in sstable format which is mainly a log-structured data layout formed by appending block writes and ending with a dump of the index (sstable "open" operation results in the index being loaded into memory). So it's very much like a checkpointing based CDP log format or an LFS format. All bigtable data is immutable, so incoming writes are simply appended to the sstable "file" on gfs. Periodically, the index is dumped checkpointing the current state of the cell data, so following a crash the exact state can be derived by starting from the last checkpoint dump and replaying the redo log since then. Bigtable does some optimizations though by introducing a delay in appending fresh data to the sstable which allows for write gathering resulting in compaction of data. Memtable is mainly an optimization that stores the current state of an sstable in memory (I think). Just like in CDP, memtable rows are copy-on-write. This kind of arrangement also fairs very well for concurrent reads, writes, etc.

Architecture:
  • Split across master - tablet server - client with client contacting tablet server directly using lookup mechanism described below. Master's job is to manage tablet allocation to tablet servers.
  • Each tablet server serves several tablets, possibly for different tables. Tablets are dynamic, they are split when they grow too large - their size is bet. 100 and 200MB.

Tablet lookup: Three level
FindLocation(userTable, tableRow):
  1. chubbyOpenAndRead("file_containing_location_of_rootTable") = rootTableLocation
  2. rootTable.FindLocation(metadata(userTable,tableRow)) = metadataTableLocation
  3. metadataTablet.FindLocation(userTable,tableRow) = userTableLocation
More about Chubby at Google labs and my post on Paxos.
.


Tablet assignment to tablet servers is a classic distributed system problem since the assignment must be "consistent" between the master and the tablet servers. This is accomplished using chubby lock files associated with master sessions and tablet server sessions with one key feature: whenever a lock is lost, the owner kills itself resulting in a restart.

Tablet serving:
Requires a combination of memtable, sstable, and compaction features . The data layout really improves the performance of tablet serving even in the presence of concurrent access.

Performance enhacements:
  • Locality groups: Storing several column families together in one SSTable.
  • Compression: compressing a locality group.
  • Per locality group bloom filter: allows us to check if the sstable for a given locality group contains something for a given key or not. Avoids going to disk if the sstable index is not preloaded into memory.
  • Instead of using per-tablet commit log, a single commit-log is used per tablet-server. This reduces no. of commit log files and gives good sequential write performance. However, it complicates tablet-server recovery: when a tablet-server dies, its tablets are passed on to many other tablet-servers and each of them will try to scan the log looking for the particular tablet assigned to it. So recovery is preceded by a sorting phase where the log is sorted by tabletID using map reduce.
  • Most important optimization: immutability of sstables. Since data written to sstables is immutable, there is no synchronization overhead when reading from sstables. So concurrency control becomes trivial. Row-level transactions do require concurrency control but they are aided by the mutability of memtable which is always implemented using copy-on-write. So concurrent transactions on a row + reads are very efficient.

Most interesting related work:

The Boxwood project [24] has components that overlap in some ways with Chubby, GFS, and Bigtable, since it provides for distributed agreement, locking, distributed chunk storage, and distributed B-tree storage. In each case where there is overlap, it appears that the Boxwood's component is targeted at a somewhat lower level than the corresponding Google service. The Boxwood project's goal is to provide infrastructure for building higher-level services such as le systems or databases, while the goal of Bigtable is to directly support client applications that wish to store data.

Further reading:
  1. OSDI 2006 Bigtable paper
  2. Interesting paper on log-structured merge tree which explains how Memtable + SStable might work together.

Friday, June 1, 2007

TRAP Arrays for CDP

This article is my take on ISCA 2006 paper "TRAP-Array: A Disk Array Architecture Providing Timely Recovery to Any Point-in-time", a paper by Qing Yang, Weijun Xiao, and Jin Ren of the University of Rhode Island. I implemented a CDP system some time back, and this seems like an interesting work on CDP.

The paper describes a way of doing CDP which drastically reduces the space requirements of a CDP system. CDP stands for Continuous Data Protection and can be thought of as a continuously checkpointing technology that allows data to be recovered to any point in time in the past, i.e., RPO (Recovery Point Objective) is arbitrarily small. CDP is usually implemented similar to log-structured file systems using a log of all changes which are "replayed" on a base image at the time of recovery. A basic issue with most CDP systems is space used by this log which is often prohibitive to CDP's adoption. This paper tries to address this problem.

Conventional compression algorithms don't do a good job at compressing CDP logs. This work is based on the premise that most changes to data write new data only to small portions, the rest is simply copied over. So XORing all appends to the log make them more amenable to compression as all trivial changes are converted to zeros by XOR priori to compression.

Main strengths of this work are:
  • TRAP offers significant cost savings over traditional CDP (even with compression). In fact, in some cases it even compares favorably over traditional COW snapshots in space savings and hence offers a better RPO for the same amount of space used.
  • TRAP offers choice between undo based recovery and redo based recovery. So depending on the recovery point, the faster one can be chosen.
  • The foundation of the idea is pretty solid and likely to remain valid for a long time. Specifically, this work exploits a well known fact about data that "for some workloads, a large fraction of disk sectors to be written contain identical content to previously written sectors within or across volumes [14]".
Main weaknesses of this work are:
  • RTO is a function of RPO which limits the choices available to the user.
  • A further fallout of the above problem is that when RPO is very large, RTO is very large too (hours as shown in section 5).

Discussion:

TRAP (TRAP-4 as described in this paper) requires additional storage for the base image apart from the parity log. So its space requirement would be worse than a COW based snapshotting/backup. Also, for a good RTO, the base image's storage should be at least as fast as that of the parity-log. However, TRAP does provide the "undo" option also in which case base-image is not strictly necessary which can offset this loss in space-efficiency with a few additional tricks.

One interesting observation is that as block size -> 0, COW based snapshotting/backup has the best space characteristics, and CDP,TRAP,etc. become equal in space characteristics. IOW, for large block sizes, TRAP has large sequences of zeros as not all parts of a block are written at once. So the compression algorithm should be really good in compressing in large sequences of zeros.

The fact that compressed-CDP uses more space than TRAP-compressed-CDP shows that traditional compression algorithms aren't optimal for compressing the CDP log. Typically, most compression algorithms aren't designed to compress duplication of large chunks of data. E.g., if 'X' is an already compressed piece of data, then compressing 'XX' will typically not result in any compression for most compression algorithms when |X| is large. IOW, compression algorithms cannot easily identify long patterns. XOR facilitates the optimal compression in this case.

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.