forked from appscode/g2
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathagent.go
296 lines (249 loc) · 6.77 KB
/
agent.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
package worker
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"unsafe"
rt "github.com/quantcast/g2/pkg/runtime"
)
// The agent of job server.
type agent struct {
sync.Mutex
reconnectState uint32
conn net.Conn
connectionVersion uint32
rw *bufio.ReadWriter
worker *Worker
in chan []byte
net, addr string
}
// Create the agent of job server.
func newAgent(net, addr string, worker *Worker) (a *agent, err error) {
a = &agent{
net: net,
addr: addr,
worker: worker,
in: make(chan []byte, rt.QueueSize),
}
return
}
func (a *agent) loadRw() *bufio.ReadWriter {
return (*bufio.ReadWriter)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&a.rw))))
}
func (a *agent) work() {
a.worker.Log(Info, "Starting agent Work For:", a.addr)
defer func() {
if err := safeCastError(recover(), "panic in work()"); err != nil {
a.reconnectError(err)
}
}()
var inpack *inPack
var l int
var err error
var data, leftdata []byte
startRw := a.loadRw()
// exit the loop if connection has been replaced because reconnect will launch a new work() thread
for startRw == a.loadRw() && !a.worker.isShuttingDown() {
if data, err = a.read(); err != nil {
if opErr, ok := err.(*net.OpError); ok {
if opErr.Temporary() {
a.worker.Log(Info, "opErr.Temporary():", a.addr)
continue
} else {
a.worker.Log(Info, "got permanent network error with server:", a.addr, "comm thread exiting.")
a.reconnectError(err)
// else - we're probably dc'ing due to a Close()
break
}
} else {
a.worker.Log(Info, "got error", err.Error(), "with server:", a.addr, "comm thread exiting...")
a.reconnectError(err)
break
}
}
if len(leftdata) > 0 { // some data left for processing
data = append(leftdata, data...)
}
if len(data) < rt.MinPacketLength { // not enough data
leftdata = data
continue
}
for {
if inpack, l, err = decodeInPack(data); err != nil {
a.reconnectError(err)
break
} else {
leftdata = nil
inpack.a = a
a.worker.in <- inpack
if len(data) == l {
break
}
if len(data) > l {
data = data[l:]
}
}
}
}
}
func (a *agent) reconnectError(err error) {
if a.conn != nil {
err = &WorkerDisconnectError{
err: err,
agent: a,
}
a.worker.err(err)
}
a.Connect()
}
func (a *agent) Close() {
if a.conn == nil {
return
}
a.Lock()
defer a.Unlock()
if a.conn != nil {
_ = a.conn.Close()
a.conn = nil
}
}
func (a *agent) Grab() (err error) {
if a.conn == nil {
return errors.New("No connection")
}
return a.grab()
}
func (a *agent) grab() error {
outpack := getOutPack()
outpack.dataType = rt.PT_GrabJobUniq
return a.Write(outpack)
}
func (a *agent) PreSleep() (err error) {
if a.conn == nil {
return errors.New("No connection")
}
outpack := getOutPack()
outpack.dataType = rt.PT_PreSleep
return a.Write(outpack)
}
func (a *agent) lockReconnect() (success bool) {
return atomic.CompareAndSwapUint32(&a.reconnectState, 0, 1)
}
// called by owner of reconnect state to tell that it has finished reconnecting
func (a *agent) resetReconnectState() {
atomic.StoreUint32(&a.reconnectState, 0)
}
func (a *agent) Connect() {
ownReconnect := a.lockReconnect()
if !ownReconnect {
//Reconnect collision, this thread will exit and wait on next a.Lock() for other to complete reconnection
return
}
defer a.resetReconnectState() // before releasing client lock we will reset reconnection state
a.worker.Log(Info, "Trying to Connect to server:", a.addr, "...")
var conn net.Conn
var err error
for !a.worker.isShuttingDown() {
for numTries := 1; !a.worker.isShuttingDown(); numTries++ {
if a.conn != nil {
_ = a.conn.Close()
a.conn = nil
}
// nil-out the rw pointer since it's no longer valid
_ = atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&a.rw)), nil)
if numTries%100 == 0 {
a.worker.Log(Info, fmt.Sprintf("Still trying to connect to server %v, attempt# %v ...", a.addr, numTries))
}
conn, err = net.Dial(a.net, a.addr)
if err != nil {
time.Sleep(500 * time.Millisecond)
continue
}
break
}
if conn == nil {
// in case worker is shutting down
break
}
// at this point the server is back online, we will disconnect and reconnect again to make sure that we don't have
// one of those dud connections which could happen if we've reconnected to gearman too quickly after it started
_ = conn.Close()
time.Sleep(3 * time.Second)
// todo: come up with a more reliable way to determine if we have a working connection to gearman, pehaps by performing a test
conn, err = net.Dial(a.net, a.addr)
if err != nil {
// looks like there is another problem, go back to the main loop
time.Sleep(time.Second)
continue
}
a.conn = conn
a.connectionVersion++
a.worker.Log(Info, "Successfully Connected to:", a.addr)
newRw := bufio.NewReadWriter(bufio.NewReader(a.conn), bufio.NewWriter(a.conn))
if swapped := atomic.CompareAndSwapPointer(
(*unsafe.Pointer)(unsafe.Pointer(&a.rw)),
unsafe.Pointer(nil), unsafe.Pointer(newRw)); !swapped {
a.worker.Log(Warning, fmt.Sprintf("Was expecting nil when replacing with new ReadWriter, server: %v", a.addr))
}
if err := a.worker.reRegisterFuncsForAgent(a); err != nil {
a.worker.Log(Error, fmt.Sprintf("Failed to register funcs for agent, error=%v, will reconnect...", err))
continue
}
if err := a.grab(); err != nil {
a.worker.Log(Error, fmt.Sprintf("Failed to request a new job assignment, error=%v, will reconnect", err))
continue
}
// only threads are a.work() and a.Work(),
// a.work() is exited when connectionVersion is incremented
// a.Work() does not exit because it uses an anonymous function to process writes
go a.work()
break
}
return
}
// read length bytes from the socket
func (a *agent) read() (data []byte, err error) {
n := 0
tmp := rt.NewBuffer(rt.BufferSize)
var buf bytes.Buffer
myRw := a.loadRw()
// read the header so we can get the length of the data
if n, err = myRw.Read(tmp); err != nil {
return
}
dl := int(binary.BigEndian.Uint32(tmp[8:12]))
// write what we read so far
buf.Write(tmp[:n])
// read until we receive all the data
for buf.Len() < dl+rt.MinPacketLength {
if n, err = myRw.Read(tmp); err != nil {
return buf.Bytes(), err
}
buf.Write(tmp[:n])
}
return buf.Bytes(), err
}
// Internal write the encoded job.
func (a *agent) Write(outpack *outPack) (err error) {
myRw := a.loadRw()
if myRw == nil {
return errors.New("Reconnect is active, discarding the response")
}
a.Lock()
defer a.Unlock()
var n int
buf := outpack.Encode()
for i := 0; i < len(buf); i += n {
n, err = myRw.Write(buf[i:])
if err != nil {
return err
}
}
return myRw.Flush()
}