-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmsActualMergingFiles.py
executable file
·741 lines (641 loc) · 37.6 KB
/
cmsActualMergingFiles.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, time, sys, getopt, fcntl, random
import shutil
import json
import glob
import multiprocessing
from multiprocessing.pool import ThreadPool
import thread
import datetime
import fileinput
import socket
import filecmp
import zlib
import zlibextras
import requests
from Logging import getLogger
log = getLogger()
max_size = 30 * 1024 * 1024 * 1024
host = os.uname()[1]
max_retries = 10
max_size_checksum = 2.7 * 1024 * 1024 * 1024
timeOut = 15 # seconds
def elasticMonitor(mergeMonitorData, runnumber, mergeType, esServerUrl, esIndexName, maxConnectionAttempts, debug):
# here the merge action is monitored by inserting a record into Elastic Search database
connectionAttempts=0 #initialize
# make dictionary to be JSON-ified and inserted into the Elastic Search DB as a document
keys = ["processed","accepted","errorEvents","fname","size","eolField1","eolField2","fm_date","ls","stream"]
values = [int(f) if str(f).isdigit() else str(f) for f in mergeMonitorData]
mergeMonitorDict=dict(zip(keys,values))
documentId = mergeMonitorData[-1]
mergeMonitorDict["host"]=host
mergeMonitorDict["runNumber"]=int(runnumber)
while True:
if connectionAttempts > 0:
log.info('connectionAttempt: {0}'.format(connectionAttempts))
try:
documentType=mergeType+'merge'
if(float(debug) >= 10):
log.info("About to try to insert into ES with the following info:")
log.info('Server: "' + esServerUrl+'/'+esIndexName+'/'+documentType+'/' + '"')
log.info("Data: '"+json.dumps(mergeMonitorDict)+"'")
#attempt to record the merge, 1s timeout!
monitorResponse=requests.post(esServerUrl+'/'+esIndexName+'/'+documentType+'/'+documentId,data=json.dumps(mergeMonitorDict),timeout=1)
if(float(debug) >= 10): log.info("Merger monitor produced response: {0}".format(monitorResponse.text))
if monitorResponse.status_code not in [200,201]:
log.error("elasticsearch replied with error code {0} and response: {1}".format(monitorResponse.status_code,monitorResponse.text))
connectionAttempts+=1
time.sleep(0.1)
if connectionAttempts > maxConnectionAttempts:
log.error('connection replied error: elasticMonitor failed to record '+documentType+' after '+ str(maxConnectionAttempts)+' attempts')
break
else:
continue
break
except (requests.exceptions.ConnectionError,requests.exceptions.Timeout) as e:
#log.error('elasticMonitor threw connection error: HTTP ' + monitorResponse.status_code)
#log.error(monitorResponse.raise_for_status())
log.exception(e)
if connectionAttempts > maxConnectionAttempts:
log.error('connection error: elasticMonitor failed to record '+documentType+' after '+ str(maxConnectionAttempts)+' attempts')
break
else:
connectionAttempts+=1
time.sleep(0.1)
continue
"""
merging option A: merging unmerged files to different files for different BUs
"""
def mergeFilesA(inpSubFolder, outSubFolder, outputMergedFolder, outputDQMMergedFolder, doCheckSum, outMergedFile, outMergedJSON, inputDataFolder, infoEoLS, eventsO, files, checkSumFix, fileSizeFix, filesJSON, errorCode, transferDest, timeReadJsns, timeIni, timeEndJsonOps, mergeType, doRemoveFiles, outputEndName, esServerUrl, esIndexName, debug):
try:
if(float(debug) >= 10): log.info("mergeFiles: {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}".format(outputMergedFolder, outMergedFile, outMergedJSON, inputDataFolder, infoEoLS, eventsO, files, checkSumFix, fileSizeFix, filesJSON, errorCode))
outMergedFileFullPath = os.path.join(outputMergedFolder, outSubFolder, "data", "open", outMergedFile)
outMergedJSONFullPath = os.path.join(outputMergedFolder, outSubFolder, "jsns", "open", outMergedJSON)
if(float(debug) >= 10): log.info('outMergedFileFullPath: {0}'.format(outMergedFileFullPath))
outMergedFileFullPathStable = ""
outMergedJSONFullPathStable = ""
paramTimeOut = [False, False] # 1: is/isn't time out? / 2: want to check it?
timeReadWrite = [0, 0]
initMergingTime = time.time()
if(float(debug) > 0): log.info("Start merge of {0}".format(outMergedJSONFullPath))
inputJsonFolder = os.path.dirname(filesJSON[0])
fileNameString = filesJSON[0].replace(inputJsonFolder,"").replace("/","").split('_')
if("DQM" in fileNameString[2] and mergeType == "macro"): paramTimeOut[1] = True; # check timeout only for DQM streams
specialStreams = False
if(fileNameString[2] == "streamDQMHistograms" or fileNameString[2] == "streamHLTRates" or fileNameString[2] == "streamL1Rates" or fileNameString[2] == "streamError"):
specialStreams = True
checksum_status = False
number_full_tries = 0
while(checksum_status == False and number_full_tries < 3):
checksum_status = True
number_full_tries = number_full_tries + 1
if os.path.exists(outMergedFileFullPath):
os.remove(outMergedFileFullPath)
if os.path.exists(outMergedJSONFullPath):
os.remove(outMergedJSONFullPath)
checkSumExpected = checkSumFix
fileSizeExpected = fileSizeFix
checkSumRead = [1]
if (mergeType == "macro" and specialStreams == False and infoEoLS[0] != 0):
iniName = fileNameString[0] + "_ls0000_" + fileNameString[2] + "_" + "StorageManager" + ".ini"
iniNameFullPath = os.path.join(outputMergedFolder, outSubFolder, "data", "open", iniName)
if os.path.exists(iniNameFullPath):
checkSumIni = 1
n_retries=0
while n_retries < max_retries:
if(n_retries > 0 and paramTimeOut[1] == True): paramTimeOut[0] = True
try:
checkSumIni=1
with open(iniNameFullPath, 'r') as fsrc:
length=16*1024
while 1:
buf = fsrc.read(length)
if not buf:
break
checkSumIni=zlib.adler32(buf,checkSumIni)
fsrc.close()
checkSumIni = checkSumIni & 0xffffffff
checkSumExpected = zlibextras.adler32_combine(checkSumIni,checkSumFix,fileSizeFix)
checkSumExpected = checkSumExpected & 0xffffffff
fileSizeExpected = os.path.getsize(iniNameFullPath) + fileSizeFix
filenames = [iniNameFullPath]
with open(outMergedFileFullPath, 'w') as fout:
append_files(filenames, fout, checkSumRead, debug, timeReadWrite, paramTimeOut)
fout.close()
break
except Exception, e:
log.warning("Error writing file {0}: {1}, waiting for 30secs".format(outMergedFileFullPath,e))
n_retries+=1
time.sleep(30)
if(n_retries == max_retries):
log.error("Could not write file {0}!".format(outMergedFileFullPath))
msg = "Could not write file {0}!: %s" % (outMergedFileFullPath)
raise RuntimeError, msg
else:
log.error("BIG PROBLEM, ini file not found!: {0}".format(iniNameFullPath))
msg = "BIG PROBLEM, ini file not found!: %s" % (iniNameFullPath)
raise RuntimeError, msg
filenames = [inputDataFolder + "/" + inpSubFolder + "/data/" + word_in_list for word_in_list in files]
if(float(debug) > 5): log.info("Will merge: {0}".format(filenames))
if(infoEoLS[0] != 0):
if (specialStreams == False and mergeType == "macro"):
with open(outMergedFileFullPath, 'a') as fout:
append_files(filenames, fout, checkSumRead, debug, timeReadWrite, paramTimeOut)
fout.close()
elif (specialStreams == False):
n_retries=0
while n_retries < max_retries:
if(n_retries > 0 and paramTimeOut[1] == True): paramTimeOut[0] = True
try:
with open(outMergedFileFullPath, 'w') as fout:
append_files(filenames, fout, checkSumRead, debug, timeReadWrite, paramTimeOut)
fout.close()
break
except Exception, e:
log.warning("Error writing file {0}: {1}, waiting for 30secs".format(outMergedFileFullPath,e))
n_retries+=1
time.sleep(30)
if(n_retries == max_retries):
log.error("Could not write file {0}!".format(outMergedFileFullPath))
msg = "Could not write file {0}!: %s" % (outMergedFileFullPath)
raise RuntimeError, msg
elif (fileNameString[2] == "streamHLTRates" or fileNameString[2] == "streamL1Rates"):
msg = "jsonMerger %s " % (outMergedFileFullPath)
goodFiles = 0
for nfile in range(0, len(filenames)):
if (os.path.exists(filenames[nfile]) and (not os.path.isdir(filenames[nfile])) and os.path.getsize(filenames[nfile]) > 0):
msg = msg + filenames[nfile] + " "
goodFiles = goodFiles + 1
if(float(debug) > 20): log.info("running {0}".format(msg))
if(goodFiles > 0):
os.system(msg)
else:
open(outMergedFileFullPath, 'w').close()
elif (fileNameString[2] == "streamError"):
if (mergeType == "mini"):
filenames = [inputDataFolder + "/" + word_in_list for word_in_list in files]
filenamesOut = [os.path.join(outputMergedFolder, outSubFolder, "data") + "/" + word_in_list for word_in_list in files]
outMergedFile = ""
for nfile in range(0, len(filenames)):
if (os.path.exists(filenames[nfile]) and (not os.path.isdir(filenames[nfile])) and os.path.getsize(filenames[nfile]) > 0):
outMergedFile = files[nfile] + "," + outMergedFile
shutil.move(filenames[nfile],filenamesOut[nfile])
else:
if (mergeType == "macro"):
msg = "fastHadd add -j 7 -o %s " % (outMergedFileFullPath)
else:
msg = "fastHadd add -o %s " % (outMergedFileFullPath)
goodFiles = 0
for nfile in range(0, len(filenames)):
if (os.path.exists(filenames[nfile]) and (not os.path.isdir(filenames[nfile])) and os.path.getsize(filenames[nfile]) > 0):
msg = msg + filenames[nfile] + " "
goodFiles = goodFiles + 1
if(float(debug) > 20): log.info("running {0}".format(msg))
if(goodFiles > 0):
os.system(msg)
try:
fileSizeExpected = os.path.getsize(outMergedFileFullPath)
except Exception, e:
log.warning("Error computing histogram filesize {0}: {1}".format(outMergedFileFullPath,e))
else:
open(outMergedFileFullPath, 'w').close()
# input events in that file, all input events, file name, output events in that files, number of merged files
# only the first three are important
theMergedJSONfile = open(outMergedJSONFullPath, 'w')
theMergedJSONfile.write(json.dumps({'data': (infoEoLS[0], eventsO, errorCode, outMergedFile, fileSizeExpected, checkSumExpected, infoEoLS[1], infoEoLS[2], infoEoLS[3], transferDest)}))
theMergedJSONfile.close()
#os.chmod(outMergedJSONFullPath, 0666)
# Last thing to do is to move the data and json files to its final location "merged/runXXXXXX/stream/open/../."
if (fileNameString[2] != "streamError"):
outMergedFileFullPathStable = os.path.join(outputMergedFolder, outSubFolder, "data", outMergedFile)
outMergedJSONFullPathStable = os.path.join(outputMergedFolder, outSubFolder, "jsns", outMergedJSON)
if (mergeType == "macro" and ("DQM" in fileNameString[2])):
outMergedFileFullPathStable = os.path.join(outputDQMMergedFolder, outSubFolder, "data", "open", outMergedFile)
outMergedJSONFullPathStable = os.path.join(outputDQMMergedFolder, outSubFolder, "jsns", "open", outMergedJSON)
# checkSum checking, doCheckSum is ignored for DQM streams
if(paramTimeOut[0] == False and (doCheckSum == "True" or "DQM" in fileNameString[2]) and fileNameString[2] != "streamError" and specialStreams == False and infoEoLS[0] != 0 and fileSizeExpected < max_size_checksum):
adler32c=1
with open(outMergedFileFullPath, 'r') as fsrc:
length=16*1024
while 1:
buf = fsrc.read(length)
if not buf:
break
adler32c=zlib.adler32(buf,adler32c)
fsrc.close()
adler32c = adler32c & 0xffffffff
if(adler32c != checkSumExpected):
log.error("BIG PROBLEM, checkSum failed != outMergedFileFullPath: {0} -(obs/exp)-> {1}/{2}".format(outMergedFileFullPath,adler32c,checkSumExpected))
outMergedFileFullPathStable = os.path.join(outputMergedFolder, outSubFolder, "bad", outMergedFile)
outMergedJSONFullPathStable = os.path.join(outputMergedFolder, outSubFolder, "bad", outMergedJSON)
checksum_status = False
if(paramTimeOut[0] == False and checksum_status == True and infoEoLS[0] != 0 and fileNameString[2] != "streamError"):
checkSumRead[0] = checkSumRead[0] & 0xffffffff
if(checkSumRead[0] != checkSumExpected):
log.error("checkSumExpRead {0} (exp/read)-> {1}/{2}".format(outMergedFileFullPath,checkSumExpected,checkSumRead[0]))
outMergedFileFullPathStable = os.path.join(outputMergedFolder, outSubFolder, "bad", outMergedFile)
outMergedJSONFullPathStable = os.path.join(outputMergedFolder, outSubFolder, "bad", outMergedJSON)
checksum_status = False
if(paramTimeOut[0] == False and checksum_status == True and infoEoLS[0] != 0 and fileNameString[2] != "streamError"):
if(fileNameString[2] != "streamError" and specialStreams == False and fileSizeExpected != os.path.getsize(outMergedFileFullPath)):
log.error("BIG PROBLEM, fileSizeExpected != outMergedFileFullPath: {0} --> {1}/{2}".format(outMergedFileFullPath,fileSizeExpected,os.path.getsize(outMergedFileFullPath)))
outMergedFileFullPathStable = os.path.join(outputMergedFolder, outSubFolder, "bad", outMergedFile)
outMergedJSONFullPathStable = os.path.join(outputMergedFolder, outSubFolder, "bad", outMergedJSON)
checksum_status = False
elif(mergeType == "macro" and fileSizeExpected > max_size):
log.error("BIG PROBLEM, fileSizeExpected is too large!: {0} --> {1}".format(outMergedFileFullPath,fileSizeExpected))
outMergedFileFullPathStable = os.path.join(outputMergedFolder, outSubFolder, "recovery", outMergedFile)
outMergedJSONFullPathStable = os.path.join(outputMergedFolder, outSubFolder, "recovery", outMergedJSON)
checksum_status = False
if(infoEoLS[0] != 0 and fileNameString[2] != "streamError"):
shutil.move(outMergedFileFullPath,outMergedFileFullPathStable)
shutil.move(outMergedJSONFullPath,outMergedJSONFullPathStable)
if (mergeType == "macro" and ("DQM" in fileNameString[2]) and checksum_status == True):
outMergedFileFullPathStableFinal = os.path.join(outputDQMMergedFolder, outSubFolder, "data", outMergedFile)
outMergedJSONFullPathStableFinal = os.path.join(outputDQMMergedFolder, outSubFolder, "jsns", outMergedJSON)
if(infoEoLS[0] != 0):
shutil.move(outMergedFileFullPathStable,outMergedFileFullPathStableFinal)
shutil.move(outMergedJSONFullPathStable,outMergedJSONFullPathStableFinal)
# remove already merged files, if wished
if(doRemoveFiles == "True" and checksum_status == True):
for nfile in range(0, len(files)):
if(float(debug) >= 10): log.info("removing file: {0}".format(files[nfile]))
inputFileToRemove = os.path.join(inputDataFolder, inpSubFolder, "data", files[nfile])
if (os.path.exists(inputFileToRemove) and (not os.path.isdir(inputFileToRemove))):
os.remove(inputFileToRemove)
for nfile in range(0, len(filesJSON)):
if(float(debug) >= 10): log.info("removing filesJSON: {0}".format(filesJSON[nfile]))
inputFileToRemove = filesJSON[nfile]
if (os.path.exists(inputFileToRemove) and (not os.path.isdir(inputFileToRemove))):
os.remove(inputFileToRemove)
# This is now done in the callback
#if mergeType == "mini":
# try:
# # Removing BoLS file, the last step
# BoLSFileName = fileNameString[0] + "_" + fileNameString[1] + "_" + fileNameString[2] + "_BoLS.jsn"
# BoLSFileNameFullPath = os.path.join(inputJsonFolder, BoLSFileName)
# if os.path.exists(BoLSFileNameFullPath):
# os.remove(BoLSFileNameFullPath)
# except Exception, e:
# log.warning("Error deleting BoLS file {0}: {1}".format(BoLSFileNameFullPath,e))
# monitor the merger by inserting record into elastic search database:
if((not (esServerUrl=='' or esIndexName=='')) and checksum_status == True):
ls=fileNameString[1][2:]
stream=fileNameString[2][6:]
runnumber=fileNameString[0][3:]
id=outMergedJSON.replace(".jsn","")
mergeMonitorData = [ infoEoLS[0], eventsO, errorCode, outMergedFile, fileSizeExpected, infoEoLS[1], infoEoLS[2], int(time.time()*1000), ls, stream, id]
elasticMonitor(mergeMonitorData, runnumber, mergeType, esServerUrl, esIndexName, 5, debug)
endMergingTime = time.time()
if(float(debug) > 0): log.info("Time for ({0}): reading {1} jsns = {2:.3f} ; starting jsn operation: {3} ; total jsn operation: {4:.3f} ; spawning waiting time: {5:.3f} ; actual merging total: {6:.3f} ; reading data: {7:.3f} ; writing data: {8:.3f}".format(outMergedJSON,len(filesJSON),timeReadJsns,time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timeIni)),timeEndJsonOps-timeIni,initMergingTime-timeEndJsonOps,endMergingTime-initMergingTime,timeReadWrite[0],timeReadWrite[1]))
except Exception,e:
log.error("mergeFilesA failed {0} - {1}".format(outMergedJSON,e))
"""
merging option C: merging unmerged files to same file for different BUs without locking the merged file
"""
def mergeFilesC(inpSubFolder, outSubFolder, outputMergedFolder, outputSMMergedFolder, doCheckSum, outMergedFile, outMergedJSON, inputDataFolder, infoEoLS, eventsO, files, checkSum, fileSize, filesJSON, errorCode, transferDest, timeReadJsns, timeIni, timeEndJsonOps, mergeType, doRemoveFiles, outputEndName, esServerUrl, esIndexName, debug):
try:
if(float(debug) >= 10): log.info("mergeFiles: {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}".format(outputMergedFolder, outputSMMergedFolder, outMergedFile, outMergedJSON, inputDataFolder, infoEoLS, eventsO, files, checkSum, fileSize, filesJSON, errorCode))
# we will merge file at the BU level only!
outMergedFileFullPath = os.path.join(outputSMMergedFolder, outSubFolder, "data", "open", outMergedFile)
outMergedJSONFullPath = os.path.join(outputMergedFolder, outSubFolder, "jsns", "open", outMergedJSON)
if(float(debug) >= 10): log.info('outMergedFileFullPath: {0}'.format(outMergedFileFullPath))
checkSumRead = [1]
paramTimeOut = [False, False]
timeReadWrite = [0, 0]
initMergingTime = time.time()
if(float(debug) > 0): log.info("Start merge of {0}".format(outMergedJSONFullPath))
if os.path.exists(outMergedJSONFullPath):
os.remove(outMergedJSONFullPath)
inputJsonFolder = os.path.dirname(filesJSON[0])
fileNameString = filesJSON[0].replace(inputJsonFolder,"").replace("/","").split('_')
lockName = fileNameString[0] + "_" + fileNameString[1] + "_" + fileNameString[2] + "_" + "StorageManager" + ".lock"
lockNameFullPath = os.path.join(outputSMMergedFolder, outSubFolder, "data", "open", lockName)
# boolean for the ES monitoring information
isFileOK = True
iniName = fileNameString[0] + "_ls0000_" + fileNameString[2] + "_" + outputEndName + ".ini"
if mergeType == "macro":
iniName = "open/" + fileNameString[0] + "_ls0000_" + fileNameString[2] + "_" + "StorageManager" + ".ini"
iniNameFullPath = os.path.join(outputSMMergedFolder, outSubFolder, "data", iniName)
if mergeType == "mini":
maxSizeMergedFile = 50 * 1024 * 1024 * 1024
if os.path.exists(iniNameFullPath):
outLockedFileSize = 0
if(os.path.exists(lockNameFullPath)):
outLockedFileSize = os.path.getsize(lockNameFullPath)
if(float(debug) > 5): log.info("Making lock file if needed({0}-{1}-{2}) {3}".format(os.path.exists(outMergedFileFullPath), outLockedFileSize, eventsO, outMergedJSONFullPath))
if (not os.path.exists(outMergedFileFullPath)):
time.sleep(random.random()*0.3)
if (not os.path.exists(outMergedFileFullPath)):
# file is generated, but do not fill it
try:
fd = os.open(outMergedFileFullPath, os.O_WRONLY | os.O_CREAT | os.O_EXCL)
fout = os.fdopen(fd, "w")
fcntl.flock(fout, fcntl.LOCK_EX)
fout.truncate(maxSizeMergedFile)
fout.seek(0)
#filenameIni = [iniNameFullPath]
#append_files(filenameIni, fout, checkSumRead, debug, timeReadWrite, paramTimeOut)
if(float(debug) > 5): log.info("outMergedFile {0} being generated".format(outMergedFileFullPath))
fcntl.flock(fout, fcntl.LOCK_UN)
fout.close()
except Exception,e:
log.warning("cmsActualMergingFilesC dat file exists {0} - {1}".format(outMergedFileFullPath,e))
checkSumIni=1
n_retries=0
while n_retries < max_retries:
try:
checkSumIni=1
with open(iniNameFullPath, 'r') as fsrc:
length=16*1024
while 1:
buf = fsrc.read(length)
if not buf:
break
checkSumIni=zlib.adler32(buf,checkSumIni)
fsrc.close()
checkSumIni = checkSumIni & 0xffffffff
break
except Exception, e:
log.warning("Error writing file {0}: {1}, waiting for 30secs".format(outMergedFileFullPath,e))
n_retries+=1
time.sleep(30)
if(n_retries == max_retries):
log.error("Could not write file {0}!".format(outMergedFileFullPath))
msg = "Could not write file {0}!: %s" % (outMergedFileFullPath)
raise RuntimeError, msg
if (not os.path.exists(lockNameFullPath)):
try:
fd = os.open(lockNameFullPath, os.O_WRONLY | os.O_CREAT | os.O_EXCL)
filelock = os.fdopen(fd, "w")
fcntl.flock(filelock, fcntl.LOCK_EX)
if(float(debug) > 5): log.info("lockFile {0} being generated".format(lockNameFullPath))
filelock.write("%s=%d:%d" %(socket.gethostname(),os.path.getsize(iniNameFullPath),checkSumIni))
filelock.flush()
#os.fdatasync(filelock)
#os.chmod(lockNameFullPath, 0666)
fcntl.flock(filelock, fcntl.LOCK_UN)
filelock.close()
except Exception,e:
log.warning("cmsActualMergingFilesC lock file exists {0} - {1}".format(lockNameFullPath,e))
else:
log.error("BIG PROBLEM, ini file not found!: {0}".format(iniNameFullPath))
msg = "BIG PROBLEM, ini file not found!: %s" % (iniNameFullPath)
raise RuntimeError, msg
filenames = [inputDataFolder + "/" + inpSubFolder + "/data/" + word_in_list for word_in_list in files]
if(float(debug) > 20): log.info("Will merge: {0}".format(filenames))
sum = 0
for nFile in range(0,len(filenames)):
if os.path.exists(filenames[nFile]) and os.path.isfile(filenames[nFile]):
sum = sum + os.path.getsize(filenames[nFile])
# no point to add information and merge files is there are no output events
if(eventsO != 0):
if(float(debug) > 5): log.info("Waiting to lock file {0}".format(outMergedJSONFullPath))
nCount = 0
lockFileExist = os.path.exists(lockNameFullPath)
while ((lockFileExist == False) or (lockFileExist == True and os.path.getsize(lockNameFullPath) == 0)):
nCount = nCount + 1
if(nCount%60 == 1): log.info("Waiting for the file to unlock: {0}".format(lockNameFullPath))
time.sleep(1)
lockFileExist = os.path.exists(lockNameFullPath)
if(nCount == 180):
log.info("Not possible to unlock file after 3 minutes!!!: {0}".format(lockNameFullPath))
return
if(float(debug) > 5): log.info("Locking file {0}".format(outMergedJSONFullPath))
with open(lockNameFullPath, 'r+w') as filelock:
fcntl.flock(filelock, fcntl.LOCK_EX)
lockFullString = filelock.readline().split(',')
ini = 0
try:
ini = int(lockFullString[len(lockFullString)-1].split(':')[0].split('=')[1])
except Exception,e:
log.warning("lockFullString1 problem({0}): {1} - {2}".format(lockNameFullPath,lockFullString,e))
time.sleep(1)
filelock.seek(0)
lockFullString = filelock.readline().split(',')
try:
ini = int(lockFullString[len(lockFullString)-1].split(':')[0].split('=')[1])
except Exception,e:
log.error("lockFullString2 problem({0}): {1} - {2}".format(lockNameFullPath,lockFullString,e))
filelock.write(",%s=%d:%d" %(socket.gethostname(),ini+sum,checkSum))
filelock.flush()
if(float(debug) > 5): log.info("Writing in lock file ({0}): {1}".format(lockNameFullPath,(ini+sum)))
#os.fdatasync(filelock)
fcntl.flock(filelock, fcntl.LOCK_UN)
filelock.close()
if(float(debug) > 5): log.info("Unlocking file {0} - {1}/{2}".format(outMergedJSONFullPath,ini,sum))
nDataCount = 0
dataFileExist = os.path.exists(outMergedFileFullPath)
while ((dataFileExist == False) or (dataFileExist == True and os.path.getsize(outMergedFileFullPath) == 0)):
nDataCount = nDataCount + 1
if(nDataCount%60 == 1): log.info("Waiting for the dat file to unlock: {0}".format(outMergedFileFullPath))
time.sleep(1)
dataFileExist = os.path.exists(outMergedFileFullPath)
if(nDataCount == 180):
log.info("Not possible to unlock dat file after 3 minutes!!!: {0}".format(outMergedFileFullPath))
return
if(float(debug) > 5): log.info("Time after unlocking, before appending jsn({0}): {1:.3f}".format(outMergedJSONFullPath, time.time()-initMergingTime))
n_retries=0
while n_retries < max_retries:
try:
with open(outMergedFileFullPath, 'r+w') as fout:
fout.seek(ini)
append_files(filenames, fout, checkSumRead, debug, timeReadWrite, paramTimeOut)
fout.close()
break
except Exception, e:
log.warning("Error writing file {0}: {1}, waiting for 30secs".format(outMergedFileFullPath,e))
n_retries+=1
time.sleep(30)
if(n_retries == max_retries):
log.error("Could not write file {0}!".format(outMergedFileFullPath))
msg = "Could not write file {0}!: %s" % (outMergedFileFullPath)
raise RuntimeError, msg
if(float(debug) > 2): log.info("Actual merging of {0} happened".format(outMergedJSONFullPath))
if(mergeType == "macro" and os.path.exists(iniNameFullPath)):
if(infoEoLS[0] != 0):
n_retries=0
while n_retries < max_retries:
try:
with open(outMergedFileFullPath, 'r+w') as fout:
fout.seek(0)
filenameIni = [iniNameFullPath]
append_files(filenameIni, fout, checkSumRead, debug, timeReadWrite, paramTimeOut)
fout.close()
break
except Exception, e:
log.warning("Error writing file {0}: {1}, waiting for 30secs".format(outMergedFileFullPath,e))
n_retries+=1
time.sleep(30)
if(n_retries == max_retries):
log.error("Could not write file {0}!".format(outMergedFileFullPath))
msg = "Could not write file {0}!: %s" % (outMergedFileFullPath)
raise RuntimeError, msg
fileSize = fileSize + os.path.getsize(iniNameFullPath)
if eventsO == 0:
fileSize = os.path.getsize(iniNameFullPath)
elif(mergeType == "macro"):
log.error("BIG PROBLEM, iniNameFullPath {0} does not exist".format(iniNameFullPath))
totalSize = 0
# Last thing to do is to move the data and json files to its final location "merged/runXXXXXX/stream/open/../."
if mergeType == "macro":
if not os.path.exists(lockNameFullPath):
msg = "lock file %s does not exist!\n" % (lockNameFullPath)
raise RuntimeError,msg
with open(lockNameFullPath, 'r+w') as filelock:
lockFullString = filelock.readline().split(',')
totalSize = int(lockFullString[len(lockFullString)-1].split(':')[0].split('=')[1])
filelock.close()
with open(outMergedFileFullPath, 'r+w') as fout:
fout.truncate(fileSize)
fout.close()
checkSumFailed = False
if(fileNameString[2] != "streamError" and fileSize != totalSize):
checkSumFailed = True
log.error("BIG PROBLEM, fileSize != outMergedFileFullPath: {0} --> {1}/{2}".format(outMergedFileFullPath,fileSize,totalSize))
outMergedFileFullPathStable = os.path.join(outputSMMergedFolder, outSubFolder, "bad", outMergedFile)
# also want to move the lock file to the bad area
lockNameFullPathStable = os.path.join(outputSMMergedFolder, outSubFolder, "bad", lockName)
shutil.move(lockNameFullPath,lockNameFullPathStable)
isFileOK = False
else:
outMergedFileFullPathStable = os.path.join(outputSMMergedFolder, outSubFolder, "data", outMergedFile)
if(fileNameString[2] != "streamError"):
# expected checksum
with open(lockNameFullPath, 'r+w') as filelock:
lockFullString = filelock.readline().split(',')
filelock.close()
checkSum = int(lockFullString[0].split(':')[1])
for nf in range(1, len(lockFullString)):
fileSizeAux = int(lockFullString[nf].split(':')[0].split('=')[1])-int(lockFullString[nf-1].split(':')[0].split('=')[1])
checkSumAux = int(lockFullString[nf].split(':')[1])
checkSum = zlibextras.adler32_combine(checkSum,checkSumAux,fileSizeAux)
checkSum = checkSum & 0xffffffff
if(doCheckSum == "True" and fileNameString[2] != "streamError"):
# observed checksum
adler32c=1
with open(outMergedFileFullPath, 'r') as fsrc:
length=16*1024
while 1:
buf = fsrc.read(length)
if not buf:
break
adler32c=zlib.adler32(buf,adler32c)
fsrc.close()
adler32c = adler32c & 0xffffffff
if(adler32c != checkSum):
checkSumFailed = True
log.error("BIG PROBLEM, checkSum failed != outMergedFileFullPath: {0} --> {1}/{2}".format(outMergedFileFullPath,adler32c,checkSum))
outMergedFileFullPathStable = os.path.join(outputSMMergedFolder, outSubFolder, "bad", outMergedFile)
# also want to move the lock file to the bad area
lockNameFullPathStable = os.path.join(outputSMMergedFolder, outSubFolder, "bad", lockName)
shutil.move(lockNameFullPath,lockNameFullPathStable)
isFileOK = False
if(doRemoveFiles == "True" and checkSumFailed == False):
if (os.path.exists(lockNameFullPath) and (not os.path.isdir(lockNameFullPath))):
os.remove(lockNameFullPath)
if(checkSumFailed == False and fileSize > max_size):
log.error("BIG PROBLEM, fileSize is too large!: {0} --> {1}".format(outMergedFileFullPath,fileSize))
outMergedFileFullPathStable = os.path.join(outputSMMergedFolder, outSubFolder, "recovery", outMergedFile)
isFileOK = False
if(float(debug) >= 10): log.info("outMergedFileFullPath/outMergedFileFullPathStable: {0}, {1}".format(outMergedFileFullPath, outMergedFileFullPathStable))
try:
shutil.move(outMergedFileFullPath,outMergedFileFullPathStable)
except Exception,e:
log.error("cmsActualMergingFilesC crashed, trying again: {0}, {1} - {2}".format(outMergedFileFullPath,outMergedFileFullPathStable,e))
try:
shutil.move(outMergedFileFullPath,outMergedFileFullPathStable)
except Exception,e:
log.error("cmsActualMergingFilesC crashed again: {0}, {1} - {2}".format(outMergedFileFullPath,outMergedFileFullPathStable,e))
return
# input events in that file, all input events, file name, output events in that files, number of merged files
# only the first three are important
theMergedJSONfile = open(outMergedJSONFullPath, 'w')
theMergedJSONfile.write(json.dumps({'data': (infoEoLS[0], eventsO, errorCode, outMergedFile, fileSize, checkSum, infoEoLS[1], infoEoLS[2], infoEoLS[3], transferDest)}))
theMergedJSONfile.close()
#os.chmod(outMergedJSONFullPath, 0666)
if(mergeType == "macro" and fileNameString[2] != "streamError"):
if(fileSize != totalSize or checkSumFailed == True):
outMergedJSONFullPathStable = os.path.join(outputSMMergedFolder, outSubFolder, "bad", outMergedJSON)
elif(fileSize > max_size):
outMergedJSONFullPathStable = os.path.join(outputSMMergedFolder, outSubFolder, "recovery", outMergedJSON)
else:
outMergedJSONFullPathStable = os.path.join(outputSMMergedFolder, outSubFolder, "jsns", outMergedJSON)
else:
outMergedJSONFullPathStable = os.path.join(outputMergedFolder, outSubFolder, "jsns", outMergedJSON)
if(float(debug) > 5): log.info("Time before move of jsn({0}): {1:.3f}".format(outMergedJSONFullPath,time.time()-initMergingTime))
shutil.move(outMergedJSONFullPath,outMergedJSONFullPathStable)
if(float(debug) > 5): log.info("Time after move of jsn({0}): {1:.3f}".format(outMergedJSONFullPath,time.time()-initMergingTime))
# remove already merged files, if wished
if(doRemoveFiles == "True"):
if mergeType == "mini":
for nfile in range(0, len(files)):
if(float(debug) >= 10): log.info("removing file: {0}".format(files[nfile]))
inputFileToRemove = os.path.join(inputDataFolder, inpSubFolder, "data", files[nfile])
if (os.path.exists(inputFileToRemove) and (not os.path.isdir(inputFileToRemove))):
os.remove(inputFileToRemove)
for nfile in range(0, len(filesJSON)):
if(float(debug) >= 10): log.info("removing filesJSON: {0}".format(filesJSON[nfile]))
inputFileToRemove = filesJSON[nfile]
if (os.path.exists(inputFileToRemove) and (not os.path.isdir(inputFileToRemove))):
os.remove(inputFileToRemove)
# This is now done in the callback
#if mergeType == "mini":
# try:
# # Removing BoLS file, the last step
# BoLSFileName = fileNameString[0] + "_" + fileNameString[1] + "_" + fileNameString[2] + "_BoLS.jsn"
# BoLSFileNameFullPath = os.path.join(inputJsonFolder, BoLSFileName)
# if os.path.exists(BoLSFileNameFullPath):
# os.remove(BoLSFileNameFullPath)
# except Exception, e:
# log.warning("Error deleting BoLS file {0}: {1}".format(BoLSFileNameFullPath,e))
# monitor the merger by inserting record into elastic search database:
if((not (esServerUrl=='' or esIndexName=='')) and isFileOK == True):
ls=fileNameString[1][2:]
stream=fileNameString[2][6:]
runnumber=fileNameString[0][3:]
id=outMergedJSON.replace(".jsn","")
mergeMonitorData = [ infoEoLS[0], eventsO, errorCode, outMergedFile, fileSize, infoEoLS[1], infoEoLS[2], int(time.time()*1000), ls, stream, id]
elasticMonitor(mergeMonitorData, runnumber, mergeType, esServerUrl, esIndexName, 5, debug)
endMergingTime = time.time()
if(float(debug) > 0): log.info("Time for ({0}): reading {1} jsns = {2:.3f} ; starting jsn operation: {3} ; total jsn operation: {4:.3f} ; spawning waiting time: {5:.3f} ; actual merging total: {6:.3f} ; reading data: {7:.3f} ; writing data: {8:.3f}".format(outMergedJSON,len(filesJSON),timeReadJsns,time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timeIni)),timeEndJsonOps-timeIni,initMergingTime-timeEndJsonOps,endMergingTime-initMergingTime,timeReadWrite[0],timeReadWrite[1]))
except Exception,e:
log.error("mergeFilesC failed {0} - {1}".format(outMergedJSON,e))
#______________________________________________________________________________
def append_files(ifnames, ofile, theCheckSumBeingRead, debug, theTimeReadWrite, theParamTimeOut):
'''
Appends the contents of files given by a list of input file names `ifname'
to the given output file object `ofile'. Returns None.
'''
iniTimeMerge = time.time()
for ifname in ifnames:
if (os.path.exists(ifname) and (not os.path.isdir(ifname)) and
(theParamTimeOut[1] == False or ifname.endswith(".ini") or time.time()-iniTimeMerge < timeOut)):
with open(ifname) as ifile:
#shutil.copyfileobj(ifile, ofile)
copyfileobj(ifile, ofile, theCheckSumBeingRead, debug, theTimeReadWrite)
ifile.close()
elif (os.path.exists(ifname) and (not os.path.isdir(ifname))):
theParamTimeOut[0] = True
break
#elif ((not os.path.isdir(ifname))):
# msg = "filename {0} does not exist".format(ifname)
# raise RuntimeError, msg
# append_files
#______________________________________________________________________________
def copyfileobj(fsrc, fdst, theCheckSumRead, theDebug, theTimeReadWrite, length=16*1024):
"""copy data from file-like object fsrc to file-like object fdst"""
iniTimeRead = 0
iniTimeWrite = 0
while 1:
if(theDebug > 0): iniTimeRead = time.time()
buf = fsrc.read(length)
if(theDebug > 0): theTimeReadWrite[0] = theTimeReadWrite[0] + time.time()-iniTimeRead
if not buf:
break
theCheckSumRead[0]=zlib.adler32(buf,theCheckSumRead[0])
if(theDebug > 0): iniTimeWrite = time.time()
fdst.write(buf)
if(theDebug > 0): theTimeReadWrite[1] = theTimeReadWrite[1] + time.time()-iniTimeWrite
# copyfileobj