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