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.Arrays;
21  import java.util.List;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.classification.InterfaceAudience;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.ServerName;
28  import org.apache.hadoop.hbase.util.ReflectionUtils;
29  import org.apache.hadoop.net.DNSToSwitchMapping;
30  import org.apache.hadoop.net.ScriptBasedMapping;
31  /**
32   * Wrapper over the rack resolution utility in Hadoop. The rack resolution
33   * utility in Hadoop does resolution from hosts to the racks they belong to.
34   *
35   */
36  @InterfaceAudience.Private
37  public class RackManager {
38    static final Log LOG = LogFactory.getLog(RackManager.class);
39    public static final String UNKNOWN_RACK = "Unknown Rack";
40  
41    private DNSToSwitchMapping switchMapping;
42  
43    public RackManager(Configuration conf) {
44      switchMapping = ReflectionUtils.instantiateWithCustomCtor(
45          conf.getClass("hbase.util.ip.to.rack.determiner", ScriptBasedMapping.class,
46               DNSToSwitchMapping.class).getName(), new Class<?>[]{Configuration.class},
47                 new Object[]{conf});
48    }
49  
50    /**
51     * Get the name of the rack containing a server, according to the DNS to
52     * switch mapping.
53     * @param server the server for which to get the rack name
54     * @return the rack name of the server
55     */
56    public String getRack(ServerName server) {
57      if (server == null) {
58        return UNKNOWN_RACK;
59      }
60      // just a note - switchMapping caches results (at least the implementation should unless the
61      // resolution is really a lightweight process)
62      List<String> racks = switchMapping.resolve(Arrays.asList(server.getHostname()));
63      if (racks != null && !racks.isEmpty()) {
64        return racks.get(0);
65      }
66  
67      return UNKNOWN_RACK;
68    }
69  }