-
Notifications
You must be signed in to change notification settings - Fork 0
/
targetgroup.go
272 lines (227 loc) · 6.87 KB
/
targetgroup.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
package tinylb
import (
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/buraksezer/consistent"
"github.com/cespare/xxhash"
"github.com/sirupsen/logrus"
)
// TargetGroup listens for incoming connections on a specific port, assigning each
// new connection to a healthy target from the configured group of targets. TargetGroups
// use consistent hashing based on the incoming connection's source address to assign
// connections to targets, so the same client will always be routed to the same target
// as long as that target is healthy and present in the target group.
type TargetGroup struct {
sync.RWMutex
TargetGroupConfig
// listener receives incoming TCP connections for this target.
listener *net.TCPListener
// targets is a map of all configured targets keyed by each target's address.
targets map[string]*Target
// targetRing is a hash ring containing only healthy targets.
targetRing *consistent.Consistent
// stop signals that all long-running goroutines for this target group should stop.
stop bool
// closed signals that the target group is no longer listening for new connections.
closed chan bool
log *logrus.Entry
}
// getTCPListener creates a net.TCPListener listening on `port`.
func getTCPListener(port uint) (*net.TCPListener, error) {
laddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:%d", port))
if err != nil {
return nil, err
}
listener, err := net.ListenTCP("tcp", laddr)
if err != nil {
return nil, err
}
return listener, nil
}
// hasher is used by the `consistent` package to assign ring members to partitions
// and keys to members.
type hasher struct{}
func (h hasher) Sum64(data []byte) uint64 {
return xxhash.Sum64(data)
}
// openTargetGroup creates a new target group with the provided configuration.
func openTargetGroup(config TargetGroupConfig, log *logrus.Logger) (*TargetGroup, error) {
listener, err := getTCPListener(config.Port)
if err != nil {
return nil, err
}
targetRing := consistent.New(nil, consistent.Config{
PartitionCount: 71,
ReplicationFactor: 20,
Load: 1.25,
Hasher: hasher{},
})
target := &TargetGroup{
TargetGroupConfig: config,
listener: listener,
targets: map[string]*Target{},
targetRing: targetRing,
stop: false,
closed: make(chan bool),
log: log.WithFields(logrus.Fields{
"target_group": config.Name,
"target_group_port": config.Port,
}),
}
if err := target.updateConfig(config); err != nil {
return nil, err
}
go target.listen()
go target.healthChecks()
return target, nil
}
// drain gracefully shuts down this target group and its targets, waiting
// for all existing connections to close.
func (tg *TargetGroup) drain() {
defer tg.listener.Close()
tg.log.Info("draining")
if tg.shouldStop() {
tg.log.Warn("already drained - noop")
return
}
tg.Lock()
tg.stop = true
tg.Unlock()
// Wait for listener to stop accepting connections
<-tg.closed
// Drain all targets
var wg sync.WaitGroup
for _, target := range tg.targets {
wg.Add(1)
func(t *Target) {
t.drain()
wg.Done()
}(target)
}
wg.Wait()
tg.log.Info("done draining")
}
// updateConfig applies the given config to this target group without downtime for
// existing or new connections.
func (tg *TargetGroup) updateConfig(config TargetGroupConfig) error {
tg.Lock()
defer tg.Unlock()
tg.log.Info("updating config")
// Don't allow on-the-fly target group port updates.
if tg.Port != config.Port {
return errors.New("cannot update target group port on-the-fly")
}
// Create new targets and keep track of them in a map for diffing with
// the existing targets below.
newTargets := make(map[string]*Target, len(config.Targets))
for _, targetConfig := range config.Targets {
target, err := newTarget(targetConfig, tg.Timeout, tg.log)
if err != nil {
return err
}
tg.targets[target.addr.String()] = target
newTargets[target.addr.String()] = target
}
// Drain targets no longer present in the config.
var drainWg sync.WaitGroup
remove := make([]string, len(tg.targets))
for addr, target := range tg.targets {
if _, ok := newTargets[addr]; !ok {
drainWg.Add(1)
go func(t *Target) {
t.drain()
drainWg.Done()
}(target)
remove = append(remove, addr)
}
}
// Overwrite the target group's config.
tg.TargetGroupConfig = config
// Remove drained targets.
drainWg.Wait()
for _, addr := range remove {
delete(tg.targets, addr)
tg.targetRing.Remove(addr)
}
tg.log.Info("done updating config")
return nil
}
func (tg *TargetGroup) shouldStop() bool {
tg.RLock()
defer tg.RUnlock()
return tg.stop
}
// listen listens for new TCP connections for this target group and proxies
// them to a healthy target.
func (tg *TargetGroup) listen() {
defer tg.listener.Close()
for !tg.shouldStop() {
conn, err := tg.pollConnection()
if err != nil {
// Only log non-timeout errors, since timeouts are expected.
if err, ok := err.(net.Error); !(ok && err.Timeout()) {
tg.log.Error("error accepting connection: ", err)
}
continue
}
target, err := tg.getTarget(conn)
if err != nil {
tg.log.Error("error getting target: ", err)
conn.Close()
continue
}
go target.proxy(conn)
}
// Signal that no more connections will be accepted
tg.closed <- true
}
// pollConnection waits for a new TCP connection to this target group's listener,
// timing out after 1 second if no new connection arrives.
func (tg *TargetGroup) pollConnection() (*net.TCPConn, error) {
tg.RLock()
defer tg.RUnlock()
tg.listener.SetDeadline(time.Now().Add(1 * time.Second))
return tg.listener.AcceptTCP()
}
// getTarget selects a healthy target for the given connection from this
// target group's hash ring. If no targets are healthy, it errors.
func (tg *TargetGroup) getTarget(conn *net.TCPConn) (*Target, error) {
tg.RLock()
defer tg.RUnlock()
if len(tg.targetRing.GetMembers()) == 0 {
return nil, errors.New("no healthy targets")
}
key := []byte(conn.RemoteAddr().String())
addr := tg.targetRing.LocateKey(key)
return tg.targets[addr.String()], nil
}
// healthChecks cycles through the target group's targets, checking
// each targets's health every 1 second. If a target is healthy,
// it's added to the target group's hash ring, and if it's unhealthy, it's
// removed.
func (tg *TargetGroup) healthChecks() {
tg.log.Info("started healthchecks")
for !tg.shouldStop() {
tg.RLock()
targets := tg.targets
tg.RUnlock()
for addr, target := range targets {
healthy := target.healthy()
// Only add a target to the hash ring if it's healthy *and* still
// present in the target group config, since this config may have
// changed concurrently. Otherwise, remove it.
tg.Lock()
if _, ok := tg.targets[addr]; ok && healthy {
tg.targetRing.Add(target.addr)
} else {
tg.targetRing.Remove(addr)
}
tg.Unlock()
}
time.Sleep(1 * time.Second)
}
tg.log.Info("stopped healthchecks")
}