pabloem commented on a change in pull request #13277:
URL: https://github.com/apache/beam/pull/13277#discussion_r521555400



##########
File path: sdks/python/apache_beam/runners/interactive/user_pipeline_tracker.py
##########
@@ -0,0 +1,132 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""Class that tracks derived/pipeline fragments from user pipelines.
+
+For internal use only; no backwards-compatibility guarantees.
+In the InteractiveRunner the design is to keep the user pipeline unchanged,
+create a copy of the user pipeline, and modify the copy. When the derived
+pipeline runs, there should only be per-user pipeline state. This makes sure
+that derived pipelines can link back to the parent user pipeline.
+"""
+# pytype: skip-file
+
+
+class UserPipelineTracker:
+  """Tracks user pipelines from derived pipelines.
+
+  This data structure is similar to a disjoint set data structure. A derived
+  pipeline can only have one parent user pipeline. A user pipeline can have 
many
+  derived pipelines.
+  """
+  def __init__(self):
+    self._user_pipelines = set()
+    self._derived_pipelines = {}
+    self._pid_to_pipelines = {}
+
+  def __iter__(self):
+    """Iterates through all the user pipelines."""
+    for p in self._user_pipelines:
+      yield p
+
+  def _key(self, pipeline):
+    return str(id(pipeline))
+
+  def clear(self):
+    """Clears the tracker of all user and derived pipelines."""
+    self._user_pipelines.clear()
+    self._derived_pipelines.clear()
+    self._pid_to_pipelines.clear()
+
+  def get_pipeline(self, pid):
+    """Returns the pipeline corresponding to the given pipeline id."""
+    if pid in self._pid_to_pipelines:
+      return self._pid_to_pipelines[pid]
+    return None

Review comment:
       Nit: you can write this as `self._pid_to_pipelines.get(pid, None)`
   (nit, meaning not a must)

##########
File path: 
sdks/python/apache_beam/runners/interactive/user_pipeline_tracker_test.py
##########
@@ -0,0 +1,181 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from __future__ import absolute_import
+
+import unittest
+
+import apache_beam as beam
+from apache_beam.runners.interactive.user_pipeline_tracker import 
UserPipelineTracker
+
+
+class UserPipelineTrackerTest(unittest.TestCase):
+  def test_getting_unknown_pid_returns_none(self):
+    ut = UserPipelineTracker()
+
+    p = beam.Pipeline()
+
+    self.assertIsNone(ut.get_pipeline(str(id(p))))
+
+  def test_getting_unknown_pipeline_returns_none(self):
+    ut = UserPipelineTracker()
+
+    p = beam.Pipeline()
+
+    self.assertIsNone(ut.get_user_pipeline(p))
+
+  def test_no_parent_returns_none(self):
+    ut = UserPipelineTracker()
+
+    user = beam.Pipeline()
+    derived = beam.Pipeline()
+    orphan = beam.Pipeline()
+
+    ut.add_derived_pipeline(user, derived)
+
+    self.assertIsNone(ut.get_user_pipeline(orphan))
+
+  def test_get_user_pipeline_is_same(self):
+    ut = UserPipelineTracker()
+
+    p = beam.Pipeline()
+    ut.add_user_pipeline(p)
+
+    self.assertIs(ut.get_user_pipeline(p), p)
+
+  def test_can_add_derived(self):
+    ut = UserPipelineTracker()
+
+    user = beam.Pipeline()
+    derived = beam.Pipeline()
+
+    ut.add_derived_pipeline(user, derived)
+
+    self.assertIs(ut.get_user_pipeline(derived), user)
+
+  def test_can_add_multiple_derived(self):
+    """Tests that there can be many user pipelines with many derived
+    pipelines.
+    """
+    ut = UserPipelineTracker()
+
+    # Add the first set of user and derived pipelines.
+    user1 = beam.Pipeline()
+    derived11 = beam.Pipeline()
+    derived12 = beam.Pipeline()
+
+    ut.add_derived_pipeline(user1, derived11)
+    ut.add_derived_pipeline(user1, derived12)
+
+    # Add the second set of user and derived pipelines.
+    user2 = beam.Pipeline()
+    derived21 = beam.Pipeline()
+    derived22 = beam.Pipeline()
+
+    ut.add_derived_pipeline(user2, derived21)
+    ut.add_derived_pipeline(user2, derived22)
+
+    # Assert that the user pipelines are correct.
+    self.assertIs(ut.get_user_pipeline(derived11), user1)
+    self.assertIs(ut.get_user_pipeline(derived12), user1)
+    self.assertIs(ut.get_user_pipeline(derived21), user2)
+    self.assertIs(ut.get_user_pipeline(derived22), user2)
+
+  def test_cannot_have_multiple_parents(self):
+    ut = UserPipelineTracker()
+
+    user1 = beam.Pipeline()
+    user2 = beam.Pipeline()
+    derived = beam.Pipeline()
+
+    ut.add_derived_pipeline(user1, derived)
+    ut.add_derived_pipeline(user2, derived)

Review comment:
       I wonder if this should throw an exception instead of silently failing?

##########
File path: sdks/python/apache_beam/runners/interactive/user_pipeline_tracker.py
##########
@@ -0,0 +1,132 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""Class that tracks derived/pipeline fragments from user pipelines.
+
+For internal use only; no backwards-compatibility guarantees.
+In the InteractiveRunner the design is to keep the user pipeline unchanged,
+create a copy of the user pipeline, and modify the copy. When the derived
+pipeline runs, there should only be per-user pipeline state. This makes sure
+that derived pipelines can link back to the parent user pipeline.
+"""
+# pytype: skip-file
+
+
+class UserPipelineTracker:
+  """Tracks user pipelines from derived pipelines.
+
+  This data structure is similar to a disjoint set data structure. A derived
+  pipeline can only have one parent user pipeline. A user pipeline can have 
many
+  derived pipelines.
+  """
+  def __init__(self):
+    self._user_pipelines = set()
+    self._derived_pipelines = {}
+    self._pid_to_pipelines = {}

