X-Git-Url: http://git.uio.no/git/?a=blobdiff_plain;f=STEER%2FCDB%2FAliCDBManager.cxx;h=0957854ec1cbb1217255a841b8254a8004923f67;hb=09d5920fcfc6bd42b5941c9dbeb76e00be842cd5;hp=a1d81b0c780daa07ae787ed3dea2503a30979a6d;hpb=c11cdcf2a22224092e31d44d1ebab37554df5082;p=u%2Fmrichter%2FAliRoot.git diff --git a/STEER/CDB/AliCDBManager.cxx b/STEER/CDB/AliCDBManager.cxx index a1d81b0c780..0957854ec1c 100644 --- a/STEER/CDB/AliCDBManager.cxx +++ b/STEER/CDB/AliCDBManager.cxx @@ -35,6 +35,8 @@ #include #include #include +#include "TMessage.h" +#include "TObject.h" ClassImp(AliCDBParam) @@ -72,7 +74,7 @@ void AliCDBManager::Init() { RegisterFactory(new AliCDBDumpFactory()); RegisterFactory(new AliCDBLocalFactory()); // AliCDBGridFactory is registered only if AliEn libraries are enabled in Root - if(!gSystem->Exec("root-config --has-alien |grep yes 2>&1 > /dev/null")){ // returns 0 if yes + if(!gSystem->Exec("root-config --has-alien 2>/dev/null |grep yes 2>&1 > /dev/null")){ // returns 0 if yes AliInfo("AliEn classes enabled in Root. AliCDBGrid factory registered."); RegisterFactory(new AliCDBGridFactory()); fCondParam = CreateParameter(fgkCondUri); @@ -101,7 +103,7 @@ void AliCDBManager::InitFromCache(TMap *entryCache, Int_t run) { } //_____________________________________________________________________________ -void AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName){ +void AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName, Bool_t singleKeys){ // // dump the entries map and the ids list to // the output file @@ -113,13 +115,32 @@ void AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName){ return; } - Printf("\ndumping entriesMap (entries'cache) with %d entries!\n", fEntryCache.GetEntries()); - Printf("\ndumping entriesList with %d entries!\n", fIds->GetEntries()); + AliInfo(Form("Dumping entriesMap (entries'cache) with %d entries!\n", fEntryCache.GetEntries())); + AliInfo(Form("Dumping entriesList with %d entries!\n", fIds->GetEntries())); f->cd(); - f->WriteObject(&fEntryCache,"CDBentriesMap"); - f->WriteObject(fIds,"CDBidsList"); + if(singleKeys){ + f->WriteObject(&fEntryCache,"CDBentriesMap"); + f->WriteObject(fIds,"CDBidsList"); + }else{ + // We write the entries one by one named by their calibration path + /* + fEntryCache.Write("CDBentriesMap"); + fIds->Write("CDBidsList"); + */ + TIter iter(fEntryCache.GetTable()); + TPair* pair = 0; + while((pair = dynamic_cast (iter.Next()))){ + TObjString *os = dynamic_cast(pair->Key()); + if (!os) continue; + TString path = os->GetString(); + AliCDBEntry *entry = dynamic_cast(pair->Value()); + if (!entry) continue; + path.ReplaceAll("/","*"); + entry->Write(path.Data()); + } + } f->Close(); delete f; @@ -127,11 +148,17 @@ void AliCDBManager::DumpToSnapshotFile(const char* snapshotFileName){ } //_____________________________________________________________________________ -Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName){ +Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName, Bool_t overwrite){ // initialize manager from a CDB snapshot, that is add the entries // to the entries map and the ids to the ids list taking them from // the map and the list found in the input file +// if the manager is locked it cannot initialize from a snapshot + if(fLock) { + AliError("Being locked I cannot initialize from the snapshot!"); + return kFALSE; + } + // open the file TString snapshotFile(snapshotFileName); if(snapshotFile.BeginsWith("alien://")){ @@ -150,7 +177,7 @@ Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName){ return kFALSE; } - // retrieve entries map + // retrieve entries' map from snapshot file TMap *entriesMap = 0; TIter next(f->GetListOfKeys()); TKey *key; @@ -164,7 +191,7 @@ Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName){ return kFALSE; } - // retrieve ids list + // retrieve ids' list from snapshot file TList *idsList = 0; TIter nextKey(f->GetListOfKeys()); TKey *keyN; @@ -178,28 +205,70 @@ Bool_t AliCDBManager::InitFromSnapshot(const char* snapshotFileName){ return kFALSE; } + // Add each (entry,id) from the snapshot to the memory: entry to the cache, id to the list of ids. + // If "overwrite" is false: add the entry to the cache and its id to the list of ids + // only if neither of them is already there. + // If "overwrite" is true: write the snapshot entry,id in any case. If something + // was already there for that calibration type, remove it and issue a warning TIter iterObj(entriesMap->GetTable()); TPair* pair = 0; - + Int_t nAdded=0; while((pair = dynamic_cast (iterObj.Next()))){ - fEntryCache.Add(pair->Key(),pair->Value()); + TObjString* os = (TObjString*) pair->Key(); + TString path = os->GetString(); + TIter iterId(idsList); + AliCDBId* id=0; + AliCDBId* correspondingId=0; + while((id = dynamic_cast (iterId.Next()))){ + TString idpath(id->GetPath()); + if(idpath==path){ + correspondingId=id; + break; + } + } + if(!correspondingId){ + AliError(Form("id for \"%s\" not found in the snapshot (while entry was). This entry is skipped!",path.Data())); + break; + } + Bool_t cached = fEntryCache.Contains(path.Data()); + Bool_t registeredId = kFALSE; + TIter iter(fIds); + AliCDBId *idT = 0; + while((idT = dynamic_cast (iter.Next()))){ + if(idT->GetPath()==path){ + registeredId = kTRUE; + break; + } + } + + if(overwrite){ + if(cached || registeredId){ + AliWarning(Form("An entry was already cached for \"%s\". Removing it before caching from snapshot",path.Data())); + UnloadFromCache(path.Data()); + } + fEntryCache.Add(pair->Key(),pair->Value()); + fIds->Add(id); + nAdded++; + }else{ + if(cached || registeredId){ + AliWarning(Form("An entry was already cached for \"%s\". Not adding this object from snapshot",path.Data())); + }else{ + fEntryCache.Add(pair->Key(),pair->Value()); + fIds->Add(id); + nAdded++; + } + } } + // fEntry is the new owner of the cache fEntryCache.SetOwnerKeyValue(kTRUE,kTRUE); entriesMap->SetOwnerKeyValue(kFALSE,kFALSE); - AliInfo(Form("%d cache entries have been loaded",fEntryCache.GetEntries())); - - TIter iterId(idsList); - - AliCDBId* id=0; - while((id = dynamic_cast (iterId.Next()))){ - fIds->Add(id); - } fIds->SetOwner(kTRUE); idsList->SetOwner(kFALSE); + AliInfo(Form("%d new (entry,id) cached. Total number %d",nAdded,fEntryCache.GetEntries())); + f->Close(); delete f; - AliInfo(Form("%d cache ids loaded. Total number of ids is %d",idsList->GetEntries(),fIds->GetEntries())); return kTRUE; } @@ -232,6 +301,8 @@ AliCDBManager::AliCDBManager(): fRun(-1), fCache(kTRUE), fLock(kFALSE), + fSnapshotMode(kFALSE), + fSnapshotFile(0), fRaw(kFALSE), fStartRunLHCPeriod(-1), fEndRunLHCPeriod(-1), @@ -264,6 +335,12 @@ AliCDBManager::~AliCDBManager() { delete fCondParam; delete fRefParam; delete fShortLived; fShortLived = 0x0; + //fSnapshotCache = 0; + //fSnapshotIdsList = 0; + if(fSnapshotMode){ + fSnapshotFile->Close(); + fSnapshotFile = 0; + } } //_____________________________________________________________________________ @@ -548,7 +625,9 @@ void AliCDBManager::SetDefaultStorage(const char* mcString, const char* simType) } SetDefaultStorage(dbString.Data()); - if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",fLHCPeriod.Data())); + fStartRunLHCPeriod=0; + fEndRunLHCPeriod=AliCDBRunRange::Infinity(); + if(!fDefaultStorage) AliFatal(Form("%s storage not there! Please check!",dbString.Data())); } } //_____________________________________________________________________________ @@ -737,7 +816,7 @@ AliCDBEntry* AliCDBManager::Get(const AliCDBPath& path, } //_____________________________________________________________________________ -AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) { +AliCDBEntry* AliCDBManager::Get(const AliCDBId& query, Bool_t forceCaching) { // get an AliCDBEntry object from the database // check if query's path and runRange are valid @@ -765,27 +844,43 @@ AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) { // first look into map of cached objects if(fCache && query.GetFirstRun() == fRun) entry = (AliCDBEntry*) fEntryCache.GetValue(query.GetPath()); - if(entry) { AliDebug(2, Form("Object %s retrieved from cache !!",query.GetPath().Data())); return entry; } + // if snapshot flag is set, try getting from the snapshot + // but in the case a specific storage is specified for this path + AliCDBParam *aPar=SelectSpecificStorage(query.GetPath()); + if(!aPar){ + if(fSnapshotMode && query.GetFirstRun() == fRun) + { + entry = GetEntryFromSnapshot(query.GetPath()); + if(entry) { + AliInfo(Form("Object \"%s\" retrieved from the snapshot.",query.GetPath().Data())); + if(query.GetFirstRun() == fRun) // no need to check fCache, fSnapshotMode not possible otherwise + CacheEntry(query.GetPath(), entry); + + if(!fIds->Contains(&entry->GetId())) + fIds->Add(entry->GetId().Clone()); + + return entry; + } + } + } + + // Entry is not in cache (and, in case we are in snapshot mode, not in the snapshot either) + // => retrieve it from the storage and cache it!! if(!fDefaultStorage) { AliError("No storage set!"); return NULL; } - // Entry is not in cache -> retrieve it from CDB and cache it!! - AliCDBStorage *aStorage=0; - AliCDBParam *aPar=SelectSpecificStorage(query.GetPath()); -// Bool_t usedDefStorage=kTRUE; + AliCDBStorage *aStorage=0; if(aPar) { aStorage=GetStorage(aPar); TString str = aPar->GetURI(); AliDebug(2,Form("Looking into storage: %s",str.Data())); -// usedDefStorage=kFALSE; - } else { aStorage=GetDefaultStorage(); AliDebug(2,"Looking into default storage"); @@ -793,7 +888,7 @@ AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) { entry = aStorage->Get(query); - if(entry && fCache && (query.GetFirstRun() == fRun)){ + if(entry && fCache && (query.GetFirstRun()==fRun || forceCaching)){ CacheEntry(query.GetPath(), entry); } @@ -806,6 +901,58 @@ AliCDBEntry* AliCDBManager::Get(const AliCDBId& query) { } +//_____________________________________________________________________________ +AliCDBEntry* AliCDBManager::GetEntryFromSnapshot(const char* path) { + // get the entry from the open snapshot file + + TString sPath(path); + sPath.ReplaceAll("/","*"); + if(!fSnapshotFile){ + AliError("No snapshot file is open!"); + return 0; + } + AliCDBEntry *entry = dynamic_cast(fSnapshotFile->Get(sPath.Data())); + if(!entry){ + AliDebug(2,Form("Cannot get a CDB entry for \"%s\" from snapshot file",path)); + return 0; + } + + return entry; +} + +//_____________________________________________________________________________ +Bool_t AliCDBManager::SetSnapshotMode(const char* snapshotFileName) { +// set the manager in snapshot mode + + if(!fCache){ + AliError("Cannot set the CDB manage in snapshot mode if the cache is not active!"); + return kFALSE; + } + + //open snapshot file + TString snapshotFile(snapshotFileName); + if(snapshotFile.BeginsWith("alien://")){ + if(!gGrid) { + TGrid::Connect("alien://",""); + if(!gGrid) { + AliError("Connection to alien failed!"); + return kFALSE; + } + } + } + + fSnapshotFile = TFile::Open(snapshotFileName); + if (!fSnapshotFile || fSnapshotFile->IsZombie()){ + AliError(Form("Cannot open file %s",snapshotFileName)); + return kFALSE; + } + + AliInfo("The CDB manager is set in snapshot mode!"); + fSnapshotMode = kTRUE; + return kTRUE; + +} + //_____________________________________________________________________________ const char* AliCDBManager::GetURI(const char* path) { // return the URI of the storage where to look for path @@ -824,6 +971,35 @@ const char* AliCDBManager::GetURI(const char* path) { return 0; } +//_____________________________________________________________________________ +Int_t AliCDBManager::GetStartRunLHCPeriod(){ + // get the first run of validity + // for the current period + // if set + if(fStartRunLHCPeriod==-1) + AliWarning("Run-range not yet set for the current LHC period."); + return fStartRunLHCPeriod; +} + +//_____________________________________________________________________________ +Int_t AliCDBManager::GetEndRunLHCPeriod(){ + // get the last run of validity + // for the current period + // if set + if(fEndRunLHCPeriod==-1) + AliWarning("Run-range not yet set for the current LHC period."); + return fEndRunLHCPeriod; +} + +//_____________________________________________________________________________ +TString AliCDBManager::GetLHCPeriod(){ + // get the current LHC period string + // + if(fLHCPeriod.IsWhitespace() || fLHCPeriod.IsNull()) + AliWarning("LHC period (OCDB folder) not yet set"); + return fLHCPeriod; +} + //_____________________________________________________________________________ AliCDBId* AliCDBManager::GetId(const AliCDBPath& path, Int_t runNumber, Int_t version, Int_t subVersion) { @@ -1038,7 +1214,7 @@ TList* AliCDBManager::GetAll(const AliCDBId& query) { } //_____________________________________________________________________________ -Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const DataType type){ +Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* metaData, const char* mirrors, DataType type){ // store an AliCDBEntry object into the database if (object==0x0) { @@ -1047,13 +1223,13 @@ Bool_t AliCDBManager::Put(TObject* object, const AliCDBId& id, AliCDBMetaData* m } AliCDBEntry anEntry(object, id, metaData); - return Put(&anEntry, type); + return Put(&anEntry, mirrors, type); } //_____________________________________________________________________________ -Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){ +Bool_t AliCDBManager::Put(AliCDBEntry* entry, const char* mirrors, DataType type){ // store an AliCDBEntry object into the database if(type == kPrivate && !fDefaultStorage) { @@ -1106,7 +1282,12 @@ Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){ AliDebug(2,Form("Storing object into storage: %s", aStorage->GetURI().Data())); - Bool_t result = aStorage->Put(entry, type); + TString strMirrors(mirrors); + Bool_t result = kFALSE; + if(!strMirrors.IsNull() && !strMirrors.IsWhitespace()) + result = aStorage->Put(entry, mirrors, type); + else + result = aStorage->Put(entry, "", type); if(fRun >= 0) QueryCDB(); @@ -1115,6 +1296,27 @@ Bool_t AliCDBManager::Put(AliCDBEntry* entry, DataType type){ } +//_____________________________________________________________________________ +void AliCDBManager::SetMirrorSEs(const char* mirrors) +{ +// set mirror Storage Elements for the default storage, if it is of type "alien" + if(fDefaultStorage->GetType() != "alien"){ + AliInfo("The default storage is not of type \"alien\". Settings for Storage Elements are not taken into account!"); + return; + } + fDefaultStorage->SetMirrorSEs(mirrors); +} + +//_____________________________________________________________________________ +const char* AliCDBManager::GetMirrorSEs() const { +// get mirror Storage Elements for the default storage, if it is of type "alien" + if(fDefaultStorage->GetType() != "alien"){ + AliInfo("The default storage is not of type \"alien\". Settings for Storage Elements are not taken into account!"); + return ""; + } + return fDefaultStorage->GetMirrorSEs(); +} + //_____________________________________________________________________________ void AliCDBManager::CacheEntry(const char* path, AliCDBEntry* entry) { @@ -1218,7 +1420,8 @@ void AliCDBManager::ClearCache(){ //_____________________________________________________________________________ void AliCDBManager::UnloadFromCache(const char* path){ // unload cached object - +// that is remove the entry from the cache and the id from the list of ids +// if(!fActiveStorages.GetEntries()) { AliDebug(2, Form("No active storages. Object \"%s\" is not unloaded from cache", path)); return; @@ -1229,29 +1432,53 @@ void AliCDBManager::UnloadFromCache(const char* path){ if(!queryPath.IsWildcard()) { // path is not wildcard, get it directly from the cache and unload it! if(fEntryCache.Contains(path)){ - AliDebug(2, Form("Unloading object \"%s\" from cache", path)); + AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", path)); TObjString pathStr(path); delete fEntryCache.Remove(&pathStr); + // we do not remove from the list of Id's (it's not very coherent but we leave the + // id for the benefit of the userinfo) + /* + TIter iter(fIds); + AliCDBId *id = 0; + while((id = dynamic_cast (iter.Next()))){ + if(queryPath.Comprises(id->GetPath())) + delete fIds->Remove(id); + }*/ } else { - AliErrorF("Cache does not contain object \"%s\"!", path); + AliError(Form("Cache does not contain object \"%s\"!", path)); } - AliDebugF(2, "Cache entries: %d",fEntryCache.GetEntries()); + AliDebug(2, Form("Cache entries: %d",fEntryCache.GetEntries())); return; } // path is wildcard: loop on the cache and unload all comprised objects! TIter iter(fEntryCache.GetTable()); TPair* pair = 0; + Int_t removed=0; while((pair = dynamic_cast (iter.Next()))){ AliCDBPath entryPath = pair->Key()->GetName(); if(queryPath.Comprises(entryPath)) { - AliDebug(2, Form("Unloading object \"%s\" from cache", entryPath.GetPath().Data())); - TObjString pathStr(entryPath.GetPath().Data()); + AliDebug(2, Form("Unloading object \"%s\" from cache and from list of ids", entryPath.GetPath().Data())); + TObjString pathStr(entryPath.GetPath()); delete fEntryCache.Remove(&pathStr); + removed++; + + // we do not remove from the list of Id's (it's not very coherent but we leave the + // id for the benefit of the userinfo) + /* + TIter iterids(fIds); + AliCDBId *anId = 0; + while((anId = dynamic_cast (iterids.Next()))){ + AliCDBPath aPath = anId->GetPath(); + TString aPathStr = aPath.GetPath(); + if(queryPath.Comprises(aPath)) { + delete fIds->Remove(anId); + } + }*/ } } - AliDebug(2,Form("Cache entries: %d",fEntryCache.GetEntries())); + AliDebug(2,Form("Cache entries and ids removed: %d Remaining: %d",removed,fEntryCache.GetEntries())); } //_____________________________________________________________________________ @@ -1328,6 +1555,73 @@ const char* AliCDBManager::GetDataTypeName(DataType type) } +//______________________________________________________________________________________________ +Bool_t AliCDBManager::DiffObjects(const char *cdbFile1, const char *cdbFile2) const +{ + // Compare byte-by-byte the objects contained in the CDB entry in two different files, + // whose name is passed as input + // Return value: + // kTRUE - in case the content of the OCDB object (persistent part) is exactly the same + // kFALSE - otherwise + + TString f1Str(cdbFile1); + TString f2Str(cdbFile2); + if (!gGrid && ( f1Str.BeginsWith("alien://") || f2Str.BeginsWith("alien://") )) + TGrid::Connect("alien://"); + + TFile * f1 = TFile::Open(cdbFile1); + if (!f1){ + Printf("Cannot open file \"%s\"",cdbFile1); + return kFALSE; + } + TFile * f2 = TFile::Open(cdbFile2); + if (!f2){ + Printf("Cannot open file \"%s\"",cdbFile2); + return kFALSE; + } + + AliCDBEntry * entry1 = (AliCDBEntry*)f1->Get("AliCDBEntry"); + if (!entry1){ + Printf("Cannot get CDB entry from file \"%s\"",cdbFile1); + return kFALSE; + } + AliCDBEntry * entry2 = (AliCDBEntry*)f2->Get("AliCDBEntry"); + if (!entry2){ + Printf("Cannot get CDB entry from file \"%s\"",cdbFile2); + return kFALSE; + } + + // stream the two objects in the buffer of two TMessages + TObject* object1 = entry1->GetObject(); + TObject* object2 = entry2->GetObject(); + TMessage * file1 = new TMessage(TBuffer::kWrite); + file1->WriteObject(object1); + Int_t size1 = file1->Length(); + TMessage * file2 = new TMessage(TBuffer::kWrite); + file2->WriteObject(object2); + Int_t size2 = file2->Length(); + if (size1!=size2){ + Printf("Problem 2: OCDB entry of different size (%d,%d)",size1,size2); + return kFALSE; + } + + // if the two buffers have the same size, check that they are the same byte-by-byte + Int_t countDiff=0; + char* buf1 = file1->Buffer(); + char* buf2 = file2->Buffer(); + //for (Int_t i=0; iBuffer()[i]!=file2->Buffer()[i]) countDiff++; + for(Int_t i=0; i0){ + Printf("The CDB objects differ by %d bytes.", countDiff); + return kFALSE; + } + + Printf("The CDB objects are the same in the two files."); + return kTRUE; +} + //______________________________________________________________________________________________ void AliCDBManager::InitShortLived() { @@ -1384,7 +1678,7 @@ Bool_t AliCDBManager::IsShortLived(const char* path) } //______________________________________________________________________________________________ -ULong_t AliCDBManager::SetLock(Bool_t lock, ULong_t key){ +ULong64_t AliCDBManager::SetLock(Bool_t lock, ULong64_t key){ // To lock/unlock user must provide the key. A new key is provided after // each successful lock. User should always backup the returned key and // use it on next access.