Skip to content

Commit e4f787a

Browse files
committed
feat (gfal2): put a timeout around stat calls
1 parent 385b299 commit e4f787a

File tree

1 file changed

+25
-45
lines changed

1 file changed

+25
-45
lines changed

src/DIRAC/Resources/Storage/GFAL2_StorageBase.py

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
MAX_SINGLE_STREAM_SIZE = 1024 * 1024 * 10 # 10MB
5454
MIN_BANDWIDTH = 0.5 * (1024 * 1024) # 0.5 MB/s
5555

56+
# Timeout when performing a stat call
57+
STAT_TIMEOUT = 10
58+
5659

5760
@contextmanager
5861
def setGfalSetting(
@@ -218,11 +221,12 @@ def exists(self, path):
218221
successful = {}
219222
failed = {}
220223

221-
for url in urls:
222-
try:
223-
successful[url] = self.__singleExists(url)
224-
except Exception as e:
225-
failed[url] = repr(e)
224+
with setGfalSetting(self.ctx, "CORE", "NAMESPACE_TIMEOUT", STAT_TIMEOUT):
225+
for url in urls:
226+
try:
227+
successful[url] = self.__singleExists(url)
228+
except Exception as e:
229+
failed[url] = repr(e)
226230

227231
resDict = {"Failed": failed, "Successful": successful}
228232
return resDict
@@ -268,29 +272,15 @@ def isFile(self, path):
268272
successful = {}
269273
failed = {}
270274

271-
for url in urls:
272-
try:
273-
successful[url] = self._isSingleFile(url)
274-
except Exception as e:
275-
failed[url] = repr(e)
275+
with setGfalSetting(self.ctx, "CORE", "NAMESPACE_TIMEOUT", STAT_TIMEOUT):
276+
for url in urls:
277+
try:
278+
successful[url] = S_ISREG(self.ctx.stat(url).st_mode)
279+
except Exception as e:
280+
failed[url] = repr(e)
276281

277282
return {"Failed": failed, "Successful": successful}
278283

279-
def _isSingleFile(self, path: str) -> bool:
280-
"""Checking if :path: exists and is a file
281-
282-
:param str path: single path on the storage (srm://...)
283-
284-
:returns: boolean
285-
286-
:raises:
287-
gfal2.GError: gfal problem
288-
289-
"""
290-
291-
statInfo = self.ctx.stat(path)
292-
return S_ISREG(statInfo.st_mode)
293-
294284
@convertToReturnValue
295285
def putFile(self, path, sourceSize: int = 0):
296286
"""Put a copy of a local file or a file on another srm storage to a directory on the
@@ -600,7 +590,8 @@ def _getSingleFileSize(self, path: str) -> int:
600590
log = self.log.getLocalSubLogger("GFAL2_StorageBase._getSingleFileSize")
601591
log.debug(f"Determining file size of {path}")
602592

603-
statInfo = self.ctx.stat(path) # keeps info like size, mode.
593+
with setGfalSetting(self.ctx, "CORE", "NAMESPACE_TIMEOUT", STAT_TIMEOUT):
594+
statInfo = self.ctx.stat(path) # keeps info like size, mode.
604595

605596
# If it is not a file
606597
if not S_ISREG(statInfo.st_mode):
@@ -675,7 +666,8 @@ def _getSingleMetadata(self, path: str) -> dict[str, Any]:
675666
log = self.log.getLocalSubLogger("GFAL2_StorageBase._getSingleMetadata")
676667
log.debug(f"Reading metadata for {path}")
677668

678-
statInfo = self.ctx.stat(path)
669+
with setGfalSetting(self.ctx, "CORE", "NAMESPACE_TIMEOUT", STAT_TIMEOUT):
670+
statInfo = self.ctx.stat(path)
679671

680672
metadataDict = self.__parseStatInfoFromApiOutput(statInfo)
681673
if metadataDict["File"] and self.checksumType:
@@ -942,27 +934,15 @@ def isDirectory(self, path):
942934
successful = {}
943935
failed = {}
944936

945-
for url in urls:
946-
try:
947-
successful[url] = self._isSingleDirectory(url)
948-
except Exception as e:
949-
failed[url] = f"Failed to determine if path is a directory {repr(e)}"
937+
with setGfalSetting(self.ctx, "CORE", "NAMESPACE_TIMEOUT", STAT_TIMEOUT):
938+
for url in urls:
939+
try:
940+
successful[url] = S_ISDIR(self.ctx.stat(url).st_mode)
941+
except Exception as e:
942+
failed[url] = f"Failed to determine if path is a directory {repr(e)}"
950943

951944
return {"Failed": failed, "Successful": successful}
952945

953-
def _isSingleDirectory(self, path: str) -> bool:
954-
"""Checking if :path: exists and is a directory
955-
956-
:param str path: single path on the storage (srm://...)
957-
958-
:returns: boolean if it is a directory or not
959-
960-
:raises:
961-
gfal2.GError in case of gfal problem
962-
"""
963-
964-
return S_ISDIR(self.ctx.stat(path).st_mode)
965-
966946
@convertToReturnValue
967947
def listDirectory(self, path):
968948
"""List the content of the path provided

0 commit comments

Comments
 (0)