-
Notifications
You must be signed in to change notification settings - Fork 33
/
broadcasts.go
388 lines (348 loc) · 11.1 KB
/
broadcasts.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright 2017 Google Inc.
//
// 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
//
// https://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 broadcasts contains code for a Fleetspeak server to manage
// broadcasts. See in particular the Manager.
package broadcasts
import (
"context"
"errors"
"fmt"
"math/rand"
"strings"
"sync"
"sync/atomic"
"time"
log "github.com/golang/glog"
"github.com/google/fleetspeak/fleetspeak/src/common"
"github.com/google/fleetspeak/fleetspeak/src/server/db"
"github.com/google/fleetspeak/fleetspeak/src/server/ids"
"github.com/google/fleetspeak/fleetspeak/src/server/internal/cache"
"github.com/google/fleetspeak/fleetspeak/src/server/internal/ftime"
"github.com/google/fleetspeak/fleetspeak/src/server/internal/notifications"
fspb "github.com/google/fleetspeak/fleetspeak/src/common/proto/fleetspeak"
spb "github.com/google/fleetspeak/fleetspeak/src/server/proto/fleetspeak_server"
)
const (
allocFrac = 0.2
allocDuration = 10 * time.Minute
)
// A Manager keeps tracts of the active broadcasts in a system. It allows a
// fleetspeak system to deliver broadcasts to client without all of the
// fleetspeak servers trying to modify the same datastore records at the same
// time.
type Manager struct {
bs db.BroadcastStore
infos map[ids.BroadcastID]*bInfo
l sync.RWMutex // Protects the structure of i.
done chan bool // Closes to indicate it is time to shut down.
working sync.WaitGroup // Indicates that work is ongoing.
basePollWait time.Duration
// These members are used to inform other parts of the system that they might
// need to check for broadcast messages.
clientCache *cache.Clients
dispatcher *notifications.Dispatcher
}
// MakeManager creates a Manager, populates it with the
// current set of broadcasts, and begins updating the broadcasts in the
// background, the time between updates is always between pw and 2*pw.
func MakeManager(ctx context.Context, bs db.BroadcastStore, pw time.Duration, clientCache *cache.Clients, dispatcher *notifications.Dispatcher) (*Manager, error) {
r := &Manager{
bs: bs,
infos: make(map[ids.BroadcastID]*bInfo),
done: make(chan bool),
basePollWait: pw,
clientCache: clientCache,
dispatcher: dispatcher,
}
if err := r.refreshInfo(ctx); err != nil {
return nil, err
}
r.working.Add(1)
go r.refreshLoop()
return r, nil
}
// bInfo contains what a broadcast manager needs to know about a broadcast.
type bInfo struct {
bID ids.BroadcastID
b *spb.Broadcast
useCount sync.WaitGroup // How many goroutines are using this bInfo and associated allocation.
// The remaining fields describe the allocation that we have for the broadcast. If limit is
// set to BroadcastUnlimited, then we don't actually have (or need) an allocation.
aID ids.AllocationID
limit uint64
sent uint64 // How many messages have we sent under the current allocation, only accessed through atomic.
expiry time.Time
lock sync.Mutex // Used to sychronize updates to the allocation record in the database.
}
// limitedAtomicIncrement atomically adds one to *addr, unless the result would
// exceed limit. Return true if successful.
func limitedAtomicIncrement(addr *uint64, limit uint64) bool {
for {
c := atomic.LoadUint64(addr)
if c >= limit {
return false
}
if atomic.CompareAndSwapUint64(addr, c, c+1) {
return true
}
}
}
// pollWait picks a time to wait before the next refresh.
func (m *Manager) pollWait() time.Duration {
return m.basePollWait + time.Duration(float64(m.basePollWait)*rand.Float64())
}
// label shadows fspb.Label, but is safe to use as a map key
type label struct {
serviceName string
label string
}
func labelFromProto(l *fspb.Label) label {
return label{serviceName: l.ServiceName, label: l.Label}
}
// MakeBroadcastMessagesForClient finds broadcasts that the client is eligible
// to receive.
func (m *Manager) MakeBroadcastMessagesForClient(ctx context.Context, id common.ClientID, labels []*fspb.Label) ([]*fspb.Message, error) {
labelSet := make(map[label]bool)
for _, l := range labels {
labelSet[labelFromProto(l)] = true
}
sent, err := m.bs.ListSentBroadcasts(ctx, id)
if err != nil {
return nil, err
}
sentSet := make(map[ids.BroadcastID]bool)
for _, s := range sent {
sentSet[s] = true
}
var is []*bInfo
m.l.RLock()
Infos:
for _, info := range m.infos {
if sentSet[info.bID] {
continue
}
if !info.expiry.IsZero() && info.expiry.Before(ftime.Now()) {
continue
}
for _, kw := range info.b.RequiredLabels {
if !labelSet[labelFromProto(kw)] {
continue Infos
}
}
if info.limit == db.BroadcastUnlimited {
atomic.AddUint64(&info.sent, 1)
} else {
if !limitedAtomicIncrement(&info.sent, info.limit) {
continue
}
}
info.useCount.Add(1)
is = append(is, info)
}
m.l.RUnlock()
// NOTE: we must call useCount.Done() on everything in "is", or the
// allocation update goroutine will get stuck. From this point on we log
// errors but don't stop.
msgs := make([]*fspb.Message, 0, len(is))
for _, i := range is {
mid, err := common.RandomMessageID()
if err != nil {
log.Errorf("unable to create message id: %v", err)
if i.limit != db.BroadcastUnlimited {
// Incantation to decrement a uint64, recommend AddUint64 docs:
atomic.AddUint64(&i.sent, ^uint64(0))
}
i.useCount.Done()
continue
}
msg := &fspb.Message{
MessageId: mid.Bytes(),
Source: i.b.Source,
Destination: &fspb.Address{
ClientId: id.Bytes(),
ServiceName: i.b.Source.ServiceName,
},
MessageType: i.b.MessageType,
Data: i.b.Data,
CreationTime: db.NowProto(),
}
i.lock.Lock()
err = m.bs.SaveBroadcastMessage(ctx, msg, i.bID, id, i.aID)
i.lock.Unlock()
if err != nil {
log.Errorf("SaveBroadcastMessage of instance of broadcast %v failed. Not sending. [%v]", i.bID, err)
if i.limit != db.BroadcastUnlimited {
// Incantation to decrement a uint64, recommend by AddUint64 docs:
atomic.AddUint64(&i.sent, ^uint64(0))
}
i.useCount.Done()
continue
}
msgs = append(msgs, msg)
i.useCount.Done()
}
return msgs, nil
}
func (m *Manager) refreshLoop() {
defer m.working.Done()
ctx := context.Background()
for {
select {
case _, ok := <-m.done:
if !ok {
return
}
case <-time.After(m.pollWait()):
}
if err := m.refreshInfo(ctx); err != nil {
log.Errorf("Error refreshing broadcast infos: %v", err)
}
}
}
// refreshInfo refreshes the bInfo map using the data from the database.
func (m *Manager) refreshInfo(ctx context.Context) error {
// Find the allocations that we don't need (or want) to change.
curr := m.findCurrentAllocs()
// Find the active broadcasts.
bs, err := m.bs.ListActiveBroadcasts(ctx)
if err != nil {
return fmt.Errorf("unable to list active broadcasts: %v", err)
}
// Create any new allocations that we'll need: everything in bs but not curr.
newAllocs := make(map[ids.BroadcastID]*bInfo)
activeIds := make(map[ids.BroadcastID]bool)
for _, b := range bs {
id, err := ids.BytesToBroadcastID(b.Broadcast.BroadcastId)
if err != nil {
log.Errorf("Broadcast [%v] has bad id, skipping: %v", b.Broadcast, err)
continue
}
activeIds[id] = true
if !curr[id] {
if b.Sent == b.Limit {
continue
}
a, err := m.bs.CreateAllocation(ctx, id, allocFrac, ftime.Now().Add(allocDuration))
if err != nil {
log.Errorf("Unable to create alloc for broadcast %v, skipping: %v", id, err)
continue
}
if a != nil {
newAllocs[id] = &bInfo{
bID: id,
b: b.Broadcast,
aID: a.ID,
limit: a.Limit,
sent: 0,
expiry: a.Expiry,
}
}
}
}
// Some things in curr might no longer be active, e.g. if the broadcast
// was canceled. Remove them from curr so that updateAllocs knows to
// clear them.
for id := range curr {
if !activeIds[id] {
delete(curr, id)
}
}
// Swap/insert the new allocations.
c := m.updateAllocs(curr, newAllocs)
// If we added any new allocations, then we should recompute broadcasts for
// any cached clients. We also start a process taking up to basePollWait time
// to notify already connected clients.
if len(newAllocs) > 0 {
m.clientCache.Clear()
go func() {
ctx, fin := context.WithTimeout(context.Background(), m.basePollWait)
m.dispatcher.NotifyAll(ctx)
fin()
}()
}
var errMsgs []string
// Cleanup the dead allocations. They've been removed from m.infos, so
// once useCount reaches 0, no new actions with them will start. We
// cleanup everything we can, even if there are errors.
for _, a := range c {
a.useCount.Wait()
if err := m.bs.CleanupAllocation(ctx, a.bID, a.aID); err != nil {
errMsgs = append(errMsgs, fmt.Sprintf("[%v,%v]:\"%v\"", a.bID, a.aID, err))
}
}
if len(errMsgs) > 0 {
return errors.New("errors cleaning up allocations - " + strings.Join(errMsgs, " "))
}
return nil
}
func (m *Manager) findCurrentAllocs() map[ids.BroadcastID]bool {
r := make(map[ids.BroadcastID]bool)
// We should be the only goroutine that modifies m.infos, so we don't need to
// touch m.l.
for _, info := range m.infos {
// If the current allocation has room to send at least one
// message, and it should last until the next tick, we keep it.
if (info.limit == db.BroadcastUnlimited || atomic.LoadUint64(&info.sent) < info.limit) &&
(info.expiry.IsZero() || info.expiry.After(ftime.Now().Add(2*m.basePollWait))) {
r[info.bID] = true
}
}
return r
}
// updateAllocs updates the map m.info.
//
// "keep" identifies records which should be preserved while "new" identifies
// records which should be updated. Any other record will be removed. The
// return value lists those structs which were removed or replaced.
func (m *Manager) updateAllocs(keep map[ids.BroadcastID]bool, new map[ids.BroadcastID]*bInfo) []*bInfo {
m.l.Lock()
defer m.l.Unlock()
var ret []*bInfo
// Make a pass through m.infos, deleting anything not in keep or new.
for _, info := range m.infos {
if keep[info.bID] {
continue
}
if new[info.bID] == nil {
ret = append(ret, info)
delete(m.infos, info.bID)
continue
}
}
// Make a pass through new, updating m.infos.
for id, info := range new {
if m.infos[id] != nil {
ret = append(ret, m.infos[id])
}
m.infos[id] = info
}
return ret
}
// Close attempts to shut down the Manager gracefully.
func (m *Manager) Close(ctx context.Context) error {
close(m.done)
m.working.Wait()
var errMsgs []string
for _, i := range m.infos {
i.useCount.Wait()
if err := m.bs.CleanupAllocation(ctx, i.bID, i.aID); err != nil {
errMsgs = append(errMsgs, fmt.Sprintf("[%v,%v]:\"%v\"", i.bID, i.aID, err))
}
}
m.infos = nil
if len(errMsgs) > 0 {
return errors.New("errors cleaning up allocations - " + strings.Join(errMsgs, " "))
}
return nil
}