Analysis of Mrappmaster event-driven model and state machine processing message process in yarn

Source: Internet
Author: User

In MRv1, the relationships between objects are implemented based on function calls, and when an object passes a message to another object, it takes the form of a function call directly, and the process is serial. For example, when Tasktracker needs to execute a task, it will first download a task-dependent file (Jar package, binary file, dictionary file, etc.) and execute the task. During the entire process, the download dependent file is blocked, that is, before the previous task does not complete the file download, the next new task will remain in the waiting state, only after the download is complete, will start a separate process to run the task. The programming model based on function call is inefficient, it implies that the whole process is serial and synchronous.

In contrast, the time-driven MRV2 introduced into the model is a more efficient way. In an event-driven programming model, all objects are abstracted into event handlers, and event handlers are associated with each other through events. Each event handles one type of event, while another event is started as needed. Compared to the programming model based on function call, this programming method has the characteristics of asynchronous concurrency, which is more efficient and more suitable for large-scale distributed systems.


Below, we take the Appllicationmaster, which is mrappmaster, which is responsible for controlling the mapreduce job, as an example, to look at the whole event-driven model and state machine.

First, according to the source code, we can see that there is one of the most important central asynchronous message Scheduler Asyncdispatcher in Mrappmaster, which is responsible for the message dispatch of the entire Mrappmaster module. In addition, there are a number of other message schedulers, such as Jobeventdispatcher, Taskeventdispatcher,taskattempteventdispatcher, Speculatoreventdispatcher and so on. At the beginning of mrappmaster, there are the following key codes in the Serviceinit () function

This.jobeventdispatcher = new Jobeventdispatcher ();//register the event Dispatchersdispatcher.register ( Jobeventtype.class, Jobeventdispatcher);d ispatcher.register (Taskeventtype.class, New Taskeventdispatcher ()); Dispatcher.register (Taskattempteventtype.class, New Taskattempteventdispatcher ());d Ispatcher.register ( Committereventtype.class, Committereventhandler);

This means that when the central message Scheduler dispatcher receives the JOBEVENTTYPE message, it forwards the message to the Jobeventdispatcher message scheduler named Jobeventdispatcher. This jobeventdispatcher is a message processor specifically designed to handle the Jobeventtype event. The underlying implementation is actually a pair<jobeventtype.class, jobeventdispatcher> this kv into the central message Scheduler dispatcher HashMap. As with the rest of the message scheduler, register the corresponding message type and its corresponding message scheduler.

