/
pubsub.go
344 lines (317 loc) · 9.44 KB
/
pubsub.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// 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 pubsub implements the go-cloud pub/sub frontend.
package pubsub
import (
"context"
"fmt"
"sync"
"go.thethings.network/lorawan-stack/v3/pkg/applicationserver/io"
"go.thethings.network/lorawan-stack/v3/pkg/applicationserver/io/pubsub/provider"
"go.thethings.network/lorawan-stack/v3/pkg/component"
"go.thethings.network/lorawan-stack/v3/pkg/errorcontext"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/task"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/unique"
"gocloud.dev/pubsub"
)
// PubSub is an pub/sub frontend that exposes ttnpb.ApplicationPubSubRegistryServer.
type PubSub struct {
ttnpb.UnimplementedApplicationPubSubRegistryServer
*component.Component
ctx context.Context
server io.Server
registry Registry
integrations sync.Map
providerStatuses ProviderStatuses
}
// New creates a new pusub frontend.
func New(
c *component.Component,
server io.Server,
registry Registry,
providerStatuses ProviderStatuses,
) (*PubSub, error) {
ctx := log.NewContextWithField(c.Context(), "namespace", "applicationserver/io/pubsub")
ps := &PubSub{
Component: c,
ctx: ctx,
server: server,
registry: registry,
providerStatuses: providerStatuses,
}
ps.RegisterTask(&task.Config{
Context: ctx,
ID: "pubsubs_start_all",
Func: ps.startAll,
Restart: task.RestartOnFailure,
Backoff: task.DefaultBackoffConfig,
})
return ps, nil
}
func (ps *PubSub) startAll(ctx context.Context) error {
return ps.registry.Range(ctx, []string{"ids"},
func(ctx context.Context, _ *ttnpb.ApplicationIdentifiers, pb *ttnpb.ApplicationPubSub) bool {
ps.startTask(ctx, pb.Ids)
return true
},
)
}
func (ps *PubSub) startTask(ctx context.Context, ids *ttnpb.ApplicationPubSubIdentifiers) {
ctx = log.NewContextWithFields(ctx, log.Fields(
"application_uid", unique.ID(ctx, ids.ApplicationIds),
"pub_sub_id", ids.PubSubId,
))
ps.StartTask(&task.Config{
Context: ctx,
ID: "pubsub",
Func: func(ctx context.Context) error {
target, err := ps.registry.Get(ctx, ids, ttnpb.ApplicationPubSubFieldPathsNested)
if err != nil && !errors.IsNotFound(err) {
return err
} else if err != nil {
log.FromContext(ctx).WithError(err).Warn("Pub/Sub not found")
return nil
}
if err := ps.providerStatuses.Enabled(ctx, target.Provider); err != nil {
log.FromContext(ctx).WithError(err).Debug("Pub/Sub not enabled")
return nil
}
return ps.start(ctx, target)
},
Restart: task.RestartOnFailure,
Backoff: io.DialTaskBackoffConfig,
})
}
type integration struct {
*ttnpb.ApplicationPubSub
ctx context.Context
cancel errorcontext.CancelFunc
closed chan struct{}
conn *provider.Connection
server io.Server
sub *io.Subscription
format Format
}
func (i *integration) handleUp(ctx context.Context) {
logger := log.FromContext(ctx)
for {
select {
case <-ctx.Done():
logger.WithError(ctx.Err()).Debug("Done sending upstream messages")
return
case up := <-i.sub.Up():
var topic *pubsub.Topic
switch up.ApplicationUp.Up.(type) {
case *ttnpb.ApplicationUp_UplinkMessage:
topic = i.conn.Topics.UplinkMessage
case *ttnpb.ApplicationUp_UplinkNormalized:
topic = i.conn.Topics.UplinkNormalized
case *ttnpb.ApplicationUp_JoinAccept:
topic = i.conn.Topics.JoinAccept
case *ttnpb.ApplicationUp_DownlinkAck:
topic = i.conn.Topics.DownlinkAck
case *ttnpb.ApplicationUp_DownlinkNack:
topic = i.conn.Topics.DownlinkNack
case *ttnpb.ApplicationUp_DownlinkSent:
topic = i.conn.Topics.DownlinkSent
case *ttnpb.ApplicationUp_DownlinkFailed:
topic = i.conn.Topics.DownlinkFailed
case *ttnpb.ApplicationUp_DownlinkQueued:
topic = i.conn.Topics.DownlinkQueued
case *ttnpb.ApplicationUp_DownlinkQueueInvalidated:
topic = i.conn.Topics.DownlinkQueueInvalidated
case *ttnpb.ApplicationUp_LocationSolved:
topic = i.conn.Topics.LocationSolved
case *ttnpb.ApplicationUp_ServiceData:
topic = i.conn.Topics.ServiceData
}
if topic == nil {
continue
}
buf, err := i.format.FromUp(up.ApplicationUp)
if err != nil {
logger.WithError(err).Warn("Failed to marshal upstream message")
continue
}
err = topic.Send(ctx, &pubsub.Message{
Body: buf,
})
if err != nil {
logger.WithError(err).Warn("Failed to publish upstream message")
i.cancel(err)
return
}
logger.Debug("Publish upstream message")
}
}
}
func (i *integration) handleDown(
ctx context.Context,
op func(io.Server, context.Context, *ttnpb.EndDeviceIdentifiers, []*ttnpb.ApplicationDownlink) error,
subscription *pubsub.Subscription,
) {
logger := log.FromContext(ctx)
for {
select {
case <-ctx.Done():
return
default:
}
msg, err := subscription.Receive(ctx)
if err != nil {
logger.WithError(err).Warn("Failed to receive downlink queue operation")
i.cancel(err)
return
}
msg.Ack()
operation, err := i.format.ToDownlinkQueueRequest(msg.Body)
if err != nil {
logger.WithError(err).Warn("Failed to decode downlink queue operation")
continue
}
if err := operation.ValidateFields(); err != nil {
logger.WithError(err).Warn("Failed to validate downlink queue operation")
continue
}
if err := operation.EndDeviceIds.ValidateContext(ctx); err != nil {
logger.WithError(err).Warn("Failed to validate downlink queue operation")
continue
}
logger.WithFields(log.Fields(
"device_uid", unique.ID(ctx, operation.EndDeviceIds),
"count", len(operation.Downlinks),
)).Debug("Handle downlink messages")
if err := op(i.server, ctx, operation.EndDeviceIds, operation.Downlinks); err != nil {
logger.WithError(err).Warn("Failed to handle downlink messages")
}
}
}
func (i *integration) startHandleDown(ctx context.Context) {
for _, downlink := range []struct {
name string
op func(io.Server, context.Context, *ttnpb.EndDeviceIdentifiers, []*ttnpb.ApplicationDownlink) error
subscription *pubsub.Subscription
}{
{
name: "push",
op: io.Server.DownlinkQueuePush,
subscription: i.conn.Subscriptions.Push,
},
{
name: "replace",
op: io.Server.DownlinkQueueReplace,
subscription: i.conn.Subscriptions.Replace,
},
} {
if downlink.subscription == nil {
continue
}
go i.handleDown(log.NewContextWithField(ctx, "operation", downlink.name), downlink.op, downlink.subscription)
}
}
func (ps *PubSub) start(ctx context.Context, pb *ttnpb.ApplicationPubSub) (err error) {
appUID := unique.ID(ctx, pb.Ids.ApplicationIds)
psUID := PubSubUID(appUID, pb.Ids.PubSubId)
ctx = log.NewContextWithFields(ctx, log.Fields(
"application_uid", appUID,
"pub_sub_id", pb.Ids.PubSubId,
"provider", fmt.Sprintf("%T", pb.Provider),
))
logger := log.FromContext(ctx)
ctx, cancel := errorcontext.New(ctx)
defer func() {
cancel(err)
}()
i := &integration{
ApplicationPubSub: pb,
ctx: ctx,
cancel: cancel,
closed: make(chan struct{}),
server: ps.server,
}
defer close(i.closed)
if _, loaded := ps.integrations.LoadOrStore(psUID, i); loaded {
logger.Debug("Pub/sub already started")
return nil
}
defer ps.integrations.Delete(psUID)
defer func() {
if err != nil {
logger.WithError(err).Warn("Pub/sub failed")
registerIntegrationFail(ctx, i, err)
}
}()
p, err := provider.GetProvider(pb)
if err != nil {
return err
}
if err := ps.providerStatuses.Enabled(ctx, pb.GetProvider()); err != nil {
return err
}
i.conn, err = p.OpenConnection(ctx, pb, ps.providerStatuses)
if err != nil {
return err
}
defer func() {
if err := i.conn.Shutdown(ctx); err != nil {
logger.WithError(err).Warn("Failed to shutdown pub/sub connection")
} else {
logger.Debug("Shutdown pub/sub connection success")
}
}()
i.sub, err = ps.server.Subscribe(ctx, "pubsub", pb.Ids.ApplicationIds, false)
if err != nil {
return err
}
go func() {
// Close the integration if the subscription is canceled.
select {
case <-i.sub.Context().Done():
err := i.sub.Context().Err()
cancel(err)
case <-ctx.Done():
}
}()
var ok bool
if i.format, ok = formats[pb.Format]; !ok {
return errFormatNotFound.WithAttributes("format", pb.Format)
}
go i.handleUp(ctx)
i.startHandleDown(ctx)
logger.Info("Pub/sub started")
registerIntegrationStart(ctx, i)
defer func() {
logger.Info("Pub/sub stopped")
registerIntegrationStop(ctx, i)
}()
<-ctx.Done()
return ctx.Err()
}
func (ps *PubSub) stop(ctx context.Context, ids *ttnpb.ApplicationPubSubIdentifiers) error {
appUID := unique.ID(ctx, ids.ApplicationIds)
psUID := PubSubUID(appUID, ids.PubSubId)
if val, ok := ps.integrations.Load(psUID); ok {
i := val.(*integration)
i.cancel(context.Canceled)
select {
case <-ctx.Done():
return ctx.Err()
case <-i.closed:
}
}
return nil
}