HBase--HMaster的启动流程

  本篇文章主要来看一看HMaster的启动流程,直接进入主题吧。
ps:这些文章都是从我先记录到我为知笔记然后有空了就写成博客的,mac的为知笔记图片保存不是很方便,这篇文章的图片通过截图过来的上传后发现有点模糊,只能将就看了。

HMaster加载配置

  • org.apache.hadoop.hbase.master.HMaster 调用类中的main方法
  • 在main方法中主要做了
  1. 打印启动日志,
  2. 调用HMasterCommandLine对象doMain()方法
    a). 日志打打印
    hbase source日志打打印

b). 调用HMasterCommandLine对象doMain()方法
HMasterCommandLine extends ServerCommandLine而HMasterCommandLine没有重写doMain()方法,即调用了o.a.h.h.u.ServerCommandLine接口中的doMain()方法
hbase HMasterCommandLine
doMain()方法,采用了Hadoop的ToolRunner机制

c). 先来看看,org.apache.hadoop.hbase.HBaseConfiguration的create()方法,从字面意思就知道是在创建配置文件
hbase HBaseConfiguration的create

d). 加载:core-default.xml,core-site.xml,hbase-default.xml,hbase-sute.xml
hbase 加载配置

  • 加载配置看完了之后,回退回来继续看
    ToolRunner.run(HBaseConfiguration.create(), this, args);
    即:
    org.apache.hadoop.util.ToolRunner.run,
    如下:
    将conf和args封装成GenericOptionsParser对象parser, 根据parser获取toolArgs
    返回tool.run(toolArgs);
    tool.run(toolArgs)也即HMasterComandLine.run(toolArgs)
    hbase HMasterComandLine.run

  • org.apache.hadoop.hbase.master.HMasterComandLine.run(toolArgs),重点在下面的startMaster()私有方法
    hbase startMaster

  • 重点来看看:org.apache.hadoop.hbase.master.HMasterCommandLine.startMaster()
    由于代码过长我就不截图了
    从注释就知道会有两种模式:本地模式和分布式模式。
    1)本地模式local,master,regionserver,ZK都在同一个JVM中;
    2)分布式模式distributed,仅只会启动一个HMaster对象。
    代码里面大部分就是做一些配置处理,zk地址端口等,重点关注

  1. local本地模式:ZK启动MiniZooKeeperCluster.startup(zkDataPath);
  2. local本地模式:hmser,regionserver启动LocalHBaseCluster.startup();
  3. distributed分布式模式:HMaster启动,HMaster.start()。
    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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    private int startMaster() {
    Configuration conf = getConf();
    TraceUtil.initTracer(conf);

    try {
    // If 'local', defer to LocalHBaseCluster instance. Starts master
    // and regionserver both in the one JVM.
    if (LocalHBaseCluster.isLocal(conf)) {
    DefaultMetricsSystem.setMiniClusterMode(true);
    final MiniZooKeeperCluster zooKeeperCluster = new MiniZooKeeperCluster(conf);
    File zkDataPath = new File(conf.get(HConstants.ZOOKEEPER_DATA_DIR));

    // find out the default client port
    int zkClientPort = 0;

    // If the zookeeper client port is specified in server quorum, use it.
    String zkserver = conf.get(HConstants.ZOOKEEPER_QUORUM);
    if (zkserver != null) {
    String[] zkservers = zkserver.split(",");

    if (zkservers.length > 1) {
    // In local mode deployment, we have the master + a region server and zookeeper server
    // started in the same process. Therefore, we only support one zookeeper server.
    String errorMsg = "Could not start ZK with " + zkservers.length +
    " ZK servers in local mode deployment. Aborting as clients (e.g. shell) will not "
    + "be able to find this ZK quorum.";
    System.err.println(errorMsg);
    throw new IOException(errorMsg);
    }

    String[] parts = zkservers[0].split(":");

    if (parts.length == 2) {
    // the second part is the client port
    zkClientPort = Integer.parseInt(parts [1]);
    }
    }
    // If the client port could not be find in server quorum conf, try another conf
    if (zkClientPort == 0) {
    zkClientPort = conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, 0);
    // The client port has to be set by now; if not, throw exception.
    if (zkClientPort == 0) {
    throw new IOException("No config value for " + HConstants.ZOOKEEPER_CLIENT_PORT);
    }
    }
    zooKeeperCluster.setDefaultClientPort(zkClientPort);
    // set the ZK tick time if specified
    int zkTickTime = conf.getInt(HConstants.ZOOKEEPER_TICK_TIME, 0);
    if (zkTickTime > 0) {
    zooKeeperCluster.setTickTime(zkTickTime);
    }

    // login the zookeeper server principal (if using security)
    ZKUtil.loginServer(conf, HConstants.ZK_SERVER_KEYTAB_FILE,
    HConstants.ZK_SERVER_KERBEROS_PRINCIPAL, null);
    int localZKClusterSessionTimeout =
    conf.getInt(HConstants.ZK_SESSION_TIMEOUT + ".localHBaseCluster", 10*1000);
    conf.setInt(HConstants.ZK_SESSION_TIMEOUT, localZKClusterSessionTimeout);
    LOG.info("Starting a zookeeper cluster");
    int clientPort = zooKeeperCluster.startup(zkDataPath);
    if (clientPort != zkClientPort) {
    String errorMsg = "Could not start ZK at requested port of " +
    zkClientPort + ". ZK was started at port: " + clientPort +
    ". Aborting as clients (e.g. shell) will not be able to find " +
    "this ZK quorum.";
    System.err.println(errorMsg);
    throw new IOException(errorMsg);
    }
    conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, Integer.toString(clientPort));

    // Need to have the zk cluster shutdown when master is shutdown.
    // Run a subclass that does the zk cluster shutdown on its way out.
    int mastersCount = conf.getInt("hbase.masters", 1);
    int regionServersCount = conf.getInt("hbase.regionservers", 1);
    // Set start timeout to 5 minutes for cmd line start operations
    conf.setIfUnset("hbase.master.start.timeout.localHBaseCluster", "300000");
    LOG.info("Starting up instance of localHBaseCluster; master=" + mastersCount +
    ", regionserversCount=" + regionServersCount);
    LocalHBaseCluster cluster = new LocalHBaseCluster(conf, mastersCount, regionServersCount,
    LocalHMaster.class, HRegionServer.class);
    ((LocalHMaster)cluster.getMaster(0)).setZKCluster(zooKeeperCluster);
    cluster.startup();
    waitOnMasterThreads(cluster);
    } else {
    logProcessInfo(getConf());
    HMaster master = HMaster.constructMaster(masterClass, conf);
    if (master.isStopped()) {
    LOG.info("Won't bring the Master up as a shutdown is requested");
    return 1;
    }
    master.start();
    master.join();
    if(master.isAborted())
    throw new RuntimeException("HMaster Aborted");
    }
    } catch (Throwable t) {
    LOG.error("Master exiting", t);
    return 1;
    }
    return 0;
    }

