/
server.go
301 lines (240 loc) · 7.41 KB
/
server.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
// This defines the methods used by the central compilation server.
//
// Author: Joseph Lisee <jlisee@gmail.com>
package cbd
import (
"log"
"net"
"reflect"
"sort"
"time"
)
// MonitorRequest is sent from a client that wishes to be sent information
// about the current jobs running on the build cluster.
type MonitorRequest struct {
Host string
}
// WorkerRequest is sent from the client to the server in order to find
// a worker to process a job
type WorkerRequest struct {
Client string // Host request a worker
Addrs []net.IPNet // IP addresses of the client
}
// Determine what kind of response the server sent
type ResponseType int
const (
Queued ResponseType = iota // No data, we are queued
NoWorkers // No workers at all available
Valid // Valid response
)
type WorkerResponse struct {
Type ResponseType // Valid or queue
ID MachineID // Uniquely identifies machine
Host string // Host of the worker (for debugging purposes)
Address net.IPNet // IP address of the worker
Port int // Port the workers accepts connections on
}
// WorkState represents the load and capacity of a worker
type WorkerState struct {
ID MachineID // Uniquely id for the worker machine
Host string // Host the worker resides one
Addrs []net.IPNet // IP addresses of the worker
Port int // Port the worker accepts jobs on
Capacity int // Number of available cores for building
Load int // How many cores are current in use
Updated time.Time // When the state was last updated
Speed float64 // The speed of the worker, computed on the server
}
// List of all currently active works
type WorkerStateList struct {
Workers []WorkerState
}
// ServerState is all the state of our server
// TODO: consider some kind of channel system instead of a mutex to get
// sync access to these data structures.
type ServerState struct {
sch Scheduler // Schedules jobs
monitorUpdates *updatePublisher // Sends to multiple channels completion information
}
func NewServerState() *ServerState {
s := new(ServerState)
s.sch = newFifoScheduler()
s.monitorUpdates = newUpdatePublisher()
return s
}
// server accepts incoming connections
func (s *ServerState) Serve(ln net.Listener) {
// Start sending worker updates at 1Hz
go s.sendWorkState(1)
// Start up our auto discover server
var a *discoveryServer
addr := ln.Addr()
if taddr, ok := addr.(*net.TCPAddr); ok {
var err error
a, err = newDiscoveryServer(taddr.Port)
if err != nil {
log.Print("Error starting auto-discovery", err)
return
}
defer a.stop()
}
// Incoming connections
for {
DebugPrint("Server accepting...")
conn, err := ln.Accept()
if err != nil {
log.Print(err)
continue
}
// Turn into a message conn
mc := NewMessageConn(conn, time.Duration(10)*time.Second)
// Spin off thread to handle the new connection
go s.handleConnection(mc)
}
}
// updateWorker updates the worker with the currene state
func (s *ServerState) updateWorker(u WorkerState) {
// Update time so that we don't have to worry worker clock being
// in sync with our local clock
u.Updated = time.Now()
// Sort the IP addresses, so the most local ones are first, and we can
// more easily find matching ones in the future
sort.Sort(ByPrivateIPAddr(u.Addrs))
// Tell the scheduler about the worker
s.sch.updateWorker(u)
}
// Remove the worker from the current set of workers
func (s *ServerState) removeWorker(id MachineID) {
// Have the scheduler remove the worker
s.sch.removeWorker(id)
}
// func (s*ServerState) pruneStaleWorkers(h string)
// handleMessage decodes incoming messages
func (s *ServerState) handleConnection(conn *MessageConn) {
// Read the first message on the connection
_, msg, err := conn.Read()
if err != nil {
log.Print("Message reader error: ", err)
return
}
// Hand the message off to the proper function
switch m := msg.(type) {
case WorkerRequest:
err = s.processWorkerRequest(conn, m)
case WorkerState:
// Push update and then start continously handling the worker connection
s.updateWorker(m)
s.handleWorkerConnection(conn, m)
case MonitorRequest:
// Create and register channel which receives information
u := make(chan interface{})
s.monitorUpdates.addObs(m.Host, u)
// Step into our routine which shuffles messages from that channel into
// the provided connection
// TODO: a better identifier for this
s.handleMonitorConnection(conn, m.Host, u)
case CompletedJob:
err = s.updateStats(m)
if err != nil {
log.Print("Error updating stats: ", err)
}
s.monitorUpdates.updates <- m
default:
log.Print("Un-handled message type: ", reflect.TypeOf(msg).Name())
}
if err != nil {
log.Printf("Request(%s) error: %s", reflect.TypeOf(msg).Name(),
err.Error())
}
}
// handleWorkerConnection continously grabs updates from one worker
// and sends updates the server state
func (s *ServerState) handleWorkerConnection(conn *MessageConn, is WorkerState) {
for {
ws, err := conn.ReadWorkerState()
if err != nil {
// Drop missing worker
s.removeWorker(is.ID)
log.Print("Error reading worker state: ", err)
break
}
s.updateWorker(ws)
}
}
// handleMonitorConnection sends completed job information to any requested
func (s *ServerState) handleMonitorConnection(conn *MessageConn, h string, cin chan interface{}) {
for j := range cin {
err := conn.Send(j)
// On an error we de-register and bail out
if err != nil {
log.Printf("Dropping monitor: %s Error: %s", h, err.Error())
s.monitorUpdates.removeObs(h)
break
}
}
}
// processWorkerRequest searches for an available worker and sends the
// result back on the given connection.
func (s *ServerState) processWorkerRequest(conn *MessageConn, req WorkerRequest) error {
// Create a go routine waiting for our scheduling result
sreq := NewSchedulerRequest(req.Addrs)
errOut := make(chan error)
go func() {
var err error
// Keep telling the waiting client we are queued
Loop:
for {
// 1 second timeout
timeout := make(chan bool, 1)
go func() {
time.Sleep(1 * time.Second)
timeout <- true
}()
// Wait for timeouts, or requests
select {
case result := <-sreq.r:
// We got a result!, send it to the user
err = conn.Send(result)
// If it's valid break out of our loop
if result.Type == Valid {
break Loop
}
case <-timeout:
// the read from ch has timed, tell the user we have a queue
// result
err = conn.Send(WorkerResponse{Type: Queued})
// Cancel the request and leave the loop
if err != nil {
cerr := s.sch.cancel(sreq.guid)
if cerr != nil {
DebugPrintf("Error canceling request %s: %s", sreq, cerr)
}
break Loop
}
}
}
errOut <- err
}()
// Schedule our request
s.sch.schedule(sreq)
// Wait for the scheduler to respond, and the message to send
return <-errOut
}
// Sends worker state to all monitoring programs
func (s *ServerState) sendWorkState(rate float64) error {
// Define sleep based our rate
msSleep := 1 / rate * 1000
d := time.Duration(int64(msSleep)) * time.Millisecond
for {
time.Sleep(d)
// Copy list into message
// TODO: maybe reduce copying here?
l := s.sch.getWorkerState()
// Send out update
s.monitorUpdates.updates <- l
}
}
// Updates scheduler state based on completed job information
func (s *ServerState) updateStats(cj CompletedJob) error {
return s.sch.completed(cj)
}