#include <TKey.h>
#include <TUUID.h>
#include <TGrid.h>
+#include "TMessage.h"
+#include "TObject.h"
ClassImp(AliCDBParam)
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);
}
//_____________________________________________________________________________
-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
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<TPair*> (iter.Next()))){
+ TObjString *os = dynamic_cast<TObjString*>(pair->Key());
+ if (!os) continue;
+ TString path = os->GetString();
+ AliCDBEntry *entry = dynamic_cast<AliCDBEntry*>(pair->Value());
+ if (!entry) continue;
+ path.ReplaceAll("/","*");
+ entry->Write(path.Data());
+ }
+ }
f->Close();
delete f;
}
//_____________________________________________________________________________
-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://")){
return kFALSE;
}
- // retrieve entries map
+ // retrieve entries' map from snapshot file
TMap *entriesMap = 0;
TIter next(f->GetListOfKeys());
TKey *key;
return kFALSE;
}
- // retrieve ids list
+ // retrieve ids' list from snapshot file
TList *idsList = 0;
TIter nextKey(f->GetListOfKeys());
TKey *keyN;
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<TPair*> (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<AliCDBId*> (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<AliCDBId*> (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<AliCDBId*> (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;
}
fRun(-1),
fCache(kTRUE),
fLock(kFALSE),
+ fSnapshotMode(kFALSE),
+ fSnapshotFile(0),
fRaw(kFALSE),
fStartRunLHCPeriod(-1),
fEndRunLHCPeriod(-1),
delete fCondParam;
delete fRefParam;
delete fShortLived; fShortLived = 0x0;
+ //fSnapshotCache = 0;
+ //fSnapshotIdsList = 0;
+ if(fSnapshotMode){
+ fSnapshotFile->Close();
+ fSnapshotFile = 0;
+ }
}
//_____________________________________________________________________________
}
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()));
}
}
//_____________________________________________________________________________
}
//_____________________________________________________________________________
-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
// 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");
entry = aStorage->Get(query);
- if(entry && fCache && (query.GetFirstRun() == fRun)){
+ if(entry && fCache && (query.GetFirstRun()==fRun || forceCaching)){
CacheEntry(query.GetPath(), entry);
}
}
+//_____________________________________________________________________________
+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<AliCDBEntry*>(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
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) {
}
//_____________________________________________________________________________
-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) {
}
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) {
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();
}
+//_____________________________________________________________________________
+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)
{
//_____________________________________________________________________________
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;
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<AliCDBId*> (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<TPair*> (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<AliCDBId*> (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()));
}
//_____________________________________________________________________________
}
+//______________________________________________________________________________________________
+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; i<size1; i++) if (file1->Buffer()[i]!=file2->Buffer()[i]) countDiff++;
+ for(Int_t i=0; i<size1; i++)
+ if (buf1[i]!=buf2[i]) countDiff++;
+
+ if (countDiff>0){
+ 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()
{
}
//______________________________________________________________________________________________
-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.