Review comment:
       could you add type annotations to these? that would help figure out when 
we're using the str(id()) and when the object itself, etc.
   
   We're full on Python 3, so you can add them like here; 
https://docs.python.org/3/library/typing.html

##########
File path: sdks/python/apache_beam/runners/interactive/user_pipeline_tracker.py
##########
@@ -0,0 +1,132 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""Class that tracks derived/pipeline fragments from user pipelines.
+
+For internal use only; no backwards-compatibility guarantees.
+In the InteractiveRunner the design is to keep the user pipeline unchanged,
+create a copy of the user pipeline, and modify the copy. When the derived
+pipeline runs, there should only be per-user pipeline state. This makes sure
+that derived pipelines can link back to the parent user pipeline.
+"""
+# pytype: skip-file
+
+
+class UserPipelineTracker:
+  """Tracks user pipelines from derived pipelines.
+
+  This data structure is similar to a disjoint set data structure. A derived
+  pipeline can only have one parent user pipeline. A user pipeline can have 
many
+  derived pipelines.
+  """
+  def __init__(self):
+    self._user_pipelines = set()
+    self._derived_pipelines = {}
+    self._pid_to_pipelines = {}

Review comment:
       fwiw - I recommend you try to add typehints everywhere going forward. 
these will be super useful.

##########
File path: 
sdks/python/apache_beam/runners/interactive/user_pipeline_tracker_test.py
##########
@@ -0,0 +1,181 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from __future__ import absolute_import
+
+import unittest
+
+import apache_beam as beam
+from apache_beam.runners.interactive.user_pipeline_tracker import 
UserPipelineTracker
+
+
+class UserPipelineTrackerTest(unittest.TestCase):
+  def test_getting_unknown_pid_returns_none(self):
+    ut = UserPipelineTracker()
+
+    p = beam.Pipeline()
+
+    self.assertIsNone(ut.get_pipeline(str(id(p))))
+
+  def test_getting_unknown_pipeline_returns_none(self):
+    ut = UserPipelineTracker()
+
+    p = beam.Pipeline()
+
+    self.assertIsNone(ut.get_user_pipeline(p))
+
+  def test_no_parent_returns_none(self):
+    ut = UserPipelineTracker()
+
+    user = beam.Pipeline()
+    derived = beam.Pipeline()
+    orphan = beam.Pipeline()
+
+    ut.add_derived_pipeline(user, derived)
+
+    self.assertIsNone(ut.get_user_pipeline(orphan))
+
+  def test_get_user_pipeline_is_same(self):
+    ut = UserPipelineTracker()
+
+    p = beam.Pipeline()
+    ut.add_user_pipeline(p)
+
+    self.assertIs(ut.get_user_pipeline(p), p)
+
+  def test_can_add_derived(self):
+    ut = UserPipelineTracker()
+
+    user = beam.Pipeline()
+    derived = beam.Pipeline()
+
+    ut.add_derived_pipeline(user, derived)
+
+    self.assertIs(ut.get_user_pipeline(derived), user)
+
+  def test_can_add_multiple_derived(self):
+    """Tests that there can be many user pipelines with many derived
+    pipelines.
+    """
+    ut = UserPipelineTracker()
+
+    # Add the first set of user and derived pipelines.
+    user1 = beam.Pipeline()
+    derived11 = beam.Pipeline()
+    derived12 = beam.Pipeline()
+
+    ut.add_derived_pipeline(user1, derived11)
+    ut.add_derived_pipeline(user1, derived12)
+
+    # Add the second set of user and derived pipelines.
+    user2 = beam.Pipeline()
+    derived21 = beam.Pipeline()
+    derived22 = beam.Pipeline()
+
+    ut.add_derived_pipeline(user2, derived21)
+    ut.add_derived_pipeline(user2, derived22)
+
+    # Assert that the user pipelines are correct.
+    self.assertIs(ut.get_user_pipeline(derived11), user1)
+    self.assertIs(ut.get_user_pipeline(derived12), user1)
+    self.assertIs(ut.get_user_pipeline(derived21), user2)
+    self.assertIs(ut.get_user_pipeline(derived22), user2)
+
+  def test_cannot_have_multiple_parents(self):
+    ut = UserPipelineTracker()
+
+    user1 = beam.Pipeline()
+    user2 = beam.Pipeline()
+    derived = beam.Pipeline()
+
+    ut.add_derived_pipeline(user1, derived)
+    ut.add_derived_pipeline(user2, derived)

Review comment:
       or perhaps the return value should communicate this somehow




----------------------------------------------------------------
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.

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


Reply via email to