-
Notifications
You must be signed in to change notification settings - Fork 159
/
member.go
236 lines (192 loc) · 6.1 KB
/
member.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
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package raft
import (
"github.com/openziti/channel/v2/protobufs"
"github.com/openziti/ziti/common/pb/cmd_pb"
"time"
"github.com/hashicorp/raft"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type Member struct {
Id string
Addr string
Voter bool
Leader bool
Version string
Connected bool
}
func (self *Controller) ListMembers() ([]*Member, error) {
configFuture := self.GetRaft().GetConfiguration()
if err := configFuture.Error(); err != nil {
return nil, errors.Wrap(err, "failed to get raft configuration")
}
var result []*Member
leaderAddr, _ := self.GetRaft().LeaderWithID()
peers := self.GetMesh().GetPeers()
memberSet := make(map[string]bool)
for _, srv := range configFuture.Configuration().Servers {
memberSet[string(srv.Address)] = true
version := "<not connected>"
connected := false
if string(srv.ID) == self.env.GetId().Token {
version = self.env.GetVersionProvider().Version()
connected = true
} else if peer, exists := peers[string(srv.Address)]; exists {
version = peer.Version.Version
connected = true
}
result = append(result, &Member{
Id: string(srv.ID),
Addr: string(srv.Address),
Voter: srv.Suffrage == raft.Voter,
Leader: srv.Address == leaderAddr,
Version: version,
Connected: connected,
})
}
if len(result) == 0 {
for addr, peer := range peers {
if _, exists := memberSet[addr]; exists {
continue
}
result = append(result, &Member{
Id: string(peer.Id),
Addr: peer.Address,
Voter: false,
Leader: peer.Address == string(leaderAddr),
Version: peer.Version.Version,
Connected: true,
})
}
}
return result, nil
}
func (self *Controller) HandleAddPeerAsLeader(req *cmd_pb.AddPeerRequest) error {
r := self.GetRaft()
configFuture := r.GetConfiguration()
if err := configFuture.Error(); err != nil {
return errors.Wrap(err, "failed to get raft configuration")
}
id := raft.ServerID(req.Id)
addr := raft.ServerAddress(req.Addr)
for _, srv := range configFuture.Configuration().Servers {
// If a node already exists with either the joining node's ID or address,
// that node may need to be removed from the config first.
if srv.ID == id || srv.Address == addr {
// However, if *both* the ID and the address are the same, then nothing -- not even
// a join operation -- is needed.
if srv.ID == id && srv.Address == addr {
logrus.Infof("node %s at %s already member of cluster, ignoring join request", id, addr)
return nil
}
future := r.RemoveServer(srv.ID, 0, 0)
if err := future.Error(); err != nil {
return errors.Wrapf(err, "error removing existing node %s at %s", id, addr)
}
}
}
var f raft.IndexFuture
if req.IsVoter {
f = r.AddVoter(id, addr, 0, 0)
} else {
f = r.AddNonvoter(id, addr, 0, 0)
}
if err := f.Error(); err != nil {
return errors.Wrap(err, "add peer failed")
}
return nil
}
func (self *Controller) HandleRemovePeerAsLeader(req *cmd_pb.RemovePeerRequest) error {
r := self.GetRaft()
configFuture := r.GetConfiguration()
if err := configFuture.Error(); err != nil {
return errors.Wrap(err, "failed to get raft configuration")
}
id := raft.ServerID(req.Id)
future := r.RemoveServer(id, 0, 0)
if err := future.Error(); err != nil {
return errors.Wrapf(err, "error removing existing node %s", id)
}
return nil
}
func (self *Controller) HandleTransferLeadershipAsLeader(req *cmd_pb.TransferLeadershipRequest) error {
r := self.GetRaft()
var future raft.Future
if req.Id == "" {
future = r.LeadershipTransfer()
} else {
configFuture := r.GetConfiguration()
if err := configFuture.Error(); err != nil {
return errors.Wrap(err, "failed to get raft configuration")
}
var targetServer *raft.Server
for _, v := range configFuture.Configuration().Servers {
if v.ID == raft.ServerID(req.Id) {
targetServer = &v
break
}
}
if targetServer == nil {
return errors.Errorf("no cluster node found with id %v", req.Id)
}
if targetServer.Suffrage != raft.Voter {
return errors.Errorf("cluster node %v is not a voting member", req.Id)
}
future = r.LeadershipTransferToServer(targetServer.ID, targetServer.Address)
}
if err := future.Error(); err != nil {
return errors.Wrapf(err, "error transfering leadership")
}
return nil
}
func (self *Controller) HandleAddPeer(req *cmd_pb.AddPeerRequest) error {
if self.IsLeader() {
return self.HandleAddPeerAsLeader(req)
}
return self.forwardToLeader(req)
}
func (self *Controller) HandleRemovePeer(req *cmd_pb.RemovePeerRequest) error {
if self.IsLeader() {
return self.HandleRemovePeerAsLeader(req)
}
return self.forwardToLeader(req)
}
func (self *Controller) HandleTransferLeadership(req *cmd_pb.TransferLeadershipRequest) error {
if self.IsLeader() {
return self.HandleTransferLeadershipAsLeader(req)
}
return self.forwardToLeader(req)
}
func (self *Controller) forwardToLeader(req protobufs.TypedMessage) error {
leader := self.GetLeaderAddr()
if leader == "" {
return errors.New("no leader, unable to forward request")
}
peer, err := self.GetMesh().GetOrConnectPeer(self.GetLeaderAddr(), 5*time.Second)
if err != nil {
return err
}
result, err := protobufs.MarshalTyped(req).WithTimeout(5 * time.Second).SendForReply(peer.Channel)
if err != nil {
return err
}
if result.ContentType == int32(cmd_pb.ContentType_SuccessResponseType) {
return nil
}
if result.ContentType == int32(cmd_pb.ContentType_ErrorResponseType) {
return errors.New(string(result.Body))
}
return errors.Errorf("unexpected response type %v", result.ContentType)
}