[ https://issues.apache.org/jira/browse/BEAM-5428?focusedWorklogId=301395&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-301395 ]
ASF GitHub Bot logged work on BEAM-5428: ---------------------------------------- Author: ASF GitHub Bot Created on: 26/Aug/19 17:59 Start Date: 26/Aug/19 17:59 Worklog Time Spent: 10m Work Description: rakeshcusat commented on pull request #9418: [BEAM-5428][WIP] State caching in the Python SDK URL: https://github.com/apache/beam/pull/9418#discussion_r317723117 ########## File path: sdks/python/apache_beam/runners/worker/statecache.py ########## @@ -0,0 +1,103 @@ +# +# 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. +# + +"""TODO mxm""" +import collections +from threading import Lock + +# (cache_token, value) + + +class StateCache(object): + + def __init__(self, max_entries): + self._cache = LRUCache(max_entries) + self._lock = Lock() + + def get(self, state_key, cache_tokens): + #assert isinstance(state_key, StateKey.__class__) + with self._lock: + cache_entry = self._cache.get(state_key) + if cache_entry: + token, value = cache_entry + return value if token in cache_tokens else None + else: + return None + + def put(self, state_key, cache_token, value): + #assert isinstance(state_key, StateKey.__class__) + with self._lock: + return self._cache.put(state_key, (cache_token, value)) + + def clear(self, state_key): + #assert isinstance(state_key, StateKey.__class__) + with self._lock: + self._cache.clear(state_key) + + def __len__(self): + return len(self._cache) + + +class LRUCache(object): + + def __init__(self, max_entries): + self._maxEntries = max_entries + self._cache = collections.OrderedDict() + + def get(self, key): + value = self._cache.pop(key, None) + if value: + self._cache[key] = value + return value + + def put(self, key, value): + while len(self._cache) >= self._maxEntries: + self._cache.popitem(last=False) + self._cache[key] = value + + def clear(self, key): + self._cache.pop(key, None) + + def __len__(self): + return len(self._cache) + + +class CacheStateKey(object): + + def __init__(self, transform_id, state_id, window, key): + self.transform_id = transform_id + self.state_id = state_id + self.window = window + self.key = key + + def __eq__(self, other): + return (isinstance(other, self.__class__) and Review comment: `state_key.SerializeToString()` can give you an inconsistent result on Python3. So I would avoid this method. ---------------------------------------------------------------- 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: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 301395) Time Spent: 5h (was: 4h 50m) > Implement cross-bundle state caching. > ------------------------------------- > > Key: BEAM-5428 > URL: https://issues.apache.org/jira/browse/BEAM-5428 > Project: Beam > Issue Type: Improvement > Components: sdk-py-harness > Reporter: Robert Bradshaw > Assignee: Rakesh Kumar > Priority: Major > Time Spent: 5h > Remaining Estimate: 0h > > Tech spec: > [https://docs.google.com/document/d/1BOozW0bzBuz4oHJEuZNDOHdzaV5Y56ix58Ozrqm2jFg/edit#heading=h.7ghoih5aig5m] > Relevant document: > [https://docs.google.com/document/d/1ltVqIW0XxUXI6grp17TgeyIybk3-nDF8a0-Nqw-s9mY/edit#|https://docs.google.com/document/d/1ltVqIW0XxUXI6grp17TgeyIybk3-nDF8a0-Nqw-s9mY/edit] > Mailing list link: > [https://lists.apache.org/thread.html/caa8d9bc6ca871d13de2c5e6ba07fdc76f85d26497d95d90893aa1f6@%3Cdev.beam.apache.org%3E] -- This message was sent by Atlassian Jira (v8.3.2#803003)