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 2021/11/05 17:54:31 UTC

[GitHub] [beam] lostluck commented on a change in pull request #15896: [BEAM-11097] Create hook to enable cross-bundle side input caching

lostluck commented on a change in pull request #15896:
URL: https://github.com/apache/beam/pull/15896#discussion_r743075662



##########
File path: sdks/go/pkg/beam/core/runtime/harness/cache_hooks.go
##########
@@ -0,0 +1,53 @@
+// 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 harness
+
+import (
+	"context"
+	"fmt"
+
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/hooks"
+)
+
+type cacheHookKey string
+
+const (
+	cacheSizeKey cacheHookKey = "sideCacheSize"
+)
+
+func init() {
+	hf := func(opts []string) hooks.Hook {
+		return hooks.Hook{
+			Init: func(ctx context.Context) (context.Context, error) {
+				if len(opts) == 0 {
+					return context.WithValue(ctx, cacheSizeKey, 0), nil
+				}
+				if len(opts) > 1 {
+					return ctx, fmt.Errorf("expected 1 option, got %v: %v", len(opts), opts)
+				}
+
+				var count int
+				_, err := fmt.Sscan(opts[0], &count)
+				if err != nil {
+					return nil, err
+				}
+
+				return context.WithValue(ctx, cacheSizeKey, count), nil

Review comment:
       Per https://pkg.go.dev/context
   
   > Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions.
   
   In this case, it's much more preferable to have an unexported Package level variable that this hook sets, and it used, rather than propagating via context.  It would be much simpler to follow: we're always using the package level field. The default value is quite evident. 
   
   The remote logging hook needed to cheat using a context value because of the dependency reversal. (the remote logging hook is in the harness/init package which calls into harness.Main(), and there's no good other good way to propagate the parameters sensibly...). For a one off, it's fine, but we probably should consider re-factoring how the remote logging is handled, to be closer to what's happening with the state cache.
   
   In this case, everything is in the harness package, so we can avoid adding values to live in the context forever.

##########
File path: sdks/go/pkg/beam/core/runtime/harness/statecache/statecache.go
##########
@@ -86,6 +92,9 @@ func (c *SideInputCache) Init(cap int) error {
 func (c *SideInputCache) SetValidTokens(cacheTokens ...*fnpb.ProcessBundleRequest_CacheToken) {
 	c.mu.Lock()
 	defer c.mu.Unlock()
+	if !c.enabled {
+		return
+	}

Review comment:
       Since `enabled` should only be written in `Init()`, and `Init()` is only written to once, I'd be OK with putting these checks before the locks.




-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org