beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Work logged] (BEAM-5264) Reference DirectRunner implementation of Python user state and timers API
Date Mon, 03 Sep 2018 15:07:10 GMT

     [ https://issues.apache.org/jira/browse/BEAM-5264?focusedWorklogId=140599&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-140599
]

ASF GitHub Bot logged work on BEAM-5264:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 03/Sep/18 15:06
            Start Date: 03/Sep/18 15:06
    Worklog Time Spent: 10m 
      Work Description: robertwb commented on a change in pull request #6304: [BEAM-5264]
Reference DirectRunner implementation of Python User State and Timers API
URL: https://github.com/apache/beam/pull/6304#discussion_r214710876
 
 

 ##########
 File path: sdks/python/apache_beam/runners/direct/direct_userstate.py
 ##########
 @@ -0,0 +1,115 @@
+#
+# 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.
+#
+
+"""Support for user state in the BundleBasedDirectRunner."""
+from __future__ import absolute_import
+
+from apache_beam.transforms.trigger import _ListStateTag
+from apache_beam.transforms.userstate import BagStateSpec
+from apache_beam.transforms.userstate import CombiningValueStateSpec
+from apache_beam.transforms.userstate import RuntimeState
+from apache_beam.transforms.userstate import RuntimeTimer
+from apache_beam.transforms.userstate import UserStateContext
+from apache_beam.transforms.userstate import UserStateUtils
+
+
+class DirectUserStateContext(UserStateContext):
+  """UserStateContext for the BundleBasedDirectRunner.
+
+  The DirectUserStateContext buffers up updates that are to be committed by the
+  TransformEvaluator after running a DoFn.
+  """
+
+  def __init__(self, step_context, dofn, key_coder):
+    self.step_context = step_context
+    self.dofn = dofn
+    self.key_coder = key_coder
+
+    self.all_state_specs, self.all_timer_specs = (
+        UserStateUtils.get_dofn_specs(dofn))
+    self.state_tags = {}
+    for state_spec in self.all_state_specs:
+      state_key = 'user/%s' % state_spec.name
+      if isinstance(state_spec, BagStateSpec):
+        state_tag = _ListStateTag(state_key)
+      elif isinstance(state_spec, CombiningValueStateSpec):
+        state_tag = _ListStateTag(state_key)
+      else:
+        raise ValueError('Invalid state spec: %s' % state_spec)
+      self.state_tags[state_spec] = state_tag
+
+    self.cached_states = {}
+    self.cached_timers = {}
+
+  def get_timer(self, timer_spec, key, window):
+    assert timer_spec in self.all_timer_specs
+    encoded_key = self.key_coder.encode(key)
+    cache_key = (encoded_key, window, timer_spec)
+    if cache_key not in self.cached_timers:
+      self.cached_timers[cache_key] = RuntimeTimer(timer_spec)
+    return self.cached_timers[cache_key]
+
+  def get_state(self, state_spec, key, window):
+    assert state_spec in self.all_state_specs
+    encoded_key = self.key_coder.encode(key)
+    cache_key = (encoded_key, window, state_spec)
+    if cache_key not in self.cached_states:
+      state_tag = self.state_tags[state_spec]
+      value_accessor = (
+          lambda: self._get_underlying_state(state_spec, key, window))
+      self.cached_states[cache_key] = (
+          RuntimeState.for_spec(state_spec, state_tag, value_accessor))
+    return self.cached_states[cache_key]
+
+  def _get_underlying_state(self, state_spec, key, window):
+    state_tag = self.state_tags[state_spec]
+    encoded_key = self.key_coder.encode(key)
+    return (self.step_context.get_keyed_state(encoded_key)
+            .get_state(window, state_tag))
+
+  def commit(self):
+    # Commit state modifications.
+    for cache_key, runtime_state in self.cached_states.items():
+      encoded_key, window, state_spec = cache_key
+      state = self.step_context.get_keyed_state(encoded_key)
+      state_tag = self.state_tags[state_spec]
+      if isinstance(state_spec, BagStateSpec):
+        if runtime_state._cleared:
+          state.clear_state(window, state_tag)
+        for new_value in runtime_state._new_values:
+          state.add_state(window, state_tag, new_value)
+      elif isinstance(state_spec, CombiningValueStateSpec):
+        if runtime_state._modified:
+          state.clear_state(window, state_tag)
+          state.add_state(
+              window, state_tag,
+              state_spec.coder.encode(runtime_state._current_accumulator))
+      else:
+        raise ValueError('Invalid state spec: %s' % state_spec)
+
+    # Commit new timers.
+    for cache_key, runtime_timer in self.cached_timers.items():
+      encoded_key, window, timer_spec = cache_key
+      state = self.step_context.get_keyed_state(encoded_key)
+      timer_name = 'user/%s' % timer_spec.name
 
 Review comment:
   Why is this prefix applied to state at `__init__` but timers at `commit`?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


Issue Time Tracking
-------------------

    Worklog Id:     (was: 140599)

> Reference DirectRunner implementation of Python user state and timers API
> -------------------------------------------------------------------------
>
>                 Key: BEAM-5264
>                 URL: https://issues.apache.org/jira/browse/BEAM-5264
>             Project: Beam
>          Issue Type: Improvement
>          Components: sdk-py-core
>    Affects Versions: 2.6.0
>            Reporter: Charles Chen
>            Assignee: Charles Chen
>            Priority: Major
>          Time Spent: 1.5h
>  Remaining Estimate: 0h
>
> This issue tracks the reference DirectRunner implementation of the Beam Python User
State and Timer API, described here: [https://s.apache.org/beam-python-user-state-and-timers].



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message