generated from Jille/raft-grpc-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaDataGossip.go
148 lines (140 loc) · 3.78 KB
/
metaDataGossip.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
package cluster
import (
"github.com/Kapperchino/jet-stream/cluster/proto/proto"
"github.com/Kapperchino/jet-stream/util"
"github.com/hashicorp/raft"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"time"
)
type ClusterDelegate struct {
ClusterState *ClusterState
}
func (c ClusterDelegate) Logger() *zerolog.Logger {
return c.ClusterState.Logger
}
func (c ClusterDelegate) NodeMeta(limit int) []byte {
shardMap := c.ClusterState.getMemberMap()
res := proto.ShardInfo{
MemberAddressMap: map[string]*proto.MemberInfo{},
LeaderId: c.ClusterState.getLeader(),
ShardId: c.ClusterState.getShardId(),
}
shardMap.ForEach(func(s string, info *MemberInfo) bool {
res.MemberAddressMap[s] = &proto.MemberInfo{
NodeId: info.NodeId,
Address: info.Address,
}
return true
})
bytes, err := util.SerializeMessage(&res)
if err != nil {
c.Logger().Err(err)
return nil
}
return bytes
}
func (c ClusterDelegate) NotifyMsg(bytes []byte) {
c.Logger().Debug().Msgf("received %s", bytes)
}
func (c ClusterDelegate) GetBroadcasts(overhead, limit int) [][]byte {
return nil
}
func (c ClusterDelegate) LocalState(join bool) []byte {
shardMap := c.ClusterState.getMemberMap()
res := proto.ShardInfo{
MemberAddressMap: map[string]*proto.MemberInfo{},
LeaderId: c.ClusterState.getLeader(),
ShardId: c.ClusterState.getShardId(),
NodeId: c.ClusterState.getNodeId(),
}
shardMap.ForEach(func(s string, info *MemberInfo) bool {
res.MemberAddressMap[s] = &proto.MemberInfo{
NodeId: info.NodeId,
Address: info.Address,
}
return true
})
bytes, err := util.SerializeMessage(&res)
if err != nil {
c.Logger().Err(err)
return nil
}
return bytes
}
func (c ClusterDelegate) MergeRemoteState(buf []byte, join bool) {
var meta proto.ShardInfo
err := util.DeserializeMessage(buf, &meta)
if err != nil {
c.Logger().Err(err).Msgf("Error deserializing message")
return
}
c.Logger().Debug().Msgf("remote state is %v, join: %s", meta, join)
//adding it to the cluster
if join {
//add node to shard
if meta.ShardId == c.ClusterState.getShardId() && meta.NodeId != c.ClusterState.getNodeId() {
if meta.LeaderId == c.ClusterState.getLeader() {
raftPrt := c.ClusterState.getShardState().Raft
//add voter
address := meta.MemberAddressMap[meta.NodeId].Address
future := raftPrt.AddVoter(raft.ServerID(meta.NodeId), raft.ServerAddress(address), 0, time.Second*20)
go func() {
err := future.Error()
if err != nil {
log.Err(err).Msgf("Error when adding voter")
}
}()
}
return
}
memberMap := util.NewMap[string, *MemberInfo]()
for key, val := range meta.MemberAddressMap {
info := MemberInfo{
NodeId: val.NodeId,
IsLeader: false,
Address: val.Address,
}
if info.NodeId == meta.LeaderId {
info.IsLeader = true
}
memberMap.Set(key, &info)
}
c.ClusterState.ClusterInfo.Set(meta.ShardId, &ShardInfo{
shardId: meta.ShardId,
Leader: meta.LeaderId,
nodeId: meta.NodeId,
MemberMap: memberMap,
})
return
}
//update the map if possible
memberMap := c.ClusterState.getMemberMap()
//check if Leader is the same
if meta.LeaderId != c.ClusterState.getLeader() {
//Leader changed need to update everything
for key, val := range meta.MemberAddressMap {
member := memberMap.Get(key)
if member != nil {
//everything is immutable other than isleader
if member.NodeId == meta.LeaderId {
member.IsLeader = true
} else {
member.IsLeader = false
}
} else {
info := MemberInfo{
NodeId: val.NodeId,
IsLeader: false,
Address: val.Address,
}
if info.NodeId == meta.LeaderId {
info.IsLeader = true
}
memberMap.Set(key, &info)
}
}
c.ClusterState.GetShardInfo().Leader = meta.LeaderId
return
}
}