-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
346 lines (325 loc) · 10.9 KB
/
client.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
package schema_registry
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/amient/avro"
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"io/ioutil"
"log"
"net/http"
"net/url"
"reflect"
"time"
)
const schemaTypeDefault = ""
const schemaTypeAvro = "AVRO"
const schemaTypeProtobuf = "PROTOBUF"
const schemaRegistryRequestTimeout = 30 * time.Second
const magicByte = 0
func NewClient(baseUrl string) *Client {
return NewClientWith(&Config{
Url: baseUrl,
LogLevel: LogNothing,
})
}
func NewClientWith(config *Config) *Client {
return &Client{
config: config,
cache1: make(map[uint32]Schema),
cacheProto: make(map[protoreflect.MessageType]uint32),
cacheAvro: make(map[avro.Fingerprint]uint32),
}
}
type Client struct {
config *Config
cache1 map[uint32]Schema //deserialization cache for schema ids (updated by GetSchema and GetSubjectVersion)
cacheProto map[protoreflect.MessageType]uint32 //one-off serialization cache for compiled protobuf types
cacheAvro map[avro.Fingerprint]uint32 //one-off serialization cache for avro schemas
}
func (c *Client) Serialize(ctx context.Context, subject string, value interface{}) ([]byte, error) {
var schemaId uint32
var err error
var wireBytes []byte
switch typedValue := value.(type) {
case avro.AvroRecord:
writerSchema := typedValue.Schema()
schemaId, err = c.RegisterAvroType(ctx, subject, writerSchema)
if err != nil {
return nil, fmt.Errorf("RegisterAvroType: %v", err)
}
writer := avro.NewGenericDatumWriter().SetSchema(writerSchema)
buf := new(bytes.Buffer)
if err := writer.Write(typedValue, avro.NewBinaryEncoder(buf)); err != nil {
return nil, err
}
wireBytes = buf.Bytes()
case proto.Message:
protoType := proto.MessageReflect(typedValue)
schemaId, err = c.RegisterProtobufType(ctx, subject, protoType)
if err != nil {
return nil, err
}
wireBytes, err = proto.Marshal(typedValue)
default:
err = fmt.Errorf("cannot serialize type: %v", reflect.TypeOf(typedValue))
}
if err != nil {
return nil, err
}
if schemaId == 0 {
panic("no schema registered for type " + reflect.TypeOf(value).String())
}
buf := new(bytes.Buffer)
err = buf.WriteByte(magicByte) // write magic byte
if err != nil {
return nil, err
}
err = binary.Write(buf, binary.BigEndian, schemaId) //write schema id
if err != nil {
return nil, err
}
_, err = buf.Write(wireBytes) //write payload
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (c *Client) Deserialize(ctx context.Context, data []byte) (interface{}, error) {
if data[0] != magicByte {
return nil, fmt.Errorf("invalid magic byte expecting %v got: %v", magicByte, data[0])
}
schemaId := binary.BigEndian.Uint32(data[1:])
if schemaId < 1 {
return nil, fmt.Errorf("invalid schema id in the serialized value: %v", schemaId)
}
schema, err := c.GetSchema(ctx, schemaId)
if err != nil {
return nil, fmt.Errorf("failed to get schema id %d: %v", schemaId, err)
}
if protoSchema, ok := schema.(*ProtobufSchema); ok {
msg, err := c.deserializeProtobuf(ctx, protoSchema, data)
if err != nil {
err = fmt.Errorf("deserializeProtobuf: %v", err)
}
return msg, err
}
if avroSchema, ok := schema.(*AvroSchema); ok {
msg, err := c.deserializeAvro(ctx, avroSchema, data)
if err != nil {
err = fmt.Errorf("deserializeAvro: %v", err)
}
return msg, err
}
return nil, fmt.Errorf("unsupported schema type: %v", reflect.TypeOf(schema))
}
func (c *Client) DeserializeInto(ctx context.Context, data []byte, into interface{}) error {
if data[0] != magicByte {
return fmt.Errorf("invalid magic byte expecting %v got: %v", magicByte, data[0])
}
lookup := func() (Schema, error) {
schemaId := binary.BigEndian.Uint32(data[1:])
if schemaId < 1 {
return nil, fmt.Errorf("invalid schema id in the serialized value: %v", schemaId)
}
schema, err := c.GetSchema(ctx, schemaId)
if err != nil {
return nil, fmt.Errorf("failed to get schema id %d: %v", schemaId, err)
}
return schema, nil
}
switch typedValue := into.(type) {
case proto.Message:
return c.deserializeProtobufInto(ctx, data[5:], typedValue)
case avro.AvroRecord:
schema, err := lookup()
if err != nil {
return err
}
return c.deserializeAvroInto(ctx, schema.(*AvroSchema), data[5:], typedValue)
default:
return fmt.Errorf("unsupported value type: %v", reflect.TypeOf(typedValue))
}
}
func (c *Client) GetSchema(ctx context.Context, schemaId uint32) (Schema, error) {
result, ok := c.cache1[schemaId]
if ok {
return result, nil
}
httpClient := c.getHttpClient()
var uri = fmt.Sprintf("%s/schemas/ids/%d", c.config.Url, schemaId)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, fmt.Errorf("error constructing schema registry http request: %v", err)
}
ctxTimeout, cancel := context.WithTimeout(ctx, schemaRegistryRequestTimeout)
defer cancel()
if c.config.LogHttp() {
log.Println("schema_registry_client", req.Method, req.URL)
}
resp, err := httpClient.Do(req.WithContext(ctxTimeout))
if err != nil {
return nil, fmt.Errorf("error calling schema registry http client: %v", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("unexpected response from the schema registry: %v", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = resp.Body.Close()
if err != nil {
return nil, err
}
response := new(getSchemaResponse)
err = json.Unmarshal(body, response)
if err != nil {
return nil, fmt.Errorf("error while unmarshalling schema registry: %v", err)
}
switch response.SchemaType {
case schemaTypeProtobuf:
result, err = c.parseProtobufSchema(ctxTimeout, response.Schema, response.References, nil)
case schemaTypeDefault:
fallthrough
case schemaTypeAvro:
result, err = c.parseAvroSchema(ctxTimeout, response.Schema, response.References, nil)
default:
return nil, fmt.Errorf("unexpected schema type: %v", response.SchemaType)
}
if err != nil {
return nil, fmt.Errorf("error parsing schema registry response: %v", err)
}
c.cache1[schemaId] = result
return result, nil
}
func (c *Client) GetSubjectVersionBySchemaId(ctx context.Context, subject string, schemaId uint32) (int, error) {
httpClient := c.getHttpClient()
var uri = fmt.Sprintf("%s/schemas/ids/%d/versions", c.config.Url, schemaId)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return 0, fmt.Errorf("error while creating http request for retreiving subject version: %v", err)
}
req.Header.Set("Content-Type", "application/json")
ctxTimeout, cancel := context.WithTimeout(ctx, schemaRegistryRequestTimeout)
defer cancel()
if c.config.LogHttp() {
log.Println("schema_registry_client", req.Method, req.URL)
}
resp, err := httpClient.Do(req.WithContext(ctxTimeout))
if err != nil {
return 0, fmt.Errorf("error while making a http request for retreiving subject version: %v", err)
}
if resp.StatusCode != 200 {
return 0, fmt.Errorf(resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("error reading schema registry http response body: %v", err)
}
response := make(getSchemaSubjectsResponse, 0)
if err := json.Unmarshal(data, &response); err != nil {
return 0, fmt.Errorf("error unmarshaling schema registry response json: %v", err)
}
for _, subjectVersion := range response {
if subjectVersion.Subject == subject {
return subjectVersion.Version, nil
}
}
return 0, fmt.Errorf("subject: %v version not found for schema id: %v", subject, schemaId)
}
func (c *Client) GetSubjectVersion(ctx context.Context, subject string, version int) (Schema, error) {
httpClient := c.getHttpClient()
var uri = fmt.Sprintf("%s/subjects/%s/versions/%d", c.config.Url, url.PathEscape(subject), version)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, fmt.Errorf("error while creating http request for retreiving schema by subject version: %v", err)
}
req.Header.Set("Content-Type", "application/json")
ctxTimeout, cancel := context.WithTimeout(ctx, schemaRegistryRequestTimeout)
defer cancel()
if c.config.LogHttp() {
log.Println("schema_registry_client", req.Method, req.URL)
}
resp, err := httpClient.Do(req.WithContext(ctxTimeout))
if err != nil {
return nil, fmt.Errorf("error while making a http request for retreiving schema by subject version: %v", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf(resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading schema registry http response body: %v", err)
}
response := new(getSchemaResponse)
if err := json.Unmarshal(data, &response); err != nil {
return nil, fmt.Errorf("error unmarshaling schema registry response json: %v", err)
}
var result Schema
switch response.SchemaType {
case schemaTypeDefault:
fallthrough
case schemaTypeAvro:
result, err = c.parseAvroSchema(ctxTimeout, response.Schema, response.References, &subject)
case schemaTypeProtobuf:
result, err = c.parseProtobufSchema(ctxTimeout, response.Schema, response.References, &subject)
default:
return nil, fmt.Errorf("unsupported schema type: %v", response.SchemaType)
}
if err != nil {
return nil, fmt.Errorf("error parsing schema registry response: %v", err)
}
if c.cache1 == nil {
c.cache1 = make(map[uint32]Schema)
}
c.cache1[response.Id] = result
return result, nil
}
func (c *Client) registerSchemaUnderSubject(ctx context.Context, subject, schemaType string, definition string, refs references) (uint32, error) {
request := &newSchemaRequest{
SchemaType: schemaType,
Schema: definition,
References: refs,
}
jsonRequest, err := json.Marshal(request)
if err != nil {
return 0, fmt.Errorf("could not marshal schema registry request: %v", err)
}
httpClient := c.getHttpClient()
var u = fmt.Sprintf("%s/subjects/%s/versions", c.config.Url, url.PathEscape(subject))
j := new(newSchemaResponse)
req, err := http.NewRequest("POST", u, bytes.NewReader(jsonRequest))
if err != nil {
return 0, fmt.Errorf("error while creating http request for registering a new schema: %v", err)
}
req.Header.Set("Content-Type", "application/json")
ctxTimeout, cancel := context.WithTimeout(ctx, schemaRegistryRequestTimeout)
defer cancel()
if c.config.LogHttp() {
log.Println("schema_registry_client", req.Method, req.URL)
}
resp, err := httpClient.Do(req.WithContext(ctxTimeout))
if err != nil {
return 0, fmt.Errorf("error while making a http request for registering a new schema: %v", err)
}
if resp.StatusCode != 200 {
return 0, fmt.Errorf(resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("error reading schema registry http response body: %v", err)
}
if err := json.Unmarshal(data, &j); err != nil {
return 0, fmt.Errorf("error unmarshaling schema registry response json: %v", err)
}
return j.Id, nil
}
func (c *Client) getHttpClient() *http.Client {
transport := new(http.Transport)
transport.TLSClientConfig = c.config.Tls
return &http.Client{Transport: transport}
}