-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_endpoint_leaf.go
209 lines (186 loc) · 5.17 KB
/
rpc_endpoint_leaf.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
package net
import (
"context"
"errors"
"net"
"sync"
"time"
"github.com/horus-scheduler/horus_controller/core"
"github.com/horus-scheduler/horus_controller/core/model"
horus_pb "github.com/horus-scheduler/horus_controller/protobuf"
grpcpool "github.com/processout/grpc-go-pool"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
type LeafRpcEndpoint struct {
sync.RWMutex
ctrlID uint16
SrvCentralAddr string
SrvLAddr string
topology *model.Topology
vcm *core.VCManager
updatedServersEgExt chan *core.LeafHealthMsg
newServersEgExt chan *ServerAddedMessage
newVCsEgExt chan *VCUpdatedMessage
rpcServer *grpc.Server
centralConnPool *grpcpool.Pool
DoneChan chan bool
}
func NewLeafRpcEndpoint(srvLAddr, srvCentralAddr string,
topology *model.Topology,
vcm *core.VCManager,
updatedServers chan *core.LeafHealthMsg,
newServers chan *ServerAddedMessage,
newVCs chan *VCUpdatedMessage,
) *LeafRpcEndpoint {
return &LeafRpcEndpoint{
SrvLAddr: srvLAddr,
SrvCentralAddr: srvCentralAddr,
topology: topology,
vcm: vcm,
updatedServersEgExt: updatedServers,
newServersEgExt: newServers,
newVCsEgExt: newVCs,
DoneChan: make(chan bool, 1),
}
}
func NewBareLeafRpcEndpoint(ctrlID uint16, srvCentralAddr string,
updatedServers chan *core.LeafHealthMsg,
newServers chan *ServerAddedMessage,
newVCs chan *VCUpdatedMessage,
) *LeafRpcEndpoint {
return &LeafRpcEndpoint{
ctrlID: ctrlID,
SrvCentralAddr: srvCentralAddr,
updatedServersEgExt: updatedServers,
newServersEgExt: newServers,
newVCsEgExt: newVCs,
DoneChan: make(chan bool, 1),
}
}
func (s *LeafRpcEndpoint) SetLocalAddress(address string) {
if len(s.SrvLAddr) == 0 {
s.SrvLAddr = address
}
}
func (s *LeafRpcEndpoint) SetTopology(topology *model.Topology) {
if s.topology == nil {
s.topology = topology
}
}
func (s *LeafRpcEndpoint) SetVCManager(vcm *core.VCManager) {
if s.vcm == nil {
s.vcm = vcm
}
}
func (s *LeafRpcEndpoint) createServiceListener() error {
lis, err := net.Listen("tcp4", s.SrvLAddr)
if err != nil {
logrus.Warn(err)
return err
}
if len(s.SrvLAddr) == 0 {
logrus.Error("[LeafRPC] serves has no local address")
return errors.New("leaf RPC serves has no local address")
}
if s.topology == nil {
logrus.Errorf("[LeafRPC] server %s has no topology", s.SrvLAddr)
return errors.New("leaf RPC serves has no topology")
}
if s.vcm == nil {
logrus.Errorf("[LeafRPC] server %s has no VC Manager", s.SrvLAddr)
return errors.New("leaf RPC serves has no VC Manager")
}
logrus.Infof("[LeafRPC] Creating topologyServer at %s", s.SrvLAddr)
s.rpcServer = grpc.NewServer()
topoServer := NewLeafSrvServer(s.topology, s.vcm,
s.updatedServersEgExt,
s.newServersEgExt,
s.newVCsEgExt)
horus_pb.RegisterHorusServiceServer(s.rpcServer, topoServer)
return s.rpcServer.Serve(lis)
}
func (s *LeafRpcEndpoint) createConnPool() {
s.Lock()
defer s.Unlock()
var factory grpcpool.Factory
factory = func() (*grpc.ClientConn, error) {
conn, err := grpc.Dial(s.SrvCentralAddr, grpc.WithInsecure())
if err != nil {
logrus.Error(err)
}
return conn, err
}
var err error
s.centralConnPool, err = grpcpool.New(factory, 10, 20, 5*time.Second)
if err != nil {
logrus.Error(err)
}
}
func (s *LeafRpcEndpoint) GetVCs() ([]*horus_pb.VCInfo, error) {
s.RLock()
defer s.RUnlock()
conn, err := s.centralConnPool.Get(context.Background())
if err != nil {
logrus.Error(err)
return nil, err
}
defer conn.Close()
client := horus_pb.NewHorusServiceClient(conn.ClientConn)
leafInfo := &horus_pb.LeafInfo{Id: uint32(s.ctrlID)}
resp, err := client.GetVCsOfLeaf(context.Background(), leafInfo)
if err != nil {
vcs := make([]*horus_pb.VCInfo, 0)
return vcs, nil
}
return resp.Vcs, err
}
func (s *LeafRpcEndpoint) GetTopology() (*horus_pb.TopoInfo, error) {
s.RLock()
defer s.RUnlock()
conn, err := s.centralConnPool.Get(context.Background())
if err != nil {
logrus.Error(err)
return nil, err
}
defer conn.Close()
client := horus_pb.NewHorusServiceClient(conn.ClientConn)
leafInfo := &horus_pb.LeafInfo{Id: uint32(s.ctrlID)}
topoInfo, err := client.GetTopologyAtLeaf(context.Background(), leafInfo)
return topoInfo, err
}
func (s *LeafRpcEndpoint) processEvents() {
for {
stop := false
if stop {
break
}
select {
case <-s.DoneChan:
logrus.Infof("[LeafRPC] Shutting down the server at: %s", s.SrvLAddr)
s.centralConnPool.Close()
s.rpcServer.Stop()
stop = true
default:
continue
}
}
}
func (s *LeafRpcEndpoint) StartClient() {
// create a pool of gRPC connections. used to send SyncEvent messages
s.createConnPool()
// Let's send any outstanding events
go s.processEvents()
}
func (s *LeafRpcEndpoint) StartServer() {
// creates the end point server. used to recv SessionUpdateEvent messages
go s.createServiceListener()
}
func (s *LeafRpcEndpoint) Start() {
// create a pool of gRPC connections. used to send SyncEvent messages
s.createConnPool()
// creates the end point server. used to recv SessionUpdateEvent messages
go s.createServiceListener()
// Let's send any outstanding events
go s.processEvents()
}