-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathremoteMailboxes.go
More file actions
363 lines (310 loc) · 9.6 KB
/
remoteMailboxes.go
File metadata and controls
363 lines (310 loc) · 9.6 KB
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
package reign
import (
"context"
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/thejerf/reign/internal"
)
var errNoConnection = errors.New("no connection")
type messageSender interface {
send(*internal.ClusterMessage) error
terminate()
}
// remoteMailboxes, which may need a better name, connects the local
// mailboxes with the remote connection. Once a node has connected to a
// remote node, either as the server or a client, the connection becomes
// symmetric and both sides run this.
//
// A given instance is responsible only for maintaining communication with
// a particular node.
type remoteMailboxes struct {
NodeID
*Address
parent *mailboxes
outgoingMailbox *Mailbox
ClusterLogger
connectionServer *connectionServer
// A set-like map that records the remote MailboxID that a local
// Address is linked to, mapped to the set of local mailboxes that
// are so linked.
// The map here starts with Node, so that when a Node goes down,
// all the mailboxes are nicely sorted out for just that node,
// then it's the remote mailbox in question, then it's the set of
// local mailboxes that are subscribed to that remote mailbox.
linksToRemote map[MailboxID]map[MailboxID]voidtype
// a debugging function that allows us to examine the messages flowing
// through
examineMessages func(interface{}) bool
// a debugging function that allows us to examine the messages as they
// are done processing
doneProcessing func(interface{}) bool
sync.Mutex
condition *sync.Cond
connection messageSender
// a debugging function that allows us to see that a connection has
// been re-established.
connectionEstablished func()
}
type newExamineMessages struct {
f func(interface{}) bool
}
type newDoneProcessing struct {
f func(interface{}) bool
}
func newRemoteMailboxes(connectionServer *connectionServer, mailboxes *mailboxes, logger ClusterLogger, source NodeID) *remoteMailboxes {
addr, mailbox := mailboxes.newLocalMailbox()
rm := &remoteMailboxes{
Address: addr,
outgoingMailbox: mailbox,
ClusterLogger: logger,
parent: mailboxes,
NodeID: source,
connectionServer: connectionServer,
// linksToRemote maps the remote MailboxID to all locally linked MailboxIDs
linksToRemote: make(map[MailboxID]map[MailboxID]voidtype)}
rm.condition = sync.NewCond(&rm.Mutex)
return rm
}
// awaitConnection waits until the remoteMailboxs have a non-nil connection
func (rm *remoteMailboxes) waitForConnection() {
rm.Lock()
defer rm.Unlock()
for rm.connection == nil {
rm.condition.Wait()
}
}
func (rm *remoteMailboxes) setConnection(ms messageSender) {
rm.Lock()
defer rm.Unlock()
rm.connection = ms
if rm.connectionEstablished != nil {
rm.connectionEstablished()
}
rm.condition.Broadcast()
}
func (rm *remoteMailboxes) unsetConnection(ms messageSender) {
rm.Lock()
defer rm.Unlock()
if rm.connection == ms {
rm.connection = nil
}
}
type terminateRemoteMailbox struct{}
func (rm *remoteMailboxes) Stop() {
_ = rm.Send(terminateRemoteMailbox{})
}
func (rm *remoteMailboxes) send(cm internal.ClusterMessage) error {
rm.Lock()
defer rm.Unlock()
if rm.connection == nil {
if rm.ClusterLogger != nil {
rm.Errorf("No connection sending: %#v", cm)
}
return errNoConnection
}
err := rm.connection.send(&cm)
if err != nil {
nErr, ok := err.(net.Error)
if ok && nErr.Temporary() {
// Temporary network error. Let's sleep for 5 seconds and try again.
//
// FIXME: (adam) As mentioned in Serve(), this is a weak attempt
// at fault tolerance. This struct could stand something more
// comprehensive.
time.Sleep(5 * time.Second)
err = rm.connection.send(&cm)
if err == nil {
return nil
}
}
rm.Errorf("%q sending: %#v", err, cm)
}
return err
}
func (rm *remoteMailboxes) String() string {
return fmt.Sprintf("remoteMailbox %d", rm.NodeID)
}
func (rm *remoteMailboxes) Serve() {
defer func() {
for remoteID, localIDs := range rm.linksToRemote {
for localID := range localIDs {
// FIXME: sendByID?
addr := Address{
mailboxID: localID,
connectionServer: rm.connectionServer,
}
_ = addr.Send(MailboxClosed(remoteID))
}
}
rm.linksToRemote = make(map[MailboxID]map[MailboxID]voidtype)
if r := recover(); r != nil {
rm.Errorf("While handling mailbox, got fatal error (this is a serious bug): %s", myString(r))
rm.Lock()
if rm.connection != nil {
rm.connection.terminate()
}
rm.Unlock()
panic(r)
}
}()
var (
ctx = context.Background()
err error
message interface{}
)
for {
if rm.doneProcessing != nil {
if !rm.doneProcessing(message) {
rm.doneProcessing = nil
}
}
message, err = rm.outgoingMailbox.Receive(ctx)
if err == ErrMailboxClosed {
rm.Trace(err)
return
}
if rm.examineMessages != nil {
if !rm.examineMessages(message) {
rm.examineMessages = nil
}
}
switch msg := message.(type) {
case internal.OutgoingMailboxMessage:
_ = rm.send(internal.IncomingMailboxMessage(msg))
// all of the gob encoding stuff seems to end up with this getting
// an extra layer of pointer indirection added to it.
// Edit (adam): It's because we're registering the objects as pointers with gob.
case *internal.IncomingMailboxMessage:
addr := Address{
mailboxID: MailboxID(msg.Target),
connectionServer: rm.connectionServer,
}
sErr := addr.Send(msg.Message)
if sErr == ErrMailboxClosed {
// Unregister the mailbox again since it appears one or more nodes
// did not receive the message either through a network error or
// straight up negligence.
//
// FIXME: (adam) This is a temporary solution until we implement some
// sort of retry mechanism when sending out UnregisterName messages
// over the socket. Really, this whole struct could use the ability
// to resend any message and be a bit more fault tolerant.
rm.Tracef("Received MailboxClosed error for mailbox ID %x; unregistering", addr.GetID())
rm.connectionServer.registry.UnregisterMailbox(rm.NodeID, addr.GetID())
}
case internal.NotifyRemote:
// FIXME: if the local addr dies, this never cleans out
// link. This will eventually be a memory leak.
// Unfortunately it implies we need another map of local
// address to their relevant entries and to subscribe to them too.
remoteID := MailboxID(msg.Remote)
localID := MailboxID(msg.Local)
linksToRemote, remoteLinksExist := rm.linksToRemote[remoteID]
if remoteLinksExist {
_, thisAddressAlreadyLinked := linksToRemote[localID]
if thisAddressAlreadyLinked {
// a no-op; msg.local has already set notify for msg.remote
continue
}
} else {
linksToRemote = make(map[MailboxID]voidtype)
rm.linksToRemote[remoteID] = linksToRemote
}
if len(linksToRemote) == 0 {
// Since this is the first link to this particular
// remote mailbox we are recording, we need to send along
// the registration message
sErr := rm.send(&internal.NotifyNodeOnTerminate{IntMailboxID: internal.IntMailboxID(remoteID)})
if sErr != nil {
addr := Address{
mailboxID: localID,
connectionServer: rm.connectionServer,
}
_ = addr.Send(MailboxClosed(remoteID))
// FIXME: Really? Panic?
panic(sErr)
}
}
linksToRemote[localID] = void
case internal.UnnotifyRemote:
remoteID := MailboxID(msg.Remote)
localID := MailboxID(msg.Local)
linksToRemote, remoteLinksExist := rm.linksToRemote[remoteID]
if !remoteLinksExist || len(linksToRemote) == 0 {
continue
}
delete(linksToRemote, localID)
if len(linksToRemote) == 0 {
// If that was the last link, we need to unregister from
// the remote node send does all the error handling I need
// here.
_ = rm.send(
&internal.RemoveNotifyNodeOnClose{
IntMailboxID: internal.IntMailboxID(remoteID),
},
)
}
case *internal.RemoteMailboxClosed:
// A remote mailbox has been terminated that we indicated
// interest in.
remoteID := MailboxID(msg.IntMailboxID)
links, linksExist := rm.linksToRemote[remoteID]
if !linksExist || len(links) == 0 {
continue
}
for subscribed := range links {
addr := Address{
mailboxID: subscribed,
connectionServer: rm.connectionServer,
}
_ = addr.Send(MailboxClosed(remoteID))
}
delete(rm.linksToRemote, remoteID)
case *internal.NotifyNodeOnTerminate:
// this has to be a localID, or we wouldn't be receiving this
// message
localID := MailboxID(msg.IntMailboxID)
addr := Address{
mailboxID: localID,
connectionServer: rm.connectionServer,
}
addr.OnCloseNotify(rm.Address)
case *internal.RemoveNotifyNodeOnClose:
localID := MailboxID(msg.IntMailboxID)
addr := Address{
mailboxID: localID,
connectionServer: rm.connectionServer,
}
addr.RemoveNotify(rm.Address)
// Note this is a local mailbox.
case MailboxClosed:
id := MailboxID(msg)
// if we are receiving this, apparently the other side wants to
// hear about it
_ = rm.send(
&internal.RemoteMailboxClosed{
IntMailboxID: internal.IntMailboxID(id),
},
)
// This allows us to test proper error handling, despite
// the fact I don't know how to panic any of the above code
case internal.PanicHandler:
panic("Panicking as requested due to panic handler")
case internal.DestroyConnection:
rm.Lock()
rm.connection.terminate()
rm.Unlock()
case newExamineMessages:
rm.examineMessages = msg.f
case newDoneProcessing:
rm.doneProcessing = msg.f
case terminateRemoteMailbox:
return
default:
rm.Errorf("Unexpected message arrived in our node mailbox: %#v", msg)
}
}
}