设为首页 收藏本站
查看: 306|回复: 0

[经验分享] flume服务管理实现源码分析

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-3-12 08:09:02 | 显示全部楼层 |阅读模式
flume可以监控并管理组件的运行状态,在组件关闭的时候可以自动拉起来,原理是通过启动一个计划任务线程池(monitorService ,线程的最大数量为30),运行监控线程(MonitorRunnable线程),每隔3s判断组件(包括Channel,SinkRunner)的状态是否符合要求(可用的状态由两种START和STOP),根据不同的要求调用对应组件不同的方法,START会调用start方法,STOP会调用stop方法,如果想监控一个组件的状态,只需对这个组件调用supervise方法即可,如果想停止监控一个组件,只需对这个组件调用unsupervise方法即可,同时有一个线程每隔两小时移除已经不再监控(调用了unsupervise方法)的组件的检查任务。

这个功能主要是通过org.apache.flume.lifecycle.LifecycleSupervisor实现
在org.apache.flume.node.Application类的构造函数中会初始化LifecycleSupervisor类的对象:
1
2
3
4
  public Application(List<LifecycleAware> components) {
    this. components = components;
    supervisor = new LifecycleSupervisor();
  }



flume进程启动时调用
1
2
3
4
5
6
7
org.apache.flume.node.Application.main--->org.apache.flume.node.Application.start
  public synchronized void start() { //start方法会对每一个组件调用LifecycleSupervisor.supervise方法,参数为组件,AlwaysRestartPolicy和START状态(即期望的状态为START)
    for(LifecycleAware component : components) {
      supervisor.supervise(component,
          new SupervisorPolicy.AlwaysRestartPolicy(), LifecycleState.START);
    }
  }



分析LifecycleSupervisor类:
org.apache.flume.lifecycle.LifecycleSupervisor实现了LifecycleAware接口,本身也有一个生命周期的概念(提供start/stop等方法)

其定义了几个重要的内部类:
1.MonitorRunnable,实现了Runnable接口的线程类
1)3个属性ScheduledExecutorService monitorService,LifecycleAware lifecycleAware,Supervisoree supervisoree;
2)主要的run方法分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    public void run() {
      long now = System.currentTimeMillis();
      try {
        if (supervisoree.status.firstSeen == null) {
          logger.debug("first time seeing {}", lifecycleAware);
          supervisoree.status.firstSeen = now; //第一次开始运行时,设置firstSeen为System.currentTimeMillis()
        }
        supervisoree.status.lastSeen = now; //设置lastSeen为now
        synchronized (lifecycleAware) {
          if (supervisoree.status.discard) { //如果Status的discard或者error的值为true,会直接退出
...
            return;
          } else if (supervisoree.status.error) {
...
            return;
          }
          supervisoree.status.lastSeenState = lifecycleAware.getLifecycleState(); //设置lastSeenState的值
          if (!lifecycleAware.getLifecycleState().equals(
              supervisoree.status.desiredState)) { //如果获取的lifecycleAware对象状态不是想设置的desiredState状态
...
            switch (supervisoree.status.desiredState) { //根据设置的desiredState状态调用lifecycleAware的不同方法,desiredState的值只有两种START和STOP
              case START:
                try {
                  lifecycleAware.start(); //状态为START时设置运行start方法
                } catch (Throwable e) {
...
                  supervisoree.status.failures++; //start方法异常时failures的值加1
                }
                break;
              case STOP:
                try {
                  lifecycleAware.stop(); //状态为STOP时设置运行stop方法
                } catch (Throwable e) {
...
                  supervisoree.status.failures++; //stop方法异常时failures的值加1
                }
                break;
              default:
...
            }
            if (!supervisoree.policy.isValid(lifecycleAware, supervisoree.status)) {
               //调用SupervisorPolicy的isValid方法,比如OnceOnlyPolicy 的isValid的方法会判断Status.failures 的值,如果为0则返回true,否则返回false
              logger.error(
                  "Policy {} of {} has been violated - supervisor should exit!",
                  supervisoree.policy, lifecycleAware);
            }
          }
        }
      } catch(Throwable t) {
...      
      }
...
    }



2.Purger,实现了Runnable接口的线程类
run方法:

1
2
3
4
5
6
    public void run() {
      if(needToPurge){  //如果needToPurge设置为true
        monitorService.purge(); //ScheduledThreadPoolExecutor.purge方法用于从工作队列中删除已经cancel的java.util.concurrent.Future对象(释放队列空间)
        needToPurge = false ; //并设置needToPurge为false
      }
    }



3.Status内部类定义了几个状态属性,代表了Supervisoree的状态

1
2
3
4
5
6
7
    public Long firstSeen;
    public Long lastSeen;
    public LifecycleState lastSeenState;
    public LifecycleState desiredState;
    public int failures ;
    public boolean discard ;
    public volatile boolean error ;



4. SupervisorPolicy 是抽象类,定义了抽象方法isValid(LifecycleAware object, Status status),包含两个扩展类AlwaysRestartPolicy 和OnceOnlyPolicy
AlwaysRestartPolicy 的isValid会一直返回true,OnceOnlyPolicy 的isValid的方法会判断Status.failures 的值,如果为0则返回true,否则返回false

5.Supervisoree包含SupervisorPolicy 和Status属性

