forked from sbawaska/eventing-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
370 lines (333 loc) · 13 KB
/
dispatcher.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
Copyright 2018 The Knative Authors
Licensed 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 dispatcher
import (
"context"
"encoding/json"
"fmt"
"sync"
"sync/atomic"
"time"
stan "github.com/nats-io/go-nats-streaming"
"go.uber.org/zap"
eventingduck "knative.dev/eventing/pkg/apis/duck/v1alpha1"
messagingv1alpha1 "knative.dev/eventing/pkg/apis/messaging/v1alpha1"
eventingchannels "knative.dev/eventing/pkg/channel"
"knative.dev/eventing/pkg/logging"
"knative.dev/eventing-contrib/natss/pkg/stanutil"
contribchannels "knative.dev/eventing-contrib/pkg/channel"
)
const (
// maxElements defines a maximum number of outstanding re-connect requests
maxElements = 10
)
var (
// retryInterval defines delay in seconds for the next attempt to reconnect to NATSS streaming server
retryInterval = 1 * time.Second
)
type SubscriptionChannelMapping map[eventingchannels.ChannelReference]map[subscriptionReference]*stan.Subscription
// SubscriptionsSupervisor manages the state of NATS Streaming subscriptions
type SubscriptionsSupervisor struct {
logger *zap.Logger
receiver *contribchannels.MessageReceiver
dispatcher *contribchannels.MessageDispatcher
subscriptionsMux sync.Mutex
subscriptions SubscriptionChannelMapping
connect chan struct{}
natssURL string
clusterID string
clientID string
// natConnMux is used to protect natssConn and natssConnInProgress during
// the transition from not connected to connected states.
natssConnMux sync.Mutex
natssConn *stan.Conn
natssConnInProgress bool
hostToChannelMap atomic.Value
}
type NatssDispatcher interface {
Start(stopCh <-chan struct{}) error
UpdateSubscriptions(channel *messagingv1alpha1.Channel, isFinalizer bool) (map[eventingduck.SubscriberSpec]error, error)
ProcessChannels(ctx context.Context, chanList []messagingv1alpha1.Channel) error
}
// NewDispatcher returns a new NatssDispatcher.
func NewDispatcher(natssURL, clusterID, clientID string, logger *zap.Logger) (NatssDispatcher, error) {
d := &SubscriptionsSupervisor{
logger: logger,
dispatcher: contribchannels.NewMessageDispatcher(logger.Sugar()),
connect: make(chan struct{}, maxElements),
natssURL: natssURL,
clusterID: clusterID,
clientID: clientID,
subscriptions: make(SubscriptionChannelMapping),
}
d.setHostToChannelMap(map[string]eventingchannels.ChannelReference{})
receiver, err := contribchannels.NewMessageReceiver(
createReceiverFunction(d, logger.Sugar()),
logger.Sugar(),
contribchannels.ResolveChannelFromHostHeader(contribchannels.ResolveChannelFromHostFunc(d.getChannelReferenceFromHost)))
if err != nil {
return nil, err
}
d.receiver = receiver
return d, nil
}
func (s *SubscriptionsSupervisor) signalReconnect() {
select {
case s.connect <- struct{}{}:
// Sent.
default:
// The Channel is already full, so a reconnection attempt will occur.
}
}
func createReceiverFunction(s *SubscriptionsSupervisor, logger *zap.SugaredLogger) func(eventingchannels.ChannelReference, *contribchannels.Message) error {
return func(channel eventingchannels.ChannelReference, m *contribchannels.Message) error {
logger.Infof("Received message from %q channel", channel.String())
// publish to Natss
ch := getSubject(channel)
message, err := json.Marshal(m)
if err != nil {
logger.Errorf("Error during marshaling of the message: %v", err)
return err
}
s.natssConnMux.Lock()
currentNatssConn := s.natssConn
s.natssConnMux.Unlock()
if currentNatssConn == nil {
return fmt.Errorf("No Connection to NATSS")
}
if err := stanutil.Publish(currentNatssConn, ch, &message, logger); err != nil {
logger.Errorf("Error during publish: %v", err)
if err.Error() == stan.ErrConnectionClosed.Error() {
logger.Error("Connection to NATSS has been lost, attempting to reconnect.")
// Informing SubscriptionsSupervisor to re-establish connection to NATSS.
s.signalReconnect()
return err
}
return err
}
logger.Debugf("Published [%s] : '%s'", channel.String(), m.Headers)
return nil
}
}
func (s *SubscriptionsSupervisor) Start(stopCh <-chan struct{}) error {
// Starting Connect to establish connection with NATS
go s.Connect(stopCh)
// Trigger Connect to establish connection with NATS
s.signalReconnect()
return s.receiver.Start(stopCh)
}
func (s *SubscriptionsSupervisor) connectWithRetry(stopCh <-chan struct{}) {
// re-attempting evey 1 second until the connection is established.
ticker := time.NewTicker(retryInterval)
defer ticker.Stop()
for {
nConn, err := stanutil.Connect(s.clusterID, s.clientID, s.natssURL, s.logger.Sugar())
if err == nil {
// Locking here in order to reduce time in locked state.
s.natssConnMux.Lock()
s.natssConn = nConn
s.natssConnInProgress = false
s.natssConnMux.Unlock()
return
}
s.logger.Sugar().Errorf("Connect() failed with error: %+v, retrying in %s", err, retryInterval.String())
select {
case <-ticker.C:
continue
case <-stopCh:
return
}
}
}
// Connect is called for initial connection as well as after every disconnect
func (s *SubscriptionsSupervisor) Connect(stopCh <-chan struct{}) {
for {
select {
case <-s.connect:
s.natssConnMux.Lock()
currentConnProgress := s.natssConnInProgress
s.natssConnMux.Unlock()
if !currentConnProgress {
// Case for lost connectivity, setting InProgress to true to prevent recursion
s.natssConnMux.Lock()
s.natssConnInProgress = true
s.natssConnMux.Unlock()
go s.connectWithRetry(stopCh)
}
case <-stopCh:
return
}
}
}
// UpdateSubscriptions creates/deletes the natss subscriptions based on channel.Spec.Subscribable.Subscribers
// Return type:map[eventingduck.SubscriberSpec]error --> Returns a map of subscriberSpec that failed with the value=error encountered.
// Ignore the value in case error != nil
func (s *SubscriptionsSupervisor) UpdateSubscriptions(channel *messagingv1alpha1.Channel, isFinalizer bool) (map[eventingduck.SubscriberSpec]error, error) {
s.subscriptionsMux.Lock()
defer s.subscriptionsMux.Unlock()
failedToSubscribe := make(map[eventingduck.SubscriberSpec]error)
cRef := eventingchannels.ChannelReference{Namespace: channel.Namespace, Name: channel.Name}
if channel.Spec.Subscribable == nil || isFinalizer {
s.logger.Sugar().Infof("Empty subscriptions for channel Ref: %v; unsubscribe all active subscriptions, if any", cRef)
chMap, ok := s.subscriptions[cRef]
if !ok {
// nothing to do
s.logger.Sugar().Infof("No channel Ref %v found in subscriptions map", cRef)
return failedToSubscribe, nil
}
for sub := range chMap {
s.unsubscribe(cRef, sub)
}
delete(s.subscriptions, cRef)
return failedToSubscribe, nil
}
subscriptions := channel.Spec.Subscribable.Subscribers
activeSubs := make(map[subscriptionReference]bool) // it's logically a set
chMap, ok := s.subscriptions[cRef]
if !ok {
chMap = make(map[subscriptionReference]*stan.Subscription)
s.subscriptions[cRef] = chMap
}
var errStrings []string
for _, sub := range subscriptions {
// check if the subscription already exist and do nothing in this case
subRef := newSubscriptionReference(sub)
if _, ok := chMap[subRef]; ok {
activeSubs[subRef] = true
s.logger.Sugar().Infof("Subscription: %v already active for channel: %v", sub, cRef)
continue
}
// subscribe and update failedSubscription if subscribe fails
natssSub, err := s.subscribe(cRef, subRef)
if err != nil {
errStrings = append(errStrings, err.Error())
s.logger.Sugar().Errorf("failed to subscribe (subscription:%q) to channel: %v. Error:%s", sub, cRef, err.Error())
failedToSubscribe[sub] = err
continue
}
chMap[subRef] = natssSub
activeSubs[subRef] = true
}
// Unsubscribe for deleted subscriptions
for sub := range chMap {
if ok := activeSubs[sub]; !ok {
s.unsubscribe(cRef, sub)
}
}
// delete the channel from s.subscriptions if chMap is empty
if len(s.subscriptions[cRef]) == 0 {
delete(s.subscriptions, cRef)
}
return failedToSubscribe, nil
}
func (s *SubscriptionsSupervisor) subscribe(channel eventingchannels.ChannelReference, subscription subscriptionReference) (*stan.Subscription, error) {
s.logger.Info("Subscribe to channel:", zap.Any("channel", channel), zap.Any("subscription", subscription))
mcb := func(msg *stan.Msg) {
message := contribchannels.Message{}
if err := json.Unmarshal(msg.Data, &message); err != nil {
s.logger.Error("Failed to unmarshal message: ", zap.Error(err))
return
}
s.logger.Sugar().Debugf("NATSS message received from subject: %v; sequence: %v; timestamp: %v, headers: '%s'", msg.Subject, msg.Sequence, msg.Timestamp, message.Headers)
if err := s.dispatcher.DispatchMessage(&message, subscription.SubscriberURI, subscription.ReplyURI, contribchannels.DispatchDefaults{Namespace: channel.Namespace}); err != nil {
s.logger.Error("Failed to dispatch message: ", zap.Error(err))
return
}
if err := msg.Ack(); err != nil {
s.logger.Error("Failed to acknowledge message: ", zap.Error(err))
}
}
// subscribe to a NATSS subject
ch := getSubject(channel)
sub := subscription.String()
s.natssConnMux.Lock()
currentNatssConn := s.natssConn
s.natssConnMux.Unlock()
if currentNatssConn == nil {
return nil, fmt.Errorf("No Connection to NATSS")
}
natssSub, err := (*currentNatssConn).Subscribe(ch, mcb, stan.DurableName(sub), stan.SetManualAckMode(), stan.AckWait(1*time.Minute))
if err != nil {
s.logger.Error(" Create new NATSS Subscription failed: ", zap.Error(err))
if err.Error() == stan.ErrConnectionClosed.Error() {
s.logger.Error("Connection to NATSS has been lost, attempting to reconnect.")
// Informing SubscriptionsSupervisor to re-establish connection to NATS
s.signalReconnect()
return nil, err
}
return nil, err
}
s.logger.Sugar().Infof("NATSS Subscription created: %+v", natssSub)
return &natssSub, nil
}
// should be called only while holding subscriptionsMux
func (s *SubscriptionsSupervisor) unsubscribe(channel eventingchannels.ChannelReference, subscription subscriptionReference) error {
s.logger.Info("Unsubscribe from channel:", zap.Any("channel", channel), zap.Any("subscription", subscription))
if stanSub, ok := s.subscriptions[channel][subscription]; ok {
// delete from NATSS
if err := (*stanSub).Unsubscribe(); err != nil {
s.logger.Error("Unsubscribing NATSS Streaming subscription failed: ", zap.Error(err))
return err
}
delete(s.subscriptions[channel], subscription)
}
return nil
}
func getSubject(channel eventingchannels.ChannelReference) string {
return channel.Name + "." + channel.Namespace
}
func (s *SubscriptionsSupervisor) getHostToChannelMap() map[string]eventingchannels.ChannelReference {
return s.hostToChannelMap.Load().(map[string]eventingchannels.ChannelReference)
}
func (s *SubscriptionsSupervisor) setHostToChannelMap(hcMap map[string]eventingchannels.ChannelReference) {
s.hostToChannelMap.Store(hcMap)
}
// NewHostNameToChannelRefMap parses each channel from cList and creates a map[string(Status.Address.HostName)]ChannelReference
func newHostNameToChannelRefMap(cList []messagingv1alpha1.Channel) (map[string]eventingchannels.ChannelReference, error) {
hostToChanMap := make(map[string]eventingchannels.ChannelReference, len(cList))
for _, c := range cList {
url := c.Status.Address.GetURL()
if cr, present := hostToChanMap[url.Host]; present {
return nil, fmt.Errorf(
"duplicate hostName found. Each channel must have a unique host header. HostName:%s, channel:%s.%s, channel:%s.%s",
url.Host,
c.Namespace,
c.Name,
cr.Namespace,
cr.Name)
}
hostToChanMap[url.Host] = eventingchannels.ChannelReference{Name: c.Name, Namespace: c.Namespace}
}
return hostToChanMap, nil
}
// ProcessChannels will be called from the controller that watches natss channels.
// It will update internal hostToChannelMap which is used to resolve the hostHeader of the
// incoming request to the correct ChannelReference in the receiver function.
func (s *SubscriptionsSupervisor) ProcessChannels(ctx context.Context, chanList []messagingv1alpha1.Channel) error {
hostToChanMap, err := newHostNameToChannelRefMap(chanList)
if err != nil {
logging.FromContext(ctx).Info("ProcessChannels: Error occurred when creating the new hostToChannel map.", zap.Error(err))
return err
}
s.setHostToChannelMap(hostToChanMap)
logging.FromContext(ctx).Info("hostToChannelMap updated successfully.")
return nil
}
func (s *SubscriptionsSupervisor) getChannelReferenceFromHost(host string) (eventingchannels.ChannelReference, error) {
chMap := s.getHostToChannelMap()
cr, ok := chMap[host]
if !ok {
return cr, fmt.Errorf("Invalid HostName:%q. HostName not found in any of the watched natss channels", host)
}
return cr, nil
}