/
relay.go
390 lines (334 loc) · 10.4 KB
/
relay.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tchannel
import (
"errors"
"fmt"
"sync"
"time"
"github.com/uber/tchannel-go/atomic"
"github.com/uber/tchannel-go/relay"
)
// _maxRelayTombs is the maximum number of tombs we'll accumulate in a single
// relayItems.
const _maxRelayTombs = 1e4
// _relayTombTTL is the length of time we'll keep a tomb before GC'ing it.
const _relayTombTTL = time.Second
type relayItem struct {
*time.Timer
remapID uint32
destination *Relayer
tomb bool
}
type relayItems struct {
sync.RWMutex
logger Logger
tombs uint64
items map[uint32]relayItem
}
func newRelayItems(logger Logger) *relayItems {
return &relayItems{
items: make(map[uint32]relayItem),
logger: logger,
}
}
// Count returns the number of non-tombstone items in the relay.
func (r *relayItems) Count() int {
r.RLock()
n := len(r.items) - int(r.tombs)
r.RUnlock()
return n
}
// Get checks for a relay item by ID, returning the item and a bool indicating
// whether the item was found.
func (r *relayItems) Get(id uint32) (relayItem, bool) {
r.RLock()
item, ok := r.items[id]
r.RUnlock()
return item, ok
}
// Add adds a relay item.
func (r *relayItems) Add(id uint32, item relayItem) {
r.Lock()
r.items[id] = item
r.Unlock()
}
// Delete removes a relayItem completely (without leaving a tombstone). It returns
// a bool indicating whether we completed a relayed call.
func (r *relayItems) Delete(id uint32) bool {
r.Lock()
item, ok := r.items[id]
if !ok {
r.Unlock()
r.logger.WithFields(LogField{"id", id}).Warn("Attempted to delete non-existent relay item.")
return false
}
delete(r.items, id)
if item.tomb {
r.tombs--
}
r.Unlock()
item.Stop()
return !item.tomb
}
// Entomb sets the tomb bit on a relayItem and schedules a garbage collection. It
// returns a bool indicating whether we completed a relayed call.
func (r *relayItems) Entomb(id uint32, deleteAfter time.Duration) bool {
r.Lock()
if r.tombs > _maxRelayTombs {
r.Unlock()
r.logger.WithFields(LogField{"id", id}).Warn("Too many tombstones, deleting relay item immediately.")
return false
}
item, ok := r.items[id]
if !ok {
r.Unlock()
r.logger.WithFields(LogField{"id", id}).Warn("Can't find relay item to entomb.")
return false
}
if item.tomb {
r.Unlock()
r.logger.WithFields(LogField{"id", id}).Warn("Re-entombing a tombstone.")
return false
}
r.tombs++
item.tomb = true
r.items[id] = item
r.Unlock()
// TODO: We should be clearing these out in batches, rather than creating
// individual timers for each item.
time.AfterFunc(deleteAfter, func() { r.Delete(id) })
return true
}
type frameType int
const (
requestFrame frameType = 0
responseFrame frameType = 1
)
// A Relayer forwards frames.
type Relayer struct {
metrics StatsReporter
hosts relay.Hosts
// outbound is the remapping for requests that originated on this
// connection, and are outbound towards some other connection.
// It stores remappings for all request frames read on this connection.
outbound *relayItems
// inbound is the remapping for requests that originated on some other
// connection which was directed to this connection.
// It stores remappings for all response frames read on this connection.
inbound *relayItems
peers *PeerList
conn *Connection
logger Logger
pending atomic.Uint32
}
// NewRelayer constructs a Relayer.
func NewRelayer(ch *Channel, conn *Connection) *Relayer {
return &Relayer{
metrics: conn.statsReporter,
hosts: ch.RelayHosts(),
outbound: newRelayItems(ch.Logger().WithFields(LogField{"relay", "outbound"})),
inbound: newRelayItems(ch.Logger().WithFields(LogField{"relay", "inbound"})),
peers: ch.Peers(),
conn: conn,
logger: conn.log,
}
}
// Hosts returns the RelayHosts guiding peer selection.
func (r *Relayer) Hosts() relay.Hosts {
return r.hosts
}
// Relay is called for each frame that is read on the connection.
func (r *Relayer) Relay(f *Frame) error {
if f.messageType() != messageTypeCallReq {
return r.handleNonCallReq(f)
}
return r.handleCallReq(newLazyCallReq(f))
}
// Receive receives frames intended for this connection.
func (r *Relayer) Receive(f *Frame, fType frameType) {
{
// TODO: Since this block is only checking for safety, we should not
// enable this in production builds.
// If we receive a response frame, we expect to find that ID in our outbound.
// If we receive a request frame, we expect to find that ID in our inbound.
items := r.receiverItems(fType)
if _, ok := items.Get(f.Header.ID); !ok {
r.logger.WithFields(
LogField{"ID", f.Header.ID},
).Warn("Received a frame without a RelayItem.")
}
}
r.conn.sendCh <- f
if finishesCall(f) {
items := r.receiverItems(fType)
r.finishRelayItem(items, f.Header.ID)
}
}
func (r *Relayer) canHandleNewCall() bool {
var canHandle bool
r.conn.withStateRLock(func() error {
canHandle = r.conn.state == connectionActive
if canHandle {
r.pending.Inc()
}
return nil
})
return canHandle
}
func (r *Relayer) getDestination(f lazyCallReq) (*Connection, bool, error) {
if _, ok := r.outbound.Get(f.Header.ID); ok {
r.logger.WithFields(LogField{"id", f.Header.ID}).Warn("received duplicate callReq")
// TODO: this is a protocol error, kill the connection.
return nil, false, errors.New("callReq with already active ID")
}
// Get the destination
hostPort := r.hosts.Get(f)
if hostPort == "" {
// TODO: What is the span in the error frame actually used for, and do we need it?
r.conn.SendSystemError(f.Header.ID, nil, errUnknownGroup(f.Service()))
return nil, false, nil
}
peer := r.peers.GetOrAdd(hostPort)
// TODO: Should connections use the call timeout? Or a separate timeout?
remoteConn, err := peer.getConnectionTimeout(f.TTL())
if err != nil {
r.logger.WithFields(
ErrField(err),
LogField{"hostPort", hostPort},
).Warn("Failed to connect to relay host.")
// TODO: Same as above, do we need span here?
r.conn.SendSystemError(f.Header.ID, nil, NewWrappedSystemError(ErrCodeNetwork, err))
return nil, false, nil
}
return remoteConn, true, nil
}
func (r *Relayer) handleCallReq(f lazyCallReq) error {
if !r.canHandleNewCall() {
return ErrChannelClosed
}
// Get a remote connection and check whether it can handle this call.
remoteConn, ok, err := r.getDestination(f)
if err == nil && ok {
if !remoteConn.relay.canHandleNewCall() {
err = NewSystemError(ErrCodeNetwork, "selected closed connection, retry")
}
}
if err != nil || !ok {
// Failed to get a remote connection, or the connection is not in the right
// state to handle this call. Since we already incremented pending on
// the current relay, we need to decrement it.
r.pending.Dec()
return err
}
destinationID := remoteConn.NextMessageID()
ttl := f.TTL()
remoteConn.relay.addRelayItem(false /* isOriginator */, destinationID, f.Header.ID, r, ttl)
r.metrics.IncCounter("relay", nil, 1)
relayToDest := r.addRelayItem(true /* isOriginator */, f.Header.ID, destinationID, remoteConn.relay, ttl)
f.Header.ID = destinationID
relayToDest.destination.Receive(f.Frame, requestFrame)
return nil
}
// Handle all frames except messageTypeCallReq.
func (r *Relayer) handleNonCallReq(f *Frame) error {
frameType := frameTypeFor(f)
// If we read a request frame, we need to use the outbound map to decide
// the destination. Otherwise, we use the inbound map.
items := r.outbound
if frameType == responseFrame {
items = r.inbound
}
item, ok := items.Get(f.Header.ID)
if !ok {
return errors.New("non-callReq for inactive ID")
}
if item.tomb {
// Call timed out, ignore this frame.
// TODO: Add metrics for this case.
return nil
}
originalID := f.Header.ID
f.Header.ID = item.remapID
item.destination.Receive(f, frameType)
if finishesCall(f) {
r.finishRelayItem(items, originalID)
}
return nil
}
// addRelayItem adds a relay item to either outbound or inbound.
func (r *Relayer) addRelayItem(isOriginator bool, id, remapID uint32, destination *Relayer, ttl time.Duration) relayItem {
item := relayItem{
remapID: remapID,
destination: destination,
}
items := r.inbound
if isOriginator {
items = r.outbound
}
item.Timer = time.AfterFunc(ttl, func() { r.timeoutRelayItem(items, id, isOriginator) })
items.Add(id, item)
return item
}
func (r *Relayer) timeoutRelayItem(items *relayItems, id uint32, isOriginator bool) {
if ok := items.Entomb(id, _relayTombTTL); !ok {
return
}
if isOriginator {
// TODO: As above. What's the span in the error frame for?
r.conn.SendSystemError(id, nil, ErrTimeout)
}
r.pending.Dec()
r.conn.checkExchanges()
}
func (r *Relayer) finishRelayItem(items *relayItems, id uint32) {
if ok := items.Delete(id); !ok {
return
}
r.pending.Dec()
r.conn.checkExchanges()
}
func (r *Relayer) canClose() bool {
if r == nil {
return true
}
return r.countPending() == 0
}
func (r *Relayer) countPending() uint32 {
return r.pending.Load()
}
func (r *Relayer) receiverItems(fType frameType) *relayItems {
if fType == requestFrame {
return r.inbound
}
return r.outbound
}
func frameTypeFor(f *Frame) frameType {
switch t := f.Header.messageType; t {
case messageTypeCallRes, messageTypeCallResContinue, messageTypeError, messageTypePingRes:
return responseFrame
case messageTypeCallReq, messageTypeCallReqContinue, messageTypePingReq:
return requestFrame
default:
panic(fmt.Sprintf("unsupported frame type: %v", t))
}
}
func errUnknownGroup(group string) error {
return NewSystemError(ErrCodeDeclined, "no peers for %q", group)
}