-
Notifications
You must be signed in to change notification settings - Fork 83
/
relay_messages.go
347 lines (281 loc) · 9.66 KB
/
relay_messages.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
// 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"
"fmt"
"time"
"github.com/uber/tchannel-go/relay"
"github.com/uber/tchannel-go/thrift/arg2"
"github.com/uber/tchannel-go/typed"
)
var _ relay.RespFrame = (*lazyCallRes)(nil)
var (
_callerNameKeyBytes = []byte(CallerName)
_routingDelegateKeyBytes = []byte(RoutingDelegate)
_routingKeyKeyBytes = []byte(RoutingKey)
_argSchemeKeyBytes = []byte(ArgScheme)
_tchanThriftValueBytes = []byte(Thrift)
)
const (
// Common to many frame types.
_flagsIndex = 0
// For call req, indexes into the frame.
// Use int for indexes to avoid overflow caused by accidental byte arithmentic.
_ttlIndex int = 1
_ttlLen int = 4
_spanIndex int = _ttlIndex + _ttlLen
_spanLength int = 25
_serviceLenIndex int = _spanIndex + _spanLength
_serviceNameIndex int = _serviceLenIndex + 1
// For call res and call res continue.
_resCodeOK = 0x00
_resCodeIndex int = 1
// For error.
_errCodeIndex int = 0
)
type lazyError struct {
*Frame
}
func newLazyError(f *Frame) lazyError {
if msgType := f.Header.messageType; msgType != messageTypeError {
panic(fmt.Errorf("newLazyError called for wrong messageType: %v", msgType))
}
return lazyError{f}
}
func (e lazyError) Code() SystemErrCode {
return SystemErrCode(e.Payload[_errCodeIndex])
}
type lazyCallRes struct {
*Frame
as []byte
arg2IsFragmented bool
arg2Payload []byte
}
func newLazyCallRes(f *Frame) (lazyCallRes, error) {
if msgType := f.Header.messageType; msgType != messageTypeCallRes {
panic(fmt.Errorf("newLazyCallRes called for wrong messageType: %v", msgType))
}
rbuf := typed.NewReadBuffer(f.SizedPayload())
rbuf.SkipBytes(1) // flags
rbuf.SkipBytes(1) // code
rbuf.SkipBytes(_spanLength) // tracing
var as []byte
nh := int(rbuf.ReadSingleByte())
for i := 0; i < nh; i++ {
keyLen := int(rbuf.ReadSingleByte())
key := rbuf.ReadBytes(keyLen)
valLen := int(rbuf.ReadSingleByte())
val := rbuf.ReadBytes(valLen)
if bytes.Equal(key, _argSchemeKeyBytes) {
as = val
continue
}
}
csumtype := ChecksumType(rbuf.ReadSingleByte()) // csumtype
rbuf.SkipBytes(csumtype.ChecksumSize()) // csum
// arg1: ignored
narg1 := int(rbuf.ReadUint16())
rbuf.SkipBytes(narg1)
// arg2: keep track of payload
narg2 := int(rbuf.ReadUint16())
arg2Payload := rbuf.ReadBytes(narg2)
arg2IsFragmented := rbuf.BytesRemaining() == 0 && hasMoreFragments(f)
// arg3: ignored
// Make sure we didn't hit any issues reading the buffer
if err := rbuf.Err(); err != nil {
return lazyCallRes{}, fmt.Errorf("read response frame: %v", err)
}
return lazyCallRes{
Frame: f,
as: as,
arg2IsFragmented: arg2IsFragmented,
arg2Payload: arg2Payload,
}, nil
}
// OK implements relay.RespFrame
func (cr lazyCallRes) OK() bool {
return isCallResOK(cr.Frame)
}
// ArgScheme implements relay.RespFrame
func (cr lazyCallRes) ArgScheme() []byte {
return cr.as
}
// Arg2IsFragmented implements relay.RespFrame
func (cr lazyCallRes) Arg2IsFragmented() bool {
return cr.arg2IsFragmented
}
// Arg2 implements relay.RespFrame
func (cr lazyCallRes) Arg2() []byte {
return cr.arg2Payload
}
type lazyCallReq struct {
*Frame
checksumTypeOffset uint16
arg2StartOffset, arg2EndOffset uint16
arg3StartOffset uint16
caller, method, delegate, key, as []byte
arg2Appends []relay.KeyVal
checksumType ChecksumType
isArg2Fragmented bool
// Intentionally an array to combine allocations with that of lazyCallReq
arg2InitialBuf [1]relay.KeyVal
}
// TODO: Consider pooling lazyCallReq and using pointers to the struct.
func newLazyCallReq(f *Frame) (*lazyCallReq, error) {
if msgType := f.Header.messageType; msgType != messageTypeCallReq {
panic(fmt.Errorf("newLazyCallReq called for wrong messageType: %v", msgType))
}
cr := &lazyCallReq{Frame: f}
cr.arg2Appends = cr.arg2InitialBuf[:0]
rbuf := typed.NewReadBuffer(f.SizedPayload())
rbuf.SkipBytes(_serviceLenIndex)
// service~1
serviceLen := rbuf.ReadSingleByte()
rbuf.SkipBytes(int(serviceLen))
// nh:1 (hk~1 hv~1){nh}
numHeaders := int(rbuf.ReadSingleByte())
for i := 0; i < numHeaders; i++ {
keyLen := int(rbuf.ReadSingleByte())
key := rbuf.ReadBytes(keyLen)
valLen := int(rbuf.ReadSingleByte())
val := rbuf.ReadBytes(valLen)
if bytes.Equal(key, _argSchemeKeyBytes) {
cr.as = val
} else if bytes.Equal(key, _callerNameKeyBytes) {
cr.caller = val
} else if bytes.Equal(key, _routingDelegateKeyBytes) {
cr.delegate = val
} else if bytes.Equal(key, _routingKeyKeyBytes) {
cr.key = val
}
}
// csumtype:1 (csum:4){0,1} arg1~2 arg2~2 arg3~2
cr.checksumTypeOffset = uint16(rbuf.BytesRead())
cr.checksumType = ChecksumType(rbuf.ReadSingleByte())
rbuf.SkipBytes(cr.checksumType.ChecksumSize())
// arg1~2
arg1Len := int(rbuf.ReadUint16())
cr.method = rbuf.ReadBytes(arg1Len)
// arg2~2
arg2Len := rbuf.ReadUint16()
cr.arg2StartOffset = uint16(rbuf.BytesRead())
cr.arg2EndOffset = cr.arg2StartOffset + arg2Len
// arg2 is fragmented if we don't see arg3 in this frame.
rbuf.SkipBytes(int(arg2Len))
cr.isArg2Fragmented = rbuf.BytesRemaining() == 0 && cr.HasMoreFragments()
if !cr.isArg2Fragmented {
// arg3~2
rbuf.SkipBytes(2)
cr.arg3StartOffset = uint16(rbuf.BytesRead())
}
if rbuf.Err() != nil {
return nil, rbuf.Err()
}
return cr, nil
}
// Caller returns the name of the originator of this callReq.
func (f *lazyCallReq) Caller() []byte {
return f.caller
}
// Service returns the name of the destination service for this callReq.
func (f *lazyCallReq) Service() []byte {
l := f.Payload[_serviceLenIndex]
return f.Payload[_serviceNameIndex : _serviceNameIndex+int(l)]
}
// Method returns the name of the method being called.
func (f *lazyCallReq) Method() []byte {
return f.method
}
// RoutingDelegate returns the routing delegate for this call req, if any.
func (f *lazyCallReq) RoutingDelegate() []byte {
return f.delegate
}
// RoutingKey returns the routing delegate for this call req, if any.
func (f *lazyCallReq) RoutingKey() []byte {
return f.key
}
// TTL returns the time to live for this callReq.
func (f *lazyCallReq) TTL() time.Duration {
ttl := binary.BigEndian.Uint32(f.Payload[_ttlIndex : _ttlIndex+_ttlLen])
return time.Duration(ttl) * time.Millisecond
}
// SetTTL overwrites the frame's TTL.
func (f *lazyCallReq) SetTTL(d time.Duration) {
ttl := uint32(d / time.Millisecond)
binary.BigEndian.PutUint32(f.Payload[_ttlIndex:_ttlIndex+_ttlLen], ttl)
}
// Span returns the Span
func (f *lazyCallReq) Span() Span {
return callReqSpan(f.Frame)
}
// HasMoreFragments returns whether the callReq has more fragments.
func (f *lazyCallReq) HasMoreFragments() bool {
return f.Payload[_flagsIndex]&hasMoreFragmentsFlag != 0
}
// Arg2EndOffset returns the offset from start of payload to the end of Arg2
// in bytes, and hasMore to be true if there are more frames and arg3 has
// not started.
func (f *lazyCallReq) Arg2EndOffset() (_ int, hasMore bool) {
return int(f.arg2EndOffset), f.isArg2Fragmented
}
// Arg2StartOffset returns the offset from start of payload to the beginning
// of Arg2 in bytes.
func (f *lazyCallReq) Arg2StartOffset() int {
return int(f.arg2StartOffset)
}
func (f *lazyCallReq) arg2() []byte {
return f.Payload[f.arg2StartOffset:f.arg2EndOffset]
}
func (f *lazyCallReq) arg3() []byte {
return f.SizedPayload()[f.arg3StartOffset:]
}
// Arg2Iterator returns the iterator for reading Arg2 key value pair
// of TChannel-Thrift Arg Scheme.
func (f *lazyCallReq) Arg2Iterator() (arg2.KeyValIterator, error) {
if !bytes.Equal(f.as, _tchanThriftValueBytes) {
return arg2.KeyValIterator{}, fmt.Errorf("%v: got %s", errArg2ThriftOnly, f.as)
}
return arg2.NewKeyValIterator(f.Payload[f.arg2StartOffset:f.arg2EndOffset])
}
func (f *lazyCallReq) Arg2Append(key, val []byte) {
f.arg2Appends = append(f.arg2Appends, relay.KeyVal{Key: key, Val: val})
}
// finishesCall checks whether this frame is the last one we should expect for
// this RPC req-res.
func finishesCall(f *Frame) bool {
switch f.messageType() {
case messageTypeError:
return true
case messageTypeCallRes, messageTypeCallResContinue:
flags := f.Payload[_flagsIndex]
return flags&hasMoreFragmentsFlag == 0
default:
return false
}
}
// isCallResOK indicates whether the call was successful
func isCallResOK(f *Frame) bool {
return f.Payload[_resCodeIndex] == _resCodeOK
}
// hasMoreFragments indicates whether there are more fragments following this frame
func hasMoreFragments(f *Frame) bool {
return f.Payload[_flagsIndex]&hasMoreFragmentsFlag != 0
}