hiveserver2高可用机制解析

本文详细介绍了HiveServer2在Hive 1.1版本中的高可用实现,通过ZooKeeper进行服务发现。在服务端,HiveServer2启动时会根据配置将服务注册到ZK;客户端在连接时,若采用动态连接并通过ZK,会从指定namespace随机选取服务地址。整个过程包括服务启动、初始化、停止以及客户端的连接流程,确保了HiveServer2的HA能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hiveserver2 ha-zk机制(hive-1.1版本)

1.服务端:
1.1 hiveserve2 mainClass 启动流程
  public static void main(String[] args) {
    HiveConf.setLoadHiveServer2Config(true);
    try {
      //1.获取启动服务类型
      ServerOptionsProcessor oproc = new ServerOptionsProcessor("hiveserver2");

      //2.解析启动命令行,生成具体启动服务
      ServerOptionsProcessorResponse oprocResponse = oproc.parse(args);

      // NOTE: It is critical to do this here so that log4j is reinitialized
      // before any of the other core hive classes are loaded
      String initLog4jMessage = LogUtils.initHiveLog4j();
      LOG.debug(initLog4jMessage);
      HiveStringUtils.startupShutdownMessage(HiveServer2.class, args, LOG);

      // Log debug message from "oproc" after log4j initialize properly
      LOG.debug(oproc.getDebugMessage().toString());

      // Call the executor which will execute the appropriate command based on the parsed options

      //3.启动对应的response,并启动
      oprocResponse.getServerOptionsExecutor().execute();
    } catch (LogInitializationException e) {
      LOG.error("Error initializing log: " + e.getMessage(), e);
      System.exit(-1);
    }
  }

ServerOptionsProcessor oproc = new ServerOptionsProcessor(“hiveserver2”); 添加对应option如下所示:

 @SuppressWarnings("static-access")
    ServerOptionsProcessor(String serverName) {
      this.serverName = serverName;
      // -hiveconf x=y
      options.addOption(OptionBuilder
          .withValueSeparator()
          .hasArgs(2)
          .withArgName("property=value")
          .withLongOpt("hiveconf")
          .withDescription("Use value for given property")
          .create());
      // -deregister <versionNumber>
      options.addOption(OptionBuilder
          .hasArgs(1)
          .withArgName("versionNumber")
          .withLongOpt("deregister")
          .withDescription("Deregister all instances of given version from dynamic service discovery")
          .create());
      options.addOption(new Option("H", "help", false, "Print help information"));
    }
1.2根据启动命令行获取具体服务
    ServerOptionsProcessorResponse parse(String[] argv) {
      try {
        commandLine = new GnuParser().parse(options, argv);
        // Process --hiveconf
        // Get hiveconf param values and set the System property values
        Properties confProps = commandLine.getOptionProperties("hiveconf");
        for (String propKey : confProps.stringPropertyNames()) {
          // save logging message for log4j output latter after log4j initialize properly
          debugMessage.append("Setting " + propKey + "=" + confProps.getProperty(propKey) + ";\n");
          System.setProperty(propKey, confProps.getProperty(propKey));
        }

        // Process --help
        if (commandLine.hasOption('H')) {
          return new ServerOptionsProcessorResponse(new HelpOptionExecutor(serverName, options));
        }

        // Process --deregister
        if (commandLine.hasOption("deregister")) {
          return new ServerOptionsProcessorResponse(new DeregisterOptionExecutor(
              commandLine.getOptionValue("deregister")));
        }
      } catch (ParseException e) {
        // Error out & exit - we were not able to parse the args successfully
        System.err.println("Error starting HiveServer2 with given arguments: ");
        System.err.println(e.getMessage());
        System.exit(-1);
      }
      // Default executor, when no option is specifie
      
      //默认返回hiveserve2服务
      return new ServerOptionsProcessorResponse(new StartOptionExecutor());
    }

StartOptionExecutor代码详情

static class StartOptionExecutor implements ServerOptionsExecutor {
    @Override
    public void execute() {
      try {
        startHiveServer2();
      } catch (Throwable t) {
        LOG.fatal("Error starting HiveServer2", t);
        System.exit(-1);
      }
    }
  }

startHiveServer2()具体实现:

对应hive-site.xml配置
HIVE_SERVER2_SUPPORT_DYNAMIC_SERVICE_DISCOVERY 
对应配置:hive.server2.support.dynamic.service.discovery
HIVE_SERVER2_ZOOKEEPER_NAMESPACE
对应配置:hive.server2.zookeeper.namespace 默认hiveserver2

注: 配置信息项详见 org.apache.hadoop.hive.conf.HiveConf
  private static void startHiveServer2() throws Throwable {
    long attempts = 0, maxAttempts = 1;
    while (true) {
      LOG.info("Starting HiveServer2");
      HiveConf hiveConf = new HiveConf();
      maxAttempts = hiveConf.getLongVar(HiveConf.ConfVars.HIVE_SERVER2_MAX_START_ATTEMPTS);
      HiveServer2 server = null;
      try {
        server = new HiveServer2();
        //初始化配置
        server.init(hiveConf);
        //服务启动
        server.start();
        // If we're supporting dynamic service discovery, we'll add the service uri for this
        // HiveServer2 instance to Zookeeper as a znode.
        //如果配置动态服务,则到zk对应namesapce注册服务地址
        if (hiveConf.getBoolVar(ConfVars
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值