Hadoop

Hadoop is a reliable shared storage and analysis system. The storage is provided by Hadoop Distributed File System (HDFS) and analysis by MapReduce.

MapReduce

  • MapReduce is a programming model that gives:

    1. Automatic parallelization and distribution
    2. Fault-tolerance
    3. I/O scheduling
    4. Status and monitoring
  • Properties

    1. same key always go to the same reduce function
    2. all value associated to the same key will appear once and only once
    3. the reduce function cannot start until all map functions are finished
    4. the key would be sorted for each reducer
    5. the code is not optimized

Programming Model

  • Input and output: each a set of key/value pairs
  • Two functions implemented by users

    1. Mapper: (k1, v1) → list(k2,v2)

      • Takes an input key/value pair
      • Produces a set of intermediate key/value pairs
    2. Reducer: (k2, list(v2)) → list(k3,v3)

      • Takes a set of values for an intermediate key
      • Produces a set of output values
  • WordCount Example:

    1. The mapper takes an input key-value pair, tokenizes the document, and emits an intermediate key-value pair for each word. (word → key, 1 → value)

      1
      2
      3
      4
      MAP(String key, String value):
      // key: document name, value: document content
      for each word w in value:
      Emit(w, "1")
    2. The reducer sums up all counts associated each word, and emits final key-value pairs with the word as the key

      1
      2
      3
      4
      5
      6
      REDUCER(String key, Iterator values)
      // key: a word, values: a list of counts
      int result = 0
      for each v in values:
      result += ParseInt(v)
      Emit(key, AsString(result))
      • Notice: All values of the same word will go to the same reducer at the same time.

Data Flow

A MapReduce job is a unit of work that the client wants to be performed. Hadoop runs the job by dividing it into tasks (map tasks and reduce tasks).

  • Nodes: control the job execution process

    1. Master: coordinates all jobs
    2. Workers/Slaves: runs tasks and sends reports to master
  • Split: Hadoop divides the input to a MapReduce job into fixed-size pieces called splits

    • Hadoop creates one map task for each split and map function runs each record in the split.
  • Split size:

    • too large: lacks parallelization
    • too small: increases management overhead
    • A good split size = HDFS block (default = 128MB)
  • Data locality optimization

    • Hadoop tries to run the map task on a node where the input data resides in HDFS.
    • Map tasks write their output to the local disk (not in HDFS).
    • Reduce tasks don’t have the advantage of data locality — the input to a single reduce task is normally the output from all mappers.
  • Dealing with Failures

    • Map worker failure
      • Map tasks completed or in-progress at worker are reset to idle
      • Reduce workers are notified when task is rescheduled on another worker
    • Reduce worker failure
      • Only in-progress tasks are reset to idle
      • Reduce task is restarted
    • Master failure
      • MapReduce task is aborted and client is notified
  • Fault Tolerance

    • If the node running the map task fails before the map output has been consumed by the reduce task, then Hadoop will automatically re-run the map task on another node to re-create the map output.
    • The output of reduce is normally sorted in HDFS for reliability where the first replica is stored on the local node and other replicas are stored on off-rack nodes.

Partition and Combiner

  1. Partition

    • Map outputs are partitioned to multiple reduce tasks
    • Simple hash key H(k) mod #R (can be user-defined)
  2. Combiner

    • Map outputs are reduced before being sent to reducers
    • Used as an optimization to reduce network traffic
    • MapReduce can only run the combine function if it has time, we have no idea when do the combine function be executed, it is controled by the MapReduce Schedule
    • Since the reducer would either take the input from mappers or combiners, it is necessary to ensure the output format of mapper and combiner are same as the input format of reducers

HDFS

Hadoop Distributed Filesystem (HDFS) is a filesystem designed for storing very large files with streaming data access patterns, running on clusters of commodity hardware.

  • Assumptions:

    1. Very large files
    2. Streaming data access: write-one, read-many-times pattern (WORM)
    3. Commodity hardware: hardware failure is a norm rather than exception
  • More Goals:

    1. Simple coherency model: data won’t be changed (synchronization overhead)
    2. Moving computation is cheaper than moving data
    3. Portability across heterogeneous hardware and software platforms
  • Not Suitable:

    1. requires low-latency data access
    2. lot of small files
    3. multiple writers, arbitrary file modifications

