Some thoughts about corresponding Taskattempt storage map scheme in the task of MapReduce source code analysis

Source: Internet
Author: User

We know that MapReduce has a three-layer dispatch model, namely Job-->task-->taskattempt, and:

1, usually a job exists multiple tasks, these tasks have a total of Map task and Redcue task two large types (for simplifying the description, Map-only job, Jobsetup task and other complex circumstances do not consider here);

2. Each task can try to run 1-n this, and usually 1 times in many cases, the task executes multiple times only if the speculative execution principle is turned on and a dragged task is present, or if a task fails before execution.

There is a member variable attempts in Taskimpl that stores the mappings between Taskattemptid and Taskattempt in the taskattempt contained in the task, and is defined and initialized as follows:

    Private Map<taskattemptid, taskattempt> attempts;
    this.attempts = Collections.emptymap ();

In other words, attempts is initially initialized to Collections.emptymap (), and we look at its implementation:

    @SuppressWarnings ("unchecked") public    static final <K,V> map<k,v> Emptymap () {        return (map<k,v >) empty_map;    }
    @SuppressWarnings ("unchecked") public    static final Map empty_map = new emptymap<> ();
    /** * @serial include */private static class Emptymap<k,v> extends Abstractmap<k,v>        Implements Serializable {private static final long serialversionuid = 6428348081105594320L;        public int size () {return 0;}        public Boolean isEmpty () {return true;}        public boolean ContainsKey (Object key) {return false;}        public boolean Containsvalue (Object value) {return false;}        Public V get (Object key) {return null;}        Public set<k> KeySet () {return Emptyset ();}        Public collection<v> values () {return Emptyset ();}        Public set<map.entry<k,v>> EntrySet () {return Emptyset ();}        public boolean equals (Object o) {return (o instanceof Map) && ((map<?,? >) o). IsEmpty ();        } public int hashcode () {return 0;} Preserves Singleton Property Private Object Readresolve () {return empty_map; }    }
As you can see, Emptymap is an empty map, with a size of 0,isempty of True,containskey and containsvalue for any key or value that is false.

The logic to add it to attempts after generating the taskattempt is as follows:

