-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_machine.go
427 lines (351 loc) · 11.1 KB
/
remote_machine.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package quacktors
import (
"bytes"
"errors"
"fmt"
"github.com/Azer0s/qpmd"
"github.com/Azer0s/quacktors/mailbox"
"github.com/Azer0s/quacktors/metrics"
"github.com/opentracing/opentracing-go"
"github.com/vmihailenco/msgpack/v5"
"net"
"sync"
)
const quitMessageType = "quit"
const monitorMessageType = "monitor"
const demonitorMessageType = "demonitor"
const newConnectionMessageType = "new_connection"
const fromVal = "from"
const toVal = "to"
const typeVal = "type"
const spanCtx = "span_ctx"
const messageVal = "message"
const machineVal = "machine"
//Machine is the struct representation of a remote machine.
type Machine struct {
connected bool
MachineId string
Address string
MessageGatewayPort uint16
gatewayQuitChan chan bool
GeneralPurposePort uint16
gpQuitChan chan bool
quitChan chan<- *Pid
messageChan chan<- interface{}
monitorChan chan<- remoteMonitorTuple
demonitorChan chan<- remoteMonitorTuple
newConnectionChan chan<- *Machine
//Stores channels to scheduled monitors
scheduled map[string]chan bool
//Stores channels to tell a monitor task to quit (when a pid is demonitored)
monitorQuitChannels map[string]chan bool
monitorsMu *sync.Mutex
}
func (m *Machine) stop() {
go func() {
m.connected = false
logger.Info("stopping connections to remote machine",
"machine_id", m.MachineId)
deleteMachine(m.MachineId)
m.gatewayQuitChan <- true
m.gpQuitChan <- true
close(m.messageChan)
m.monitorsMu.Lock()
defer m.monitorsMu.Unlock()
if len(m.scheduled) != 0 {
//Terminate all scheduled events/send down message to monitor tasks
logger.Debug("sending out scheduled events after remote machine disconnect",
"machine_id", m.MachineId)
for k, ch := range m.scheduled {
ch <- true
close(ch)
delete(m.scheduled, k)
}
}
if len(m.monitorQuitChannels) != 0 {
logger.Debug("deleting machine connection monitor abort channels",
"machine_id", m.MachineId)
//Delete monitorQuitChannels
for n, c := range m.monitorQuitChannels {
close(c)
delete(m.monitorQuitChannels, n)
}
}
m.monitorQuitChannels = nil
}()
}
func (m *Machine) setupMonitor(monitor *Pid) {
name := monitor.String()
monitorChannel := make(chan bool)
m.scheduled[name] = monitorChannel
monitorQuitChannel := make(chan bool)
m.monitorQuitChannels[name] = monitorQuitChannel
go func() {
select {
case <-monitorQuitChannel:
return
case <-monitorChannel:
doSend(monitor, DisconnectMessage{MachineId: m.MachineId, Address: m.Address}, nil)
}
}()
}
func (m *Machine) setupRemoteMonitor(r remoteMonitorTuple) {
m.monitorsMu.Lock()
defer m.monitorsMu.Unlock()
name := r.From.String() + "_" + r.To.String()
monitorChannel := make(chan bool)
m.scheduled[name] = monitorChannel
monitorQuitChannel := make(chan bool)
m.monitorQuitChannels[name] = monitorQuitChannel
go func() {
select {
case <-monitorQuitChannel:
return
case <-monitorChannel:
doSend(r.From, DownMessage{Who: r.To}, nil)
}
}()
}
func (m *Machine) removeRemoteMonitor(r remoteMonitorTuple) {
m.monitorsMu.Lock()
defer m.monitorsMu.Unlock()
name := r.From.String() + "_" + r.To.String()
m.monitorQuitChannels[name] <- true
delete(m.scheduled, name)
delete(m.monitorQuitChannels, name)
}
func (m *Machine) startMessageClient(mb *mailbox.Mailbox, gatewayQuitChan <-chan bool, okChan chan<- bool, errorChan chan<- error) {
logger.Debug("starting message client for remote machine",
"machine_id", m.MachineId)
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", m.Address, m.MessageGatewayPort))
if err != nil {
errorChan <- err
return
}
defer func() {
l := mb.Len()
if l != 0 {
//record dropped message metric
metrics.RecordDropRemote(m.MachineId, l)
}
}()
okChan <- true
messageChan := mb.Out()
for {
select {
case mi := <-messageChan:
message := mi.(remoteMessageTuple)
if d, ok := message.Message.(DownMessage); ok {
logger.Trace("cleaning up old remote monitor abortable, local PID just went down",
"monitor_gpid", message.To.String(),
"monitored_pid", d.Who.Id)
remoteMonitorQuitAbortablesMu.Lock()
delete(remoteMonitorQuitAbortables, message.To.String()+"_"+d.Who.String())
remoteMonitorQuitAbortablesMu.Unlock()
}
msgMap, err := encodeValue(message.Message.Type(), message.Message)
if err != nil {
continue
}
spanCtxBytes := &bytes.Buffer{}
if message.SpanContext != nil {
_ = opentracing.GlobalTracer().Inject(message.SpanContext, opentracing.Binary, spanCtxBytes)
}
b, err := msgpack.Marshal(map[string]interface{}{
toVal: message.To.Id,
typeVal: message.Message.Type(),
messageVal: msgMap,
spanCtx: spanCtxBytes.Bytes(),
})
if err != nil {
logger.Warn("there was an error while sending message to remote machine",
"receiver_gpid", message.To.String(),
"machine_id", m.MachineId,
"error", err)
m.stop()
}
_, err = conn.Write(b)
if err != nil {
logger.Warn("there was an error while sending message to remote machine",
"receiver_gpid", message.To.String(),
"machine_id", m.MachineId,
"error", err)
m.stop()
}
case <-gatewayQuitChan:
logger.Info("closing connection to remote message gateway",
"machine_id", m.MachineId)
_ = conn.Close()
return
}
}
}
func (m *Machine) startGpClient(gpQuitChan <-chan bool, quitChan <-chan *Pid, monitorChan <-chan remoteMonitorTuple, demonitorChan <-chan remoteMonitorTuple, newConnectionChan <-chan *Machine, okChan chan<- bool, errorChan chan<- error) {
logger.Debug("starting general purpose client for remote machine",
"machine_id", m.MachineId)
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", m.Address, m.GeneralPurposePort))
if err != nil {
errorChan <- err
return
}
err = sendRequest(conn, qpmd.Request{
RequestType: qpmd.REQUEST_HELLO,
Data: map[string]interface{}{
qpmd.MACHINE_ID: machineId,
qpmd.MESSAGE_GATEWAY_PORT: messageGatewayPort,
qpmd.GP_GATEWAY_PORT: gpGatewayPort,
},
})
if err != nil {
errorChan <- err
return
}
res, err := readResponse(conn)
if err != nil {
errorChan <- err
return
}
if res.ResponseType != qpmd.RESPONSE_OK {
errorChan <- errors.New("remote machine returned non okay result")
return
}
okChan <- true
for {
select {
case p := <-quitChan:
err := sendRequest(conn, qpmd.Request{
RequestType: quitMessageType,
Data: map[string]interface{}{
pidVal: p.Id,
},
})
if err != nil {
logger.Warn("there was an error while sending kill command to remote machine",
"target_gpid", p.String(),
"machine_id", m.MachineId,
"error", err)
m.stop()
}
case r := <-monitorChan:
//Note: when we monitor a foreign pid, we also have to link up the remote machine
//to the actual monitor. I.e. if the connection to the remote machine goes down, we also have to send out
//down messages to the monitors
err := sendRequest(conn, qpmd.Request{
RequestType: monitorMessageType,
Data: map[string]interface{}{
fromVal: r.From,
toVal: r.To,
},
})
if err != nil {
logger.Warn("there was an error while sending monitor request to remote machine",
"monitor_pid", r.From.Id,
"monitored_gpid", r.To.String(),
"machine_id", m.MachineId,
"error", err)
m.stop()
}
//this is the above mentioned link; if the remote connection goes down, a DownMessage is sent
//to the monitoring PID (but obviously from the local machine because the remote one already
//disconnected)
m.setupRemoteMonitor(r)
case r := <-demonitorChan:
err := sendRequest(conn, qpmd.Request{
RequestType: demonitorMessageType,
Data: map[string]interface{}{
fromVal: r.From,
toVal: r.To,
},
})
if err != nil {
logger.Warn("there was an error while sending demonitor request to remote machine",
"monitor", r.From.String(),
"monitored_pid", r.To.String(),
"machine_id", m.MachineId,
"error", err)
m.stop()
}
//remove "link" to the connection
m.removeRemoteMonitor(r)
case machine := <-newConnectionChan:
err := sendRequest(conn, qpmd.Request{
RequestType: newConnectionMessageType,
Data: map[string]interface{}{
machineVal: machine,
},
})
if err != nil {
logger.Warn("there was an error while sending new connection information to remote machine",
"new_machine_id", machine.MachineId,
"machine_id", m.MachineId,
"error", err)
m.stop()
}
case <-gpQuitChan:
logger.Info("closing connection to remote general purpose gateway",
"machine_id", m.MachineId)
_ = conn.Close()
return
}
}
}
func (m *Machine) connect() error {
//quitChan, monitorChan, demonitorChan and newConnectionChan each have buffers of 100
//this is a, sort of, "close protection" for when a remote machine disconnects
//there is a short time frame (i.e. a couple ns) where the *Machine is closing
//but is not yet marked "closed" or deleted from the machine register
//in a couple ns, nothing much can happen really (if there are a lot of actors open,
//maybe 1 or 2 will manage to send out some commands to the dangling *Machine)
//this buffer is there as a precaution to avoid leaking goroutines (because as soon as the
//machine is dereferenced, the channels get garbage collected and the machine has to be
//dereferenced at some point because it's then only attached to the RemoteSystem instance which
//will return an error once used again, forcing the application to reconnect and destroying
//the old Machine ptr)
quitChan := make(chan *Pid, 100)
mb := mailbox.New()
monitorChan := make(chan remoteMonitorTuple, 100)
demonitorChan := make(chan remoteMonitorTuple, 100)
newConnectionChan := make(chan *Machine, 100)
m.quitChan = quitChan
m.messageChan = mb.In()
m.monitorChan = monitorChan
m.demonitorChan = demonitorChan
m.newConnectionChan = newConnectionChan
m.scheduled = make(map[string]chan bool)
m.monitorQuitChannels = make(map[string]chan bool)
m.monitorsMu = &sync.Mutex{}
//Buffer size of 2 to avoid leaks if both connections fail
gatewayQuitChan := make(chan bool, 2)
gpQuitChan := make(chan bool, 2)
m.gatewayQuitChan = gatewayQuitChan
m.gpQuitChan = gpQuitChan
errorChan := make(chan error)
okChan := make(chan bool)
logger.Info("connecting to remote machine",
"machine_id", m.MachineId)
go m.startMessageClient(mb, gatewayQuitChan, okChan, errorChan)
select {
case err := <-errorChan:
logger.Warn("there was an error while connecting to remote machine",
"machine_id", m.MachineId,
"error", err)
return err
case <-okChan:
//everything went fine
}
go m.startGpClient(gpQuitChan, quitChan, monitorChan, demonitorChan, newConnectionChan, okChan, errorChan)
select {
case err := <-errorChan:
gatewayQuitChan <- true
logger.Warn("there was an error while connecting to remote machine",
"machine_id", m.MachineId,
"error", err)
return err
case <-okChan:
//everything went fine
}
logger.Info("successfully established connection to remote machine",
"machine_id", m.MachineId)
m.connected = true
return nil
}