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.ServerName;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.client.RegionReplicaUtil;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * Class that hold all the paths of znode for HBase.
032 */
033@InterfaceAudience.Private
034public class ZNodePaths {
035  // TODO: Replace this with ZooKeeper constant when ZOOKEEPER-277 is resolved.
036  public static final char ZNODE_PATH_SEPARATOR = '/';
037
038  public static final String META_ZNODE_PREFIX_CONF_KEY = "zookeeper.znode.metaserver";
039  public static final String META_ZNODE_PREFIX = "meta-region-server";
040  private static final String DEFAULT_SNAPSHOT_CLEANUP_ZNODE = "snapshot-cleanup";
041
042  // base znode for this cluster
043  public final String baseZNode;
044
045  /**
046   * The prefix of meta znode. Does not include baseZNode. Its a 'prefix' because meta replica id
047   * integer can be tagged on the end (if 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 =
117      joinZNode(replicationZNode, 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().append("ZNodePaths [baseZNode=").append(baseZNode)
125      .append(", rsZNode=").append(rsZNode).append(", drainingZNode=").append(drainingZNode)
126      .append(", masterAddressZNode=").append(masterAddressZNode)
127      .append(", backupMasterAddressesZNode=").append(backupMasterAddressesZNode)
128      .append(", clusterStateZNode=").append(clusterStateZNode).append(", tableZNode=")
129      .append(tableZNode).append(", clusterIdZNode=").append(clusterIdZNode)
130      .append(", splitLogZNode=").append(splitLogZNode).append(", balancerZNode=")
131      .append(balancerZNode).append(", regionNormalizerZNode=").append(regionNormalizerZNode)
132      .append(", switchZNode=").append(switchZNode).append(", namespaceZNode=")
133      .append(namespaceZNode).append(", masterMaintZNode=").append(masterMaintZNode)
134      .append(", replicationZNode=").append(replicationZNode).append(", peersZNode=")
135      .append(peersZNode).append(", queuesZNode=").append(queuesZNode).append(", hfileRefsZNode=")
136      .append(hfileRefsZNode).append(", snapshotCleanupZNode=").append(snapshotCleanupZNode)
137      .append("]").toString();
138  }
139
140  /** Returns the znode string corresponding to a replicaId */
141  public String getZNodeForReplica(int replicaId) {
142    if (RegionReplicaUtil.isDefaultReplica(replicaId)) {
143      return joinZNode(baseZNode, metaZNodePrefix);
144    } else {
145      return joinZNode(baseZNode, metaZNodePrefix + "-" + replicaId);
146    }
147  }
148
149  /**
150   * Parses the meta replicaId from the passed path.
151   * @param path the name of the full path which includes baseZNode. n
152   */
153  public int getMetaReplicaIdFromPath(String path) {
154    // Extract the znode from path. The prefix is of the following format.
155    // baseZNode + PATH_SEPARATOR.
156    int prefixLen = baseZNode.length() + 1;
157    return getMetaReplicaIdFromZNode(path.substring(prefixLen));
158  }
159
160  /**
161   * Parse the meta replicaId from the passed znode
162   * @param znode the name of the znode, does not include baseZNode n
163   */
164  public int getMetaReplicaIdFromZNode(String znode) {
165    return znode.equals(metaZNodePrefix)
166      ? RegionInfo.DEFAULT_REPLICA_ID
167      : Integer.parseInt(znode.substring(metaZNodePrefix.length() + 1));
168  }
169
170  /** Returns True if meta znode. */
171  public boolean isMetaZNodePrefix(String znode) {
172    return znode != null && znode.startsWith(this.metaZNodePrefix);
173  }
174
175  /** Returns True is the fully qualified path is for meta location */
176  public boolean isMetaZNodePath(String path) {
177    int prefixLen = baseZNode.length() + 1;
178    return path.length() > prefixLen && isMetaZNodePrefix(path.substring(prefixLen));
179  }
180
181  /**
182   * Returns whether the path is supposed to be readable by the client and DOES NOT contain
183   * sensitive information (world readable).
184   */
185  public boolean isClientReadable(String path) {
186    // Developer notice: These znodes are world readable. DO NOT add more znodes here UNLESS
187    // all clients need to access this data to work. Using zk for sharing data to clients (other
188    // than service lookup case is not a recommended design pattern.
189    return path.equals(baseZNode) || isMetaZNodePath(path) || path.equals(masterAddressZNode)
190      || path.equals(clusterIdZNode) || path.equals(rsZNode) ||
191      // /hbase/table and /hbase/table/foo is allowed, /hbase/table-lock is not
192      path.equals(tableZNode) || path.startsWith(tableZNode + "/");
193  }
194
195  public String getRsPath(ServerName sn) {
196    return joinZNode(rsZNode, sn.toString());
197  }
198
199  /**
200   * Join the prefix znode name with the suffix znode name to generate a proper full znode name.
201   * <p>
202   * Assumes prefix does not end with slash and suffix does not begin with it.
203   * @param prefix beginning of znode name
204   * @param suffix ending of znode name
205   * @return result of properly joining prefix with suffix
206   */
207  public static String joinZNode(String prefix, String suffix) {
208    return prefix + ZNodePaths.ZNODE_PATH_SEPARATOR + suffix;
209  }
210}