-
Notifications
You must be signed in to change notification settings - Fork 159
/
agent.go
182 lines (155 loc) · 5.75 KB
/
agent.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
package controller
import (
"fmt"
"github.com/openziti/ziti/common/pb/cmd_pb"
"io"
"net"
"time"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v2"
"github.com/openziti/channel/v2/protobufs"
"github.com/openziti/ziti/common/handler_common"
"github.com/openziti/ziti/common/pb/mgmt_pb"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const (
AgentAppId byte = 1
AgentIdHeader = 10
AgentAddrHeader = 11
AgentIsVoterHeader = 12
)
func (self *Controller) RegisterAgentBindHandler(bindHandler channel.BindHandler) {
self.agentBindHandlers = append(self.agentBindHandlers, bindHandler)
}
func (self *Controller) bindAgentChannel(binding channel.Binding) error {
binding.AddReceiveHandlerF(int32(mgmt_pb.ContentType_SnapshotDbRequestType), self.agentOpSnapshotDb)
binding.AddReceiveHandlerF(int32(mgmt_pb.ContentType_RaftListMembersRequestType), self.agentOpRaftList)
binding.AddReceiveHandlerF(int32(mgmt_pb.ContentType_RaftAddPeerRequestType), self.agentOpRaftAddPeer)
binding.AddReceiveHandlerF(int32(mgmt_pb.ContentType_RaftRemovePeerRequestType), self.agentOpRaftRemovePeer)
binding.AddReceiveHandlerF(int32(mgmt_pb.ContentType_RaftTransferLeadershipRequestType), self.agentOpRaftTransferLeadership)
binding.AddReceiveHandlerF(int32(mgmt_pb.ContentType_RaftInitFromDb), self.agentOpInitFromDb)
for _, bh := range self.agentBindHandlers {
if err := binding.Bind(bh); err != nil {
return err
}
}
return nil
}
func (self *Controller) HandleCustomAgentAsyncOp(conn net.Conn) error {
logrus.Debug("received agent operation request")
appIdBuf := []byte{0}
_, err := io.ReadFull(conn, appIdBuf)
if err != nil {
return err
}
appId := appIdBuf[0]
if appId != AgentAppId {
logrus.WithField("appId", appId).Debug("invalid app id on agent request")
return errors.New("invalid operation for controller")
}
options := channel.DefaultOptions()
options.ConnectTimeout = time.Second
listener := channel.NewExistingConnListener(self.config.Id, conn, nil)
_, err = channel.NewChannel("agent", listener, channel.BindHandlerF(self.bindAgentChannel), options)
return err
}
func (self *Controller) agentOpSnapshotDb(m *channel.Message, ch channel.Channel) {
log := pfxlog.Logger()
if err := self.network.SnapshotDatabase(); err != nil {
log.WithError(err).Error("failed to snapshot db")
handler_common.SendOpResult(m, ch, "db.snapshot", err.Error(), false)
} else {
handler_common.SendOpResult(m, ch, "db.snapshot", "", true)
}
}
func (self *Controller) agentOpRaftList(m *channel.Message, ch channel.Channel) {
members, err := self.raftController.ListMembers()
if err != nil {
handler_common.SendOpResult(m, ch, "raft.list", err.Error(), false)
}
result := &mgmt_pb.RaftMemberListResponse{}
for _, member := range members {
result.Members = append(result.Members, &mgmt_pb.RaftMember{
Id: member.Id,
Addr: member.Addr,
IsVoter: member.Voter,
IsLeader: member.Leader,
Version: member.Version,
IsConnected: member.Connected,
})
}
if err = protobufs.MarshalTyped(result).ReplyTo(m).WithTimeout(time.Second).Send(ch); err != nil {
pfxlog.Logger().WithError(err).Error("failure sending raft member list response")
}
}
func (self *Controller) agentOpRaftAddPeer(m *channel.Message, ch channel.Channel) {
addr, found := m.GetStringHeader(AgentAddrHeader)
if !found {
handler_common.SendOpResult(m, ch, "raft.join", "address not supplied", false)
return
}
id, found := m.GetStringHeader(AgentIdHeader)
if !found {
peerId, peerAddr, err := self.raftController.Mesh.GetPeerInfo(addr, 15*time.Second)
if err != nil {
errMsg := fmt.Sprintf("id not supplied and unable to retrieve [%v]", err.Error())
handler_common.SendOpResult(m, ch, "raft.join", errMsg, false)
return
}
id = string(peerId)
addr = string(peerAddr)
}
isVoter, found := m.GetBoolHeader(AgentIsVoterHeader)
if !found {
isVoter = true
}
req := &cmd_pb.AddPeerRequest{
Addr: addr,
Id: id,
IsVoter: isVoter,
}
if err := self.raftController.Join(req); err != nil {
handler_common.SendOpResult(m, ch, "cluster.add-peer", err.Error(), false)
return
}
handler_common.SendOpResult(m, ch, "cluster.add-peer", fmt.Sprintf("success, added %v at %v to cluster", id, addr), true)
}
func (self *Controller) agentOpRaftRemovePeer(m *channel.Message, ch channel.Channel) {
id, found := m.GetStringHeader(AgentIdHeader)
if !found {
handler_common.SendOpResult(m, ch, "cluster.remove-peer", "id not supplied", false)
return
}
req := &cmd_pb.RemovePeerRequest{
Id: id,
}
if err := self.raftController.HandleRemovePeer(req); err != nil {
handler_common.SendOpResult(m, ch, "cluster.remove-peer", err.Error(), false)
return
}
handler_common.SendOpResult(m, ch, "cluster.remove-peer", fmt.Sprintf("success, removed %v from cluster", id), true)
}
func (self *Controller) agentOpRaftTransferLeadership(m *channel.Message, ch channel.Channel) {
id, _ := m.GetStringHeader(AgentIdHeader)
req := &cmd_pb.TransferLeadershipRequest{
Id: id,
}
if err := self.raftController.HandleTransferLeadership(req); err != nil {
handler_common.SendOpResult(m, ch, "cluster.transfer-leadership", err.Error(), false)
return
}
handler_common.SendOpResult(m, ch, "cluster.transfer-leadership", "success", true)
}
func (self *Controller) agentOpInitFromDb(m *channel.Message, ch channel.Channel) {
sourceDbPath := string(m.Body)
if len(sourceDbPath) == 0 {
handler_common.SendOpResult(m, ch, "raft.initFromDb", "source db not supplied", false)
return
}
if err := self.InitializeRaftFromBoltDb(sourceDbPath); err != nil {
handler_common.SendOpResult(m, ch, "raft.initFromDb", err.Error(), false)
return
}
handler_common.SendOpResult(m, ch, "raft.initFromDb", fmt.Sprintf("success, initialized from [%v]", sourceDbPath), true)
}