RuntimeSAMStageOut.py 10.8 KB
Newer Older
Andrea Sciaba's avatar
Andrea Sciaba committed
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python
"""
_RuntimeSAMStageOut_

Test script for using TFC to determine local stage out is working properly
at a site

"""

import time
import os
import sys

Andrea Sciaba's avatar
Andrea Sciaba committed
14
from WMCore.Storage.SiteLocalConfig import loadSiteLocalConfig
Andrea Sciaba's avatar
Andrea Sciaba committed
15

Andrea Sciaba's avatar
Andrea Sciaba committed
16
from WMCore.Storage.Registry import retrieveStageOutImpl
Andrea Sciaba's avatar
Andrea Sciaba committed
17

Andrea Sciaba's avatar
Andrea Sciaba committed
18
19
import WMCore.Storage.Backends
import WMCore.Storage.Plugins
Andrea Sciaba's avatar
Andrea Sciaba committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

class StageOutDiagnostic:
    """
    _StageOutDiagnostic_

    Object to test the local stage out details step by step


    """
    def __init__(self):
        self.summary = {}
        self.summary.setdefault('SiteConf' , "NotRun" )
        self.summary.setdefault('TFC' , "NotRun")
        self.summary.setdefault('LocalStageOut' , "NotRun")
        self.summary.setdefault('CleanUp' , "NotRun")
        self.status = 0
        self.siteConf = None
        self.tfc = None
        self.datestamp = time.asctime(time.localtime(time.time()))
39
        self.datestamp = self.datestamp.replace(" ", "-").replace(":", "_")
40
        self.testLFN = "/store/unmerged/SAM/StageOutTest-%i-%s" % (os.getpid(), self.datestamp)
Andrea Sciaba's avatar
Andrea Sciaba committed
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
        

    def __call__(self):
        """
        _operator()_

        Invoke step by step tests and create the summary

        """
        try:
            self.testSiteConf()
        except Exception, ex:
            print str(ex)
            self.status = 1
            self.complete()
            return

        try:
            self.testTFC()
        except Exception, ex:
            print str(ex)
            self.status = 2
            self.complete()
            return

        try:
            self.testLocalStageOut()
        except Exception, ex:
            print str(ex)
            self.status = 3
            self.complete()
            return
            
        try:
            self.testCleanUp()
        except Exception, ex:
            print str(ex)
            self.status = 4
            self.complete()
            return

    def complete(self):
        """
        _complete_

        Print summary after tests
        
        """
        msg = "==== StageOut Test Summary ====\n"
        if self.status != 0:
            msg += "Status: FAILED: %s\n" % self.status
        else:
            msg += "Test Successful\n"
            
        for key, val in self.summary.items():
            msg += "  Test: %s : %s\n" % (key, val)
        print msg
        return
        
        
    def testSiteConf(self):
        """
        _testSiteConf_

        Test reading the site conf file

        """
        if not os.environ.has_key("CMS_PATH"):
            msg = "CMS_PATH Not Set: Cannot find SiteConf"
            self.summary['SiteConf'] = "Failed: CMS_PATH not set"
            raise RuntimeError, msg

        try:
            self.siteConf = loadSiteLocalConfig()
        except Exception, ex:
            msg = "Error loading Site Conf File: %s" % str(ex)
            self.summary['SiteConf'] = "Failed: Cannot load SiteConf"
            raise RuntimeError, msg
        
        if self.siteConf.localStageOut['command'] == None:
            msg = "LocalStageOut Command is not set"
            self.summary['SiteConf'] = \
                    "Failed: local-stage-out command not set"
            raise RuntimeError, msg

        if self.siteConf.localStageOut['se-name'] == None:
            msg = "LocalStageOut SE Name is not set"
            self.summary['SiteConf'] = \
                    "Failed: local-stage-out se-name not set"
            raise RuntimeError, msg

        if self.siteConf.localStageOut['catalog'] == None:
            msg = "LocalStageOut Catalog is not set"
            self.summary['SiteConf'] = \
                    "Failed: local-stage-out catalog not set"
            raise RuntimeError, msg

        msg = "SiteConf test successful:\n"
        for key, val in self.siteConf.localStageOut.items():
            msg += "   %s = %s\n" % (key, val)
        print msg
        
        return
    
            
    def testTFC(self):
        """
        _testTFC_

        Test the Trivial File Catalog

        """
        try:
            self.tfc = self.siteConf.trivialFileCatalog()
        except Exception, ex:
            msg = "Failed to load Trivial File Catalog: %s" % str(ex)
            self.summary['TFC'] = "Failed: Cannot load TFC"
            raise RuntimeError, msg

        sampleLFN = "/store/unmerged/testLFN"
        try:
            samplePFN = self.tfc.matchLFN(self.tfc.preferredProtocol,
                                          sampleLFN)
        except Exception, ex:
            msg = "Failed to translate LFN: %s" % str(ex)
            self.summary['TFC'] = "Failed: Cannot translate LFN to PFN"
            raise RuntimeError, msg

        msg = "TFC test successful:\n"
        msg += "Mapped LFN: %s\n To PFN: %s\n" % (sampleLFN, samplePFN)
