/
server_serverannounce.go
279 lines (230 loc) · 6.04 KB
/
server_serverannounce.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"context"
"errors"
"io"
"net"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/chronos-tachyon/roxy/lib/roxyutil"
"github.com/chronos-tachyon/roxy/proto/roxy_v0"
"github.com/rs/zerolog"
)
func (s *ATCServer) ServerAnnounce(
sas roxy_v0.AirTrafficControl_ServerAnnounceServer,
) error {
if s.admin {
return status.Error(codes.PermissionDenied, "method ServerAnnounce not permitted over Admin interface")
}
ctx, logger := s.rpcBegin(sas.Context(), "ServerAnnounce")
logger.Trace().
Str("func", "ServerAnnounce.Recv").
Msg("Call")
req, err := sas.Recv()
if err != nil {
logger.Error().
Str("func", "ServerAnnounce.Recv").
Err(err).
Msg("Error")
return err
}
logger.Trace().
Interface("req", req).
Msg("Request")
first := req.First
if first == nil {
return status.Error(codes.InvalidArgument, "first is absent")
}
err = roxyutil.ValidateATCServiceName(first.ServiceName)
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}
err = roxyutil.ValidateATCUniqueID(first.UniqueId)
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}
err = roxyutil.ValidateATCLocation(first.Location)
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}
tcpAddr, err := makeTCPAddr(first.Ip, first.Port, first.Zone)
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}
impl := s.ref.AcquireSharedImpl()
needImplRelease := true
defer func() {
if needImplRelease {
s.ref.ReleaseSharedImpl()
}
}()
key, svc, err := impl.ServiceMap.CheckInput(first.ServiceName, first.ShardNumber, first.HasShardNumber, false)
logger.Debug().
Stringer("key", key).
Msg("RPC")
if err != nil {
return err
}
err = checkAuthInfo(ctx, svc.AllowedServerNames)
if err != nil {
return err
}
shardData := s.ref.GetOrInsertShard(key, svc, impl.CostMap)
shardData.Mutex.Lock()
serverData := shardData.GetOrInsertServerLocked(first, tcpAddr)
serverData.CostHistory.UpdateAbsolute(req.CostCounter)
serverData.UpdateLocked(true, req.IsServing)
goAwayCh := (<-chan *roxy_v0.GoAway)(serverData.GoAwayCh)
shardData.Mutex.Unlock()
needImplRelease = false
s.ref.ReleaseSharedImpl()
active := &activeServerAnnounce{
logger: logger,
s: s,
sas: sas,
shardData: shardData,
serverData: serverData,
errCh: make(chan error),
}
go active.recvThread(req.IsServing)
// Send an empty response as an acknowledgement that the first request
// has been received and accepted. This has the benefit of resetting
// the retry counter in lib/atcclient.
err = active.doSend(&roxy_v0.ServerAnnounceResponse{})
if err != nil {
return err
}
for {
select {
case <-gMultiServer.Done():
err := status.Error(codes.Unavailable, "ATC server is shutting down")
logger.Trace().
Err(err).
Msg("Return")
return err
case err := <-active.errCh:
return err
case goAway, ok := <-goAwayCh:
if !ok {
return <-active.errCh
}
if goAway == nil {
err := status.Errorf(codes.NotFound, "Key %v no longer exists", shardData.Key)
logger.Trace().
Err(err).
Msg("Return")
return err
}
err := active.doSend(&roxy_v0.ServerAnnounceResponse{GoAway: goAway})
if err == nil {
err = <-active.errCh
}
return err
}
}
}
type activeServerAnnounce struct {
logger zerolog.Logger
s *ATCServer
sas roxy_v0.AirTrafficControl_ServerAnnounceServer
shardData *ShardData
serverData *ServerData
errCh chan error
}
func (active *activeServerAnnounce) doSend(resp *roxy_v0.ServerAnnounceResponse) error {
active.logger.Trace().
Str("func", "ServerAnnounce.Send").
Interface("resp", resp).
Msg("Call")
err := active.sas.Send(resp)
if err != nil {
active.logger.Error().
Str("func", "ServerAnnounce.Send").
Err(err).
Msg("Error")
}
return err
}
func (active *activeServerAnnounce) recvThread(lastIsServing bool) {
var err error
var sendErr bool
defer func() {
active.shardData.Mutex.Lock()
active.serverData.CostHistory.Update()
active.serverData.UpdateLocked(false, lastIsServing)
active.shardData.Mutex.Unlock()
if sendErr {
select {
case active.errCh <- err:
default:
}
}
close(active.errCh)
}()
for {
active.logger.Trace().
Str("func", "ServerAnnounce.Recv").
Msg("Call")
var req *roxy_v0.ServerAnnounceRequest
req, err = active.sas.Recv()
s, ok := status.FromError(err)
switch {
case err == nil:
active.logger.Trace().
Str("func", "ServerAnnounce.Recv").
Interface("req", req).
Msg("Request")
case err == io.EOF:
fallthrough
case errors.Is(err, context.Canceled):
fallthrough
case errors.Is(err, context.DeadlineExceeded):
fallthrough
case ok && s.Code() == codes.Canceled:
active.logger.Trace().
Str("func", "ServerAnnounce.Recv").
Err(err).
Msg("Hangup")
return
default:
active.logger.Error().
Str("func", "ServerAnnounce.Recv").
Err(err).
Msg("Error")
sendErr = true
return
}
active.shardData.Mutex.Lock()
active.serverData.CostHistory.UpdateAbsolute(req.CostCounter)
active.serverData.UpdateLocked(true, req.IsServing)
active.shardData.Mutex.Unlock()
lastIsServing = req.IsServing
}
}
func makeTCPAddr(ip []byte, port uint32, zone string) (*net.TCPAddr, error) {
if port >= 65536 {
return nil, errors.New("invalid TCP port")
}
portNum := int(port)
if len(ip) != net.IPv4len && len(ip) != net.IPv6len {
return nil, errors.New("invalid IP address")
}
ipAddr := net.IP(ip)
if ipv4 := ipAddr.To4(); ipv4 != nil {
ipAddr = ipv4
}
isIPv6 := (len(ip) == net.IPv6len)
isLocal := ipAddr.IsLinkLocalUnicast() || ipAddr.IsLinkLocalMulticast() || ipAddr.IsInterfaceLocalMulticast()
if isIPv6 && isLocal && zone == "" {
return nil, errors.New("link-local IPv6 address requires zone")
}
if zone != "" && !isLocal && !isIPv6 {
return nil, errors.New("zone requires link-local IPv6 address")
}
tcpAddr := &net.TCPAddr{
IP: ipAddr,
Port: portNum,
Zone: zone,
}
return tcpAddr, nil
}