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 **************************************************************************/
16 /////////////////////////////////////////////////////////////////////////////////////////////////
19 // access class to a DataBase in an AliEn storage //
21 /////////////////////////////////////////////////////////////////////////////////////////////////
25 #include <TGridResult.h>
30 #include <TObjArray.h>
31 #include <TObjString.h>
36 #include "AliCDBEntry.h"
37 #include "AliCDBGrid.h"
38 #include "AliCDBManager.h"
43 //_____________________________________________________________________________
44 AliCDBGrid::AliCDBGrid(const char *gridUrl, const char *user, const char *dbFolder,
45 const char *se, const char* cacheFolder, Bool_t operateDisconnected,
46 Long64_t cacheSize, Long_t cleanupInterval) :
52 fCacheFolder(cacheFolder),
53 fOperateDisconnected(operateDisconnected),
54 fCacheSize(cacheSize),
55 fCleanupInterval(cleanupInterval)
59 // if the same Grid is alreay active, skip connection
60 if (!gGrid || fGridUrl != gGrid->GridUrl()
61 || (( fUser != "" ) && ( fUser != gGrid->GetUser() )) ) {
62 // connection to the Grid
63 AliInfo("Connection to the Grid...");
65 AliInfo(Form("gGrid = %p; fGridUrl = %s; gGrid->GridUrl() = %s",gGrid,fGridUrl.Data(), gGrid->GridUrl()));
66 AliInfo(Form("fUser = %s; gGrid->GetUser() = %s",fUser.Data(), gGrid->GetUser()));
68 TGrid::Connect(fGridUrl.Data(),fUser.Data());
72 AliError("Connection failed!");
76 TString initDir(gGrid->Pwd(0));
77 if (fDBFolder[0] != '/') {
78 fDBFolder.Prepend(initDir);
81 // check DBFolder: trying to cd to DBFolder; if it does not exist, create it
82 if(!gGrid->Cd(fDBFolder.Data(),0)){
83 AliDebug(2,Form("Creating new folder <%s> ...",fDBFolder.Data()));
84 TGridResult* res = gGrid->Command(Form("mkdir -p %s",fDBFolder.Data()));
85 TString result = res->GetKey(0,"__result__");
87 AliFatal(Form("Cannot create folder <%s> !",fDBFolder.Data()));
91 AliDebug(2,Form("Folder <%s> found",fDBFolder.Data()));
94 // removes any '/' at the end of path, then append one '/'
95 while(fDBFolder.EndsWith("/")) fDBFolder.Remove(fDBFolder.Last('/'));
99 fBaseFolder = fDBFolder;
103 // Check if local cache folder is already defined
104 TString origCache(TFile::GetCacheFileDir());
105 if(fCacheFolder.Length() > 0) {
106 if(origCache.Length() == 0) {
107 AliInfo(Form("Setting local cache to: %s", fCacheFolder.Data()));
108 } else if(fCacheFolder != origCache) {
109 AliWarning(Form("Local cache folder was already defined, changing it to: %s",
110 fCacheFolder.Data()));
113 // default settings are: operateDisconnected=kTRUE, forceCacheread = kFALSE
114 if(!TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected)) {
115 AliError(Form("Could not set cache folder %s !", fCacheFolder.Data()));
118 // reset fCacheFolder because the function may have
119 // slightly changed the folder name (e.g. '/' added)
120 fCacheFolder = TFile::GetCacheFileDir();
123 // default settings are: cacheSize=1GB, cleanupInterval = 0
124 if(!TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval)) {
125 AliError(Form("Could not set following values "
126 "to ShrinkCacheFileDir: cacheSize = %lld, cleanupInterval = %ld !",
127 fCacheSize, fCleanupInterval));
131 // return to the initial directory
132 gGrid->Cd(initDir.Data(),0);
134 fNretry = 3; // default
135 fInitRetrySeconds = 5; // default
138 //_____________________________________________________________________________
139 AliCDBGrid::~AliCDBGrid()
142 delete gGrid; gGrid=0;
146 //_____________________________________________________________________________
147 Bool_t AliCDBGrid::FilenameToId(TString& filename, AliCDBId& id) {
148 // build AliCDBId from full path filename (fDBFolder/path/Run#x_#y_v#z_s0.root)
150 if(filename.Contains(fDBFolder)){
151 filename = filename(fDBFolder.Length(),filename.Length()-fDBFolder.Length());
154 TString idPath = filename(0,filename.Last('/'));
156 if(!id.IsValid()) return kFALSE;
158 filename=filename(idPath.Length()+1,filename.Length()-idPath.Length());
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);
166 // TODO backward compatibility ... maybe remove later!
168 TRegexp oldKeyPattern("^Run[0-9]+_[0-9]+_v[0-9]+.root$");
169 oldKeyPattern.Index(filename, &oldmSize);
171 AliDebug(2,Form("Bad filename <%s>.", filename.Data()));
174 AliDebug(2,Form("Old filename format <%s>.", filename.Data()));
175 id.SetSubVersion(-11); // TODO trick to ensure backward compatibility
179 id.SetSubVersion(-1); // TODO trick to ensure backward compatibility
182 filename.Resize(filename.Length() - sizeof(".root") + 1);
184 TObjArray* strArray = (TObjArray*) filename.Tokenize("_");
186 TString firstRunString(((TObjString*) strArray->At(0))->GetString());
187 id.SetFirstRun(atoi(firstRunString.Data() + 3));
188 id.SetLastRun(atoi(((TObjString*) strArray->At(1))->GetString()));
190 TString verString(((TObjString*) strArray->At(2))->GetString());
191 id.SetVersion(atoi(verString.Data() + 1));
198 //_____________________________________________________________________________
199 Bool_t AliCDBGrid::IdToFilename(const AliCDBId& id, TString& filename) const {
200 // build file name from AliCDBId (path, run range, version) and fDBFolder
202 if (!id.GetAliCDBRunRange().IsValid()) {
203 AliDebug(2,Form("Invalid run range <%d, %d>.",
204 id.GetFirstRun(), id.GetLastRun()));
208 if (id.GetVersion() < 0) {
209 AliDebug(2,Form("Invalid version <%d>.", id.GetVersion()));
213 filename = Form("Run%d_%d_v%d",
218 if (id.GetSubVersion() != -11) filename += "_s0"; // TODO to ensure backward compatibility
221 filename.Prepend(fDBFolder + id.GetPath() + '/');
226 //_____________________________________________________________________________
227 void AliCDBGrid::SetRetry(Int_t nretry, Int_t initsec) {
229 // Function to set the exponential retry for putting entries in the OCDB
231 AliWarning("WARNING!!! You are changing the exponential retry times and delay: this function should be used by experts!");
233 fInitRetrySeconds = initsec;
234 AliDebug(2,Form("fNretry = %d, fInitRetrySeconds = %d", fNretry, fInitRetrySeconds));
238 //_____________________________________________________________________________
239 Bool_t AliCDBGrid::PrepareId(AliCDBId& id) {
240 // prepare id (version) of the object that will be stored (called by PutEntry)
242 TString initDir(gGrid->Pwd(0));
244 TString dirName(fDBFolder);
246 Bool_t dirExist=kFALSE;
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);
255 //gGrid->Command(cmd.Data());
256 dirName+=Form("%s/",id.GetPathLevel(i).Data());
257 dirExist=gGrid->Cd(dirName,0);
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());
266 // if folders are new add tags to them
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()));
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()));
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()));
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()));
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()));
321 gGrid->Cd(initDir,0);
324 AliCDBId anId; // the id got from filename
325 AliCDBRunRange lastRunRange(-1,-1); // highest runRange found
326 Int_t lastVersion=0; // highest version found
328 TGridResult *res = gGrid->Ls(dirName);
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();
342 // GRP entries with explicitly set version escape default incremental versioning
343 if(id.GetPath().Contains("GRP") && id.HasVersion() && lastVersion!=0)
345 AliDebug(5,Form("Entry %s won't be put in the destination OCDB", id.ToString().Data()));
349 id.SetVersion(lastVersion + 1);
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",
356 AliDebug(2, Form("and it will hide previously stored object with version %d!",
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()));
367 //_____________________________________________________________________________
368 AliCDBId* AliCDBGrid::GetId(const TObjArray& validFileIds, const AliCDBId& query) {
369 // look for the Id that matches query's requests (highest or exact version)
371 if(validFileIds.GetEntriesFast() < 1)
374 TIter iter(&validFileIds);
379 while((anIdPtr = dynamic_cast<AliCDBId*> (iter.Next()))){
380 if(anIdPtr->GetPath() != query.GetPath()) continue;
382 //if(!CheckVersion(query, anIdPtr, result)) return NULL;
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()));
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()));
404 if (!result) return NULL;
406 return dynamic_cast<AliCDBId*> (result->Clone());
409 //_____________________________________________________________________________
410 AliCDBId* AliCDBGrid::GetEntryId(const AliCDBId& queryId) {
411 // get AliCDBId from the database
412 // User must delete returned object
416 AliCDBId selectedId(queryId);
417 if (!selectedId.HasVersion()) {
418 // if version is not specified, first check the selection criteria list
419 GetSelection(&selectedId);
422 TObjArray validFileIds;
423 validFileIds.SetOwner(1);
425 // look for file matching query requests (path, runRange, version)
426 if(selectedId.GetFirstRun() == fRun && fPathFilter.Comprises(selectedId.GetAliCDBPath()) &&
427 fVersion == selectedId.GetVersion() && !fMetaDataFilter){
428 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
429 AliDebug(2, Form("List of files valid for run %d was loaded. Looking there for fileids valid for path %s!",
430 selectedId.GetFirstRun(), selectedId.GetPath().Data()));
431 dataId = GetId(fValidFileIds, selectedId);
434 // List of files valid for reqested run was not loaded. Looking directly into CDB
435 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!",
436 selectedId.GetFirstRun(), selectedId.GetVersion(), selectedId.GetPath().Data()));
439 MakeQueryFilter(selectedId.GetFirstRun(), selectedId.GetLastRun(), 0, filter);
441 TString pattern = ".root";
442 TString optionQuery = "-y -m";
443 if(selectedId.GetVersion() >= 0) {
444 pattern.Prepend(Form("_v%d_s0",selectedId.GetVersion()));
448 TString folderCopy(Form("%s%s/Run",fDBFolder.Data(),selectedId.GetPath().Data()));
450 if (optionQuery.Contains("-y")){
451 AliInfo("Only latest version will be returned");
454 AliDebug(2,Form("** fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
455 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());
457 AliCDBId validFileId;
458 for(int i=0; i<res->GetEntries(); i++){
459 TString filename = res->GetKey(i, "lfn");
460 if(filename == "") continue;
461 if(FilenameToId(filename, validFileId))
462 validFileIds.AddLast(validFileId.Clone());
466 dataId = GetId(validFileIds, selectedId);
472 //_____________________________________________________________________________
473 AliCDBEntry* AliCDBGrid::GetEntry(const AliCDBId& queryId) {
474 // get AliCDBEntry from the database
476 AliCDBId* dataId = GetEntryId(queryId);
478 if (!dataId) return NULL;
481 if (!IdToFilename(*dataId, filename)) {
482 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
487 AliCDBEntry* anEntry = GetEntryFromFile(filename, dataId);
493 //_____________________________________________________________________________
494 AliCDBEntry* AliCDBGrid::GetEntryFromFile(TString& filename, AliCDBId* dataId){
495 // Get AliCBEntry object from file "filename"
497 AliDebug(2,Form("Opening file: %s",filename.Data()));
499 filename.Prepend("/alien");
501 // if option="CACHEREAD" TFile will use the local caching facility!
502 TString option="READ";
503 if(fCacheFolder != ""){
505 // Check if local cache folder was changed in the meanwhile
506 TString origCache(TFile::GetCacheFileDir());
507 if(fCacheFolder != origCache) {
508 AliWarning(Form("Local cache folder has been overwritten!! fCacheFolder = %s origCache = %s",
509 fCacheFolder.Data(), origCache.Data()));
510 TFile::SetCacheFileDir(fCacheFolder.Data(), fOperateDisconnected);
511 TFile::ShrinkCacheFileDir(fCacheSize, fCleanupInterval);
514 option.Prepend("CACHE");
517 AliDebug(2, Form("Option: %s", option.Data()));
519 TFile *file = TFile::Open(filename, option);
521 AliDebug(2,Form("Can't open file <%s>!", filename.Data()));
525 // get the only AliCDBEntry object from the file
526 // the object in the file is an AliCDBEntry entry named "AliCDBEntry"
528 AliCDBEntry* anEntry = dynamic_cast<AliCDBEntry*> (file->Get("AliCDBEntry"));
531 AliDebug(2,Form("Bad storage data: file does not contain an AliCDBEntry object!"));
536 // The object's Id is not reset during storage
537 // If object's Id runRange or version do not match with filename,
538 // it means that someone renamed file by hand. In this case a warning msg is issued.
541 AliCDBId entryId = anEntry->GetId();
542 Int_t tmpSubVersion = dataId->GetSubVersion();
543 dataId->SetSubVersion(entryId.GetSubVersion()); // otherwise filename and id may mismatch
544 if(!entryId.IsEqual(dataId)){
545 AliWarning(Form("Mismatch between file name and object's Id!"));
546 AliWarning(Form("File name: %s", dataId->ToString().Data()));
547 AliWarning(Form("Object's Id: %s", entryId.ToString().Data()));
549 dataId->SetSubVersion(tmpSubVersion);
552 anEntry->SetLastStorage("grid");
554 // Check whether entry contains a TTree. In case load the tree in memory!
555 LoadTreeFromFile(anEntry);
557 // close file, return retieved entry
558 file->Close(); delete file; file=0;
563 //_____________________________________________________________________________
564 TList* AliCDBGrid::GetEntries(const AliCDBId& queryId) {
565 // multiple request (AliCDBStorage::GetAll)
567 TList* result = new TList();
570 TObjArray validFileIds;
571 validFileIds.SetOwner(1);
573 Bool_t alreadyLoaded = kFALSE;
575 // look for file matching query requests (path, runRange)
576 if(queryId.GetFirstRun() == fRun &&
577 fPathFilter.Comprises(queryId.GetAliCDBPath()) && fVersion < 0 && !fMetaDataFilter){
578 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
579 AliDebug(2,Form("List of files valid for run %d and for path %s was loaded. Looking there!",
580 queryId.GetFirstRun(), queryId.GetPath().Data()));
582 alreadyLoaded = kTRUE;
585 // List of files valid for reqested run was not loaded. Looking directly into CDB
586 AliDebug(2,Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
587 queryId.GetFirstRun(), queryId.GetPath().Data()));
590 MakeQueryFilter(queryId.GetFirstRun(), queryId.GetLastRun(), 0, filter);
592 TString path = queryId.GetPath();
594 TString pattern = "Run*.root";
595 TString optionQuery = "-y";
597 TString addFolder = "";
598 if (!path.Contains("*")){
599 if (!path.BeginsWith("/")) addFolder += "/";
603 if (path.BeginsWith("/")) path.Remove(0,1);
604 if (path.EndsWith("/")) path.Remove(path.Length()-1,1);
605 TObjArray* tokenArr = path.Tokenize("/");
606 if (tokenArr->GetEntries() != 3) {
607 AliError("Not a 3 level path! Keeping old query...");
608 pattern.Prepend(path+"/");
611 TString str0 = ((TObjString*)tokenArr->At(0))->String();
612 TString str1 = ((TObjString*)tokenArr->At(1))->String();
613 TString str2 = ((TObjString*)tokenArr->At(2))->String();
614 if (str0 != "*" && str1 != "*" && str2 == "*"){
615 // e.g. "ITS/Calib/*"
616 addFolder = "/"+str0+"/"+str1;
618 else if (str0 != "*" && str1 == "*" && str2 == "*"){
620 addFolder = "/"+str0;
622 else if (str0 == "*" && str1 == "*" && str2 == "*"){
624 // do nothing: addFolder is already an empty string;
627 // e.g. "ITS/*/RecoParam"
628 pattern.Prepend(path+"/");
631 delete tokenArr; tokenArr=0;
634 TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
636 AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
638 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());
641 AliError("Grid query failed");
645 AliCDBId validFileId;
646 for(int i=0; i<res->GetEntries(); i++){
647 TString filename = res->GetKey(i, "lfn");
648 if(filename == "") continue;
649 if(FilenameToId(filename, validFileId))
650 validFileIds.AddLast(validFileId.Clone());
657 iter = new TIter(&fValidFileIds);
659 iter = new TIter(&validFileIds);
662 TObjArray selectedIds;
663 selectedIds.SetOwner(1);
665 // loop on list of valid Ids to select the right version to get.
666 // According to query and to the selection criteria list, version can be the highest or exact
670 AliCDBPath queryPath = queryId.GetAliCDBPath();
671 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
672 AliCDBPath thisCDBPath = anIdPtr->GetAliCDBPath();
673 if(!(queryPath.Comprises(thisCDBPath)) || pathCopy.GetPath() == thisCDBPath.GetPath()) continue;
674 pathCopy = thisCDBPath;
676 // check the selection criteria list for this query
677 AliCDBId thisId(*anIdPtr);
678 thisId.SetVersion(queryId.GetVersion());
679 if(!thisId.HasVersion()) GetSelection(&thisId);
682 dataId = GetId(fValidFileIds, thisId);
684 dataId = GetId(validFileIds, thisId);
686 if(dataId) selectedIds.Add(dataId);
691 // selectedIds contains the Ids of the files matching all requests of query!
692 // All the objects are now ready to be retrieved
693 iter = new TIter(&selectedIds);
694 while((anIdPtr = dynamic_cast<AliCDBId*> (iter->Next()))){
696 if (!IdToFilename(*anIdPtr, filename)) {
697 AliDebug(2,Form("Bad data ID encountered! Subnormal error!"));
701 AliCDBEntry* anEntry = GetEntryFromFile(filename, anIdPtr);
703 if(anEntry) result->Add(anEntry);
711 //_____________________________________________________________________________
712 Bool_t AliCDBGrid::PutEntry(AliCDBEntry* entry, const char* mirrors) {
713 // put an AliCDBEntry object into the database
715 AliCDBId& id = entry->GetId();
717 // set version for the entry to be stored
718 if (!PrepareId(id)) return kFALSE;
720 // build filename from entry's id
722 if (!IdToFilename(id, filename)) {
723 AliError("Bad ID encountered! Subnormal error!");
727 TString folderToTag = Form("%s%s",
729 id.GetPath().Data());
731 TDirectory* saveDir = gDirectory;
733 TString fullFilename = Form("/alien%s", filename.Data());
734 TString seMirrors(mirrors);
735 // specify SE to filename
736 // if a list of SEs was passed, set the first as SE for opening the file. The others will be used in cascade in case of
737 // failure in opening the file. The remaining will be used to create replicas.
738 TObjArray *arraySEs = seMirrors.Tokenize(',');
739 Int_t nSEs = arraySEs->GetEntries();
740 Int_t remainingSEs = 1;
742 if (fSE != "default") fullFilename += Form("?se=%s",fSE.Data());
749 AliDebug(2, Form("fNretry = %d, fInitRetrySeconds = %d",fNretry,fInitRetrySeconds));
750 TString targetSE("");
751 while(remainingSEs>0){
753 TObjString *target = (TObjString*) arraySEs->At(nSEs-remainingSEs);
754 targetSE=target->String();
755 if ( !(targetSE.BeginsWith("ALICE::") && targetSE.CountChar(':')==4) ) {
756 AliError(Form("\"%s\" is an invalid storage element identifier.",targetSE.Data()));
759 fullFilename.Remove(fullFilename.Last('?'));
760 fullFilename += Form("?se=%s",targetSE.Data());
762 Int_t remainingAttempts=fNretry;
763 Int_t nsleep = fInitRetrySeconds; // number of seconds between attempts. We let it increase exponentially
764 while(remainingAttempts > 0) {
765 AliDebug(2, Form("Putting the file in the OCDB - Attempt n. %d",fNretry-remainingAttempts+1));
766 file = TFile::Open(fullFilename,"CREATE");
768 if(!file || !file->IsWritable()){
769 AliError(Form("Can't open file <%s>!", filename.Data()));
770 if(file && !file->IsWritable()) file->Close(); delete file; file=0;
771 AliDebug(2,Form("Attempt %d failed, sleeping for %d seconds",fNretry-remainingAttempts+1,nsleep));
772 if(remainingAttempts>0) sleep(nsleep);
776 nsleep*=fInitRetrySeconds;
782 AliError(Form("All %d attempts have failed on all %d SEs. Returning...",fNretry,nSEs));
789 //SetTreeToFile(entry, file);
791 entry->SetVersion(id.GetVersion());
793 // write object (key name: "AliCDBEntry")
794 Bool_t result = (file->WriteTObject(entry, "AliCDBEntry") != 0);
795 if (!result) AliError(Form("Can't write entry to file <%s>!", filename.Data()));
798 if (saveDir) saveDir->cd(); else gROOT->cd();
799 file->Close(); delete file; file=0;
803 if(!TagFileId(filename, &id)){
804 AliInfo(Form("CDB tagging failed. Deleting file %s!",filename.Data()));
805 if(!gGrid->Rm(filename.Data()))
806 AliError("Can't delete file!");
810 TagFileMetaData(filename, entry->GetMetaData());
813 AliInfo(Form("CDB object stored into file %s", filename.Data()));
815 AliInfo(Form("Storage Element: %s", fSE.Data()));
817 AliInfo(Form("Storage Element: %s", targetSE.Data()));
819 //In case of other SEs specified by the user, mirror the file to the remaining SEs
820 while(remainingSEs>0){
821 TString mirrorCmd("mirror ");
822 mirrorCmd += filename;
824 TObjString *target = (TObjString*) arraySEs->At(nSEs-remainingSEs);
825 TString mirrorSE(target->String());
826 mirrorCmd += mirrorSE;
827 AliDebug(5,Form("mirror command: \"%s\"",mirrorCmd.Data()));
828 AliInfo(Form("Mirroring to storage element: %s", mirrorSE.Data()));
829 gGrid->Command(mirrorCmd.Data());
835 //_____________________________________________________________________________
836 Bool_t AliCDBGrid::AddTag(TString& folderToTag, const char* tagname){
837 // add "tagname" tag (CDB or CDB_MD) to folder where object will be stored
839 Bool_t result = kTRUE;
840 AliDebug(2, Form("adding %s tag to folder %s", tagname, folderToTag.Data()));
841 TString addTag = Form("addTag %s %s", folderToTag.Data(), tagname);
842 TGridResult *gridres = gGrid->Command(addTag.Data());
843 const char* resCode = gridres->GetKey(0,"__result__"); // '1' if success
844 if(resCode[0] != '1') {
845 AliError(Form("Couldn't add %s tags to folder %s !",
846 tagname, folderToTag.Data()));
853 //_____________________________________________________________________________
854 Bool_t AliCDBGrid::TagFileId(TString& filename, const AliCDBId* id){
855 // tag stored object in CDB table using object Id's parameters
858 TString dirname(filename);
859 Int_t dirNumber = gGrid->Mkdir(dirname.Remove(dirname.Last('/')),"-d");
861 TString addTagValue1 = Form("addTagValue %s CDB ", filename.Data());
862 TString addTagValue2 = Form("first_run=%d last_run=%d version=%d ",
866 TString addTagValue3 = Form("path_level_0=\"%s\" path_level_1=\"%s\" path_level_2=\"%s\" ",
867 id->GetPathLevel(0).Data(),
868 id->GetPathLevel(1).Data(),
869 id->GetPathLevel(2).Data());
870 //TString addTagValue4 = Form("version_path=\"%s\" dir_number=%d",Form("%d_%s",id->GetVersion(),filename.Data()),dirNumber);
871 TString addTagValue4 = Form("version_path=\"%09d%s\" dir_number=%d",id->GetVersion(),filename.Data(),dirNumber);
872 TString addTagValue = Form("%s%s%s%s",
876 addTagValue4.Data());
878 Bool_t result = kFALSE;
879 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
880 TGridResult* res = gGrid->Command(addTagValue.Data());
881 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
882 if(resCode[0] != '1') {
883 AliError(Form("Couldn't add CDB tag value to file %s !",
887 AliDebug(2, "Object successfully tagged.");
895 //_____________________________________________________________________________
896 Bool_t AliCDBGrid::TagShortLived(TString& filename, Bool_t value){
897 // tag folder with ShortLived tag
899 TString addTagValue = Form("addTagValue %s ShortLived_try value=%d", filename.Data(), value);
901 Bool_t result = kFALSE;
902 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
903 TGridResult* res = gGrid->Command(addTagValue.Data());
904 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
905 if(resCode[0] != '1') {
906 AliError(Form("Couldn't add ShortLived tag value to file %s !", filename.Data()));
909 AliDebug(2,"Object successfully tagged.");
917 //_____________________________________________________________________________
918 Bool_t AliCDBGrid::TagFileMetaData(TString& filename, const AliCDBMetaData* md){
919 // tag stored object in CDB table using object Id's parameters
921 TString addTagValue1 = Form("addTagValue %s CDB_MD ", filename.Data());
922 TString addTagValue2 = Form("object_classname=\"%s\" responsible=\"%s\" beam_period=%d ",
923 md->GetObjectClassName(),
924 md->GetResponsible(),
925 md->GetBeamPeriod());
926 TString addTagValue3 = Form("aliroot_version=\"%s\" comment=\"%s\"",
927 md->GetAliRootVersion(),
929 TString addTagValue = Form("%s%s%s",
932 addTagValue3.Data());
934 Bool_t result = kFALSE;
935 AliDebug(2, Form("Tagging file. Tag command: %s", addTagValue.Data()));
936 TGridResult* res = gGrid->Command(addTagValue.Data());
937 const char* resCode = res->GetKey(0,"__result__"); // '1' if success
938 if(resCode[0] != '1') {
939 AliWarning(Form("Couldn't add CDB_MD tag value to file %s !",
943 AliDebug(2,"Object successfully tagged.");
949 //_____________________________________________________________________________
950 TList* AliCDBGrid::GetIdListFromFile(const char* fileName){
952 TString turl(fileName);
953 turl.Prepend("/alien" + fDBFolder);
954 turl += "?se="; turl += fSE.Data();
955 TFile *file = TFile::Open(turl);
957 AliError(Form("Can't open selection file <%s>!", turl.Data()));
961 TList *list = new TList();
969 keycycle = "AliCDBId;";
972 id = (AliCDBId*) file->Get(keycycle);
976 file->Close(); delete file; file=0;
983 //_____________________________________________________________________________
984 Bool_t AliCDBGrid::Contains(const char* path) const{
985 // check for path in storage's DBFolder
987 TString initDir(gGrid->Pwd(0));
988 TString dirName(fDBFolder);
989 dirName += path; // dirName = fDBFolder/path
990 Bool_t result=kFALSE;
991 if (gGrid->Cd(dirName,0)) result=kTRUE;
992 gGrid->Cd(initDir.Data(),0);
996 //_____________________________________________________________________________
997 void AliCDBGrid::QueryValidFiles()
999 // Query the CDB for files valid for AliCDBStorage::fRun
1000 // fills list fValidFileIds with AliCDBId objects created from file name
1003 MakeQueryFilter(fRun, fRun, fMetaDataFilter, filter);
1005 TString path = fPathFilter.GetPath();
1007 TString pattern = "Run*";
1008 TString optionQuery = "-y";
1010 pattern += Form("_v%d_s0", fVersion);
1014 AliDebug(2,Form("pattern: %s", pattern.Data()));
1016 TString addFolder = "";
1017 if (!path.Contains("*")){
1018 if (!path.BeginsWith("/")) addFolder += "/";
1022 if (path.BeginsWith("/")) path.Remove(0,1);
1023 if (path.EndsWith("/")) path.Remove(path.Length()-1,1);
1024 TObjArray* tokenArr = path.Tokenize("/");
1025 if (tokenArr->GetEntries() != 3) {
1026 AliError("Not a 3 level path! Keeping old query...");
1027 pattern.Prepend(path+"/");
1030 TString str0 = ((TObjString*)tokenArr->At(0))->String();
1031 TString str1 = ((TObjString*)tokenArr->At(1))->String();
1032 TString str2 = ((TObjString*)tokenArr->At(2))->String();
1033 if (str0 != "*" && str1 != "*" && str2 == "*"){
1034 // e.g. "ITS/Calib/*"
1035 addFolder = "/"+str0+"/"+str1;
1037 else if (str0 != "*" && str1 == "*" && str2 == "*"){
1039 addFolder = "/"+str0;
1041 else if (str0 == "*" && str1 == "*" && str2 == "*"){
1043 // do nothing: addFolder is already an empty string;
1046 // e.g. "ITS/*/RecoParam"
1047 pattern.Prepend(path+"/");
1050 delete tokenArr; tokenArr=0;
1053 TString folderCopy(Form("%s%s",fDBFolder.Data(),addFolder.Data()));
1055 AliDebug(2,Form("fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
1057 if (optionQuery == "-y"){
1058 AliInfo("Only latest version will be returned");
1061 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, optionQuery.Data());
1064 AliError("Grid query failed");
1070 while ((map = (TMap*)next())) {
1072 if ((entry = (TObjString *) ((TMap *)map)->GetValue("lfn"))) {
1073 TString& filename = entry->String();
1074 if(filename.IsNull()) continue;
1075 AliDebug(2,Form("Found valid file: %s", filename.Data()));
1076 AliCDBId *validFileId = new AliCDBId;
1077 Bool_t result = FilenameToId(filename, *validFileId);
1079 fValidFileIds.AddLast(validFileId);
1090 //_____________________________________________________________________________
1091 void AliCDBGrid::MakeQueryFilter(Int_t firstRun, Int_t lastRun,
1092 const AliCDBMetaData* md, TString& result) const
1094 // create filter for file query
1096 result = Form("CDB:first_run<=%d and CDB:last_run>=%d", firstRun, lastRun);
1098 // if(version >= 0) {
1099 // result += Form(" and CDB:version=%d", version);
1101 // if(pathFilter.GetLevel0() != "*") {
1102 // result += Form(" and CDB:path_level_0=\"%s\"", pathFilter.GetLevel0().Data());
1104 // if(pathFilter.GetLevel1() != "*") {
1105 // result += Form(" and CDB:path_level_1=\"%s\"", pathFilter.GetLevel1().Data());
1107 // if(pathFilter.GetLevel2() != "*") {
1108 // result += Form(" and CDB:path_level_2=\"%s\"", pathFilter.GetLevel2().Data());
1112 if(md->GetObjectClassName()[0] != '\0') {
1113 result += Form(" and CDB_MD:object_classname=\"%s\"", md->GetObjectClassName());
1115 if(md->GetResponsible()[0] != '\0') {
1116 result += Form(" and CDB_MD:responsible=\"%s\"", md->GetResponsible());
1118 if(md->GetBeamPeriod() != 0) {
1119 result += Form(" and CDB_MD:beam_period=%d", md->GetBeamPeriod());
1121 if(md->GetAliRootVersion()[0] != '\0') {
1122 result += Form(" and CDB_MD:aliroot_version=\"%s\"", md->GetAliRootVersion());
1124 if(md->GetComment()[0] != '\0') {
1125 result += Form(" and CDB_MD:comment=\"%s\"", md->GetComment());
1128 AliDebug(2, Form("filter: %s",result.Data()));
1132 //_____________________________________________________________________________
1133 Int_t AliCDBGrid::GetLatestVersion(const char* path, Int_t run){
1134 // get last version found in the database valid for run and path
1136 AliCDBPath aCDBPath(path);
1137 if(!aCDBPath.IsValid() || aCDBPath.IsWildcard()) {
1138 AliError(Form("Invalid path in request: %s", path));
1141 AliCDBId query(path, run, run, -1, -1);
1143 AliCDBId* dataId = 0;
1144 // look for file matching query requests (path, runRange, version)
1145 if(run == fRun && fPathFilter.Comprises(aCDBPath) && fVersion < 0){
1146 // look into list of valid files previously loaded with AliCDBStorage::FillValidFileIds()
1147 AliDebug(2, Form("List of files valid for run %d and for path %s was loaded. Looking there!",
1149 dataId = GetId(fValidFileIds, query);
1150 if (!dataId) return -1;
1151 Int_t version = dataId->GetVersion();
1156 // List of files valid for reqested run was not loaded. Looking directly into CDB
1157 AliDebug(2, Form("List of files valid for run %d and for path %s was not loaded. Looking directly into CDB!",
1160 TObjArray validFileIds;
1161 validFileIds.SetOwner(1);
1164 MakeQueryFilter(run, run, 0, filter);
1166 TString pattern = ".root";
1168 TString folderCopy(Form("%s%s/Run",fDBFolder.Data(),path));
1170 AliDebug(2,Form("** fDBFolder = %s, pattern = %s, filter = %s",folderCopy.Data(), pattern.Data(), filter.Data()));
1171 TGridResult *res = gGrid->Query(folderCopy, pattern, filter, "-y -m");
1173 AliCDBId validFileId;
1174 if (res->GetEntries()>1){
1175 AliWarning("Number of found entries >1, even if option -y was used");
1176 for(int i=0; i<res->GetEntries(); i++){
1177 TString filename = res->GetKey(i, "lfn");
1178 if(filename == "") continue;
1179 if(FilenameToId(filename, validFileId))
1180 validFileIds.AddLast(validFileId.Clone());
1182 dataId = GetId(validFileIds, query);
1183 if (!dataId) return -1;
1185 Int_t version = dataId->GetVersion();
1189 else if (res->GetEntries()==1){
1190 TString filename = res->GetKey(0, "lfn");
1191 if(filename == "") {
1192 AliError("The only entry found has filename empty");
1195 if(FilenameToId(filename, validFileId)) return validFileId.GetVersion();
1197 AliError("Impossible to get FileId from filename");
1202 AliError("No entries found");
1211 //_____________________________________________________________________________
1212 Int_t AliCDBGrid::GetLatestSubVersion(const char* /*path*/, Int_t /*run*/, Int_t /*version*/){
1213 // get last subversion found in the database valid for run and path
1214 AliError("Objects in GRID storage have no sub version!");
1219 /////////////////////////////////////////////////////////////////////////////////////////////////
1221 // AliCDBGrid factory //
1223 /////////////////////////////////////////////////////////////////////////////////////////////////
1225 ClassImp(AliCDBGridFactory)
1227 //_____________________________________________________________________________
1228 Bool_t AliCDBGridFactory::Validate(const char* gridString) {
1229 // check if the string is valid Grid URI
1231 TRegexp gridPattern("^alien://.+$");
1233 return TString(gridString).Contains(gridPattern);
1236 //_____________________________________________________________________________
1237 AliCDBParam* AliCDBGridFactory::CreateParameter(const char* gridString) {
1238 // create AliCDBGridParam class from the URI string
1240 if (!Validate(gridString)) {
1244 TString buffer(gridString);
1246 TString gridUrl = "alien://";
1248 TString dbFolder = "";
1249 TString se = "default";
1250 TString cacheFolder = "";
1251 Bool_t operateDisconnected = kTRUE;
1252 Long64_t cacheSize = (UInt_t) 1024*1024*1024; // 1GB
1253 Long_t cleanupInterval = 0;
1255 TObjArray *arr = buffer.Tokenize('?');
1257 TObjString *str = 0;
1259 while((str = (TObjString*) iter.Next())){
1260 TString entry(str->String());
1261 Int_t indeq = entry.Index('=');
1263 if(entry.BeginsWith("alien://")) { // maybe it's a gridUrl!
1267 AliError(Form("Invalid entry! %s",entry.Data()));
1272 TString key = entry(0,indeq);
1273 TString value = entry(indeq+1,entry.Length()-indeq);
1275 if(key.Contains("grid",TString::kIgnoreCase)) {
1278 else if (key.Contains("user",TString::kIgnoreCase)){
1281 else if (key.Contains("se",TString::kIgnoreCase)){
1284 else if (key.Contains("cacheF",TString::kIgnoreCase)){
1285 cacheFolder = value;
1286 if (!cacheFolder.IsNull() && !cacheFolder.EndsWith("/"))
1289 else if (key.Contains("folder",TString::kIgnoreCase)){
1292 else if (key.Contains("operateDisc",TString::kIgnoreCase)){
1293 if(value == "kTRUE") {
1294 operateDisconnected = kTRUE;
1295 } else if (value == "kFALSE") {
1296 operateDisconnected = kFALSE;
1297 } else if (value == "0" || value == "1") {
1298 operateDisconnected = (Bool_t) value.Atoi();
1300 AliError(Form("Invalid entry! %s",entry.Data()));
1304 else if (key.Contains("cacheS",TString::kIgnoreCase)){
1305 if(value.IsDigit()) {
1306 cacheSize = value.Atoi();
1308 AliError(Form("Invalid entry! %s",entry.Data()));
1312 else if (key.Contains("cleanupInt",TString::kIgnoreCase)){
1313 if(value.IsDigit()) {
1314 cleanupInterval = value.Atoi();
1316 AliError(Form("Invalid entry! %s",entry.Data()));
1321 AliError(Form("Invalid entry! %s",entry.Data()));
1327 AliDebug(2, Form("gridUrl: %s", gridUrl.Data()));
1328 AliDebug(2, Form("user: %s", user.Data()));
1329 AliDebug(2, Form("dbFolder: %s", dbFolder.Data()));
1330 AliDebug(2, Form("s.e.: %s", se.Data()));
1331 AliDebug(2, Form("local cache folder: %s", cacheFolder.Data()));
1332 AliDebug(2, Form("local cache operate disconnected: %d", operateDisconnected));
1333 AliDebug(2, Form("local cache size: %lld", cacheSize));
1334 AliDebug(2, Form("local cache cleanup interval: %ld", cleanupInterval));
1337 AliError("Base folder must be specified!");
1341 return new AliCDBGridParam(gridUrl.Data(), user.Data(),
1342 dbFolder.Data(), se.Data(), cacheFolder.Data(),
1343 operateDisconnected, cacheSize, cleanupInterval);
1346 //_____________________________________________________________________________
1347 AliCDBStorage* AliCDBGridFactory::Create(const AliCDBParam* param) {
1348 // create AliCDBGrid storage instance from parameters
1350 AliCDBGrid *grid = 0;
1351 if (AliCDBGridParam::Class() == param->IsA()) {
1353 const AliCDBGridParam* gridParam = (const AliCDBGridParam*) param;
1354 grid = new AliCDBGrid(gridParam->GridUrl().Data(),
1355 gridParam->GetUser().Data(),
1356 gridParam->GetDBFolder().Data(),
1357 gridParam->GetSE().Data(),
1358 gridParam->GetCacheFolder().Data(),
1359 gridParam->GetOperateDisconnected(),
1360 gridParam->GetCacheSize(),
1361 gridParam->GetCleanupInterval());
1365 if(!gGrid && grid) {
1366 delete grid; grid=0;
1372 /////////////////////////////////////////////////////////////////////////////////////////////////
1374 // AliCDBGrid Parameter class // //
1376 /////////////////////////////////////////////////////////////////////////////////////////////////
1378 ClassImp(AliCDBGridParam)
1380 //_____________________________________________________________________________
1381 AliCDBGridParam::AliCDBGridParam():
1388 fOperateDisconnected(),
1393 // default constructor
1397 //_____________________________________________________________________________
1398 AliCDBGridParam::AliCDBGridParam(const char* gridUrl, const char* user, const char* dbFolder,
1399 const char* se, const char* cacheFolder, Bool_t operateDisconnected,
1400 Long64_t cacheSize, Long_t cleanupInterval):
1404 fDBFolder(dbFolder),
1406 fCacheFolder(cacheFolder),
1407 fOperateDisconnected(operateDisconnected),
1408 fCacheSize(cacheSize),
1409 fCleanupInterval(cleanupInterval)
1415 TString uri = Form("%s?User=%s?DBFolder=%s?SE=%s?CacheFolder=%s"
1416 "?OperateDisconnected=%d?CacheSize=%lld?CleanupInterval=%ld",
1417 fGridUrl.Data(), fUser.Data(),
1418 fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1419 fOperateDisconnected, fCacheSize, fCleanupInterval);
1424 //_____________________________________________________________________________
1425 AliCDBGridParam::~AliCDBGridParam() {
1430 //_____________________________________________________________________________
1431 AliCDBParam* AliCDBGridParam::CloneParam() const {
1434 return new AliCDBGridParam(fGridUrl.Data(), fUser.Data(),
1435 fDBFolder.Data(), fSE.Data(), fCacheFolder.Data(),
1436 fOperateDisconnected, fCacheSize, fCleanupInterval);
1439 //_____________________________________________________________________________
1440 ULong_t AliCDBGridParam::Hash() const {
1441 // return Hash function
1443 return fGridUrl.Hash()+fUser.Hash()+fDBFolder.Hash()+fSE.Hash()+fCacheFolder.Hash();
1446 //_____________________________________________________________________________
1447 Bool_t AliCDBGridParam::IsEqual(const TObject* obj) const {
1448 // check if this object is equal to AliCDBParam obj
1454 if (AliCDBGridParam::Class() != obj->IsA()) {
1458 AliCDBGridParam* other = (AliCDBGridParam*) obj;
1460 if(fGridUrl != other->fGridUrl) return kFALSE;
1461 if(fUser != other->fUser) return kFALSE;
1462 if(fDBFolder != other->fDBFolder) return kFALSE;
1463 if(fSE != other->fSE) return kFALSE;
1464 if(fCacheFolder != other->fCacheFolder) return kFALSE;
1465 if(fOperateDisconnected != other->fOperateDisconnected) return kFALSE;
1466 if(fCacheSize != other->fCacheSize) return kFALSE;
1467 if(fCleanupInterval != other->fCleanupInterval) return kFALSE;