Hbase 执行事件(EventHandler 、 ExcutorService 、Excutor)

先拿HMaster启动来说, 会根据serverName初始化一个 ExcutorService  ,然后通过  ExcutorService.startExecutorService  会跟ExcutorType 去初始化 Excutor , Excutor 里面会初始化一个 ThreadPoolExecutor  。  这样来了一个Event (比如Disable Table),然后指定哪个EventHandler实现类去 process() ,然后从ExcutorService 找到处理EventType的Excutor,然后 Exutor 中的ThreadPoolExecutor  去执行EventHandler实现类这个线程。

Hbase 执行事件(EventHandler 、 ExcutorService 、Excutor)_第1张图片


下面是详细介绍:


EventHandler  是一个Hbase所有事件的一个抽象类,所有子类都应该实现process方法,如果有必要,子类需要在自己的构造函数中去检查表是否存在、是否disable等等。

EventHandler中定义EventType 即事件类型,事件类型主要有如下几大类

    // Messages originating from RS (NOTE: there is NO direct communication from
    // RS to Master). These are a result of RS updates into ZK.
      RS 更新到 ZK的事件
 //RS_ZK_REGION_CLOSING    (1),   // It is replaced by M_ZK_REGION_CLOSING(HBASE-4739)
    RS_ZK_REGION_CLOSED       (2),   // RS has finished closing a region  RS完成关闭region
    RS_ZK_REGION_OPENING      (3),   // RS is in process of opening a region  RS正在打个一个region
    RS_ZK_REGION_OPENED       (4),   // RS has finished opening a region  RS已经打开了一个region
    RS_ZK_REGION_SPLITTING    (5),   // RS has started a region split RS现在正在执行split
    RS_ZK_REGION_SPLIT        (6),   // RS split has completed.  RS执行split已经完毕
    RS_ZK_REGION_FAILED_OPEN  (7),   // RS failed to open a region  RS 打开region失败

    // Messages originating from Master to RS
    Master 要求 RS的事件
    M_RS_OPEN_REGION          (20),  // Master asking RS to open a region     Master要求RS打开或关闭一个region、root 、meta
    M_RS_OPEN_ROOT            (21),  // Master asking RS to open root 
    M_RS_OPEN_META            (22),  // Master asking RS to open meta
    M_RS_CLOSE_REGION         (23),  // Master asking RS to close a region
    M_RS_CLOSE_ROOT           (24),  // Master asking RS to close root
    M_RS_CLOSE_META           (25),  // Master asking RS to close meta

    // Messages originating from Client to Master
    客户端要求Master的事件
    C_M_DELETE_TABLE          (40),   // Client asking Master to delete a table    客户要求增、删、改、查、disable、enable表及增改删列族
    C_M_DISABLE_TABLE         (41),   // Client asking Master to disable a table
    C_M_ENABLE_TABLE          (42),   // Client asking Master to enable a table
    C_M_MODIFY_TABLE          (43),   // Client asking Master to modify a table
    C_M_ADD_FAMILY            (44),   // Client asking Master to add family to table
    C_M_DELETE_FAMILY         (45),   // Client asking Master to delete family of table
    C_M_MODIFY_FAMILY         (46),   // Client asking Master to modify family of table
    C_M_CREATE_TABLE          (47),   // Client asking Master to create a table

    // Updates from master to ZK. This is done by the master and there is
    // nothing to process by either Master or RS
    M_ZK_REGION_OFFLINE       (50),  // Master adds this region as offline in ZK
    M_ZK_REGION_CLOSING       (51),  // Master adds this region as closing in ZK

    // Master controlled events to be executed on the master
    M_SERVER_SHUTDOWN         (70),  // Master is processing shutdown of a RS
    M_META_SERVER_SHUTDOWN    (72),  // Master is processing shutdown of RS hosting a meta region (-ROOT- or .META.).

    // WARNING: Please do not insert, remove or swap any line in this enum.
    // RegionTransitionData.write() uses eventType.ordinal() that is the enum index
    // and not the value specified in the enum definition. so we can't add stuff in the middle.
    C_M_SNAPSHOT_TABLE        (48),   // Client asking Master to snapshot an offline table
    C_M_RESTORE_SNAPSHOT      (49);   // Client asking Master to snapshot an offline table

Eventhandler中定义了EventHandlerListener接口,可以通过实现改接口的beforeProcess(EventHandler event)  和afterProcess(EventHandler event)以便在事件前后添加监听。

以下是所有实现eventHandler的子类,给个实现类的process为真正的实现

Hbase 执行事件(EventHandler 、 ExcutorService 、Excutor)_第2张图片


由于EventHandler是实现Runnable接口的所以可以将它交给Excutor处理,Excute内部可以实现ThreadPoolExcutor,然后再submit执行具体的EventHandler,执行过程执行状态监控有ExecutorStatus获取信息。

在Excutor作为内部类上面封装为ExcutorService 供外部调用,并对EventHandler.eventType提交到不同的Excutor,总共有如下几种ExcutorType:

 // Master executor services   Master执行
    MASTER_CLOSE_REGION        (1),
    MASTER_OPEN_REGION         (2),
    MASTER_SERVER_OPERATIONS   (3),
    MASTER_TABLE_OPERATIONS    (4),
    MASTER_RS_SHUTDOWN         (5),
    MASTER_META_SERVER_OPERATIONS (6),

    // RegionServer executor services    RS执行
    RS_OPEN_REGION             (20),
    RS_OPEN_ROOT               (21),
    RS_OPEN_META               (22),
    RS_CLOSE_REGION            (23),
    RS_CLOSE_ROOT              (24),
    RS_CLOSE_META              (25);
将eventHandler.eventType映射到对应的ExcutorType上。

下面以创建表为例:

excutorservice 提交一个CreateTableHandler,根据CreateTableHandler中的EventType 确定ExcutorType,从而决定那个Excutor去执行。


部分代码如下:

Hmater中创建表会调用

 this.executorService.submit(new CreateTableHandler(this,
      this.fileSystemManager, this.serverManager, hTableDescriptor, conf,
      newRegions, catalogTracker, assignmentManager));
上面已经说过CreateTableHandler是EventHandler的子类,

再看一下怎么跟Evenet.Type关联的:

  public CreateTableHandler(Server server, MasterFileSystem fileSystemManager,
    ServerManager serverManager, HTableDescriptor hTableDescriptor,
    Configuration conf, HRegionInfo [] newRegions,
    CatalogTracker catalogTracker, AssignmentManager assignmentManager)
    throws NotAllMetaRegionsOnlineException, TableExistsException,
    IOException {
    super(server, EventType.C_M_CREATE_TABLE);

PS:ServerName toString格式: host,port,startcode。startcode为初始化的系统时间

你可能感兴趣的:(hbase)