主要的方法分析:
在构造方法中初始化几个重要的属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
  public LifecycleSupervisor() {
    lifecycleState = LifecycleState.IDLE;
    supervisedProcesses = new HashMap<LifecycleAware, Supervisoree>(); // supervisedProcesses 用于存放LifecycleAware和Supervisoree对象的键值对,代表已经管理的组件
    monitorFutures = new HashMap<LifecycleAware, ScheduledFuture<?>>(); //monitorFutures 用于存放LifecycleAware对象和ScheduledFuture对象的键值对
    monitorService = new ScheduledThreadPoolExecutor(10,
        new ThreadFactoryBuilder().setNameFormat(
            "lifecycleSupervisor-" + Thread.currentThread().getId() + "-%d")
            .build()); // monitorService 用于调用Purger线程,定时移除线程池中已经cancel的task
    monitorService.setMaximumPoolSize(20);
    monitorService.setKeepAliveTime(30, TimeUnit.SECONDS);
    purger = new Purger();
    needToPurge = false; // 初始时为false,在有task cancel的时候设置为true
  }



  
start方法用于启动检测线程池:

1
2
3
4
5
6
  public synchronized void start() {
....
    monitorService.scheduleWithFixedDelay( purger, 2, 2, TimeUnit. HOURS); //在两小时后每隔两小时运行一次Purger,释放线程池的工作队列
    lifecycleState = LifecycleState. START; //设置状态为START
...
  }




stop方法首先关闭线程池,然后关闭各个组件
1)线程池关闭

1
monitorService.shutdown();



2)各组件关闭

1
2
3
4
5
6
7
   for ( final Entry<LifecycleAware, Supervisoree> entry : supervisedProcesses
        .entrySet()) { //遍历supervisedProcesses中的各个组件
      if (entry.getKey(). getLifecycleState().equals(LifecycleState.START)) { //如果组件的当前状态是START,则首先设置其需要变成的状态为STOP,并调用组件的stop方法
        entry.getValue(). status. desiredState = LifecycleState.STOP;
        entry.getKey().stop();
      }
    }




supervise方法用于监控对应的组件,有3个参数LifecycleAware lifecycleAware, SupervisorPolicy policy, LifecycleState desiredState

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public synchronized void supervise(LifecycleAware lifecycleAware,
      SupervisorPolicy policy, LifecycleState desiredState) {
    if(this. monitorService.isShutdown()
        || this.monitorService .isTerminated()
        || this.monitorService .isTerminating()){ //检测监控线程池是否正常
      throw new FlumeException("Supervise called on " + lifecycleAware + " " +
          "after shutdown has been initiated. " + lifecycleAware + " will not" +
          " be started");
    }
    Preconditions.checkState(!supervisedProcesses .containsKey(lifecycleAware),
        "Refusing to supervise " + lifecycleAware + " more than once" ); //检测是否已经管理
.....
    Supervisoree process = new Supervisoree(); //初始化Supervisoree对象
    process.status = new Status(); //并实例化Supervisoree对象的Status属性
    process.policy = policy; //设置Supervisoree的属性
    process.status.desiredState = desiredState;
    process.status.error = false;
    MonitorRunnable monitorRunnable = new MonitorRunnable(); //初始化一个MonitorRunnable 对象(线程),并设置对象的属性
    monitorRunnable.lifecycleAware = lifecycleAware;
    monitorRunnable.supervisoree = process;
    monitorRunnable.monitorService = monitorService;
    supervisedProcesses.put(lifecycleAware, process); //向supervisedProcesses中插入键值对,代表已经开始管理的组件
    ScheduledFuture<?> future = monitorService.scheduleWithFixedDelay(
        monitorRunnable, 0, 3, TimeUnit. SECONDS); // 设置计划任务线程池,每隔3s之后运行monitorRunnable
    monitorFutures.put(lifecycleAware, future); // 向monitorFutures中插入键值对
  }




unsupervise方法用于停止组件并从监控容器中去除:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    synchronized (lifecycleAware) {
    Supervisoree supervisoree = supervisedProcesses.get(lifecycleAware); //从已经管理的Supervisoree  hashmap中获取Supervisoree对象
    supervisoree.status.discard = true;  //设置Supervisoree对象的Status属性的discard 值为discard
      this.setDesiredState(lifecycleAware, LifecycleState.STOP);
      //调用setDesiredState方法,设置Supervisoree对象的Status属性的desiredState 值为STOP(supervisoree.status.desiredState = desiredState)
      logger.info("Stopping component: {}", lifecycleAware);
      lifecycleAware.stop(); //调用组件的stop方法
    }
    supervisedProcesses.remove(lifecycleAware); //从supervisedProcesses hashmap中移除这个组件
    monitorFutures.get(lifecycleAware).cancel(false);
    //调用组件对应的ScheduledFuture的cancel方法取消任务(A Future represents the result of an asynchronous computation.
,cancel :Attempts to cancel execution of this task.)
    needToPurge = true; //设置needToPurge 的属性为true,这样就可以在purge中删除已经cancel的ScheduledFuture对象
    monitorFutures.remove(lifecycleAware);



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-45793-1-1.html 上篇帖子: 卸载和安装LINUX上的JDK 下篇帖子: flume自动reload配置的源码分析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表