[
https://issues.apache.org/jira/browse/BEAM-11097?focusedWorklogId=649239&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-649239
]
ASF GitHub Bot logged work on BEAM-11097:
-----------------------------------------
Author: ASF GitHub Bot
Created on: 10/Sep/21 14:35
Start Date: 10/Sep/21 14:35
Worklog Time Spent: 10m
Work Description: jrmccluskey commented on a change in pull request
#15483:
URL: https://github.com/apache/beam/pull/15483#discussion_r706234335
##########
File path: sdks/go/pkg/beam/core/runtime/harness/statecache/statecache.go
##########
@@ -0,0 +1,172 @@
+// 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.
+
+// Package statecache implements the state caching feature described by the
+// Beam Fn API
+package statecache
+
+import (
+ "sync"
+
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/exec"
+ "github.com/apache/beam/sdks/v2/go/pkg/beam/internal/errors"
+ fnpb "github.com/apache/beam/sdks/v2/go/pkg/beam/model/fnexecution_v1"
+)
+
+// SideInputCache stores a cache of reusable inputs for the purposes of
+// eliminating redundant calls to the runner during execution of ParDos
+// using side inputs.
+type SideInputCache struct {
+ cache map[string]*exec.ReusableInput
+ idsToTokens map[string]string
+ validTokens []string
+ mu sync.Mutex
+ capacity int
+}
+
+// Init makes the cache map and the map of IDs to cache tokens for the
+// SideInputCache. Should only be called once.
+func (c *SideInputCache) Init(cap int) error {
+ if cap <= 0 {
+ return errors.Errorf("capacity must be a positive integer, got
%v", cap)
+ }
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ c.cache = make(map[string]*exec.ReusableInput, cap)
+ c.idsToTokens = make(map[string]string)
+ return nil
+}
+
+// Completely clears the list of valid tokens. Should be called when
+// starting to handle a new request.
+func (c *SideInputCache) clearValidTokens() {
+ c.validTokens = nil
+}
+
+// SetValidTokens clears the list of valid tokens then sets new ones, also
updating the mapping of
+// transform and side input IDs to cache tokens in the process. Should be
called at the start of every
+// new ProcessBundleRequest. If the runner does not support caching, the
passed cache token values
+// should be empty and all get/set requests will silently be no-ops.
+func (c *SideInputCache) SetValidTokens(cacheTokens
...fnpb.ProcessBundleRequest_CacheToken) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ c.clearValidTokens()
+ for _, tok := range cacheTokens {
+ // User State caching is currently not supported, so these
tokens are ignored
+ if tok.GetUserState() != nil {
+ continue
+ } else {
+ s := tok.GetSideInput()
+ transformID := s.GetTransformId()
+ sideInputID := s.GetSideInputId()
+ token := string(tok.GetToken())
+ c.setValidToken(transformID, sideInputID, token)
+ }
+ }
+}
+
+// setValidToken adds a new valid token for a request into the SideInputCache
struct
+// and maps the transform ID and side input ID pairing to the cache token.
+func (c *SideInputCache) setValidToken(transformID, sideInputID, token string)
{
+ idKey := transformID + sideInputID
+ c.idsToTokens[idKey] = token
+ c.validTokens = append(c.validTokens, token)
+}
+
+func (c *SideInputCache) makeAndValidateToken(transformID, sideInputID string)
(string, bool) {
+ idKey := transformID + sideInputID
+ // Check if it's a known token
+ tok, ok := c.idsToTokens[idKey]
+ if !ok {
+ return "", false
+ }
+ // Check if the known token is valid for this request
+ for _, t := range c.validTokens {
+ if t == tok {
+ return tok, true
+ }
+ }
+ return "", false
+}
+
+// QueryCache takes a transform ID and side input ID and checking if a
corresponding side
+// input has been cached. A query having a bad token (e.g. one that doesn't
make a known
+// token or one that makes a known but currently invalid token) is treated the
same as a
+// cache miss.
+func (c *SideInputCache) QueryCache(transformID, sideInputID string)
*exec.ReusableInput {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ tok, ok := c.makeAndValidateToken(transformID, sideInputID)
+ if !ok {
+ return nil
+ }
+ // Check to see if cached
+ input, ok := c.cache[tok]
+ if !ok {
+ return nil
+ }
+ return input
+}
+
+// SetCache allows a user to place a ReusableInput materialized from the
reader into the SideInputCache
+// with its corresponding transform ID and side input ID. If the IDs do not
pair with a known, valid token
+// then we silently do not cache the input, as this is an indication that the
runner is treating that input
+// as uncacheable.
+func (c *SideInputCache) SetCache(transformID, sideInputID string, input
*exec.ReusableInput) error {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ tok, ok := c.makeAndValidateToken(transformID, sideInputID)
+ if !ok {
+ return nil
+ }
+ if len(c.cache) > c.capacity {
+ err := c.evictElement()
+ if err != nil {
+ return errors.Errorf("Cache at or above capacity, got
%v", err)
+ }
+ }
+ c.cache[tok] = input
+ return nil
+}
+
+func (c *SideInputCache) isValid(token string) bool {
+ for _, t := range c.validTokens {
+ if t == token {
+ return true
+ }
+ }
+ return false
+}
+
+// evictElement randomly evicts a ReusableInput that is not currently valid
from the cache.
+// It should only be called by a goroutine that obtained the lock in SetCache.
+func (c *SideInputCache) evictElement() error {
+ deleted := false
+ // Select a key from the cache at random
+ for k := range c.cache {
+ // Do not evict an element if it's currently valid
+ if !c.isValid(k) {
+ delete(c.cache, k)
+ deleted = true
+ break
+ }
+ }
+ // Nothing is deleted if every side input is still valid, meaning that
the cache size
+ // is likely too small.
+ if !deleted {
Review comment:
I do like the idea of moving to populating metrics for this instead of
throwing an error.
--
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]
Issue Time Tracking
-------------------
Worklog Id: (was: 649239)
Time Spent: 2h 10m (was: 2h)
> [Go SDK] Windowed Side Input Caching (Cross Bundle)
> ---------------------------------------------------
>
> Key: BEAM-11097
> URL: https://issues.apache.org/jira/browse/BEAM-11097
> Project: Beam
> Issue Type: Improvement
> Components: sdk-go
> Reporter: Robert Burke
> Assignee: Jack McCluskey
> Priority: P3
> Labels: starter
> Time Spent: 2h 10m
> Remaining Estimate: 0h
>
> This is implementing https://issues.apache.org/jira/browse/BEAM-5428 for the
> Go SDK.
> Side inputs are valid for given window. That means they can be and correctly
> cached and re-used in multiple bundles if the data has already been loaded
> into a given worker.
> Side input data for a given bundle is specified and keyed by the window and a
> a side input request token. The Go SDK presently doesn't use this information
> for a SDK side worker cache of data.
> Care needs to be taken to allow the data to be garbage collected if it hasn't
> been used in the last minute or so. In practice, Global Window side inputs
> will not be evicted while still in use, but infrequently accessed data will
> possibly be evicted more regularly. A limit should be set to avoid running
> out of memory.
> As presently implemented, each ProcessElement call (or StartBundle or
> FinishBundle) will only query for side input data on user request. However,
> results of user requests are not cached between elements or bundles, which
> means there's additional lookup and serialization time from the runner side.
> It may be possible to re-use allocated side input elements, but even a
> solution that re-decodes data on every call would be an improvement if the
> data isn't re-streamed from the runner.
> If https://issues.apache.org/jira/browse/BEAM-3293 has been implemented, then
> per-key+window caching should additionally be considered for more granular
> caching of data.
> This optimization has an outsized impact on streaming performance as side
> input data can change per bundle, and streaming bundles are typically small,
> and quickly processed.
> This work would be implemented in the exec package:
>
> [https://github.com/apache/beam/blob/master/sdks/go/pkg/beam/core/runtime/exec/sideinput.go]
>
> and probably the harness package
>
> [https://github.com/apache/beam/blob/c185adb7652034b5d044aae65a1a972d9ceb4377/sdks/go/pkg/beam/core/runtime/harness/statemgr.go#L50]
--
This message was sent by Atlassian Jira
(v8.3.4#803005)