forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.cxx
More file actions
1864 lines (1582 loc) · 55.4 KB
/
Copy pathManager.cxx
File metadata and controls
1864 lines (1582 loc) · 55.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "CCDB/Manager.h"
#include <fairlogger/Logger.h> // for LOG
#include <TGrid.h> // for gGrid, TGrid
#include <TKey.h> // for TKey
#include <TMessage.h> // for TMessage
#include <TObjString.h> // for TObjString
#include <TRegexp.h> // for TRegexp
#include <TSAXParser.h> // for TSAXParser
#include <TUUID.h> // for TUUID
#include "CCDB/Condition.h" // for Condition
#include "CCDB/FileStorage.h" // for FileStorageFactory
#include "CCDB/GridStorage.h" // for GridStorageFactory
#include "CCDB/LocalStorage.h" // for LocalStorageFactory
#include "TFile.h" // for TFile
#include "TSystem.h" // for TSystem, gSystem
#include "CCDB/XmlHandler.h" // for XmlHandler
using namespace o2::ccdb;
ClassImp(StorageParameters)
ClassImp(Manager)
TString Manager::sOcdbFolderXmlFile("alien:///alice/data/OCDBFoldervsIdRunRange.xml");
Manager *Manager::sInstance = nullptr;
Manager *Manager::Instance(TMap *entryCache, Int_t run)
{
// returns Manager instance (singleton)
if (!sInstance) {
sInstance = new Manager();
if (!entryCache) {
sInstance->init();
} else {
sInstance->initFromCache(entryCache, run);
}
}
return sInstance;
}
void Manager::init()
{
// factory registering
registerFactory(new FileStorageFactory());
registerFactory(new LocalStorageFactory());
// GridStorageFactory is registered only if AliEn libraries are enabled in Root
if (!gSystem->Exec("root-config --has-alien 2>/dev/null |grep yes 2>&1 > /dev/null")) { // returns 0 if yes
LOG(INFO) << "AliEn classes enabled in Root. GridStorage factory registered.";
registerFactory(new GridStorageFactory());
}
}
void Manager::initFromCache(TMap *entryCache, Int_t run)
{
// initialize manager from existing cache
// used on the slaves in case of parallel reconstruction
setRun(run);
TIter iter(entryCache->GetTable());
TPair *pair = nullptr;
while ((pair = dynamic_cast<TPair *>(iter.Next()))) {
mConditionCache.Add(pair->Key(), pair->Value());
}
// mCondition is the new owner of the cache
mConditionCache.SetOwnerKeyValue(kTRUE, kTRUE);
entryCache->SetOwnerKeyValue(kFALSE, kFALSE);
LOG(INFO) << mConditionCache.GetEntries() << " cache entries have been loaded";
}
void Manager::dumpToSnapshotFile(const char *snapshotFileName, Bool_t singleKeys) const
{
//
// If singleKeys is true, dump the entries map and the ids list to the snapshot file
// (provided mostly for historical reasons, the file is then read with initFromSnapshot),
// otherwise write to file each Condition separately (the is the preferred way, the file
// is then read with setSnapshotMode).
// open the file
TFile *f = TFile::Open(snapshotFileName, "RECREATE");
if (!f || f->IsZombie()) {
LOG(ERROR) << "Cannot open file " << snapshotFileName;
return;
}
LOG(INFO) << "Dumping entriesMap (entries'cache) with " << mConditionCache.GetEntries() << " entries!";
LOG(INFO) << "Dumping entriesList with " << mIds->GetEntries() << "entries!";
f->cd();
if (singleKeys) {
f->WriteObject(&mConditionCache, "CDBentriesMap");
f->WriteObject(mIds, "CDBidsList");
} else {
// We write the entries one by one named by their calibration path
TIter iter(mConditionCache.GetTable());
TPair *pair = nullptr;
while ((pair = dynamic_cast<TPair *>(iter.Next()))) {
TObjString *os = dynamic_cast<TObjString *>(pair->Key());
if (!os) {
continue;
}
TString path = os->GetString();
Condition *entry = dynamic_cast<Condition *>(pair->Value());
if (!entry) {
continue;
}
path.ReplaceAll("/", "*");
entry->Write(path.Data());
}
}
f->Close();
delete f;
}
void Manager::dumpToLightSnapshotFile(const char *lightSnapshotFileName) const
{
// The light snapshot does not contain the CDB objects (Entries) but
// only the information identifying them, that is the map of storages and
// the list of Ids, as in the UserInfo of AliESDs.root
// open the file
TFile *f = TFile::Open(lightSnapshotFileName, "RECREATE");
if (!f || f->IsZombie()) {
LOG(ERROR) << "Cannot open file " << lightSnapshotFileName;
return;
}
LOG(INFO) << "Dumping map of storages with " << mStorageMap->GetEntries() << " entries!";
LOG(INFO) << "Dumping entriesList with " << mIds->GetEntries() << " entries!";
f->WriteObject(mStorageMap, "cdbStoragesMap");
f->WriteObject(mIds, "CDBidsList");
f->Close();
delete f;
}
Bool_t Manager::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 (mLock) {
LOG(ERROR) << "Being locked I cannot initialize from the snapshot!";
return kFALSE;
}
// open the file
TString snapshotFile(snapshotFileName);
if (snapshotFile.BeginsWith("alien://")) {
if (!gGrid) {
TGrid::Connect("alien://", "");
if (!gGrid) {
LOG(ERROR) << "Connection to alien failed!";
return kFALSE;
}
}
}
TFile *f = TFile::Open(snapshotFileName);
if (!f || f->IsZombie()) {
LOG(ERROR) << "Cannot open file " << snapshotFileName;
return kFALSE;
}
// retrieve entries' map from snapshot file
TMap *entriesMap = nullptr;
TIter next(f->GetListOfKeys());
TKey *key;
while ((key = (TKey *) next())) {
if (strcmp(key->GetClassName(), "TMap") != 0) {
continue;
}
entriesMap = (TMap *) key->ReadObj();
break;
}
if (!entriesMap || entriesMap->GetEntries() == 0) {
LOG(ERROR) << "Cannot get valid map of CDB entries from snapshot file";
return kFALSE;
}
// retrieve ids' list from snapshot file
TList *idsList = nullptr;
TIter nextKey(f->GetListOfKeys());
TKey *keyN;
while ((keyN = (TKey *) nextKey())) {
if (strcmp(keyN->GetClassName(), "TList") != 0) {
continue;
}
idsList = (TList *) keyN->ReadObj();
break;
}
if (!idsList || idsList->GetEntries() == 0) {
LOG(ERROR) << "Cannot get valid list of CDB entries from snapshot file";
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 = nullptr;
Int_t nAdded = 0;
while ((pair = dynamic_cast<TPair *>(iterObj.Next()))) {
TObjString *os = (TObjString *) pair->Key();
TString path = os->GetString();
TIter iterId(idsList);
ConditionId *id = nullptr;
ConditionId *correspondingId = nullptr;
while ((id = dynamic_cast<ConditionId *>(iterId.Next()))) {
TString idpath(id->getPathString());
if (idpath == path) {
correspondingId = id;
break;
}
}
if (!correspondingId) {
LOG(ERROR) << R"(id for ")" << path.Data()
<< R"(" not found in the snapshot (while entry was). This entry is skipped!)";
break;
}
Bool_t cached = mConditionCache.Contains(path.Data());
Bool_t registeredId = kFALSE;
TIter iter(mIds);
ConditionId *idT = nullptr;
while ((idT = dynamic_cast<ConditionId *>(iter.Next()))) {
if (idT->getPathString() == path) {
registeredId = kTRUE;
break;
}
}
if (overwrite) {
if (cached || registeredId) {
LOG(WARNING) << R"(An entry was already cached for ")" << path.Data()
<< R"(". Removing it before caching from snapshot)";
unloadFromCache(path.Data());
}
mConditionCache.Add(pair->Key(), pair->Value());
mIds->Add(id);
nAdded++;
} else {
if (cached || registeredId) {
LOG(WARNING) << R"(An entry was already cached for ")" << path.Data()
<< R"(". Not adding this object from snapshot)";
} else {
mConditionCache.Add(pair->Key(), pair->Value());
mIds->Add(id);
nAdded++;
}
}
}
// mCondition is the new owner of the cache
mConditionCache.SetOwnerKeyValue(kTRUE, kTRUE);
entriesMap->SetOwnerKeyValue(kFALSE, kFALSE);
mIds->SetOwner(kTRUE);
idsList->SetOwner(kFALSE);
LOG(INFO) << nAdded << " new (entry,id) cached. Total number " << mConditionCache.GetEntries();
f->Close();
delete f;
return kTRUE;
}
void Manager::destroy()
{
// delete ALCDBManager instance and active storages
if (sInstance) {
delete sInstance;
sInstance = nullptr;
}
}
Manager::Manager()
: TObject(),
mFactories(),
mActiveStorages(),
mSpecificStorages(),
mConditionCache(),
mIds(nullptr),
mStorageMap(nullptr),
mDefaultStorage(nullptr),
mdrainStorage(nullptr),
mOfficialStorageParameters(nullptr),
mReferenceStorageParameters(nullptr),
mRun(-1),
mCache(kTRUE),
mLock(kFALSE),
mSnapshotMode(kFALSE),
mSnapshotFile(nullptr),
mOcdbUploadMode(kFALSE),
mRaw(kFALSE),
mCvmfsOcdb(""),
mStartRunLhcPeriod(-1),
mEndRunLhcPeriod(-1),
mLhcPeriod(""),
mKey(0)
{
// default constuctor
mFactories.SetOwner(1);
mActiveStorages.SetOwner(1);
mSpecificStorages.SetOwner(1);
mConditionCache.SetName("CDBConditionCache");
mConditionCache.SetOwnerKeyValue(kTRUE, kTRUE);
mStorageMap = new TMap();
mStorageMap->SetOwner(1);
mIds = new TList();
mIds->SetOwner(1);
}
Manager::~Manager()
{
// destructor
clearCache();
destroyActiveStorages();
mFactories.Delete();
mdrainStorage = nullptr;
mDefaultStorage = nullptr;
delete mStorageMap;
mStorageMap = nullptr;
delete mIds;
mIds = nullptr;
delete mOfficialStorageParameters;
delete mReferenceStorageParameters;
if (mSnapshotMode) {
mSnapshotFile->Close();
mSnapshotFile = nullptr;
}
}
void Manager::putActiveStorage(StorageParameters *param, Storage *storage)
{
// put a storage object into the list of active storages
mActiveStorages.Add(param, storage);
LOG(DEBUG) << "Active storages: " << mActiveStorages.GetEntries();
}
void Manager::registerFactory(StorageFactory *factory)
{
// add a storage factory to the list of registerd factories
if (!mFactories.Contains(factory)) {
mFactories.Add(factory);
}
}
Bool_t Manager::hasStorage(const char *dbString) const
{
// check if dbString is a URI valid for one of the registered factories
TIter iter(&mFactories);
StorageFactory *factory = nullptr;
while ((factory = (StorageFactory *) iter.Next())) {
if (factory->validateStorageUri(dbString)) {
return kTRUE;
}
}
return kFALSE;
}
StorageParameters *Manager::createStorageParameter(const char *dbString) const
{
// create StorageParameters object from URI string
TString uriString(dbString);
if (!mCvmfsOcdb.IsNull() && uriString.BeginsWith("alien://")) {
alienToCvmfsUri(uriString);
}
TIter iter(&mFactories);
StorageFactory *factory = nullptr;
while ((factory = (StorageFactory *) iter.Next())) {
StorageParameters *param = factory->createStorageParameter(uriString);
if (param) {
return param;
}
}
return nullptr;
}
void Manager::alienToCvmfsUri(TString &uriString) const
{
// convert alien storage uri to local:///cvmfs storage uri (called when OCDB_PATH is set)
TObjArray *arr = uriString.Tokenize('?');
TIter iter(arr);
TObjString *str = nullptr;
TString entryKey = "";
TString entryValue = "";
TString newUriString = "";
while ((str = (TObjString *) iter.Next())) {
TString entry(str->String());
Int_t indeq = entry.Index('=');
entryKey = entry(0, indeq + 1);
entryValue = entry(indeq + 1, entry.Length() - indeq);
if (entryKey.Contains("folder", TString::kIgnoreCase)) {
TRegexp re_RawFolder("^/alice/data/20[0-9]+/OCDB");
TRegexp re_MCFolder("^/alice/simulation/2008/v4-15-Release");
TString rawFolder = entryValue(re_RawFolder);
TString mcFolder = entryValue(re_MCFolder);
if (!rawFolder.IsNull()) {
entryValue.Replace(0, 6, "/cvmfs/alice-ocdb.cern.ch/calibration");
// entryValue.Replace(entryValue.Length()-4, entryValue.Length(), "");
} else if (!mcFolder.IsNull()) {
entryValue.Replace(0, 36, "/cvmfs/alice-ocdb.cern.ch/calibration/MC");
} else {
LOG(FATAL) << "Environment variable for cvmfs OCDB folder set for an invalid OCDB storage:\n "
<< entryValue.Data();
}
} else {
newUriString += entryKey;
}
newUriString += entryValue;
newUriString += '?';
}
newUriString.Prepend("local://");
newUriString.Remove(TString::kTrailing, '?');
uriString = newUriString;
}
Storage *Manager::getStorage(const char *dbString)
{
// Get the CDB storage corresponding to the URI string passed as argument
// If "raw://" is passed, get the storage for the raw OCDB for the current run (mRun)
TString uriString(dbString);
if (uriString.EqualTo("raw://")) {
if (!mLhcPeriod.IsNull() && !mLhcPeriod.IsWhitespace()) {
return getDefaultStorage();
} else {
TString lhcPeriod("");
Int_t startRun = -1, endRun = -1;
getLHCPeriodAgainstAlienFile(mRun, lhcPeriod, startRun, endRun);
return getStorage(lhcPeriod.Data());
}
}
StorageParameters *param = createStorageParameter(dbString);
if (!param) {
LOG(ERROR) << "Failed to activate requested storage! Check URI: " << dbString;
return nullptr;
}
Storage *aStorage = getStorage(param);
delete param;
return aStorage;
}
Storage *Manager::getStorage(const StorageParameters *param)
{
// get storage object from StorageParameters object
// if the list of active storages already contains
// the requested storage, return it
Storage *aStorage = getActiveStorage(param);
if (aStorage) {
return aStorage;
}
// if lock is ON, cannot activate more storages!
if (mLock) {
if (mDefaultStorage) {
LOG(FATAL) << "Lock is ON, and default storage is already set: cannot reset it or activate "
"more storages!";
}
}
// loop on the list of registered factories
TIter iter(&mFactories);
StorageFactory *factory = nullptr;
while ((factory = (StorageFactory *) iter.Next())) {
// each factory tries to create its storage from the parameter
aStorage = factory->createStorage(param);
if (aStorage) {
putActiveStorage(param->cloneParam(), aStorage);
aStorage->setUri(param->getUri());
if (mRun >= 0) {
if (aStorage->getStorageType() == "alien" || aStorage->getStorageType() == "local") {
aStorage->queryStorages(mRun);
}
}
return aStorage;
}
}
LOG(ERROR) << "Failed to activate requested storage! Check URI: " << param->getUri().Data();
return nullptr;
}
Storage *Manager::getActiveStorage(const StorageParameters *param)
{
// get a storage object from the list of active storages
return dynamic_cast<Storage *>(mActiveStorages.GetValue(param));
}
TList *Manager::getActiveStorages()
{
// return list of active storages
// user has responsibility to delete returned object
TList *result = new TList();
TIter iter(mActiveStorages.GetTable());
TPair *aPair = nullptr;
while ((aPair = (TPair *) iter.Next())) {
result->Add(aPair->Value());
}
return result;
}
void Manager::setdrainMode(const char *dbString)
{
// set drain storage from URI string
mdrainStorage = getStorage(dbString);
}
void Manager::setdrainMode(const StorageParameters *param)
{
// set drain storage from StorageParameters
mdrainStorage = getStorage(param);
}
void Manager::setdrainMode(Storage *storage)
{
// set drain storage from another active storage
mdrainStorage = storage;
}
Bool_t Manager::drain(Condition *entry)
{
// drain retrieved object to drain storage
LOG(DEBUG) << "draining into drain storage...";
return mdrainStorage->putObject(entry);
}
Bool_t Manager::setOcdbUploadMode()
{
// Set the framework in official upload mode. This tells the framework to upload
// objects to cvmfs after they have been uploaded to AliEn OCDBs.
// It return false if the executable to upload to cvmfs is not found.
TString cvmfsUploadExecutable("$HOME/bin/ocdb-cvmfs");
gSystem->ExpandPathName(cvmfsUploadExecutable);
if (gSystem->AccessPathName(cvmfsUploadExecutable)) {
return kFALSE;
}
mOcdbUploadMode = kTRUE;
return kTRUE;
}
void Manager::setDefaultStorage(const char *storageUri)
{
// sets default storage from URI string
// if in the cvmfs case (triggered by environment variable) check for path validity
// and modify Uri if it is "raw://"
TString cvmfsOcdb(gSystem->Getenv("OCDB_PATH"));
if (!cvmfsOcdb.IsNull()) {
mCvmfsOcdb = cvmfsOcdb;
validateCvmfsCase();
}
// checking whether we are in the raw case
TString uriTemp(storageUri);
if (uriTemp == "raw://") {
mRaw = kTRUE; // read then by setRun to check if the method has to be called again with expanded uri
LOG(INFO) << "Setting the run-number will set the corresponding OCDB for raw data reconstruction.";
return;
}
Storage *bckStorage = mDefaultStorage;
mDefaultStorage = getStorage(storageUri);
if (!mDefaultStorage) {
return;
}
if (bckStorage && (mDefaultStorage != bckStorage)) {
LOG(WARNING) << "Existing default storage replaced: clearing cache!";
clearCache();
}
if (mStorageMap->Contains("default")) {
delete mStorageMap->Remove(((TPair *) mStorageMap->FindObject("default"))->Key());
}
mStorageMap->Add(new TObjString("default"), new TObjString(mDefaultStorage->getUri()));
}
void Manager::setDefaultStorage(const StorageParameters *param)
{
// set default storage from StorageParameters object
Storage *bckStorage = mDefaultStorage;
mDefaultStorage = getStorage(param);
if (!mDefaultStorage) {
return;
}
if (bckStorage && (mDefaultStorage != bckStorage)) {
LOG(WARNING) << "Existing default storage replaced: clearing cache!";
clearCache();
}
if (mStorageMap->Contains("default")) {
delete mStorageMap->Remove(((TPair *) mStorageMap->FindObject("default"))->Key());
}
mStorageMap->Add(new TObjString("default"), new TObjString(mDefaultStorage->getUri()));
}
void Manager::setDefaultStorage(Storage *storage)
{
// set default storage from another active storage
// if lock is ON, cannot activate more storages!
if (mLock) {
if (mDefaultStorage) {
LOG(FATAL) << "Lock is ON, and default storage is already set: cannot reset it or activate "
"more storages!";
}
}
if (!storage) {
unsetDefaultStorage();
return;
}
Storage *bckStorage = mDefaultStorage;
mDefaultStorage = storage;
if (bckStorage && (mDefaultStorage != bckStorage)) {
LOG(WARNING) << "Existing default storage replaced: clearing cache!";
clearCache();
}
if (mStorageMap->Contains("default")) {
delete mStorageMap->Remove(((TPair *) mStorageMap->FindObject("default"))->Key());
}
mStorageMap->Add(new TObjString("default"), new TObjString(mDefaultStorage->getUri()));
}
void Manager::validateCvmfsCase() const
{
// The OCDB_PATH variable contains the path to the directory in /cvmfs/ which is
// an AliRoot tag based snapshot of the AliEn file catalogue (e.g.
// /cvmfs/alice.cern.ch/x86_64-2.6-gnu-4.1.2/Packages/OCDB/v5-05-76-AN).
// The directory has to contain:
// 1) <data|MC>/20??.list.gz gzipped text files listing the OCDB files (seen by that AliRoot tag)
// 2) bin/getOCDBFilesPerRun.sh (shell+awk) script extracting from 1) the list
// of valid files for the given run.
if (!mCvmfsOcdb.BeginsWith("/cvmfs")) //!!!! to be commented out for testing
LOG(FATAL) << "OCDB_PATH set to an invalid path: " << mCvmfsOcdb.Data();
TString cvmfsUri(mCvmfsOcdb);
gSystem->ExpandPathName(cvmfsUri);
if (gSystem->AccessPathName(cvmfsUri))
LOG(FATAL) << "OCDB_PATH set to an invalid path: " << cvmfsUri.Data();
// check that we find the two scripts we need
LOG(DEBUG) << "OCDB_PATH envvar is set. Changing OCDB storage from alien:// to local:///cvmfs type.";
cvmfsUri = cvmfsUri.Strip(TString::kTrailing, '/');
cvmfsUri.Append("/bin/getOCDBFilesPerRun.sh");
if (gSystem->AccessPathName(cvmfsUri))
LOG(FATAL) << "Cannot find valid script: " << cvmfsUri.Data();
}
void Manager::setDefaultStorageFromRun(Int_t run)
{
// set default storage from the run number - to be used only with raw data
// if lock is ON, cannot activate more storages!
if (mLock) {
if (mDefaultStorage) {
LOG(FATAL) << "Lock is ON, and default storage is already set: cannot activate default "
"storage from run number";
}
}
TString lhcPeriod("");
Int_t startRun = 0, endRun = 0;
if (!mCvmfsOcdb.IsNull()) { // mRaw and cvmfs case: set LHC period from cvmfs file
getLHCPeriodAgainstCvmfsFile(run, lhcPeriod, startRun, endRun);
} else { // mRaw: set LHC period from AliEn XML file
getLHCPeriodAgainstAlienFile(run, lhcPeriod, startRun, endRun);
}
mLhcPeriod = lhcPeriod;
mStartRunLhcPeriod = startRun;
mEndRunLhcPeriod = endRun;
setDefaultStorage(mLhcPeriod.Data());
if (!mDefaultStorage)
LOG(FATAL) << mLhcPeriod.Data() << " storage not there! Please check!";
}
void Manager::getLHCPeriodAgainstAlienFile(Int_t run, TString &lhcPeriod, Int_t &startRun, Int_t &endRun)
{
// set LHC period (year + first, last run) comparing run number and AliEn XML file
// retrieve XML file from alien
if (!gGrid) {
TGrid::Connect("alien://", "");
if (!gGrid) {
LOG(ERROR) << "Connection to alien failed!";
return;
}
}
TUUID uuid;
TString rndname = "/tmp/";
rndname += "OCDBFolderXML.";
rndname += uuid.AsString();
rndname += ".xml";
LOG(DEBUG) << "file to be copied = " << sOcdbFolderXmlFile.Data();
if (!TFile::Cp(sOcdbFolderXmlFile.Data(), rndname.Data())) {
LOG(FATAL) << "Cannot make a local copy of OCDBFolder xml file in " << rndname.Data();
}
XmlHandler *saxcdb = new XmlHandler();
saxcdb->setRun(run);
TSAXParser *saxParser = new TSAXParser();
saxParser->ConnectToHandler(" Handler", saxcdb);
saxParser->ParseFile(rndname.Data());
LOG(INFO) << " LHC folder = " << saxcdb->getOcdbFolder().Data();
LOG(INFO) << " LHC period start run = " << saxcdb->getStartIdRunRange();
LOG(INFO) << " LHC period end run = " << saxcdb->getEndIdRunRange();
lhcPeriod = saxcdb->getOcdbFolder();
startRun = saxcdb->getStartIdRunRange();
endRun = saxcdb->getEndIdRunRange();
}
void Manager::getLHCPeriodAgainstCvmfsFile(Int_t run, TString &lhcPeriod, Int_t &startRun, Int_t &endRun)
{
// set LHC period (year + first, last run) comparing run number and CVMFS file
// We don't want to connect to AliEn just to set the uri from the runnumber
// for that we use the script getUriFromYear.sh in the cvmfs AliRoot package
TString getYearScript(mCvmfsOcdb);
getYearScript = getYearScript.Strip(TString::kTrailing, '/');
getYearScript.Append("/bin/getUriFromYear.sh");
if (gSystem->AccessPathName(getYearScript))
LOG(FATAL) << "Cannot find valid script: " << getYearScript.Data();
TString inoutFile(gSystem->WorkingDirectory());
inoutFile += "/uri_range_";
inoutFile += TString::Itoa(run, 10);
TString command(getYearScript);
command += ' ';
command += TString::Itoa(run, 10);
command += Form(" > %s", inoutFile.Data());
LOG(DEBUG) << R"(Running command: ")" << command.Data() << R"(")";
Int_t result = gSystem->Exec(command.Data());
if (result != 0) {
LOG(FATAL) << R"(Was not able to execute ")" << command.Data() << R"(")";
}
// now read the file with the uri and first and last run
std::ifstream file(inoutFile.Data());
if (!file.is_open()) {
LOG(FATAL) << R"(Error opening file ")" << inoutFile.Data() << R"("!)";
}
TString line;
TObjArray *oStringsArray = nullptr;
while (line.ReadLine(file)) {
oStringsArray = line.Tokenize(' ');
}
TObjString *oStrUri = dynamic_cast<TObjString *>(oStringsArray->At(0));
TObjString *oStrFirst = dynamic_cast<TObjString *>(oStringsArray->At(1));
TString firstRun = oStrFirst->GetString();
TObjString *oStrLast = dynamic_cast<TObjString *>(oStringsArray->At(2));
TString lastRun = oStrLast->GetString();
lhcPeriod = oStrUri->GetString();
startRun = firstRun.Atoi();
endRun = lastRun.Atoi();
file.close();
}
void Manager::unsetDefaultStorage()
{
// Unset default storage
// if lock is ON, action is forbidden!
if (mLock) {
if (mDefaultStorage) {
LOG(FATAL) << "Lock is ON: cannot unset default storage!";
}
}
if (mDefaultStorage) {
LOG(WARNING) << "Clearing cache!";
clearCache();
}
mRun = mStartRunLhcPeriod = mEndRunLhcPeriod = -1;
mRaw = kFALSE;
mDefaultStorage = nullptr;
}
void Manager::setSpecificStorage(const char *calibType, const char *dbString, Int_t version, Int_t subVersion)
{
// sets storage specific for detector or calibration type (works with Manager::getObject(...))
StorageParameters *aPar = createStorageParameter(dbString);
if (!aPar) {
return;
}
setSpecificStorage(calibType, aPar, version, subVersion);
delete aPar;
}
void Manager::setSpecificStorage(const char *calibType, const StorageParameters *param, Int_t version, Int_t subVersion)
{
// sets storage specific for detector or calibration type (works with Manager::getObject(...))
// Default storage should be defined prior to any specific storages, e.g.:
// Manager::instance()->setDefaultStorage("alien://");
// Manager::instance()->setSpecificStorage("TPC/*","local://DB_TPC");
// Manager::instance()->setSpecificStorage("*/Align/*","local://DB_TPCAlign");
// calibType must be a valid CDB path! (3 level folder structure)
// Specific version/subversion is set in the uniqueid of the Param value stored in the
// specific storages map
if (!mDefaultStorage && !mRaw) {
LOG(ERROR) << "Please activate a default storage first!";
return;
}
IdPath aPath(calibType);
if (!aPath.isValid()) {
LOG(ERROR) << "Not a valid path: " << calibType;
return;
}
TObjString *objCalibType = new TObjString(aPath.getPathString());
if (mSpecificStorages.Contains(objCalibType)) {
LOG(WARNING) << R"(Storage ")" << calibType << R"(" already activated! It will be replaced by the new one)";
StorageParameters *checkPar = dynamic_cast<StorageParameters *>(mSpecificStorages.GetValue(calibType));
if (checkPar) {
delete checkPar;
}
delete mSpecificStorages.Remove(objCalibType);
}
Storage *aStorage = getStorage(param);
if (!aStorage) {
return;
}
// Set the unique id of the AliCDBParam stored in the map to store specific version/subversion
UInt_t uId = ((subVersion + 1) << 16) + (version + 1);
StorageParameters *specificParam = param->cloneParam();
specificParam->SetUniqueID(uId);
mSpecificStorages.Add(objCalibType, specificParam);
if (mStorageMap->Contains(objCalibType)) {
delete mStorageMap->Remove(objCalibType);
}
mStorageMap->Add(objCalibType->Clone(), new TObjString(param->getUri()));
}
Storage *Manager::getSpecificStorage(const char *calibType)
{
// get storage specific for detector or calibration type
IdPath calibPath(calibType);
if (!calibPath.isValid()) {
return nullptr;
}
StorageParameters *checkPar = (StorageParameters *) mSpecificStorages.GetValue(calibPath.getPathString());
if (!checkPar) {
LOG(ERROR) << calibType << " storage not found!";
return nullptr;
} else {
return getStorage(checkPar);
}
}
StorageParameters *Manager::selectSpecificStorage(const TString &path)
{
// select storage valid for path from the list of specific storages
IdPath aPath(path);
if (!aPath.isValid()) {
return nullptr;
}
TIter iter(&mSpecificStorages);
TObjString *aCalibType = nullptr;
IdPath tmpPath("null/null/null");
StorageParameters *aPar = nullptr;
while ((aCalibType = (TObjString *) iter.Next())) {
IdPath calibTypePath(aCalibType->GetName());
if (calibTypePath.isSupersetOf(aPath)) {
if (calibTypePath.isSupersetOf(tmpPath)) {
continue;
}
aPar = (StorageParameters *) mSpecificStorages.GetValue(aCalibType);
tmpPath.setPath(calibTypePath.getPathString());
}
}
return aPar;
}
Condition *Manager::getCondition(const IdPath &path, Int_t runNumber, Int_t version, Int_t subVersion)
{
// get an Condition object from the database
if (runNumber < 0) {
// RunNumber is not specified. Try with mRun
if (mRun < 0) {
LOG(ERROR) << "Run number neither specified in query nor set in Manager! Use Manager::setRun.";
return nullptr;
}
runNumber = mRun;
}
return getCondition(ConditionId(path, runNumber, runNumber, version, subVersion));
}
Condition *Manager::getCondition(const IdPath &path, const IdRunRange &runRange, Int_t version, Int_t subVersion)
{
// get an Condition object from the database!
return getCondition(ConditionId(path, runRange, version, subVersion));
}
Condition *Manager::getCondition(const ConditionId &queryId, Bool_t forceCaching)
{
// get an Condition object from the database
// check if queryId's path and runRange are valid
// queryId is invalid also if version is not specified and subversion is!
if (!queryId.isValid()) {
LOG(ERROR) << "Invalid query: " << queryId.ToString().Data();
return nullptr;
}
// query is not specified if path contains wildcard or run range= [-1,-1]
if (!queryId.isSpecified()) {
LOG(ERROR) << "Unspecified query: " << queryId.ToString().Data();
return nullptr;
}
if (mLock && !(mRun >= queryId.getFirstRun() && mRun <= queryId.getLastRun()))
LOG(FATAL) << "Lock is ON: cannot use different run number than the internal one!";
if (mCache && !(mRun >= queryId.getFirstRun() && mRun <= queryId.getLastRun()))
LOG(WARNING) << "Run number explicitly set in query: CDB cache temporarily disabled!";
Condition *entry = nullptr;
// first look into map of cached objects
if (mCache && queryId.getFirstRun() == mRun) {
entry = (Condition *) mConditionCache.GetValue(queryId.getPathString());
}
if (entry) {
LOG(DEBUG) << "Object " << queryId.getPathString().Data() << " retrieved from cache !!";
return entry;