Hadoop Distributed File System: Design Essentials and Architecture

2012-08-12 00:00:00

 Introduction to Hadoop: a distributed system infrastructure developed by the Apache Foundation. Users can develop distributed programs without knowing the low-level details of distribution, making full use of the power of a cluster for high-speed computing and storage. Hadoop implements a distributed file system (Hadoop Distributed File System), or HDFS for short. HDFS is highly fault-tolerant and designed to be deployed on low-cost hardware. It also provides high-throughput access to application data, making it suitable for applications with very large data sets. HDFS relaxes POSIX requirements so that data in the file system can be accessed in a streaming manner.

Hadoop Distributed File System: Design Essentials and Architecture

 

Hadoop official website: http://hadoop.apache.org/

Hadoop Chinese documentation: http://hadoop.apache.org/common/docs/r0.21.0/cn/

 

1. Assumptions and Design Goals

 

1. Hardware failure is the norm rather than the exception. An HDFS instance may consist of hundreds or thousands of servers, and any component may fail at any time, so error detection and fast, automatic recovery are core architectural goals of HDFS.

2. Applications running on HDFS differ from general applications: they mainly read data in a streaming fashion for batch processing. Rather than focusing on low-latency data access, what matters more is high throughput of data access.

3. HDFS aims to support large data sets: a typical file stored on it ranges in size from gigabytes to terabytes, and a single HDFS instance should support tens of millions of files.

4. HDFS applications require a write-once-read-many access model for files. Once a file has been created, written, and closed, it does not need to be changed. This assumption simplifies data consistency issues and makes high-throughput data access possible. Typical examples such as the MapReduce framework, or a web crawler application, fit this model well.

5. Moving computation is cheaper than moving data. The closer a requested computation is to the data it operates on, the more efficient it is, and this is even more true when the data reaches massive scale. Moving the computation closer to the data is clearly better than moving the data to where the application runs, and HDFS provides applications with interfaces for exactly that.

6. Portability across heterogeneous hardware and software platforms.

 

2. NameNode and DataNode

 

HDFS adopts a master/slave architecture. An HDFS cluster consists of a single NameNode and a number of DataNodes. The NameNode is a central server that manages the file system namespace and client access to files. DataNodes, generally one per node in the cluster, manage the storage attached to the nodes they run on. Internally, a file is split into one or more blocks, and these blocks are stored in a set of DataNodes. The NameNode executes file system namespace operations such as opening, closing, and renaming files and directories, and also determines the mapping of blocks to specific DataNodes. DataNodes create, delete, and replicate blocks under the direction of the NameNode. Both NameNode and DataNode are designed to run on commodity, inexpensive machines running Linux. HDFS is developed in Java, so it can be deployed on a wide range of machines. A typical deployment runs a dedicated NameNode on one machine, with each of the other machines in the cluster running one DataNode instance. The architecture does not exclude running multiple DataNodes on the same machine, but that is less common.

 

A single NameNode greatly simplifies the system architecture. The NameNode holds and manages all HDFS metadata, so user data never needs to flow through the NameNode (that is, file data reads and writes happen directly on the DataNodes).

 

 

3. The File System Namespace

 

HDFS supports a traditional hierarchical file organization: like most other file systems, users can create directories and create, delete, move, and rename files within them. HDFS does not support user quotas or access permissions, nor does it support links, but the current architecture does not exclude implementing these features. The NameNode maintains the file system namespace, and any modification to the file system namespace or file attributes is recorded by the NameNode. An application can set the number of replicas HDFS keeps for a file; this number is called the replication factor of the file, and this information is also kept by the NameNode.

 

4. Data Replication

 

HDFS is designed to reliably store very large files across machines in a large cluster. It stores each file as a sequence of blocks; all blocks in a file except the last one are the same size. The blocks of a file are replicated for fault tolerance. The block size and replication factor of a file are configurable. The replication factor can be specified at file creation time and changed later. Files in HDFS are write-once and strictly have a single writer at any time. The NameNode makes all decisions about block replication. It periodically receives a heartbeat and a block report from each DataNode in the cluster. Receipt of a heartbeat indicates that the DataNode is working properly, and the block report contains a list of all the blocks on that DataNode.

 

 

1. Replica placement. Replica placement is the key to HDFS reliability and performance. HDFS uses a policy called rack awareness to improve data reliability, availability, and network bandwidth utilization. The short-term goal of implementing this policy is to validate its behavior in production environments, observe how it behaves, and build a foundation for testing and research so that more advanced policies can be implemented. A large HDFS instance generally runs on a cluster spanning multiple racks; communication between two machines on different racks has to go through a switch, and under normal circumstances the bandwidth between two nodes in the same rack is greater than the bandwidth between two machines on different racks.

