forked from uber/tchannel-go
/
relay.go
925 lines (803 loc) · 28.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
// 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 (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"sync"
"time"
"github.com/uber/tchannel-go/relay"
"github.com/uber/tchannel-go/typed"
"go.uber.org/atomic"
)
const (
// _defaultRelayMaxTombs is the default maximum number of tombs we'll accumulate
// in a single relayItems.
_defaultRelayMaxTombs = 3e4
// _relayTombTTL is the length of time we'll keep a tomb before GC'ing it.
_relayTombTTL = 3 * time.Second
// _defaultRelayMaxTimeout is the default max TTL for relayed calls.
_defaultRelayMaxTimeout = 2 * time.Minute
)
// Error strings.
const (
_relayErrorNotFound = "relay-not-found"
_relayErrorDestConnSlow = "relay-dest-conn-slow"
_relayErrorSourceConnSlow = "relay-source-conn-slow"
_relayArg2ModifyFailed = "relay-arg2-modify-failed"
// _relayNoRelease indicates that the relayed frame should not be released immediately, since
// relayed frames normally end up in a send queue where it is released afterward. However in some
// cases, such as frames that are fragmented due to being mutated, we need to release the original
// frame as it won't be relayed.
_relayNoRelease = false
_relayShouldRelease = true
)
// TODO: Replace errFrameNotSent with more specific errors from Receive.
var (
errRelayMethodFragmented = NewSystemError(ErrCodeBadRequest, "relay handler cannot receive fragmented calls")
errFrameNotSent = NewSystemError(ErrCodeNetwork, "frame was not sent to remote side")
errBadRelayHost = NewSystemError(ErrCodeDeclined, "bad relay host implementation")
errUnknownID = errors.New("non-callReq for inactive ID")
errNoNHInArg2 = errors.New("no nh in arg2")
errFragmentedArg2WithAppend = errors.New("fragmented arg2 not supported for appends")
errArg2ThriftOnly = errors.New("cannot inspect or modify arg2 for non-Thrift calls")
)
type relayItem struct {
remapID uint32
tomb bool
isOriginator bool
call RelayCall
destination *Relayer
span Span
timeout *relayTimer
mutatedChecksum Checksum
}
type relayItems struct {
sync.RWMutex
logger Logger
timeouts *relayTimerPool
maxTombs uint64
tombs uint64
items map[uint32]relayItem
}
type frameReceiver interface {
Receive(f *Frame, fType frameType) (sent bool, failureReason string)
}
func newRelayItems(logger Logger, maxTombs uint64) *relayItems {
if maxTombs == 0 {
maxTombs = _defaultRelayMaxTombs
}
return &relayItems{
items: make(map[uint32]relayItem),
logger: logger,
maxTombs: maxTombs,
}
}
func (ri *relayItem) reportRelayBytes(fType frameType, frameSize uint16) {
if fType == requestFrame {
ri.call.SentBytes(frameSize)
} else {
ri.call.ReceivedBytes(frameSize)
}
}
// 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, and will stop the timeout with the
// read lock held (to avoid a race between timeout stop and deletion).
// It returns whether a timeout was stopped, and if the item was found.
func (r *relayItems) Get(id uint32, stopTimeout bool) (_ relayItem, stopped bool, found bool) {
r.RLock()
defer r.RUnlock()
item, ok := r.items[id]
if !ok {
return relayItem{}, false /* stopped */, false /* found */
}
if !stopTimeout {
return item, false /* stopped */, true /* found */
}
return item, item.timeout.Stop(), true /* found */
}
// 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 the deleted item, along with a bool indicating whether we completed a
// relayed call.
func (r *relayItems) Delete(id uint32) (relayItem, 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 item, false
}
delete(r.items, id)
if item.tomb {
r.tombs--
}
r.Unlock()
item.timeout.Release()
return item, !item.tomb
}
// Entomb sets the tomb bit on a relayItem and schedules a garbage collection. It
// returns the entombed item, along with a bool indicating whether we completed
// a relayed call.
func (r *relayItems) Entomb(id uint32, deleteAfter time.Duration) (relayItem, bool) {
r.Lock()
if r.tombs > r.maxTombs {
r.Unlock()
r.logger.WithFields(LogField{"id", id}).Warn("Too many tombstones, deleting relay item immediately.")
return r.Delete(id)
}
item, ok := r.items[id]
if !ok {
r.Unlock()
r.logger.WithFields(LogField{"id", id}).Warn("Can't find relay item to entomb.")
return item, false
}
if item.tomb {
r.Unlock()
r.logger.WithFields(LogField{"id", id}).Warn("Re-entombing a tombstone.")
return item, 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 item, true
}
type frameType int
const (
requestFrame frameType = 0
responseFrame frameType = 1
)
// A Relayer forwards frames.
type Relayer struct {
relayHost RelayHost
maxTimeout time.Duration
maxConnTimeout time.Duration
// localHandlers is the set of service names that are handled by the local
// channel.
localHandler map[string]struct{}
// 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
// timeouts is the pool of timers used to track call timeouts.
// It allows timer re-use, while allowing timers to be created and started separately.
timeouts *relayTimerPool
peers *RootPeerList
conn *Connection
relayConn *relay.Conn
logger Logger
pending atomic.Uint32
}
// NewRelayer constructs a Relayer.
func NewRelayer(ch *Channel, conn *Connection) *Relayer {
r := &Relayer{
relayHost: ch.RelayHost(),
maxTimeout: ch.relayMaxTimeout,
maxConnTimeout: ch.relayMaxConnTimeout,
localHandler: ch.relayLocal,
outbound: newRelayItems(conn.log.WithFields(LogField{"relayItems", "outbound"}), ch.relayMaxTombs),
inbound: newRelayItems(conn.log.WithFields(LogField{"relayItems", "inbound"}), ch.relayMaxTombs),
peers: ch.RootPeers(),
conn: conn,
relayConn: &relay.Conn{
RemoteAddr: conn.conn.RemoteAddr().String(),
RemoteProcessName: conn.RemotePeerInfo().ProcessName,
IsOutbound: conn.connDirection == outbound,
Context: conn.baseContext,
},
logger: conn.log,
}
r.timeouts = newRelayTimerPool(r.timeoutRelayItem, ch.relayTimerVerify)
return r
}
// Relay is called for each frame that is read on the connection.
func (r *Relayer) Relay(f *Frame) (shouldRelease bool, _ error) {
if f.messageType() != messageTypeCallReq {
err := r.handleNonCallReq(f)
if err == errUnknownID {
// This ID may be owned by an outgoing call, so check the outbound
// message exchange, and if it succeeds, then the frame has been
// handled successfully.
if err := r.conn.outbound.forwardPeerFrame(f); err == nil {
return _relayNoRelease, nil
}
}
return _relayNoRelease, err
}
cr, err := newLazyCallReq(f)
if err != nil {
return _relayNoRelease, err
}
return r.handleCallReq(cr)
}
// Receive receives frames intended for this connection.
// It returns whether the frame was sent and a reason for failure if it failed.
func (r *Relayer) Receive(f *Frame, fType frameType) (sent bool, failureReason string) {
id := f.Header.ID
// 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)
finished := finishesCall(f)
// Stop the timeout if the call if finished.
item, stopped, ok := items.Get(id, finished /* stopTimeout */)
if !ok {
r.logger.WithFields(
LogField{"id", id},
).Warn("Received a frame without a RelayItem.")
return false, _relayErrorNotFound
}
if item.tomb || (finished && !stopped) {
// Item has previously timed out, or is in the process of timing out.
// TODO: metrics for late-arriving frames.
return true, ""
}
// call res frames don't include the OK bit, so we can't wait until the last
// frame of a relayed RPC to determine if the call succeeded.
if fType == responseFrame {
// If we've gotten a response frame, we're the originating relayer and
// should handle stats.
if succeeded, failMsg := determinesCallSuccess(f); succeeded {
item.call.Succeeded()
} else if len(failMsg) > 0 {
item.call.Failed(failMsg)
}
}
select {
case r.conn.sendCh <- f:
default:
// Buffer is full, so drop this frame and cancel the call.
// Since this is typically due to the send buffer being full, get send buffer
// usage + limit and add that to the log.
sendBuf, sendBufLimit, sendBufErr := r.conn.sendBufSize()
now := r.conn.timeNow().UnixNano()
logFields := []LogField{
{"id", id},
{"destConnSendBufferCurrent", sendBuf},
{"destConnSendBufferLimit", sendBufLimit},
{"sendChQueued", len(r.conn.sendCh)},
{"sendChCapacity", cap(r.conn.sendCh)},
{"lastActivityRead", r.conn.lastActivityRead.Load()},
{"lastActivityWrite", r.conn.lastActivityRead.Load()},
{"sinceLastActivityRead", time.Duration(now - r.conn.lastActivityRead.Load()).String()},
{"sinceLastActivityWrite", time.Duration(now - r.conn.lastActivityWrite.Load()).String()},
}
if sendBufErr != nil {
logFields = append(logFields, LogField{"destConnSendBufferError", sendBufErr.Error()})
}
r.logger.WithFields(logFields...).Warn("Dropping call due to slow connection.")
items := r.receiverItems(fType)
err := _relayErrorDestConnSlow
// If we're dealing with a response frame, then the client is slow.
if fType == responseFrame {
err = _relayErrorSourceConnSlow
}
r.failRelayItem(items, id, err, errFrameNotSent)
return false, err
}
if finished {
r.finishRelayItem(items, id)
}
return true, ""
}
func (r *Relayer) canHandleNewCall() (bool, connectionState) {
var (
canHandle bool
curState connectionState
)
r.conn.withStateRLock(func() error {
curState = r.conn.state
canHandle = curState == connectionActive
if canHandle {
r.pending.Inc()
}
return nil
})
return canHandle, curState
}
func (r *Relayer) getDestination(f *lazyCallReq, call RelayCall) (*Connection, bool, error) {
if _, _, ok := r.outbound.Get(f.Header.ID, false /* stopTimeout */); ok {
r.logger.WithFields(
LogField{"id", f.Header.ID},
LogField{"source", string(f.Caller())},
LogField{"dest", string(f.Service())},
LogField{"method", string(f.Method())},
).Warn("Received duplicate callReq.")
call.Failed(ErrCodeProtocol.relayMetricsKey())
// TODO: this is a protocol error, kill the connection.
return nil, false, errors.New("callReq with already active ID")
}
// Get the destination
peer, ok := call.Destination()
if !ok {
call.Failed("relay-bad-relay-host")
r.conn.SendSystemError(f.Header.ID, f.Span(), errBadRelayHost)
return nil, false, errBadRelayHost
}
remoteConn, err := peer.getConnectionRelay(f.TTL(), r.maxConnTimeout)
if err != nil {
r.logger.WithFields(
ErrField(err),
LogField{"source", string(f.Caller())},
LogField{"dest", string(f.Service())},
LogField{"method", string(f.Method())},
LogField{"selectedPeer", peer},
).Warn("Failed to connect to relay host.")
call.Failed("relay-connection-failed")
r.conn.SendSystemError(f.Header.ID, f.Span(), NewWrappedSystemError(ErrCodeNetwork, err))
return nil, false, nil
}
return remoteConn, true, nil
}
func (r *Relayer) handleCallReq(f *lazyCallReq) (shouldRelease bool, _ error) {
if handled := r.handleLocalCallReq(f); handled {
return _relayNoRelease, nil
}
call, err := r.relayHost.Start(f, r.relayConn)
if err != nil {
// If we have a RateLimitDropError we record the statistic, but
// we *don't* send an error frame back to the client.
if _, silentlyDrop := err.(relay.RateLimitDropError); silentlyDrop {
if call != nil {
call.Failed("relay-dropped")
call.End()
}
return _relayNoRelease, nil
}
if _, ok := err.(SystemError); !ok {
err = NewSystemError(ErrCodeDeclined, err.Error())
}
if call != nil {
call.Failed(GetSystemErrorCode(err).relayMetricsKey())
call.End()
}
r.conn.SendSystemError(f.Header.ID, f.Span(), err)
// If the RelayHost returns a protocol error, close the connection.
if GetSystemErrorCode(err) == ErrCodeProtocol {
return _relayNoRelease, r.conn.close(LogField{"reason", "RelayHost returned protocol error"})
}
return _relayNoRelease, nil
}
// Check that the current connection is in a valid state to handle a new call.
if canHandle, state := r.canHandleNewCall(); !canHandle {
call.Failed("relay-client-conn-inactive")
call.End()
err := errConnNotActive{"incoming", state}
r.conn.SendSystemError(f.Header.ID, f.Span(), NewWrappedSystemError(ErrCodeDeclined, err))
return _relayNoRelease, err
}
// Get a remote connection and check whether it can handle this call.
remoteConn, ok, err := r.getDestination(f, call)
if err == nil && ok {
if canHandle, state := remoteConn.relay.canHandleNewCall(); !canHandle {
err = NewWrappedSystemError(ErrCodeNetwork, errConnNotActive{"selected remote", state})
call.Failed("relay-remote-inactive")
r.conn.SendSystemError(f.Header.ID, f.Span(), NewWrappedSystemError(ErrCodeDeclined, err))
}
}
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.decrementPending()
call.End()
return _relayNoRelease, err
}
origID := f.Header.ID
destinationID := remoteConn.NextMessageID()
ttl := f.TTL()
if ttl > r.maxTimeout {
ttl = r.maxTimeout
f.SetTTL(r.maxTimeout)
}
span := f.Span()
var mutatedChecksum Checksum
if len(f.arg2Appends) > 0 {
mutatedChecksum = f.checksumType.New()
}
// The remote side of the relay doesn't need to track stats or call state.
remoteConn.relay.addRelayItem(false /* isOriginator */, destinationID, f.Header.ID, r, ttl, span, call, nil /* mutatedChecksum */)
relayToDest := r.addRelayItem(true /* isOriginator */, f.Header.ID, destinationID, remoteConn.relay, ttl, span, call, mutatedChecksum)
f.Header.ID = destinationID
// If we have appends, the size of the frame to be relayed will change, potentially going
// over the max frame size. Do a fragmenting send which is slightly more expensive but
// will handle fragmenting if it is needed.
if len(f.arg2Appends) > 0 {
if err := r.fragmentingSend(call, f, relayToDest, origID); err != nil && true {
r.failRelayItem(r.outbound, origID, _relayArg2ModifyFailed, err)
r.logger.WithFields(
LogField{"id", origID},
LogField{"err", err.Error()},
LogField{"caller", string(f.Caller())},
LogField{"dest", string(f.Service())},
LogField{"method", string(f.Method())},
).Warn("Failed to send call with modified arg2.")
}
// fragmentingSend always sends new frames in place of the old frame so we must
// release it separately
return _relayShouldRelease, nil
}
call.SentBytes(f.Frame.Header.FrameSize())
sent, failure := relayToDest.destination.Receive(f.Frame, requestFrame)
if !sent {
r.failRelayItem(r.outbound, origID, failure, errFrameNotSent)
return _relayNoRelease, nil
}
return _relayNoRelease, nil
}
// Handle all frames except messageTypeCallReq.
func (r *Relayer) handleNonCallReq(f *Frame) error {
frameType := frameTypeFor(f)
finished := finishesCall(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
}
// Stop the timeout if the call if finished.
item, stopped, ok := items.Get(f.Header.ID, finished /* stopTimeout */)
if !ok {
return errUnknownID
}
if item.tomb || (finished && !stopped) {
// Item has previously timed out, or is in the process of timing out.
// TODO: metrics for late-arriving frames.
return nil
}
// Recalculate and update the checksum for this frame if it has non-nil item.mutatedChecksum
// (meaning the call was mutated) and it is a callReqContinue frame.
if f.messageType() == messageTypeCallReqContinue && item.mutatedChecksum != nil {
r.updateMutatedCallReqContinueChecksum(f, item.mutatedChecksum)
}
// Track sent/received bytes. We don't do this before we check
// for timeouts, since this should only be called before call.End().
item.reportRelayBytes(frameType, f.Header.FrameSize())
originalID := f.Header.ID
f.Header.ID = item.remapID
sent, failure := item.destination.Receive(f, frameType)
if !sent {
r.failRelayItem(items, originalID, failure, errFrameNotSent)
return nil
}
if finished {
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, span Span, call RelayCall, mutatedChecksum Checksum) relayItem {
item := relayItem{
isOriginator: isOriginator,
call: call,
remapID: remapID,
destination: destination,
span: span,
mutatedChecksum: mutatedChecksum,
}
items := r.inbound
if isOriginator {
items = r.outbound
}
item.timeout = r.timeouts.Get()
items.Add(id, item)
item.timeout.Start(ttl, items, id, isOriginator)
return item
}
func (r *Relayer) timeoutRelayItem(items *relayItems, id uint32, isOriginator bool) {
item, ok := items.Entomb(id, _relayTombTTL)
if !ok {
return
}
if isOriginator {
r.conn.SendSystemError(id, item.span, ErrTimeout)
item.call.Failed("timeout")
item.call.End()
}
r.decrementPending()
}
// failRelayItem tombs the relay item so that future frames for this call are not
// forwarded. We keep the relay item tombed, rather than delete it to ensure that
// future frames do not cause error logs.
func (r *Relayer) failRelayItem(items *relayItems, id uint32, reason string, err error) {
// Stop the timeout, so we either fail it here, or in the timeout goroutine but not both.
item, stopped, found := items.Get(id, true /* stopTimeout */)
if !found {
items.logger.WithFields(LogField{"id", id}).Warn("Attempted to fail non-existent relay item.")
return
}
if !stopped {
return
}
// Entomb it so that we don't get unknown exchange errors on further frames
// for this call.
item, ok := items.Entomb(id, _relayTombTTL)
if !ok {
return
}
if item.isOriginator {
// If the client is too slow, then there's no point sending an error frame.
if reason != _relayErrorSourceConnSlow {
r.conn.SendSystemError(id, item.span, fmt.Errorf("%v: %v", reason, err))
}
item.call.Failed(reason)
item.call.End()
}
r.decrementPending()
}
func (r *Relayer) finishRelayItem(items *relayItems, id uint32) {
item, ok := items.Delete(id)
if !ok {
return
}
if item.isOriginator {
item.call.End()
if item.mutatedChecksum != nil {
item.mutatedChecksum.Release()
}
}
r.decrementPending()
}
func (r *Relayer) decrementPending() {
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 (r *Relayer) handleLocalCallReq(cr *lazyCallReq) (shouldRelease bool) {
// Check whether this is a service we want to handle locally.
if _, ok := r.localHandler[string(cr.Service())]; !ok {
return _relayNoRelease
}
f := cr.Frame
// We can only handle non-fragmented calls in the relay channel.
// This is a simplification to avoid back references from a mex to a
// relayItem so that the relayItem is cleared when the call completes.
if cr.HasMoreFragments() {
r.logger.WithFields(
LogField{"id", cr.Header.ID},
LogField{"source", string(cr.Caller())},
LogField{"dest", string(cr.Service())},
LogField{"method", string(cr.Method())},
).Error("Received fragmented callReq intended for local relay channel, can only handle unfragmented calls.")
r.conn.SendSystemError(f.Header.ID, cr.Span(), errRelayMethodFragmented)
return _relayShouldRelease
}
if release := r.conn.handleFrameNoRelay(f); release {
r.conn.opts.FramePool.Release(f)
}
return _relayShouldRelease
}
func (r *Relayer) fragmentingSend(call RelayCall, f *lazyCallReq, relayToDest relayItem, origID uint32) error {
if f.isArg2Fragmented {
return errFragmentedArg2WithAppend
}
if !bytes.Equal(f.as, _tchanThriftValueBytes) {
return fmt.Errorf("%v: got %s", errArg2ThriftOnly, f.as)
}
cs := relayToDest.mutatedChecksum
// TODO(echung): should we pool the writers?
fragWriter := newFragmentingWriter(
r.logger, r.newFragmentSender(relayToDest.destination, f, origID, call),
cs,
)
arg2Writer, err := fragWriter.ArgWriter(false /* last */)
if err != nil {
return fmt.Errorf("get arg2 writer: %v", err)
}
if err := writeArg2WithAppends(arg2Writer, f.arg2(), f.arg2Appends); err != nil {
return fmt.Errorf("write arg2: %v", err)
}
if err := arg2Writer.Close(); err != nil {
return fmt.Errorf("close arg2 writer: %v", err)
}
if err := NewArgWriter(fragWriter.ArgWriter(true /* last */)).Write(f.arg3()); err != nil {
return errors.New("arg3 write failed")
}
return nil
}
func (r *Relayer) updateMutatedCallReqContinueChecksum(f *Frame, cs Checksum) {
rbuf := typed.NewReadBuffer(f.SizedPayload())
rbuf.SkipBytes(1) // flags
rbuf.SkipBytes(1) // checksum type: this should match the checksum type of the callReq frame
checksumRef := typed.BytesRef(rbuf.ReadBytes(cs.Size()))
// We only support non-fragmented arg2 for mutated calls, so by the time we hit callReqContinue both
// arg1 and arg2 must already have been read. As the call would be finished when we've read all of
// arg3, it isn't necessary to separately track its completion.
//
// In theory we could have a frame with 0-length arg3, which can happen if a manual flush occurred
// after writing 0 bytes for arg3. This is handled correctly by
// 1) reading n=0 (nArg3)
// 2) reading 0 bytes from the rbuf
// 3) updating the checksum with the current running checksum
//
// Additionally, if the checksum type results in a 0-length checksum, the .Update() would
// become a copy between empty slices, which correctly becomes a noop.
// TODO(cinchurge): include a test for len(arg3)==0 in the unit tests
n := rbuf.ReadUint16()
cs.Add(rbuf.ReadBytes(int(n)))
checksumRef.Update(cs.Sum())
}
func writeArg2WithAppends(w io.WriteCloser, arg2 []byte, appends []relay.KeyVal) (err error) {
if len(arg2) < 2 {
return errNoNHInArg2
}
writer := typed.NewWriter(w)
// nh:2 is the first two bytes of arg2, which should always be present
nh := binary.BigEndian.Uint16(arg2[:2]) + uint16(len(appends))
writer.WriteUint16(nh)
// arg2[2:] is the existing sequence of key/val pairs, which we can just copy
// over verbatim
if len(arg2) > 2 {
writer.WriteBytes(arg2[2:])
}
// append new key/val pairs to end of arg2
for _, kv := range appends {
writer.WriteLen16Bytes(kv.Key)
writer.WriteLen16Bytes(kv.Val)
}
return writer.Err()
}
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 determinesCallSuccess(f *Frame) (succeeded bool, failMsg string) {
switch f.messageType() {
case messageTypeError:
msg := newLazyError(f).Code().MetricsKey()
return false, msg
case messageTypeCallRes:
if newLazyCallRes(f).OK() {
return true, ""
}
return false, "application-error"
default:
return false, ""
}
}
func validateRelayMaxTimeout(d time.Duration, logger Logger) time.Duration {
maxMillis := d / time.Millisecond
if maxMillis > 0 && maxMillis <= math.MaxUint32 {
return d
}
if d == 0 {
return _defaultRelayMaxTimeout
}
logger.WithFields(
LogField{"configuredMaxTimeout", d},
LogField{"defaultMaxTimeout", _defaultRelayMaxTimeout},
).Warn("Configured RelayMaxTimeout is invalid, using default instead.")
return _defaultRelayMaxTimeout
}
type sentBytesReporter interface {
SentBytes(size uint16)
}
type relayFragmentSender struct {
callReq *lazyCallReq
framePool FramePool
frameReceiver frameReceiver
failRelayItemFunc func(items *relayItems, id uint32, failure string, err error)
outboundRelayItems *relayItems
origID uint32
sentReporter sentBytesReporter
}
func (r *Relayer) newFragmentSender(dstRelay frameReceiver, cr *lazyCallReq, origID uint32, sentReporter sentBytesReporter) *relayFragmentSender {
// TODO(cinchurge): pool fragment senders
return &relayFragmentSender{
callReq: cr,
framePool: r.conn.opts.FramePool,
frameReceiver: dstRelay,
failRelayItemFunc: r.failRelayItem,
outboundRelayItems: r.outbound,
origID: origID,
sentReporter: sentReporter,
}
}
func (rfs *relayFragmentSender) newFragment(initial bool, checksum Checksum) (*writableFragment, error) {
frame := rfs.framePool.Get()
frame.Header.ID = rfs.callReq.Header.ID
if initial {
frame.Header.messageType = messageTypeCallReq
} else {
frame.Header.messageType = messageTypeCallReqContinue
}
contents := typed.NewWriteBuffer(frame.Payload[:])
// flags:1
// Flags MUST be copied over from the callReq frame to all new fragments since if there are more
// fragments to follow the callReq, the destination needs to know about this or those frames will
// be dropped from the call
flagsRef := contents.DeferByte()
flagsRef.Update(rfs.callReq.Payload[_flagsIndex])
if initial {
// Copy all data before the checksum for the initial frame
contents.WriteBytes(rfs.callReq.Payload[_flagsIndex+1 : rfs.callReq.checksumTypeOffset])
}
// checksumType:1
contents.WriteSingleByte(byte(checksum.TypeCode()))
// checksum: checksum.Size()
checksumRef := contents.DeferBytes(checksum.Size())
if initial {
// arg1~1: write arg1 to the initial frame
contents.WriteUint16(uint16(len(rfs.callReq.method)))
contents.WriteBytes(rfs.callReq.method)
checksum.Add(rfs.callReq.method)
}
// TODO(cinchurge): pool writableFragment
return &writableFragment{
flagsRef: flagsRef,
checksumRef: checksumRef,
// checksum will be released by the relayer when the call is finished
checksum: &noReleaseChecksum{Checksum: checksum},
contents: contents,
frame: frame,
}, contents.Err()
}
func (rfs *relayFragmentSender) flushFragment(wf *writableFragment) error {
wf.frame.Header.SetPayloadSize(uint16(wf.contents.BytesWritten()))
rfs.sentReporter.SentBytes(wf.frame.Header.FrameSize())
sent, failure := rfs.frameReceiver.Receive(wf.frame, requestFrame)
if !sent {
rfs.failRelayItemFunc(rfs.outboundRelayItems, rfs.origID, failure, errFrameNotSent)
return nil
}
return nil
}
func (rfs *relayFragmentSender) doneSending() {}