-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
timers.go
170 lines (143 loc) · 5.11 KB
/
timers.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
// 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 timers contains structs for setting pipeline timers.
package timers
import (
"reflect"
"time"
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/graph/mtime"
)
var (
// ProviderType represents the type of timer provider.
ProviderType = reflect.TypeOf((*Provider)(nil)).Elem()
)
// TimeDomain represents different time domains to set timer.
type TimeDomain int32
const (
// UnspecifiedTimeDomain represents unspecified time domain.
UnspecifiedTimeDomain TimeDomain = 0
// EventTimeDomain is time from the perspective of the data
EventTimeDomain TimeDomain = 1
// ProcessingTimeDomain is time from the perspective of the
// execution of your pipeline
ProcessingTimeDomain TimeDomain = 2
)
// TimerMap holds timer information obtained from the pipeline.
//
// For SDK internal use, and subject to change.
type TimerMap struct {
Family string
Tag string
Clear bool
FireTimestamp, HoldTimestamp mtime.Time
}
type timerConfig struct {
Tag string
HoldTimestamp mtime.Time
}
type timerOptions func(*timerConfig)
// WithTag sets the tag for the timer.
func WithTag(tag string) timerOptions {
return func(tm *timerConfig) {
tm.Tag = tag
}
}
// WithOutputTimestamp sets the output timestamp for the timer.
func WithOutputTimestamp(outputTimestamp time.Time) timerOptions {
return func(tm *timerConfig) {
tm.HoldTimestamp = mtime.FromTime(outputTimestamp)
}
}
// Context is a parameter for OnTimer methods to receive the fired Timer.
type Context struct {
Family string
Tag string
}
// Provider represents a timer provider interface.
//
// The methods are not intended for end user use, and is subject to change.
type Provider interface {
Set(t TimerMap)
}
// PipelineTimer interface represents valid timer type.
type PipelineTimer interface {
Timers() map[string]TimeDomain
}
// EventTime represents the event time timer.
type EventTime struct {
Family string
}
// Timers returns mapping of timer family ID and its time domain.
func (et EventTime) Timers() map[string]TimeDomain {
return map[string]TimeDomain{et.Family: EventTimeDomain}
}
// Set sets the timer for a event-time timestamp. Calling this method repeatedly for the same key
// will cause it overwrite previously set timer.
func (et EventTime) Set(p Provider, FiringTimestamp time.Time, opts ...timerOptions) {
tc := timerConfig{}
for _, opt := range opts {
opt(&tc)
}
tm := TimerMap{Family: et.Family, Tag: tc.Tag, FireTimestamp: mtime.FromTime(FiringTimestamp), HoldTimestamp: mtime.FromTime(FiringTimestamp)}
if !tc.HoldTimestamp.ToTime().IsZero() {
tm.HoldTimestamp = tc.HoldTimestamp
}
p.Set(tm)
}
// Clear clears this timer.
func (et EventTime) Clear(p Provider) {
p.Set(TimerMap{Family: et.Family, Clear: true})
}
// ClearTag clears this timer for the given tag.
func (et EventTime) ClearTag(p Provider, tag string) {
p.Set(TimerMap{Family: et.Family, Clear: true, Tag: tag})
}
// ProcessingTime represents the processing time timer.
type ProcessingTime struct {
Family string
}
// Timers returns mapping of timer family ID and its time domain.
func (pt ProcessingTime) Timers() map[string]TimeDomain {
return map[string]TimeDomain{pt.Family: ProcessingTimeDomain}
}
// Set sets the timer for processing time domain. Calling this method repeatedly for the same key
// will cause it overwrite previously set timer.
func (pt ProcessingTime) Set(p Provider, FiringTimestamp time.Time, opts ...timerOptions) {
tc := timerConfig{}
for _, opt := range opts {
opt(&tc)
}
tm := TimerMap{Family: pt.Family, Tag: tc.Tag, FireTimestamp: mtime.FromTime(FiringTimestamp), HoldTimestamp: mtime.FromTime(FiringTimestamp)}
if !tc.HoldTimestamp.ToTime().IsZero() {
tm.HoldTimestamp = tc.HoldTimestamp
}
p.Set(tm)
}
// Clear clears this timer.
func (pt ProcessingTime) Clear(p Provider) {
p.Set(TimerMap{Family: pt.Family, Clear: true})
}
// ClearTag clears this timer for the given tag.
func (pt ProcessingTime) ClearTag(p Provider, tag string) {
p.Set(TimerMap{Family: pt.Family, Clear: true, Tag: tag})
}
// InEventTime creates and returns a new EventTime timer object.
func InEventTime(family string) EventTime {
return EventTime{Family: family}
}
// InProcessingTime creates and returns a new ProcessingTime timer object.
func InProcessingTime(family string) ProcessingTime {
return ProcessingTime{Family: family}
}