20
20
import DIRAC
21
21
from DIRAC import S_OK , S_ERROR , gLogger , gConfig
22
22
from DIRAC .Core .Utilities import DErrno
23
+ from DIRAC .Core .Utilities import Time
23
24
from DIRAC .Core .Utilities .Adler import fileAdler , compareAdler
24
25
from DIRAC .Core .Utilities .File import makeGuid , getSize
25
26
from DIRAC .Core .Utilities .List import randomize , breakListIntoChunks
26
27
from DIRAC .Core .Utilities .ReturnValues import returnSingleResult
27
28
from DIRAC .Core .Security .ProxyInfo import getProxyInfo
28
29
from DIRAC .ConfigurationSystem .Client .Helpers .Operations import Operations
29
- from DIRAC .AccountingSystem .Client .DataStoreClient import gDataStoreClient
30
- from DIRAC .AccountingSystem .Client .Types .DataOperation import DataOperation
30
+ from DIRAC .MonitoringSystem .Client .DataOperationSender import DataOperationSender
31
31
from DIRAC .DataManagementSystem .Utilities .DMSHelpers import DMSHelpers
32
32
from DIRAC .Resources .Catalog .FileCatalog import FileCatalog
33
33
from DIRAC .Resources .Storage .StorageElement import StorageElement
@@ -46,8 +46,8 @@ def _isOlderThan(stringTime, days):
46
46
return False
47
47
48
48
49
- def _initialiseAccountingObject (operation , se , files ):
50
- """create accouting record"""
49
+ def _initialiseAccountingDict (operation , se , files ):
50
+ """create Accounting/Monitoring record"""
51
51
accountingDict = {}
52
52
accountingDict ["OperationType" ] = operation
53
53
result = getProxyInfo ()
@@ -67,9 +67,7 @@ def _initialiseAccountingObject(operation, se, files):
67
67
accountingDict ["TransferTime" ] = 0.0
68
68
accountingDict ["FinalStatus" ] = "Successful"
69
69
accountingDict ["Source" ] = DIRAC .siteName ()
70
- oDataOperation = DataOperation ()
71
- oDataOperation .setValuesFromDict (accountingDict )
72
- return oDataOperation
70
+ return accountingDict
73
71
74
72
75
73
class DataManager (object ):
@@ -104,6 +102,7 @@ def __init__(self, catalogs=None, masterCatalogOnly=False, vo=False):
104
102
self .dmsHelper = DMSHelpers (vo = vo )
105
103
self .registrationProtocol = self .dmsHelper .getRegistrationProtocols ()
106
104
self .thirdPartyProtocols = self .dmsHelper .getThirdPartyProtocols ()
105
+ self .dataOpSender = DataOperationSender ()
107
106
108
107
def setAccountingClient (self , client ):
109
108
"""Set Accounting Client instance"""
@@ -540,24 +539,32 @@ def putAndRegister(self, lfn, fileName, diracSE, guid=None, path=None, checksum=
540
539
failed = {}
541
540
##########################################################
542
541
# Perform the put here.
543
- oDataOperation = _initialiseAccountingObject ("putAndRegister" , diracSE , 1 )
544
- oDataOperation .setStartTime ()
545
- oDataOperation .setValueByKey ("TransferSize" , size )
546
- startTime = time .time ()
542
+ startTime = Time .dateTime ()
543
+ transferStartTime = time .time ()
547
544
res = returnSingleResult (storageElement .putFile (fileDict ))
548
- putTime = time .time () - startTime
549
- oDataOperation .setValueByKey ("TransferTime" , putTime )
550
- if not res ["OK" ]:
545
+ putTime = time .time () - transferStartTime
551
546
547
+ accountingDict = _initialiseAccountingDict ("putAndRegister" , diracSE , 1 )
548
+ accountingDict ["TransferSize" ] = size
549
+ accountingDict ["TransferTime" ] = putTime
550
+
551
+ if not res ["OK" ]:
552
552
# We don't consider it a failure if the SE is not valid
553
553
if not DErrno .cmpError (res , errno .EACCES ):
554
- oDataOperation .setValueByKey ("TransferOK" , 0 )
555
- oDataOperation .setValueByKey ("FinalStatus" , "Failed" )
556
- oDataOperation .setEndTime ()
557
- gDataStoreClient .addRegister (oDataOperation )
558
- gDataStoreClient .commit ()
559
- startTime = time .time ()
560
- log .debug ("putAndRegister: Sending accounting took %.1f seconds" % (time .time () - startTime ))
554
+
555
+ accountingDict ["TransferOK" ] = 0
556
+ accountingDict ["FinalStatus" ] = "Failed"
557
+ sendingResult = self .dataOpSender .sendData (
558
+ accountingDict , commitFlag = True , startTime = startTime , endTime = Time .dateTime ()
559
+ )
560
+
561
+ log .verbose ("Committing data operation" )
562
+ if not sendingResult ["OK" ]:
563
+ log .error ("Couldn't commit data operation" , sendingResult ["Message" ])
564
+ return sendingResult
565
+ log .verbose ("Done committing" )
566
+ log .debug ("putAndRegister: Sending took %.1f seconds" % (time .time () - transferStartTime ))
567
+
561
568
errStr = "Failed to put file to Storage Element."
562
569
log .debug (errStr , "%s: %s" % (fileName , res ["Message" ]))
563
570
return S_ERROR ("%s %s" % (errStr , res ["Message" ]))
@@ -572,7 +579,6 @@ def putAndRegister(self, lfn, fileName, diracSE, guid=None, path=None, checksum=
572
579
log .debug (errStr , res ["Message" ])
573
580
return S_ERROR ("%s %s" % (errStr , res ["Message" ]))
574
581
destUrl = res ["Value" ]
575
- oDataOperation .setValueByKey ("RegistrationTotal" , 1 )
576
582
577
583
fileTuple = (lfn , destUrl , size , destinationSE , guid , checksum )
578
584
registerDict = {
@@ -586,25 +592,35 @@ def putAndRegister(self, lfn, fileName, diracSE, guid=None, path=None, checksum=
586
592
startTime = time .time ()
587
593
res = self .registerFile (fileTuple )
588
594
registerTime = time .time () - startTime
589
- oDataOperation .setValueByKey ("RegistrationTime" , registerTime )
595
+
596
+ accountingDict ["RegistrationTotal" ] = 1
597
+ accountingDict ["RegistrationTime" ] = registerTime
598
+
590
599
if not res ["OK" ]:
591
600
errStr = "Completely failed to register file."
592
601
log .debug (errStr , res ["Message" ])
593
602
failed [lfn ] = {"register" : registerDict }
594
- oDataOperation .setValueByKey ("FinalStatus" , "Failed" )
603
+ accountingDict ["FinalStatus" ] = "Failed"
604
+
595
605
elif lfn in res ["Value" ]["Failed" ]:
596
606
errStr = "Failed to register file."
597
607
log .debug (errStr , "%s %s" % (lfn , res ["Value" ]["Failed" ][lfn ]))
598
- oDataOperation . setValueByKey ( "FinalStatus" , "Failed" )
608
+ accountingDict [ "FinalStatus" ] = "Failed"
599
609
failed [lfn ] = {"register" : registerDict }
600
610
else :
601
611
successful [lfn ]["register" ] = registerTime
602
- oDataOperation . setValueByKey ( "RegistrationOK" , 1 )
603
- oDataOperation . setEndTime ()
604
- gDataStoreClient . addRegister ( oDataOperation )
612
+ accountingDict [ "RegistrationOK" ] = 1
613
+
614
+ # Send to Monitoring/Accounting
605
615
startTime = time .time ()
606
- gDataStoreClient .commit ()
607
- log .debug ("Sending accounting took %.1f seconds" % (time .time () - startTime ))
616
+ sendingResult = self .dataOpSender .sendData (accountingDict , commitFlag = True )
617
+ log .verbose ("Committing data operation" )
618
+ if not sendingResult ["OK" ]:
619
+ log .error ("Couldn't commit data operation" , sendingResult ["Message" ])
620
+ return sendingResult
621
+ log .verbose ("Done committing" )
622
+ log .debug ("putAndRegister: Sending took %.1f seconds" % (time .time () - startTime ))
623
+
608
624
return S_OK ({"Successful" : successful , "Failed" : failed })
609
625
610
626
def replicateAndRegister (self , lfn , destSE , sourceSE = "" , destPath = "" , localCache = "" , catalog = "" ):
@@ -1186,7 +1202,7 @@ def removeFile(self, lfn, force=None):
1186
1202
failed .update (res ["Value" ]["Failed" ])
1187
1203
successful .update (res ["Value" ]["Successful" ])
1188
1204
1189
- gDataStoreClient . commit ()
1205
+ self . dataOpSender . concludeSending ()
1190
1206
return S_OK ({"Successful" : successful , "Failed" : failed })
1191
1207
1192
1208
def __removeFile (self , lfnDict ):
@@ -1286,7 +1302,7 @@ def removeReplica(self, storageElementName, lfn):
1286
1302
return res
1287
1303
failed .update (res ["Value" ]["Failed" ])
1288
1304
successful .update (res ["Value" ]["Successful" ])
1289
- gDataStoreClient . commit ()
1305
+ self . dataOpSender . concludeSending ()
1290
1306
return S_OK ({"Successful" : successful , "Failed" : failed })
1291
1307
1292
1308
def __removeReplica (self , storageElementName , lfns , replicaDict = None ):
@@ -1399,20 +1415,23 @@ def __removeCatalogReplica(self, replicaTuples):
1399
1415
:param replicaTuples : list of (lfn, catalogPFN, se)
1400
1416
"""
1401
1417
log = self .log .getSubLogger ("__removeCatalogReplica" )
1402
- oDataOperation = _initialiseAccountingObject ( "removeCatalogReplica" , "" , len ( replicaTuples ))
1403
- oDataOperation . setStartTime ()
1404
- start = time .time ()
1418
+
1419
+ startTime = Time . dateTime ()
1420
+ registrationStartTime = time .time ()
1405
1421
# HACK!
1406
1422
replicaDict = {}
1407
1423
for lfn , pfn , se in replicaTuples :
1408
1424
replicaDict [lfn ] = {"SE" : se , "PFN" : pfn }
1409
1425
res = self .fileCatalog .removeReplica (replicaDict )
1410
- oDataOperation .setEndTime ()
1411
- oDataOperation .setValueByKey ("RegistrationTime" , time .time () - start )
1426
+ endTime = Time .dateTime ()
1427
+ accountingDict = _initialiseAccountingDict ("removeCatalogReplica" , "" , len (replicaTuples ))
1428
+ accountingDict ["RegistrationTime" ] = time .time () - registrationStartTime
1429
+
1412
1430
if not res ["OK" ]:
1413
- oDataOperation .setValueByKey ("RegistrationOK" , 0 )
1414
- oDataOperation .setValueByKey ("FinalStatus" , "Failed" )
1415
- gDataStoreClient .addRegister (oDataOperation )
1431
+ accountingDict ["RegistrationOK" ] = 0
1432
+ accountingDict ["FinalStatus" ] = "Failed"
1433
+ self .dataOpSender .sendData (accountingDict , startTime = startTime , endTime = endTime )
1434
+
1416
1435
errStr = "Completely failed to remove replica: "
1417
1436
log .debug (errStr , res ["Message" ])
1418
1437
return S_ERROR ("%s %s" % (errStr , res ["Message" ]))
@@ -1437,8 +1456,9 @@ def __removeCatalogReplica(self, replicaTuples):
1437
1456
for lfn in success :
1438
1457
log .debug ("Successfully removed replica." , lfn )
1439
1458
1440
- oDataOperation .setValueByKey ("RegistrationOK" , len (success ))
1441
- gDataStoreClient .addRegister (oDataOperation )
1459
+ accountingDict ["RegistrationOK" ] = len (success )
1460
+ self .dataOpSender .sendData (accountingDict , startTime = startTime , endTime = endTime )
1461
+
1442
1462
return res
1443
1463
1444
1464
def __removePhysicalReplica (self , storageElementName , lfnsToRemove , replicaDict = None ):
@@ -1456,19 +1476,22 @@ def __removePhysicalReplica(self, storageElementName, lfnsToRemove, replicaDict=
1456
1476
errStr = "The storage element is not currently valid."
1457
1477
log .verbose (errStr , "%s %s" % (storageElementName , res ["Message" ]))
1458
1478
return S_ERROR ("%s %s" % (errStr , res ["Message" ]))
1459
- oDataOperation = _initialiseAccountingObject ( "removePhysicalReplica" , storageElementName , len ( lfnsToRemove ))
1460
- oDataOperation . setStartTime ()
1461
- start = time .time ()
1479
+
1480
+ startTime = Time . dateTime ()
1481
+ transferStartTime = time .time ()
1462
1482
lfnsToRemove = list (lfnsToRemove )
1463
1483
ret = storageElement .getFileSize (lfnsToRemove , replicaDict = replicaDict )
1464
1484
deletedSizes = ret .get ("Value" , {}).get ("Successful" , {})
1465
1485
res = storageElement .removeFile (lfnsToRemove , replicaDict = replicaDict )
1466
- oDataOperation .setEndTime ()
1467
- oDataOperation .setValueByKey ("TransferTime" , time .time () - start )
1486
+ endTime = Time .dateTime ()
1487
+ accountingDict = _initialiseAccountingDict ("removePhysicalReplica" , storageElementName , len (lfnsToRemove ))
1488
+ accountingDict ["TransferTime" ] = time .time () - transferStartTime
1489
+
1468
1490
if not res ["OK" ]:
1469
- oDataOperation .setValueByKey ("TransferOK" , 0 )
1470
- oDataOperation .setValueByKey ("FinalStatus" , "Failed" )
1471
- gDataStoreClient .addRegister (oDataOperation )
1491
+ accountingDict ["TransferOK" ] = 0
1492
+ accountingDict ["FinalStatus" ] = "Failed"
1493
+ self .dataOpSender .sendData (accountingDict , startTime = startTime , endTime = endTime )
1494
+
1472
1495
log .debug ("Failed to remove replicas." , res ["Message" ])
1473
1496
else :
1474
1497
for lfn , value in list (res ["Value" ]["Failed" ].items ()):
@@ -1479,10 +1502,11 @@ def __removePhysicalReplica(self, storageElementName, lfnsToRemove, replicaDict=
1479
1502
res ["Value" ]["Successful" ][lfn ] = True
1480
1503
1481
1504
deletedSize = sum (deletedSizes .get (lfn , 0 ) for lfn in res ["Value" ]["Successful" ])
1482
- oDataOperation .setValueByKey ("TransferSize" , deletedSize )
1483
- oDataOperation .setValueByKey ("TransferOK" , len (res ["Value" ]["Successful" ]))
1484
1505
1485
- gDataStoreClient .addRegister (oDataOperation )
1506
+ accountingDict ["TransferSize" ] = deletedSize
1507
+ accountingDict ["TransferOK" ] = len (res ["Value" ]["Successful" ])
1508
+ self .dataOpSender .sendData (accountingDict , startTime = startTime , endTime = endTime )
1509
+
1486
1510
infoStr = "Successfully issued accounting removal request."
1487
1511
log .debug (infoStr )
1488
1512
return res
@@ -1851,3 +1875,6 @@ def getReplica(self, lfn, storageElementName, localPath=False):
1851
1875
:param bool singleFile: execute for the first LFN only
1852
1876
"""
1853
1877
return self .__executeIfReplicaExists (storageElementName , lfn , "getFile" , localPath = localPath )
1878
+
1879
+ def __del__ (self ):
1880
+ self .dataOpSender .concludeSending ()
0 commit comments