-
Notifications
You must be signed in to change notification settings - Fork 753
/
notifier.go
265 lines (228 loc) · 8.2 KB
/
notifier.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package events
import (
"sync/atomic"
"time"
"github.com/pborman/uuid"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/api/serviceerror"
enumsspb "go.temporal.io/server/api/enums/v1"
"go.temporal.io/server/common"
"go.temporal.io/server/common/clock"
"go.temporal.io/server/common/collection"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/metrics"
)
const (
eventsChanSize = 1000
)
type (
Notifier interface {
common.Daemon
NotifyNewHistoryEvent(event *Notification)
WatchHistoryEvent(identifier definition.WorkflowIdentifier) (string, chan *Notification, error)
UnwatchHistoryEvent(identifier definition.WorkflowIdentifier, subscriberID string) error
}
Notification struct {
ID definition.WorkflowIdentifier
LastFirstEventID int64
LastFirstEventTxnID int64
NextEventID int64
PreviousStartedEventID int64
Timestamp time.Time
CurrentBranchToken []byte
WorkflowState enumsspb.WorkflowExecutionState
WorkflowStatus enumspb.WorkflowExecutionStatus
}
NotifierImpl struct {
timeSource clock.TimeSource
metrics metrics.Client
// internal status indicator
status int32
// stop signal channel
closeChan chan bool
// this channel will never close
eventsChan chan *Notification
// function which calculate the shard ID from given namespaceID and workflowID pair
workflowIDToShardID func(string, string) int32
// concurrent map with key workflowIdentifier, value map[string]chan *Notification.
// the reason for the second map being non thread safe:
// 1. expected number of subscriber per workflow is low, i.e. < 5
// 2. update to this map is already guarded by GetAndDo API provided by ConcurrentTxMap
eventsPubsubs collection.ConcurrentTxMap
}
)
var _ Notifier = (*NotifierImpl)(nil)
func NewNotification(
namespaceID string,
workflowExecution *commonpb.WorkflowExecution,
lastFirstEventID int64,
lastFirstEventTxnID int64,
nextEventID int64,
previousStartedEventID int64,
currentBranchToken []byte,
workflowState enumsspb.WorkflowExecutionState,
workflowStatus enumspb.WorkflowExecutionStatus,
) *Notification {
return &Notification{
ID: definition.NewWorkflowIdentifier(
namespaceID,
workflowExecution.GetWorkflowId(),
workflowExecution.GetRunId(),
),
LastFirstEventID: lastFirstEventID,
LastFirstEventTxnID: lastFirstEventTxnID,
NextEventID: nextEventID,
PreviousStartedEventID: previousStartedEventID,
CurrentBranchToken: currentBranchToken,
WorkflowState: workflowState,
WorkflowStatus: workflowStatus,
}
}
func NewNotifier(
timeSource clock.TimeSource,
metrics metrics.Client,
workflowIDToShardID func(string, string) int32,
) *NotifierImpl {
hashFn := func(key interface{}) uint32 {
notification, ok := key.(Notification)
if !ok {
return 0
}
return uint32(workflowIDToShardID(notification.ID.NamespaceID, notification.ID.WorkflowID))
}
return &NotifierImpl{
timeSource: timeSource,
metrics: metrics,
status: common.DaemonStatusInitialized,
closeChan: make(chan bool),
eventsChan: make(chan *Notification, eventsChanSize),
workflowIDToShardID: workflowIDToShardID,
eventsPubsubs: collection.NewShardedConcurrentTxMap(1024, hashFn),
}
}
func (notifier *NotifierImpl) WatchHistoryEvent(
identifier definition.WorkflowIdentifier) (string, chan *Notification, error) {
channel := make(chan *Notification, 1)
subscriberID := uuid.New()
subscribers := map[string]chan *Notification{
subscriberID: channel,
}
_, _, err := notifier.eventsPubsubs.PutOrDo(identifier, subscribers, func(key interface{}, value interface{}) error {
subscribers := value.(map[string]chan *Notification)
if _, ok := subscribers[subscriberID]; ok {
// UUID collision
return serviceerror.NewInternal("Unable to watch on workflow execution.")
}
subscribers[subscriberID] = channel
return nil
})
if err != nil {
return "", nil, err
}
return subscriberID, channel, nil
}
func (notifier *NotifierImpl) UnwatchHistoryEvent(
identifier definition.WorkflowIdentifier, subscriberID string) error {
success := true
notifier.eventsPubsubs.RemoveIf(identifier, func(key interface{}, value interface{}) bool {
subscribers := value.(map[string]chan *Notification)
if _, ok := subscribers[subscriberID]; !ok {
// cannot find the subscribe ID, which means there is a bug
success = false
} else {
delete(subscribers, subscriberID)
}
return len(subscribers) == 0
})
if !success {
// cannot find the subscribe ID, which means there is a bug
return serviceerror.NewInternal("Unable to unwatch on workflow execution.")
}
return nil
}
func (notifier *NotifierImpl) dispatchHistoryEventNotification(event *Notification) {
identifier := event.ID
timer := notifier.metrics.StartTimer(metrics.HistoryEventNotificationScope, metrics.HistoryEventNotificationFanoutLatency)
defer timer.Stop()
notifier.eventsPubsubs.GetAndDo(identifier, func(key interface{}, value interface{}) error { //nolint:errcheck
subscribers := value.(map[string]chan *Notification)
for _, channel := range subscribers {
select {
case channel <- event:
default:
// in case the channel is already filled with message
// this should NOT happen, unless there is a bug or high load
}
}
return nil
})
}
func (notifier *NotifierImpl) enqueueHistoryEventNotification(event *Notification) {
// set the Timestamp just before enqueuing the event
event.Timestamp = notifier.timeSource.Now()
select {
case notifier.eventsChan <- event:
default:
// in case the channel is already filled with message
// this can be caused by high load
notifier.metrics.IncCounter(metrics.HistoryEventNotificationScope,
metrics.HistoryEventNotificationFailDeliveryCount)
}
}
func (notifier *NotifierImpl) dequeueHistoryEventNotifications() {
for {
// send out metrics about the current number of messages in flight
notifier.metrics.UpdateGauge(metrics.HistoryEventNotificationScope,
metrics.HistoryEventNotificationInFlightMessageGauge, float64(len(notifier.eventsChan)))
select {
case event := <-notifier.eventsChan:
// send out metrics about message processing delay
timeelapsed := time.Since(event.Timestamp)
notifier.metrics.RecordTimer(metrics.HistoryEventNotificationScope,
metrics.HistoryEventNotificationQueueingLatency, timeelapsed)
notifier.dispatchHistoryEventNotification(event)
case <-notifier.closeChan:
// shutdown
return
}
}
}
func (notifier *NotifierImpl) Start() {
if !atomic.CompareAndSwapInt32(¬ifier.status, common.DaemonStatusInitialized, common.DaemonStatusStarted) {
return
}
go notifier.dequeueHistoryEventNotifications()
}
func (notifier *NotifierImpl) Stop() {
if !atomic.CompareAndSwapInt32(¬ifier.status, common.DaemonStatusStarted, common.DaemonStatusStopped) {
return
}
close(notifier.closeChan)
}
func (notifier *NotifierImpl) NotifyNewHistoryEvent(event *Notification) {
notifier.enqueueHistoryEventNotification(event)
}