You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/08/18 23:49:44 UTC

[GitHub] [beam] davidyan74 commented on a change in pull request #12415: [BEAM-10603] Add the RecordingManager and associated classes.

davidyan74 commented on a change in pull request #12415:
URL: https://github.com/apache/beam/pull/12415#discussion_r472548446



##########
File path: sdks/python/apache_beam/runners/interactive/interactive_runner.py
##########
@@ -152,7 +152,11 @@ def run_pipeline(self, pipeline, options):
               user_pipeline)):
         streaming_cache_manager = ie.current_env().get_cache_manager(
             user_pipeline)
-        if streaming_cache_manager:
+
+        # Only make the server if it doens't exist already.
+        if (streaming_cache_manager and
+            not ie.current_env().get_test_stream_service_controller(

Review comment:
       Just wondering, what if the service controller has stopped? Is it possible?

##########
File path: sdks/python/apache_beam/runners/interactive/recording_manager.py
##########
@@ -0,0 +1,330 @@
+#
+# 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 logging
+import threading
+import time
+import warnings
+
+import apache_beam as beam
+from apache_beam.runners.interactive import background_caching_job as bcj
+from apache_beam.runners.interactive import interactive_environment as ie
+from apache_beam.runners.interactive import interactive_runner as ir
+from apache_beam.runners.interactive import pipeline_fragment as pf
+from apache_beam.runners.interactive import pipeline_instrument as pi
+from apache_beam.runners.interactive import utils
+from apache_beam.runners.interactive.options.capture_limiters import CountLimiter
+from apache_beam.runners.interactive.options.capture_limiters import ProcessingTimeLimiter
+
+_LOGGER = logging.getLogger(__name__)
+
+PipelineState = beam.runners.runner.PipelineState
+
+
+class ElementStream:
+  """A stream of elements from a given PCollection."""
+  def __init__(
+      self,
+      pcoll,  # type: beam.pvalue.PCollection
+      var,  # type: str
+      cache_key,  # type: str
+      max_n,  # type: int
+      max_duration_secs  # type: float
+      ):
+    self._pcoll = pcoll
+    self._cache_key = cache_key
+    self._pipeline = pcoll.pipeline
+    self._var = var
+    self._n = max_n
+    self._duration_secs = max_duration_secs
+
+    # A small state variable that when True, indicates that no more new elements
+    # will be yielded if read() is called again.
+    self._done = False
+
+  def var(self):
+    # type: () -> str
+
+    """Returns the variable named that defined this PCollection."""
+    return self._var
+
+  def display_id(self, suffix):
+    #Any type: (str) -> str
+
+    """Returns a unique id able to be displayed in a web browser."""
+    return utils.obfuscate(self._cache_key, suffix)
+
+  def is_computed(self):
+    # type: () -> boolean
+
+    """Returns True if no more elements will be recorded."""
+    return self._pcoll in ie.current_env().computed_pcollections
+
+  def is_done(self):
+    # type: () -> boolean
+
+    """Returns True if no more new elements will be yielded."""
+    return self._done
+
+  def read(self, tail=True):
+    # type: (boolean) -> Any
+
+    """Reads the elements currently recorded."""
+
+    # Get the cache manager and wait until the file exists.
+    cache_manager = ie.current_env().get_cache_manager(self._pipeline)
+    while not cache_manager.exists('full', self._cache_key):
+      pass
+
+    # Retrieve the coder for the particular PCollection which will be used to
+    # decode elements read from cache.
+    coder = cache_manager.load_pcoder('full', self._cache_key)
+
+    # Read the elements from the cache.
+    limiters = [
+        CountLimiter(self._n), ProcessingTimeLimiter(self._duration_secs)
+    ]
+    if hasattr(cache_manager, 'read_multiple'):
+      reader = cache_manager.read_multiple([('full', self._cache_key)],
+                                           limiters=limiters,
+                                           tail=tail)
+    else:
+      reader, _ = cache_manager.read('full', self._cache_key, limiters=limiters)
+
+    # Because a single TestStreamFileRecord can yield multiple elements, we
+    # limit the count again here in the to_element_list call.
+    for e in utils.to_element_list(reader,
+                                   coder,
+                                   include_window_info=True,
+                                   n=self._n):
+      yield e
+
+    # A limiter being triggered means that we have fulfilled the user's request.
+    # This implies that reading from the cache again won't yield any new
+    # elements. WLOG, this applies to the user pipeline being terminated.
+    if any(l.is_triggered()
+           for l in limiters) or ie.current_env().is_terminated(self._pipeline):
+      self._done = True
+
+
+class Recording:
+  """A group of PCollections from a given pipeline run."""
+  def __init__(
+      self,
+      user_pipeline,  # type: beam.Pipeline
+      pcolls,  # type: List[beam.pvalue.PCollection]
+      result,  # type: beam.runner.PipelineResult
+      pipeline_instrument,  # type: beam.runners.interactive.PipelineInstrument
+      max_n,  # type: int
+      max_duration_secs  # type: float
+      ):
+
+    self._user_pipeline = user_pipeline
+    self._result = result
+    self._pcolls = pcolls
+
+    pcoll_var = lambda pcoll: pipeline_instrument.cacheable_var_by_pcoll_id(
+        pipeline_instrument.pcolls_to_pcoll_id.get(str(pcoll), None))
+
+    self._streams = {
+        pcoll: ElementStream(
+            pcoll,
+            pcoll_var(pcoll),
+            pipeline_instrument.cache_key(pcoll),
+            max_n,
+            max_duration_secs)
+        for pcoll in pcolls
+    }
+    self._start = time.time()
+    self._duration_secs = max_duration_secs
+    self._set_computed = bcj.is_cache_complete(str(id(user_pipeline)))
+
+    # Run a separate thread for marking the PCollections done. This is because
+    # the pipeline run may be asynchronous.
+    self._mark_computed = threading.Thread(target=self._mark_all_computed)
+    self._mark_computed.daemon = True
+    self._mark_computed.start()
+
+  def _mark_all_computed(self):
+    # type: () -> None
+
+    """Marks all the PCollections upon a successful pipeline run."""
+    if not self._result:
+      return
+
+    while not PipelineState.is_terminal(self._result.state):

Review comment:
       This also looks like busy waiting.

##########
File path: sdks/python/apache_beam/runners/interactive/interactive_runner.py
##########
@@ -152,7 +152,11 @@ def run_pipeline(self, pipeline, options):
               user_pipeline)):
         streaming_cache_manager = ie.current_env().get_cache_manager(
             user_pipeline)
