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.3 2006/06/12 09:11:16 jgrosseo
19 coding conventions (Alberto)
21 Revision 1.2 2006/06/06 14:26:40 jgrosseo
22 o) removed files that were moved to STEER
23 o) shuttle updated to follow the new interface (Alberto)
25 Revision 1.1 2006/03/07 07:52:34 hristov
26 New version (B.Yordanov)
28 Revision 1.5 2005/11/21 09:03:48 byordano
31 Revision 1.4 2005/11/20 10:12:37 byordano
32 comments added to AliShuttleTrigger
38 // This class is to deal with DAQ LogBook and DAQ "end of run" notification.
39 // It has severeal two modes:
40 // 1) syncrhnized - Collect(), CollectNew() and CollectAll methods
41 // 2) asynchronized - Run() - starts listening for DAQ "end of run"
42 // notification by DIM service.
45 #include "AliShuttleTrigger.h"
47 #include <TSQLServer.h>
48 #include <TSQLResult.h>
50 #include <TObjArray.h>
54 #include "AliCDBManager.h"
55 #include "AliCDBStorage.h"
56 #include "AliCDBEntry.h"
58 #include "AliDCSValue.h"
59 #include "AliShuttleConfig.h"
60 #include "AliShuttle.h"
61 #include "DATENotifier.h"
63 ClassImp(TerminateSignalHandler)
65 //______________________________________________________________________
66 TerminateSignalHandler::TerminateSignalHandler(const TerminateSignalHandler& /*other*/):
69 // copy constructor (not implemented)
73 //______________________________________________________________________
74 TerminateSignalHandler &TerminateSignalHandler::operator=(const TerminateSignalHandler& /*other*/)
76 // assignment operator (not implemented)
81 //______________________________________________________________________________________________
82 Bool_t TerminateSignalHandler::Notify()
84 // Sentd terminate command to the Shuttle trigger
86 AliInfo("Terminate signal received ...");
87 fTrigger->Terminate();
92 //______________________________________________________________________________________________
93 //______________________________________________________________________________________________
95 ClassImp(AliShuttleTrigger)
97 //______________________________________________________________________________________________
98 AliShuttleTrigger::AliShuttleTrigger(const AliShuttleConfig* config,
99 UInt_t timeout, Int_t retries):
100 fConfig(config), fShuttle(NULL),
101 fNotified(kFALSE), fTerminate(kFALSE), fCondition(&fMutex),
102 fQuitSignalHandler(this, kSigQuit),
103 fInterruptSignalHandler(this, kSigInterrupt)
106 // config - pointer to the AliShuttleConfig object which represents
108 // mainStorage - pointer to AliCDBStorage for the undelying CDBStorage
109 // localStorage (local) CDB storage to be used if mainStorage is unavailable
112 fShuttle = new AliShuttle(config, timeout, retries);
114 gSystem->AddSignalHandler(&fQuitSignalHandler);
115 gSystem->AddSignalHandler(&fInterruptSignalHandler);
120 //______________________________________________________________________
121 AliShuttleTrigger::AliShuttleTrigger(const AliShuttleTrigger& /*other*/):
124 // copy constructor (not implemented)
128 //______________________________________________________________________
129 AliShuttleTrigger &AliShuttleTrigger::operator=(const AliShuttleTrigger& /*other*/)
131 // assignment operator (not implemented)
140 //______________________________________________________________________________________________
141 AliShuttleTrigger::~AliShuttleTrigger()
145 gSystem->RemoveSignalHandler(&fQuitSignalHandler);
146 gSystem->RemoveSignalHandler(&fInterruptSignalHandler);
151 //______________________________________________________________________________________________
152 Bool_t AliShuttleTrigger::Notify() {
154 // Trigger CollectNew() methods in asynchronized (listen) mode.
155 // Usually called automaticly by DATENotifier on "end of run"
156 // notification event.
169 //______________________________________________________________________________________________
170 void AliShuttleTrigger::Terminate() {
172 // Stop triggers listen mode and exist from Run()
173 // Usually called automaticly by TerminateSignalHandler.
180 //______________________________________________________________________________________________
181 void AliShuttleTrigger::Run() {
183 // AliShuttleTrigger main loop for asynchronized (listen) mode.
184 // It spawns DIM service listener and waits for DAQ "end of run"
185 // notification. Calls CollectNew() on notification.
190 DATENotifier* notifier = new DATENotifier(this, "/DATE/LOGBOOK/UPDATE");
196 while (!(fNotified || fTerminate)) {
205 AliInfo("Terminated.");
215 //______________________________________________________________________________________________
216 Bool_t AliShuttleTrigger::RetrieveDATEEntries(const char* whereClause,
217 TObjArray& entries, Int_t& lastRun)
219 // Retrieve start time and end time for all runs in the DAQ logbook
220 // that aren't processed yet
223 sqlQuery += "select run, time_start, time_end from logbook ";
224 sqlQuery += whereClause;
225 sqlQuery += " order by run";
228 TString logbookHost="mysql://";
229 logbookHost+=fConfig->GetDAQLogBookHost();
231 aServer = TSQLServer::Connect(logbookHost,
232 fConfig->GetDAQLogBookUser(),
233 fConfig->GetDAQLogBookPassword());
235 AliError("Can't establish connection to DAQ log book DB!");
239 aServer->GetTables("REFSYSLOG");
242 aResult = aServer->Query(sqlQuery);
244 AliError(Form("Can't execute query <%s>!", sqlQuery.Data()));
249 if (aResult->GetFieldCount() != 3) {
250 AliError("Invalid SQL result field number!");
259 while ((aRow = aResult->Next())) {
260 TString runString(aRow->GetField(0), aRow->GetFieldLength(0));
261 Int_t run = runString.Atoi();
263 TString startTimeString(aRow->GetField(1),
264 aRow->GetFieldLength(1));
265 Int_t startTime = startTimeString.Atoi();
267 AliWarning(Form("Zero StartTime for run <%d>!", run));
268 AliWarning("Going to skip this run!");
272 TString endTimeString(aRow->GetField(2),
273 aRow->GetFieldLength(2));
274 Int_t endTime = endTimeString.Atoi();
276 AliWarning(Form("Zero EndTime for run <%d>!", run));
277 AliWarning("Going to skip this run!");
281 if (startTime > endTime) {
282 AliWarning(Form("StartTime bigger than EndTime for run <%d>", run));
283 AliWarning("Going to skip this run!");
287 entries.AddLast(new AliShuttleTriggerDATEEntry(run, startTime, endTime));
304 //______________________________________________________________________________________________
305 Bool_t AliShuttleTrigger::RetrieveConditionsData(const TObjArray& dateEntries)
307 // Retrieve conditions data for all runs that aren't processed yet
309 Bool_t hasError = kFALSE;
311 TIter iter(&dateEntries);
312 AliShuttleTriggerDATEEntry* anEntry;
313 while ((anEntry = (AliShuttleTriggerDATEEntry*) iter.Next())) {
314 if(!fShuttle->Process(anEntry->GetRun(),
315 anEntry->GetStartTime(),
316 anEntry->GetEndTime())) {
324 //______________________________________________________________________________________________
325 Bool_t AliShuttleTrigger::Collect(Int_t run)
328 // Collects conditions date for the given run.
331 AliInfo(Form("Collecting conditions data for run <%d> ...", run));
333 TString whereClause("where run = ");
337 TObjArray dateEntries;
338 if (!RetrieveDATEEntries(whereClause, dateEntries, lastRun)) {
339 AliError("Can't retrieve entries from DAQ log book.");
343 if (!dateEntries.GetEntriesFast()) {
344 AliError(Form("There isn't entry for run <%d> in DAQ log book!",
349 if (dateEntries.GetEntriesFast() > 1) {
350 AliError(Form("There is more than one entry for run <%d> in DAQ log book", run));
354 if (!RetrieveConditionsData(dateEntries)) {
355 AliError("An error occured during conditions data retrieval!");
362 //______________________________________________________________________________________________
363 Bool_t AliShuttleTrigger::CollectNew()
366 // Collects conditions data for all new run written to DAQ LogBook.
369 // TODO revise this! last run number is ONLY allowed to be written when run was processed successfully!!!
371 AliInfo("Collecting conditions data for new runs ...");
375 AliCDBEntry* cdbEntry = AliCDBManager::Instance()->GetStorage(AliShuttle::GetLocalURI())
376 ->Get("/SHUTTLE/SYSTEM/LASTRUN", 0);
378 TObject* anObject = cdbEntry->GetObject();
379 if (anObject == NULL ||
380 anObject->IsA() != AliDCSValue::Class()) {
381 AliError("Invalid last run object stored to CDB!");
384 AliDCSValue* simpleValue = (AliDCSValue*) anObject;
385 lastRun = simpleValue->GetInt();
388 AliWarning("There isn't last run stored! Starting from run 21200");
389 lastRun = 21200; // TODO maybe exit here
392 AliInfo(Form("Last run number <%d>", lastRun));
394 TString whereClause("where run > ");
395 whereClause += lastRun;
398 TObjArray dateEntries;
399 if (!RetrieveDATEEntries(whereClause, dateEntries, newLastRun)) {
400 AliError("Can't retrieve entries from DAQ log book.");
404 if (newLastRun > lastRun) {
405 AliDCSValue lastRunObj(newLastRun, 0);
406 AliCDBMetaData metaData;
407 AliCDBId cdbID(AliCDBPath("SHUTTLE", "SYSTEM", "LASTRUN"), 0, 0);
409 UInt_t result = AliCDBManager::Instance()->GetStorage(AliShuttle::GetLocalURI())
410 ->Put(&lastRunObj, cdbID, &metaData);
413 AliError("Can't store last run to CDB!");
418 if (!RetrieveConditionsData(dateEntries)) {
419 AliError("An error occured during conditions data retrieval!");
426 //______________________________________________________________________________________________
427 Bool_t AliShuttleTrigger::CollectAll()
430 // Collects conditions data for all run written in DAQ LogBook.
433 AliInfo("Collecting conditions data for all runs ...");
436 TObjArray dateEntries;
437 if (!RetrieveDATEEntries("", dateEntries, lastRun)) {
438 AliError("Can't retrieve entries from DAQ log book.");
442 if (!RetrieveConditionsData(dateEntries)) {
443 AliError("An error occured during conditions data retrieval!");