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