1 |
package appl.parallel.client; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.rmi.RemoteException; |
5 |
import java.util.Vector; |
6 |
|
7 |
import org.apache.log4j.LogManager; |
8 |
import org.apache.log4j.Logger; |
9 |
|
10 |
import schmitzm.lang.AbstractNamedObject; |
11 |
import appl.ext.XuluConfig; |
12 |
import appl.parallel.ComputingResourceContainer; |
13 |
import appl.parallel.model.ParallelStepModel; |
14 |
import appl.parallel.server.XuluServer; |
15 |
import appl.parallel.services.GlobalDiscoveryService; |
16 |
import appl.parallel.spmd.AdvancedSPMDClientController; |
17 |
import appl.parallel.spmd.SPMDClientController; |
18 |
import appl.parallel.spmd.SPMDClientInterface; |
19 |
import appl.parallel.spmd.split.SplitMap; |
20 |
import appl.parallel.util.Helper; |
21 |
|
22 |
import com.sun.jini.tool.ClassServer; |
23 |
|
24 |
import edu.bonn.xulu.XuluModellingPlatform; |
25 |
import edu.bonn.xulu.appl.XuluPlugin; |
26 |
|
27 |
/** |
28 |
* This plugin is responsible that all services required for remote |
29 |
* execution are properly started. It first starts the |
30 |
* {@link GlobalDiscoveryService}. Then it uses the following entries of |
31 |
* {@link XuluConfig} for configuration: <br> |
32 |
* <br> |
33 |
* <code>RemoteExecutionController.spmd.start</code> - if, <code>true</code> |
34 |
* the {@link ClientDataServer} is started. <br><br> |
35 |
* <code>RemoteExecutionController.http.start</code> - if, <code>true</code> |
36 |
* the {@link ClassServer HTTP-Class server} is started. <br><br> |
37 |
* <code>RemoteExecutionController.startLocalXuluServer</code> - if, |
38 |
* <code>true</code> a local {@link XuluServer} is started. <br>. |
39 |
* |
40 |
* @author Dominik Appl |
41 |
*/ |
42 |
public class RemoteExecutionController extends AbstractNamedObject implements |
43 |
XuluPlugin { |
44 |
|
45 |
private final Logger LOG = LogManager.getLogger(this.getClass().getName()); |
46 |
|
47 |
private ClientDataServer spmdClient; |
48 |
|
49 |
private boolean started = false; |
50 |
|
51 |
private XuluServer localServer; |
52 |
|
53 |
private ClassServer httpServer; |
54 |
|
55 |
private RemoteEventHandler eventHandlerProxy; |
56 |
|
57 |
private GlobalDiscoveryService discoveryService; |
58 |
|
59 |
private boolean startHTTP = false; |
60 |
|
61 |
private boolean startSPMD = false; |
62 |
|
63 |
private boolean startXuluServer = false; |
64 |
|
65 |
private boolean httpVerbose = false; |
66 |
|
67 |
private int httpPort = 80; |
68 |
|
69 |
private String httpBaseDir = ".\\classes"; //default: will be overwritten with the configuration |
70 |
|
71 |
private Vector<ResourceChangeListener> activeChangeListeners = new Vector<ResourceChangeListener>(); |
72 |
|
73 |
private Vector<ComputingResourceContainer> lastKnownResources; |
74 |
|
75 |
public RemoteExecutionController() { |
76 |
startDiscovery(); |
77 |
} |
78 |
|
79 |
/** |
80 |
* Starts the Plugin |
81 |
* |
82 |
* @see edu.bonn.xulu.appl.XuluPlugin#execute(edu.bonn.xulu.XuluModellingPlatform) |
83 |
*/ |
84 |
public void execute(XuluModellingPlatform appl) { |
85 |
started = true; |
86 |
loadConfig(); |
87 |
startEventSystem(); |
88 |
startSPMDClient(); |
89 |
startXuluServer(); |
90 |
startDiscovery(); |
91 |
startHTTPServer(); |
92 |
//discover own server |
93 |
refreshResources(); |
94 |
} |
95 |
|
96 |
private void startEventSystem() { |
97 |
// start EventProxy |
98 |
try { |
99 |
eventHandlerProxy = new RemoteEventHandler(); |
100 |
eventHandlerProxy.startService(); |
101 |
} catch (RemoteException e1) { |
102 |
// TODO Auto-generated catch block |
103 |
e1.printStackTrace(); |
104 |
} |
105 |
} |
106 |
|
107 |
private void startDiscovery() { |
108 |
if (discoveryService != null) |
109 |
discoveryService.stopService(); |
110 |
discoveryService = new GlobalDiscoveryService(); |
111 |
} |
112 |
|
113 |
/** |
114 |
* Load the configuration form the {@link XuluConfig} |
115 |
*/ |
116 |
private void loadConfig() { |
117 |
XuluConfig config = XuluConfig.getXuluConfig(); |
118 |
startSPMD = config |
119 |
.getBooleanProperty("RemoteExecutionController.spmd.start"); |
120 |
startXuluServer = config |
121 |
.getBooleanProperty("RemoteExecutionController.startlocalxuluserver"); |
122 |
startHTTP = config |
123 |
.getBooleanProperty("RemoteExecutionController.http.start"); |
124 |
httpVerbose = config |
125 |
.getBooleanProperty("RemoteExecutionController.http.verbose"); |
126 |
if (config.getIntProperty("RemoteExecutionController.http.port") != 0) |
127 |
httpPort = config |
128 |
.getIntProperty("RemoteExecutionController.http.port"); |
129 |
if (config.getProperty("RemoteExecutionController.http.basedir") != null) |
130 |
httpBaseDir = config |
131 |
.getProperty("RemoteExecutionController.http.basedir"); |
132 |
} |
133 |
|
134 |
private void startHTTPServer() { |
135 |
// start the http server |
136 |
if (startHTTP) { |
137 |
try { |
138 |
httpServer = new ClassServer(httpPort, httpBaseDir, true, |
139 |
httpVerbose); |
140 |
httpServer.start(); |
141 |
} catch (IOException e) { |
142 |
LOG.error("Could not create HTTP-Server!", e); |
143 |
e.printStackTrace(); |
144 |
} |
145 |
} |
146 |
} |
147 |
|
148 |
private void startSPMDClient() { |
149 |
try { |
150 |
if (startSPMD) |
151 |
spmdClient = new ClientDataServer(eventHandlerProxy); |
152 |
} catch (RemoteException e) { |
153 |
LOG.error("SPMD Client could NOT be started. " + e.getMessage()); |
154 |
e.printStackTrace(); |
155 |
} |
156 |
} |
157 |
|
158 |
private void startXuluServer() { |
159 |
try { |
160 |
if (startXuluServer) |
161 |
localServer = new XuluServer(false, spmdClient); |
162 |
} catch (RemoteException e) { |
163 |
LOG.error("XuluServer could NOT be started. " + e.getMessage()); |
164 |
e.printStackTrace(); |
165 |
} |
166 |
} |
167 |
|
168 |
/* |
169 |
* (non-Javadoc) |
170 |
* |
171 |
* @see edu.bonn.xulu.appl.XuluPlugin#isStarted() |
172 |
*/ |
173 |
public boolean isStarted() { |
174 |
return started; |
175 |
} |
176 |
|
177 |
/* |
178 |
* (non-Javadoc) |
179 |
* |
180 |
* @see edu.bonn.xulu.appl.XuluPlugin#isVisible() |
181 |
*/ |
182 |
public boolean isVisible() { |
183 |
// TODO Auto-generated method stub |
184 |
return false; |
185 |
} |
186 |
|
187 |
/* |
188 |
* (non-Javadoc) |
189 |
* |
190 |
* @see edu.bonn.xulu.appl.XuluPlugin#setVisible(boolean) |
191 |
*/ |
192 |
public void setVisible(boolean visible) { |
193 |
// TODO Auto-generated method stub |
194 |
|
195 |
} |
196 |
|
197 |
/* |
198 |
* (non-Javadoc) |
199 |
* |
200 |
* @see edu.bonn.xulu.appl.XuluPlugin#stop() |
201 |
*/ |
202 |
public void stop() { |
203 |
started = false; |
204 |
if (httpServer != null) |
205 |
httpServer.stop(); |
206 |
if (spmdClient != null) |
207 |
spmdClient.close(); |
208 |
spmdClient = null; |
209 |
if (localServer != null) |
210 |
localServer.stopServer(); |
211 |
spmdClient = null; |
212 |
eventHandlerProxy.stopService(); |
213 |
} |
214 |
|
215 |
/** |
216 |
* Gets active Resources and triggers the discovery-process. After |
217 |
* the resources are found an update is called on all registered |
218 |
* {@link ResourceChangeListener}s. |
219 |
* * |
220 |
*/ |
221 |
public void refreshResources() { |
222 |
Vector<ComputingResourceContainer> remoteResources = discoveryService |
223 |
.getRemoteResources(); |
224 |
|
225 |
lastKnownResources = remoteResources; |
226 |
//notify listeners about the resource update |
227 |
for (ResourceChangeListener listener : activeChangeListeners) { |
228 |
listener.updateResources(remoteResources); |
229 |
} |
230 |
|
231 |
// The advanced Server still makes some problems and therefore is here |
232 |
// deactivated. May be fixed later. |
233 |
// try { |
234 |
// if(localServer!=null){ |
235 |
// ComputingResourceProperties localInfo = |
236 |
// localServer.getResourceInformation(); |
237 |
// localInfo.setProperty("Name", "Advanced Internal Server"); |
238 |
// remoteResources.add(new ComputingResourceContainer(localServer, |
239 |
// localInfo)); |
240 |
// } |
241 |
// |
242 |
// } catch (RemoteException e) { |
243 |
// // TODO Auto-generated catch block |
244 |
// e.printStackTrace(); |
245 |
// } |
246 |
} |
247 |
|
248 |
|
249 |
/** |
250 |
* adds the change lister |
251 |
* |
252 |
* @param r the change listener |
253 |
*/ |
254 |
public void addResourceChangeListener(ResourceChangeListener r){ |
255 |
activeChangeListeners.add(r); |
256 |
} |
257 |
|
258 |
/** |
259 |
* Removes the change listener |
260 |
* @param r the change listener to remove |
261 |
*/ |
262 |
public void removeResourceChangeListener(ResourceChangeListener r){ |
263 |
activeChangeListeners.remove(r); |
264 |
} |
265 |
|
266 |
|
267 |
|
268 |
/** |
269 |
* Calculates weight for the given resources for the use in {@link SplitMap} |
270 |
* splitmaps. For this the PerfomanceRating read out of the |
271 |
* {@link XuluConfig} property <br> |
272 |
* "PerformanceRating." + name of the machine <br> |
273 |
* If the rating is 0 an average rating is assumed. |
274 |
* |
275 |
* @param computingResources |
276 |
* @return the weights |
277 |
*/ |
278 |
private double[] calculateWeights( |
279 |
Vector<ComputingResourceContainer> computingResources) { |
280 |
// first get the rating. Try to find the value in the |
281 |
// (user defined)configuration first, if not there try resource info. |
282 |
int ratings[] = new int[computingResources.size()]; |
283 |
for (int i = 0; i < ratings.length; i++) { |
284 |
ComputingResourceContainer container = computingResources.get(i); |
285 |
String resName = container.getInformation().getName(); |
286 |
String rating = XuluConfig.getXuluConfig().getProperty( |
287 |
"PerformanceRating." + resName, false); |
288 |
// if not found take the benchmark value |
289 |
if (rating == null) { |
290 |
rating = container.getInformation().getProperty("Rating"); |
291 |
} |
292 |
if (rating == null) |
293 |
rating = "0"; |
294 |
ratings[i] = Integer.valueOf(rating); |
295 |
} |
296 |
return Helper.calculateWeights(ratings); |
297 |
} |
298 |
|
299 |
/** |
300 |
* Gives a new {@link SPMDClientController}. |
301 |
* |
302 |
* @param computingResources |
303 |
* the resources to be used by the new controller |
304 |
* @return a new {@link SPMDClientController} for the given resources |
305 |
*/ |
306 |
public SPMDClientController getNewSPMDClientController( |
307 |
ComputingResourceContainer[] computingResources) { |
308 |
Vector<ComputingResourceContainer> resvec = new Vector<ComputingResourceContainer>( |
309 |
computingResources.length); |
310 |
for (ComputingResourceContainer container : computingResources) { |
311 |
resvec.add(container); |
312 |
} |
313 |
return getNewSPMDClientController(resvec); |
314 |
} |
315 |
|
316 |
/** |
317 |
* Gives a new {@link SPMDClientController}. |
318 |
* |
319 |
* @param computingResources |
320 |
* the resources to be used by the new controller |
321 |
* @return a new {@link SPMDClientController} for the given resources |
322 |
*/ |
323 |
public SPMDClientController getNewSPMDClientController( |
324 |
Vector<ComputingResourceContainer> computingResources) { |
325 |
double[] weights = calculateWeights(computingResources); |
326 |
return new AdvancedSPMDClientController(computingResources, weights, |
327 |
spmdClient, eventHandlerProxy); |
328 |
} |
329 |
|
330 |
/** |
331 |
* @return the execution controller of the given |
332 |
* {@link XuluModellingPlatform} or null, if not found |
333 |
*/ |
334 |
public static RemoteExecutionController getRemoteExecutionController( |
335 |
XuluModellingPlatform modellingPlatform) { |
336 |
XuluPlugin[] plugins = modellingPlatform.getRegistry().getPlugins(); |
337 |
for (XuluPlugin plugin : plugins) { |
338 |
if (plugin instanceof RemoteExecutionController) |
339 |
return (RemoteExecutionController) plugin; |
340 |
} |
341 |
return null; |
342 |
} |
343 |
|
344 |
public RemoteEventHandler getEventProxy() { |
345 |
return eventHandlerProxy; |
346 |
} |
347 |
|
348 |
/** |
349 |
* Prepares a Model for execution, which means it creates a |
350 |
* {@link SPMDClientController} with the given Resources |
351 |
* |
352 |
* @param model |
353 |
* the model which is to be executed parallel |
354 |
* @param selectedResourceContainers |
355 |
* the resources on which the model is executed |
356 |
*/ |
357 |
public void prepareModelForSPMDExecution(ParallelStepModel model, |
358 |
ComputingResourceContainer[] selectedResourceContainers) { |
359 |
SPMDClientInterface controller = getNewSPMDClientController(selectedResourceContainers); |
360 |
model.setSPMDController(controller); |
361 |
} |
362 |
} |