-
Notifications
You must be signed in to change notification settings - Fork 790
/
thrift_data.go
261 lines (235 loc) · 8.71 KB
/
thrift_data.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
/*
* Copyright 2023 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package thrift
import (
"context"
"fmt"
"github.com/apache/thrift/lib/go/thrift"
"github.com/bytedance/gopkg/lang/mcache"
"github.com/cloudwego/kitex/pkg/protocol/bthrift"
"github.com/cloudwego/kitex/pkg/remote"
"github.com/cloudwego/kitex/pkg/remote/codec/perrors"
)
const marshalThriftBufferSize = 1024
// MarshalThriftData only encodes the data (without the prepending methodName, msgType, seqId)
// It will allocate a new buffer and encode to it
func MarshalThriftData(ctx context.Context, codec remote.PayloadCodec, data interface{}) ([]byte, error) {
c, ok := codec.(*thriftCodec)
if !ok {
c = defaultCodec
}
return c.marshalThriftData(ctx, data)
}
// marshalBasicThriftData only encodes the data (without the prepending method, msgType, seqId)
// It will allocate a new buffer and encode to it
func (c thriftCodec) marshalThriftData(ctx context.Context, data interface{}) ([]byte, error) {
// encode with hyper codec
// NOTE: to ensure hyperMarshalEnabled is inlined so split the check logic, or it may cause performance loss
if c.hyperMarshalEnabled() && hyperMarshalAvailable(data) {
return c.hyperMarshalBody(data)
}
// encode with FastWrite
if c.CodecType&FastWrite != 0 {
if msg, ok := data.(ThriftMsgFastCodec); ok {
payloadSize := msg.BLength()
payload := mcache.Malloc(payloadSize)
msg.FastWriteNocopy(payload, nil)
return payload, nil
}
}
if err := verifyMarshalBasicThriftDataType(data); err != nil {
// Basic can be used for disabling frugal, we need to check it
if c.CodecType != Basic && hyperMarshalAvailable(data) {
// fallback to frugal when the generated code is using slim template
return c.hyperMarshalBody(data)
}
return nil, err
}
// fallback to old thrift way (slow)
transport := thrift.NewTMemoryBufferLen(marshalThriftBufferSize)
tProt := thrift.NewTBinaryProtocol(transport, true, true)
if err := marshalBasicThriftData(ctx, tProt, data); err != nil {
return nil, err
}
return transport.Bytes(), nil
}
// verifyMarshalBasicThriftDataType verifies whether data could be marshaled by old thrift way
func verifyMarshalBasicThriftDataType(data interface{}) error {
switch data.(type) {
case MessageWriter:
case MessageWriterWithContext:
default:
return errEncodeMismatchMsgType
}
return nil
}
// marshalBasicThriftData only encodes the data (without the prepending method, msgType, seqId)
// It uses the old thrift way which is much slower than FastCodec and Frugal
func marshalBasicThriftData(ctx context.Context, tProt thrift.TProtocol, data interface{}) error {
switch msg := data.(type) {
case MessageWriter:
if err := msg.Write(tProt); err != nil {
return perrors.NewProtocolErrorWithErrMsg(err, fmt.Sprintf("thrift marshal, Write failed: %s", err.Error()))
}
case MessageWriterWithContext:
if err := msg.Write(ctx, tProt); err != nil {
return perrors.NewProtocolErrorWithErrMsg(err, fmt.Sprintf("thrift marshal, Write failed: %s", err.Error()))
}
default:
return errEncodeMismatchMsgType
}
return nil
}
// UnmarshalThriftException decode thrift exception from tProt
// If your input is []byte, you can wrap it with `NewBinaryProtocol(remote.NewReaderBuffer(buf))`
func UnmarshalThriftException(tProt thrift.TProtocol) error {
exception := thrift.NewTApplicationException(thrift.UNKNOWN_APPLICATION_EXCEPTION, "")
if err := exception.Read(tProt); err != nil {
return perrors.NewProtocolErrorWithErrMsg(err, fmt.Sprintf("thrift unmarshal Exception failed: %s", err.Error()))
}
if err := tProt.ReadMessageEnd(); err != nil {
return perrors.NewProtocolErrorWithErrMsg(err, fmt.Sprintf("thrift unmarshal, ReadMessageEnd failed: %s", err.Error()))
}
return remote.NewTransError(exception.TypeId(), exception)
}
// UnmarshalThriftData only decodes the data (after methodName, msgType and seqId)
// It will decode from the given buffer.
// Note:
// 1. `method` is only used for generic calls
// 2. if the buf contains an exception, you should call UnmarshalThriftException instead.
func UnmarshalThriftData(ctx context.Context, codec remote.PayloadCodec, method string, buf []byte, data interface{}) error {
c, ok := codec.(*thriftCodec)
if !ok {
c = defaultCodec
}
tProt := NewBinaryProtocol(remote.NewReaderBuffer(buf))
err := c.unmarshalThriftData(ctx, tProt, method, data, len(buf))
if err == nil {
tProt.Recycle()
}
return err
}
func (c thriftCodec) fastMessageUnmarshalEnabled() bool {
return c.CodecType&FastRead != 0
}
func (c thriftCodec) fastMessageUnmarshalAvailable(data interface{}, payloadLen int) bool {
if payloadLen == 0 && c.CodecType&EnableSkipDecoder == 0 {
return false
}
_, ok := data.(ThriftMsgFastCodec)
return ok
}
func (c thriftCodec) fastUnmarshal(tProt *BinaryProtocol, data interface{}, dataLen int) error {
msg := data.(ThriftMsgFastCodec)
if dataLen > 0 {
buf, err := tProt.next(dataLen)
if err != nil {
return remote.NewTransError(remote.ProtocolError, err)
}
_, err = msg.FastRead(buf)
if err != nil {
return remote.NewTransError(remote.ProtocolError, err)
}
return nil
}
buf, err := getSkippedStructBuffer(tProt)
if err != nil {
return err
}
_, err = msg.FastRead(buf)
if err != nil {
return remote.NewTransError(remote.ProtocolError, err).AppendMessage("caught in FastCodec using SkipDecoder Buffer")
}
return err
}
// unmarshalThriftData only decodes the data (after methodName, msgType and seqId)
// method is only used for generic calls
func (c thriftCodec) unmarshalThriftData(ctx context.Context, tProt *BinaryProtocol, method string, data interface{}, dataLen int) error {
// decode with hyper unmarshal
if c.hyperMessageUnmarshalEnabled() && c.hyperMessageUnmarshalAvailable(data, dataLen) {
return c.hyperUnmarshal(tProt, data, dataLen)
}
// decode with FastRead
if c.fastMessageUnmarshalEnabled() && c.fastMessageUnmarshalAvailable(data, dataLen) {
return c.fastUnmarshal(tProt, data, dataLen)
}
if err := verifyUnmarshalBasicThriftDataType(data); err != nil {
// Basic can be used for disabling frugal, we need to check it
if c.CodecType != Basic && c.hyperMessageUnmarshalAvailable(data, dataLen) {
// fallback to frugal when the generated code is using slim template
return c.hyperUnmarshal(tProt, data, dataLen)
}
return err
}
// fallback to old thrift way (slow)
return decodeBasicThriftData(ctx, tProt, method, data)
}
func (c thriftCodec) hyperUnmarshal(tProt *BinaryProtocol, data interface{}, dataLen int) error {
if dataLen > 0 {
buf, err := tProt.next(dataLen - bthrift.Binary.MessageEndLength())
if err != nil {
return remote.NewTransError(remote.ProtocolError, err)
}
if err = c.hyperMessageUnmarshal(buf, data); err != nil {
return remote.NewTransError(remote.ProtocolError, err)
}
return nil
}
buf, err := getSkippedStructBuffer(tProt)
if err != nil {
return err
}
if err = c.hyperMessageUnmarshal(buf, data); err != nil {
return remote.NewTransError(remote.ProtocolError, err).AppendMessage("caught in Frugal using SkipDecoder Buffer")
}
return nil
}
// verifyUnmarshalBasicThriftDataType verifies whether data could be unmarshal by old thrift way
func verifyUnmarshalBasicThriftDataType(data interface{}) error {
switch data.(type) {
case MessageReader:
case MessageReaderWithMethodWithContext:
default:
return errDecodeMismatchMsgType
}
return nil
}
// decodeBasicThriftData decode thrift body the old way (slow)
func decodeBasicThriftData(ctx context.Context, tProt thrift.TProtocol, method string, data interface{}) error {
var err error
switch t := data.(type) {
case MessageReader:
if err = t.Read(tProt); err != nil {
return remote.NewTransError(remote.ProtocolError, err)
}
case MessageReaderWithMethodWithContext:
// methodName is necessary for generic calls to methodInfo from serviceInfo
if err = t.Read(ctx, method, tProt); err != nil {
return remote.NewTransError(remote.ProtocolError, err)
}
default:
return errDecodeMismatchMsgType
}
return nil
}
func getSkippedStructBuffer(tProt *BinaryProtocol) ([]byte, error) {
sd := skipDecoder{ByteBuffer: tProt.trans}
buf, err := sd.NextStruct()
if err != nil {
return nil, remote.NewTransError(remote.ProtocolError, err).AppendMessage("caught in SkipDecoder NextStruct phase")
}
return buf, nil
}