HDFS Architectural

  • Architectural

    • A namenode: a master server that manages the file system namespace and regulates access to files by clients.
    • Multiple datanode: each manages storage attached to the nodes, and serves read/write data requests from clients.
  • Data Blocks
    HDFS arranges data in blocks of very large size (128MB by default), which designed for very large files

    • too small: increase management overhead + expensive in disk seeks
    • too large: loss parallelism that cannot access the block parallelly by datanodes
  • Namespace
    HDFS supports a traditional hierarchical file organization, one can create and remove files, move a file from one directory to another, or rename a file.

  • Data Replication
    Blocks of a file are replicated (create identical copies) for fault tolerance. The block size and replication factor (number of copies) can be defined in the configuration.
    The namenode makes the decision of regarding replications of blocks. It periodically receives a Heartbeat (keep-alive message) and Blockreport (a list of all blocks) from each of the datanodes.

    • Default replication factor is 3 (2 in single machine)
    • Rack-aware placement: balance the reliability and write performance
      1. One replica in one node in the locak rack
      2. One replica in another node in the local rack (if node fault, it can search from other server in same rack)
      3. One replica in a different node in a different rack
  • Robustness

    • Data disk failures

      • if namenode receives no heartbeats which means datanode fails, re-replicate data to new datanodes
    • Cluster rebalancing

      • HDFS can dynamically generate new replicas and rebalance other data in the cluster
    • Data integrity

      • Checksum is attached to each block
    • Namenode failure

      • Currently it’s a single point of failure in HDFS
      • Needs manual intervention
    • Snapshots

      • Currently not supported

Read

  • HDFS Read Steps

    1. HDFS client opens the file through the file system object.
    2. The file system object calls the namenode, using RPC, to determine the locations of the blocks for the first few blocks in the file. The file system object returns an input stream.
    3. The client then calls the read function to retrieve data blocks through the input stream.
    4. Data is streamed from the datanode back to client, which calls the read function repeatedly on the input stream.
    5. When the end of the block is reached, the input stream will close the connection to the datanode, then find the best datanode for the next block
    6. When the client has finished reading, it closes the input stream.
  • Notices

    1. The client contacts datanodes directly to retrieve data and is guided by the namenode to the best datanode for each block. Namenode doesn’t serve data.
    2. If the input stream encounters an error while communicating with a datanode, then it will try the next “closest” one for that block. The distance is defined in considering the network as a tree:

      1. Distance 0: processes on the same node
      2. Distance 2: processes on the different nodes but on the same rack
      3. Distance 4: processes on the nodes on different racks but in the same data center
      4. Distance 6: processes on the same nodes in different data centers

Write

  • HDFS Write Steps

    1. HDFS client creates the file on the file system object.
    2. The file system object makes an RPC call to the namenode to create a new file in the filesystem’s namespace. An output stream is created.
    3. As the client writes data, the output stream splits it into packets, which it writes to an internal queue called the data queue.
    4. The output stream streams the packets to the first datanode in the pipeline, which stores the packet and forwards it to the second datanode in the pipeline, followed by the third (and last) datanode in the pipeline.
    5. The output stream maintains an internal queue of packets that are waiting to be acknowledged by datanodes called the ack queue. A packet is removed from the ack queue only when it has been acknowledged by all the datanodes in the pipeline.
    6. When the client has finished writing data, it closes the output stream.
    7. The client contacts the namenode to signal that the file is complete.
  • Implementation

    1. Pipeline:
      it recieves one full copy of data block, sends it forward (needless to wait for recieving all data)

      • Bottleneck: if client did not recieve the acknowledgement, it would start from the beginning
    2. Dispatch:
      it sends the data block to specific datanode directly (all datanode needless to send data)

      • Bottleneck: speed for writing data into the datanode does not based on the network speed

YARN

Yet Another Resource Negotiator (YARN) provides APIs for computing frameworks (e.g., MapReduce) to request and work with cluster resources (Goal: decouple programming model and resource management).

  • Workflow
    1. The client requests the resource manager for running an application master process
    2. The resource manager finds a node manager to launch the application master in a container
    3. The application master may request more resources if necessary
    4. The application master runs the requests in containers