Include dir structure in tarball for cvmfs
[u/mrichter/AliRoot.git] / STEER / CDB / AliCDBGrid.cxx
CommitLineData
9e1ceb13 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// //
18// AliCDBGrid //
19// access class to a DataBase in an AliEn storage //
20// //
21/////////////////////////////////////////////////////////////////////////////////////////////////
22
b09247a2 23#include <cstdlib>
9e1ceb13 24#include <TGrid.h>
25#include <TGridResult.h>
26#include <TFile.h>
6dc56e97 27#include <TKey.h>
9e1ceb13 28#include <TROOT.h>
4667c116 29#include <TList.h>
9e1ceb13 30#include <TObjArray.h>
31#include <TObjString.h>
89004e35 32#include <TMath.h>
9e1ceb13 33#include <TRegexp.h>
34
35#include "AliLog.h"
fdf65bb5 36#include "AliCDBEntry.h"
9e1ceb13 37#include "AliCDBGrid.h"
4667c116 38#include "AliCDBManager.h"
9e1ceb13 39
40
41ClassImp(AliCDBGrid)
42
43//_____________________________________________________________________________
c75fabed 44AliCDBGrid::AliCDBGrid(const char *gridUrl, const char *user, const char *dbFolder,
5078a13f 45 const char *se, const char* cacheFolder, Bool_t operateDisconnected,
46 Long64_t cacheSize, Long_t cleanupInterval) :
47 AliCDBStorage(),
48 fGridUrl(gridUrl),
49 fUser(user),
50 fDBFolder(dbFolder),
51 fSE(se),
52 fMirrorSEs(""),
53 fCacheFolder(cacheFolder),
54 fOperateDisconnected(operateDisconnected),
55 fCacheSize(cacheSize),
56 fCleanupInterval(cleanupInterval)
9e1ceb13 57{
58// constructor //
59
5078a13f 60 // if the same Grid is alreay active, skip connection
61 if (!gGrid || fGridUrl != gGrid->GridUrl()
62 || (( fUser != "" ) && ( fUser != gGrid->GetUser() )) ) {
63 // connection to the Grid
64 AliInfo("Connection to the Grid...");
65 if(gGrid){
66 AliInfo(Form("gGrid = %p; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl()));
67 AliInfo(Form("fUser = %s; gGrid->GetUser() = %s",fUser.Data(), gGrid->GetUser()));
68 }
69 TGrid::Connect(fGridUrl.Data(),fUser.Data());
70 }
71
72 if(!gGrid) {
73 AliError("Connection failed!");
74 return;
75 }
76
77 TString initDir(gGrid->Pwd(0));
78 if (fDBFolder[0] != '/') {
79 fDBFolder.Prepend(initDir);
80 }
81
82 // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
83 if(!gGrid->Cd(fDBFolder.Data(),0)){
84 AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data()));
85 TGridResult* res = gGrid->Command(Form("mkdir -p %s",fDBFolder.Data()));
86 TString result = res->GetKey(0,"__result__");
87 if(result == "0"){
88 AliFatal(Form("Cannot create folder <%s> !",fDBFolder.Data()));
89 return;
90 }
91 } else {
92 AliDebug(2,Form("Folder <%s> found",fDBFolder.Data()));
93 }
94
95 // removes any '/' at the end of path, then append one '/'
96 while(fDBFolder.EndsWith("/")) fDBFolder.Remove(fDBFolder.Last('/'));
97 fDBFolder+="/";
98
99 fType="alien";
100 fBaseFolder = fDBFolder;
101
102 // Setting the cache
103
104 // Check if local cache folder is already defined
105 TString origCache(TFile::GetCacheFileDir());
106 if(fCacheFolder.Length() > 0) {
107 if(origCache.Length() == 0) {
108 AliInfo(Form("Setting local cache to: %s", fCacheFolder.Data()));
109 } else if(fCacheFolder != origCache) {
110 AliWarning(Form("Local cache folder was already defined, changing it to: %s",
111 fCacheFolder.Data()));
112 }
113
114 // default settings are: operateDisconnected=kTRUE, forceCacheread = kFALSE
115 if(!TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected)) {
116 AliError(Form("Could not set cache folder %s !", fCacheFolder.Data()));
117 fCacheFolder = "";
118 } else {
119 // reset fCacheFolder because the function may have
120 // slightly changed the folder name (e.g. '/' added)
121 fCacheFolder = TFile::GetCacheFileDir();
122 }
123
124 // default settings are: cacheSize=1GB, cleanupInterval = 0
125 if(!TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval)) {
126 AliError(Form("Could not set following values "
127 "to ShrinkCacheFileDir: cacheSize = %lld, cleanupInterval = %ld !",
128 fCacheSize, fCleanupInterval));
129 }
130 }
131
132 // return to the initial directory
133 gGrid->Cd(initDir.Data(),0);
134
135 fNretry = 3; // default
136 fInitRetrySeconds = 5; // default
9e1ceb13 137}
138
139//_____________________________________________________________________________
5078a13f 140AliCDBGrid::~AliCDBGrid() {
9e1ceb13 141// destructor
5078a13f 142 delete gGrid; gGrid=0;
9e1ceb13 143
144}
145
146//_____________________________________________________________________________
62032124 147Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) {
4005d0b5 148// build AliCDBId from full path filename (fDBFolder/path/Run#x_#y_v#z_s0.root)
62032124 149
5078a13f 150 if(filename.Contains(fDBFolder)){
151 filename = filename(fDBFolder.Length(),filename.Length()-fDBFolder.Length());
152 }
62032124 153
5078a13f 154 TString idPath = filename(0,filename.Last('/'));
155 id.SetPath(idPath);
156 if(!id.IsValid()) return kFALSE;
62032124 157
5078a13f 158 filename=filename(idPath.Length()+1,filename.Length()-idPath.Length());
9e1ceb13 159
5078a13f 160 Ssiz_t mSize;
161 // valid filename: Run#firstRun_#lastRun_v#version_s0.root
162 TRegexp keyPattern("^Run[0-9]+_[0-9]+_v[0-9]+_s0.root$");
163 keyPattern.Index(filename, &mSize);
164 if (!mSize) {
4005d0b5 165
5078a13f 166 // TODO backward compatibility ... maybe remove later!
167 Ssiz_t oldmSize;
168 TRegexp oldKeyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
169 oldKeyPattern.Index(filename, &oldmSize);
170 if(!oldmSize) {
171 AliDebug(2,Form("Bad filename <%s>.", filename.Data()));
172 return kFALSE;
173 } else {
174 AliDebug(2,Form("Old filename format <%s>.", filename.Data()));
175 id.SetSubVersion(-11); // TODO trick to ensure backward compatibility
176 }
4005d0b5 177
5078a13f 178 } else {
179 id.SetSubVersion(-1); // TODO trick to ensure backward compatibility
180 }
9e1ceb13 181
5078a13f 182 filename.Resize(filename.Length() - sizeof(".root") + 1);
9e1ceb13 183
5078a13f 184 TObjArray* strArray = (TObjArray*) filename.Tokenize("_");
9e1ceb13 185
5078a13f 186 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
187 id.SetFirstRun(atoi(firstRunString.Data() + 3));
188 id.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
62032124 189
5078a13f 190 TString verString(((TObjString*) strArray->At(2))->GetString());
191 id.SetVersion(atoi(verString.Data() + 1));
9e1ceb13 192
5078a13f 193 delete strArray;
9e1ceb13 194
5078a13f 195 return kTRUE;
9e1ceb13 196}
197
198//_____________________________________________________________________________
c3a7b59a 199Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) const {
62032124 200// build file name from AliCDBId (path, run range, version) and fDBFolder
9e1ceb13 201
5078a13f 202 if (!id.GetAliCDBRunRange().IsValid()) {
203 AliDebug(2,Form("Invalid run range <%d, %d>.",
204 id.GetFirstRun(), id.GetLastRun()));
205 return kFALSE;
206 }
9e1ceb13 207
5078a13f 208 if (id.GetVersion() < 0) {
209 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
210 return kFALSE;
211 }
62032124 212
5078a13f 213 filename = Form("Run%d_%d_v%d",
214 id.GetFirstRun(),
215 id.GetLastRun(),
216 id.GetVersion());
62032124 217
5078a13f 218 if (id.GetSubVersion() != -11) filename += "_s0"; // TODO to ensure backward compatibility
219 filename += ".root";
4005d0b5 220
5078a13f 221 filename.Prepend(fDBFolder + id.GetPath() + '/');
9e1ceb13 222
5078a13f 223 return kTRUE;
9e1ceb13 224}
225
226//_____________________________________________________________________________
5bfe3965 227void AliCDBGrid::SetRetry(Int_t nretry, Int_t initsec) {
89004e35 228
5078a13f 229 // Function to set the exponential retry for putting entries in the OCDB
89004e35 230
5078a13f 231 AliWarning("WARNING!!! You are changing the exponential retry times and delay: this function should be used by experts!");
232 fNretry = nretry;
233 fInitRetrySeconds = initsec;
234 AliDebug(2,Form("fNretry = %d, fInitRetrySeconds = %d", fNretry, fInitRetrySeconds));
89004e35 235}
236
237
238//_____________________________________________________________________________
9e1ceb13 239Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
240// prepare id (version) of the object that will be stored (called by PutEntry)
241
5078a13f 242 TString initDir(gGrid->Pwd(0));
243
244 TString dirName(fDBFolder);
245
246 Bool_t dirExist=kFALSE;
247
248
249
250 // go to the path; if directory does not exist, create it
251 for(int i=0;i<3;i++){
252 //TString cmd("find -d ");
253 //cmd += Form("%s ",dirName);
254 //cmd +=
255 //gGrid->Command(cmd.Data());
256 dirName+=Form("%s/",id.GetPathLevel(i).Data());
257 dirExist=gGrid->Cd(dirName,0);
258 if (!dirExist) {
259 AliDebug(2,Form("Creating new folder <%s> ...",dirName.Data()));
260 if(!gGrid->Mkdir(dirName,"",0)){
261 AliError(Form("Cannot create directory <%s> !",dirName.Data()));
262 gGrid->Cd(initDir.Data());
263 return kFALSE;
264 }
265
266 // if folders are new add tags to them
267 if(i == 1) {
268 // TODO Currently disabled
269 // add short lived tag!
270 // AliInfo("Tagging level 1 folder with \"ShortLived\" tag");
271 // if(!AddTag(dirName,"ShortLived_try")){
272 // AliError(Form("Could not tag folder %s !", dirName.Data()));
273 // if(!gGrid->Rmdir(dirName.Data())){
274 // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
275 // }
276 // return 0;
277 //}
278
279 } else if(i == 2) {
280 AliDebug(2,"Tagging level 2 folder with \"CDB\" and \"CDB_MD\" tag");
281 if(!AddTag(dirName,"CDB")){
282 AliError(Form("Could not tag folder %s !", dirName.Data()));
283 if(!gGrid->Rmdir(dirName.Data())){
284 AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
285 }
286 return 0;
287 }
288 if(!AddTag(dirName,"CDB_MD")){
289 AliError(Form("Could not tag folder %s !", dirName.Data()));
290 if(!gGrid->Rmdir(dirName.Data())){
291 AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
292 }
293 return 0;
294 }
295
296 // TODO Currently disabled
297 // add short lived tag!
298 // TString path=id.GetPath();
299 // if(AliCDBManager::Instance()->IsShortLived(path.Data())) {
300 // AliInfo(Form("Tagging %s as short lived", dirName.Data()));
301 // if(!TagShortLived(dirName, kTRUE)){
302 // AliError(Form("Could not tag folder %s !", dirName.Data()));
303 // if(!gGrid->Rmdir(dirName.Data())){
304 // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
305 // }
306 // return 0;
307 // }
308 // } else {
309 // AliInfo(Form("Tagging %s as long lived", dirName.Data()));
310 // if(!TagShortLived(dirName, kFALSE)){
311 // AliError(Form("Could not tag folder %s !", dirName.Data()));
312 // if(!gGrid->Rmdir(dirName.Data())){
313 // AliError(Form("Unexpected: could not remove %s directory!", dirName.Data()));
314 // }
315 // return 0;
316 // }
317 // }
318 }
319 }
320 }
321 gGrid->Cd(initDir,0);
322
323 TString filename;
324 AliCDBId anId; // the id got from filename
325 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
326 Int_t lastVersion=0; // highest version found
327
328 TGridResult *res = gGrid->Ls(dirName);
329
330 //loop on the files in the directory, look for highest version
331 for(int i=0; i < res->GetEntries(); i++){
332 filename=res->GetFileNamePath(i);
333 if (!FilenameToId(filename, anId)) continue;
334 if (anId.GetAliCDBRunRange().Overlaps(id.GetAliCDBRunRange()) && anId.GetVersion() > lastVersion) {
335 lastVersion = anId.GetVersion();
336 lastRunRange = anId.GetAliCDBRunRange();
337 }
338
339 }
340 delete res;
341
342 // GRP entries with explicitly set version escape default incremental versioning
343 if(id.GetPath().Contains("GRP") && id.HasVersion() && lastVersion!=0)
344 {
345 AliDebug(5,Form("Entry %s won't be put in the destination OCDB", id.ToString().Data()));
346 return kFALSE;
347 }
348
349 id.SetVersion(lastVersion + 1);
350 id.SetSubVersion(0);
351
352 TString lastStorage = id.GetLastStorage();
353 if(lastStorage.Contains(TString("new"), TString::kIgnoreCase) && id.GetVersion() > 1 ){
354 AliDebug(2, Form("A NEW object is being stored with version %d",
355 id.GetVersion()));
356 AliDebug(2, Form("and it will hide previously stored object with version %d!",
357 id.GetVersion()-1));
358 }
359
360 if(!lastRunRange.IsAnyRange() && !(lastRunRange.IsEqual(&id.GetAliCDBRunRange())))
361 AliWarning(Form("Run range modified w.r.t. previous version (Run%d_%d_v%d)",
362 lastRunRange.GetFirstRun(), lastRunRange.GetLastRun(), id.GetVersion()));
363
364 return kTRUE;
9e1ceb13 365}
366
367//_____________________________________________________________________________
6dc56e97 368AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query) {
62032124 369// look for the Id that matches query's requests (highest or exact version)
9e1ceb13 370
5078a13f 371 if(validFileIds.GetEntriesFast() < 1)
372 return NULL;
373
374 TIter iter(&validFileIds);
375
376 AliCDBId *anIdPtr=0;
377 AliCDBId* result=0;
378
379 while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
380 if(anIdPtr->GetPath() != query.GetPath()) continue;
381
382 //if(!CheckVersion(query, anIdPtr, result)) return NULL;
383
384 if (!query.HasVersion()){ // look for highest version
385 if(result && result->GetVersion() > anIdPtr->GetVersion()) continue;
386 if(result && result->GetVersion() == anIdPtr->GetVersion()) {
387 AliError(Form("More than one object valid for run %d, version %d!",
388 query.GetFirstRun(), anIdPtr->GetVersion()));
389 return NULL;
390 }
27d2c5d1 391 result = new AliCDBId(*anIdPtr);
5078a13f 392 } else { // look for specified version
393 if(query.GetVersion() != anIdPtr->GetVersion()) continue;
394 if(result && result->GetVersion() == anIdPtr->GetVersion()){
395 AliError(Form("More than one object valid for run %d, version %d!",
396 query.GetFirstRun(), anIdPtr->GetVersion()));
397 return NULL;
398 }
27d2c5d1 399 result = new AliCDBId(*anIdPtr);
5078a13f 400 }
401
402 }
403
27d2c5d1 404 return result;
62032124 405}
406
9e1ceb13 407//_____________________________________________________________________________
4667c116 408AliCDBId* AliCDBGrid::GetEntryId(const AliCDBId& queryId) {
409// get AliCDBId from the database
410// User must delete returned object
9e1ceb13 411
5078a13f 412 AliCDBId* dataId=0;
413
414 AliCDBId selectedId(queryId);
415 if (!selectedId.HasVersion()) {
416 // if version is not specified, first check the selection criteria list
417 GetSelection(&selectedId);
418 }
419
420 TObjArray validFileIds;
421 validFileIds.SetOwner(1);
422
423 // look for file matching query requests (path, runRange, version)
424 if(selectedId.GetFirstRun() == fRun && fPathFilter.Comprises(selectedId.GetAliCDBPath()) &&
425 fVersion == selectedId.GetVersion() && !fMetaDataFilter){
426 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
427 AliDebug(2, Form("List of files valid for run %d was loaded. Looking there for fileids valid for path %s!",
428 selectedId.GetFirstRun(), selectedId.GetPath().Data()));
429 dataId = GetId(fValidFileIds, selectedId);
430
431 } else {
432 // List of files valid for reqested run was not loaded. Looking directly into CDB
433 AliDebug(2, Form("List of files valid for run %d and version %d was not loaded. Looking directly into CDB for fileids valid for path %s!",
434 selectedId.GetFirstRun(), selectedId.GetVersion(), selectedId.GetPath().Data()));
435
436 TString filter;
437 MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(), 0, filter);
438
439 TString pattern = ".root";
440 TString optionQuery = "-y -m";
441 if(selectedId.GetVersion() >= 0) {
442 pattern.Prepend(Form("_v%d_s0",selectedId.GetVersion()));
443 optionQuery = "";
444 }
445
446 TString folderCopy(Form("%s%s/Run",fDBFolder.Data(),selectedId.GetPath().Data()));
447
448 if (optionQuery.Contains("-y")){
449 AliInfo("Only latest version will be returned");
450 }
451
452 AliDebug(2,Form("** fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
453 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());
454 if (res) {
5078a13f 455 for(int i=0; i<res->GetEntries(); i++){
27d2c5d1 456 AliCDBId *validFileId = new AliCDBId();
5078a13f 457 TString filename = res->GetKey(i, "lfn");
458 if(filename == "") continue;
27d2c5d1 459 if(FilenameToId(filename, *validFileId))
460 validFileIds.AddLast(validFileId);
5078a13f 461 }
462 delete res;
463 }else{
464 return 0; // this should be only in case of file catalogue glitch
465 }
466
467 dataId = GetId(validFileIds, selectedId);
468 }
469
470 return dataId;
4667c116 471}
472
473//_____________________________________________________________________________
474AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
475// get AliCDBEntry from the database
476
5078a13f 477 AliCDBId* dataId = GetEntryId(queryId);
4667c116 478
5078a13f 479 if (!dataId){
480 AliFatal(TString::Format("No valid CDB object found! request was: %s", queryId.ToString().Data()));
481 return NULL;
482 }
9e1ceb13 483
5078a13f 484 TString filename;
485 if (!IdToFilename(*dataId, filename)) {
486 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
487 delete dataId;
488 AliFatal(TString::Format("No valid CDB object found! request was: %s", queryId.ToString().Data()));
489 }
9e1ceb13 490
5078a13f 491 AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId);
62032124 492
5078a13f 493 delete dataId;
494 if(!anEntry)
495 AliFatal(TString::Format("No valid CDB object found! request was: %s", queryId.ToString().Data()));
e6608c2e 496
5078a13f 497 return anEntry;
62032124 498}
499
500//_____________________________________________________________________________
8e245d15 501AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, AliCDBId* dataId){
62032124 502// Get AliCBEntry object from file "filename"
9e1ceb13 503
5078a13f 504 AliDebug(2,Form("Opening file: %s",filename.Data()));
62032124 505
5078a13f 506 filename.Prepend("/alien");
c75fabed 507
5078a13f 508 // if option="CACHEREAD" TFile will use the local caching facility!
509 TString option="READ";
510 if(fCacheFolder != ""){
c75fabed 511
5078a13f 512 // Check if local cache folder was changed in the meanwhile
513 TString origCache(TFile::GetCacheFileDir());
514 if(fCacheFolder != origCache) {
515 AliWarning(Form("Local cache folder has been overwritten!! fCacheFolder = %s origCache = %s",
516 fCacheFolder.Data(), origCache.Data()));
517 TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected);
518 TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval);
519 }
c75fabed 520
5078a13f 521 option.Prepend("CACHE");
522 }
c75fabed 523
5078a13f 524 AliDebug(2, Form("Option: %s", option.Data()));
c75fabed 525
5078a13f 526 TFile *file = TFile::Open(filename, option);
527 if (!file) {
528 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
529 return NULL;
530 }
9e1ceb13 531
5078a13f 532 // get the only AliCDBEntry object from the file
533 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
9e1ceb13 534
5078a13f 535 AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file->Get("AliCDBEntry"));
9e1ceb13 536
5078a13f 537 if (!anEntry) {
538 AliDebug(2,Form("Bad storage data: file does not contain an AliCDBEntry object!"));
539 file->Close();
540 return NULL;
541 }
9e1ceb13 542
5078a13f 543 // The object's Id is not reset during storage
544 // If object's Id runRange or version do not match with filename,
545 // it means that someone renamed file by hand. In this case a warning msg is issued.
62032124 546
5078a13f 547 if(anEntry){
548 AliCDBId entryId = anEntry->GetId();
549 Int_t tmpSubVersion = dataId->GetSubVersion();
550 dataId->SetSubVersion(entryId.GetSubVersion()); // otherwise filename and id may mismatch
551 if(!entryId.IsEqual(dataId)){
552 AliWarning(Form("Mismatch between file name and object's Id!"));
553 AliWarning(Form("File name: %s", dataId->ToString().Data()));
554 AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
555 }
556 dataId->SetSubVersion(tmpSubVersion);
557 }
9e1ceb13 558
5078a13f 559 anEntry->SetLastStorage("grid");
62032124 560
5078a13f 561 // Check whether entry contains a TTree. In case load the tree in memory!
562 LoadTreeFromFile(anEntry);
145f45a2 563
5078a13f 564 // close file, return retieved entry
565 file->Close(); delete file; file=0;
62032124 566
5078a13f 567 return anEntry;
9e1ceb13 568}
569
570//_____________________________________________________________________________
62032124 571TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
9e1ceb13 572// multiple request (AliCDBStorage::GetAll)
573
5078a13f 574 TList* result = new TList();
575 result->SetOwner();
576
577 TObjArray validFileIds;
578 validFileIds.SetOwner(1);
579
580 Bool_t alreadyLoaded = kFALSE;
581
582 // look for file matching query requests (path, runRange)
583 if(queryId.GetFirstRun() == fRun &&
584 fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
585 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
586 AliDebug(2,Form("List of files valid for run %d and for path %s was loaded. Looking there!",
587 queryId.GetFirstRun(), queryId.GetPath().Data()));
588
589 alreadyLoaded = kTRUE;
590
591 } else {
592 // List of files valid for reqested run was not loaded. Looking directly into CDB
593 AliDebug(2,Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
594 queryId.GetFirstRun(), queryId.GetPath().Data()));
595
596 TString filter;
597 MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(), 0, filter);
598
599 TString path = queryId.GetPath();
600
601 TString pattern = "Run*.root";
602 TString optionQuery = "-y";
603
604 TString addFolder = "";
605 if (!path.Contains("*")){
606 if (!path.BeginsWith("/")) addFolder += "/";
607 addFolder += path;
608 }
609 else{
610 if (path.BeginsWith("/")) path.Remove(0,1);
611 if (path.EndsWith("/")) path.Remove(path.Length()-1,1);
612 TObjArray* tokenArr = path.Tokenize("/");
613 if (tokenArr->GetEntries() != 3) {
614 AliError("Not a 3 level path! Keeping old query...");
615 pattern.Prepend(path+"/");
616 }
617 else{
618 TString str0 = ((TObjString*)tokenArr->At(0))->String();
619 TString str1 = ((TObjString*)tokenArr->At(1))->String();
620 TString str2 = ((TObjString*)tokenArr->At(2))->String();
621 if (str0 != "*" && str1 != "*" && str2 == "*"){
622 // e.g. "ITS/Calib/*"
623 addFolder = "/"+str0+"/"+str1;
624 }
625 else if (str0 != "*" && str1 == "*" && str2 == "*"){
626 // e.g. "ITS/*/*"
627 addFolder = "/"+str0;
628 }
629 else if (str0 == "*" && str1 == "*" && str2 == "*"){
630 // e.g. "*/*/*"
631 // do nothing: addFolder is already an empty string;
632 }
633 else{
634 // e.g. "ITS/*/RecoParam"
635 pattern.Prepend(path+"/");
636 }
637 }
638 delete tokenArr; tokenArr=0;
639 }
640
641 TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
642
643 AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
644
645 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());
646
647 if (!res) {
648 AliError("Grid query failed");
649 return 0;
650 }
651
5078a13f 652 for(int i=0; i<res->GetEntries(); i++){
27d2c5d1 653 AliCDBId *validFileId = new AliCDBId();
5078a13f 654 TString filename = res->GetKey(i, "lfn");
655 if(filename == "") continue;
27d2c5d1 656 if(FilenameToId(filename, *validFileId))
657 validFileIds.AddLast(validFileId);
5078a13f 658 }
659 delete res;
660 }
661
662 TIter *iter=0;
663 if(alreadyLoaded){
664 iter = new TIter(&fValidFileIds);
665 } else {
666 iter = new TIter(&validFileIds);
667 }
668
669 TObjArray selectedIds;
670 selectedIds.SetOwner(1);
671
672 // loop on list of valid Ids to select the right version to get.
673 // According to query and to the selection criteria list, version can be the highest or exact
674 AliCDBPath pathCopy;
675 AliCDBId* anIdPtr=0;
676 AliCDBId* dataId=0;
677 AliCDBPath queryPath = queryId.GetAliCDBPath();
678 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
679 AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath();
680 if(!(queryPath.Comprises(thisCDBPath)) || pathCopy.GetPath() == thisCDBPath.GetPath()) continue;
681 pathCopy = thisCDBPath;
682
683 // check the selection criteria list for this query
684 AliCDBId thisId(*anIdPtr);
685 thisId.SetVersion(queryId.GetVersion());
686 if(!thisId.HasVersion()) GetSelection(&thisId);
687
688 if(alreadyLoaded){
689 dataId = GetId(fValidFileIds, thisId);
690 } else {
691 dataId = GetId(validFileIds, thisId);
692 }
693 if(dataId) selectedIds.Add(dataId);
694 }
695
696 delete iter; iter=0;
697
698 // selectedIds contains the Ids of the files matching all requests of query!
699 // All the objects are now ready to be retrieved
700 iter = new TIter(&selectedIds);
701 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
702 TString filename;
703 if (!IdToFilename(*anIdPtr, filename)) {
704 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
705 continue;
706 }
707
708 AliCDBEntry* anEntry = GetEntryFromFile(filename, anIdPtr);
709
710 if(anEntry) result->Add(anEntry);
711
712 }
713 delete iter; iter=0;
714
715 return result;
9e1ceb13 716}
717
718//_____________________________________________________________________________
06eeadea 719Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry, const char* mirrors) {
5078a13f 720// put an AliCDBEntry object into the database
721
722 AliCDBId& id = entry->GetId();
723
724 // set version for the entry to be stored
725 if (!PrepareId(id)) return kFALSE;
726
727 // build filename from entry's id
728 TString filename;
729 if (!IdToFilename(id, filename)) {
67522875 730 AliError("Bad ID encountered, cannot make a file name out of it!");
5078a13f 731 return kFALSE;
732 }
733
734 TString folderToTag = Form("%s%s",
735 fDBFolder.Data(),
736 id.GetPath().Data());
737
738 TDirectory* saveDir = gDirectory;
739
740 TString fullFilename = Form("/alien%s", filename.Data());
741 TString seMirrors(mirrors);
742 if(seMirrors.IsNull() || seMirrors.IsWhitespace()) seMirrors=GetMirrorSEs();
743 // specify SE to filename
744 // if a list of SEs was passed to this method or set via SetMirrorSEs, set the first as SE for opening the file.
745 // The other SEs will be used in cascade in case of failure in opening the file.
746 // The remaining SEs will be used to create replicas.
747 TObjArray *arraySEs = seMirrors.Tokenize(',');
748 Int_t nSEs = arraySEs->GetEntries();
749 Int_t remainingSEs = 1;
750 if(nSEs == 0){
751 if (fSE != "default") fullFilename += Form("?se=%s",fSE.Data());
752 }else{
753 remainingSEs = nSEs;
754 }
755
756 // open file
67522875 757 TFile *file = 0;
758 TFile *reopenedFile = 0;
5078a13f 759 AliDebug(2, Form("fNretry = %d, fInitRetrySeconds = %d",fNretry,fInitRetrySeconds));
760 TString targetSE("");
761
762 Bool_t result = kFALSE;
763 Bool_t reOpenResult = kFALSE;
764 Int_t reOpenAttempts=0;
67522875 765 while( !reOpenResult && reOpenAttempts<2 ) { //loop to check the file after closing it, to catch the unlikely but possible case when the file
5078a13f 766 // is cleaned up by alien just before closing as a consequence of a network disconnection while writing
767
67522875 768 while( !file && remainingSEs>0 ) {
5078a13f 769 if(nSEs!=0){
770 TObjString *target = (TObjString*) arraySEs->At(nSEs-remainingSEs);
771 targetSE=target->String();
772 if ( !(targetSE.BeginsWith("ALICE::") && targetSE.CountChar(':')==4) ) {
67522875 773 AliError( Form("\"%s\" is an invalid storage element identifier.",targetSE.Data()) );
5078a13f 774 continue;
775 }
67522875 776 if ( fullFilename.Contains('?')) fullFilename.Remove(fullFilename.Last('?') );
5078a13f 777 fullFilename += Form("?se=%s",targetSE.Data());
778 }
779 Int_t remainingAttempts=fNretry;
780 Int_t nsleep = fInitRetrySeconds; // number of seconds between attempts. We let it increase exponentially
781 AliDebug(2, Form("Uploading file into SE #%d: %s",nSEs-remainingSEs+1,targetSE.Data()));
782 while(remainingAttempts > 0) {
783 AliDebug(2, Form("Uploading file into OCDB at %s - Attempt #%d",targetSE.Data(),fNretry-remainingAttempts+1));
784 remainingAttempts--;
785 file = TFile::Open(fullFilename,"CREATE");
786 if(!file || !file->IsWritable()){
67522875 787 if(file) { // file is not writable
788 file->Close(); delete file; file=0;
789 }
5078a13f 790 TString message(TString::Format("Attempt %d failed.",fNretry-remainingAttempts));
791 if(remainingAttempts>0) {
792 message += " Sleeping for "; message += nsleep; message += " seconds";
793 }else{
794 if(remainingSEs>0) message += " Trying to upload at next SE";
795 }
796 AliDebug(2, message.Data());
797 if(remainingAttempts>0) sleep(nsleep);
798 }else{
799 remainingAttempts=0;
800 }
801 nsleep*=fInitRetrySeconds;
802 }
803 remainingSEs--;
804 }
805 if(!file){
806 AliError(Form("All %d attempts have failed on all %d SEs. Returning...",fNretry,nSEs));
807 return kFALSE;
808 }
809
810 file->cd();
811
812 //SetTreeToFile(entry, file);
813 entry->SetVersion(id.GetVersion());
814
815 // write object (key name: "AliCDBEntry")
816 result = (file->WriteTObject(entry, "AliCDBEntry") != 0);
5078a13f 817 file->Close();
67522875 818 if (!result) {
819 AliError(Form("Can't write entry to file <%s>!", filename.Data()));
820 } else {
5078a13f 821 AliDebug(2, Form("Reopening file %s for checking its correctness",fullFilename.Data()));
67522875 822 reopenedFile = TFile::Open(fullFilename.Data(),"READ");
823 if(!reopenedFile){
5078a13f 824 reOpenResult = kFALSE;
825 AliInfo(Form("The file %s was closed successfully but cannot be reopened. Trying now to regenerate it (regeneration attempt number %d)",
826 fullFilename.Data(),++reOpenAttempts));
827 delete file; file=0;
828 AliDebug(2, Form("Removing file %s", filename.Data()));
829 if(!gGrid->Rm(filename.Data()))
830 AliError("Can't delete file!");
831 remainingSEs++;
832 }else{
833 reOpenResult = kTRUE;
67522875 834 if ( ! AliCDBManager::Instance()->IsOCDBUploadMode() ) {
835 reopenedFile->Close();
836 delete reopenedFile; reopenedFile=0;
837 }
5078a13f 838 }
5078a13f 839 }
840 }
841
842 if (saveDir) saveDir->cd(); else gROOT->cd();
843 delete file; file=0;
844
845 if(result && reOpenResult) {
846
847 if(!TagFileId(filename, &id)){
848 AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data()));
849 if(!gGrid->Rm(filename.Data()))
850 AliError("Can't delete file!");
851 return kFALSE;
852 }
853
854 TagFileMetaData(filename, entry->GetMetaData());
855 }else{
67522875 856 AliError("The file could not be opened or the object could not be written.");
5078a13f 857 if(!gGrid->Rm(filename.Data()))
858 AliError("Can't delete file!");
859 return kFALSE;
860 }
861
862 AliInfo(Form("CDB object stored into file %s", filename.Data()));
863 if(nSEs==0)
864 AliInfo(Form("Storage Element: %s", fSE.Data()));
865 else
866 AliInfo(Form("Storage Element: %s", targetSE.Data()));
867
868 //In case of other SEs specified by the user, mirror the file to the remaining SEs
869 for(Int_t i=0; i<nSEs; i++){
870 if(i==nSEs-remainingSEs-1) continue; // skip mirroring to the SE where the file was saved
871 TString mirrorCmd("mirror ");
872 mirrorCmd += filename;
873 mirrorCmd += " ";
874 TObjString *target = (TObjString*) arraySEs->At(i);
875 TString mirrorSE(target->String());
876 mirrorCmd += mirrorSE;
877 AliDebug(5,Form("mirror command: \"%s\"",mirrorCmd.Data()));
878 AliInfo(Form("Mirroring to storage element: %s", mirrorSE.Data()));
879 gGrid->Command(mirrorCmd.Data());
880 }
881 arraySEs->Delete(); arraySEs=0;
882
67522875 883 if ( AliCDBManager::Instance()->IsOCDBUploadMode() ) { // if uploading to OCDBs, add to cvmfs too
884 if ( !filename.BeginsWith("/alice/data") && !filename.BeginsWith("/alice/simulation/2008/v4-15-Release") ) {
885 AliError ( Form ( "Cannot upload to CVMFS OCDBs a non official CDB object: \"%s\"!", filename.Data() ) );
886 } else {
887 if ( !PutInCvmfs( filename, reopenedFile) )
888 AliError( Form( "Could not upload AliEn file \"%s\" to CVMFS OCDB!", filename.Data() ) );
889 }
890 reopenedFile->Close();
891 delete reopenedFile; reopenedFile=0;
892 }
893
894 return kTRUE;
895}
896
897//_____________________________________________________________________________
7c1ed912 898Bool_t AliCDBGrid::PutInCvmfs( TString& filename, TFile* cdbFile ) const
899{
67522875 900 // Add the CDB object to cvmfs OCDB
901
7c1ed912 902 TString cvmfsFilename( filename );
903 // cvmfsFilename.Remove(TString::kTrailing, '/');
904 TString basename = ( cvmfsFilename( cvmfsFilename.Last( '/' ) + 1, cvmfsFilename.Length() ) );
905 TString cvmfsDirname = cvmfsFilename.Remove( cvmfsFilename.Last( '/' ), cvmfsFilename.Length() );
906 TRegexp threeLevelsRE( "[^/]+/[^/]+/[^/]+$" );
907 TString threeLevels = cvmfsDirname( threeLevelsRE );
67522875 908
909 TRegexp re_RawFolder("^/alice/data/20[0-9]+/OCDB");
910 TRegexp re_MCFolder("^/alice/simulation/2008/v4-15-Release");
911 TString rawFolder = cvmfsDirname(re_RawFolder);
912 TString mcFolder = cvmfsDirname(re_MCFolder);
913 if ( !rawFolder.IsNull() ) {
914 cvmfsDirname.Replace(0, 6, "/cvmfs/alice-ocdb.cern.ch/calibration");
915 } else if ( !mcFolder.IsNull() ){
916 cvmfsDirname.Replace(0,36,"/cvmfs/alice-ocdb.cern.ch/calibration/MC");
917 } else {
918 AliError(Form("OCDB folder set for an invalid OCDB storage:\n %s", cvmfsDirname.Data()));
919 return kFALSE;
920 }
7c1ed912 921 // now cvmfsDirname is the full dirname in cvmfs (trailing slash included)
67522875 922 AliDebug(3, Form("Publishing \"%s\" in \"%s\"", basename.Data(), cvmfsDirname.Data()));
923
7c1ed912 924 // Tar the file with the right prefix path. Include the directory structure in the tarball
925 // to cover the case of a containing directory being new in cvmfs.
926 TString firstLevel(threeLevels(0, threeLevels.First('/')));
927 gSystem->Exec( Form("rm -r %s", firstLevel.Data()) );
928 Int_t result = gSystem->Exec( Form("mkdir -p %s", threeLevels.Data()) );
929 if ( result != 0 ) {
930 AliError ( Form ( "Could not create the directory \"%s\"", threeLevels.Data() ) );
931 return kFALSE;
932 }
933 cdbFile->Cp(Form("%s/%s", threeLevels.Data(), basename.Data() ));
67522875 934 TString tarFileName("cdbObjectToAdd.tar.gz");
f5cea44a 935 // tarCommand should be e.g.: tar --transform 's,^,/cvmfs/alice-ocdb.cern.ch/calibration/data/2010/OCDB/,S' -cvzf objecttoadd.tar.gz basename
7c1ed912 936 result = gSystem->Exec ( Form( "tar --transform 's,^,%s/,S' -cvzf %s %s", cvmfsDirname.Data(), tarFileName.Data(), firstLevel.Data() ) );
67522875 937 if ( result != 0 ) {
938 AliError ( Form ( "Could not create the tarball for the object \"%s\"", filename.Data() ) );
939 return kFALSE;
940 }
941
7c1ed912 942 // Copy the file to cvmfs (requires to have the executable in the path and access to the server)
67522875 943 result = gSystem->Exec( Form( "ocdb-cvmfs %s", tarFileName.Data() ) );
944 if ( result != 0 ) {
945 AliError ( Form ( "Could not execute \"ocdb-cvmfs %s\"", filename.Data() ) );
946 return kFALSE;
947 }
948
949 // Remove the local file and the tar-file
7c1ed912 950 gSystem->Exec( Form( "rm -r %s", firstLevel.Data() ) );
67522875 951 gSystem->Exec( Form( "rm %s", tarFileName.Data() ) );
952
5078a13f 953 return kTRUE;
62032124 954}
c5941d67 955
62032124 956//_____________________________________________________________________________
957Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){
958// add "tagname" tag (CDB or CDB_MD) to folder where object will be stored
959
5078a13f 960 Bool_t result = kTRUE;
961 AliDebug(2, Form("adding %s tag to folder %s", tagname, folderToTag.Data()));
962 TString addTag = Form("addTag %s %s", folderToTag.Data(), tagname);
963 TGridResult *gridres = gGrid->Command(addTag.Data());
964 const char* resCode = gridres->GetKey(0,"__result__"); // '1' if success
965 if(resCode[0] != '1') {
966 AliError(Form("Couldn't add %s tags to folder %s !",
967 tagname, folderToTag.Data()));
968 result = kFALSE;
969 }
970 delete gridres;
971 return result;
9e1ceb13 972}
973
b05400be 974//_____________________________________________________________________________
c3a7b59a 975Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
62032124 976// tag stored object in CDB table using object Id's parameters
977
0e10c8fd 978
5078a13f 979 TString dirname(filename);
980 Int_t dirNumber = gGrid->Mkdir(dirname.Remove(dirname.Last('/')),"-d");
981
982 TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data());
983 TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ",
984 id->GetFirstRun(),
985 id->GetLastRun(),
986 id->GetVersion());
987 TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\" ",
988 id->GetPathLevel(0).Data(),
989 id->GetPathLevel(1).Data(),
990 id->GetPathLevel(2).Data());
991 //TString addTagValue4 = Form("version_path=\"%s\" dir_number=%d",Form("%d_%s",id->GetVersion(),filename.Data()),dirNumber);
992 TString addTagValue4 = Form("version_path=\"%09d%s\" dir_number=%d",id->GetVersion(),filename.Data(),dirNumber);
993 TString addTagValue = Form("%s%s%s%s",
994 addTagValue1.Data(),
995 addTagValue2.Data(),
996 addTagValue3.Data(),
997 addTagValue4.Data());
998
999 Bool_t result = kFALSE;
1000 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
1001 TGridResult* res = gGrid->Command(addTagValue.Data());
1002 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
1003 if(resCode[0] != '1') {
1004 AliError(Form("Couldn't add CDB tag value to file %s !",
1005 filename.Data()));
1006 result = kFALSE;
1007 } else {
1008 AliDebug(2, "Object successfully tagged.");
1009 result = kTRUE;
1010 }
1011 delete res;
1012 return result;
62032124 1013
1014}
1015
1016//_____________________________________________________________________________
4667c116 1017Bool_t AliCDBGrid::TagShortLived(TString& filename, Bool_t value){
1018// tag folder with ShortLived tag
1019
5078a13f 1020 TString addTagValue = Form("addTagValue %s ShortLived_try value=%d", filename.Data(), value);
1021
1022 Bool_t result = kFALSE;
1023 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
1024 TGridResult* res = gGrid->Command(addTagValue.Data());
1025 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
1026 if(resCode[0] != '1') {
1027 AliError(Form("Couldn't add ShortLived tag value to file %s !", filename.Data()));
1028 result = kFALSE;
1029 } else {
1030 AliDebug(2,"Object successfully tagged.");
1031 result = kTRUE;
1032 }
1033 delete res;
1034 return result;
4667c116 1035
1036}
1037
1038//_____________________________________________________________________________
c3a7b59a 1039Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
62032124 1040// tag stored object in CDB table using object Id's parameters
1041
5078a13f 1042 TString addTagValue1 = Form("addTagValue %s CDB_MD ", filename.Data());
1043 TString addTagValue2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
1044 md->GetObjectClassName(),
1045 md->GetResponsible(),
1046 md->GetBeamPeriod());
1047 TString addTagValue3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
1048 md->GetAliRootVersion(),
1049 md->GetComment());
1050 TString addTagValue = Form("%s%s%s",
1051 addTagValue1.Data(),
1052 addTagValue2.Data(),
1053 addTagValue3.Data());
1054
1055 Bool_t result = kFALSE;
1056 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
1057 TGridResult* res = gGrid->Command(addTagValue.Data());
1058 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
1059 if(resCode[0] != '1') {
1060 AliWarning(Form("Couldn't add CDB_MD tag value to file %s !",
1061 filename.Data()));
1062 result = kFALSE;
1063 } else {
1064 AliDebug(2,"Object successfully tagged.");
1065 result = kTRUE;
1066 }
1067 return result;
62032124 1068}
1069
1070//_____________________________________________________________________________
b05400be 1071TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
6dc56e97 1072
5078a13f 1073 TString turl(fileName);
1074 turl.Prepend("/alien" + fDBFolder);
1075 turl += "?se="; turl += fSE.Data();
1076 TFile *file = TFile::Open(turl);
1077 if (!file) {
1078 AliError(Form("Can't open selection file <%s>!", turl.Data()));
1079 return NULL;
1080 }
1081
1082 TList *list = new TList();
1083 list->SetOwner();
1084 int i=0;
1085 TString keycycle;
1086
1087 AliCDBId *id;
1088 while(1){
1089 i++;
1090 keycycle = "AliCDBId;";
1091 keycycle+=i;
1092
1093 id = (AliCDBId*) file->Get(keycycle);
1094 if(!id) break;
1095 list->AddFirst(id);
1096 }
1097 file->Close(); delete file; file=0;
1098
1099 return list;
b05400be 1100
1101
1102}
1103
1104//_____________________________________________________________________________
1105Bool_t AliCDBGrid::Contains(const char* path) const{
1106// check for path in storage's DBFolder
1107
5078a13f 1108 TString initDir(gGrid->Pwd(0));
1109 TString dirName(fDBFolder);
1110 dirName += path; // dirName = fDBFolder/path
1111 Bool_t result=kFALSE;
1112 if (gGrid->Cd(dirName,0)) result=kTRUE;
1113 gGrid->Cd(initDir.Data(),0);
1114 return result;
b05400be 1115}
1116
62032124 1117//_____________________________________________________________________________
1118void AliCDBGrid::QueryValidFiles()
1119{
5078a13f 1120 // Query the CDB for files valid for AliCDBStorage::fRun
1121 // Fills list fValidFileIds with AliCDBId objects extracted from CDB files
1122 // selected from AliEn metadata.
1123 // If fVersion was not set, fValidFileIds is filled with highest versions.
1124
1125 TString filter;
1126 MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
1127
1128 TString path = fPathFilter.GetPath();
1129
1130 TString pattern = "Run*";
1131 TString optionQuery = "-y";
1132 if(fVersion >= 0) {
1133 pattern += Form("_v%d_s0", fVersion);
1134 optionQuery = "";
1135 }
1136 pattern += ".root";
1137 AliDebug(2,Form("pattern: %s", pattern.Data()));
1138
1139 TString addFolder = "";
1140 if (!path.Contains("*")){
1141 if (!path.BeginsWith("/")) addFolder += "/";
1142 addFolder += path;
1143 }
1144 else{
1145 if (path.BeginsWith("/")) path.Remove(0,1);
1146 if (path.EndsWith("/")) path.Remove(path.Length()-1,1);
1147 TObjArray* tokenArr = path.Tokenize("/");
1148 if (tokenArr->GetEntries() != 3) {
1149 AliError("Not a 3 level path! Keeping old query...");
1150 pattern.Prepend(path+"/");
1151 }
1152 else{
1153 TString str0 = ((TObjString*)tokenArr->At(0))->String();
1154 TString str1 = ((TObjString*)tokenArr->At(1))->String();
1155 TString str2 = ((TObjString*)tokenArr->At(2))->String();
1156 if (str0 != "*" && str1 != "*" && str2 == "*"){
1157 // e.g. "ITS/Calib/*"
1158 addFolder = "/"+str0+"/"+str1;
1159 }
1160 else if (str0 != "*" && str1 == "*" && str2 == "*"){
1161 // e.g. "ITS/*/*"
1162 addFolder = "/"+str0;
1163 }
1164 else if (str0 == "*" && str1 == "*" && str2 == "*"){
1165 // e.g. "*/*/*"
1166 // do nothing: addFolder is already an empty string;
1167 }
1168 else{
1169 // e.g. "ITS/*/RecoParam"
1170 pattern.Prepend(path+"/");
1171 }
1172 }
1173 delete tokenArr; tokenArr=0;
1174 }
1175
1176 TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
1177
1178 AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
1179
1180 if (optionQuery == "-y"){
1181 AliInfo("Only latest version will be returned");
1182 }
1183
1184 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());
1185
1186 if (!res) {
1187 AliError("Grid query failed");
1188 return;
1189 }
1190
1191 TIter next(res);
1192 TMap *map;
1193 while ((map = (TMap*)next())) {
1194 TObjString *entry;
1195 if ((entry = (TObjString *) ((TMap *)map)->GetValue("lfn"))) {
1196 TString& filename = entry->String();
1197 if(filename.IsNull()) continue;
1198 AliDebug(2,Form("Found valid file: %s", filename.Data()));
27d2c5d1 1199 AliCDBId *validFileId = new AliCDBId();
5078a13f 1200 Bool_t result = FilenameToId(filename, *validFileId);
1201 if(result) {
1202 fValidFileIds.AddLast(validFileId);
1203 }
1204 else {
1205 delete validFileId;
1206 }
1207 }
1208 }
1209 delete res;
62032124 1210
1211}
1212
1213//_____________________________________________________________________________
c3a7b59a 1214void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
5078a13f 1215 const AliCDBMetaData* md, TString& result) const
62032124 1216{
5078a13f 1217 // create filter for file query
1218
1219 result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun);
1220
1221 // if(version >= 0) {
1222 // result += Form(" and CDB:version=%d", version);
1223 // }
1224 // if(pathFilter.GetLevel0() != "*") {
1225 // result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
1226 // }
1227 // if(pathFilter.GetLevel1() != "*") {
1228 // result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
1229 // }
1230 // if(pathFilter.GetLevel2() != "*") {
1231 // result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
1232 // }
1233
1234 if(md){
1235 if(md->GetObjectClassName()[0] != '\0') {
1236 result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName());
1237 }
1238 if(md->GetResponsible()[0] != '\0') {
1239 result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible());
1240 }
1241 if(md->GetBeamPeriod() != 0) {
1242 result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod());
1243 }
1244 if(md->GetAliRootVersion()[0] != '\0') {
1245 result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion());
1246 }
1247 if(md->GetComment()[0] != '\0') {
1248 result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment());
1249 }
1250 }
1251 AliDebug(2, Form("filter: %s",result.Data()));
62032124 1252
1253}
1254
c3a7b59a 1255
9e1ceb13 1256/////////////////////////////////////////////////////////////////////////////////////////////////
1257// //
1258// AliCDBGrid factory //
1259// //
1260/////////////////////////////////////////////////////////////////////////////////////////////////
1261
1262ClassImp(AliCDBGridFactory)
1263
5078a13f 1264 //_____________________________________________________________________________
1265 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
1266 // check if the string is valid Grid URI
9e1ceb13 1267
5078a13f 1268 TRegexp gridPattern("^alien://.+$");
9e1ceb13 1269
5078a13f 1270 return TString(gridString).Contains(gridPattern);
1271 }
9e1ceb13 1272
1273//_____________________________________________________________________________
1274AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
5078a13f 1275 // create AliCDBGridParam class from the URI string
1276
1277 if (!Validate(gridString)) {
1278 return NULL;
1279 }
1280
1281 TString buffer(gridString);
1282
1283 TString gridUrl = "alien://";
1284 TString user = "";
1285 TString dbFolder = "";
1286 TString se = "default";
1287 TString cacheFolder = "";
1288 Bool_t operateDisconnected = kTRUE;
1289 Long64_t cacheSize = (UInt_t) 1024*1024*1024; // 1GB
1290 Long_t cleanupInterval = 0;
1291
1292 TObjArray *arr = buffer.Tokenize('?');
1293 TIter iter(arr);
1294 TObjString *str = 0;
1295
1296 while((str = (TObjString*) iter.Next())){
1297 TString entry(str->String());
1298 Int_t indeq = entry.Index('=');
1299 if(indeq == -1) {
1300 if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
1301 gridUrl = entry;
1302 continue;
1303 } else {
1304 AliError(Form("Invalid entry! %s",entry.Data()));
1305 continue;
1306 }
1307 }
1308
1309 TString key = entry(0,indeq);
1310 TString value = entry(indeq+1,entry.Length()-indeq);
1311
1312 if(key.Contains("grid",TString::kIgnoreCase)) {
1313 gridUrl += value;
1314 }
1315 else if (key.Contains("user",TString::kIgnoreCase)){
1316 user = value;
1317 }
1318 else if (key.Contains("se",TString::kIgnoreCase)){
1319 se = value;
1320 }
1321 else if (key.Contains("cacheF",TString::kIgnoreCase)){
1322 cacheFolder = value;
1323 if (!cacheFolder.IsNull() && !cacheFolder.EndsWith("/"))
1324 cacheFolder += "/";
1325 }
1326 else if (key.Contains("folder",TString::kIgnoreCase)){
1327 dbFolder = value;
1328 }
1329 else if (key.Contains("operateDisc",TString::kIgnoreCase)){
1330 if(value == "kTRUE") {
1331 operateDisconnected = kTRUE;
1332 } else if (value == "kFALSE") {
1333 operateDisconnected = kFALSE;
1334 } else if (value == "0" || value == "1") {
1335 operateDisconnected = (Bool_t) value.Atoi();
1336 } else {
1337 AliError(Form("Invalid entry! %s",entry.Data()));
1338 return NULL;
1339 }
1340 }
1341 else if (key.Contains("cacheS",TString::kIgnoreCase)){
1342 if(value.IsDigit()) {
1343 cacheSize = value.Atoi();
1344 } else {
1345 AliError(Form("Invalid entry! %s",entry.Data()));
1346 return NULL;
1347 }
1348 }
1349 else if (key.Contains("cleanupInt",TString::kIgnoreCase)){
1350 if(value.IsDigit()) {
1351 cleanupInterval = value.Atoi();
1352 } else {
1353 AliError(Form("Invalid entry! %s",entry.Data()));
1354 return NULL;
1355 }
1356 }
1357 else{
1358 AliError(Form("Invalid entry! %s",entry.Data()));
1359 return NULL;
1360 }
1361 }
1362 delete arr; arr=0;
1363
1364 AliDebug(2, Form("gridUrl: %s", gridUrl.Data()));
1365 AliDebug(2, Form("user: %s", user.Data()));
1366 AliDebug(2, Form("dbFolder: %s", dbFolder.Data()));
1367 AliDebug(2, Form("s.e.: %s", se.Data()));
1368 AliDebug(2, Form("local cache folder: %s", cacheFolder.Data()));
1369 AliDebug(2, Form("local cache operate disconnected: %d", operateDisconnected));
1370 AliDebug(2, Form("local cache size: %lld", cacheSize));
1371 AliDebug(2, Form("local cache cleanup interval: %ld", cleanupInterval));
1372
1373 if(dbFolder == ""){
1374 AliError("Base folder must be specified!");
1375 return NULL;
1376 }
1377
1378 return new AliCDBGridParam(gridUrl.Data(), user.Data(),
1379 dbFolder.Data(), se.Data(), cacheFolder.Data(),
1380 operateDisconnected, cacheSize, cleanupInterval);
9e1ceb13 1381}
1382
1383//_____________________________________________________________________________
1384AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
5078a13f 1385 // create AliCDBGrid storage instance from parameters
1386
1387 AliCDBGrid *grid = 0;
1388 if (AliCDBGridParam::Class() == param->IsA()) {
1389
1390 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
1391 grid = new AliCDBGrid(gridParam->GridUrl().Data(),
1392 gridParam->GetUser().Data(),
1393 gridParam->GetDBFolder().Data(),
1394 gridParam->GetSE().Data(),
1395 gridParam->GetCacheFolder().Data(),
1396 gridParam->GetOperateDisconnected(),
1397 gridParam->GetCacheSize(),
1398 gridParam->GetCleanupInterval());
1399
1400 }
1401
1402 if(!gGrid && grid) {
1403 delete grid; grid=0;
1404 }
1405
1406 return grid;
9e1ceb13 1407}
1408
1409/////////////////////////////////////////////////////////////////////////////////////////////////
1410// //
c75fabed 1411// AliCDBGrid Parameter class // //
9e1ceb13 1412// //
1413/////////////////////////////////////////////////////////////////////////////////////////////////
1414
1415ClassImp(AliCDBGridParam)
1416
5078a13f 1417 //_____________________________________________________________________________
1418 AliCDBGridParam::AliCDBGridParam():
1419 AliCDBParam(),
1420 fGridUrl(),
1421 fUser(),
1422 fDBFolder(),
1423 fSE(),
1424 fCacheFolder(),
1425 fOperateDisconnected(),
1426 fCacheSize(),
1427 fCleanupInterval()
1428
1429{
1430 // default constructor
9e1ceb13 1431
1432}
1433
1434//_____________________________________________________________________________
c75fabed 1435AliCDBGridParam::AliCDBGridParam(const char* gridUrl, const char* user, const char* dbFolder,
5078a13f 1436 const char* se, const char* cacheFolder, Bool_t operateDisconnected,
1437 Long64_t cacheSize, Long_t cleanupInterval):
1438 AliCDBParam(),
1439 fGridUrl(gridUrl),
1440 fUser(user),
1441 fDBFolder(dbFolder),
1442 fSE(se),
1443 fCacheFolder(cacheFolder),
1444 fOperateDisconnected(operateDisconnected),
1445 fCacheSize(cacheSize),
1446 fCleanupInterval(cleanupInterval)
9e1ceb13 1447{
5078a13f 1448 // constructor
c75fabed 1449
5078a13f 1450 SetType("alien");
9e1ceb13 1451
5078a13f 1452 TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s?CacheFolder=%s"
1453 "?OperateDisconnected=%d?CacheSize=%lld?CleanupInterval=%ld",
1454 fGridUrl.Data(), fUser.Data(),
1455 fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1456 fOperateDisconnected, fCacheSize, fCleanupInterval);
62032124 1457
5078a13f 1458 SetURI(uri.Data());
9e1ceb13 1459}
1460
1461//_____________________________________________________________________________
1462AliCDBGridParam::~AliCDBGridParam() {
5078a13f 1463 // destructor
9e1ceb13 1464
1465}
1466
1467//_____________________________________________________________________________
1468AliCDBParam* AliCDBGridParam::CloneParam() const {
5078a13f 1469 // clone parameter
9e1ceb13 1470
5078a13f 1471 return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(),
1472 fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1473 fOperateDisconnected, fCacheSize, fCleanupInterval);
9e1ceb13 1474}
1475
1476//_____________________________________________________________________________
1477ULong_t AliCDBGridParam::Hash() const {
5078a13f 1478 // return Hash function
9e1ceb13 1479
5078a13f 1480 return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash()+fCacheFolder.Hash();
9e1ceb13 1481}
1482
1483//_____________________________________________________________________________
1484Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
5078a13f 1485 // check if this object is equal to AliCDBParam obj
1486
1487 if (this == obj) {
1488 return kTRUE;
1489 }
1490
1491 if (AliCDBGridParam::Class() != obj->IsA()) {
1492 return kFALSE;
1493 }
1494
1495 AliCDBGridParam* other = (AliCDBGridParam*) obj;
1496
1497 if(fGridUrl != other->fGridUrl) return kFALSE;
1498 if(fUser != other->fUser) return kFALSE;
1499 if(fDBFolder != other->fDBFolder) return kFALSE;
1500 if(fSE != other->fSE) return kFALSE;
1501 if(fCacheFolder != other->fCacheFolder) return kFALSE;
1502 if(fOperateDisconnected != other->fOperateDisconnected) return kFALSE;
1503 if(fCacheSize != other->fCacheSize) return kFALSE;
1504 if(fCleanupInterval != other->fCleanupInterval) return kFALSE;
1505 return kTRUE;
9e1ceb13 1506}
1507