]> git.uio.no Git - u/mrichter/AliRoot.git/blame - SHUTTLE/AliShuttleTrigger.cxx
removing dynamic types
[u/mrichter/AliRoot.git] / SHUTTLE / AliShuttleTrigger.cxx
CommitLineData
d477ad88 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$
57f50b3c 18 Revision 1.5 2006/07/10 13:01:41 jgrosseo
19 enhanced storing of last sucessfully processed run (alberto)
20
a7160fe9 21 Revision 1.4 2006/07/04 14:59:57 jgrosseo
22 revision of AliDCSValue: Removed wrapper classes, reduced storage size per value by factor 2
23
45a493ce 24 Revision 1.3 2006/06/12 09:11:16 jgrosseo
25 coding conventions (Alberto)
26
58bc3020 27 Revision 1.2 2006/06/06 14:26:40 jgrosseo
28 o) removed files that were moved to STEER
29 o) shuttle updated to follow the new interface (Alberto)
30
b948db8d 31 Revision 1.1 2006/03/07 07:52:34 hristov
32 New version (B.Yordanov)
33
d477ad88 34 Revision 1.5 2005/11/21 09:03:48 byordano
35 one more print added
36
37 Revision 1.4 2005/11/20 10:12:37 byordano
38 comments added to AliShuttleTrigger
39
40 */
41
42
43//
44// This class is to deal with DAQ LogBook and DAQ "end of run" notification.
45// It has severeal two modes:
46// 1) syncrhnized - Collect(), CollectNew() and CollectAll methods
b948db8d 47// 2) asynchronized - Run() - starts listening for DAQ "end of run"
d477ad88 48// notification by DIM service.
49//
50
51#include "AliShuttleTrigger.h"
52
53#include <TSQLServer.h>
54#include <TSQLResult.h>
55#include <TSQLRow.h>
56#include <TObjArray.h>
57#include <TSystem.h>
58
59#include "AliLog.h"
b948db8d 60#include "AliCDBManager.h"
d477ad88 61#include "AliCDBStorage.h"
62#include "AliCDBEntry.h"
63
45a493ce 64#include "AliDCSValue.h"
d477ad88 65#include "AliShuttleConfig.h"
66#include "AliShuttle.h"
67#include "DATENotifier.h"
68
69ClassImp(TerminateSignalHandler)
b948db8d 70
58bc3020 71//______________________________________________________________________
72TerminateSignalHandler::TerminateSignalHandler(const TerminateSignalHandler& /*other*/):
73TSignalHandler()
74{
75// copy constructor (not implemented)
76
77}
78
79//______________________________________________________________________
80TerminateSignalHandler &TerminateSignalHandler::operator=(const TerminateSignalHandler& /*other*/)
81{
82// assignment operator (not implemented)
83
84return *this;
85}
86
b948db8d 87//______________________________________________________________________________________________
58bc3020 88Bool_t TerminateSignalHandler::Notify()
89{
90// Sentd terminate command to the Shuttle trigger
d477ad88 91
92 AliInfo("Terminate signal received ...");
93 fTrigger->Terminate();
94
95 return kTRUE;
96}
97
58bc3020 98//______________________________________________________________________________________________
99//______________________________________________________________________________________________
100
d477ad88 101ClassImp(AliShuttleTrigger)
b948db8d 102
103//______________________________________________________________________________________________
d477ad88 104AliShuttleTrigger::AliShuttleTrigger(const AliShuttleConfig* config,
b948db8d 105 UInt_t timeout, Int_t retries):
106 fConfig(config), fShuttle(NULL),
d477ad88 107 fNotified(kFALSE), fTerminate(kFALSE), fCondition(&fMutex),
108 fQuitSignalHandler(this, kSigQuit),
109 fInterruptSignalHandler(this, kSigInterrupt)
110{
111 //
112 // config - pointer to the AliShuttleConfig object which represents
113 // the configuration
b948db8d 114 // mainStorage - pointer to AliCDBStorage for the undelying CDBStorage
115 // localStorage (local) CDB storage to be used if mainStorage is unavailable
d477ad88 116 //
117
b948db8d 118 fShuttle = new AliShuttle(config, timeout, retries);
d477ad88 119
120 gSystem->AddSignalHandler(&fQuitSignalHandler);
121 gSystem->AddSignalHandler(&fInterruptSignalHandler);
122}
123
58bc3020 124
125
126//______________________________________________________________________
127AliShuttleTrigger::AliShuttleTrigger(const AliShuttleTrigger& /*other*/):
128TObject()
129{
130// copy constructor (not implemented)
131
132}
133
134//______________________________________________________________________
135AliShuttleTrigger &AliShuttleTrigger::operator=(const AliShuttleTrigger& /*other*/)
136{
137// assignment operator (not implemented)
138
139return *this;
140}
141
142
143
144
145
b948db8d 146//______________________________________________________________________________________________
58bc3020 147AliShuttleTrigger::~AliShuttleTrigger()
148{
149// destructor
d477ad88 150
151 gSystem->RemoveSignalHandler(&fQuitSignalHandler);
152 gSystem->RemoveSignalHandler(&fInterruptSignalHandler);
153
154 delete fShuttle;
155}
156
b948db8d 157//______________________________________________________________________________________________
d477ad88 158Bool_t AliShuttleTrigger::Notify() {
159 //
160 // Trigger CollectNew() methods in asynchronized (listen) mode.
161 // Usually called automaticly by DATENotifier on "end of run"
162 // notification event.
163 //
164
165 fMutex.Lock();
166
167 fNotified = kTRUE;
168 fCondition.Signal();
169
170 fMutex.UnLock();
171
172 return kTRUE;
173}
174
b948db8d 175//______________________________________________________________________________________________
d477ad88 176void AliShuttleTrigger::Terminate() {
177 //
178 // Stop triggers listen mode and exist from Run()
179 // Usually called automaticly by TerminateSignalHandler.
180 //
181
182 fTerminate = kTRUE;
183 fCondition.Signal();
184}
185
b948db8d 186//______________________________________________________________________________________________
d477ad88 187void AliShuttleTrigger::Run() {
188 //
189 // AliShuttleTrigger main loop for asynchronized (listen) mode.
190 // It spawns DIM service listener and waits for DAQ "end of run"
191 // notification. Calls CollectNew() on notification.
192 //
193
194 fTerminate = kFALSE;
195
196 DATENotifier* notifier = new DATENotifier(this, "/DATE/LOGBOOK/UPDATE");
197
198 while (1) {
199
200 fMutex.Lock();
201
202 while (!(fNotified || fTerminate)) {
203 fCondition.Wait();
204 }
205
206 fNotified = kFALSE;
207
208 fMutex.UnLock();
209
210 if (fTerminate) {
211 AliInfo("Terminated.");
212 break;
213 }
214
215 CollectNew();
216 }
217
218 delete notifier;
219}
220
b948db8d 221//______________________________________________________________________________________________
d477ad88 222Bool_t AliShuttleTrigger::RetrieveDATEEntries(const char* whereClause,
a7160fe9 223 TObjArray& entries)
58bc3020 224{
225// Retrieve start time and end time for all runs in the DAQ logbook
226// that aren't processed yet
d477ad88 227
228 TString sqlQuery;
57f50b3c 229 sqlQuery = Form("select run, time_start, time_end from logbook %s order by run",
230 whereClause);
d477ad88 231
232 TSQLServer* aServer;
57f50b3c 233 TString logbookHost=Form("mysql://%s", fConfig->GetDAQlbHost());
234
b948db8d 235 aServer = TSQLServer::Connect(logbookHost,
57f50b3c 236 fConfig->GetDAQlbUser(),
237 fConfig->GetDAQlbPass());
d477ad88 238 if (!aServer) {
239 AliError("Can't establish connection to DAQ log book DB!");
240 return kFALSE;
241 }
57f50b3c 242
b948db8d 243 aServer->GetTables("REFSYSLOG");
d477ad88 244
245 TSQLResult* aResult;
246 aResult = aServer->Query(sqlQuery);
247 if (!aResult) {
248 AliError(Form("Can't execute query <%s>!", sqlQuery.Data()));
249 delete aServer;
250 return kFALSE;
251 }
252
253 if (aResult->GetFieldCount() != 3) {
254 AliError("Invalid SQL result field number!");
255 delete aResult;
256 delete aServer;
257 return kFALSE;
258 }
259
d477ad88 260 TSQLRow* aRow;
261 while ((aRow = aResult->Next())) {
262 TString runString(aRow->GetField(0), aRow->GetFieldLength(0));
263 Int_t run = runString.Atoi();
264
265 TString startTimeString(aRow->GetField(1),
266 aRow->GetFieldLength(1));
267 Int_t startTime = startTimeString.Atoi();
268 if (!startTime) {
269 AliWarning(Form("Zero StartTime for run <%d>!", run));
270 AliWarning("Going to skip this run!");
271 continue;
272 }
273
274 TString endTimeString(aRow->GetField(2),
275 aRow->GetFieldLength(2));
276 Int_t endTime = endTimeString.Atoi();
277 if (!endTime) {
278 AliWarning(Form("Zero EndTime for run <%d>!", run));
279 AliWarning("Going to skip this run!");
280 continue;
281 }
282
283 if (startTime > endTime) {
284 AliWarning(Form("StartTime bigger than EndTime for run <%d>", run));
285 AliWarning("Going to skip this run!");
286 continue;
287 }
288
58bc3020 289 entries.AddLast(new AliShuttleTriggerDATEEntry(run, startTime, endTime));
d477ad88 290 delete aRow;
291 }
292
293 delete aResult;
57f50b3c 294
a7160fe9 295 aServer->Close();
d477ad88 296 delete aServer;
297
298 entries.SetOwner(1);
299
300 return kTRUE;
301}
302
b948db8d 303//______________________________________________________________________________________________
a7160fe9 304Bool_t AliShuttleTrigger::RetrieveConditionsData(const TObjArray& dateEntries, Int_t &lastRun)
58bc3020 305{
306// Retrieve conditions data for all runs that aren't processed yet
d477ad88 307
308 Bool_t hasError = kFALSE;
309
310 TIter iter(&dateEntries);
58bc3020 311 AliShuttleTriggerDATEEntry* anEntry;
a7160fe9 312 lastRun=-1;
58bc3020 313 while ((anEntry = (AliShuttleTriggerDATEEntry*) iter.Next())) {
a7160fe9 314 Bool_t processError = kFALSE;
315 if(lastRun == -1) lastRun = anEntry->GetRun();
b948db8d 316 if(!fShuttle->Process(anEntry->GetRun(),
d477ad88 317 anEntry->GetStartTime(),
318 anEntry->GetEndTime())) {
a7160fe9 319 processError = kTRUE;
320 hasError = kTRUE;
b948db8d 321 }
57f50b3c 322 // Only the last SUCCESSFUL run must be stored!
323 if(!hasError && !processError) lastRun = anEntry->GetRun();
d477ad88 324 }
325
a7160fe9 326 return hasError == kFALSE;
d477ad88 327}
328
b948db8d 329//______________________________________________________________________________________________
a7160fe9 330Bool_t AliShuttleTrigger::Collect(Int_t run)
58bc3020 331{
d477ad88 332 //
333 // Collects conditions date for the given run.
334 //
335
336 AliInfo(Form("Collecting conditions data for run <%d> ...", run));
337
338 TString whereClause("where run = ");
339 whereClause += run;
340
d477ad88 341 TObjArray dateEntries;
a7160fe9 342 if (!RetrieveDATEEntries(whereClause, dateEntries)) {
d477ad88 343 AliError("Can't retrieve entries from DAQ log book.");
344 return kFALSE;
345 }
346
347 if (!dateEntries.GetEntriesFast()) {
348 AliError(Form("There isn't entry for run <%d> in DAQ log book!",
349 run));
350 return kFALSE;
351 }
352
353 if (dateEntries.GetEntriesFast() > 1) {
354 AliError(Form("There is more than one entry for run <%d> in DAQ log book", run));
355 return kFALSE;
356 }
357
a7160fe9 358 Int_t lastRun;
359 if (!RetrieveConditionsData(dateEntries, lastRun)) {
d477ad88 360 AliError("An error occured during conditions data retrieval!");
361 return kFALSE;
362 }
363
364 return kTRUE;
365}
366
b948db8d 367//______________________________________________________________________________________________
58bc3020 368Bool_t AliShuttleTrigger::CollectNew()
369{
d477ad88 370 //
371 // Collects conditions data for all new run written to DAQ LogBook.
372 //
373
45a493ce 374 // TODO revise this! last run number is ONLY allowed to be written when run was processed successfully!!!
375
d477ad88 376 AliInfo("Collecting conditions data for new runs ...");
377
378 Int_t lastRun;
379
b948db8d 380 AliCDBEntry* cdbEntry = AliCDBManager::Instance()->GetStorage(AliShuttle::GetLocalURI())
381 ->Get("/SHUTTLE/SYSTEM/LASTRUN", 0);
d477ad88 382 if (cdbEntry) {
383 TObject* anObject = cdbEntry->GetObject();
b948db8d 384 if (anObject == NULL ||
45a493ce 385 anObject->IsA() != AliDCSValue::Class()) {
d477ad88 386 AliError("Invalid last run object stored to CDB!");
387 return kFALSE;
388 }
45a493ce 389 AliDCSValue* simpleValue = (AliDCSValue*) anObject;
d477ad88 390 lastRun = simpleValue->GetInt();
a7160fe9 391 AliInfo(Form("Last run successfully stored: %d",lastRun));
d477ad88 392 delete cdbEntry;
393 } else {
a7160fe9 394 AliWarning("There isn't last run stored! Starting from run 21240");
395 lastRun = 21240; // TODO maybe exit here
d477ad88 396 }
397
398 AliInfo(Form("Last run number <%d>", lastRun));
399
400 TString whereClause("where run > ");
401 whereClause += lastRun;
402
403 Int_t newLastRun;
404 TObjArray dateEntries;
a7160fe9 405 if (!RetrieveDATEEntries(whereClause, dateEntries)) {
d477ad88 406 AliError("Can't retrieve entries from DAQ log book.");
407 return kFALSE;
408 }
409
a7160fe9 410 if (!RetrieveConditionsData(dateEntries, newLastRun)) {
411 AliError("Process of at least one run failed!");
412 // return kFALSE;
413 }
414
d477ad88 415 if (newLastRun > lastRun) {
45a493ce 416 AliDCSValue lastRunObj(newLastRun, 0);
d477ad88 417 AliCDBMetaData metaData;
418 AliCDBId cdbID(AliCDBPath("SHUTTLE", "SYSTEM", "LASTRUN"), 0, 0);
419
b948db8d 420 UInt_t result = AliCDBManager::Instance()->GetStorage(AliShuttle::GetLocalURI())
421 ->Put(&lastRunObj, cdbID, &metaData);
422
423 if (!result) {
d477ad88 424 AliError("Can't store last run to CDB!");
425 return kFALSE;
426 }
427 }
428
a7160fe9 429
d477ad88 430 return kTRUE;
431}
432
b948db8d 433//______________________________________________________________________________________________
58bc3020 434Bool_t AliShuttleTrigger::CollectAll()
435{
d477ad88 436 //
437 // Collects conditions data for all run written in DAQ LogBook.
438 //
439
440 AliInfo("Collecting conditions data for all runs ...");
441
442 Int_t lastRun;
443 TObjArray dateEntries;
a7160fe9 444 if (!RetrieveDATEEntries("", dateEntries)) {
d477ad88 445 AliError("Can't retrieve entries from DAQ log book.");
446 return kFALSE;
447 }
448
a7160fe9 449 if (!RetrieveConditionsData(dateEntries, lastRun)) {
d477ad88 450 AliError("An error occured during conditions data retrieval!");
451 return kFALSE;
452 }
453
454 return kTRUE;
455}
456