-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
routers.go
407 lines (371 loc) · 12.1 KB
/
routers.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright 2023 Google LLC
//
// 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 managedwriter
import (
"fmt"
"sort"
"sync"
"time"
)
type poolRouter interface {
// poolAttach is called once to signal a router that it is responsible for a given pool.
poolAttach(pool *connectionPool) error
// poolDetach is called as part of clean connectionPool shutdown.
// It provides an opportunity for the router to shut down internal state.
poolDetach() error
// writerAttach is a hook to notify the router that a new writer is being attached to the pool.
// It provides an opportunity for the router to allocate resources and update internal state.
writerAttach(writer *ManagedStream) error
// writerAttach signals the router that a given writer is being removed from the pool. The router
// does not have responsibility for closing the writer, but this is called as part of writer close.
writerDetach(writer *ManagedStream) error
// pickConnection is used to select a connection for a given pending write.
pickConnection(pw *pendingWrite) (*connection, error)
}
// simpleRouter is a primitive traffic router that routes all traffic to its single connection instance.
//
// This router is designed for our migration case, where an single ManagedStream writer has as 1:1 relationship
// with a connectionPool. You can multiplex with this router, but it will never scale beyond a single connection.
type simpleRouter struct {
mode connectionMode
pool *connectionPool
mu sync.RWMutex
conn *connection
writers map[string]struct{}
}
func (rtr *simpleRouter) poolAttach(pool *connectionPool) error {
if rtr.pool == nil {
rtr.pool = pool
return nil
}
return fmt.Errorf("router already attached to pool %q", rtr.pool.id)
}
func (rtr *simpleRouter) poolDetach() error {
rtr.mu.Lock()
defer rtr.mu.Unlock()
if rtr.conn != nil {
rtr.conn.close()
rtr.conn = nil
}
return nil
}
func (rtr *simpleRouter) writerAttach(writer *ManagedStream) error {
if writer.id == "" {
return fmt.Errorf("writer has no ID")
}
rtr.mu.Lock()
defer rtr.mu.Unlock()
rtr.writers[writer.id] = struct{}{}
if rtr.conn == nil {
rtr.conn = newConnection(rtr.pool, rtr.mode, nil)
}
return nil
}
func (rtr *simpleRouter) writerDetach(writer *ManagedStream) error {
if writer.id == "" {
return fmt.Errorf("writer has no ID")
}
rtr.mu.Lock()
defer rtr.mu.Unlock()
delete(rtr.writers, writer.id)
if len(rtr.writers) == 0 && rtr.conn != nil {
// no attached writers, cleanup and remove connection.
defer rtr.conn.close()
rtr.conn = nil
}
return nil
}
// Picking a connection is easy; there's only one.
func (rtr *simpleRouter) pickConnection(pw *pendingWrite) (*connection, error) {
rtr.mu.RLock()
defer rtr.mu.RUnlock()
if rtr.conn != nil {
return rtr.conn, nil
}
return nil, fmt.Errorf("no connection available")
}
func newSimpleRouter(mode connectionMode) *simpleRouter {
return &simpleRouter{
// We don't add a connection until writers attach.
mode: mode,
writers: make(map[string]struct{}),
}
}
// sharedRouter is a more comprehensive router for a connection pool.
//
// It maintains state for both exclusive and shared connections, but doesn't commingle the
// two. If the router is configured to allow multiplex, it also runs a watchdog goroutine
// that allows is to curate traffic there by reassigning writers to different connections.
//
// Multiplexing routing here is designed for connection sharing among more idle writers,
// and does NOT yet handle the use case where a single writer produces enough traffic to
// warrant fanout across multiple connections.
type sharedRouter struct {
pool *connectionPool
multiplex bool
maxConns int // multiplex limit.
close chan struct{} // for shutting down watchdog
// mu guards access to exclusive connections
mu sync.RWMutex
// keyed by writer ID
exclusiveConns map[string]*connection
// multiMu guards access to multiplex mappings.
multiMu sync.RWMutex
// keyed by writer ID
multiMap map[string]*connection
// keyed by connection ID
invertedMultiMap map[string][]*ManagedStream
multiConns []*connection
}
type connPair struct {
writer *ManagedStream
conn *connection
}
// attaches the router to the connection pool. The watchdog goroutine
// only curates multiplex connections, so we don't start it if the
// router isn't going to process that traffic.
func (sr *sharedRouter) poolAttach(pool *connectionPool) error {
if sr.pool == nil {
sr.pool = pool
sr.close = make(chan struct{})
if sr.multiplex {
go sr.watchdog()
}
return nil
}
return fmt.Errorf("router already attached to pool %q", sr.pool.id)
}
// poolDetach gives us an opportunity to cleanup connections during
// shutdown/close.
func (sr *sharedRouter) poolDetach() error {
sr.mu.Lock()
// cleanup explicit connections
for writerID, conn := range sr.exclusiveConns {
conn.close()
delete(sr.exclusiveConns, writerID)
}
sr.mu.Unlock()
// cleanup multiplex resources
sr.multiMu.Lock()
for _, co := range sr.multiConns {
co.close()
}
sr.multiMap = make(map[string]*connection)
sr.multiConns = nil
close(sr.close) // trigger watchdog shutdown
sr.multiMu.Unlock()
return nil
}
func (sr *sharedRouter) writerAttach(writer *ManagedStream) error {
if writer == nil {
return fmt.Errorf("invalid writer")
}
if writer.id == "" {
return fmt.Errorf("writer has empty ID")
}
if sr.multiplex && canMultiplex(writer.StreamName()) {
return sr.writerAttachMulti(writer)
}
// Handle non-multiplex writer.
sr.mu.Lock()
defer sr.mu.Unlock()
if pair := sr.exclusiveConns[writer.id]; pair != nil {
return fmt.Errorf("writer %q already attached", writer.id)
}
sr.exclusiveConns[writer.id] = newConnection(sr.pool, simplexConnectionMode, writer.streamSettings)
return nil
}
// multiAttach is the multiplex-specific logic for writerAttach.
// It should only be called from writerAttach. We use the same
// orderAndGrow as watchdog, and simply attach the new writer to
// the most idle connection.
func (sr *sharedRouter) writerAttachMulti(writer *ManagedStream) error {
sr.multiMu.Lock()
defer sr.multiMu.Unlock()
// order any existing connections
sr.orderAndGrowMultiConns()
conn := sr.multiConns[0]
sr.multiMap[writer.id] = conn
var writers []*ManagedStream
if w, ok := sr.invertedMultiMap[conn.id]; ok {
writers = append(w, writer)
} else {
// first connection
writers = []*ManagedStream{writer}
}
sr.invertedMultiMap[conn.id] = writers
return nil
}
// orderMultiConns orders the connection slice by current load, and will grow
// the connections if necessary.
//
// Should only be called with R/W lock.
func (sr *sharedRouter) orderAndGrowMultiConns() {
sort.SliceStable(sr.multiConns,
func(i, j int) bool {
return sr.multiConns[i].curLoad() < sr.multiConns[j].curLoad()
})
if len(sr.multiConns) == 0 {
sr.multiConns = []*connection{newConnection(sr.pool, multiplexConnectionMode, nil)}
} else if sr.multiConns[0].isLoaded() && len(sr.multiConns) < sr.maxConns {
sr.multiConns = append([]*connection{newConnection(sr.pool, multiplexConnectionMode, nil)}, sr.multiConns...)
}
}
var (
// Used by rebalanceWriters to avoid rebalancing if the load difference is within the threshold range.
connLoadDeltaThreshold = 1.2
watchDogInterval = 500 * time.Millisecond
)
// rebalanceWriters looks for opportunities to redistribute traffic load.
//
// This is run as part of a heartbeat, when the connections have been ordered
// by load.
//
// Should only be called with the multiplex mutex r/w lock.
func (sr *sharedRouter) rebalanceWriters() {
mostIdleIdx := 0
leastIdleIdx := len(sr.multiConns) - 1
mostIdleConn := sr.multiConns[0]
mostIdleLoad := mostIdleConn.curLoad()
if mostIdleConn.isLoaded() {
// Don't rebalance if all connections are loaded.
return
}
// only look for rebalance opportunies between different connections.
for mostIdleIdx != leastIdleIdx {
targetConn := sr.multiConns[leastIdleIdx]
if targetConn.curLoad() < mostIdleLoad*connLoadDeltaThreshold {
// the load delta isn't significant enough between most and least idle connections
// to warrant moving traffic. Done for this heartbeat.
return
}
candidates, ok := sr.invertedMultiMap[targetConn.id]
if !ok {
leastIdleIdx = leastIdleIdx - 1
continue
}
if len(candidates) == 1 {
leastIdleIdx = leastIdleIdx - 1
continue
}
// Multiple writers, relocate one.
candidate, remaining := candidates[0], candidates[1:]
// update the moved forward map
sr.multiMap[candidate.id] = mostIdleConn
// update the inverse map
sr.invertedMultiMap[targetConn.id] = remaining
idleWriters, ok := sr.invertedMultiMap[mostIdleConn.id]
if ok {
sr.invertedMultiMap[mostIdleConn.id] = append(idleWriters, candidate)
} else {
sr.invertedMultiMap[mostIdleConn.id] = []*ManagedStream{candidate}
}
return
}
}
func (sr *sharedRouter) writerDetach(writer *ManagedStream) error {
if writer == nil {
return fmt.Errorf("invalid writer")
}
if sr.multiplex && canMultiplex(writer.StreamName()) {
return sr.writerDetachMulti(writer)
}
// Handle non-multiplex writer.
sr.mu.Lock()
defer sr.mu.Unlock()
conn := sr.exclusiveConns[writer.id]
if conn == nil {
return fmt.Errorf("writer not currently attached")
}
conn.close()
delete(sr.exclusiveConns, writer.id)
return nil
}
// writerDetachMulti is the multiplex-specific logic for writerDetach.
// It should only be called from writerDetach.
func (sr *sharedRouter) writerDetachMulti(writer *ManagedStream) error {
sr.multiMu.Lock()
defer sr.multiMu.Unlock()
delete(sr.multiMap, writer.id)
// If the number of writers drops to zero, close all open connections.
if len(sr.multiMap) == 0 {
for _, co := range sr.multiConns {
co.close()
}
sr.multiConns = nil
}
return nil
}
// pickConnection either routes a write to a connection for explicit streams,
// or delegates too pickMultiplexConnection for the multiplex case.
func (sr *sharedRouter) pickConnection(pw *pendingWrite) (*connection, error) {
if pw.writer == nil {
return nil, fmt.Errorf("no writer present pending write")
}
if sr.multiplex && canMultiplex(pw.writer.StreamName()) {
return sr.pickMultiplexConnection(pw)
}
sr.mu.RLock()
defer sr.mu.RUnlock()
conn := sr.exclusiveConns[pw.writer.id]
if conn == nil {
return nil, fmt.Errorf("writer %q unknown", pw.writer.id)
}
return conn, nil
}
func (sr *sharedRouter) pickMultiplexConnection(pw *pendingWrite) (*connection, error) {
sr.multiMu.RLock()
defer sr.multiMu.RUnlock()
conn := sr.multiMap[pw.writer.id]
if conn == nil {
// TODO: update map
return nil, fmt.Errorf("no multiplex connection assigned")
}
return conn, nil
}
// watchdog is intended to run as a goroutine where multiplex features are enabled.
//
// Our goals during a heartbeat are simple:
// * ensure we have sufficient connections.
// * ensure traffic from writers is well distributed across connections.
//
// Our rebalancing strategy in this iteration is modest. We order the connections by
// current load, and then examine the busiest connection(s) looking for opportunities
// to redistribute traffic.
func (sr *sharedRouter) watchdog() {
for {
select {
case <-sr.close:
return
case <-time.After(watchDogInterval):
sr.watchdogPulse()
}
}
}
// an individual pulse of the watchdog loop.
func (sr *sharedRouter) watchdogPulse() {
sr.multiMu.Lock()
defer sr.multiMu.Unlock()
sr.orderAndGrowMultiConns()
sr.rebalanceWriters()
}
func newSharedRouter(multiplex bool, maxConns int) *sharedRouter {
return &sharedRouter{
multiplex: multiplex,
maxConns: maxConns,
exclusiveConns: make(map[string]*connection),
multiMap: make(map[string]*connection),
invertedMultiMap: make(map[string][]*ManagedStream),
}
}