-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatushub.go
282 lines (235 loc) · 6.21 KB
/
statushub.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
package main
import (
"fmt"
"log"
"net"
"time"
)
type Status struct {
Name string `json:"name"`
Names []string `json:"names"`
Groups []string `json:"groups"`
IP string `json:"ip"`
UUID string `json:"uuid"`
Version string `json:"version"`
Queries int64 `json:"queries"`
Qps float64 `json:"qps"`
Qps1 float64 `json:"qps1m"`
Uptime int64 `json:"uptime"`
Status string `json:"status"`
LastStatusUpdate time.Time `json:"-"`
Connection *ServerConnection
Data ServerUpdate
}
type statusMap map[int]*Status
type StatusHub struct {
statusUpdates chan *ServerUpdate
statusMsgChan chan *ServerStatusMsg
addServerChan chan net.IP
nextServerID chan int
serverStatus statusMap
statuses chan statusMap
remove chan string
quit chan bool
configRevision int
configManager chan bool
}
func NewHub() *StatusHub {
hub := new(StatusHub)
hub.statusUpdates = make(chan *ServerUpdate, 10)
hub.statusMsgChan = make(chan *ServerStatusMsg, 10)
hub.addServerChan = make(chan net.IP)
hub.statuses = make(chan statusMap)
hub.quit = make(chan bool, 1)
hub.serverStatus = make(statusMap)
hub.nextServerID = make(chan int)
hub.configManager = make(chan bool)
go hub.makeServerID()
go hub.arbiter()
return hub
}
func (s *StatusHub) MarkConfigurationStart() {
s.configManager <- false
}
func (s *StatusHub) MarkConfigurationEnd() {
s.configManager <- true
}
func (s *StatusHub) makeServerID() int {
i := 1
for {
log.Println("Ready to make server id", i)
s.nextServerID <- i
i++
}
}
func (s *StatusHub) arbiter() {
log.Println("running arbiter")
for {
select {
case new := <-s.statusUpdates:
// log.Println("Adding status for", new.IP)
srv, ok := s.serverStatus[new.ConnID]
if ok {
if len(new.UUID) > 0 {
if dupeID := s.FindUUID(new.UUID); dupeID > 0 && dupeID != new.ConnID {
log.Printf("Duplicate connection to %s (uuid %s); this is %d, dupe is %d", new.IP, new.UUID, new.ConnID, dupeID)
// try keeping the connection that's the one reported by the server
if srv.Connection.IP.String() != new.IP {
dupeID = new.ConnID
}
s.serverStatus[dupeID].Connection.Stop()
delete(s.serverStatus, dupeID)
continue
}
}
updateStatus(srv, new)
} else {
log.Printf("got status update for unknown connection %d (ip %s)", new.ConnID, new.IP)
}
// TODO: push to seriesly
case msg := <-s.statusMsgChan:
// log.Printf("Got StatusMsg from '%d': %s\n", msg.ConnID, msg.Status)
srv, ok := s.serverStatus[msg.ConnID]
if ok {
srv.Status = msg.Status
}
case s.statuses <- s.serverStatus:
case cm := <-s.configManager:
switch cm {
case false:
s.configRevision++
case true:
for connID, srv := range s.serverStatus {
if srv.Connection.configRevision < s.configRevision {
log.Printf("Server %s has an old config revision, disconnecting %d", srv.IP, connID)
srv.Connection.Stop()
delete(s.serverStatus, connID)
}
}
}
case ip := <-s.addServerChan:
log.Println("Adding monitoring of", ip)
foundDuplicate := false
for _, server := range s.serverStatus {
if server.IP == ip.String() {
foundDuplicate = true
log.Printf("Already monitoring '%s'\n", ip.String())
server.Connection.configRevision = s.configRevision
break
}
}
if foundDuplicate {
continue
}
log.Printf("Creating new connection for %s", ip)
sc := NewServerConnection(ip, s.statusUpdates, s.statusMsgChan)
sc.configRevision = s.configRevision
log.Printf("Start() on %s", sc.IP)
connID := <-s.nextServerID
log.Println("got server id", connID)
status := new(Status)
status.IP = ip.String()
status.Connection = sc
s.serverStatus[connID] = status
sc.Start(connID)
case <-s.quit:
log.Printf("StatusHub got quit!\n")
for connID, srv := range s.serverStatus {
log.Printf("Sending quit to %d (%s)\n", connID, srv.IP)
srv.Connection.Stop()
delete(s.serverStatus, connID)
}
// TODO: do we need to close the channels?
log.Println("Arbiter done")
return
}
}
}
func (s *StatusHub) FindUUID(UUID string) int {
for connID, server := range s.serverStatus {
if server.UUID == UUID {
return connID
}
}
return 0
}
func updateStatus(srv *Status, new *ServerUpdate) {
srv.Data = *new
srv.LastStatusUpdate = time.Now()
if len(new.Version) > 0 {
srv.Version = new.Version
}
if len(new.ID) > 0 {
srv.Name = new.ID
}
if len(new.UUID) > 0 {
srv.UUID = new.UUID
}
if new.Uptime > 0 {
srv.Uptime = new.Uptime
}
srv.Qps = new.Qps
srv.Queries = new.Queries
if new.Qps1 > 0 {
srv.Qps1 = new.Qps1
}
if len(new.Hostname) > 0 {
// This needs to accumulate the various names that have been
// discovered for this server, maybe.
srv.Names = []string{new.Hostname}
}
if len(new.Groups) > 0 {
srv.Groups = new.Groups
}
}
func (s *StatusHub) Status() []*Status {
current := <-s.statuses
rv := make([]*Status, 0)
for _, status := range current {
// log.Printf("Status for '%name': %#v\n", name, status)
if !status.LastStatusUpdate.IsZero() || len(status.Status) > 0 {
rv = append(rv, status)
}
}
return rv
}
func (s *StatusHub) Stop() {
s.quit <- true
}
func (s *StatusHub) addIP(ip net.IP) error {
s.addServerChan <- ip
return nil
}
func (s *StatusHub) AddNameBackground(ipstr string, ch chan error) {
go func() {
err := s.AddName(ipstr)
if err == nil {
ch <- err
} else {
ch <- fmt.Errorf("error adding server '%s': %s", ipstr, err)
}
}()
}
func (s *StatusHub) AddName(ipstr string) error {
ip := net.ParseIP(ipstr)
if ip != nil {
return s.addIP(ip)
}
// return fmt.Errorf("Could not parse IP: '%s'", ipstr)
addrs, err := net.LookupIP(ipstr)
log.Printf("IP: %s, %#v %d\n", ipstr, addrs, len(addrs))
if err != nil || len(addrs) == 0 {
return fmt.Errorf("Could not lookup name: '%s': %s", ipstr, err)
}
if false {
return fmt.Errorf("Could not find IPs for: '%s'\n", ipstr)
}
for _, addr := range addrs {
log.Println("Adding", addr)
err = s.addIP(addr)
if err != nil {
log.Printf("Could not add '%s': %s\n", addr, err)
}
}
return nil
}