damccorm commented on code in PR #41396:
URL: https://github.com/apache/beam/pull/41396#discussion_r4667163261


##########
sdks/python/apache_beam/runners/worker/statecache.py:
##########
@@ -331,6 +332,42 @@ def put(self, key: Any, value: Any) -> None:
         self._current_weight -= weighted_value.weight()
         self._evict_count += 1
 
+  def extend(self, key: Any, elements: Iterable[Any]) -> None:
+    assert self.is_cache_enabled()
+    with self._lock:
+      entry = self._cache.get(key, None)
+      if entry is None or _safe_isinstance(entry, _LoadingValue):
+        return

Review Comment:
   I'm having a hard time reasoning about when we would hit this behavior - I 
think never?
   
   Should this throw instead?



##########
sdks/python/apache_beam/runners/worker/statecache.py:
##########
@@ -331,6 +332,42 @@ def put(self, key: Any, value: Any) -> None:
         self._current_weight -= weighted_value.weight()
         self._evict_count += 1
 
+  def extend(self, key: Any, elements: Iterable[Any]) -> None:
+    assert self.is_cache_enabled()
+    with self._lock:
+      entry = self._cache.get(key, None)
+      if entry is None or _safe_isinstance(entry, _LoadingValue):
+        return
+      if not hasattr(entry.value(), 'extend'):

Review Comment:
   Same sort of question - will we ever hit this? If yes, is this a regression?



##########
sdks/python/apache_beam/runners/worker/statecache.py:
##########
@@ -331,6 +332,42 @@ def put(self, key: Any, value: Any) -> None:
         self._current_weight -= weighted_value.weight()
         self._evict_count += 1
 
+  def extend(self, key: Any, elements: Iterable[Any]) -> None:
+    assert self.is_cache_enabled()
+    with self._lock:
+      entry = self._cache.get(key, None)
+      if entry is None or _safe_isinstance(entry, _LoadingValue):
+        return
+      if not hasattr(entry.value(), 'extend'):
+        raise TypeError(
+            'Cached value of type %s does not support extend' %
+            type(entry.value()))
+
+    elements = list(elements)
+    added_weight = max(0, get_deep_size(*elements)) if elements else 0
+    with self._lock:
+      entry = self._cache.get(key, None)
+      if entry is None or _safe_isinstance(entry, _LoadingValue):
+        return
+      cached_value = entry.value()
+      if not hasattr(cached_value, 'extend'):
+        raise TypeError(
+            'Cached value of type %s does not support extend' %
+            type(cached_value))

Review Comment:
   These are also the same checks as above - I don't think we need the above 
locking given that. I think some of the sizing functions below also rely on 
cached_value being a list (which is the only possibility as of now). So maybe 
we should just replace this with a check that `entry` is nonempty and 
`cached_value` is a list, and throw if either is not correct? That makes the 
assumptions quite clear and should maintain the current behavior.



##########
sdks/python/apache_beam/runners/worker/sdk_worker.py:
##########
@@ -1269,10 +1269,7 @@ def extend(
         # Materialize provided iterable to ensure reproducible iterations,
         # here and when writing to the state handler below.
         elements = list(elements)

Review Comment:
   Looks like we're doing this work inside extend as well - we should only need 
this in one place (probably here given the preceding comment)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to