-        if streaming_cache_manager:
+
+        # Only make the server if it doens't exist already.

Review comment:
       typo: doesn't

##########
File path: sdks/python/apache_beam/runners/interactive/recording_manager.py
##########
@@ -0,0 +1,330 @@
+#
+# 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 logging
+import threading
+import time
+import warnings
+
+import apache_beam as beam
+from apache_beam.runners.interactive import background_caching_job as bcj
+from apache_beam.runners.interactive import interactive_environment as ie
+from apache_beam.runners.interactive import interactive_runner as ir
+from apache_beam.runners.interactive import pipeline_fragment as pf
+from apache_beam.runners.interactive import pipeline_instrument as pi
+from apache_beam.runners.interactive import utils
+from apache_beam.runners.interactive.options.capture_limiters import CountLimiter
+from apache_beam.runners.interactive.options.capture_limiters import ProcessingTimeLimiter
+
+_LOGGER = logging.getLogger(__name__)
+
+PipelineState = beam.runners.runner.PipelineState
+
+
+class ElementStream:
+  """A stream of elements from a given PCollection."""
+  def __init__(
+      self,
+      pcoll,  # type: beam.pvalue.PCollection
+      var,  # type: str
+      cache_key,  # type: str
+      max_n,  # type: int
+      max_duration_secs  # type: float
+      ):
+    self._pcoll = pcoll
+    self._cache_key = cache_key
+    self._pipeline = pcoll.pipeline
+    self._var = var
+    self._n = max_n
+    self._duration_secs = max_duration_secs
+
+    # A small state variable that when True, indicates that no more new elements
+    # will be yielded if read() is called again.
+    self._done = False
+
+  def var(self):
+    # type: () -> str
+
+    """Returns the variable named that defined this PCollection."""
+    return self._var
+
+  def display_id(self, suffix):
+    #Any type: (str) -> str
+
+    """Returns a unique id able to be displayed in a web browser."""
+    return utils.obfuscate(self._cache_key, suffix)
+
+  def is_computed(self):
+    # type: () -> boolean
+
+    """Returns True if no more elements will be recorded."""
+    return self._pcoll in ie.current_env().computed_pcollections
+
+  def is_done(self):
+    # type: () -> boolean
+
+    """Returns True if no more new elements will be yielded."""
+    return self._done
+
+  def read(self, tail=True):
+    # type: (boolean) -> Any
+
+    """Reads the elements currently recorded."""
+
+    # Get the cache manager and wait until the file exists.
+    cache_manager = ie.current_env().get_cache_manager(self._pipeline)
+    while not cache_manager.exists('full', self._cache_key):
+      pass
+
+    # Retrieve the coder for the particular PCollection which will be used to
+    # decode elements read from cache.
+    coder = cache_manager.load_pcoder('full', self._cache_key)
+
+    # Read the elements from the cache.
+    limiters = [
+        CountLimiter(self._n), ProcessingTimeLimiter(self._duration_secs)
+    ]
+    if hasattr(cache_manager, 'read_multiple'):

