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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
.master
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Arrays
;
22 import java
.util
.Collections
;
23 import java
.util
.List
;
25 import org
.apache
.yetus
.audience
.InterfaceAudience
;
26 import org
.slf4j
.Logger
;
27 import org
.slf4j
.LoggerFactory
;
28 import org
.apache
.hadoop
.conf
.Configuration
;
29 import org
.apache
.hadoop
.hbase
.ServerName
;
30 import org
.apache
.hadoop
.hbase
.util
.ReflectionUtils
;
31 import org
.apache
.hadoop
.net
.DNSToSwitchMapping
;
32 import org
.apache
.hadoop
.net
.ScriptBasedMapping
;
34 * Wrapper over the rack resolution utility in Hadoop. The rack resolution
35 * utility in Hadoop does resolution from hosts to the racks they belong to.
38 @InterfaceAudience.Private
39 public class RackManager
{
40 private static final Logger LOG
= LoggerFactory
.getLogger(RackManager
.class);
41 public static final String UNKNOWN_RACK
= "Unknown Rack";
43 private DNSToSwitchMapping switchMapping
;
45 public RackManager() {
48 public RackManager(Configuration conf
) {
49 switchMapping
= ReflectionUtils
.instantiateWithCustomCtor(
50 conf
.getClass("hbase.util.ip.to.rack.determiner", ScriptBasedMapping
.class,
51 DNSToSwitchMapping
.class).getName(), new Class
<?
>[]{Configuration
.class},
56 * Get the name of the rack containing a server, according to the DNS to
58 * @param server the server for which to get the rack name
59 * @return the rack name of the server
61 public String
getRack(ServerName server
) {
65 // just a note - switchMapping caches results (at least the implementation should unless the
66 // resolution is really a lightweight process)
67 List
<String
> racks
= switchMapping
.resolve(Collections
.singletonList(server
.getHostname()));
68 if (racks
!= null && !racks
.isEmpty()) {
76 * Same as {@link #getRack(ServerName)} except that a list is passed
77 * @param servers list of servers we're requesting racks information for
78 * @return list of racks for the given list of servers
80 public List
<String
> getRack(List
<ServerName
> servers
) {
81 // just a note - switchMapping caches results (at least the implementation should unless the
82 // resolution is really a lightweight process)
83 List
<String
> serversAsString
= new ArrayList
<>(servers
.size());
84 for (ServerName server
: servers
) {
85 serversAsString
.add(server
.getHostname());
87 List
<String
> racks
= switchMapping
.resolve(serversAsString
);