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