001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.zookeeper; 019 020import java.io.File; 021import java.io.IOException; 022import java.io.PrintWriter; 023import java.net.InetAddress; 024import java.net.NetworkInterface; 025import java.nio.charset.StandardCharsets; 026import java.util.ArrayList; 027import java.util.Enumeration; 028import java.util.List; 029import java.util.Map.Entry; 030import java.util.Properties; 031import org.apache.hadoop.conf.Configuration; 032import org.apache.hadoop.hbase.HBaseConfiguration; 033import org.apache.hadoop.hbase.HBaseInterfaceAudience; 034import org.apache.hadoop.hbase.HConstants; 035import org.apache.hadoop.hbase.util.DNS; 036import org.apache.hadoop.hbase.util.Strings; 037import org.apache.hadoop.util.StringUtils; 038import org.apache.yetus.audience.InterfaceAudience; 039import org.apache.yetus.audience.InterfaceStability; 040import org.apache.zookeeper.server.DatadirCleanupManager; 041import org.apache.zookeeper.server.ServerConfig; 042import org.apache.zookeeper.server.ZooKeeperServerMain; 043import org.apache.zookeeper.server.admin.AdminServer; 044import org.apache.zookeeper.server.quorum.QuorumPeerConfig; 045import org.apache.zookeeper.server.quorum.QuorumPeerMain; 046import org.slf4j.Logger; 047import org.slf4j.LoggerFactory; 048 049/** 050 * HBase's version of ZooKeeper's QuorumPeer. When HBase is set to manage ZooKeeper, this class is 051 * used to start up QuorumPeer instances. By doing things in here rather than directly calling to 052 * ZooKeeper, we have more control over the process. This class uses {@link ZKConfig} to get 053 * settings from the hbase-site.xml file. 054 */ 055@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS) 056@InterfaceStability.Evolving 057public final class HQuorumPeer { 058 private static final Logger LOG = LoggerFactory.getLogger(HQuorumPeer.class); 059 060 private HQuorumPeer() { 061 } 062 063 /** 064 * Parse ZooKeeper configuration from HBase XML config and run a QuorumPeer. 065 * @param args String[] of command line arguments. Not used. 066 */ 067 public static void main(String[] args) { 068 Configuration conf = HBaseConfiguration.create(); 069 try { 070 Properties zkProperties = ZKConfig.makeZKProps(conf); 071 writeMyID(zkProperties); 072 QuorumPeerConfig zkConfig = new QuorumPeerConfig(); 073 zkConfig.parseProperties(zkProperties); 074 075 // login the zookeeper server principal (if using security) 076 ZKAuthentication.loginServer(conf, HConstants.ZK_SERVER_KEYTAB_FILE, 077 HConstants.ZK_SERVER_KERBEROS_PRINCIPAL, zkConfig.getClientPortAddress().getHostName()); 078 079 runZKServer(zkConfig); 080 } catch (Exception e) { 081 LOG.error("Failed to start ZKServer", e); 082 System.exit(-1); 083 } 084 } 085 086 private static void runZKServer(QuorumPeerConfig zkConfig) 087 throws IOException, AdminServer.AdminServerException { 088 089 /** 090 * Start and schedule the purge task autopurge.purgeInterval is 0 by default,so in fact the 091 * DatadirCleanupManager task will not be started to clean the logs by default. Config is 092 * recommended only for standalone server. 093 */ 094 095 DatadirCleanupManager purgeMgr = new DatadirCleanupManager(zkConfig.getDataDir(), 096 zkConfig.getDataLogDir(), zkConfig.getSnapRetainCount(), zkConfig.getPurgeInterval()); 097 purgeMgr.start(); 098 099 if (zkConfig.isDistributed()) { 100 QuorumPeerMain qp = new QuorumPeerMain(); 101 qp.runFromConfig(zkConfig); 102 } else { 103 ZooKeeperServerMain zk = new ZooKeeperServerMain(); 104 ServerConfig serverConfig = new ServerConfig(); 105 serverConfig.readFrom(zkConfig); 106 zk.runFromConfig(serverConfig); 107 } 108 } 109 110 private static boolean addressIsLocalHost(String address) { 111 return address.equals("localhost") || address.equals("127.0.0.1"); 112 } 113 114 static void writeMyID(Properties properties) throws IOException { 115 long myId = -1; 116 117 Configuration conf = HBaseConfiguration.create(); 118 String myAddress = Strings.domainNamePointerToHostName( 119 DNS.getDefaultHost(conf.get("hbase.zookeeper.dns.interface", "default"), 120 conf.get("hbase.zookeeper.dns.nameserver", "default"))); 121 122 List<String> ips = new ArrayList<>(); 123 124 // Add what could be the best (configured) match 125 ips.add(myAddress.contains(".") ? myAddress : StringUtils.simpleHostname(myAddress)); 126 127 // For all nics get all hostnames and IPs 128 Enumeration<?> nics = NetworkInterface.getNetworkInterfaces(); 129 while (nics.hasMoreElements()) { 130 Enumeration<?> rawAdrs = ((NetworkInterface) nics.nextElement()).getInetAddresses(); 131 while (rawAdrs.hasMoreElements()) { 132 InetAddress inet = (InetAddress) rawAdrs.nextElement(); 133 ips.add(StringUtils.simpleHostname(inet.getHostName())); 134 ips.add(inet.getHostAddress()); 135 } 136 } 137 138 for (Entry<Object, Object> entry : properties.entrySet()) { 139 String key = entry.getKey().toString().trim(); 140 String value = entry.getValue().toString().trim(); 141 if (key.startsWith("server.")) { 142 int dot = key.indexOf('.'); 143 long id = Long.parseLong(key.substring(dot + 1)); 144 String[] parts = value.split(":"); 145 String address = parts[0]; 146 if (addressIsLocalHost(address) || ips.contains(address)) { 147 myId = id; 148 break; 149 } 150 } 151 } 152 153 // Set the max session timeout from the provided client-side timeout 154 properties.setProperty("maxSessionTimeout", conf.get(HConstants.ZK_SESSION_TIMEOUT, 155 Integer.toString(HConstants.DEFAULT_ZK_SESSION_TIMEOUT))); 156 157 if (myId == -1) { 158 throw new IOException( 159 "Could not find my address: " + myAddress + " in list of ZooKeeper quorum servers"); 160 } 161 162 String dataDirStr = properties.get("dataDir").toString().trim(); 163 File dataDir = new File(dataDirStr); 164 if (!dataDir.isDirectory()) { 165 if (!dataDir.mkdirs()) { 166 throw new IOException("Unable to create data dir " + dataDir); 167 } 168 } 169 170 File myIdFile = new File(dataDir, "myid"); 171 PrintWriter w = new PrintWriter(myIdFile, StandardCharsets.UTF_8.name()); 172 w.println(myId); 173 w.close(); 174 } 175}