-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eventagg: add FlushTrigger mechanism to MapReduceAggregator
This patch introduces the FlushTrigger interface, which can be used by the MapReduceAggregator to determine when it's time to flush the current aggregation. Along with the interface, an initial implementation is provided called `WindowedFlush`. `WindowedFlush` aligns event aggregations to truncated time intervals given a user-provided time window. For example, if a window of 5 minutes was given, the `WindowedFlush` would enforce the following window boundaries: - [12:00:00, 12:05:00) - [12:05:00, 12:10:00) - [12:10:00, 12:15:00) - etc. This is a first pass implementation of the flush mechanism used in the eventagg package. As needs evolve, the interface and/or implementation is subject to change. For the purposed of prototyping though, this meets our needs. Release note: none
- Loading branch information
1 parent
fe8d17d
commit fdabacc
Showing
7 changed files
with
206 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package eventagg | ||
|
||
import "time" | ||
|
||
// FlushTrigger defines the interface used by aggregators, such as MapReduceAggregator, | ||
// to determine when a flush of the current aggregation should be triggered. | ||
// | ||
// FlushTriggers are generally invoked as soon as events are consumed by an aggregator, | ||
// but *before* the event itself is aggregated. | ||
type FlushTrigger interface { | ||
// shouldFlush returns true if this FlushTrigger has been tripped. | ||
shouldFlush() bool | ||
} | ||
|
||
// WindowedFlush is a FlushTrigger which triggers flushes on a wall clock aligned | ||
// time window. | ||
// | ||
// On initialization, a window duration is provided. Each time window will be a time | ||
// truncated to that time window. | ||
// | ||
// For example, if we provide a window of 5 minutes, the windows will be: | ||
// - [12:00:00, 12:05:00) | ||
// - [12:05:00, 12:10:00) | ||
// - [12:10:00, 12:15:00) | ||
// - etc. | ||
type WindowedFlush struct { | ||
window time.Duration | ||
curWindowEnd time.Time | ||
nowFn func() time.Time | ||
} | ||
|
||
var _ FlushTrigger = (*WindowedFlush)(nil) | ||
|
||
// NewWindowedFlush returns a new WindowedFlush for the provided window. | ||
func NewWindowedFlush(window time.Duration, nowFn func() time.Time) *WindowedFlush { | ||
w := &WindowedFlush{ | ||
window: window, | ||
nowFn: nowFn, | ||
} | ||
w.curWindowEnd = w.newWindowEnd() | ||
return w | ||
} | ||
|
||
func (w *WindowedFlush) newWindowEnd() time.Time { | ||
return w.nowFn().Truncate(w.window).Add(w.window) | ||
} | ||
|
||
func (w *WindowedFlush) shouldFlush() bool { | ||
t := w.nowFn() | ||
if t.Equal(w.curWindowEnd) || t.After(w.curWindowEnd) { | ||
w.curWindowEnd = w.newWindowEnd() | ||
return true | ||
} | ||
return false | ||
} |
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,40 @@ | ||
// Copyright 2024 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package eventagg | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/util/timeutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWindowedFlush(t *testing.T) { | ||
window := 5 * time.Minute | ||
now := timeutil.Now() | ||
// Make sure we're not directly on the window boundary. | ||
if now.Truncate(window).Equal(now) { | ||
now = now.Add(1 * time.Minute) | ||
} | ||
flush := NewWindowedFlush(window, func() time.Time { | ||
return now | ||
}) | ||
// Initially, we should be within the current window. | ||
require.False(t, flush.shouldFlush()) | ||
// If we fast-forward to the beginning of the next window, we expect | ||
// a flush to be triggered. | ||
now = now.Add(window).Truncate(window) | ||
require.True(t, flush.shouldFlush()) | ||
// Times occurring before the current window's end should not trigger a flush. | ||
now = now.Add(1 * time.Minute) | ||
require.False(t, flush.shouldFlush()) | ||
} |
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
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
Oops, something went wrong.