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.net; |
---|
19 | |
---|
20 | import java.util.*; |
---|
21 | |
---|
22 | import org.apache.hadoop.conf.Configuration; |
---|
23 | import org.apache.hadoop.conf.Configured; |
---|
24 | |
---|
25 | /** |
---|
26 | * Implements the {@link DNSToSwitchMapping} via static mappings. Used |
---|
27 | * in testcases that simulate racks. |
---|
28 | * |
---|
29 | */ |
---|
30 | public class StaticMapping extends Configured implements DNSToSwitchMapping { |
---|
31 | public void setconf(Configuration conf) { |
---|
32 | String[] mappings = conf.getStrings("hadoop.configured.node.mapping"); |
---|
33 | if (mappings != null) { |
---|
34 | for (int i = 0; i < mappings.length; i++) { |
---|
35 | String str = mappings[i]; |
---|
36 | String host = str.substring(0, str.indexOf('=')); |
---|
37 | String rack = str.substring(str.indexOf('=') + 1); |
---|
38 | addNodeToRack(host, rack); |
---|
39 | } |
---|
40 | } |
---|
41 | } |
---|
42 | /* Only one instance per JVM */ |
---|
43 | private static Map<String, String> nameToRackMap = new HashMap<String, String>(); |
---|
44 | |
---|
45 | static synchronized public void addNodeToRack(String name, String rackId) { |
---|
46 | nameToRackMap.put(name, rackId); |
---|
47 | } |
---|
48 | public List<String> resolve(List<String> names) { |
---|
49 | List<String> m = new ArrayList<String>(); |
---|
50 | synchronized (nameToRackMap) { |
---|
51 | for (String name : names) { |
---|
52 | String rackId; |
---|
53 | if ((rackId = nameToRackMap.get(name)) != null) { |
---|
54 | m.add(rackId); |
---|
55 | } else { |
---|
56 | m.add(NetworkTopology.DEFAULT_RACK); |
---|
57 | } |
---|
58 | } |
---|
59 | return m; |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|