//the task to be created runs an attempt Taskattemptimpl instance attempt the corresponding relationship with its ID to Taskimpl task run attempt collection attempts,//    Attempts is first initialized to Collections.emptymap ()//this.attempts = Collections.emptymap (); Switch (attempts.size ()) {case 0://If the attempts size is 0, which is collections.emptymap (), replace it with Collections.singletonmap (), and join the Taskattemptimpl instance attempt attempts = Collections.singletonmap (Attempt.getid (), (taskattempt) Attem        PT);              Break        Case 1://If the attempts size is 1, which is Collections.singletonmap (), replace it with Linkedhashmap and join the previous and current Taskattemptimpl instances attempt Map<taskattemptid, taskattempt> newattempts = new Linkedhashmap<taskattemptid, TaskAttempt> (MaxA        ttempts);        Newattempts.putall (attempts);        attempts = newattempts;        Attempts.put (Attempt.getid (), attempt);      Break        Default://If the attempts size is greater than 1, the description is actually a linkedhashmap, put it directly attempts.put (Attempt.getid (), attempt);    Break }
When a task first generates a taskattempt and joins it to attempts, attempts is collections.emptymap (), and its size is definitely 0, when Taskattempt is added to attempts, Converts the attempts into Collections.singletonmap, which contains only a map of the key-value pair. and collections.singletonmap is defined as follows:

    public static <K,V> map<k,v> Singletonmap (K key, V value) {        return new singletonmap<> (key, value); c2/>}
    private static class Singletonmap<k,v> extends Abstractmap<k,v> implements Serializable        {private static final long serialversionuid = -6979724477215052911l;        Private Final K K;        Private Final v V;            Singletonmap (k key, V value) {k = key;        v = value;        } public int size () {return 1;}        public Boolean isEmpty () {return false;}        public boolean ContainsKey (Object key) {return Eq (key, k);}        public boolean Containsvalue (Object value) {return EQ (value, v);}        Public V get (Object key) {return (EQ (key, k) v:null);}        private transient set<k> keySet = null;        private transient set<map.entry<k,v>> entryset = null;        private transient collection<v> values = null;            Public set<k> KeySet () {if (keyset==null) KeySet = Singleton (K); REturn KeySet; } public set<map.entry<k,v>> EntrySet () {if (entryset==null) EntrySet = Coll            Ections.<map.entry<k,v>>singleton (New simpleimmutableentry<> (K, V));        return entryset;            Public collection<v> values () {if (values==null) values = Singleton (V);        return values; }    }

As can be seen, Singletonmap is a map containing only a pair of key-value, whose size is fixed to 1,containskey and Containsvalue returns the entry key, value is the K inside the Singletonmap, V is equal, get is determined to return V or null, depending on whether the incoming parameter is k, and so on.

And when the attempts size is 1, That is, when the collections.singletonmap, add taskattempt words, you need to change attempts to linkedhashmap, the previous and newly added taskattempt to join, thereafter, if there is taskattempt to join, Put it directly. Linkedhashmap initialization, its capacity has been determined, for the maxattempts, this maxattempts is taken from the method Getmaxattempts (), which is an abstract method in Taskimpl, by its two sub-classes Maptaskimpl, Reducetaskimpl are implemented separately, as follows:

Taskimpl.java

  No override of this method is require that the subclass is initialized.  protected abstract int getmaxattempts ();
Maptaskimpl.java

  @Override  protected int getmaxattempts () {    return Conf.getint (mrjobconfig.map_max_attempts, 4);  }
Reducetaskimpl.java

  @Override  protected int getmaxattempts () {    return Conf.getint (mrjobconfig.reduce_max_attempts, 4);  }
As can be seen, map and reduce task taskattempt have a limit, taken from the parameter mapreduce.map.maxattempts, mapreduce.reduce.maxattempts, parameters are not configured, the default is 4. Now that we have the upper limit of the number of taskattempt, we initialize the Linkedhashmap specified capacity, which is constructed as follows:

    /**     * Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance     * with the specified Initial capacity and a default load factor (0.75).     *     * @param  initialcapacity The initial capacity     * @throws IllegalArgumentException If the initial capacity is Negative     */public    linkedhashmap (int initialcapacity) {        super (initialcapacity);        Accessorder = false;    }
Call the constructor of the parent class HashMap, as follows:

    /**     * Constructs an empty <tt>HashMap</tt> with the specified initial     * capacity and the default Loa D factor (0.75).     *     * @param  initialcapacity The initial capacity.     * @throws IllegalArgumentException If the initial capacity is negative.     */Public    HashMap (int. initialcapacity) {This        (initialcapacity, default_load_factor);    }
Determines its initial capacity for the specified initialcapacity.


Thinking:

Why does MapReduce have to be so designed? I think about it, there are roughly two reasons for business logic and performance:

1, task scheduling execution is sequential, and task abstract class Taskimpl implementation class, whether it is maptaskimpl, or REDUCETASKIMPL structure, are must first, so there is a problem, If attempts come up to be constructed as a specified size of linkedhashmap, will inevitably cause the waste of space, but also the performance of the consumption, moreover, the success of the job execution, or something, and if we initialize to Collections.emptymap (), It is easy to solve the above two problems;

2, according to common sense, ideally, each task should have and only one taskattempt, only if the task fails to retry after the failure, or to open the speculative execution mechanism to effectively speed up the execution of the drag task to open the backup task, etc., there will be more than one taskattempt, When the first taskattempt is constructed, the attempts is upgraded from Collections.emptymap () to Collections.singletonmap (), whether in terms of space utilization, performance, or business logic, are more closely fit the actual situation;

3, the need to retry the task or to open the backup task, the attempts from Collections.singletonmap () to the specified capacity of Linkedhashmap, which has the concept of delayed loading;

4, the less resources, the higher the performance, for other jobs or tasks, is a kind of gospel, can improve the overall resource utilization efficiency of the cluster.

The above performance and business logic considerations, you may not think much of the performance improvement, but if in large-scale clusters, when the number of large jobs, the number of tasks, this advantage is more obvious, and the benefits of it, in other jobs, will be a boon! This design of the details, it is worth our learning, reference and reflection!











Some thoughts about corresponding Taskattempt storage map scheme in the task of MapReduce source code analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.