Through a process called rack awareness, the NameNode determines the rack id each DataNode belongs to. A simple but unoptimized policy is to place replicas on a single rack. This prevents data loss when an entire rack (other than the one holding the replicas) fails, and allows reads to be served from multiple racks. This simple policy spreads replicas across the cluster, which helps load balancing in the event of component failure, but it increases the cost of writes, because a write operation has to transfer the block to multiple racks.

In most cases the replication factor is 3. The HDFS placement policy is to put one replica on a node in the local rack, one replica on a different node in the same local rack, and the last replica on a node in a different rack. Rack failures are far less common than node failures, so this policy does not compromise data reliability or availability. One third of the replicas are on one node, two thirds are on one rack, and the rest are spread across the remaining racks. This policy improves write performance.

 

2. Replica selection. To reduce overall bandwidth consumption and read latency, HDFS tries to let a reader read the closest replica. If there is a replica on the same rack as the reader, that replica is read. If an HDFS cluster spans multiple data centers, the reader first tries a replica in its local data center.

 

3. SafeMode

After startup the NameNode enters a special state called SafeMode, in which the NameNode does not replicate data blocks. The NameNode receives heartbeats and block reports from all DataNodes. A block report contains the list of all data blocks held by a given DataNode. Each block has a specified minimum number of replicas. When the NameNode detects that the minimum number of replicas of a data block on a DataNode has been confirmed, that block is considered safe; if a certain percentage of data blocks (this parameter is configurable) is confirmed safe, the NameNode exits SafeMode. It then determines which data blocks still have fewer replicas than the specified number and replicates those blocks to other DataNodes.

 

5. Persistence of File System Metadata

 

The NameNode stores HDFS metadata. For any operation that modifies file system metadata, the NameNode records it using a transaction log called the EditLog. For example, creating a file in HDFS causes the NameNode to insert a record into the EditLog to denote the operation; likewise, changing the replication factor of a file also inserts a new record into the EditLog. The NameNode stores this EditLog in the file system of the local OS. The entire file system namespace, including the mapping of blocks to files and the file attributes, is stored in a file called the FsImage, which is also placed on the file system of the machine where the NameNode runs.

The NameNode keeps an image of the entire file system namespace and the file block map in memory. This key metadata is designed to be compact, so a NameNode with 4G of memory is enough to support a huge number of files and directories. When the NameNode starts up, it reads the EditLog and FsImage from disk, applies all transactions in the EditLog to the in-memory FsImage, flushes this new version of the FsImage from memory to disk, and then truncates the old EditLog, because all the transactions of that old EditLog have already been applied to the FsImage. This process is called a checkpoint. In the current implementation the checkpoint happens only at NameNode startup; periodic checkpointing will be supported in the near future.

A DataNode knows nothing about files except that it stores the data of blocks in its local file system. It stores each HDFS data block as a separate file in the local file system. A DataNode does not create all files in the same directory; instead, it uses heuristics to determine the optimal number of files per directory and creates subdirectories when appropriate. Creating all files in one directory is not the optimal choice, because the local file system may not efficiently support a large number of files in a single directory. When a DataNode starts up, it scans its local file system, generates a list of all the HDFS data blocks corresponding to these local files, and sends the report to the NameNode; this report is the block report.

 

6. Communication Protocols

 

All HDFS communication protocols are built on top of the TCP/IP protocol. A client connects to the NameNode on a configurable port and interacts with the NameNode through the ClientProtocol, while DataNodes interact with the NameNode using the DataNodeProtocol. A remote procedure call (RPC) is abstracted from the ClientProtocol and the DataNodeProtocol. By design, the NameNode does not initiate an RPC; it only responds to RPC requests coming from clients and DataNodes.

 

7. Robustness

 

The primary goal of HDFS is to achieve reliable data storage in the presence of failures. The three common failures are: NameNode failures, DataNode failures, and network partitions.

 

1. Disk data errors, heartbeat detection, and re-replication

Each DataNode node sends a heartbeat to the NameNode periodically. A network partition may cause a subset of DataNodes to lose contact with the NameNode. The NameNode detects this situation through the absence of heartbeats and marks those DataNodes as dead; no new I/O requests are sent to them. Any data registered on a dead DataNode is no longer valid. The death of a DataNode may cause the replication factor of some blocks to fall below the specified value. The NameNode constantly tracks blocks that need replication and starts replication whenever needed. Re-replication may be needed in the following cases: a DataNode node fails, a replica is corrupted, a hard disk on a DataNode has errors, or the replication factor of a file is increased.

 

2. Cluster Rebalancing

HDFS supports a data rebalancing scheme: if the free space on a DataNode node falls below a certain threshold, a scheme is started to automatically move data from that DataNode to a DataNode with free space. When requests for a particular file suddenly increase, a scheme may also be started to create new replicas of that file and distribute them across the cluster to satisfy application demand. These rebalancing schemes are not yet implemented.

 

3. Data Integrity

