-
Notifications
You must be signed in to change notification settings - Fork 153
/
trigger.go
174 lines (153 loc) · 4.07 KB
/
trigger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package execute
import (
"fmt"
"github.com/influxdata/flux"
"github.com/influxdata/flux/plan"
)
type Trigger interface {
Triggered(TriggerContext) bool
Finished() bool
Reset()
}
type TriggerContext struct {
Table TableContext
Watermark Time
CurrentProcessingTime Time
}
type TableContext struct {
Key flux.GroupKey
Count int
}
func NewTriggerFromSpec(spec plan.TriggerSpec) Trigger {
switch s := spec.(type) {
case plan.NarrowTransformationTriggerSpec:
return &narrowTransformationTrigger{}
case plan.AfterWatermarkTriggerSpec:
return &afterWatermarkTrigger{
allowedLateness: Duration(s.AllowedLateness),
}
case plan.RepeatedTriggerSpec:
return &repeatedlyForever{
t: NewTriggerFromSpec(s.Trigger),
}
case plan.AfterProcessingTimeTriggerSpec:
return &afterProcessingTimeTrigger{
duration: Duration(s.Duration),
}
case plan.AfterAtLeastCountTriggerSpec:
return &afterAtLeastCount{
atLeast: s.Count,
}
case plan.OrFinallyTriggerSpec:
return &orFinally{
main: NewTriggerFromSpec(s.Main),
finally: NewTriggerFromSpec(s.Finally),
}
default:
//TODO(nathanielc): Add proper error handling here.
// Maybe separate validation of a spec and creation of a spec so we know we cannot error during creation?
panic(fmt.Sprintf("unsupported trigger spec provided %T", spec))
}
}
// Informally a narrow transformation is one where each output table originates
// from a single input table. Once an input table is processed, the resulting
// output table may be sent downstream immediately. The trigger associated with
// these kinds of transformations fires immediately as well as finishes
// immediately. This behavior ensures there is at most one table in the
// transformation's data cache at any given time.
type narrowTransformationTrigger struct{}
func (t *narrowTransformationTrigger) Triggered(c TriggerContext) bool {
return true
}
func (t *narrowTransformationTrigger) Finished() bool {
return true
}
func (t *narrowTransformationTrigger) Reset() {}
// afterWatermarkTrigger triggers once the watermark is greater than the bounds of the block.
type afterWatermarkTrigger struct {
allowedLateness Duration
finished bool
}
func (t *afterWatermarkTrigger) Triggered(c TriggerContext) bool {
timeIdx := ColIdx(DefaultStopColLabel, c.Table.Key.Cols())
if timeIdx < 0 {
return false
}
stop := c.Table.Key.ValueTime(timeIdx)
if c.Watermark >= stop.Add(t.allowedLateness) {
t.finished = true
}
return c.Watermark >= stop
}
func (t *afterWatermarkTrigger) Finished() bool {
return t.finished
}
func (t *afterWatermarkTrigger) Reset() {
t.finished = false
}
type repeatedlyForever struct {
t Trigger
}
func (t *repeatedlyForever) Triggered(c TriggerContext) bool {
return t.t.Triggered(c)
}
func (t *repeatedlyForever) Finished() bool {
if t.t.Finished() {
t.Reset()
}
return false
}
func (t *repeatedlyForever) Reset() {
t.t.Reset()
}
type afterProcessingTimeTrigger struct {
duration Duration
triggerTimeSet bool
triggerTime Time
current Time
}
func (t *afterProcessingTimeTrigger) Triggered(c TriggerContext) bool {
if !t.triggerTimeSet {
t.triggerTimeSet = true
t.triggerTime = c.CurrentProcessingTime.Add(t.duration)
}
t.current = c.CurrentProcessingTime
return t.current >= t.triggerTime
}
func (t *afterProcessingTimeTrigger) Finished() bool {
return t.triggerTimeSet && t.current >= t.triggerTime
}
func (t *afterProcessingTimeTrigger) Reset() {
t.triggerTimeSet = false
}
type afterAtLeastCount struct {
n, atLeast int
}
func (t *afterAtLeastCount) Triggered(c TriggerContext) bool {
t.n = c.Table.Count
return t.n >= t.atLeast
}
func (t *afterAtLeastCount) Finished() bool {
return t.n >= t.atLeast
}
func (t *afterAtLeastCount) Reset() {
t.n = 0
}
type orFinally struct {
main Trigger
finally Trigger
finished bool
}
func (t *orFinally) Triggered(c TriggerContext) bool {
if t.finally.Triggered(c) {
t.finished = true
return true
}
return t.main.Triggered(c)
}
func (t *orFinally) Finished() bool {
return t.finished
}
func (t *orFinally) Reset() {
t.finished = false
}