lostluck commented on code in PR #26101: URL: https://github.com/apache/beam/pull/26101#discussion_r1174021972
########## sdks/go/examples/timer_wordcap/wordcap.go: ########## @@ -0,0 +1,227 @@ +// 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. + +// timer_wordcap is a toy streaming pipeline that uses State and Timers with PubSub. It +// does the following: +// +// (1) create a topic and publish a few messages to it +// (2) Set user state and timer +// +// NOTE: it only runs on Dataflow and must be manually cancelled. +package main + +import ( + "context" + "flag" + "fmt" + "os" + "time" + + "github.com/apache/beam/sdks/v2/go/pkg/beam" + "github.com/apache/beam/sdks/v2/go/pkg/beam/core/graph/mtime" + "github.com/apache/beam/sdks/v2/go/pkg/beam/core/sdf" + "github.com/apache/beam/sdks/v2/go/pkg/beam/core/state" + "github.com/apache/beam/sdks/v2/go/pkg/beam/core/timers" + "github.com/apache/beam/sdks/v2/go/pkg/beam/io/rtrackers/offsetrange" + "github.com/apache/beam/sdks/v2/go/pkg/beam/log" + "github.com/apache/beam/sdks/v2/go/pkg/beam/register" + "github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx" + "github.com/apache/beam/sdks/v2/go/pkg/beam/x/debug" + "golang.org/x/exp/slog" +) + +var ( + input = flag.String("input", os.ExpandEnv("$USER-wordcap"), "Pubsub input topic.") +) + +var ( + data = []string{ + "foo", + "bar", + "baz", + } +) + +type Stateful struct { + ElementBag state.Bag[string] + TimerTime state.Value[int64] + MinTime state.Combining[int64, int64, int64] + + OutputState timers.ProcessingTime +} + +func NewStateful() *Stateful { + return &Stateful{ + ElementBag: state.MakeBagState[string]("elementBag"), + TimerTime: state.MakeValueState[int64]("timerTime"), + MinTime: state.MakeCombiningState[int64, int64, int64]("minTiInBag", func(a, b int64) int64 { + if a < b { + return a + } + return b + }), + + OutputState: timers.InProcessingTime("outputState"), + } +} + +func (s *Stateful) OnTimer(ctx context.Context, ts beam.EventTime, tp timers.Provider, key, timerKey, timerTag string) { Review Comment: Thanks for your interest! The design doc is a little out of date, and was authored with a less than complete understanding of timers. The Beam Model doc is correct and any values would need to be carried over via the state API. The FnAPI simply doesn't have a provision for the additional data. Timers and State are implicitly per key, but timers can have an additional "tag" associated with them for an additional uniqueness factor. -------- We definitely do need to have emitters working (which would always match the ProcessElement emitters exactly), and it's probably simply just not implemented in this WIP PR. Adding new "lifecycle" methods isn't simple. I believe that GBKs *should* be able to have a timer callback and associated state it's largely just a validation thing on the SDK that needs updating. -- 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]
