lostluck commented on code in PR #31105: URL: https://github.com/apache/beam/pull/31105#discussion_r1581491967
########## sdks/go/pkg/beam/runners/prism/internal/engine/holds.go: ########## @@ -0,0 +1,105 @@ +// 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 engine + +import ( + "container/heap" + "fmt" + + "github.com/apache/beam/sdks/v2/go/pkg/beam/core/graph/mtime" +) + +// holdHeap orders holds based on their timestamps +// so we can always find the minimum timestamp of pending holds. +type holdHeap []mtime.Time + +func (h holdHeap) Len() int { return len(h) } +func (h holdHeap) Less(i, j int) bool { return h[i] < h[j] } +func (h holdHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } + +func (h *holdHeap) Push(x any) { + // Push and Pop use pointer receivers because they modify the slice's length, + // not just its contents. + *h = append(*h, x.(mtime.Time)) +} + +func (h *holdHeap) Pop() any { + old := *h + n := len(old) + x := old[n-1] + *h = old[0 : n-1] + return x +} + +// holdTracker track the watermark holds for a stage. +// +// Timers hold back the watermark until they fire, but multiple +// timers may set the same watermark hold. +// To track when the watermark may advance further this structure maintains +// counts for each set watermark hold. +// As timers are processed, their associated holds are removed, reducing the counts. +// +// A heap of the hold times is kept so we have quick access to the minimum hold, for calculating +// how to advance the watermark. +type holdTracker struct { + heap holdHeap + counts map[mtime.Time]int +} + +func newHoldTracker() *holdTracker { + return &holdTracker{ + counts: map[mtime.Time]int{}, + } +} + +// Drop the given hold count. When the count of a hold time reaches zero, it's +// removed from the heap. Drop panics if holds become negative. +func (ht *holdTracker) Drop(hold mtime.Time, v int) { + n := ht.counts[hold] - v + if n == 0 { + delete(ht.counts, hold) + for i, h := range ht.heap { + if hold == h { + heap.Remove(&ht.heap, i) + break + } + } + } else if n > 0 { + ht.counts[hold] = n + } else { + panic(fmt.Sprintf("prism error: negative watermark hold count %v for time %v", n, hold)) + } Review Comment: Sort of, but that wouldn't be the same. 1. That only assumes that the value is 0 before dropping. The current code is checking if it's negative after dropping. The current code catches additional errors. 2. The current code also reduces the number of lookups that occur, and sets that occur. That said, I will reduce the if-else ladder somewhat. -- 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]
