1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 Revision 1.6 2006/07/19 10:09:55 jgrosseo
19 new configuration, accesst to DAQ FES (Alberto)
21 Revision 1.5 2006/07/10 13:01:41 jgrosseo
22 enhanced storing of last sucessfully processed run (alberto)
24 Revision 1.4 2006/07/04 14:59:57 jgrosseo
25 revision of AliDCSValue: Removed wrapper classes, reduced storage size per value by factor 2
27 Revision 1.3 2006/06/12 09:11:16 jgrosseo
28 coding conventions (Alberto)
30 Revision 1.2 2006/06/06 14:26:40 jgrosseo
31 o) removed files that were moved to STEER
32 o) shuttle updated to follow the new interface (Alberto)
34 Revision 1.1 2006/03/07 07:52:34 hristov
35 New version (B.Yordanov)
37 Revision 1.5 2005/11/21 09:03:48 byordano
40 Revision 1.4 2005/11/20 10:12:37 byordano
41 comments added to AliShuttleTrigger
47 // This class is to deal with DAQ LogBook and DAQ "end of run" notification.
48 // It has severeal two modes:
49 // 1) syncrhnized - Collect(), CollectNew() and CollectAll methods
50 // 2) asynchronized - Run() - starts listening for DAQ "end of run"
51 // notification by DIM service.
54 #include "AliShuttleTrigger.h"
56 #include <TSQLServer.h>
57 #include <TSQLResult.h>
59 #include <TObjArray.h>
63 #include "AliCDBManager.h"
64 #include "AliCDBStorage.h"
65 #include "AliCDBEntry.h"
67 #include "AliDCSValue.h"
68 #include "AliShuttleConfig.h"
69 #include "AliShuttle.h"
70 #include "DATENotifier.h"
72 ClassImp(TerminateSignalHandler)
74 //______________________________________________________________________
75 TerminateSignalHandler::TerminateSignalHandler(const TerminateSignalHandler& /*other*/):
78 // copy constructor (not implemented)
82 //______________________________________________________________________
83 TerminateSignalHandler &TerminateSignalHandler::operator=(const TerminateSignalHandler& /*other*/)
85 // assignment operator (not implemented)
90 //______________________________________________________________________________________________
91 Bool_t TerminateSignalHandler::Notify()
93 // Sentd terminate command to the Shuttle trigger
95 AliInfo("Terminate signal received ...");
96 fTrigger->Terminate();
101 //______________________________________________________________________________________________
102 //______________________________________________________________________________________________
104 ClassImp(AliShuttleTrigger)
106 //______________________________________________________________________________________________
107 AliShuttleTrigger::AliShuttleTrigger(const AliShuttleConfig* config,
108 UInt_t timeout, Int_t retries):
109 fConfig(config), fShuttle(NULL),
110 fNotified(kFALSE), fTerminate(kFALSE), fCondition(&fMutex),
111 fQuitSignalHandler(this, kSigQuit),
112 fInterruptSignalHandler(this, kSigInterrupt)
115 // config - pointer to the AliShuttleConfig object which represents
117 // mainStorage - pointer to AliCDBStorage for the undelying CDBStorage
118 // localStorage (local) CDB storage to be used if mainStorage is unavailable
121 fShuttle = new AliShuttle(config, timeout, retries);
123 gSystem->AddSignalHandler(&fQuitSignalHandler);
124 gSystem->AddSignalHandler(&fInterruptSignalHandler);
129 //______________________________________________________________________
130 AliShuttleTrigger::AliShuttleTrigger(const AliShuttleTrigger& /*other*/):
133 // copy constructor (not implemented)
137 //______________________________________________________________________
138 AliShuttleTrigger &AliShuttleTrigger::operator=(const AliShuttleTrigger& /*other*/)
140 // assignment operator (not implemented)
149 //______________________________________________________________________________________________
150 AliShuttleTrigger::~AliShuttleTrigger()
154 gSystem->RemoveSignalHandler(&fQuitSignalHandler);
155 gSystem->RemoveSignalHandler(&fInterruptSignalHandler);
160 //______________________________________________________________________________________________
161 Bool_t AliShuttleTrigger::Notify() {
163 // Trigger CollectNew() methods in asynchronized (listen) mode.
164 // Usually called automaticly by DATENotifier on "end of run"
165 // notification event.
178 //______________________________________________________________________________________________
179 void AliShuttleTrigger::Terminate() {
181 // Stop triggers listen mode and exist from Run()
182 // Usually called automaticly by TerminateSignalHandler.
189 //______________________________________________________________________________________________
190 void AliShuttleTrigger::Run() {
192 // AliShuttleTrigger main loop for asynchronized (listen) mode.
193 // It spawns DIM service listener and waits for DAQ "end of run"
194 // notification. Calls CollectNew() on notification.
199 DATENotifier* notifier = new DATENotifier(this, "/DATE/LOGBOOK/UPDATE");
205 while (!(fNotified || fTerminate)) {
214 AliInfo("Terminated.");
224 //______________________________________________________________________________________________
225 Bool_t AliShuttleTrigger::RetrieveDATEEntries(const char* whereClause,
228 // Retrieve start time and end time for all runs in the DAQ logbook
229 // that aren't processed yet
232 sqlQuery = Form("select run, time_start, time_end from logbook %s order by run",
236 TString logbookHost=Form("mysql://%s", fConfig->GetDAQlbHost());
238 aServer = TSQLServer::Connect(logbookHost,
239 fConfig->GetDAQlbUser(),
240 fConfig->GetDAQlbPass());
242 AliError("Can't establish connection to DAQ log book DB!");
246 aServer->GetTables("REFSYSLOG");
249 aResult = aServer->Query(sqlQuery);
251 AliError(Form("Can't execute query <%s>!", sqlQuery.Data()));
256 if (aResult->GetFieldCount() != 3) {
257 AliError("Invalid SQL result field number!");
264 while ((aRow = aResult->Next())) {
265 TString runString(aRow->GetField(0), aRow->GetFieldLength(0));
266 Int_t run = runString.Atoi();
268 TString startTimeString(aRow->GetField(1),
269 aRow->GetFieldLength(1));
270 Int_t startTime = startTimeString.Atoi();
272 AliWarning(Form("Zero StartTime for run <%d>!", run));
273 AliWarning("Going to skip this run!");
277 TString endTimeString(aRow->GetField(2),
278 aRow->GetFieldLength(2));
279 Int_t endTime = endTimeString.Atoi();
281 AliWarning(Form("Zero EndTime for run <%d>!", run));
282 AliWarning("Going to skip this run!");
286 if (startTime > endTime) {
287 AliWarning(Form("StartTime bigger than EndTime for run <%d>", run));
288 AliWarning("Going to skip this run!");
292 entries.AddLast(new AliShuttleTriggerDATEEntry(run, startTime, endTime));
306 //______________________________________________________________________________________________
307 Bool_t AliShuttleTrigger::RetrieveConditionsData(const TObjArray& dateEntries, Int_t &lastRun)
309 // Retrieve conditions data for all runs that aren't processed yet
311 Bool_t hasError = kFALSE;
313 TIter iter(&dateEntries);
314 AliShuttleTriggerDATEEntry* anEntry;
316 while ((anEntry = (AliShuttleTriggerDATEEntry*) iter.Next())) {
317 Bool_t processError = kFALSE;
318 if(lastRun == -1) lastRun = anEntry->GetRun();
319 if(!fShuttle->Process(anEntry->GetRun(),
320 anEntry->GetStartTime(),
321 anEntry->GetEndTime())) {
322 processError = kTRUE;
325 // Only the last SUCCESSFUL run must be stored!
326 if(!hasError && !processError) lastRun = anEntry->GetRun();
329 return hasError == kFALSE;
332 //______________________________________________________________________________________________
333 Bool_t AliShuttleTrigger::Collect(Int_t run)
336 // Collects conditions date for the given run.
339 AliInfo(Form("Collecting conditions data for run <%d> ...", run));
341 TString whereClause("where run = ");
344 TObjArray dateEntries;
345 if (!RetrieveDATEEntries(whereClause, dateEntries)) {
346 AliError("Can't retrieve entries from DAQ log book.");
350 if (!dateEntries.GetEntriesFast()) {
351 AliError(Form("There isn't entry for run <%d> in DAQ log book!",
356 if (dateEntries.GetEntriesFast() > 1) {
357 AliError(Form("There is more than one entry for run <%d> in DAQ log book", run));
362 if (!RetrieveConditionsData(dateEntries, lastRun)) {
363 AliError("An error occured during conditions data retrieval!");
370 //______________________________________________________________________________________________
371 Bool_t AliShuttleTrigger::CollectNew()
374 // Collects conditions data for all new run written to DAQ LogBook.
377 AliInfo("Collecting conditions data for new runs ...");
381 AliCDBEntry* cdbEntry = AliCDBManager::Instance()->GetStorage(AliShuttle::GetLocalURI())
382 ->Get("/SHUTTLE/SYSTEM/LASTRUN", 0);
384 TObject* anObject = cdbEntry->GetObject();
385 if (anObject == NULL ||
386 anObject->IsA() != AliDCSValue::Class()) {
387 AliError("Invalid last run object stored to CDB!");
390 AliDCSValue* simpleValue = (AliDCSValue*) anObject;
391 lastRun = simpleValue->GetInt();
392 AliInfo(Form("Last run successfully stored: %d",lastRun));
395 AliWarning("There isn't last run stored! Starting from run 21240");
396 lastRun = 21240; // TODO maybe exit here
399 AliInfo(Form("Last run number <%d>", lastRun));
401 TString whereClause("where run > ");
402 whereClause += lastRun;
405 TObjArray dateEntries;
406 if (!RetrieveDATEEntries(whereClause, dateEntries)) {
407 AliError("Can't retrieve entries from DAQ log book.");
411 if (!RetrieveConditionsData(dateEntries, newLastRun)) {
412 AliError("Process of at least one run failed!");
416 if (newLastRun > lastRun) {
417 AliDCSValue lastRunObj(newLastRun, 0);
418 AliCDBMetaData metaData;
419 AliCDBId cdbID(AliCDBPath("SHUTTLE", "SYSTEM", "LASTRUN"), 0, 0);
421 UInt_t result = AliCDBManager::Instance()->GetStorage(AliShuttle::GetLocalURI())
422 ->Put(&lastRunObj, cdbID, &metaData);
425 AliError("Can't store last run to CDB!");
434 //______________________________________________________________________________________________
435 Bool_t AliShuttleTrigger::CollectAll()
438 // Collects conditions data for all run written in DAQ LogBook.
441 AliInfo("Collecting conditions data for all runs ...");
444 TObjArray dateEntries;
445 if (!RetrieveDATEEntries("", dateEntries)) {
446 AliError("Can't retrieve entries from DAQ log book.");
450 if (!RetrieveConditionsData(dateEntries, lastRun)) {
451 AliError("An error occured during conditions data retrieval!");