171
172
#        msg += "Using Catalog Rules:\n"
#        msg += str(self.tfc)
Andrea Sciaba's avatar
Andrea Sciaba committed
173
174
175
176
177
178
179
180
181
182
183
        print msg
        return
        
        
    def testLocalStageOut(self):
        """
        _testLocalStageOut_

        Test a local stage out

        """
Andrea Sciaba's avatar
Andrea Sciaba committed
184
185
186
        wasSuccessful = False
        msg = ""

Andrea Sciaba's avatar
Andrea Sciaba committed
187
188
189
190
191
        handle = open("TEST-FILE", 'w')
        for i in range(0, 1000):
            handle.write("This is a test file\n")
        handle.close()
        sourcePFN = os.path.join(os.getcwd(), "TEST-FILE")
Andrea Sciaba's avatar
Andrea Sciaba committed
192
                
Andrea Sciaba's avatar
Andrea Sciaba committed
193
194
195
196
197
198
199
200
        
        seName   = self.siteConf.localStageOut['se-name']
        command  = self.siteConf.localStageOut['command']
        options  = self.siteConf.localStageOut.get('option', None)
        protocol = self.tfc.preferredProtocol
        
        targetPFN = self.tfc.matchLFN(self.tfc.preferredProtocol, self.testLFN)
        
Andrea Sciaba's avatar
Andrea Sciaba committed
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
        # first try the regular stageout
        try: # an exception around normal stageout
            try:
                impl = retrieveStageOutImpl(command)
            except Exception, ex:
                msg += "Unable to retrieve impl for local stage out:\n"
                msg += "Error retrieving StageOutImpl for command named: %s\n" % (
                    command,)
                self.summary['LocalStageOut'] = \
                          "Failure: Cant retrieve StageOut Impl"
                raise RuntimeError, msg
            
            try:
                impl.retryPause = 15
                impl(protocol, sourcePFN, targetPFN, options)
                wasSuccessful = True
            except Exception, ex:
                msg += "Failure for local stage out:\n"
                msg += str(ex)
                self.summary['LocalStageOut'] = \
                                          "Failure: Local Stage Out Failed"
                raise RuntimeError, msg
            if wasSuccessful:
                return
                
        except RuntimeError, ex:
    
            ### FALLBACK ###
            ### there are N fallbacks in a list called fallbackStageOut ###
            for fallbackCount in range(len(self.siteConf.fallbackStageOut)):
                seName   = self.siteConf.fallbackStageOut[fallbackCount]['se-name']
                command  = self.siteConf.fallbackStageOut[fallbackCount]['command']
                options  = self.siteConf.fallbackStageOut[fallbackCount].get('option', None)
                if self.siteConf.fallbackStageOut[fallbackCount]['lfn-prefix']:
                    targetPFN = self.siteConf.fallbackStageOut[fallbackCount]['lfn-prefix'] + self.testLFN
                else:
                    targetPFN = self.testLFN
