-
Notifications
You must be signed in to change notification settings - Fork 1
/
stagerSDMS.py
1088 lines (830 loc) · 45.1 KB
/
stagerSDMS.py
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
#!/usr/bin/env python
b'This script requires python 3.4'
"""
Script to prepare staging of files according to stageing file.
Stager which reads staging file, sets stageMarker in `HPSS_<Target>` collection
and prepares staging to target.
For detailed documentation, see: README_StageXRD.md
"""
import sys
import os
import re
import json
import psutil
import logging as log
import time
import socket
import datetime
import shlex, subprocess
from subprocess import STDOUT, check_output
from mongoUtil import mongoDbUtil
import pymongo
from pymongo import results
from pymongo import errors
from pymongo import bulk
from pprint import pprint
##############################################
# -- GLOBAL CONSTANTS
SCRATCH_SPACE = "/global/projecta/projectdirs/starprod/stageArea"
SCRATCH_LIMIT = 10*1024 # in GB
SCRATCH_FREE_MIN = 1024 # in GB
HPSS_TAPE_ORDER_SCRIPT = "/usr/common/usg/bin/hpss_file_sorter.script"
HPSS_SPLIT_MAX = 10
META_MANAGER = "pstarxrdr1"
##############################################
# -- Check for a proper Python Version
if sys.version[0:3] < '3.0':
print ('Python version 3.0 or greater required (found: {0}).'.format(sys.version[0:5]))
sys.exit(-1)
# ----------------------------------------------------------------------------------
class stagerSDMS:
""" Stager to from HPSS at NERSC"""
# _________________________________________________________
def __init__(self, dbUtil, stageingFile):
self._stageingFile = stageingFile
self._scratchSpace = SCRATCH_SPACE
self._scratchLimit = SCRATCH_LIMIT
self._scratchFreeMin = SCRATCH_FREE_MIN
self._dbUtil = dbUtil
self._scratchSpaceFlag = True # still enough space
self._listOfStageTargets = ['XRD'] # , 'Disk']
self._listOfQueryItems = ['runyear', 'system', 'energy',
'trigger', 'production', 'day',
'runnumber', 'stream']
self._listOfTargets = ['picoDst', 'picoDstJet']
# -- Base folders for targets
self._baseFolders = {'picoDst': 'picodsts',
'picoDstJet': 'picodsts/JetPicoDsts'}
# -- base Collection Names
self._baseColl = {'picoDst': 'PicoDsts',
'picoDstJet': 'PicoDstsJets'}
# -- Get collections
self._addCollections()
# -- Get XRD staging parameters
self._getXRDStagingParameters()
# -- Get HPSS staging parameters
self._getHPSSStagingParameters()
# _________________________________________________________
def _readStagingFile(self):
"""Read in staging file."""
with open(self._stageingFile) as dataFile:
setList = json.load(dataFile)
try:
self._sets = setList['sets']
except:
print('Error reading staging file: no "sets" found')
sys.exit(-1)
try:
self._nCopies = setList['nCopies']
except:
self._nCopies = 1
# _________________________________________________________
def _addCollections(self):
"""Get collections from mongoDB."""
# -- Data server collection
self._collServerXRD = self._dbUtil.getCollection('XRD_DataServers')
# -- Process Locks
self._collProcessLocks = self._dbUtil.getCollection('Process_Locks')
# -- HPPS files collection
self._collsHPSSFiles = self._dbUtil.getCollection('HPSS_Files')
# -- Collections in HPSS - the 'truth'
self._collsHPSS = dict.fromkeys(self._listOfTargets)
for target in self._listOfTargets:
self._collsHPSS[target] = self._dbUtil.getCollection('HPSS_' + self._baseColl[target])
# -- Collections for the staging target
self._collsStageTarget = dict.fromkeys(self._listOfTargets)
self._collsStageTargetNew = dict.fromkeys(self._listOfTargets)
self._collsStageTargetMiss = dict.fromkeys(self._listOfTargets)
for target in self._listOfTargets:
self._collsStageTarget[target] = dict.fromkeys(self._listOfStageTargets)
self._collsStageTargetNew[target] = dict.fromkeys(self._listOfStageTargets)
self._collsStageTargetMiss[target] = dict.fromkeys(self._listOfStageTargets)
for stageTarget in self._listOfStageTargets:
self._collsStageTarget[target][stageTarget] = self._dbUtil.getCollection(stageTarget+'_'+ self._baseColl[target])
self._collsStageTargetNew[target][stageTarget] = self._dbUtil.getCollection(stageTarget+'_'+ self._baseColl[target]+'_new')
self._collsStageTargetMiss[target][stageTarget] = self._dbUtil.getCollection(stageTarget+'_'+ self._baseColl[target]+'_missing')
# -- Collection of files to stage from HPSS
self._collStageFromHPSS = self._dbUtil.getCollection('Stage_From_HPSS')
# -- Collection of files to stage to stageTarget
self._collsStageToStageTarget = dict.fromkeys(self._listOfStageTargets)
for stageTarget in self._listOfStageTargets:
self._collsStageToStageTarget[stageTarget] = self._dbUtil.getCollection('Stage_To_'+stageTarget)
# _________________________________________________________
def _getXRDStagingParameters(self):
"""Get staging parameters for XRD"""
self._stageXRD = dict()
self._stageXRD['timeOut'] = 1800
# -s isntead -v
self._stageXRD['xrdcpOptions'] = "-v --nopbar -S 4"
nEntries = self._collServerXRD.find().count()
self._stageXRD['tryMax'] = 10 * nEntries
self._stageXRD['server'] = dict()
doc = self._collServerXRD.find_one({'roles':'MENDEL_ONE_MANAGER'})
self._stageXRD['server']['MENDEL_1'] = doc['nodeName'] + '-ib.nersc.gov'
doc = self._collServerXRD.find_one({'roles':'MENDEL_TWO_MANAGER'})
self._stageXRD['server']['MENDEL_2'] = doc['nodeName'] + '-ib.nersc.gov'
doc = self._collServerXRD.find_one({'roles':'META_MANAGER'})
if doc:
self._stageXRD['server']['MENDEL_ALL'] = doc['nodeName'] + '.nersc.gov'
else:
self._stageXRD['server']['MENDEL_ALL'] = META_MANAGER + '.nersc.gov'
# _________________________________________________________
def _getHPSSStagingParameters(self):
"""Get staging parameters for HPSS"""
self._stageHPSS = dict()
self._stageHPSS['tapeOrderScript'] = HPSS_TAPE_ORDER_SCRIPT
self._stageHPSS['splitMax'] = HPSS_SPLIT_MAX
# _________________________________________________________
def prepareStaging(self):
"""Perpare staging as start of a new cycle"""
# -- start new staging cycle
if not self._dbUtil.checkSetProcessLock("staging_cycle_active"):
# -- Read in staging File
self._readStagingFile()
# -- Mark files to be staged in HPSS list
self.markFilesToBeStaged()
self.numberOfFilesToBeStaged()
# -- Get list of files to be staged
self.getListOfFilesFromHPSS()
# -- Get tape ordering for HPSS files
self.createTapeOrderingHPSS()
# _________________________________________________________
def markFilesToBeStaged(self):
"""Mark files to be staged from staging file in `HPSS_<target>`."""
# -- Reset previous stage markers
self._resetAllStagingMarks()
# -- Loop over every set from staging file one-by-one as stageSet
for stageSet in self._sets:
if not self._prepareSet(stageSet):
continue
# -- Set stage marker using the the stageSet as find query
self._collsHPSS[self._target].update_many(stageSet, {'$set': {self._targetField: True}})
# _________________________________________________________
def _prepareSet(self, stageSet):
"""Prepare set to be staged.
Do basic checks, returns False if set can't be staged
"""
# -- Check for stageTarget
try:
stageTarget = stageSet['stageTarget']
if stageTarget not in self._listOfStageTargets:
print('Error reading staging file: Unknown "stageTarget"', stageTarget)
self._stageTarget = None
return False
self._targetField = "staging.stageMarker{0}".format(stageTarget)
except:
print('Error reading staging file: no "stageTarget" found in set' , stageSet)
self._stageTarget = None
return False
# -- Check for target
try:
target = stageSet['target']
if target not in self._listOfTargets:
print('Error reading staging file: Unknown "target"', target)
self._target = None
return False
self._target = target
except:
print('Error reading staging file: no "target" found in set' , stageSet)
self._target = None
return False
# -- Clean up
del(stageSet['target'])
del(stageSet['stageTarget'])
# -- Check if query items are correct
for key, value in stageSet.items():
if "starDetails." in key:
if key[12:] not in self._listOfQueryItems:
print('Error reading staging file: Query item does not exist:', key, value)
return False
continue
if key not in self._listOfQueryItems:
print('Error reading staging file: Query item does not exist:', key, value)
return False
del(stageSet[key])
starKey = 'starDetails.' + key
stageSet[starKey] = value
return True
# _________________________________________________________
def _resetAllStagingMarks(self):
"""Reset all staging marks in `HPSS_<target>`."""
# -- Rest all staging markers
for target, collection in self._collsHPSS.items():
for targetKey in set().union(*(dic.keys() for dic in collection.distinct('staging'))):
targetField = "staging.{0}".format(targetKey)
collection.update_many({}, {'$set': {targetField: False}})
# _________________________________________________________
def numberOfFilesToBeStaged(self):
"""Prints number of all files to be staged `HPSS_<target>`."""
for target, collection in self._collsHPSS.items():
for targetKey in set().union(*(dic.keys() for dic in collection.distinct('staging'))):
targetField = "staging.{0}".format(targetKey)
nStaged = collection.find({targetField: True}).count()
print('For {0} in collection: {1}'.format(target, collection.name))
print(' Files to be staged with {0}: {1}'.format(targetField, nStaged))
# _________________________________________________________
def getListOfFilesFromHPSS(self):
"""Get list of files of type target to be retrieved from HPSS"""
# -- Loop over targets (picoDsts, etc.)
for target in self._listOfTargets:
# -- Loop over stage targets (XRD, disk)
for stageTarget in self._listOfStageTargets:
stageField = 'staging.stageMarker{0}'.format(stageTarget)
# -- Get all documents from HPSS to be staged
hpssDocs = list(self._collsHPSS[target].find({'target': target, stageField: True}))
docsSetHPSS = set([item['filePath'] for item in hpssDocs])
# -- Get all files on stageing Target
queryNCopies = 'storage.details.{}'.format(self._nCopies - 1)
stagedDocs = list(self._collsStageTarget[target][stageTarget].find({'storage.location': stageTarget,
'target': target,
queryNCopies: {"$exists": True}}))
docsSetStaged = set([item['filePath'] for item in stagedDocs])
# -- Document to be staged
docsToStage = docsSetHPSS - docsSetStaged
# -- Documents to be removed from stageTarget
docsToRemove = docsSetStaged - docsSetHPSS
# -- Get collection to stage from HPSS and to stageTarget
self._prepareStageColls(docsToStage, target, stageTarget)
# ____________________________________________________________________________
def _prepareStageColls(self, docsToStage, target, stageTarget):
"""Fill collection of files to stage.
- Fill collection to stage from HPSS to Disk
- Fill collection to stage from Disk to Target
"""
# -- Loop over all paths and gather information
for currentPath in docsToStage:
hpssDoc = self._collsHPSS[target].find_one({'filePath': currentPath})
if hpssDoc['isInTarFile']:
hpssFilePath = hpssDoc['fileFullPathTar']
else:
hpssFilePath = hpssDoc['fileFullPath']
# -- Get doc of actual file on HPSS
hpssDocFile = self._collsHPSSFiles.find_one({'fileFullPath': hpssFilePath})
# -- Create doc : stageDocFromHPSS
stageDocFromHPSS = {'fileFullPath': hpssFilePath,
'stageStatus': 'unstaged',
'target': target,
'stageTarget': stageTarget}
if hpssDoc['isInTarFile']:
stageDocFromHPSS['filesInTar'] = hpssDocFile['filesInTar']
stageDocFromHPSS['isInTarFile'] = True
# -- Update doc in collStageFromHPSS if doc exists otherwise add new
self._collStageFromHPSS.find_one_and_update({'fileFullPath': hpssFilePath},
{'$addToSet': {'listOfFiles': hpssDoc['fileFullPath']},
'$setOnInsert' : stageDocFromHPSS}, upsert = True)
# -- Get Update doc in collStageToStagingTarget
emptyList = []
stageDocToTarget = {
'filePath': currentPath,
'fileFullPath': hpssDoc['fileFullPath'],
'fileSize': hpssDoc['fileSize'],
'target': hpssDoc['target'],
'stageStatusHPSS': 'unstaged',
'stageStatusTarget': 'unstaged',
'note': emptyList}
# -- Basic set of stage targets if no document exists
if self._nCopies == 1:
stageDocToTarget['stageTargetList'] = ['MENDEL_ALL']
else:
stageDocToTarget['stageTargetList'] = ['MENDEL_1', 'MENDEL_2']
# -- Get list of sub cluster for stageTarget collection if document exists already
xrdDoc = self._collsStageTarget[target][stageTarget].find_one({'filePath': currentPath})
if (xrdDoc):
stageTargetList = []
for node in xrdDoc['storage']['details']:
nodeDoc = self._collServerXRD.find_one({'nodeName': node})
if not 'MENDEL_ONE_DATASERVER' in nodeDoc['roles']:
stageTargetList.append('MENDEL_1')
if not 'MENDEL_TWO_DATASERVER' in nodeDoc['roles']:
stageTargetList.append('MENDEL_2')
if len(stageTargetList) > 0:
stageDocToTarget['stageTargetList'] = stageTargetList
# -- Insert new doc in collStageToStagingTarget
try:
self._collsStageToStageTarget[stageTarget].insert(stageDocToTarget)
except:
pass
# ____________________________________________________________________________
def createTapeOrderingHPSS(self):
"""Create tape ordering"""
# -- Write order file
with open("{0}/orderMe.txt".format(self._scratchSpace), "w") as orderMe:
for hpssDocFile in self._collStageFromHPSS.find({'stageStatus':'unstaged'}):
print(hpssDocFile['fileFullPath'], file=orderMe)
# -- Call tape ordering script
cmdLine = '{0} {1}/orderMe.txt'.format(self._stageHPSS['tapeOrderScript'], self._scratchSpace)
cmd = shlex.split(cmdLine)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# -- Process output and update collection
orderIdx = 0
for text in iter(p.stdout.readline, b''):
self._collStageFromHPSS.find_one_and_update({'fileFullPath': text.decode("utf-8").rstrip(), 'stageStatus': 'unstaged'},
{'$set' : {'orderIdx': orderIdx}})
orderIdx += 1
# -- Clean up order file
os.remove("{0}/orderMe.txt".format(self._scratchSpace))
# -- Split stage set
self._splitStageFromHPSS()
# ____________________________________________________________________________
def _splitStageFromHPSS(self):
"""Split list in n parts"""
nAll = self._collStageFromHPSS.find({'stageStatus':'unstaged'}).count()
if nAll <= self._stageHPSS['splitMax']:
self._collStageFromHPSS.update_many({'stageStatus':'unstaged'}, {'$set' : {'stageGroup': 1}})
return
self._collStageFromHPSS.update_many({'stageStatus':'unstaged'}, {'$set' : {'stageGroup': -1}})
split = int(nAll / self._stageHPSS['splitMax'])
for splitIdx in range(1, self._stageHPSS['splitMax']+2):
for doc in self._collStageFromHPSS.find({'stageStatus':'unstaged',
'stageGroup': -1}, {'_id':True}).sort('orderIdx', pymongo.ASCENDING).limit(split):
self._collStageFromHPSS.update_one({'_id': doc['_id']}, {'$set': {'stageGroup': splitIdx}})
# ____________________________________________________________________________
def stageFromHPSS(self):
"""Stage list of files from HPSS on to scratch space
Implemented as single squential process to keep file ordering.
"""
lock = True
for stageGroup in sorted(self._collStageFromHPSS.find({'stageStatus': 'unstaged'}).distinct('stageGroup')):
if not self._dbUtil.checkSetProcessLock("stagingHPSS_{}".format(stageGroup)):
lock = False
break
if lock:
return
listOfFilesToStage = []
## -- Decide on to stage file or subFile
while True:
# -- Check if there is enough space on disk
if not self._checkScratchSpaceStatus():
break
now = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M')
# - Get next unstaged document and set status to staging
stageDoc = self._collStageFromHPSS.find_one_and_update({'stageStatus': 'unstaged', 'stageGroup': stageGroup},
{'$set':{'stageStatus': 'staging',
'timeStamp': now}}, sort=[('orderIdx', pymongo.ASCENDING)])
if not stageDoc:
break
# -- Use hsi to extract one file only
if not stageDoc['isInTarFile']:
self._extractHPSSFile(stageDoc['fileFullPath'], stageDoc['stageTarget'])
# -- Use htar to extract from a htar file
else:
extractFileWise = False
# -- more the 25% percent of all file need to be extracted -> Get the whole file
if stageDoc['filesInTar']*0.25 > len(stageDoc['listOfFiles']):
extractFileWise = True
self._extractHPSSTarFile(stageDoc['fileFullPath'], stageDoc['stageTarget'],
stageDoc['listOfFiles'], stageDoc['target'], extractFileWise)
self._dbUtil.unsetProcessLock("stagingHPSS_{}".format(stageGroup))
# ____________________________________________________________________________
def _checkScratchSpaceStatus(self):
"""Check free space on disk.
Returs true if still enough space to stage more from HPSS
"""
scratchSpaceFlag = True
freeSpace = self._getFreeSpaceOnScratchDisk()
usedSpace = self._getUsedSpaceOnStagingArea()
# -- Check that freespace is larger 1 TB, otherwise set flag to false
if freeSpace < self._scratchFreeMin:
scratchSpaceFlag = False
# -- Check that not more than the limit is used
if usedSpace > self._scratchLimit:
scratchSpaceFlag = False
self._scratchSpaceFlag = scratchSpaceFlag
return self._scratchSpaceFlag
# ____________________________________________________________________________
def _getFreeSpaceOnScratchDisk(self):
"""Get free space on disk in GB."""
# -- Call tape ordering script
cmdLine = '/usr/common/usg/bin/prjaquota starprod'
cmd = shlex.split(cmdLine)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for text in iter(p.stdout.readline, b''):
if 'starprod' in text.decode("utf-8").rstrip():
line = text.decode("utf-8").rstrip().split()
freeSpace = int(line[2]) - int(line[1]) # in GB all - used
break
return freeSpace
# ____________________________________________________________________________
def _getUsedSpaceOnStagingArea(self):
"""Get used space on stageing area in GB."""
pipe = [{'$match': {'stageStatusHPSS': 'staged', 'stageStatusTarget': { '$ne': "staged" } }},
{'$group': {'_id': None, 'total': {'$sum': '$fileSize'}}}]
usedSpace = 0
for stageTarget in self._listOfStageTargets:
for res in self._collsStageToStageTarget[stageTarget].aggregate(pipeline=pipe):
if res:
usedSpace += res['total']
return int(usedSpace / 1073741824.) # Bytes in GBytes
# ____________________________________________________________________________
def _extractHPSSFile(self, fileFullPath, stageTarget):
"""Extract one file only using hsi."""
print("NOT IMPEMENTED YET")
# self._collStageFromHPSS.find_one_and_update({'fileFullPath': fileFullPath)},
# {'$set' : {'stageStatus': 'staged'}})
# ____________________________________________________________________________
def _extractHPSSTarFile(self, fileFullPath, stageTarget, listOfFiles, target, extractFileWise):
"""Extract from HTAR files using htar."""
# -- Extract file-by-file and add it to staging target collection as staged
if extractFileWise:
isExctractSucessful = True
for targetFile in listOfFiles:
# -- htar of single file
cmdLine = 'htar -xf {0} {1}'.format(fileFullPath, targetFile)
cmd = shlex.split(cmdLine)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, cwd=self._scratchSpace)
isFileExctractSucessful = False
for lineTerminated in iter(p.stdout.readline, b''):
line = lineTerminated.decode("utf-8").rstrip('\t\n')
lineCleaned = ' '.join(line.split())
if lineCleaned == "HTAR: HTAR SUCCESSFUL":
isfileExctractSucessful = True
break
isExctractSucessful += isfileExctractSucessful
# -- Update staging traget if extraction was successful
if isfileExctractSucessful:
self._collsStageToStageTarget[stageTarget].find_one_and_update({'fileFullPath': targetFile},
{'$set': {'stageStatusHPSS': 'staged'}})
# -- Extract the full file
else:
isExctractSucessful = False
# -- htar of full file
cmdLine = 'htar -xf {0}'.format(fileFullPath)
cmd = shlex.split(cmdLine)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, cwd=self._scratchSpace)
for lineTerminated in iter(p.stdout.readline, b''):
line = lineTerminated.decode("utf-8").rstrip('\t\n')
lineCleaned = ' '.join(line.split())
if lineCleaned == "HTAR: HTAR SUCCESSFUL":
isExctractSucessful = True
break
if isExctractSucessful:
for doc in self._collsHPSS[target].find({'fileFullPathTar': fileFullPath}):
upsertDoc = {'fileFullPath': doc['fileFullPath'],
'fileSize': doc['fileSize'],
'stageDummy': True}
self._collsStageToStageTarget[stageTarget].find_one_and_update({'fileFullPath': doc['fileFullPath']},
{'$set': {'stageStatusHPSS': 'staged'},
'$setOnInsert': upsertDoc}, upsert = True)
# -- Update stage status of HPSS stage collection
stageStatus = 'staged' if isExctractSucessful else 'failed'
self._collStageFromHPSS.find_one_and_update({'fileFullPath': fileFullPath},
{'$set' : {'stageStatus': stageStatus}})
# ____________________________________________________________________________
def _checkFileSizeOfStagedFile(self, doc):
"""Check fileSize of staged file."""
if not doc:
return False
try:
fstat = os.stat(os.path.join(self._scratchSpace, doc['fileFullPath']))
except:
return False
if doc['fileSize'] == fstat.st_size:
return True
else:
return False
# ____________________________________________________________________________
def stageToXRD(self):
"""Stage all files from stageing area to staging target."""
# -- Remove dummy files from full file stage
self.cleanDummyStagedFiles()
stageTarget = "XRD"
collXRD = self._collsStageToStageTarget[stageTarget]
# -- Loop over all documents in target collection
while True:
isStagingSucessful = True
now = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M')
# - Get next unstaged document and set status to staging
stageDoc = collXRD.find_one_and_update({'stageStatusHPSS': 'staged', 'stageStatusTarget': 'unstaged'},
{'$set':{'stageStatusTarget': 'staging', 'timeStamp':now}})
if not stageDoc:
break
# -- Get stage server
for serverTarget in stageDoc['stageTargetList']:
# -- XRD command to copy from HPSS staging area to XRD
xrdcpCmd = "xrdcp {0} {1}{2} xroot://{3}//star/{4}/{5}".format(self._stageXRD['xrdcpOptions'],
self._scratchSpace, stageDoc['fileFullPath'],
self._stageXRD['server'][serverTarget],
self._baseFolders[stageDoc['target']],
stageDoc['filePath'])
cmd = shlex.split(xrdcpCmd)
# -- Allow for several trials : 'tryMax'
trial = 0
while trial < self._stageXRD['tryMax']:
trial += 1
try:
output = check_output(cmd, stderr=STDOUT, timeout=self._stageXRD['timeOut'])
# -- Except error conditions
except subprocess.CalledProcessError as err:
isStagingSucessful = False
xrdCode = 'Unknown'
# -- Parse output for differnt XRD error conditions
for text in err.output.decode("utf-8").rstrip().split('\n'):
if "file already exists" in text:
xrdCode = "FileExistsAlready"
break
elif "no space left on device" in text:
xrdCode = "NoSpaceLeftOnDevice"
break
elif "No such file or directory" in text:
xrdCode = "NoSuchFileOrDirectory"
break
# -- File exits - not seeas as error
if xrdCode == 'FileExistsAlready':
isStagingSucessful = True
break
# -- Update entry
errorType = 'ErrorCode_{0}'.format(xrdCode)
note = err.output.decode("utf-8").rstrip()
collXRD.find_one_and_update({'fileFullPath': stageDoc['fileFullPath']},
{'$inc': {errorType: 1},
'$set': {'trials': trial},
'$push': {'note': note}})
continue
# -- Except timeout
except subprocess.TimeoutExpired as err:
isStagingSucessful = False
xrdCode = 'TimeOut'
errorType = 'ErrorCode_{0}'.format(xrdCode)
note = "Time out after {0}s".format(self._stageXRD['timeOut'])
collXRD.find_one_and_update({'fileFullPath': stageDoc['fileFullPath']},
{'$inc': {errorType: 1},
'$set': {'trials': trial},
'$push': {'note': note}})
continue
except:
isStagingSucessful = False
xrdCode = 'OtherError'
errorType = 'ErrorCode_{0}'.format(xrdCode)
note = "Other error"
collXRD.find_one_and_update({'fileFullPath': stageDoc['fileFullPath']},
{'$inc': {errorType: 1},
'$set': {'trials': trial},
'$push': {'note': note}})
continue
# -- XRD staging successful
if isStagingSucessful:
break
# -- Clean up on disk and update collection
if isStagingSucessful:
# -- Remove file from disk
fullFilePathOnScratch = self._scratchSpace + stageDoc['fileFullPath']
try:
os.remove(fullFilePathOnScratch)
except:
pass
# -- Set status to staged
collXRD.find_one_and_update({'fileFullPath': stageDoc['fileFullPath']},
{'$set': {'stageStatusTarget': 'staged'}})
# -- Tell servers have new files have been staged
self._setFileHasBeenStagedToXRD()
# -- Staging failed
else:
collXRD.find_one_and_update({'fileFullPath': stageDoc['fileFullPath']},
{'$set': {'stageStatusTarget': 'failed'}})
# ____________________________________________________________________________
def _setFileHasBeenStagedToXRD(self):
"""Add that file as been added to XRD."""
self._collServerXRD.update_many({'isDataServerXRD': True},
{'$set' : {'newFilesStaged': True}})
# ____________________________________________________________________________
def cleanDummyStagedFiles(self):
"""Remove all dummy staged files"""
for stageTarget in self._listOfStageTargets:
collTarget = self._collsStageToStageTarget[stageTarget]
fileListToDelete = [doc['fileFullPath'] for doc in collTarget.find({'stageStatusHPSS': 'staged', 'stageDummy': True})]
for fileName in fileListToDelete:
dummyFile = self._scratchSpace + fileName
try:
os.remove(dummyFile)
except:
pass
try:
collTarget.delete_one({'fileFullPath': fileName})
except:
pass
# ____________________________________________________________________________
def _rmEmptyFoldersOnScratch(self):
"""Remove empty folders on scratch"""
open('{}/.hiddenMarker'.format(self._scratchSpace),'a').close()
for root, dirs, files in os.walk(self._scratchSpace, topdown=False):
if len(files) == 0 and len(dirs) == 0:
os.removedirs(root)
# ____________________________________________________________________________
def printStagingStats(self):
"""Print staging statistics."""
stageTarget = "XRD"
target = 'picoDst'
collXRD = self._collsStageToStageTarget[stageTarget]
if collXRD.find().count() > 0:
print(" All Docs in {}: {}".format(collXRD.name, collXRD.find().count()))
print(" Unstaged : {}".format(collXRD.find({'stageStatusTarget': 'unstaged'}).count()))
print(" Unstaged : {} but staged already from HPSS".format(collXRD.find({'stageStatusTarget': 'unstaged', 'stageStatusHPSS': 'staged'}).count()))
print(" Staged : {}".format(collXRD.find({'stageStatusTarget': 'staged'}).count()))
print(" Dummy : {}".format(collXRD.find({'stageStatusHPSS': 'staged', 'stageDummy': True}).count()))
print(" Failed : {}".format(collXRD.find({'stageStatusTarget': 'failed'}).count()))
print(" Investigate: {}".format(collXRD.find({'stageStatusTarget': 'investigate'}).count()))
print(" Staging : {}".format(collXRD.find({'stageStatusTarget': 'staging'}).count()))
print(" ")
if self._collStageFromHPSS.find().count() > 0:
print(" All Docs in {}: {}".format(self._collStageFromHPSS.name, self._collStageFromHPSS.find().count()))
print(" Unstaged: {}".format(self._collStageFromHPSS.find({'stageStatus': 'unstaged'}).count()))
print(" Staged : {}".format(self._collStageFromHPSS.find({'stageStatus': 'staged'}).count()))
print(" Staging : {}".format(self._collStageFromHPSS.find({'stageStatus': 'staging'}).count()))
print(" Failed : {}".format(self._collStageFromHPSS.find({'stageStatus': 'failed'}).count()))
print(" ")
if self._collServerXRD.find({'isDataServerXRD': True, 'newFilesStaged': True}).count() > 0:
print(" Number of dataserver with new files staged: {} (no new XRD Crawler Run)".format(self._collServerXRD.find({'isDataServerXRD': True, 'newFilesStaged': True}).count()))
dataServerList = [d['nodeName'] for d in self._collServerXRD.find({'isDataServerXRD': True, 'newFilesStaged': True})]
print(" List :", dataServerList)
print(" ")
if self._collsStageTargetNew[target][stageTarget].find().count() > 0:
print(" Unprocessed new files on data server: {}".format(self._collsStageTargetNew[target][stageTarget].find().count()))
print(" ")
if self._collsStageTargetMiss[target][stageTarget].find().count() > 0:
print(" Unprocessed missing files on data server: {}".format(self._collsStageTargetMiss[target][stageTarget].find().count()))
print(" ")
print(" Free space on scratch disk: {} GB".format(self._getFreeSpaceOnScratchDisk()))
print(" Used space in staging area: {} GB".format(self._getUsedSpaceOnStagingArea()))
print(" ")
print(" ")
# ____________________________________________________________________________
def _checkUnstaged(self):
"""Check if no unstaged are left - return False if none left"""
# -- HPSS nothing left to stage
if self._collHPSS.find({'stageStatus': 'unstaged'}).count() > 0:
return True
# -- XRD nothing left to stage
if self._collXRD.find({'stageStatusTarget': 'unstaged'}).count() > 0:
return True
return False
# ____________________________________________________________________________
def _checkFailedXRD(self):
"""Check if some failed files are left"""
# -- Loop over all failed Target files
for doc in self._collXRD.find({'stageStatusTarget': 'failed'}):
# -- If files have been rested more the 10 times, set them to investigate
resetFailed = 0
try:
resetFailed = doc['resetFailed']
except:
pass
if resetFailed > 10:
self._collXRD.update_one({'_id': doc['_id']},
{'$set': {'stageStatusTarget': 'investigate'}})
# -- Get other errors than no space left on device
otherError = []
hasNoSpace = False
hasNoFile = False
for key, value in doc.items():
if "ErrorCode" in key:
if "NoSpaceLeftOnDevice" in key:
hasNoSpace = True
elif "NoSuchFileOrDirectory" in key:
hasNoFile = True
else:
otherError.append(key)
# -- If file is gone from staging area (why so ever) delete entry
if hasNoFile:
self._collXRD.delete_one({'_id': doc['_id']})
continue
# -- If all errors are no space left on device, reset to unstaged and increase resetFailed count
if hasNoSpace and len(otherError) == 0:
self._collXRD.update_one({'_id': doc['_id']},
{'$set': {'stageStatusTarget': 'unstaged'},
'$inc': {'resetFailed': 1}})
# -- Else , set to investigate
self._collXRD.update_one({'_id': doc['_id']}, {'$set': {'stageStatusTarget': 'investigate'}})
# ____________________________________________________________________________
def _checkFailedHPSS(self):
"""Check if some files have been staged already before staging failed"""
## -- Get all failed files
while True:
now = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M')
# - Get next unstaged document and set status to staging
stageDoc = self._collHPSS.find_one_and_update({'stageStatus': 'failed',
{'$set':{'stageStatus': 'checking', 'timeStamp': now}})
if not stageDoc:
break
for targetFile in stageDoc['listOfFiles']:
# -- Get file to be staged
xrdDoc = self._collXRD.find_one({'fileFullPath': targetFile})
# -- if file exists -
if os.path.isfile(os.path.join(self._scratchSpace, targetFile)):
# -- File is not to be staged ... remove it from disk
if not xrdDoc:
os.path.rm(os.path.join(self._scratchSpace, targetFile))
# -- Compare fileSize on disk and in collXRD
# Check if full file had been extracted
# -- if fileSize is OK - set to staged
if self._checkFileSizeOfStagedFile(targetFile, xrdDoc):
collXRD.find_one_and_update({'fileFullPath': targetFile},
{'$set': {'stageStatusHPSS': 'staged'})
# -- Otherwise remove file from disk and remove entry in collXRD
else:
os.path.rm(os.path.join(self._scratchSpace, targetFile))
collXRD.delete_one({'_id': xrdDoc['_id']})
# -- File is not on disk:
# Remove it from collXRD if present
# Otherwise do nothing
else
if xrdDoc:
self._collXRD.delete_one({'_id': xrdDoc['_id']})
# -- Remove failed entry in HPSS coll
self._collHPSS.delete_one({'_id': stageDoc['_id']})
# ____________________________________________________________________________
def _checkStaging(self):
"""Check if files are still staging
set them back to unstaged in case of too long in staging
"""
# -- Set all files from HPSS - still in staging state after 18 hours back to unstaged
nHoursAgo = (datetime.datetime.now() - datetime.timedelta(hours=18)).strftime('%Y-%m-%d-%H-%M')
self._collHPSS.update_many({'stageStatus': 'staging', 'timeStamp': {"$lt": nHoursAgo}},
{'$set': {'stageStatus': 'unstaged'},
'$inc': {'resetFailed': 1}})
# -- Set all files to XRD - still in staging state after 1 hours back to unstaged
nHoursAgo = (datetime.datetime.now() - datetime.timedelta(hours=1)).strftime('%Y-%m-%d-%H-%M')
self._collXRD.update_many({'stageStatusTarget': 'staging', 'timeStamp': {"$lt": nHoursAgo}},
{'$set': {'stageStatusTarget': 'unstaged'},
'$inc': {'resetFailed': 1}})
# ____________________________________________________________________________
def killZombieXRDCP(self):
"""Kill zombie xrdcp processes if not in staging cycle."""
if not self._dbUtil.checkProcessLock("staging_cycle_active"):
xrdcpCmd = "xrdcp"
for proc in psutil.process_iter():
if proc.name() == xrdcpCmd:
try:
proc.kill()
except:
pass
# ____________________________________________________________________________
def checkForEndOfStagingCycle(self):
"""Check for end of staging cycle."""
stageTarget = "XRD"
target = 'picoDst'
self._collHPSS = self._collStageFromHPSS
self._collXRD = self._collsStageToStageTarget[stageTarget]