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