Review comment:
       This looks like a hack. Is it possible to add read_multiple to CacheManager itself and FileBasedCacheManager as well?

##########
File path: sdks/python/apache_beam/runners/interactive/recording_manager.py
##########
@@ -0,0 +1,330 @@
+#
+# 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 logging
+import threading
+import time
+import warnings
+
+import apache_beam as beam
+from apache_beam.runners.interactive import background_caching_job as bcj
+from apache_beam.runners.interactive import interactive_environment as ie
+from apache_beam.runners.interactive import interactive_runner as ir
+from apache_beam.runners.interactive import pipeline_fragment as pf
+from apache_beam.runners.interactive import pipeline_instrument as pi
+from apache_beam.runners.interactive import utils
+from apache_beam.runners.interactive.options.capture_limiters import CountLimiter
+from apache_beam.runners.interactive.options.capture_limiters import ProcessingTimeLimiter
+
+_LOGGER = logging.getLogger(__name__)
+
+PipelineState = beam.runners.runner.PipelineState
+
+
+class ElementStream:
+  """A stream of elements from a given PCollection."""
+  def __init__(
+      self,
+      pcoll,  # type: beam.pvalue.PCollection
+      var,  # type: str
+      cache_key,  # type: str
+      max_n,  # type: int
+      max_duration_secs  # type: float
+      ):
+    self._pcoll = pcoll
+    self._cache_key = cache_key
+    self._pipeline = pcoll.pipeline
+    self._var = var
+    self._n = max_n
+    self._duration_secs = max_duration_secs
+
+    # A small state variable that when True, indicates that no more new elements
+    # will be yielded if read() is called again.
+    self._done = False
+
+  def var(self):
+    # type: () -> str
+
+    """Returns the variable named that defined this PCollection."""
+    return self._var
+
+  def display_id(self, suffix):
+    #Any type: (str) -> str
+
+    """Returns a unique id able to be displayed in a web browser."""
+    return utils.obfuscate(self._cache_key, suffix)
+
+  def is_computed(self):
+    # type: () -> boolean
+
+    """Returns True if no more elements will be recorded."""
+    return self._pcoll in ie.current_env().computed_pcollections
+
+  def is_done(self):
+    # type: () -> boolean
+
+    """Returns True if no more new elements will be yielded."""
+    return self._done
+
+  def read(self, tail=True):
+    # type: (boolean) -> Any
+
+    """Reads the elements currently recorded."""
+
+    # Get the cache manager and wait until the file exists.
+    cache_manager = ie.current_env().get_cache_manager(self._pipeline)
+    while not cache_manager.exists('full', self._cache_key):
+      pass

Review comment:
       This looks like busy waiting, which can hog the CPU. Can we do it another way? Or at least have a sleep in the while loop?




----------------------------------------------------------------
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:
users@infra.apache.org