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.ArrayList; |
---|
21 | import java.util.List; |
---|
22 | import java.util.Map; |
---|
23 | import java.util.concurrent.ConcurrentHashMap; |
---|
24 | |
---|
25 | /** |
---|
26 | * A cached implementation of DNSToSwitchMapping that takes an |
---|
27 | * raw DNSToSwitchMapping and stores the resolved network location in |
---|
28 | * a cache. The following calls to a resolved network location |
---|
29 | * will get its location from the cache. |
---|
30 | * |
---|
31 | */ |
---|
32 | public class CachedDNSToSwitchMapping implements DNSToSwitchMapping { |
---|
33 | private Map<String, String> cache = new ConcurrentHashMap<String, String>(); |
---|
34 | protected DNSToSwitchMapping rawMapping; |
---|
35 | |
---|
36 | public CachedDNSToSwitchMapping(DNSToSwitchMapping rawMapping) { |
---|
37 | this.rawMapping = rawMapping; |
---|
38 | } |
---|
39 | |
---|
40 | public List<String> resolve(List<String> names) { |
---|
41 | // normalize all input names to be in the form of IP addresses |
---|
42 | names = NetUtils.normalizeHostNames(names); |
---|
43 | |
---|
44 | List <String> result = new ArrayList<String>(names.size()); |
---|
45 | if (names.isEmpty()) { |
---|
46 | return result; |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | // find out all names without cached resolved location |
---|
51 | List<String> unCachedHosts = new ArrayList<String>(names.size()); |
---|
52 | for (String name : names) { |
---|
53 | if (cache.get(name) == null) { |
---|
54 | unCachedHosts.add(name); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | // Resolve those names |
---|
59 | List<String> rNames = rawMapping.resolve(unCachedHosts); |
---|
60 | |
---|
61 | // Cache the result |
---|
62 | if (rNames != null) { |
---|
63 | for (int i=0; i<unCachedHosts.size(); i++) { |
---|
64 | cache.put(unCachedHosts.get(i), rNames.get(i)); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | // Construct the result |
---|
69 | for (String name : names) { |
---|
70 | //now everything is in the cache |
---|
71 | String networkLocation = cache.get(name); |
---|
72 | if (networkLocation != null) { |
---|
73 | result.add(networkLocation); |
---|
74 | } else { //resolve all or nothing |
---|
75 | return null; |
---|
76 | } |
---|
77 | } |
---|
78 | return result; |
---|
79 | } |
---|
80 | } |
---|