/
cluster.go
615 lines (516 loc) · 14.7 KB
/
cluster.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package main
import (
"encoding/gob"
"encoding/json"
"errors"
"log"
"net"
"net/rpc"
"sort"
"sync"
"time"
"github.com/tinode/chat/server/auth"
rh "github.com/tinode/chat/server/ringhash"
"github.com/tinode/chat/server/store/types"
)
const (
// Default timeout before attempting to reconnect to a node
defaultClusterReconnect = 200 * time.Millisecond
// Number of replicas in ringhash
clusterHashReplicas = 20
)
type clusterNodeConfig struct {
Name string `json:"name"`
Addr string `json:"addr"`
}
type clusterConfig struct {
// List of all members of the cluster, including this member
Nodes []clusterNodeConfig `json:"nodes"`
// Name of this cluster node
ThisName string `json:"self"`
// Failover configuration
Failover *clusterFailoverConfig
}
// ClusterNode is a client's connection to another node.
type ClusterNode struct {
lock sync.Mutex
// RPC endpoint
endpoint *rpc.Client
// True if the endpoint is believed to be connected
connected bool
// True if a go routine is trying to reconnect the node
reconnecting bool
// TCP address in the form host:port
address string
// Name of the node
name string
// A number of times this node has failed in a row
failCount int
// Channel for shutting down the runner; buffered, 1
done chan bool
}
// ClusterSess is a basic info on a remote session where the message was created.
type ClusterSess struct {
// IP address of the client. For long polling this is the IP of the last poll
RemoteAddr string
// User agent, a string provived by an authenticated client in {login} packet
UserAgent string
// ID of the current user or 0
Uid types.Uid
// User's authentication level
AuthLvl auth.Level
// Protocol version of the client: ((major & 0xff) << 8) | (minor & 0xff)
Ver int
// Human language of the client
Lang string
// Device ID
DeviceID string
// Session ID
Sid string
}
// ClusterReq is a Proxy to Master request message.
type ClusterReq struct {
// Name of the node sending this request
Node string
// Ring hash signature of the node sending this request
// Signature must match the signature of the receiver, otherwise the
// Cluster is desynchronized.
Signature string
Msg *ClientComMessage
// Expanded (routable) topic name
RcptTo string
// Originating session
Sess *ClusterSess
// True if the original session has disconnected
SessGone bool
}
// ClusterResp is a Master to Proxy response message.
type ClusterResp struct {
Msg []byte
// Session ID to forward message to, if any.
FromSID string
}
// Handle outbound node communication: read messages from the channel, forward to remote nodes.
// FIXME(gene): this will drain the outbound queue in case of a failure: all unprocessed messages will be dropped.
// Maybe it's a good thing, maybe not.
func (n *ClusterNode) reconnect() {
var reconnTicker *time.Ticker
// Avoid parallel reconnection threads
n.lock.Lock()
if n.reconnecting {
n.lock.Unlock()
return
}
n.reconnecting = true
n.lock.Unlock()
var count = 0
var err error
for {
// Attempt to reconnect right away
if n.endpoint, err = rpc.Dial("tcp", n.address); err == nil {
if reconnTicker != nil {
reconnTicker.Stop()
}
n.lock.Lock()
n.connected = true
n.reconnecting = false
n.lock.Unlock()
log.Printf("cluster: connection to '%s' established", n.name)
return
} else if count == 0 {
reconnTicker = time.NewTicker(defaultClusterReconnect)
}
count++
select {
case <-reconnTicker.C:
// Wait for timer to try to reconnect again. Do nothing if the timer is inactive.
case <-n.done:
// Shutting down
log.Printf("cluster: node '%s' shutdown started", n.name)
reconnTicker.Stop()
if n.endpoint != nil {
n.endpoint.Close()
}
n.lock.Lock()
n.connected = false
n.reconnecting = false
n.lock.Unlock()
log.Printf("cluster: node '%s' shut down completed", n.name)
return
}
}
}
func (n *ClusterNode) call(proc string, msg interface{}, resp interface{}) error {
if !n.connected {
return errors.New("cluster: node '" + n.name + "' not connected")
}
if err := n.endpoint.Call(proc, msg, resp); err != nil {
log.Printf("cluster: call failed to '%s' [%s]", n.name, err)
n.lock.Lock()
if n.connected {
n.endpoint.Close()
n.connected = false
go n.reconnect()
}
n.lock.Unlock()
return err
}
return nil
}
func (n *ClusterNode) callAsync(proc string, msg interface{}, resp interface{}, done chan *rpc.Call) *rpc.Call {
if done != nil && cap(done) == 0 {
log.Panic("cluster: RPC done channel is unbuffered")
}
if !n.connected {
call := &rpc.Call{
ServiceMethod: proc,
Args: msg,
Reply: resp,
Error: errors.New("cluster: node '" + n.name + "' not connected"),
Done: done,
}
if done != nil {
done <- call
}
return call
}
myDone := make(chan *rpc.Call, 1)
go func() {
select {
case call := <-myDone:
if call.Error != nil {
n.lock.Lock()
if n.connected {
n.endpoint.Close()
n.connected = false
go n.reconnect()
}
n.lock.Unlock()
}
if done != nil {
done <- call
}
}
}()
call := n.endpoint.Go(proc, msg, resp, myDone)
call.Done = done
return call
}
// Proxy forwards message to master
func (n *ClusterNode) forward(msg *ClusterReq) error {
log.Printf("cluster: forwarding request to node '%s'", n.name)
msg.Node = globals.cluster.thisNodeName
rejected := false
err := n.call("Cluster.Master", msg, &rejected)
if err == nil && rejected {
err = errors.New("cluster: master node out of sync")
}
return err
}
// Master responds to proxy
func (n *ClusterNode) respond(msg *ClusterResp) error {
log.Printf("cluster: replying to node '%s'", n.name)
unused := false
return n.call("Cluster.Proxy", msg, &unused)
}
// Cluster is the representation of the cluster.
type Cluster struct {
// Cluster nodes with RPC endpoints
nodes map[string]*ClusterNode
// Name of the local node
thisNodeName string
// Resolved address to listed on
listenOn string
// Socket for inbound connections
inbound *net.TCPListener
// Ring hash for mapping topic names to nodes
ring *rh.Ring
// Failover parameters. Could be nil if failover is not enabled
fo *clusterFailover
}
// Master at topic's master node receives C2S messages from topic's proxy nodes.
// The message is treated like it came from a session: find or create a session locally,
// dispatch the message to it like it came from a normal ws/lp connection.
// Called by a remote node.
func (c *Cluster) Master(msg *ClusterReq, rejected *bool) error {
log.Printf("cluster: Master request received from node '%s'", msg.Node)
// Find the local session associated with the given remote session.
sess := globals.sessionStore.Get(msg.Sess.Sid)
if msg.SessGone {
// Original session has disconnected. Tear down the local proxied session.
if sess != nil {
sess.stop <- nil
}
} else if msg.Signature == c.ring.Signature() {
// This cluster member received a request for a topic it owns.
if sess == nil {
// If the session is not found, create it.
node := globals.cluster.nodes[msg.Node]
if node == nil {
log.Println("cluster: request from an unknown node", msg.Node)
return nil
}
sess = globals.sessionStore.Create(node, msg.Sess.Sid)
go sess.rpcWriteLoop()
}
// Update session params which may have changed since the last call.
sess.uid = msg.Sess.Uid
sess.authLvl = msg.Sess.AuthLvl
sess.ver = msg.Sess.Ver
sess.userAgent = msg.Sess.UserAgent
sess.remoteAddr = msg.Sess.RemoteAddr
sess.lang = msg.Sess.Lang
sess.deviceID = msg.Sess.DeviceID
// Dispatch remote message to a local session.
sess.dispatch(msg.Msg)
} else {
// Reject the request: wrong signature, cluster is out of sync.
*rejected = true
}
return nil
}
// Proxy receives messages from the master node addressed to a specific local session.
// Called by Session.writeRPC
func (Cluster) Proxy(msg *ClusterResp, unused *bool) error {
log.Println("cluster: response from Master for session", msg.FromSID)
// This cluster member received a response from topic owner to be forwarded to a session
// Find appropriate session, send the message to it
if sess := globals.sessionStore.Get(msg.FromSID); sess != nil {
if !sess.queueOutBytes(msg.Msg) {
log.Println("cluster.Proxy: timeout")
}
} else {
log.Println("cluster: master response for unknown session", msg.FromSID)
}
return nil
}
// Given topic name, find appropriate cluster node to route message to
func (c *Cluster) nodeForTopic(topic string) *ClusterNode {
key := c.ring.Get(topic)
if key == c.thisNodeName {
log.Println("cluster: request to route to self")
// Do not route to self
return nil
}
node := globals.cluster.nodes[key]
if node == nil {
log.Println("cluster: no node for topic", topic, key)
}
return node
}
func (c *Cluster) isRemoteTopic(topic string) bool {
if c == nil {
// Cluster not initialized, all topics are local
return false
}
return c.ring.Get(topic) != c.thisNodeName
}
// Forward client message to the Master (cluster node which owns the topic)
func (c *Cluster) routeToTopic(msg *ClientComMessage, topic string, sess *Session) error {
// Find the cluster node which owns the topic, then forward to it.
n := c.nodeForTopic(topic)
if n == nil {
return errors.New("attempt to route to non-existent node")
}
// Save node name: it's need in order to inform relevant nodes when the session is disconnected
if sess.nodes == nil {
sess.nodes = make(map[string]bool)
}
sess.nodes[n.name] = true
return n.forward(
&ClusterReq{
Node: c.thisNodeName,
Signature: c.ring.Signature(),
Msg: msg,
RcptTo: topic,
Sess: &ClusterSess{
Uid: sess.uid,
AuthLvl: sess.authLvl,
RemoteAddr: sess.remoteAddr,
UserAgent: sess.userAgent,
Ver: sess.ver,
Lang: sess.lang,
DeviceID: sess.deviceID,
Sid: sess.sid}})
}
// Session terminated at origin. Inform remote Master nodes that the session is gone.
func (c *Cluster) sessionGone(sess *Session) error {
if c == nil {
return nil
}
// Save node name: it's need in order to inform relevant nodes when the session is disconnected
for name := range sess.nodes {
n := c.nodes[name]
if n != nil {
return n.forward(
&ClusterReq{
Node: c.thisNodeName,
SessGone: true,
Sess: &ClusterSess{
Uid: sess.uid,
RemoteAddr: sess.remoteAddr,
UserAgent: sess.userAgent,
Ver: sess.ver,
Sid: sess.sid}})
}
}
return nil
}
// Returns snowflake worker id
func clusterInit(configString json.RawMessage, self *string) int {
if globals.cluster != nil {
log.Fatal("Cluster already initialized.")
}
// This is a standalone server, not initializing
if len(configString) == 0 {
log.Println("Running as a standalone server.")
return 1
}
var config clusterConfig
if err := json.Unmarshal(configString, &config); err != nil {
log.Fatal(err)
}
thisName := *self
if thisName == "" {
thisName = config.ThisName
}
// Name of the current node is not specified - disable clustering
if thisName == "" {
log.Println("Running as a standalone server.")
return 1
}
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
globals.cluster = &Cluster{
thisNodeName: thisName,
nodes: make(map[string]*ClusterNode)}
var nodeNames []string
for _, host := range config.Nodes {
nodeNames = append(nodeNames, host.Name)
if host.Name == thisName {
globals.cluster.listenOn = host.Addr
// Don't create a cluster member for this local instance
continue
}
n := ClusterNode{
address: host.Addr,
name: host.Name,
done: make(chan bool, 1)}
globals.cluster.nodes[host.Name] = &n
}
if len(globals.cluster.nodes) == 0 {
// Cluster needs at least two nodes.
log.Fatal("Invalid cluster size: 1")
}
if !globals.cluster.failoverInit(config.Failover) {
globals.cluster.rehash(nil)
}
sort.Strings(nodeNames)
workerId := sort.SearchStrings(nodeNames, thisName) + 1
return workerId
}
// This is a session handler at a master node: forward messages from the master to the session origin.
func (sess *Session) rpcWriteLoop() {
// There is no readLoop for RPC, delete the session here
defer func() {
log.Println("writeRPC - stop")
sess.closeRPC()
globals.sessionStore.Delete(sess)
for _, sub := range sess.subs {
// sub.done is the same as topic.unreg
sub.done <- &sessionLeave{sess: sess, unsub: false}
}
}()
var unused bool
for {
select {
case msg, ok := <-sess.send:
if !ok || sess.clnode.endpoint == nil {
// channel closed
return
}
// The error is returned if the remote node is down. Which means the remote
// session is also disconnected.
if err := sess.clnode.call("Cluster.Proxy",
&ClusterResp{Msg: msg.([]byte), FromSID: sess.sid}, &unused); err != nil {
log.Println("sess.writeRPC: " + err.Error())
return
}
case msg := <-sess.stop:
// Shutdown is requested, don't care if the message is delivered
if msg != nil {
sess.clnode.call("Cluster.Proxy", &ClusterResp{Msg: msg.([]byte), FromSID: sess.sid},
&unused)
}
return
case topic := <-sess.detach:
delete(sess.subs, topic)
if len(sess.subs) == 0 {
// TODO(gene): the session is not connected to any topics here, shut it down
}
}
}
}
// Proxied session is being closed at the Master node
func (sess *Session) closeRPC() {
if sess.proto == CLUSTER {
log.Println("cluster: session closed at master")
}
}
// Start accepting connections.
func (c *Cluster) start() {
addr, err := net.ResolveTCPAddr("tcp", c.listenOn)
if err != nil {
log.Fatal(err)
}
c.inbound, err = net.ListenTCP("tcp", addr)
if err != nil {
log.Fatal(err)
}
for _, n := range c.nodes {
go n.reconnect()
}
if c.fo != nil {
go c.run()
}
err = rpc.Register(c)
if err != nil {
log.Fatal(err)
}
go rpc.Accept(c.inbound)
log.Printf("Cluster of %d nodes initialized, node '%s' listening on [%s]", len(globals.cluster.nodes)+1,
globals.cluster.thisNodeName, c.listenOn)
}
func (c *Cluster) shutdown() {
if globals.cluster == nil {
return
}
globals.cluster = nil
c.inbound.Close()
if c.fo != nil {
c.fo.done <- true
}
for _, n := range c.nodes {
n.done <- true
}
log.Println("Cluster shut down")
}
// Recalculate the ring hash using provided list of nodes or only nodes in a non-failed state.
// Returns the list of nodes used for ring hash.
func (c *Cluster) rehash(nodes []string) []string {
ring := rh.New(clusterHashReplicas, nil)
var ringKeys []string
if nodes == nil {
for _, node := range c.nodes {
ringKeys = append(ringKeys, node.name)
}
ringKeys = append(ringKeys, c.thisNodeName)
} else {
for _, name := range nodes {
ringKeys = append(ringKeys, name)
}
}
ring.Add(ringKeys...)
c.ring = ring
return ringKeys
}