Andrea Sciaba's avatar
Andrea Sciaba committed
238
        
Andrea Sciaba's avatar
Andrea Sciaba committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
                try:
                    impl = retrieveStageOutImpl(command)
                except Exception, ex:
                    msg += "Unable to retrieve impl for local stage out:\n"
                    msg += "Error retrieving StageOutImpl for command named: %s\n" % (
                        command,)
                    self.summary['LocalStageOut'] += \
                              "\nFailure: Cant retrieve StageOut Impl for fallback %s" % fallbackCount
                    raise RuntimeError, msg
                
                try:
                    impl.retryPause = 15
                    impl(protocol, sourcePFN, targetPFN, options)
                    wasSuccessful = True
                except Exception, ex:
                    msg += "Failure for local stage out:\n"
                    msg += str(ex)
                    self.summary['LocalStageOut'] += \
                                              "\nFailure: Fallback %s Stage Out Failed" % fallbackCount
                    raise RuntimeError, msg
                
                if wasSuccessful:
                    self.summary['LocalStageOut'] = ""
                    return
Andrea Sciaba's avatar
Andrea Sciaba committed
263
        
Andrea Sciaba's avatar
Andrea Sciaba committed
264
265
266
267
        # If we got here, nothing worked
        raise RuntimeError, msg
	

Andrea Sciaba's avatar
Andrea Sciaba committed
268
269
270
271
    def testCleanUp(self):
        """
        _testCleanUp_

272
        Test clean up of the temp file from SE
Andrea Sciaba's avatar
Andrea Sciaba committed
273
274

        """
Andrea Sciaba's avatar
Andrea Sciaba committed
275
276
277
        os.remove( "TEST-FILE" )
        commandList = [ self.siteConf.localStageOut[ 'command' ] ]
        pfnList     = [ self.tfc.matchLFN(self.tfc.preferredProtocol, self.testLFN) ]
278
        
Andrea Sciaba's avatar
Andrea Sciaba committed
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
        for fallback in self.siteConf.fallbackStageOut:
           commandList.append( fallback[ 'command' ])
       	   if fallback['lfn-prefix']:
               pfnList.append( fallback[ 'lfn-prefix' ] + self.testLFN )
           else:
               pfnList.append( self.testLFN )
        		 	
        wasSuccessful = False
        msg = ""
        for (command, pfn) in zip( commandList, pfnList ):
            try: # outer try to catch the fallback as a whole
        	
                try: # inner try for getting the impl
                    implInstance = retrieveStageOutImpl(command)
                except Exception, ex:
                    msg += "Unable to retrieve impl for clean up:\n"
                    msg += "Error retrieving StageOutImpl for command named: %s\n" % (command,)
                    self.summary['CleanUp'] = "Failure: Cant retrieve StageOut Impl"
                    raise RuntimeError, msg
		        
                #  //
                # //  Invoke StageOut Impl removeFile method
                #//
                try: # inner try for calling removeFile
                    implInstance.removeFile(pfn)
                except Exception, ex:
                    msg += "Error performing Cleanup command for impl "
                    msg += "%s\n" % command
                    msg += "On PFN: %s\n" % pfn
                    msg += str(ex)
                    self.summary['CleanUp'] = "Failure: Cleanup operation Failed"
                    raise RuntimeError, msg
                wasSuccessful = True
		    
            except: # except for outer try
                wasSuccessful = False
	        
            # See if this fallback worked
            if wasSuccessful:
                self.summary['CleanUp'] = ""
                return
            else:
                msg += "Trying Fallback...\n"
       	
        # nothing worked, bomb out
        raise RuntimeError, msg
325
    
Andrea Sciaba's avatar
Andrea Sciaba committed
326
327
328
329
330
331
if __name__ == '__main__':

    diagnostic = StageOutDiagnostic()

    diagnostic()
    sys.exit(diagnostic.status)