After dispatcher initiates a processing message thread, you can see the central Message Processor processing messages in the following code.

 Runnable CreateThread () {return new Runnable () {@Override public void run () {while (!stopped &&!)          Thread.CurrentThread (). isinterrupted ()) {event event;   try {event = Eventqueue.take (); Get message from Message queue of Central message processor dispatcher} catch (Interruptedexception IE) {if (!stopped) {Log.wa            RN ("Asyncdispatcher thread Interrupted", IE);          } return;   } if (event! = null) {dispatch (event);  Distribute the Message}}}; }
  protected void dispatch (Event event) {   //all events go thru this loop    if (log.isdebugenabled () ) {      Log.debug ("dispatching the event" + Event.getclass (). GetName () + "."           + event.tostring ());   }    class<?  Extends enum> type = Event.gettype (). Getdeclaringclass ();  Get the type of event     try{      EventHandler handler = Eventdispatchers.get (type); Get the corresponding message processor from HASHMAP based on the event type       if (handler! = null) {        Handler.handle (event  );  Use the resulting message processor to handle this event, jump to the appropriate processor implementation      } else {        throw new Exception ("No handler  For registered for "+ type";     }   }    catch (Throwable t) {      TODO Maybe log the state of the queue      log.fatal ("Error in Dispatcher thread", T);    &NBS P if (Exitondispatchexception&nbsP         && (Shutdownhookmanager.get (). isshutdowninprogress ()) = = False) {        Log.info ("Exiting, Bbye..");         System.exit ( -1);     }   } }

When the client submits a job, Mrappmaster creates a job and sends a Job_init event to the central message processor dispatcher. Let's take the Job_init event as an example to see what happened.

First dispatcher receives the Job_init event, the processor that gets the event based on the Job_init event type (jobeventtype.class) should be jobeventdispatcher, Send it to Jobeventdispatcher.

Private class Jobeventdispatcher implements Eventhandler<jobevent> {  @SuppressWarnings ("unchecked")  @ Override public  void handle (Jobevent event) {    ((eventhandler<jobevent>) context.getjob (event.getjobid ())). Handle (event);  This job handles this event  }}
Jobeventdispatcher handles this job_init message. After entering the JOBIMPL message handler function
public void handle (Jobevent event) {if (log.isdebugenabled ()) {Log.debug ("processing" + event.getjobid () + "of Typ  E "+ event.gettype ());    } try {writelock.lock ();    Jobstateinternal oldstate = Getinternalstate ();   try {getstatemachine (). Dotransition (Event.gettype (), event);    Gets the current state machine of the job, making a state transition based on the current message event.      } catch (Invalidstatetransitonexception e) {//jumps to the dotransition () function implemented by StateMachine.      Log.error ("Can ' t handle this event at current state", e);      Adddiagnostic ("Invalid event" + event.gettype () + "on Job" + this.jobid);    Eventhandler.handle (New Jobevent (This.jobid, jobeventtype.internal_error)); }//notify the EventHandler of state change if (oldstate! = Getinternalstate ()) {Log.info (jobId + "Job Transit      ioned from "+ Oldstate +" to "+ getinternalstate ());    Rememberlastnonfinalstate (oldstate);  }} finally {Writelock.unlock (); }}
Mrappmaster message distribution process to this end, this has entered the operation of the job state machine, of course, depending on the task, may enter the task state machine, taskattempt state machine and so on. In short, the state machine and the various hooks in the state machine correspond to job/task/taskattempt control.

We know that a job corresponds to a state machine, and we should also know that in yarn implementation A state machine consists of the following three parts: 1. Status (node) 2. Event (ARC) 3. Hook (processing after triggering the event).

In the Jobimpl.java file, we can see the process of building the job state machine:

Protected static final Statemachinefactory<jobimpl, Jobstateinternal, Jobeventtype, jobevent> statemachinefact        Ory = new Statemachinefactory<jobimpl, jobstateinternal, Jobeventtype, jobevent> (jobstateinternal.new) Constructs the Jobimpl state machine, the initial state is the new state//transitions from New, addtransition (Jobstateinternal.new, JobSt Ateinternal.new,//Add a status change Jobeventtype.job_diagnostic_update, Diagnostic_update_transiti ON). Addtransition (Jobstateinternal.new, jobstateinternal.new,//Add a status change Jobeventtype.job_cou Nter_update, counter_update_transition). Addtransition//Add a            State changes (Jobstateinternal.new, Enumset.of (jobstateinternal.inited, jobstateinternal.failed), Jobeventtype.job_init, New Inittransition ()). Addtransition (Jobstateinternal.new, Jobstateinternal.kill ED, Jobeventt.Ype.            Job_kill, New Killnewjobtransition ()). Addtransition (Jobstateinternal.new, Jobstateinternal.error, Jobeventtype.internal_error, Internal_error_transition). Addtransition (Jobstateinternal.new, JobSt            Ateinternal.reboot, Jobeventtype.job_am_reboot, internal_reboot_transition) ...    ...
There are many more, the job state machine is compared to a complex state machine, involving a lot of state and events, can be seen through the yarn state machine in-depth understanding, there is no more discussion.
Call the Addtransition () function to add a state change.  The Addtransition () function has four parameters, respectively: 1.preState 2.postState 3.eventType 4.hook. They represent 1. The state before the event, 2. The state after the event occurred, 3. Events, 4. Hooks triggered when an event occurs

Code in Addtransition (Jobstateinternal.new,enumset.of (jobstateinternal.inited, jobstateinternal.failed), Jobeventtype.job_init,new inittransition ()) This state change indicates that the event occurred before the new state, the event was inited state or failed state, and the event was Job_init, Hooks are inittransition objects. That is, if the current state of the job state machine is new, which is a job_init event, then the state opportunity triggers the dotransition () function in the Inittransition object, and if the transition () function returns the Inited state, Then state machine latest state is inited, if the dotransition () function returns failed state, then state machine is the latest state is failed.

So let's take a look at the state transformation process of state machine.

private  Class Internalstatemachine implements Statemachine<state, EventType, event> {private final OPERAND OPERAND;  Private State currentstate;    Internalstatemachine (OPERAND OPERAND, State initialstate) {this.operand = OPERAND;    This.currentstate = initialstate;    if (!optimized) {maybemakestatemachinetable ();  }} @Override public synchronized state getcurrentstate () {return currentstate; } @Override Public Synchronized state dotransition (EventType EventType, event event) throws Invalidstatetransitone xception {currentstate = StateMachineFactory.this.doTransition//state machine change (operand, curr    Entstate, EventType, event);  return currentstate; }}
Private state dotransition          (OPERAND OPERAND, State oldstate, EventType EventType, EVENT E Vent)     throws Invalidstatetransitonexception { //We can assume that statemachinetable is Non-null Becau SE we call // maybemakestatemachinetable () When we build a innerstatemachine, // and This code o  Nly gets called from inside a working innerstatemachine .  map<eventtype, Transition<operand, state, EventType,                                event>> transitionmap    = Statemachinetable.get (oldstate); Gets all occurrences of this state according to the current status   if (Transitionmap! = null) {    Transition<operand, state, EventType, EVENT&G T                               transition        = Transitionmap.get (EventType); Gets the corresponding hook based on the event, for a Transition object (transition is an interface)     if (transition! = null) {      return trans  Ition.dotransition (operand, oldstate, event, EventType); //Call the object's Dotransition () function    } }  throw new Invalidstatetransitonexception (Oldstate, EventType);} 
private Class singleinternalarc                  implements Transition<operand, ST ATE, EventType, event> {  private State poststate;  private Singlearctransition<operand, event> hook; Transition hook  Singleinternalarc (state poststate,      Singlearctransition<operand, EVENT > Hook) {    this.poststate = poststate;    This.hook = hook; }  @Override   Public State dotransition (OPERAND OPERAND, State oldstate,                    & nbsp       Event event, EventType EventType) {    if (hook! = null) {      Hook.transit          Ion (operand, event); Call Hook's transition () function    }    return poststate; }} 
This statemachinetable is a map<state, map<eventtype, transition>> data structure. All <eventtype of this state can be obtained according to the current state, transition> correspondence. The Transition object (also known as a hook) can then be obtained based on the event type. Then, the state machine can be called by the current state, the event calls the hook function. is essentially the form of Statemachine.hook (Prestate, event), because OO thought has been distorted. This is followed by the implementation of the transition object, because at the beginning of the construction of the state machine, Jobimpl constructs a state change addtransition (Jobstateinternal.new,enumset.of ( jobstateinternal.inited, jobstateinternal.failed), Jobeventtype.job_init,new inittransition ()), where the hooks are The Inittransition object. The transition () function of the Inittransition object is called when the hook is called. The transition () function of the Inittransition object includes many operations that initialize a job, such as creating a maptask, creating a reducetask, creating an input file that maptask requires, split, and so on. Do not make too much narration here, you can read the relevant articles on your own.

This is the basic process of yarn's state machine work and how it relates to the Mrappmaster message distribution mechanism. Due to the many improvements in yarn relative to MRV1, Mrappmaster now replaces the jobtracker of the past MRv1 to control job and task, with clearer structure and more modularity. Overall, Mrappmaster is a relatively complex module that requires more careful analysis.


If you want to reprint please indicate reprint from http://blog.csdn.net/gjt19910817/article/details/43441801



Analysis of Mrappmaster event-driven model and state machine processing message process in yarn

Related Article

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.