ZK启动

ZK启动MiniZooKeeperCluster.startup(zkDataPath);
hbase ZK启动MiniZooKeeperCluster

zk的启动,主要逻辑在org.apache.zookeeper.server.NIOServerCnxnFactory
hbase NIOServerCnxnFactory

local HBase的启动(Master,RegionServer)以线程的方式
初始化一个LocalHBaseCluster,
org.apache.hadoop.hbase.LocalHBaseCluster
hbase LocalHBaseCluster

org.apache.hadoop.hbase.LocalHBaseCluster 实例化,可以看到下面调用了addMaster()、addRegionServer()
hbase LocalHBaseCluster

创建master线程

  • addMaster(),调用
    JVMClusterUtil.createMasterThread创建master线程
    hbase createMasterThread创建master线程

  • 创建master线程
    hbase 创建master线程

  • hmc 即 org.apache.hadoop.hbase.master.HMasterCommandLine$LocalHMaster 对象
    LocalHMaster extends HMaster
    Master extends HRegionServer implements MasterServices
    构造函数主要做了几件事情:1.初始化相关配置 2.初始化rpcServer 3.初始化zk监控类,下面看看一些具体的细节
    1)在HRegionServer中会做初始化配置,检查
    hbase 在HRegionServer中会做初始化配置

2)在HRegionServer中创建MasterRpcService
hbase 在HRegionServer中创建MasterRpcService
hbase 在HRegionServer中创建MasterRpcService

3)在HRegionServer中创建initializeFileSystem();
hbase 在HRegionServer中创建initializeFileSystem

4)在HRegionServer中初始化zk监控类,启动相关服务
hbase 在HRegionServer中初始化zk监控类

启动regionServer类似

hbase 在HRegionServer中初始化zk监控类
hbase 在HRegionServer中初始化zk监控类

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器