您的位置:首页 > 运维架构 > Apache

RM源码之org.apache.hadoop.service.AbstractService分析

2015-08-19 14:05 736 查看
org.apache.hadoop.service.AbstractService父类为org.apache.hadoop.service.Service

其中Service主要变量和方法为:





状态改变枚举


/** Constructed but not initialized */
NOTINITED(0, "NOTINITED"),

/** Initialized but not started or stopped */
INITED(1, "INITED"),

/** started and not stopped */
STARTED(2, "STARTED"),

/** stopped. No further state transitions are permitted */
STOPPED(3, "STOPPED");



Service中主要是服务通用一些功能

1.初始化

2.服务状态

3.生命周期记录

4.状态改变

5.监听器

---服务器可以处于状态---

=====================================================================

org.apache.hadoop.service.AbstractService实现了Service

主要功能三个方面如下:

1.状态改变

2.状态改变,启动相应监听

3.记录状态改变事件

4.Blocker操作

----------------------------

初始化过程

1.初始化过程是由NOTINITED->INITED状态过程

1.1状态改变是通过一个二维数据来维护状态之间切换是否有效

2.实际初始化过程是由serviceInit来实现,应该会有子类来实现具体初始化过程

3.当从NOTINITED->INITED切换完成会唤醒监听器

4.如果有状态改变失败将退出

5.记录状态改变

1.初始化过程是由NOTINITED->INITED状态过程

1.1状态间切换关系


private static final boolean[][] statemap =
{
// uninited inited started stopped
/* uninited */ {false, true, false, true},
/* inited */ {false, true, true, true},
/* started */ {false, false, true, true},
/* stopped */ {false, false, false, true},
};



具体初始化流程,还是上图吧,方便一些。










public void start() {
if (isInState(STATE.STARTED)) {
return;
}
//enter the started state
synchronized (stateChangeLock) {
if (stateModel.enterState(STATE.STARTED) != STATE.STARTED) {
try {
startTime = System.currentTimeMillis();
serviceStart();
if (isInState(STATE.STARTED)) {
//if the service started (and isn't now in a later state), notify
if (LOG.isDebugEnabled()) {
LOG.debug("Service " + getName() + " is started");
}
notifyListeners();
}
} catch (Exception e) {
noteFailure(e);
ServiceOperations.stopQuietly(LOG, this);
throw ServiceStateException.convert(e);
}
}
}
}



@Override
public void stop() {
if (isInState(STATE.STOPPED)) {
return;
}
synchronized (stateChangeLock) {
if (enterState(STATE.STOPPED) != STATE.STOPPED) {
try {
serviceStop();
} catch (Exception e) {
//stop-time exceptions are logged if they are the first one,
noteFailure(e);
throw ServiceStateException.convert(e);
} finally {
//report that the service has terminated
terminationNotification.set(true);
synchronized (terminationNotification) {
terminationNotification.notifyAll();
}
//notify anything listening for events
notifyListeners();
}
} else {
//already stopped: note it
if (LOG.isDebugEnabled()) {
LOG.debug("Ignoring re-entrant call to stop()");
}
}
}
}


基本来状态改变都是这个样子,没有太多变化,都是先去检查,是否可以改变,可以改变执行由子类实现的方法。

-------------------------------------------

记录状态改变





唤醒Listener


/**
* Notify local and global listeners of state changes.
* Exceptions raised by listeners are NOT passed up.
*/
private void notifyListeners() {
try {
listeners.notifyListeners(this);
globalListeners.notifyListeners(this);
} catch (Throwable e) {
LOG.warn("Exception while notifying listeners of " + this + ": " + e,
e);
}
}




/**
* List of state change listeners; it is final to ensure
* that it will never be null.
*/
private final ServiceOperations.ServiceListeners listeners
= new ServiceOperations.ServiceListeners();
/**
* Static listeners to all events across all services
*/
private static ServiceOperations.ServiceListeners globalListeners
= new ServiceOperations.ServiceListeners();

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: