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 static org.apache.hadoop.hbase.HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT; 021import static org.apache.hadoop.hbase.HConstants.SPLIT_LOGDIR_NAME; 022import static org.apache.hadoop.hbase.HConstants.ZOOKEEPER_ZNODE_PARENT; 023 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.client.RegionInfo; 026import org.apache.hadoop.hbase.client.RegionReplicaUtil; 027import org.apache.yetus.audience.InterfaceAudience; 028 029/** 030 * Class that hold all the paths of znode for HBase. 031 */ 032@InterfaceAudience.Private 033public class ZNodePaths { 034 // TODO: Replace this with ZooKeeper constant when ZOOKEEPER-277 is resolved. 035 public static final char ZNODE_PATH_SEPARATOR = '/'; 036 037 public static final String META_ZNODE_PREFIX_CONF_KEY = "zookeeper.znode.metaserver"; 038 public static final String META_ZNODE_PREFIX = "meta-region-server"; 039 private static final String DEFAULT_SNAPSHOT_CLEANUP_ZNODE = "snapshot-cleanup"; 040 041 // base znode for this cluster 042 public final String baseZNode; 043 044 /** 045 * The prefix of meta znode. Does not include baseZNode. 046 * Its a 'prefix' because meta replica id integer can be tagged on the end (if 047 * no number present, it is 'default' replica). 048 */ 049 private final String metaZNodePrefix; 050 051 // znode containing ephemeral nodes of the regionservers 052 public final String rsZNode; 053 // znode containing ephemeral nodes of the draining regionservers 054 public final String drainingZNode; 055 // znode of currently active master 056 public final String masterAddressZNode; 057 // znode of this master in backup master directory, if not the active master 058 public final String backupMasterAddressesZNode; 059 // znode containing the current cluster state 060 public final String clusterStateZNode; 061 // znode used for table disabling/enabling 062 // Still used in hbase2 by MirroringTableStateManager; it mirrors internal table state out to 063 // zookeeper for hbase1 clients to make use of. If no hbase1 clients disable. See 064 // MirroringTableStateManager. To be removed in hbase3. 065 @Deprecated 066 public final String tableZNode; 067 // znode containing the unique cluster ID 068 public final String clusterIdZNode; 069 // znode used for log splitting work assignment 070 public final String splitLogZNode; 071 // znode containing the state of the load balancer 072 public final String balancerZNode; 073 // znode containing the state of region normalizer 074 public final String regionNormalizerZNode; 075 // znode containing the state of all switches, currently there are split and merge child node. 076 public final String switchZNode; 077 // znode containing namespace descriptors 078 public final String namespaceZNode; 079 // znode of indicating master maintenance mode 080 public final String masterMaintZNode; 081 082 // znode containing all replication state. 083 public final String replicationZNode; 084 // znode containing a list of all remote slave (i.e. peer) clusters. 085 public final String peersZNode; 086 // znode containing all replication queues 087 public final String queuesZNode; 088 // znode containing queues of hfile references to be replicated 089 public final String hfileRefsZNode; 090 // znode containing the state of the snapshot auto-cleanup 091 final String snapshotCleanupZNode; 092 093 public ZNodePaths(Configuration conf) { 094 baseZNode = conf.get(ZOOKEEPER_ZNODE_PARENT, DEFAULT_ZOOKEEPER_ZNODE_PARENT); 095 metaZNodePrefix = conf.get(META_ZNODE_PREFIX_CONF_KEY, META_ZNODE_PREFIX); 096 rsZNode = joinZNode(baseZNode, conf.get("zookeeper.znode.rs", "rs")); 097 drainingZNode = joinZNode(baseZNode, conf.get("zookeeper.znode.draining.rs", "draining")); 098 masterAddressZNode = joinZNode(baseZNode, conf.get("zookeeper.znode.master", "master")); 099 backupMasterAddressesZNode = 100 joinZNode(baseZNode, conf.get("zookeeper.znode.backup.masters", "backup-masters")); 101 clusterStateZNode = joinZNode(baseZNode, conf.get("zookeeper.znode.state", "running")); 102 tableZNode = joinZNode(baseZNode, conf.get("zookeeper.znode.tableEnableDisable", "table")); 103 clusterIdZNode = joinZNode(baseZNode, conf.get("zookeeper.znode.clusterId", "hbaseid")); 104 splitLogZNode = joinZNode(baseZNode, conf.get("zookeeper.znode.splitlog", SPLIT_LOGDIR_NAME)); 105 balancerZNode = joinZNode(baseZNode, conf.get("zookeeper.znode.balancer", "balancer")); 106 regionNormalizerZNode = 107 joinZNode(baseZNode, conf.get("zookeeper.znode.regionNormalizer", "normalizer")); 108 switchZNode = joinZNode(baseZNode, conf.get("zookeeper.znode.switch", "switch")); 109 namespaceZNode = joinZNode(baseZNode, conf.get("zookeeper.znode.namespace", "namespace")); 110 masterMaintZNode = 111 joinZNode(baseZNode, conf.get("zookeeper.znode.masterMaintenance", "master-maintenance")); 112 replicationZNode = joinZNode(baseZNode, conf.get("zookeeper.znode.replication", "replication")); 113 peersZNode = 114 joinZNode(replicationZNode, conf.get("zookeeper.znode.replication.peers", "peers")); 115 queuesZNode = joinZNode(replicationZNode, conf.get("zookeeper.znode.replication.rs", "rs")); 116 hfileRefsZNode = joinZNode(replicationZNode, 117 conf.get("zookeeper.znode.replication.hfile.refs", "hfile-refs")); 118 snapshotCleanupZNode = joinZNode(baseZNode, 119 conf.get("zookeeper.znode.snapshot.cleanup", DEFAULT_SNAPSHOT_CLEANUP_ZNODE)); 120 } 121 122 @Override 123 public String toString() { 124 return new StringBuilder() 125 .append("ZNodePaths [baseZNode=").append(baseZNode) 126 .append(", rsZNode=").append(rsZNode) 127 .append(", drainingZNode=").append(drainingZNode) 128 .append(", masterAddressZNode=").append(masterAddressZNode) 129 .append(", backupMasterAddressesZNode=").append(backupMasterAddressesZNode) 130 .append(", clusterStateZNode=").append(clusterStateZNode) 131 .append(", tableZNode=").append(tableZNode) 132 .append(", clusterIdZNode=").append(clusterIdZNode) 133 .append(", splitLogZNode=").append(splitLogZNode) 134 .append(", balancerZNode=").append(balancerZNode) 135 .append(", regionNormalizerZNode=").append(regionNormalizerZNode) 136 .append(", switchZNode=").append(switchZNode) 137 .append(", namespaceZNode=").append(namespaceZNode) 138 .append(", masterMaintZNode=").append(masterMaintZNode) 139 .append(", replicationZNode=").append(replicationZNode) 140 .append(", peersZNode=").append(peersZNode) 141 .append(", queuesZNode=").append(queuesZNode) 142 .append(", hfileRefsZNode=").append(hfileRefsZNode) 143 .append(", snapshotCleanupZNode=").append(snapshotCleanupZNode) 144 .append("]").toString(); 145 } 146 147 /** 148 * @return the znode string corresponding to a replicaId 149 */ 150 public String getZNodeForReplica(int replicaId) { 151 if (RegionReplicaUtil.isDefaultReplica(replicaId)) { 152 return joinZNode(baseZNode, metaZNodePrefix); 153 } else { 154 return joinZNode(baseZNode, metaZNodePrefix + "-" + replicaId); 155 } 156 } 157 158 /** 159 * Parses the meta replicaId from the passed path. 160 * @param path the name of the full path which includes baseZNode. 161 * @return replicaId 162 */ 163 public int getMetaReplicaIdFromPath(String path) { 164 // Extract the znode from path. The prefix is of the following format. 165 // baseZNode + PATH_SEPARATOR. 166 int prefixLen = baseZNode.length() + 1; 167 return getMetaReplicaIdFromZNode(path.substring(prefixLen)); 168 } 169 170 /** 171 * Parse the meta replicaId from the passed znode 172 * @param znode the name of the znode, does not include baseZNode 173 * @return replicaId 174 */ 175 public int getMetaReplicaIdFromZNode(String znode) { 176 return znode.equals(metaZNodePrefix)? 177 RegionInfo.DEFAULT_REPLICA_ID: 178 Integer.parseInt(znode.substring(metaZNodePrefix.length() + 1)); 179 } 180 181 /** 182 * @return True if meta znode. 183 */ 184 public boolean isMetaZNodePrefix(String znode) { 185 return znode != null && znode.startsWith(this.metaZNodePrefix); 186 } 187 188 /** 189 * @return True is the fully qualified path is for meta location 190 */ 191 public boolean isMetaZNodePath(String path) { 192 int prefixLen = baseZNode.length() + 1; 193 return path.length() > prefixLen && isMetaZNodePrefix(path.substring(prefixLen)); 194 } 195 196 /** 197 * Returns whether the path is supposed to be readable by the client and DOES NOT contain 198 * sensitive information (world readable). 199 */ 200 public boolean isClientReadable(String path) { 201 // Developer notice: These znodes are world readable. DO NOT add more znodes here UNLESS 202 // all clients need to access this data to work. Using zk for sharing data to clients (other 203 // than service lookup case is not a recommended design pattern. 204 return path.equals(baseZNode) || isMetaZNodePath(path) || path.equals(masterAddressZNode) || 205 path.equals(clusterIdZNode) || path.equals(rsZNode) || 206 // /hbase/table and /hbase/table/foo is allowed, /hbase/table-lock is not 207 path.equals(tableZNode) || path.startsWith(tableZNode + "/"); 208 } 209 210 /** 211 * Join the prefix znode name with the suffix znode name to generate a proper full znode name. 212 * <p> 213 * Assumes prefix does not end with slash and suffix does not begin with it. 214 * @param prefix beginning of znode name 215 * @param suffix ending of znode name 216 * @return result of properly joining prefix with suffix 217 */ 218 public static String joinZNode(String prefix, String suffix) { 219 return prefix + ZNodePaths.ZNODE_PATH_SEPARATOR + suffix; 220 } 221}