A data block retrieved from a DataNode may be corrupted, either because of a storage device error on the DataNode, a network error, or a software bug. The HDFS client software implements checksum verification of HDFS file contents. When a client creates a new HDFS file, it computes a checksum for each block of the file and stores these checksums as a separate hidden file under the same HDFS namespace. When a client retrieves file content, it verifies whether the data obtained from the DataNode matches the checksum in the corresponding checksum file; if they do not match, the client can choose to fetch a replica of that block from another DataNode.

 

4. Metadata Disk Failure

The FsImage and the EditLog are the central data structures of HDFS. If these files get corrupted, the entire HDFS instance fails. Therefore, the NameNode can be configured to support maintaining multiple copies of the FsImage and the EditLog. Any modification to the FsImage or the EditLog is synchronized to their copies. This synchronization may degrade the number of namespace transactions the NameNode can support per second. This cost is acceptable because HDFS is data-intensive rather than metadata-intensive. When the NameNode restarts, it always picks the most recent consistent FsImage and EditLog to use.

The NameNode is a single point of failure in HDFS. If the machine running the NameNode fails, manual intervention is required. Currently, restarting a NameNode that has stopped due to a failure on another machine is not yet supported.

 

5. Snapshots

Snapshots support taking a copy of data at a given point in time, so that when HDFS data gets corrupted it can be restored to a known-good point in the past. Snapshot support is not yet available in HDFS.

 

8. Data Organization

 

1. Data Blocks

Applications compatible with HDFS all deal with large data sets. They write data once and read it one or more times, and the read speed must satisfy streaming reads. HDFS supports write-once-read-many semantics for files. A typical block size is 64MB, so files are always split into 64M chunks, and each chunk is stored on a different DataNode.

 

2. Steps

A request from a client to create a file is not immediately forwarded to the NameNode. In fact, the HDFS client caches the file data into a local temporary file. Writes from the application are transparently redirected to this temporary file. Only when the accumulated data in the temporary file exceeds one block size (64M by default) does the client contact the NameNode. The NameNode inserts the file name into the hierarchy of the file system, allocates a data block for it, and returns the DataNode identifier and the target data block to the client. The client flushes the local temporary file to the specified DataNode. When the file is closed, the remaining unflushed data in the temporary file is also transferred to the specified DataNode, and then the client tells the NameNode that the file is closed. Only at that point does the NameNode commit the file creation operation to persistent storage. If the NameNode dies before the file is closed, the file is lost.

The above approach is the result of careful consideration of the target applications that run on HDFS. Without client-side caching, network speed and network congestion would have a considerable impact on throughput.

 

3. Pipelined Replication

When a client writes data to an HDFS file, the data is first written to a local temporary file. Suppose the replication factor of the file is set to 3; the client obtains a list of DataNodes from the NameNode to hold the replicas. The client then starts transferring data to the first DataNode, which receives the data in small chunks (4KB), writes each chunk to its local store, and at the same time forwards that chunk to the second DataNode. The second DataNode does the same, receiving in small chunks, storing them in its local store, and forwarding them to the third DataNode; the third DataNode simply receives and stores the data. This is pipelined replication.

 

9. Accessibility

 

HDFS provides applications with several ways to access it: the DFSShell for interacting with HDFS data through the command line, the Java API, C language wrapper APIs, and browser-based access. Access through the WebDAV protocol is under development. See the documentation for details on usage.

 

10. Space Reclamation

 

1. File Deletion and Restore

When a user or application deletes a file, the file is not immediately removed from HDFS. Instead, HDFS renames the file and moves it to the /trash directory. While the file is still in the /trash directory, it can be quickly restored. The length of time a file stays in /trash is configurable; when that time is exceeded, the NameNode deletes the file from the namespace. Deleting the file also frees the data blocks associated with it. Note that there is a waiting time delay between the moment a user deletes a file and the moment free HDFS space increases.

While a deleted file is still kept in the /trash directory, if the user wants to restore the file, they can browse the /trash directory and retrieve the file. The /trash directory keeps only the most recent copy of a deleted file. The /trash directory is no different from other file directories except for one point: HDFS applies a special policy on this directory to delete files automatically; the current default policy deletes files that have been retained for more than 6 hours, and this policy will later be made a configurable interface.

 

2. Decrease of the Replication Factor

When the replication factor of a file is reduced, the NameNode selects the excess replicas to delete. The next heartbeat passes this information to the DataNodes, which then remove the corresponding blocks and free the space; likewise, there is a time delay between calling the setReplication method and the increase in free cluster space.

 

References:

HDFS Java API: http://hadoop.apache.org/core/docs/current/api/

HDFS source code: http://hadoop.apache.org/core/version_control.html

 

Original: http://hadoop.apache.org/core/docs/current/hdfs_design.html

 

Chinese translation: http://hadoop.apache.org/common/docs/r0.21.0/cn/hdfs_design.html

« Back to News list