lostluck commented on code in PR #29829: URL: https://github.com/apache/beam/pull/29829#discussion_r1437844025
########## sdks/go/test/integration/primitives/timers.go: ########## @@ -0,0 +1,133 @@ +// 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 primitives + +import ( + "context" + "strconv" + + "github.com/apache/beam/sdks/v2/go/pkg/beam" + "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/register" + "github.com/apache/beam/sdks/v2/go/pkg/beam/testing/passert" +) + +// Based on https://github.com/apache/beam/blob/master/runners/flink/src/test/java/org/apache/beam/runners/flink/PortableTimersExecutionTest.java + +func init() { + register.DoFn2x0[[]byte, func(string, int)](&inputFn[string, int]{}) + register.DoFn6x0[beam.Window, state.Provider, timers.Provider, string, int, func(kv[string, int])](&eventTimeFn{}) + register.Emitter2[string, int]() + register.Emitter1[kv[string, int]]() +} + +type kv[K, V any] struct { + Key K + Value V +} + +func kvfn[K, V any](k K, v V) kv[K, V] { + return kv[K, V]{k, v} +} + +type inputFn[K, V any] struct { + Inputs []kv[K, V] +} + +func (fn *inputFn[K, V]) ProcessElement(_ []byte, emit func(K, V)) { + for _, in := range fn.Inputs { + emit(in.Key, in.Value) + } +} + +type eventTimeFn struct { + Callback timers.EventTime + MyKey state.Value[string] + + Offset int + TimerOutput int +} + +func (fn *eventTimeFn) ProcessElement(w beam.Window, sp state.Provider, tp timers.Provider, key string, value int, emit func(kv[string, int])) { + fn.Callback.Set(tp, w.MaxTimestamp().ToTime()) + fn.MyKey.Write(sp, key) + emit(kvfn(key, value+fn.Offset)) +} + +func (fn *eventTimeFn) OnTimer(ctx context.Context, ts beam.EventTime, sp state.Provider, tp timers.Provider, key string, timer timers.Context, emit func(kv[string, int])) { + switch timer.Family { + case fn.Callback.Family: + switch timer.Tag { + case "": + read, ok, err := fn.MyKey.Read(sp) + if err != nil { + panic(err) + } + if !ok { + panic("State must be set.") + } + emit(kvfn(read, fn.TimerOutput)) + } + default: + if fn.Callback.Family != timer.Family || timer.Tag != "" { + panic("unexpected timer family: " + timer.Family + " tag:" + timer.Tag + " want: " + fn.Callback.Family) + } + } +} + +// TimersEventTime takes in an impulse transform and then validates +// event time timer execution. +// +// The impulse is provided outside to swap between a bounded impulse, and +// an unbounded one, because the Go SDK uses that to determine if a pipeline +// is "streaming" or not. This matters at least for executions on Dataflow. +// +// Regardless,the pipelines should pass. Review Comment: 1. Validator is actually constructing the pipeline. It's not validating a pipeline. The validation has to be built in. It's not clear what benefit the additional indirection through a type (which is still exposed as a `func(s beam.Scope)` anyway serves here, and it's not going to be obvious a "Validator" is actually the same as expected by `ptest.BuildAndRun`, even though the compiler is happy with it. 2. I agree that the current TimersEventTime builder function isn't ideal. I'm going to unexport it, and simply move the full pipeline builds "internally", so it's not exposed. -- 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]
