forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[apache#30083][prism] Factor out hold tracking to dedicated structures (
apache#31105) * [prism] Factor out hold tracking to dedicated structures * review comment-reorder move code out of ladder. --------- Co-authored-by: lostluck <13907733+lostluck@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
236 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
sdks/go/pkg/beam/runners/prism/internal/engine/holds.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
ht.counts[hold] = n | ||
return | ||
} else if n < 0 { | ||
panic(fmt.Sprintf("prism error: negative watermark hold count %v for time %v", n, hold)) | ||
} | ||
delete(ht.counts, hold) | ||
for i, h := range ht.heap { | ||
if hold == h { | ||
heap.Remove(&ht.heap, i) | ||
break | ||
} | ||
} | ||
} | ||
|
||
// Add a hold a number of times to heap. If the hold time isn't already present in the heap, it is added. | ||
func (ht *holdTracker) Add(hold mtime.Time, v int) { | ||
// Mark the hold in the heap. | ||
ht.counts[hold] = ht.counts[hold] + v | ||
|
||
if len(ht.counts) != len(ht.heap) { | ||
// Since there's a difference, the hold should not be in the heap, so we add it. | ||
heap.Push(&ht.heap, hold) | ||
} | ||
} | ||
|
||
// Min returns the earliest hold in the heap. Returns [mtime.MaxTimestamp] if the heap is empty. | ||
func (ht *holdTracker) Min() mtime.Time { | ||
minWatermarkHold := mtime.MaxTimestamp | ||
if len(ht.heap) > 0 { | ||
minWatermarkHold = ht.heap[0] | ||
} | ||
return minWatermarkHold | ||
} |
115 changes: 115 additions & 0 deletions
115
sdks/go/pkg/beam/runners/prism/internal/engine/holds_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// 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 ( | ||
"testing" | ||
|
||
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/graph/mtime" | ||
) | ||
|
||
func TestHoldTracker(t *testing.T) { | ||
|
||
type op func(*holdTracker) | ||
add := func(hold mtime.Time, count int) op { | ||
return func(ht *holdTracker) { | ||
ht.Add(hold, count) | ||
} | ||
} | ||
|
||
drop := func(hold mtime.Time, count int) op { | ||
return func(ht *holdTracker) { | ||
ht.Drop(hold, count) | ||
} | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
ops []op | ||
wantMin mtime.Time | ||
wantLen int | ||
}{ | ||
{ | ||
name: "zero-max", | ||
wantMin: mtime.MaxTimestamp, | ||
wantLen: 0, | ||
}, { | ||
|
||
name: "one-min", | ||
ops: []op{ | ||
add(mtime.MinTimestamp, 1), | ||
}, | ||
wantMin: mtime.MinTimestamp, | ||
wantLen: 1, | ||
}, { | ||
|
||
name: "cleared-max", | ||
ops: []op{ | ||
add(mtime.MinTimestamp, 1), | ||
drop(mtime.MinTimestamp, 1), | ||
}, | ||
wantMin: mtime.MaxTimestamp, | ||
wantLen: 0, | ||
}, { | ||
name: "cleared-non-eogw", | ||
ops: []op{ | ||
add(mtime.MinTimestamp, 1), | ||
add(mtime.EndOfGlobalWindowTime, 1), | ||
drop(mtime.MinTimestamp, 1), | ||
}, | ||
wantMin: mtime.EndOfGlobalWindowTime, | ||
wantLen: 1, | ||
}, { | ||
name: "uncleared-non-min", | ||
ops: []op{ | ||
add(mtime.MinTimestamp, 2), | ||
add(mtime.EndOfGlobalWindowTime, 1), | ||
drop(mtime.MinTimestamp, 1), | ||
}, | ||
wantMin: mtime.MinTimestamp, | ||
wantLen: 2, | ||
}, { | ||
name: "uncleared-non-min", | ||
ops: []op{ | ||
add(1, 1), | ||
add(2, 1), | ||
add(3, 1), | ||
drop(2, 1), | ||
add(4, 1), | ||
add(3, 1), | ||
drop(1, 1), | ||
add(2, 1), | ||
drop(4, 1), | ||
}, | ||
wantMin: 2, | ||
wantLen: 2, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
tracker := newHoldTracker() | ||
for _, op := range test.ops { | ||
op(tracker) | ||
} | ||
if got, want := tracker.Min(), test.wantMin; got != want { | ||
t.Errorf("tracker.heap.Min() = %v, want %v", got, want) | ||
} | ||
if got, want := tracker.heap.Len(), test.wantLen; got != want { | ||
t.Errorf("tracker.heap.Len() = %v, want %v", got, want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters