18903: Merge branch 'main' into 18903-fix-activity-script
[arvados.git] / sdk / python / arvados / keep.py
index 53776017dbad07cfc1e73f96f0f0d978626a4f98..7c05cc0a6a2c72ca818686b6eea5c6f0a4874d3d 100644 (file)
@@ -1058,7 +1058,7 @@ class KeepClient(object):
     def get(self, loc_s, **kwargs):
         return self._get_or_head(loc_s, method="GET", **kwargs)
 
-    def _get_or_head(self, loc_s, method="GET", num_retries=None, request_id=None, headers=None, cache_slot_get=True):
+    def _get_or_head(self, loc_s, method="GET", num_retries=None, request_id=None, headers=None, prefetch=False):
         """Get data from Keep.
 
         This method fetches one or more blocks of data from Keep.  It
@@ -1097,16 +1097,19 @@ class KeepClient(object):
             if method == "GET":
                 slot, first = self.block_cache.reserve_cache(locator.md5sum)
                 if not first:
-                    self.hits_counter.add(1)
-                    if cache_slot_get:
-                        blob = slot.get()
-                        if blob is None:
-                            raise arvados.errors.KeepReadError(
-                                "failed to read {}".format(loc_s))
-                        return blob
-                    else:
-                        slot = None  # prevent finally from calling slot.set()
+                    if prefetch:
+                        # this is request for a prefetch, if it is
+                        # already in flight, return immediately.
+                        # clear 'slot' to prevent finally block from
+                        # calling slot.set()
+                        slot = None
                         return None
+                    self.hits_counter.add(1)
+                    blob = slot.get()
+                    if blob is None:
+                        raise arvados.errors.KeepReadError(
+                            "failed to read {}".format(loc_s))
+                    return blob
 
             self.misses_counter.add(1)