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.1 2006/03/07 07:52:34 hristov
19 New version (B.Yordanov)
21 Revision 1.5 2005/11/21 09:03:48 byordano
24 Revision 1.4 2005/11/20 10:12:37 byordano
25 comments added to AliShuttleTrigger
31 // This class is to deal with DAQ LogBook and DAQ "end of run" notification.
32 // It has severeal two modes:
33 // 1) syncrhnized - Collect(), CollectNew() and CollectAll methods
34 // 2) asynchronized - Run() - starts listening for DAQ "end of run"
35 // notification by DIM service.
38 #include "AliShuttleTrigger.h"
40 #include <TSQLServer.h>
41 #include <TSQLResult.h>
43 #include <TObjArray.h>
47 #include "AliCDBManager.h"
48 #include "AliCDBStorage.h"
49 #include "AliCDBEntry.h"
51 #include "AliSimpleValue.h"
52 #include "AliShuttleConfig.h"
53 #include "AliShuttle.h"
54 #include "DATENotifier.h"
56 ClassImp(TerminateSignalHandler)
58 //______________________________________________________________________________________________
59 Bool_t TerminateSignalHandler::Notify() {
61 AliInfo("Terminate signal received ...");
62 fTrigger->Terminate();
67 ClassImp(AliShuttleTrigger)
69 //______________________________________________________________________________________________
70 AliShuttleTrigger::AliShuttleTrigger(const AliShuttleConfig* config,
71 UInt_t timeout, Int_t retries):
72 fConfig(config), fShuttle(NULL),
73 fNotified(kFALSE), fTerminate(kFALSE), fCondition(&fMutex),
74 fQuitSignalHandler(this, kSigQuit),
75 fInterruptSignalHandler(this, kSigInterrupt)
78 // config - pointer to the AliShuttleConfig object which represents
80 // mainStorage - pointer to AliCDBStorage for the undelying CDBStorage
81 // localStorage (local) CDB storage to be used if mainStorage is unavailable
84 fShuttle = new AliShuttle(config, timeout, retries);
86 gSystem->AddSignalHandler(&fQuitSignalHandler);
87 gSystem->AddSignalHandler(&fInterruptSignalHandler);
90 //______________________________________________________________________________________________
91 AliShuttleTrigger::~AliShuttleTrigger() {
93 gSystem->RemoveSignalHandler(&fQuitSignalHandler);
94 gSystem->RemoveSignalHandler(&fInterruptSignalHandler);
99 //______________________________________________________________________________________________
100 Bool_t AliShuttleTrigger::Notify() {
102 // Trigger CollectNew() methods in asynchronized (listen) mode.
103 // Usually called automaticly by DATENotifier on "end of run"
104 // notification event.
117 //______________________________________________________________________________________________
118 void AliShuttleTrigger::Terminate() {
120 // Stop triggers listen mode and exist from Run()
121 // Usually called automaticly by TerminateSignalHandler.
128 //______________________________________________________________________________________________
129 void AliShuttleTrigger::Run() {
131 // AliShuttleTrigger main loop for asynchronized (listen) mode.
132 // It spawns DIM service listener and waits for DAQ "end of run"
133 // notification. Calls CollectNew() on notification.
138 DATENotifier* notifier = new DATENotifier(this, "/DATE/LOGBOOK/UPDATE");
144 while (!(fNotified || fTerminate)) {
153 AliInfo("Terminated.");
163 //______________________________________________________________________________________________
164 Bool_t AliShuttleTrigger::RetrieveDATEEntries(const char* whereClause,
165 TObjArray& entries, Int_t& lastRun) {
168 sqlQuery += "select run, time_start, time_end from logbook ";
169 sqlQuery += whereClause;
170 sqlQuery += " order by run";
173 TString logbookHost="mysql://";
174 logbookHost+=fConfig->GetDAQLogBookHost();
176 aServer = TSQLServer::Connect(logbookHost,
177 fConfig->GetDAQLogBookUser(),
178 fConfig->GetDAQLogBookPassword());
180 AliError("Can't establish connection to DAQ log book DB!");
184 aServer->GetTables("REFSYSLOG");
187 aResult = aServer->Query(sqlQuery);
189 AliError(Form("Can't execute query <%s>!", sqlQuery.Data()));
194 if (aResult->GetFieldCount() != 3) {
195 AliError("Invalid SQL result field number!");
204 while ((aRow = aResult->Next())) {
205 TString runString(aRow->GetField(0), aRow->GetFieldLength(0));
206 Int_t run = runString.Atoi();
208 TString startTimeString(aRow->GetField(1),
209 aRow->GetFieldLength(1));
210 Int_t startTime = startTimeString.Atoi();
212 AliWarning(Form("Zero StartTime for run <%d>!", run));
213 AliWarning("Going to skip this run!");
217 TString endTimeString(aRow->GetField(2),
218 aRow->GetFieldLength(2));
219 Int_t endTime = endTimeString.Atoi();
221 AliWarning(Form("Zero EndTime for run <%d>!", run));
222 AliWarning("Going to skip this run!");
226 if (startTime > endTime) {
227 AliWarning(Form("StartTime bigger than EndTime for run <%d>", run));
228 AliWarning("Going to skip this run!");
232 entries.AddLast(new DATEEntry(run, startTime, endTime));
249 //______________________________________________________________________________________________
250 Bool_t AliShuttleTrigger::RetrieveConditionsData(const TObjArray& dateEntries) {
252 Bool_t hasError = kFALSE;
254 TIter iter(&dateEntries);
256 while ((anEntry = (DATEEntry*) iter.Next())) {
257 if(!fShuttle->Process(anEntry->GetRun(),
258 anEntry->GetStartTime(),
259 anEntry->GetEndTime())) {
267 //______________________________________________________________________________________________
268 Bool_t AliShuttleTrigger::Collect(Int_t run) {
270 // Collects conditions date for the given run.
273 AliInfo(Form("Collecting conditions data for run <%d> ...", run));
275 TString whereClause("where run = ");
279 TObjArray dateEntries;
280 if (!RetrieveDATEEntries(whereClause, dateEntries, lastRun)) {
281 AliError("Can't retrieve entries from DAQ log book.");
285 if (!dateEntries.GetEntriesFast()) {
286 AliError(Form("There isn't entry for run <%d> in DAQ log book!",
291 if (dateEntries.GetEntriesFast() > 1) {
292 AliError(Form("There is more than one entry for run <%d> in DAQ log book", run));
296 if (!RetrieveConditionsData(dateEntries)) {
297 AliError("An error occured during conditions data retrieval!");
304 //______________________________________________________________________________________________
305 Bool_t AliShuttleTrigger::CollectNew() {
307 // Collects conditions data for all new run written to DAQ LogBook.
310 AliInfo("Collecting conditions data for new runs ...");
314 AliCDBEntry* cdbEntry = AliCDBManager::Instance()->GetStorage(AliShuttle::GetLocalURI())
315 ->Get("/SHUTTLE/SYSTEM/LASTRUN", 0);
317 TObject* anObject = cdbEntry->GetObject();
318 if (anObject == NULL ||
319 anObject->IsA() != AliSimpleValue::Class()) {
320 AliError("Invalid last run object stored to CDB!");
323 AliSimpleValue* simpleValue = (AliSimpleValue*) anObject;
324 lastRun = simpleValue->GetInt();
327 AliWarning("There isn't last run stored! Starting from run 21200");
331 AliInfo(Form("Last run number <%d>", lastRun));
333 TString whereClause("where run > ");
334 whereClause += lastRun;
337 TObjArray dateEntries;
338 if (!RetrieveDATEEntries(whereClause, dateEntries, newLastRun)) {
339 AliError("Can't retrieve entries from DAQ log book.");
343 if (newLastRun > lastRun) {
344 AliSimpleValue lastRunObj(newLastRun);
345 AliCDBMetaData metaData;
346 AliCDBId cdbID(AliCDBPath("SHUTTLE", "SYSTEM", "LASTRUN"), 0, 0);
348 UInt_t result = AliCDBManager::Instance()->GetStorage(AliShuttle::GetLocalURI())
349 ->Put(&lastRunObj, cdbID, &metaData);
352 AliError("Can't store last run to CDB!");
357 if (!RetrieveConditionsData(dateEntries)) {
358 AliError("An error occured during conditions data retrieval!");
365 //______________________________________________________________________________________________
366 Bool_t AliShuttleTrigger::CollectAll() {
368 // Collects conditions data for all run written in DAQ LogBook.
371 AliInfo("Collecting conditions data for all runs ...");
374 TObjArray dateEntries;
375 if (!RetrieveDATEEntries("", dateEntries, lastRun)) {
376 AliError("Can't retrieve entries from DAQ log book.");
380 if (!RetrieveConditionsData(dateEntries)) {
381 AliError("An error occured during conditions data retrieval!");