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.master;
019
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.List;
023
024import org.apache.yetus.audience.InterfaceAudience;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.util.ReflectionUtils;
030import org.apache.hadoop.net.DNSToSwitchMapping;
031import org.apache.hadoop.net.ScriptBasedMapping;
032/**
033 * Wrapper over the rack resolution utility in Hadoop. The rack resolution
034 * utility in Hadoop does resolution from hosts to the racks they belong to.
035 *
036 */
037@InterfaceAudience.Private
038public class RackManager {
039  private static final Logger LOG = LoggerFactory.getLogger(RackManager.class);
040  public static final String UNKNOWN_RACK = "Unknown Rack";
041
042  private DNSToSwitchMapping switchMapping;
043
044  public RackManager() {
045  }
046
047  public RackManager(Configuration conf) {
048    switchMapping = ReflectionUtils.instantiateWithCustomCtor(
049        conf.getClass("hbase.util.ip.to.rack.determiner", ScriptBasedMapping.class,
050             DNSToSwitchMapping.class).getName(), new Class<?>[]{Configuration.class},
051               new Object[]{conf});
052  }
053
054  /**
055   * Get the name of the rack containing a server, according to the DNS to
056   * switch mapping.
057   * @param server the server for which to get the rack name
058   * @return the rack name of the server
059   */
060  public String getRack(ServerName server) {
061    if (server == null) {
062      return UNKNOWN_RACK;
063    }
064    // just a note - switchMapping caches results (at least the implementation should unless the
065    // resolution is really a lightweight process)
066    List<String> racks = switchMapping.resolve(Collections.singletonList(server.getHostname()));
067    if (racks != null && !racks.isEmpty()) {
068      return racks.get(0);
069    }
070
071    return UNKNOWN_RACK;
072  }
073
074  /**
075   * Same as {@link #getRack(ServerName)} except that a list is passed
076   * @param servers list of servers we're requesting racks information for
077   * @return list of racks for the given list of servers
078   */
079  public List<String> getRack(List<ServerName> servers) {
080    // just a note - switchMapping caches results (at least the implementation should unless the
081    // resolution is really a lightweight process)
082    List<String> serversAsString = new ArrayList<>(servers.size());
083    for (ServerName server : servers) {
084      serversAsString.add(server.getHostname());
085    }
086    List<String> racks = switchMapping.resolve(serversAsString);
087    return racks;
088  }
089}