-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessFactory.h
530 lines (457 loc) · 14.9 KB
/
ProcessFactory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#ifndef ProcessFactory_h_IS_INCLUDED
#define ProcessFactory_h_IS_INCLUDED
#include "ProcessFactory/IncludeExtLibs.h"
#include "ProcessFactory/ResourceMonitor.h"
#include <list>
#include <map>
#ifdef USE_GCC
#include <sys/types.h>
#include <signal.h>
#endif
// -------------------------------------------------------
// ProcessFactoryConfig
//
// - display a string containing a unix-type display parameter, e.g. "blade0:0.0" used
// for redirecting the display output to other hosts
// - debugLevel an integer indicating the level of debug printf's (normally = 0)
// -------------------------------------------------------
struct ProcessFactoryConfig
{
static int factoryPort;
static string display;
static int debugLevel;
static string configurationFile;
static string systemManagerHost;
static int systemManagerPort;
static string networkInterfaceIP;
static string debugTool;
static HostInformation factoryInfo;
static string binDir;
static string logDir;
};
class NotifySystemManagerThread;
using namespace std;
namespace ProcessFactorySpace
{
class ProcessFactoryCommand;
// -------------------------------------------------------
// class ProcessFactory
// -------------------------------------------------------
class ProcessFactory //: public Component
: public ProcessFactoryInterface, public Thread
{
private:
// -- start outdated variables --
typedef map<int, string> MapInstanceIdComponentName;
MapInstanceIdComponentName mapInstanceIdComponentName;
int privateInstanceId;
// -- end outdated variables --
typedef map<string, HostInformation> MapComponentInformation;
MapComponentInformation mapComponentInformation;
Mutex guard;
InterfaceHandle systemManagerInterfaceHandle;
InterfaceHandle factoryInterfaceHandle;
ResourceMonitor resourceMonitor;
NotifySystemManagerThread *notifySystemManagerThread;
ProcessFactoryCommand *componentFactoryCommand;
bool runProcessFactory;
private:
/**
* Creates a process by executing executableName.
*
* @param executableName Name of the executable file.
* @param componentName Name of the component to be started.
* @param args Command line arguments provided when executing executableName.
*/
int createProcess(const string &executableName, const string &componentName, const string &args, stringstream &errorMsg);
/**
* Notifies the SystemManager when the OnlineStatus (ONLINE or OFFLINE) of a component changes.
*
* @param info A component's HostInformation.
* @param status OnlineStatus (ONLINE or OFFLINE) of the component.
*/
void notifySystemManager(HostInformation &info, HostInformation::OnlineStatus status);
#ifdef USE_GCC
/**
* Reaps (waits-out) Zombie processes that the ProcessFactory has forked but not waited for yet.
*/
void reapZombieProcesses();
#endif
/**
* OUTDATED FUNCTION:
* waits for the factory to receive the notification about
* the creation of the object. If the notification message has
* not been received within the specified timeOut a
* MiddlewareException will be thrown.
*
* @param timeOut the time out period in milliseconds
* @param processId the process id of the object currently being created
* @param componentName the type of the object currently being created
* @param instanceName the name of the instance currently being created
* @param hostName the name of the computer hosting the object currently being created
*/
InterfaceHandle getInterfaceHandle(int timeOut, int instanceId, int processId, const string& componentName, const string& instanceName, const string& hostName);
private:
void cmdPrintHostInformation(ostream& ostr);
public: // Methods below are public since they are called from signal handler and main().
void cmdQuit();
void cmdKillAll();
public:
/**
* Creates a Component Factory.
*
* @param name the name of this instance
* @param factoryPort the port number of this factory, i.e. the port number of the main factory stub
* @param systemManager InterfaceHandle to systemManager
*/
ProcessFactory(const string& name, int factoryPort, InterfaceHandle systemManager);
/**
* Stops ProcessFactory.
*/
virtual ~ProcessFactory();
/**
* Allows ProcessFactoryCommand to access private functions in LookupServer.
*/
friend class ProcessFactoryCommand;
/**
* Main thread-loop of the ProcessFactory.
* It continouously checks the running state of components (ONLINE or OFFLINE),
* and reports to the System Manager when it changes.
*/
virtual void run();
public:
virtual inline InterfaceHandle GetInterfaceHandle() { return factoryInterfaceHandle; }
public:
// -- start outdated functions --
virtual InterfaceHandle CreateComponent(const string& componentType, const string& instanceName);
virtual void ComponentCreated(const string& hostName, int mainStubPort, int processID, const string& instanceName, const string& processFileName, int instanceId);
virtual InterfaceHandle GetComponent(const string& componentName, const string& instanceName);
// -- end outdated functions --
/**
* Creates (executes) a component using the given HostInformation, and provides the component with information
* on how to contact the LookupServer. This is called from SystemManager.
*
* @param info The HostInformation includes all information that is needed to start the component.
* @param lookupServer Contains the host information for the LookupServer.
* @return String containing (potential) error messages.
*/
virtual string CreateComponent(HostInformation &info, HostInformation lookupServer);
/**
* Creates (executes) a component using the given HostInformation, and provides the component with information
* on how to contact the LookupServer. This is called from components and is identical to CreateComponent,
* except it notifies the SystemManager if it is available.
*
* @param info The HostInformation includes all information that is needed to start the component.
* @param lookupServer Contains the host information for the LookupServer.
* @return String containing (potential) error messages.
*/
virtual string CreateExtComponent(HostInformation &info, HostInformation lookupServer);
/**
* Kills (stops) a component identified by componentName.
*
* @param componentName The name of the component to be killed (stopped).
* @return Returns 1 if component was killed, 0 if otherwise.
*/
virtual int KillComponent(string componentName);
/**
* Kills (stops) all components that has been started by the ProcessFactory
*
* @return Returns 1.
*/
virtual int KillAll();
/**
* Checks if a component identified with HostInformation is running.
*
* @param componentInfo HostInformation for a given component.
* @return Returns 1 if a component is running, 0 if not running or not found.
*/
virtual int IsComponentRunning(HostInformation componentInfo);
/**
* Retrieves resource information about a component identified by componentName.
*
* @param componentName The component's name.
* @return HostResources with memory and cpu information regarding componentName.
*/
virtual HostResources GetProcessResources(string componentName);
/**
* Retrieves resource information about the machine (node) the Component Factory is running.
*
* @return HostResources with memory and cpu information regarding the machine (node).
*/
virtual HostResources GetNodeResources();
/**
* Retrieves all resource information the Component Factory has (includes node and components).
*
* @return String with all resource information that can be "decoded" with HostResourceSpace::ParseHostResourcesFromString().
*/
virtual string GetAllComponentResources();
/**
* Returns a map (pid, HostResources) of all resource information on the ProcessFactory.
*
* @return map<int, HostResources> Map of all resource information (pid, HostResources).
*/
virtual map<int, HostResources> GetMapComponentResources();
};
// -------------------------------------------------------
// class ProcessFactoryCommand
// -------------------------------------------------------
/**
* ProcessFactoryCommand is a simple command interface to the ProcessFactory
*/
class ProcessFactoryCommand : public Thread
{
public:
/**
* Uses a ProcessFactory reference to call ProcessFactory's class functions for print, quit, etc.
*
* @param cf The ProcessFactory object.
*/
ProcessFactoryCommand(ProcessFactory &cf) : componentFactory(cf)
{
start();
}
/**
* Empty destructor.
*/
~ProcessFactoryCommand()
{}
private:
inline void printCommand()
{
cout << "ProcessFactory commands: print-info (p), quit-clean (q), stop-immediately (s), help (h)." << endl;
}
inline virtual void run()
{
printCommand();
while(true)
{
string command;
try
{
istream &ret = getline(cin, command);
if(ret.eof() || ret.bad() || !ret.good())
{
throw string("End of file!");
break;
}
}
catch(...)
{
//cout << "ProcessFactoryCommand: WARNING! getline returned End of file! Stopping command line!" << endl;
cout << "ProcessFactoryCommand::run(): WARNING! ProcessFactory user interface (UI) is off." << endl;
break;
}
if(strncmp(command.c_str(), "quit", 1) == 0)
{
cout << "Do you want to quit and kill all components (k), or, just quit (q)? " ;
string confirm;
getline(cin, confirm);
if(strncmp(confirm.c_str(), "quit", 1) == 0)
{
cout << "Quitting ProcessFactory without killing running components..." << endl;
componentFactory.cmdQuit();
break;
}
else if(strncmp(confirm.c_str(), "kill", 1) == 0)
{
cout << "Quitting ProcessFactory and killing running components..." << endl;
componentFactory.cmdKillAll();
msleep(2000);
componentFactory.cmdQuit();
break;
}
else printCommand();
}
else if(strncmp(command.c_str(), "print-info", 1) == 0)
{
componentFactory.cmdPrintHostInformation(cout);
}
else if(strncmp(command.c_str(), "help", 1) == 0)
{
printCommand();
}
else if(strncmp(command.c_str(), "stop-immediately", 1) == 0)
{
cout << "Stop-immediatly leaves all components started by this factory running! Really stop (y,n)? " << endl;
string confirm;
getline(cin, confirm);
if(strncmp(confirm.c_str(), "yes", 1) == 0)
{
#ifdef USE_GCC
kill(getpid(), SIGSEGV);
#else
_exit(0);
#endif
}
else printCommand();
}
else if(!command.empty())
{
cout << "Command: " << command << " is not supported" << endl;
}
}
}
private:
ProcessFactory &componentFactory;
};
// -------------------------------------------------------
// class used to notify system manager of dead processes
// -------------------------------------------------------
class NotifySystemManagerThread : public Thread
{
typedef map<int, HostResources> MapComponentResources;
public:
NotifySystemManagerThread(InterfaceHandle handle)
: systemManagerInterfaceHandle(handle)
, systemProxy(NULL)
, newArrival(false)
, stopNotifyThread(false)
{
start();
}
~NotifySystemManagerThread() {}
private:
virtual void run()
{
list<HostInformation> localList;
while(!stopNotifyThread)
{
guard.lock();
//monitor.Enter();
if(localList.empty() && listHostInformation.empty() && !newArrival)
monitor.wait(&guard);
if(stopNotifyThread)
{
guard.unlock();
//monitor.Exit();
break;
}
while(!listHostInformation.empty())
{
localList.push_back(listHostInformation.front());
listHostInformation.pop_front();
}
newArrival = false;
MapComponentResources sendMap = mapComponentResources;
guard.unlock();
//monitor.Exit();
// start sending only local variables!
this->sendHostRegistrations(localList);
this->sendResourceMap(sendMap);
}
}
// mutex in -> mutex out
void sendHostRegistrations(list<HostInformation> &listHostInformation)
{
bool printOutError = true; // for printout-debugging only
while(!listHostInformation.empty())
{
try
{
if(stopNotifyThread) break;
HostInformation info = listHostInformation.front();
SystemManagerProxy *systemProxy = getOrCreateProxy(systemManagerInterfaceHandle);
if(systemProxy == NULL)
{
throw ("");
}
else if(!systemProxy->IsConnected())
{
throw("");
}
HostInformation retInfo = systemProxy->HostRegistration(info);
listHostInformation.pop_front();
}
catch(...)
{
if(printOutError)
{
cout << "NotifySystemManagerThread::run(): Failed to contact SystemManager " << systemManagerInterfaceHandle << endl;
printOutError = false;
}
msleep(500);
}
}
}
void sendResourceMap(MapComponentResources sendMap)
{
try
{
if(!sendMap.empty())
{
SystemManagerProxy *systemProxy = getOrCreateProxy(systemManagerInterfaceHandle);
if(systemProxy == NULL)
{
throw("");
}
else if(!systemProxy->IsConnected())
{
throw("");
}
systemProxy->SetResourceInformation(ProcessFactoryConfig::factoryInfo, sendMap);
}
}
catch(...)
{
//cout << "UpdateResourceThread::run(): Failed to contact SystemManager " << systemManagerInterfaceHandle << endl;
}
}
SystemManagerProxy* getOrCreateProxy(InterfaceHandle systemManagerInterfaceHandle)
{
/*bool createNewProxy = false;
if(systemProxy != NULL) if(systemProxy->IsConnected() == false) createNewProxy = true;
if(systemProxy == NULL || createNewProxy)
{
try
{
if(systemProxy != NULL) delete systemProxy;
systemProxy = new SystemManagerProxy(systemManagerInterfaceHandle, true);
}
catch(...)
{
//cout << "getOrCreateProxy(): WARNING! Unable to contact SystemManager " << systemManagerInterfaceHandle << endl;
systemProxy = NULL;
return NULL;
}
}
//if(systemProxy == NULL)
// cout << "getOrCreateProxy(): WARNING! Unable to contact SystemManager " << systemManagerInterfaceHandle << endl;
*/
if(systemProxy == NULL) //delete systemProxy;
systemProxy = new SystemManagerProxy(systemManagerInterfaceHandle, true);
return systemProxy;
}
public:
inline void addAndWakeup(const HostInformation &info)
{
MutexLocker lock(&guard);
//monitor.Enter();
newArrival = true;
listHostInformation.push_back(info);
monitor.wakeAll();
//monitor.NotifyAll();
//monitor.Exit();
}
inline void addAndWakeup(const MapComponentResources &newMapComponentResources)
{
MutexLocker lock(&guard);
//monitor.Enter();
newArrival = true;
mapComponentResources = newMapComponentResources;
monitor.wakeAll();
//monitor.NotifyAll();
//monitor.Exit();
}
inline void stopThread(bool b) { stopNotifyThread = b; }
private:
InterfaceHandle systemManagerInterfaceHandle;
SystemManagerProxy *systemProxy;
bool newArrival;
mutable Mutex guard;
WaitCondition monitor;
list<HostInformation> listHostInformation;
MapComponentResources mapComponentResources;
bool stopNotifyThread;
};
} // namespace ProcessFactorySpace
#endif