hnnsgstfssn commented on code in PR #26101: URL: https://github.com/apache/beam/pull/26101#discussion_r1172506840
########## 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: This example only includes the key. If I want to access the value in `OnTimer`, will I have to pass it through the state API, or possibly as a timer tag if it can be made a `string`? I see that [the design](https://docs.google.com/document/d/1rcKa1Z6orDDFr1l8t6NA1eLl6zanQbYAEiAqk39NQUU/) says a value should be mapped, but [this](https://beam.apache.org/documentation/basics/#state-and-timers) says the interaction is through state API. I have not been able to add an input value, getting some variant of ``` panic: reflect: Call using zero Value argument goroutine 1472 [running]: runtime/debug.Stack() /usr/lib/go/src/runtime/debug/stack.go:24 +0x65 github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/exec.callNoPanic.func1() /home/rru/go/pkg/mod/github.com/riteshghorse/beam/sdks/[email protected]/go/pkg/beam/core/runtime/exec/util.go:58 +0xa5 panic({0x1d9c940, 0xc000c39260}) /usr/lib/go/src/runtime/panic.go:884 +0x213 reflect.Value.call({0x2016b60?, 0xc000a4d480?, 0x44f6d2?}, {0x28561b5, 0x4}, {0xc000a89970, 0x7, 0x5ddc11?}) /usr/lib/go/src/reflect/value.go:437 +0x1aee reflect.Value.Call({0x2016b60?, 0xc000a4d480?, 0x40e427?}, {0xc000a89970?, 0x24e75a0?, 0xc0006e6801?}) /usr/lib/go/src/reflect/value.go:370 +0xbc github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/reflectx.(*reflectFunc).Call(0xc000a86ed0, {0xc000931340?, 0xc0006e6988?, 0xa6660f?}) /home/rru/go/pkg/mod/github.com/riteshghorse/beam/sdks/[email protected]/go/pkg/beam/core/util/reflectx/call.go:87 +0x59 github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/exec.(*invoker).initCall.func46({0x0, 0x0, 0x0, 0x0, 0x0}, {0x3ea9da0, 0x1, 0x1}, 0xf?) /home/rru/go/pkg/mod/github.com/riteshghorse/beam/sdks/[email protected]/go/pkg/beam/core/runtime/exec/fn_arity.go:307 +0x94 github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/exec.(*invoker).invokeWithOpts(0xc000b6a780, {0x2b630a8?, 0xc000a4ccc0}, {0x0, 0x0, 0x0, 0x0, 0x0}, {0x3ea9da0, 0x1, ...}, ...) ``` Similarly I have not been able to add any emitter, getting similar errors as per above. Another issue I had was that it failed to validate when the `beam.EventTime` was placed after the `timers.Provider`. It says then that it needs to be before the main input. Moving it into the same position as here seems to work. Finally I was unable to add timers to a GBK DoFn, having it complain about needing to use KV with timers, but I need to investigate further on this one. Maybe it is not supposed to be possible to use timers in such cases? -- 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]
