-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.go
197 lines (176 loc) · 4.65 KB
/
worker.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
package gobearmon
import "bufio"
import "encoding/json"
import "log"
import "net"
import "strings"
import "sync"
import "time"
type Worker struct {
ViewAddr string
Controller *Controller
NumThreads int
mu sync.Mutex
activeController string
availableWorkers map[int]chan CheckId
pendingResults map[CheckId]*CheckResult
}
func (this *Worker) Start() {
this.availableWorkers = make(map[int]chan CheckId)
this.pendingResults = make(map[CheckId]*CheckResult)
go this.updateController()
go this.updateView()
for i := 0; i < this.NumThreads; i++ {
go this.task(i)
}
}
func (this *Worker) updateController() {
var currentController string
var conn net.Conn
var in *bufio.Reader
for {
if conn == nil {
currentController = this.getActiveController()
if currentController != "" {
log.Printf("worker: connecting to controller at %s", currentController)
var err error
conn, err = net.DialTimeout("tcp", currentController, 10 * time.Second)
if err != nil {
log.Printf("worker: connect error: %s", err.Error())
time.Sleep(10 * time.Second)
continue
}
conn.Write([]byte(cfg.Default.Password + "\n"))
in = bufio.NewReader(conn)
} else {
time.Sleep(5 * time.Second)
continue
}
} else {
activeController := this.getActiveController()
if currentController != activeController {
log.Printf("worker: controller changed from %s to %s, disconnecting", currentController, activeController)
conn.Close()
conn = nil
continue
}
}
// do controller request
request := MakeControllerRequest()
this.mu.Lock()
request.Count = len(this.availableWorkers)
for checkId, checkResult := range this.pendingResults {
request.Results[checkId] = checkResult
}
this.mu.Unlock()
requestBytes, err := json.Marshal(request)
if err != nil {
panic(err)
}
conn.Write([]byte(string(requestBytes) + "\n"))
// decode response
conn.SetReadDeadline(time.Now().Add(5 * time.Second))
line, err := in.ReadString('\n')
if err != nil {
log.Printf("worker: controller disconnected: %s", err.Error())
conn.Close()
conn = nil
time.Sleep(10 * time.Second)
continue
}
var response ControllerResponse
err = json.Unmarshal([]byte(strings.TrimSpace(line)), &response)
if err != nil {
log.Printf("worker: controller json error: %s", err.Error())
conn.Close()
conn = nil
time.Sleep(10 * time.Second)
continue
}
// process response
this.mu.Lock()
for checkId := range request.Results {
delete(this.pendingResults, checkId)
}
idx := 0
for workerId, ch := range this.availableWorkers {
if idx >= len(response.Checks) {
break
}
ch <- response.Checks[idx]
delete(this.availableWorkers, workerId)
idx++
}
if idx != len(response.Checks) {
log.Println("worker: warning: got more checks than able to distribute!")
}
this.mu.Unlock()
time.Sleep(2 * time.Second)
}
}
func (this *Worker) getActiveController() string {
this.mu.Lock()
defer this.mu.Unlock()
return this.activeController
}
func (this *Worker) setActiveController(controller string) {
this.mu.Lock()
defer this.mu.Unlock()
if this.activeController != controller {
log.Printf("worker: updating controller from %s to %s", this.activeController, controller)
this.activeController = controller
}
}
func (this *Worker) updateView() {
var conn net.Conn
var in *bufio.Reader
for {
if conn == nil {
log.Printf("worker: connecting to viewserver at %s", this.ViewAddr)
var err error
conn, err = net.DialTimeout("tcp", this.ViewAddr, 10 * time.Second)
if err != nil {
log.Printf("worker: viewserver connect error: %s", err.Error())
time.Sleep(10 * time.Second)
continue
}
conn.Write([]byte(cfg.Default.Password + "\n"))
in = bufio.NewReader(conn)
}
conn.Write([]byte("request\n"))
conn.SetReadDeadline(time.Now().Add(5 * time.Second))
line, err := in.ReadString('\n')
if err != nil {
log.Printf("worker: viewserver disconnected: %s", err.Error())
conn.Close()
conn = nil
time.Sleep(10 * time.Second)
continue
}
this.setActiveController(strings.TrimSpace(line))
time.Sleep(10 * time.Second)
}
}
func (this *Worker) task(workerId int) {
ch := make(chan CheckId)
for {
this.mu.Lock()
this.availableWorkers[workerId] = ch
this.mu.Unlock()
checkId := <- ch
check := this.Controller.GetCheck(checkId)
var result *CheckResult
if check == nil {
result = &CheckResult{
Status: StatusFail,
Message: "check does not exist",
}
log.Printf("assigned check id=%d, but check not found in local store", checkId)
} else {
result = DoCheck(check)
}
this.mu.Lock()
this.pendingResults[checkId] = result
this.mu.Unlock()
}
}