jrmccluskey commented on a change in pull request #15483:
URL: https://github.com/apache/beam/pull/15483#discussion_r706222276



##########
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()

Review comment:
       This is what I consider to be the best part of the implementation. The 
list of valid tokens is kept separate from the cache itself, so clearing that 
list isn't invalidating the cached ReusableInputs. On each request we get the 
pool of valid side input tokens from the runner, so we need that check of which 
side inputs we're allowed to re-use for that call; however, we still keep all 
of the side inputs we've seen cached up to the capacity so if they become 
relevant for future bundles the cache already has them. 




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


Reply via email to