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