-
Notifications
You must be signed in to change notification settings - Fork 1
/
peer_list_mgr.go
183 lines (162 loc) · 5.06 KB
/
peer_list_mgr.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
// Copyright (c) 2017 Uber Technologies, 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
//
// 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 peerlistmgr
import (
"context"
"math/rand"
"sync"
"time"
"github.com/pkg/errors"
"github.com/uber/tchannel-go"
"go.uber.org/zap"
"github.com/jaegertracing/jaeger/pkg/discovery"
)
var introspectOptions = &tchannel.IntrospectionOptions{IncludeEmptyPeers: true}
// PeerListManager uses a discovery.Notifier to manage tchannel.PeerList
// by making sure that there are connections to at least minPeers.
type PeerListManager struct {
peers *tchannel.PeerList
discoverer discovery.Discoverer // used for initial seed of the peers
notifier discovery.Notifier
options
rnd *rand.Rand
discoCh chan []string // used to receive notifications
stopCh chan struct{} // used to break out of timer loop that maintains connections
exitWG sync.WaitGroup // used to block Stop() until go-routines have exited
}
// New creates new PeerListManager.
func New(
peerList *tchannel.PeerList,
discoverer discovery.Discoverer,
notifier discovery.Notifier,
opts ...Option,
) (*PeerListManager, error) {
options := Options.apply(opts...)
mgr := &PeerListManager{
options: options,
peers: peerList,
discoverer: discoverer,
notifier: notifier,
discoCh: make(chan []string, 100),
stopCh: make(chan struct{}),
rnd: rand.New(rand.NewSource(time.Now().UTC().UnixNano())),
}
instances, err := discoverer.Instances()
if err != nil {
return nil, errors.Wrap(err, "cannot get initial set of instances")
}
mgr.updatePeers(instances)
mgr.exitWG.Add(2)
go mgr.processDiscoveryNotifications()
go mgr.maintainConnections()
notifier.Register(mgr.discoCh)
return mgr, nil
}
// Stop shuts down the manager. It blocks until both bg go-routines exit.
func (m *PeerListManager) Stop() {
m.notifier.Unregister(m.discoCh)
close(m.discoCh)
close(m.stopCh)
m.exitWG.Wait()
}
func (m *PeerListManager) processDiscoveryNotifications() {
defer m.exitWG.Done()
for instances := range m.discoCh {
m.updatePeers(instances)
}
}
func (m *PeerListManager) maintainConnections() {
defer m.exitWG.Done()
ticker := time.NewTicker(m.connCheckFrequency)
defer ticker.Stop()
for {
select {
case <-ticker.C:
m.ensureConnections()
case <-m.stopCh:
return
}
}
}
func (m *PeerListManager) updatePeers(instances []string) {
current := make(map[string]struct{})
for _, addr := range instances {
m.logger.Info("Registering active peer", zap.String("peer", addr))
m.peers.Add(addr)
current[addr] = struct{}{}
}
toRemove := []string{}
for _, existing := range m.peers.IntrospectList(introspectOptions) {
if _, ok := current[existing.HostPort]; !ok {
toRemove = append(toRemove, existing.HostPort)
}
}
for _, existing := range toRemove {
m.logger.Info("Removing inactive peer", zap.String("peer", existing))
m.peers.Remove(existing)
}
}
func (m *PeerListManager) getMinPeers(peers map[string]*tchannel.Peer) int {
minPeers := m.minPeers
if l := len(peers); l < minPeers {
minPeers = l
}
return minPeers
}
func (m *PeerListManager) findConnected(peers map[string]*tchannel.Peer) (connected int, noConn []*tchannel.Peer) {
notConnected := make([]*tchannel.Peer, 0, len(peers))
numConnected := 0
for _, peer := range peers {
_, out := peer.NumConnections()
if out > 0 {
numConnected++
} else {
notConnected = append(notConnected, peer)
}
}
return numConnected, notConnected
}
func (m *PeerListManager) ensureConnections() {
peers := m.peers.Copy()
minPeers := m.getMinPeers(peers)
numConnected, notConnected := m.findConnected(peers)
if numConnected >= minPeers {
return
}
m.logger.Info("Not enough connected peers",
zap.Int("connected", numConnected),
zap.Int("required", minPeers))
for i := range notConnected {
// swap current peer with random from the remaining positions
r := i + m.rnd.Intn(len(notConnected)-i)
notConnected[i], notConnected[r] = notConnected[r], notConnected[i]
// try to connect to current peer (swapped)
peer := notConnected[i]
m.logger.Info("Trying to connect to peer", zap.String("host:port", peer.HostPort()))
ctx, cancel := context.WithTimeout(context.Background(), m.connCheckTimeout)
conn, err := peer.GetConnection(ctx)
cancel()
if err != nil {
m.logger.Error("Unable to connect", zap.String("host:port", peer.HostPort()), zap.Duration("connCheckTimeout", m.connCheckTimeout), zap.Error(err))
continue
}
if conn.IsActive() {
m.logger.Info("Connected to peer", zap.String("host:port", conn.RemotePeerInfo().HostPort))
numConnected++
if numConnected >= minPeers {
return
}
}
}
}