Hadoop Common源码分析之服务Service
Service是定义Hadoop中服务生命周期的一个接口。Service内部定义了服务的状态及生命周期,在服务被构造后,其一个生命周期内的状态为NOTINITED未初始化--INITED已初始化--已启动STARTED--已停止STOPPED,而这一生命周期内服务状态的变化,是随着如下方法链的调用而变化的:init()--start()--stop(),服务构造后整体方法调用及状态转移如下图所示:
下面,我们来看下Service的源码分析,先看下其内部定义的服务状态,如下:
/** * Service states * 服务状态 */ public enum STATE { /** 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"); /** * An integer value for use in array lookup and JMX interfaces. * Although {@link Enum#ordinal()} could do this, explicitly * identify the numbers gives more stability guarantees over time. */ private final int value; /** * A name of the state that can be used in messages */ private final String statename; private STATE(int value, String name) { this.value = value; this.statename = name; } /** * Get the integer value of a state * @return the numeric value of the state */ public int getValue() { return value; } /** * Get the name of a state * @return the state's name */ @Override public String toString() { return statename; } }STATE是一个枚举类,代表了服务生命周期内各阶段的状态,分别为:
1、NOTINITED--0:服务实例已经构造但是还没有初始化;
2、INITED--1:服务实例已经初始化但还没有启动或停止;
3、STARTED--2:服务实例已经启动但没有停止;
4、STOPPED--3:服务实例已经停止,不允许进一步的状态转换。
接下来,我们再看下导致服务状态变更的各个方法,如下:
1、初始化服务init(): NOTINITED-->INITED/STOPPED
/** * Initialize the service. * 初始化服务 * * The transition MUST be from {@link STATE#NOTINITED} to {@link STATE#INITED} * unless the operation failed and an exception was raised, in which case * {@link #stop()} MUST be invoked and the service enter the state * {@link STATE#STOPPED}. * * 初始化后,服务状态由NOTINITED转换到INITED,除非异常发生导致服务初始化操作失败, * 在这种情况下,stop()方法应该被调用,并且服务状态进入STOPPED。 * * @param config the configuration of the service * @throws RuntimeException on any failure during the operation初始化后,服务状态由NOTINITED转换到INITED,除非异常发生导致服务初始化操作失败,在这种情况下,stop()方法应该被调用,并且服务状态进入STOPPED。
2、启动服务start():INITED-->STARTED/STOPPED
/** * Start the service. * 启动服务 * * The transition MUST be from {@link STATE#INITED} to {@link STATE#STARTED} * unless the operation failed and an exception was raised, in which case * {@link #stop()} MUST be invoked and the service enter the state * {@link STATE#STOPPED}. * * 服务启动后,服务状态由INITED转换到STARTED,除非异常发生导致服务启动操作失败, * 在这种情况下,stop()方法应该被调用,并且服务状态进入STOPPED。 * * @throws RuntimeException on any failure during the operation */服务启动后,服务状态由INITED转换到STARTED,除非异常发生导致服务启动操作失败,在这种情况下,stop()方法应该被调用,并且服务状态进入STOPPED。
3、停止服务stop():NOTINITED/INITED/STARTED-->STOPPED
/** * Stop the service. This MUST be a no-op if the service is already * in the {@link STATE#STOPPED} state. It SHOULD be a best-effort attempt * to stop all parts of the service. * 停止服务。如果服务已经处于STOPPED状态,这必须是一个空操作。它应该尽力试图停止服务的所有部分。 * * The implementation must be designed to complete regardless of the service * state, including the initialized/uninitialized state of all its internal * fields. * 实现者必须被设计为完成这个操作,而不管服务的状态,包括其内部所有字段初始化/未初始化的状态。 * * @throws RuntimeException on any failure during the stop operation */ void stop();停止服务。如果服务已经处于STOPPED状态,这必须是一个空操作。它应该尽力试图停止服务的所有部分。实现者必须被设计为完成这个操作,而不管服务的状态,包括其内部所有字段初始化/未初始化的状态。
为了兼顾Java7闭包条款,Service接口还提供了close()方法,实际上就是调用stop()方法,代码如下:
/** * A version of stop() that is designed to be usable in Java7 closure * clauses. * 适用于Java7闭包条款的stop()版本,实际上就是调用stop()方法。 * * Implementation classes MUST relay this directly to {@link #stop()} * 实现类必须直接传递给stop()方法。 * * @throws IOException never * @throws RuntimeException on any failure during the stop operation */ void close() throws IOException;Service的另外一大块内容,就是对于服务状态变更时间的监听,并提供了注册监听器与注销监听器的方法,如下:
1、注册监听器registerServiceListener()
/** * Register a listener to the service state change events. * If the supplied listener is already listening to this service, * this method is a no-op. * 注册一个监听器到服务状态变更事件。 * 如果提供的监听器已经监听此服务,这种方法是一个空操作。 * @param listener a new listener */ void registerServiceListener(ServiceStateChangeListener listener);
2、注销监听器unregisterServiceListener()
/** * Unregister a previously registered listener of the service state * change events. No-op if the listener is already unregistered. * 注销之前的一个注册到服务状态变更事件的监听器。 * 如果监听器已经注销,这种方法是一个空操作。 * @param listener the listener to unregister. */ void unregisterServiceListener(ServiceStateChangeListener listener);还有,Service还提供了获取服务失败时发生的第一个异常和当时状态的方法,如下:
/** * Get the first exception raised during the service failure. If null, * no exception was logged * 获取服务失败时发生的第一个异常 * @return the failure logged during a transition to the stopped state */ Throwable getFailureCause(); /** * Get the state in which the failure in {@link #getFailureCause()} occurred. * 获取服务失败发生的第一个异常时的服务状态 * @return the state or null if there was no failure */ STATE getFailureState();既然是一个服务,Service还提供了阻塞等待服务停止的waitForServiceToStop()方法,如下:
/** * Block waiting for the service to stop; uses the termination notification * object to do so. * 阻塞,等待服务停止;通过终止通知对象实现。 * * This method will only return after all the service stop actions * have been executed (to success or failure), or the timeout elapsed * This method can be called before the service is inited or started; this is * to eliminate any race condition with the service stopping before * this event occurs. * @param timeout timeout in milliseconds. A value of zero means "forever" * @return true iff the service stopped in the time period */ boolean waitForServiceToStop(long timeout);其它诸如获取服务名、获取当前服务状态、获取服务启动时间、查询是否服务是在一个特定的状态、获取生命周期历史的一个快照、获取远程依赖服务的正在停止服务的阻塞者等方法不再一一介绍,读者可自行查询,下面只把代码贴出:
/** * Get the name of this service. * 获取服务名 * @return the service name */ String getName(); /** * Get the configuration of this service. * 获取服务的配置信息 * This is normally not a clone and may be manipulated, though there are no * guarantees as to what the consequences of such actions may be * @return the current configuration, unless a specific implentation chooses * otherwise. */ Configuration getConfig(); /** * Get the current service state * 获取当前服务状态 * @return the state of the service */ STATE getServiceState(); /** * Get the service start time * 获取服务启动时间 * @return the start time of the service. This will be zero if the service * has not yet been started. */ long getStartTime(); /** * Query to see if the service is in a specific state. * 查询是否服务是在一个特定的状态 * In a multi-threaded system, the state may not hold for very long. * 在多线程系统中,服务状态可能不会保持很长时间。 * @param state the expected state * @return true if, at the time of invocation, the service was in that state. */ boolean isInState(STATE state); /** * Get a snapshot of the lifecycle history; it is a static list * 获取生命周期历史的一个快照,它是一个静态列表 * @return a possibly empty but never null list of lifecycle events. */ public List<LifecycleEvent> getLifecycleHistory(); /** * Get the blockers on a service -remote dependencies * that are stopping the service from being <i>live</i>. * * 获取远程依赖服务的正在停止服务的阻塞者 * * @return a (snapshotted) map of blocker name->description values */ public Map<String, String> getBlockers();
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
MapReduce源码分析之JobSplitWriter
JobSplitWriter被作业客户端用于写分片相关文件,包括分片数据文件job.split和分片元数据信息文件job.splitmetainfo。它有两个静态成员变量,如下: // 分片版本,当前默认为1 private static final int splitVersion = JobSplit.META_SPLIT_VERSION; // 分片文件头部,为UTF-8格式的字符串"SPL"的字节数组"SPL" private static final byte[] SPLIT_FILE_HEADER; 并且,提供了一个静态方法,完成SPLIT_FILE_HEADER的初始化,代码如下: // 静态方法,加载SPLIT_FILE_HEADER为UTF-8格式的字符串"SPL"的字节数组byte[] static { try { SPLIT_FILE_HEADER = "SPL".getBytes("UTF-8"); } catch (UnsupportedEncodingException u) { throw new RuntimeException(u); } } ...
- 下一篇
MapReduce实例浅析
1.MapReduce概述 Hadoop Map/Reduce是一个使用简易的软件框架,基于它写出来的应用程序能够运行在由上千个商用机器组成的大型集群上,并以一种可靠容错的方式并行处理上T级别的数据集。 一个Map/Reduce作业(job)通常会把输入的数据集切分为若干独立的数据块,由map任务(task)以完全并行的方式处理它们。框架会对map的输出先进行排序, 然后把结果输入给reduce任务。通常作业的输入和输出都会被存储在文件系统中。 整个框架负责任务的调度和监控,以及重新执行已经失败的任务。 通常,Map/Reduce框架和分布式文件系统是运行在一组相同的节点上的,也就是说,计算节点和存储节点通常在一起。这种配置允许框架在那些已经存好数据的节点上高效地调度任务,这可以使整个集群的网络带宽被非常高效地利用。 Map/Reduce框架由一个单独的masterJobTracker和每个集群节点一个slaveTaskTracker共同组成。master负责调度构成一个作业的所有任务,这些任务分布在不同的slave上,master监控它们的执行,重新执行已经失败的任务。而slave仅...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS6,CentOS7官方镜像安装Oracle11G
- Red5直播服务器,属于Java语言的直播服务器
- Hadoop3单机部署,实现最简伪集群
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS6,7,8上安装Nginx,支持https2.0的开启