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 | |
---|
19 | package org.apache.hadoop.net; |
---|
20 | |
---|
21 | import java.util.*; |
---|
22 | import java.io.*; |
---|
23 | |
---|
24 | import org.apache.commons.logging.Log; |
---|
25 | import org.apache.commons.logging.LogFactory; |
---|
26 | import org.apache.hadoop.util.*; |
---|
27 | import org.apache.hadoop.util.Shell.ShellCommandExecutor; |
---|
28 | import org.apache.hadoop.conf.*; |
---|
29 | |
---|
30 | /** |
---|
31 | * This class implements the {@link DNSToSwitchMapping} interface using a |
---|
32 | * script configured via topology.script.file.name . |
---|
33 | */ |
---|
34 | public final class ScriptBasedMapping extends CachedDNSToSwitchMapping |
---|
35 | implements Configurable |
---|
36 | { |
---|
37 | public ScriptBasedMapping() { |
---|
38 | super(new RawScriptBasedMapping()); |
---|
39 | } |
---|
40 | |
---|
41 | // script must accept at least this many args |
---|
42 | static final int MIN_ALLOWABLE_ARGS = 1; |
---|
43 | |
---|
44 | static final int DEFAULT_ARG_COUNT = 100; |
---|
45 | |
---|
46 | static final String SCRIPT_FILENAME_KEY = "topology.script.file.name"; |
---|
47 | static final String SCRIPT_ARG_COUNT_KEY = "topology.script.number.args"; |
---|
48 | |
---|
49 | public ScriptBasedMapping(Configuration conf) { |
---|
50 | this(); |
---|
51 | setConf(conf); |
---|
52 | } |
---|
53 | |
---|
54 | public Configuration getConf() { |
---|
55 | return ((RawScriptBasedMapping)rawMapping).getConf(); |
---|
56 | } |
---|
57 | |
---|
58 | public void setConf(Configuration conf) { |
---|
59 | ((RawScriptBasedMapping)rawMapping).setConf(conf); |
---|
60 | } |
---|
61 | |
---|
62 | private static final class RawScriptBasedMapping |
---|
63 | implements DNSToSwitchMapping { |
---|
64 | private String scriptName; |
---|
65 | private Configuration conf; |
---|
66 | private int maxArgs; //max hostnames per call of the script |
---|
67 | private static Log LOG = |
---|
68 | LogFactory.getLog(ScriptBasedMapping.class); |
---|
69 | public void setConf (Configuration conf) { |
---|
70 | this.scriptName = conf.get(SCRIPT_FILENAME_KEY); |
---|
71 | this.maxArgs = conf.getInt(SCRIPT_ARG_COUNT_KEY, DEFAULT_ARG_COUNT); |
---|
72 | this.conf = conf; |
---|
73 | } |
---|
74 | public Configuration getConf () { |
---|
75 | return conf; |
---|
76 | } |
---|
77 | |
---|
78 | public RawScriptBasedMapping() {} |
---|
79 | |
---|
80 | public List<String> resolve(List<String> names) { |
---|
81 | List <String> m = new ArrayList<String>(names.size()); |
---|
82 | |
---|
83 | if (names.isEmpty()) { |
---|
84 | return m; |
---|
85 | } |
---|
86 | |
---|
87 | if (scriptName == null) { |
---|
88 | for (int i = 0; i < names.size(); i++) { |
---|
89 | m.add(NetworkTopology.DEFAULT_RACK); |
---|
90 | } |
---|
91 | return m; |
---|
92 | } |
---|
93 | |
---|
94 | String output = runResolveCommand(names); |
---|
95 | if (output != null) { |
---|
96 | StringTokenizer allSwitchInfo = new StringTokenizer(output); |
---|
97 | while (allSwitchInfo.hasMoreTokens()) { |
---|
98 | String switchInfo = allSwitchInfo.nextToken(); |
---|
99 | m.add(switchInfo); |
---|
100 | } |
---|
101 | |
---|
102 | if (m.size() != names.size()) { |
---|
103 | // invalid number of entries returned by the script |
---|
104 | LOG.warn("Script " + scriptName + " returned " |
---|
105 | + Integer.toString(m.size()) + " values when " |
---|
106 | + Integer.toString(names.size()) + " were expected."); |
---|
107 | return null; |
---|
108 | } |
---|
109 | } else { |
---|
110 | // an error occurred. return null to signify this. |
---|
111 | // (exn was already logged in runResolveCommand) |
---|
112 | return null; |
---|
113 | } |
---|
114 | |
---|
115 | return m; |
---|
116 | } |
---|
117 | |
---|
118 | private String runResolveCommand(List<String> args) { |
---|
119 | int loopCount = 0; |
---|
120 | if (args.size() == 0) { |
---|
121 | return null; |
---|
122 | } |
---|
123 | StringBuffer allOutput = new StringBuffer(); |
---|
124 | int numProcessed = 0; |
---|
125 | if (maxArgs < MIN_ALLOWABLE_ARGS) { |
---|
126 | LOG.warn("Invalid value " + Integer.toString(maxArgs) |
---|
127 | + " for " + SCRIPT_ARG_COUNT_KEY + "; must be >= " |
---|
128 | + Integer.toString(MIN_ALLOWABLE_ARGS)); |
---|
129 | return null; |
---|
130 | } |
---|
131 | |
---|
132 | while (numProcessed != args.size()) { |
---|
133 | int start = maxArgs * loopCount; |
---|
134 | List <String> cmdList = new ArrayList<String>(); |
---|
135 | cmdList.add(scriptName); |
---|
136 | for (numProcessed = start; numProcessed < (start + maxArgs) && |
---|
137 | numProcessed < args.size(); numProcessed++) { |
---|
138 | cmdList.add(args.get(numProcessed)); |
---|
139 | } |
---|
140 | File dir = null; |
---|
141 | String userDir; |
---|
142 | if ((userDir = System.getProperty("user.dir")) != null) { |
---|
143 | dir = new File(userDir); |
---|
144 | } |
---|
145 | ShellCommandExecutor s = new ShellCommandExecutor( |
---|
146 | cmdList.toArray(new String[0]), dir); |
---|
147 | try { |
---|
148 | s.execute(); |
---|
149 | allOutput.append(s.getOutput() + " "); |
---|
150 | } catch (Exception e) { |
---|
151 | LOG.warn(StringUtils.stringifyException(e)); |
---|
152 | return null; |
---|
153 | } |
---|
154 | loopCount++; |
---|
155 | } |
---|
156 | return allOutput.toString(); |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|