View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master;
19  
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.ServerName;
29  import org.apache.hadoop.hbase.util.ReflectionUtils;
30  import org.apache.hadoop.net.DNSToSwitchMapping;
31  import org.apache.hadoop.net.ScriptBasedMapping;
32  /**
33   * Wrapper over the rack resolution utility in Hadoop. The rack resolution
34   * utility in Hadoop does resolution from hosts to the racks they belong to.
35   *
36   */
37  @InterfaceAudience.Private
38  public class RackManager {
39    static final Log LOG = LogFactory.getLog(RackManager.class);
40    public static final String UNKNOWN_RACK = "Unknown Rack";
41  
42    private DNSToSwitchMapping switchMapping;
43  
44    public RackManager() {
45    }
46  
47    public RackManager(Configuration conf) {
48      switchMapping = ReflectionUtils.instantiateWithCustomCtor(
49          conf.getClass("hbase.util.ip.to.rack.determiner", ScriptBasedMapping.class,
50               DNSToSwitchMapping.class).getName(), new Class<?>[]{Configuration.class},
51                 new Object[]{conf});
52    }
53  
54    /**
55     * Get the name of the rack containing a server, according to the DNS to
56     * switch mapping.
57     * @param server the server for which to get the rack name
58     * @return the rack name of the server
59     */
60    public String getRack(ServerName server) {
61      if (server == null) {
62        return UNKNOWN_RACK;
63      }
64      // just a note - switchMapping caches results (at least the implementation should unless the
65      // resolution is really a lightweight process)
66      List<String> racks = switchMapping.resolve(Arrays.asList(server.getHostname()));
67      if (racks != null && !racks.isEmpty()) {
68        return racks.get(0);
69      }
70  
71      return UNKNOWN_RACK;
72    }
73  
74    /**
75     * Same as {@link #getRack(ServerName)} except that a list is passed
76     * @param servers list of servers we're requesting racks information for
77     * @return list of racks for the given list of servers
78     */
79    public List<String> getRack(List<ServerName> servers) {
80      // just a note - switchMapping caches results (at least the implementation should unless the
81      // resolution is really a lightweight process)
82      List<String> serversAsString = new ArrayList<String>(servers.size());
83      for (ServerName server : servers) {
84        serversAsString.add(server.getHostname());
85      }
86      List<String> racks = switchMapping.resolve(serversAsString);
87      return racks;
88    }
89  }