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