Formatting changes.
[u/mrichter/AliRoot.git] / SHUTTLE / AliShuttleConfig.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*
17 $Log$
18 Revision 1.10  2006/10/20 15:22:59  jgrosseo
19 o) Adding time out to the execution of the preprocessors: The Shuttle forks and the parent process monitors the child
20 o) Merging Collect, CollectAll, CollectNew function
21 o) Removing implementation of empty copy constructors (declaration still there!)
22
23 Revision 1.9  2006/10/02 16:38:39  jgrosseo
24 update (alberto):
25 fixed memory leaks
26 storing of objects that failed to be stored to the grid before
27 interfacing of shuttle status table in daq system
28
29 Revision 1.8  2006/08/15 10:50:00  jgrosseo
30 effc++ corrections (alberto)
31
32 Revision 1.7  2006/07/20 09:54:40  jgrosseo
33 introducing status management: The processing per subdetector is divided into several steps,
34 after each step the status is stored on disk. If the system crashes in any of the steps the Shuttle
35 can keep track of the number of failures and skips further processing after a certain threshold is
36 exceeded. These thresholds can be configured in LDAP.
37
38 Revision 1.6  2006/07/19 10:09:55  jgrosseo
39 new configuration, accesst to DAQ FES (Alberto)
40
41 Revision 1.5  2006/07/10 13:01:41  jgrosseo
42 enhanced storing of last sucessfully processed run (alberto)
43
44 Revision 1.4  2006/06/12 09:11:16  jgrosseo
45 coding conventions (Alberto)
46
47 Revision 1.3  2006/06/06 14:26:40  jgrosseo
48 o) removed files that were moved to STEER
49 o) shuttle updated to follow the new interface (Alberto)
50
51 Revision 1.7  2006/05/12 09:07:16  colla
52 12/05/06
53 New configuration complete
54
55 Revision 1.2  2006/03/07 07:52:34  hristov
56 New version (B.Yordanov)
57
58 Revision 1.4  2005/11/19 14:20:31  byordano
59 logbook config added to AliShuttleConfig
60
61 Revision 1.3  2005/11/17 19:24:25  byordano
62 TList changed to TObjArray in AliShuttleConfig
63
64 Revision 1.2  2005/11/17 14:43:23  byordano
65 import to local CVS
66
67 Revision 1.1.1.1  2005/10/28 07:33:58  hristov
68 Initial import as subdirectory in AliRoot
69
70 Revision 1.1.1.1  2005/09/12 22:11:40  byordano
71 SHUTTLE package
72
73 Revision 1.3  2005/08/30 09:13:02  byordano
74 some docs added
75
76 */
77
78
79 //
80 // This class keeps the AliShuttle configuration.
81 // It reads the configuration for LDAP server.
82 // For every child entry in basedn which has schema type 'shuttleConfig'
83 // it creates a detector configuration. This configuration includes:
84 // DCS server host and port and the set of aliases for which data from
85 // will be retrieved (used by AliShuttle).
86 //
87
88
89 #include "AliShuttleConfig.h"
90 #include "AliShuttleInterface.h"
91
92 #include "AliLog.h"
93
94 #include <TSystem.h>
95 #include <TObjString.h>
96 #include <TLDAPResult.h>
97 #include <TLDAPEntry.h>
98 #include <TLDAPAttribute.h>
99
100
101 AliShuttleConfig::AliShuttleConfigHolder::AliShuttleConfigHolder(const TLDAPEntry* entry):
102 fDetector(""),
103 fDCSHost(""),
104 fDCSPort(0),
105 fIsValid(kFALSE),
106 fSkipDCSQuery(kFALSE)
107 {
108 // constructor of the shuttle configuration holder
109
110         TLDAPAttribute* anAttribute;
111         fDCSAliases = new TObjArray();
112         fDCSAliases->SetOwner(1);
113
114         anAttribute = entry->GetAttribute("det"); // MUST
115         fDetector = anAttribute->GetValue();
116
117         anAttribute = entry->GetAttribute("DCSHost"); // MAY
118         if (!anAttribute) {
119                 AliWarning(
120                         Form("%s has not DCS host entry - Shuttle will skip DCS data query!",
121                                 fDetector.Data()));
122                 fIsValid = kTRUE;
123                 fSkipDCSQuery = kTRUE;
124                 return;
125         }
126
127         fDCSHost = anAttribute->GetValue();
128
129         anAttribute = entry->GetAttribute("DCSPort"); // MAY
130         if (!anAttribute) {
131                 AliError(Form("Invalid configuration! %s has DCS Host but no port number!",
132                                 fDetector.Data()));
133                 return;
134         }
135         TString portStr = anAttribute->GetValue();
136         fDCSPort = portStr.Atoi();
137
138         anAttribute = entry->GetAttribute("DCSAlias"); // MAY
139         if (!anAttribute) {
140                 AliError(Form("Invalid configuration! %s has DCS host settings but no DCSAlias entries!",
141                                 fDetector.Data()));
142                 return;
143         }
144
145         const char* anAlias;
146         while ((anAlias = anAttribute->GetValue())) {
147                 fDCSAliases->AddLast(new TObjString(anAlias));
148         }
149
150         fIsValid = kTRUE;
151
152
153 }
154
155 //______________________________________________________________________________________________
156 AliShuttleConfig::AliShuttleConfigHolder::~AliShuttleConfigHolder()
157 {
158 // destructor of the shuttle configuration holder
159
160         delete fDCSAliases;
161 }
162
163 ClassImp(AliShuttleConfig)
164
165 //______________________________________________________________________________________________
166 AliShuttleConfig::AliShuttleConfig(const char* host, Int_t port,
167         const char* binddn, const char* password, const char* basedn):
168         fIsValid(kFALSE),
169         fDAQlbHost(""), fDAQlbUser(""), fDAQlbPass(""),
170         fMaxRetries(0), fPPTimeOut(0), fDetectorMap(), fDetectorList(),
171         fShuttleInstanceHost(""), fProcessedDetectors(), fProcessAll(kFALSE)
172 {
173         //
174         // host: ldap server host
175         // port: ldap server port
176         // binddn: binddn used for ldap binding (simple bind is used!).
177         // password: password for binddn
178         // basedn: this is basedn whose childeren entries which have
179         // (objectClass=shuttleConfig) will be used as detector configurations.
180         //
181
182         fDetectorMap.SetOwner();
183         fDetectorList.SetOwner(0); //fDetectorList and fDetectorMap share the same object!
184         fProcessedDetectors.SetOwner();
185
186         TLDAPServer aServer(host, port, binddn, password, 3);
187
188         if (!aServer.IsConnected()) {
189                 AliError(Form("Can't connect to ldap server %s:%d",
190                                 host, port));
191                 return;
192         }
193
194         // reads configuration for the shuttle running on this machine
195
196         fShuttleInstanceHost = gSystem->HostName();
197         TString queryFilter = Form("(ShuttleHost=%s)", fShuttleInstanceHost.Data());
198
199         TLDAPResult* aResult = aServer.Search(basedn, LDAP_SCOPE_ONELEVEL, queryFilter.Data());
200
201         if (!aResult) {
202                 AliError(Form("Can't find configuration with base DN: %s",
203                                 basedn));
204                 return;
205         }
206
207         if (aResult->GetCount() == 0) {
208                 AliError(Form("No Shuttle instance for host = %s!",
209                                         fShuttleInstanceHost.Data()));
210                 AliError(Form("All detectors will be processed."));
211                 fProcessAll=kTRUE;
212         }
213
214         if (aResult->GetCount() > 1) {
215                 AliError(Form("More than one Shuttle instance for host %s!",
216                                         fShuttleInstanceHost.Data()));
217                 delete aResult;
218                 return;
219         }
220
221         TLDAPEntry* anEntry = 0;
222         TLDAPAttribute* anAttribute = 0;
223
224         if(!fProcessAll){
225                 anEntry = aResult->GetNext();
226                 anAttribute = anEntry->GetAttribute("detectors");
227                 const char *detName;
228                 while((detName = anAttribute->GetValue())){
229                         TObjString *objDet= new TObjString(detName);
230                         fProcessedDetectors.Add(objDet);
231                 }
232         }
233
234         delete anEntry; delete aResult;
235
236         // Detector configuration (DCS Archive DB settings)
237
238         aResult = aServer.Search(basedn, LDAP_SCOPE_ONELEVEL, "(objectClass=AliShuttleDetector)");
239         if (!aResult) {
240                 AliError(Form("Can't find configuration with base DN: %s", basedn));
241                 return;
242         }
243
244
245         while ((anEntry = aResult->GetNext())) {
246                 AliShuttleConfigHolder* aHolder = new AliShuttleConfigHolder(anEntry);
247                 delete anEntry;
248
249                 if (!aHolder->IsValid()) {
250                         AliError("Detector configuration error!");
251                         delete aHolder;
252                         delete aResult;
253                         return;
254                 }
255
256                 TObjString* detStr = new TObjString(aHolder->GetDetector());
257                 fDetectorMap.Add(detStr, aHolder);
258                 fDetectorList.AddLast(detStr);
259         }
260
261         delete aResult;
262
263         // Global configuration (DAQ logbook)
264
265         aResult = aServer.Search(basedn, LDAP_SCOPE_ONELEVEL,
266                         "(objectClass=AliShuttleGlobalConfig)");
267         if (!aResult) {
268                 AliError(Form("Can't find configuration with base DN: %s",
269                                 basedn));
270                 return;
271         }
272
273         if (aResult->GetCount() == 0) {
274                 AliError("Can't find DAQ logbook configuration!");
275                 delete aResult;
276                 return;
277         }
278
279         if (aResult->GetCount() > 1) {
280                 AliError("More than one DAQ logbook configuration found!");
281                 delete aResult;
282                 return;
283         }
284
285         anEntry = aResult->GetNext();
286
287         anAttribute = anEntry->GetAttribute("DAQLogbookHost");
288         if (!anAttribute) {
289                 AliError("Can't find DAQLogbookHost attribute!");
290                 delete anEntry; delete aResult;
291                 return;
292         }
293         fDAQlbHost = anAttribute->GetValue();
294
295         anAttribute = anEntry->GetAttribute("DAQLogbookUser");
296         if (!anAttribute) {
297                 AliError("Can't find DAQLogbookUser attribute!");
298                 delete aResult; delete anEntry;
299                 return;
300         }
301         fDAQlbUser = anAttribute->GetValue();
302
303         anAttribute = anEntry->GetAttribute("DAQLogbookPassword");
304         if (!anAttribute) {
305                 AliError("Can't find DAQLogbookPassword attribute!");
306                 delete aResult; delete anEntry;
307                 return;
308         }
309         fDAQlbPass = anAttribute->GetValue();
310
311         anAttribute = anEntry->GetAttribute("MaxRetries");
312         if (!anAttribute) {
313                 AliError("Can't find MaxRetries attribute!");
314                 delete aResult; delete anEntry;
315                 return;
316         }
317         TString tmpStr = anAttribute->GetValue();
318         fMaxRetries = tmpStr.Atoi();
319
320         anAttribute = anEntry->GetAttribute("PPTimeOut");
321         if (!anAttribute) {
322                 AliError("Can't find PPTimeOut attribute!");
323                 delete aResult; delete anEntry;
324                 return;
325         }
326         tmpStr = anAttribute->GetValue();
327         fPPTimeOut = tmpStr.Atoi();
328
329         delete aResult; delete anEntry;
330
331         // FES configuration (FES logbook and hosts)
332
333         for(int iSys=0;iSys<3;iSys++){
334                 queryFilter = Form("(system=%s)", AliShuttleInterface::GetSystemName(iSys));
335                 aResult = aServer.Search(basedn, LDAP_SCOPE_ONELEVEL, queryFilter.Data());
336                 if (!aResult) {
337                         AliError(Form("Can't find configuration for system: %s",
338                                         AliShuttleInterface::GetSystemName(iSys)));
339                         return;
340                 }
341
342                 if (aResult->GetCount() != 1 ) {
343                         AliError("Error in FES configuration!");
344                         delete aResult;
345                         return;
346                 }
347
348                 anEntry = aResult->GetNext();
349
350                 anAttribute = anEntry->GetAttribute("LogbookHost");
351                 if (!anAttribute) {
352                         AliError(Form ("Can't find LogbookHost attribute for %s!!",
353                                                 AliShuttleInterface::GetSystemName(iSys)));
354                         delete aResult; delete anEntry;
355                         return;
356                 }
357                 fFESlbHost[iSys] = anAttribute->GetValue();
358
359                 anAttribute = anEntry->GetAttribute("LogbookUser");
360                 if (!anAttribute) {
361                         AliError(Form ("Can't find LogbookUser attribute for %s!!",
362                                                 AliShuttleInterface::GetSystemName(iSys)));
363                         delete aResult; delete anEntry;
364                         return;
365                 }
366                 fFESlbUser[iSys] = anAttribute->GetValue();
367
368                 anAttribute = anEntry->GetAttribute("LogbookPassword");
369                 if (!anAttribute) {
370                         AliError(Form ("Can't find LogbookPassword attribute for %s!!",
371                                                 AliShuttleInterface::GetSystemName(iSys)));
372                         delete aResult; delete anEntry;
373                         return;
374                 }
375                 fFESlbPass[iSys] = anAttribute->GetValue();
376
377                 anAttribute = anEntry->GetAttribute("FSHost");
378                 if (!anAttribute) {
379                         AliError(Form ("Can't find FSHost attribute for %s!!",
380                                                 AliShuttleInterface::GetSystemName(iSys)));
381                         delete aResult; delete anEntry;
382                         return;
383                 }
384                 fFESHost[iSys] = anAttribute->GetValue();
385
386                 anAttribute = anEntry->GetAttribute("FSUser");
387                 if (!anAttribute) {
388                         AliError(Form ("Can't find FSUser attribute for %s!!",
389                                                 AliShuttleInterface::GetSystemName(iSys)));
390                         delete aResult; delete anEntry;
391                         return;
392                 }
393                 fFESUser[iSys] = anAttribute->GetValue();
394
395                 anAttribute = anEntry->GetAttribute("FSPassword");
396                 if (anAttribute) fFESPass[iSys] = anAttribute->GetValue();
397
398                 delete aResult; delete anEntry;
399         }
400
401         fIsValid = kTRUE;
402 }
403
404 //______________________________________________________________________________________________
405 AliShuttleConfig::~AliShuttleConfig()
406 {
407 // destructor
408
409         fDetectorMap.DeleteAll();
410         fDetectorList.Clear();
411         fProcessedDetectors.Delete();
412 }
413
414 //______________________________________________________________________________________________
415 const TObjArray* AliShuttleConfig::GetDetectors() const
416 {
417         //
418         // returns collection of TObjString which contains the name
419         // of every detector which is in the configuration.
420         //
421
422         return &fDetectorList;
423 }
424
425 //______________________________________________________________________________________________
426 Bool_t AliShuttleConfig::HasDetector(const char* detector) const
427 {
428         //
429         // checks for paricular detector in the configuration.
430         //
431         return fDetectorMap.GetValue(detector) != NULL;
432 }
433
434 //______________________________________________________________________________________________
435 const char* AliShuttleConfig::GetDCSHost(const char* detector) const
436 {
437         //
438         // returns DCS server host used by particular detector
439         //
440         
441         AliShuttleConfigHolder* aHolder = (AliShuttleConfigHolder*) fDetectorMap.GetValue(detector);
442         if (!aHolder) {
443                 AliError(Form("There isn't configuration for detector: %s",
444                         detector));
445                 return NULL;
446         }
447
448         return aHolder->GetDCSHost();
449 }
450
451 //______________________________________________________________________________________________
452 Int_t AliShuttleConfig::GetDCSPort(const char* detector) const
453 {
454         //
455         // returns DCS server port used by particular detector
456         //
457
458
459         AliShuttleConfigHolder* aHolder = (AliShuttleConfigHolder*) fDetectorMap.GetValue(detector);
460         if (!aHolder) {
461                 AliError(Form("There isn't configuration for detector: %s",
462                         detector));
463                 return 0;
464         }
465
466         return aHolder->GetDCSPort();
467 }
468
469 //______________________________________________________________________________________________
470 const TObjArray* AliShuttleConfig::GetDCSAliases(const char* detector) const
471 {
472         //
473         // returns collection of TObjString which represents the set of aliases
474         // which used for data retrieval for particular detector
475         //
476
477         AliShuttleConfigHolder* aHolder = (AliShuttleConfigHolder*) fDetectorMap.GetValue(detector);
478         if (!aHolder) {
479                 AliError(Form("There isn't configuration for detector: %s",
480                         detector));
481                 return NULL;
482         }
483
484         return aHolder->GetDCSAliases();
485 }
486
487 //______________________________________________________________________________________________
488 Bool_t AliShuttleConfig::HostProcessDetector(const char* detector) const
489 {
490         // return TRUE if detector is handled by host or if fProcessAll is TRUE
491
492         if(fProcessAll) return kTRUE;
493         TIter iter(&fProcessedDetectors);
494         TObjString* detName;
495         while((detName = (TObjString*) iter.Next())){
496                 if(detName->String() == detector) return kTRUE;
497         }
498         return kFALSE;
499 }
500
501 //______________________________________________________________________________________________
502 void AliShuttleConfig::Print(Option_t* /*option*/) const
503 {
504 // print configuration
505         
506         TString result;
507         result += '\n';
508
509         result += Form("\nShuttle running on %s \n\n", fShuttleInstanceHost.Data());
510
511         if(fProcessAll) {
512                 result += Form("All detectors will be processed! \n\n");
513         } else {
514                 result += "Detectors processed by this host: ";
515                 TIter it(&fProcessedDetectors);
516                 TObjString* aDet;
517                 while ((aDet = (TObjString*) it.Next())) {
518                         result += Form("%s ", aDet->String().Data());
519                 }
520                 result += "\n\n";
521         }
522
523         result += Form("PP time out = %d - Max total retries = %d\n\n", fPPTimeOut, fMaxRetries);
524
525         result += Form("DAQ Logbook Configuration \n \tHost: %s - User: %s - ",
526                 fDAQlbHost.Data(), fDAQlbUser.Data());
527
528         result += "Password: ";
529         result.Append('*', fDAQlbPass.Length());
530         result += "\n\n";
531
532         for(int iSys=0;iSys<3;iSys++){
533                 result += Form("FES Configuration for %s system\n", AliShuttleInterface::GetSystemName(iSys));
534                 result += Form("\tLogbook host: \t%s - \tUser: %s\n",
535                                                 fFESlbHost[iSys].Data(), fFESlbUser[iSys].Data());
536                 // result += Form("Logbook Password:",fFESlbPass[iSys].Data());
537                 result += Form("\tFES host: \t%s - \tUser: %s\n\n", fFESHost[iSys].Data(), fFESUser[iSys].Data());
538                 // result += Form("FES Password:",fFESPass[iSys].Data());
539         }
540
541         TIter iter(fDetectorMap.GetTable());
542         TPair* aPair;
543         while ((aPair = (TPair*) iter.Next())) {
544                 AliShuttleConfigHolder* aHolder = (AliShuttleConfigHolder*) aPair->Value();
545                 if(aHolder->SkipDCSQuery()) continue;
546                 result += Form("DCS archive DB configuration for *** %s *** \n", aHolder->GetDetector());
547                 result += Form("\tAmanda server: %s:%d \n", aHolder->GetDCSHost(), aHolder->GetDCSPort());
548
549                 result += "\tDCS Aliases: ";
550                 const TObjArray* aliases = aHolder->GetDCSAliases();
551                 TIter it(aliases);
552                 TObjString* anAlias;
553                 while ((anAlias = (TObjString*) it.Next())) {
554                         result += Form("%s ", anAlias->String().Data());
555                 }
556
557                 result += "\n\n";
558
559         }
560
561         if(!fIsValid) result += "\n\n********** !!!!! Configuration is INVALID !!!!! **********\n";
562
563         AliInfo(result);
564 }