-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
134 lines (107 loc) · 2.81 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
package castcenter
import (
"fmt"
"net"
"time"
"github.com/sirupsen/logrus"
)
// ClusterStatus .
type ClusterStatus struct {
SubNetLeader bool
Conns map[string]*net.UDPConn
}
// GetClusterStatus .
func (c *CastCenter) GetClusterStatus() *ClusterStatus {
cs := c.clusterStatus.Load()
if cs == nil {
return nil
}
return cs.(*ClusterStatus)
}
// SetClusterStatus .
func (c *CastCenter) SetClusterStatus(cs *ClusterStatus) {
logrus.Infof("castcenter fresh cluster status: %+v", cs)
c.clusterStatus.Store(cs)
}
func (c *CastCenter) heartbeatKey() string {
return fmt.Sprintf("%s:heartbeat", c.clusterName)
}
func (c *CastCenter) subnetLeaderKey() string {
return fmt.Sprintf("%s:subnet:%s:leader", c.clusterName, netinfo.SubNet)
}
func (c *CastCenter) heartbeat() error {
now := time.Now().Unix()
timeout := int64(c.clusterTimeout / time.Second)
oldStatus := c.GetClusterStatus()
_, err := c.redis.ZAdd(c.heartbeatKey(), c.serviceAddr, float64(now))
if err != nil {
logrus.WithError(err).Errorf("castcenter: heartbeat fail")
return err
}
subnetLeader, err := c.checkSubNetLeader()
if err != nil {
logrus.WithError(err).Errorf("castcenter: fetch hosts list fail")
return err
}
hosts, err := c.redis.ZRangeByScore(c.heartbeatKey(), float64(now-timeout), float64(now+timeout))
if err != nil {
logrus.WithError(err).Errorf("castcenter: fetch hosts list fail")
return err
}
var oldConns map[string]*net.UDPConn
if oldStatus != nil && oldStatus.Conns != nil {
oldConns = oldStatus.Conns
}
conns := make(map[string]*net.UDPConn, len(hosts))
for _, host := range hosts {
addr, err := net.ResolveUDPAddr("udp", host)
if err != nil {
logrus.WithError(err).Errorf("castcenter: append conn fail when resolve udp addr, host= %s", host)
return err
}
if netinfo.SubNet.Contains(addr.IP) {
continue
}
conn, ok := oldConns[host]
if ok {
conns[host] = conn
continue
}
conn, err = net.DialUDP("udp", nil, addr)
if err != nil {
logrus.WithError(err).Errorf("castcenter: append conn fail when dial udp conn, host= %s", host)
return err
}
conns[host] = conn
}
c.SetClusterStatus(&ClusterStatus{
SubNetLeader: subnetLeader,
Conns: conns,
})
return nil
}
func (c *CastCenter) checkSubNetLeader() (bool, error) {
key := c.subnetLeaderKey()
ok, err := c.redis.SetNX(key, c.serviceAddr, c.clusterTimeout)
if err != nil {
return false, err
}
if ok {
logrus.Infof("castcenter: as subnet leader")
return true, nil
}
oldStatus := c.GetClusterStatus()
if oldStatus != nil && !oldStatus.SubNetLeader {
return false, nil
}
host, err := c.redis.Get(key)
if err != nil {
return false, err
}
if host != c.serviceAddr {
return false, nil
}
c.redis.Expire(key, c.clusterTimeout)
logrus.Infof("castcenter: as subnet leader, refresh")
return true, nil
}