标签:
Hadoop2.x.x版本的底层实现中作了很多优化:用状态机对各种对象生命周期和状态转移进行管理;采用事件机制避免线程同步与阻塞;采用Protocol Buffers优化RPC性能;采用Apache Avro优化日志等。本文主要针对YARN中状态机的实现进行分析,在这个过程中,会捎带一些事件的内容。
YARN中的很多组件之间进行通信,主要借助于事件。为了可读性、可维护性及可扩展性,YARN中的事件由事件名称和事件类型组成。比如JobImpl处理的事件名称为JobEvent,而事件类型为JobEventType。有关Hadoop2.6.0的事件分类与实现可以参考《Hadoop2.6.0的事件分类与实现》一文。
YARN中的每个组件都有其自身所处的一系列状态,比如JobImpl内部的一系列状态都定义在JobStateInternal中,如代码清单1所示。
代码清单1
public enum JobStateInternal { NEW, SETUP, INITED, RUNNING, COMMITTING, SUCCEEDED, FAIL_WAIT, FAIL_ABORT, FAILED, KILL_WAIT, KILL_ABORT, KILLED, ERROR, REBOOT }
我们已经了解了事件与状态的基本实现与概念,那么事件与状态有什么关系?从哲学角度讲,状态是一个事物的静止属性,而事件则是一个事物与外界沟通的桥梁,只有静止却没有变化,那么它只是一滩死水。事物只有在接收信息后动起来,才算与外界有了互动。一个事物动起来就会潜移默化的发生改变,它内部就会发生转换。一个对象当前处于状态state0,当对象接收到事件Event后,将引发转换动作transition,最终当前对象的状态过渡到state1,这个过程可以用图1来表示。
图1 状态迁移示例
YARN中与过渡相关的类图如图2所示。
图2 YARN中与过渡相关的类图
YARN中的各个组件的变化都离不开状态的过度与变化,于是对这种行为进行了抽象,这种转换分为两类:单弧过渡与多弧过渡。(这种翻译不知道是否准确,我认为从一个状态到另一个状态的转换发生时,就像是在两个状态之间划了一道弧线一样)
YARN中单弧过渡的实现代码如代码清单2,它的作用是当有限状态机(FSM)中的状态转换为已经注册到状态机的某种状态时,伴随的行为。
代码清单2
@Public @Evolving public interface SingleArcTransition<OPERAND, EVENT> { /** * Transition hook. * * @param operand the entity attached to the FSM, whose internal * state may change. * @param event causal event */ public void transition(OPERAND operand, EVENT event); }
由于SingleArcTransition的具体实现类只负责接收到事件后的具体操作或行为,并没有包含状态相关的信息,所以在状态机执行状态过渡时,并不是直接调用SingleArcTransition具体实现类的transition方法,而是由接口Transition定义(见代码清单3)真正的转态过渡(包括行为和状态改变)。
代码清单3
private interface Transition<OPERAND, STATE extends Enum<STATE>, EVENTTYPE extends Enum<EVENTTYPE>, EVENT> { STATE doTransition(OPERAND operand, STATE oldState, EVENT event, EVENTTYPE eventType); }
SingleInternalArc作为Transition接口的实现类,在代理SingleArcTransition的同时,负责状态变换,见代码清单4。
代码清单4
private class SingleInternalArc implements Transition<OPERAND, STATE, 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, EVENT event, EVENTTYPE eventType) { if (hook != null) { hook.transition(operand, event); } return postState; } }
YARN中多弧过渡的实现代码如代码清单5,它的作用是当有限状态机(FSM)中的状态转换为已经注册到状态机的多个有效状态中的一个时,伴随的行为与操作。
代码清单5
@Public @Evolving public interface MultipleArcTransition <OPERAND, EVENT, STATE extends Enum<STATE>> { /** * Transition hook. * @return the postState. Post state must be one of the * valid post states registered in StateMachine. * @param operand the entity attached to the FSM, whose internal * state may change. * @param event causal event */ public STATE transition(OPERAND operand, EVENT event); }由于MultipleArcTransition的具体实现类只负责接收到事件后的具体操作或行为,并没有包含状态相关的信息,所以在状态机执行状态过渡时,并不是直接调用MultipleArcTransition具体实现类的transition方法,而是通过代理类MultipleInternalArc,见代码清单6。MultipleInternalArc也实现了Transition接口,并在代理MultipleArcTransition的转换行为的同时,负责状态变换。
代码清单6
private class MultipleInternalArc implements Transition<OPERAND, STATE, EVENTTYPE, EVENT>{ // Fields private Set<STATE> validPostStates; private MultipleArcTransition<OPERAND, EVENT, STATE> hook; // transition hook MultipleInternalArc(Set<STATE> postStates, MultipleArcTransition<OPERAND, EVENT, STATE> hook) { this.validPostStates = postStates; this.hook = hook; } @Override public STATE doTransition(OPERAND operand, STATE oldState, EVENT event, EVENTTYPE eventType) throws InvalidStateTransitonException { STATE postState = hook.transition(operand, event); if (!validPostStates.contains(postState)) { throw new InvalidStateTransitonException(oldState, eventType); } return postState; } }
为了将所有状态机中的状态过渡与状态建立起映射关系,YARN中提供了ApplicableTransition接口用于将SingleInternalArc和MultipleInternalArc添加到状态机的拓扑表中,提高在检索状态对应的过渡实现时的性能,ApplicableTransition的实现类为ApplicableSingleOrMultipleTransition类,其apply方法用于代理SingleInternalArc和MultipleInternalArc,将它们添加到状态拓扑表中。ApplicableTransition接口的定义见代码清单7,ApplicableSingleOrMultipleTransition的实现见代码清单8。
代码清单7
private interface ApplicableTransition <OPERAND, STATE extends Enum<STATE>, EVENTTYPE extends Enum<EVENTTYPE>, EVENT> { void apply(StateMachineFactory<OPERAND, STATE, EVENTTYPE, EVENT> subject); }
代码清单8
static private class ApplicableSingleOrMultipleTransition <OPERAND, STATE extends Enum<STATE>, EVENTTYPE extends Enum<EVENTTYPE>, EVENT> implements ApplicableTransition<OPERAND, STATE, EVENTTYPE, EVENT> { final STATE preState; final EVENTTYPE eventType; final Transition<OPERAND, STATE, EVENTTYPE, EVENT> transition; ApplicableSingleOrMultipleTransition (STATE preState, EVENTTYPE eventType, Transition<OPERAND, STATE, EVENTTYPE, EVENT> transition) { this.preState = preState; this.eventType = eventType; this.transition = transition; } @Override public void apply (StateMachineFactory<OPERAND, STATE, EVENTTYPE, EVENT> subject) { Map<EVENTTYPE, Transition<OPERAND, STATE, EVENTTYPE, EVENT>> transitionMap = subject.stateMachineTable.get(preState); if (transitionMap == null) { // I use HashMap here because I would expect most EVENTTYPE‘s to not // apply out of a particular state, so FSM sizes would be // quadratic if I use EnumMap‘s here as I do at the top level. transitionMap = new HashMap<EVENTTYPE, Transition<OPERAND, STATE, EVENTTYPE, EVENT>>(); subject.stateMachineTable.put(preState, transitionMap); } transitionMap.put(eventType, transition); } }
YARN中状态机的实现类是StateMachineFactory,它主要包含4个属性信息:
代码清单9
private class TransitionsListNode { final ApplicableTransition<OPERAND, STATE, EVENTTYPE, EVENT> transition; final TransitionsListNode next; TransitionsListNode (ApplicableTransition<OPERAND, STATE, EVENTTYPE, EVENT> transition, TransitionsListNode next) { this.transition = transition; this.next = next; } }
transitionsListNode形成的过渡列表节点可以用图3表示。
图3 transitionsListNode过渡链表结构
图4 状态拓扑表数据结构
StateMachineFactory的公有构造器只有一个,其实现见代码清单10。
代码清单10
public StateMachineFactory(STATE defaultInitialState) { this.transitionsListNode = null; this.defaultInitialState = defaultInitialState; this.optimized = false; this.stateMachineTable = null; }可见新建的StateMachineFactory实例只有一个默认初始状态参数defaultInitialState。
StateMachineFactory的私有构造器有两个,其中代码清单11中的构造器在addTransition方法中使用。从其实现看出,此构造器的主要作用是构建transitionsListNode链表。
代码清单11
private StateMachineFactory (StateMachineFactory<OPERAND, STATE, EVENTTYPE, EVENT> that, ApplicableTransition<OPERAND, STATE, EVENTTYPE, EVENT> t) { this.defaultInitialState = that.defaultInitialState; this.transitionsListNode = new TransitionsListNode(t, that.transitionsListNode); this.optimized = false; this.stateMachineTable = null; }而代码清单12中的构造器则在installTopology方法中使用。
代码清单12
private StateMachineFactory (StateMachineFactory<OPERAND, STATE, EVENTTYPE, EVENT> that, boolean optimized) { this.defaultInitialState = that.defaultInitialState; this.transitionsListNode = that.transitionsListNode; this.optimized = optimized; if (optimized) { makeStateMachineTable(); } else { stateMachineTable = null; } }代码清单12中的构造器当optimized参数为true时,调用了makeStateMachineTable方法,makeStateMachineTable的实现见代码清单13所示。
代码清单13
private void makeStateMachineTable() { Stack<ApplicableTransition<OPERAND, STATE, EVENTTYPE, EVENT>> stack = new Stack<ApplicableTransition<OPERAND, STATE, EVENTTYPE, EVENT>>(); Map<STATE, Map<EVENTTYPE, Transition<OPERAND, STATE, EVENTTYPE, EVENT>>> prototype = new HashMap<STATE, Map<EVENTTYPE, Transition<OPERAND, STATE, EVENTTYPE, EVENT>>>(); prototype.put(defaultInitialState, null); // I use EnumMap here because it‘ll be faster and denser. I would // expect most of the states to have at least one transition. stateMachineTable = new EnumMap<STATE, Map<EVENTTYPE, Transition<OPERAND, STATE, EVENTTYPE, EVENT>>>(prototype); for (TransitionsListNode cursor = transitionsListNode; cursor != null; cursor = cursor.next) { stack.push(cursor.transition); } while (!stack.isEmpty()) { stack.pop().apply(this); } }通过阅读makeStateMachineTable的实现,不难看出其作用:
为了简化叙述,本节以JobImpl中状态机的构建为例。由于JobImpl的状态机预设的(调用addTransition方法)加入的ApplicableSingleOrMultipleTransition非常多,我们节选其中的2个作为典型进行分析。最后还会分析installTopology方法的实现。JobImpl中状态机的定义见代码清单14。
代码清单14
protected static final StateMachineFactory<JobImpl, JobStateInternal, JobEventType, JobEvent> stateMachineFactory = new StateMachineFactory<JobImpl, JobStateInternal, JobEventType, JobEvent> (JobStateInternal.NEW) // Transitions from NEW state .addTransition(JobStateInternal.NEW, JobStateInternal.NEW, JobEventType.JOB_DIAGNOSTIC_UPDATE, DIAGNOSTIC_UPDATE_TRANSITION) .addTransition(JobStateInternal.NEW, JobStateInternal.NEW, JobEventType.JOB_COUNTER_UPDATE, COUNTER_UPDATE_TRANSITION) .addTransition (JobStateInternal.NEW, EnumSet.of(JobStateInternal.INITED, JobStateInternal.NEW), JobEventType.JOB_INIT, new InitTransition()) // 省略其它addTransition调用 // create the topology tables .installTopology();
public StateMachineFactory <OPERAND, STATE, EVENTTYPE, EVENT> addTransition(STATE preState, STATE postState, EVENTTYPE eventType, SingleArcTransition<OPERAND, EVENT> hook){ return new StateMachineFactory<OPERAND, STATE, EVENTTYPE, EVENT> (this, new ApplicableSingleOrMultipleTransition<OPERAND, STATE, EVENTTYPE, EVENT> (preState, eventType, new SingleInternalArc(postState, hook))); }
代码清单16
public StateMachineFactory <OPERAND, STATE, EVENTTYPE, EVENT> addTransition(STATE preState, Set<STATE> postStates, EVENTTYPE eventType, MultipleArcTransition<OPERAND, EVENT, STATE> hook){ return new StateMachineFactory<OPERAND, STATE, EVENTTYPE, EVENT> (this, new ApplicableSingleOrMultipleTransition<OPERAND, STATE, EVENTTYPE, EVENT> (preState, eventType, new MultipleInternalArc(postStates, hook))); }
public StateMachineFactory <OPERAND, STATE, EVENTTYPE, EVENT> installTopology() { return new StateMachineFactory<OPERAND, STATE, EVENTTYPE, EVENT>(this, true); }
private static final DiagnosticsUpdateTransition DIAGNOSTIC_UPDATE_TRANSITION = new DiagnosticsUpdateTransition();
private static class DiagnosticsUpdateTransition implements SingleArcTransition<JobImpl, JobEvent> { @Override public void transition(JobImpl job, JobEvent event) { job.addDiagnostic(((JobDiagnosticsUpdateEvent) event) .getDiagnosticUpdate()); } }
public static class InitTransition implements MultipleArcTransition<JobImpl, JobEvent, JobStateInternal> { @Override public JobStateInternal transition(JobImpl job, JobEvent event) { // 省略具体逻辑 } } }
private STATE doTransition (OPERAND operand, STATE oldState, EVENTTYPE eventType, EVENT event) throws InvalidStateTransitonException { // We can assume that stateMachineTable is non-null because we call // maybeMakeStateMachineTable() when we build an InnerStateMachine , // and this code only gets called from inside a working InnerStateMachine . Map<EVENTTYPE, Transition<OPERAND, STATE, EVENTTYPE, EVENT>> transitionMap = stateMachineTable.get(oldState); if (transitionMap != null) { Transition<OPERAND, STATE, EVENTTYPE, EVENT> transition = transitionMap.get(eventType); if (transition != null) { return transition.doTransition(operand, oldState, event, eventType); } } throw new InvalidStateTransitonException(oldState, eventType); }
后记:个人总结整理的《深入理解Spark:核心思想与源码分析》一书现在已经正式出版上市,目前京东、当当、天猫等网站均有销售,欢迎感兴趣的同学购买。
京东:http://item.jd.com/11846120.html
当当:http://product.dangdang.com/23838168.html
标签:
原文地址:http://blog.csdn.net/beliefer/article/details/51190842