forked from riferrei/srclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemaRegistryClient.go
526 lines (450 loc) · 14.9 KB
/
schemaRegistryClient.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
package srclient
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"sync"
"time"
"github.com/linkedin/goavro/v2"
"golang.org/x/sync/semaphore"
)
// ISchemaRegistryClient provides the
// definition of the operations that
// this Schema Registry client provides.
type ISchemaRegistryClient interface {
GetSubjects() ([]string, error)
GetSchema(schemaID int) (*Schema, error)
GetLatestSchema(subject string, isKey bool) (*Schema, error)
GetSchemaVersions(subject string, isKey bool) ([]int, error)
GetSchemaByVersion(subject string, version int, isKey bool) (*Schema, error)
CreateSchema(subject string, schema string, schemaType SchemaType, isKey bool, references ...Reference) (*Schema, error)
DeleteSubject(subject string, permanent bool) error
SetCredentials(username string, password string)
SetTimeout(timeout time.Duration)
CachingEnabled(value bool)
CodecCreationEnabled(value bool)
IsSchemaCompatible(subject, schema, version string, schemaType SchemaType, isKey bool) (bool, error)
}
// SchemaRegistryClient allows interactions with
// Schema Registry over HTTP. Applications using
// this client can retrieve data about schemas,
// which in turn can be used to serialize and
// deserialize data.
type SchemaRegistryClient struct {
schemaRegistryURL string
credentials *credentials
httpClient *http.Client
cachingEnabled bool
cachingEnabledLock sync.RWMutex
codecCreationEnabled bool
codecCreationEnabledLock sync.RWMutex
idSchemaCache map[int]*Schema
idSchemaCacheLock sync.RWMutex
subjectSchemaCache map[string]*Schema
subjectSchemaCacheLock sync.RWMutex
sem *semaphore.Weighted
}
var _ ISchemaRegistryClient = new(SchemaRegistryClient)
type SchemaType string
const (
Protobuf SchemaType = "PROTOBUF"
Avro SchemaType = "AVRO"
Json SchemaType = "JSON"
)
func (s SchemaType) String() string {
return string(s)
}
// Schema references use the import statement of Protobuf and
// the $ref field of JSON Schema. They are defined by the name
// of the import or $ref and the associated subject in the registry.
type Reference struct {
Name string `json:"name"`
Subject string `json:"subject"`
Version int `json:"version"`
}
// Schema is a data structure that holds all
// the relevant information about schemas.
type Schema struct {
id int
schema string
version int
codec *goavro.Codec
}
type credentials struct {
username string
password string
}
type schemaRequest struct {
Schema string `json:"schema"`
SchemaType string `json:"schemaType"`
References []Reference `json:"references"`
}
type schemaResponse struct {
Subject string `json:"subject"`
Version int `json:"version"`
Schema string `json:"schema"`
ID int `json:"id"`
}
type isCompatibleResponse struct {
IsCompatible bool `json:"is_compatible"`
}
const (
schemaByID = "/schemas/ids/%d"
subjectVersions = "/subjects/%s/versions"
subjectByVersion = "/subjects/%s/versions/%s"
subjects = "/subjects"
contentType = "application/vnd.schemaregistry.v1+json"
)
// CreateSchemaRegistryClient creates a client that allows
// interactions with Schema Registry over HTTP. Applications
// using this client can retrieve data about schemas, which
// in turn can be used to serialize and deserialize records.
func CreateSchemaRegistryClient(schemaRegistryURL string) *SchemaRegistryClient {
return &SchemaRegistryClient{
schemaRegistryURL: schemaRegistryURL,
httpClient: &http.Client{Timeout: 5 * time.Second},
cachingEnabled: true,
codecCreationEnabled: false,
idSchemaCache: make(map[int]*Schema),
subjectSchemaCache: make(map[string]*Schema),
sem: semaphore.NewWeighted(16),
}
}
// GetSchema gets the schema associated with the given id.
func (client *SchemaRegistryClient) GetSchema(schemaID int) (*Schema, error) {
if client.getCachingEnabled() {
client.idSchemaCacheLock.RLock()
cachedSchema := client.idSchemaCache[schemaID]
client.idSchemaCacheLock.RUnlock()
if cachedSchema != nil {
return cachedSchema, nil
}
}
resp, err := client.httpRequest("GET", fmt.Sprintf(schemaByID, schemaID), nil)
if err != nil {
return nil, err
}
var schemaResp = new(schemaResponse)
err = json.Unmarshal(resp, &schemaResp)
if err != nil {
return nil, err
}
var codec *goavro.Codec
if client.getCodecCreationEnabled() {
codec, err = goavro.NewCodec(schemaResp.Schema)
if err != nil {
return nil, err
}
}
var schema = &Schema{
id: schemaID,
schema: schemaResp.Schema,
codec: codec,
}
if client.getCachingEnabled() {
client.idSchemaCacheLock.Lock()
client.idSchemaCache[schemaID] = schema
client.idSchemaCacheLock.Unlock()
}
return schema, nil
}
// GetLatestSchema gets the schema associated with the given subject.
// The schema returned contains the last version for that subject.
func (client *SchemaRegistryClient) GetLatestSchema(subject string, isKey bool) (*Schema, error) {
// In order to ensure consistency, we need
// to temporarily disable caching to force
// the retrieval of the latest release from
// Schema Registry.
cachingEnabled := client.getCachingEnabled()
client.CachingEnabled(false)
schema, err := client.getVersion(subject, "latest", isKey)
client.CachingEnabled(cachingEnabled)
return schema, err
}
// GetSchemaVersions returns a list of versions from a given subject.
func (client *SchemaRegistryClient) GetSchemaVersions(subject string, isKey bool) ([]int, error) {
concreteSubject := getConcreteSubject(subject, isKey)
resp, err := client.httpRequest("GET", fmt.Sprintf(subjectVersions, concreteSubject), nil)
if err != nil {
return nil, err
}
var versions = []int{}
err = json.Unmarshal(resp, &versions)
if err != nil {
return nil, err
}
return versions, nil
}
// GetSubjects returns a list of all subjects in the registry
func (client *SchemaRegistryClient) GetSubjects() ([]string, error) {
resp, err := client.httpRequest("GET", subjects, nil)
if err != nil {
return nil, err
}
var allSubjects = []string{}
err = json.Unmarshal(resp, &allSubjects)
if err != nil {
return nil, err
}
return allSubjects, nil
}
// GetSchemaByVersion gets the schema associated with the given subject.
// The schema returned contains the version specified as a parameter.
func (client *SchemaRegistryClient) GetSchemaByVersion(subject string, version int, isKey bool) (*Schema, error) {
return client.getVersion(subject, strconv.Itoa(version), isKey)
}
// CreateSchema creates a new schema in Schema Registry and associates
// with the subject provided. It returns the newly created schema with
// all its associated information.
func (client *SchemaRegistryClient) CreateSchema(subject string, schema string,
schemaType SchemaType, isKey bool, references ...Reference) (*Schema, error) {
concreteSubject := getConcreteSubject(subject, isKey)
switch schemaType {
case Avro, Json:
compiledRegex := regexp.MustCompile(`\r?\n`)
schema = compiledRegex.ReplaceAllString(schema, " ")
case Protobuf:
break
default:
return nil, fmt.Errorf("invalid schema type. valid values are Avro, Json, or Protobuf")
}
if references == nil {
references = make([]Reference, 0)
}
schemaReq := schemaRequest{Schema: schema, SchemaType: schemaType.String(), References: references}
schemaBytes, err := json.Marshal(schemaReq)
if err != nil {
return nil, err
}
payload := bytes.NewBuffer(schemaBytes)
resp, err := client.httpRequest("POST", fmt.Sprintf(subjectVersions, concreteSubject), payload)
if err != nil {
return nil, err
}
schemaResp := new(schemaResponse)
err = json.Unmarshal(resp, &schemaResp)
if err != nil {
return nil, err
}
// Conceptually, the schema returned below will be the
// exactly same one created above. However, since Schema
// Registry can have multiple concurrent clients writing
// schemas, this may produce an incorrect result. Thus,
// this logic strongly relies on the idempotent guarantees
// from Schema Registry, as well as in the best practice
// that schemas don't change very often.
newSchema, err := client.GetLatestSchema(subject, isKey)
if err != nil {
return nil, err
}
if client.getCachingEnabled() {
// Update the subject-2-schema cache
cacheKey := cacheKey(concreteSubject,
strconv.Itoa(newSchema.version))
client.subjectSchemaCacheLock.Lock()
client.subjectSchemaCache[cacheKey] = newSchema
client.subjectSchemaCacheLock.Unlock()
// Update the id-2-schema cache
client.idSchemaCacheLock.Lock()
client.idSchemaCache[newSchema.id] = newSchema
client.idSchemaCacheLock.Unlock()
}
return newSchema, nil
}
// IsSchemaCompatible checks if the given schema is compatible with the given subject and version
// valid versions are versionID and "latest"
func (client *SchemaRegistryClient) IsSchemaCompatible(subject, schema, version string, schemaType SchemaType, isKey bool) (bool, error) {
schemaReq := schemaRequest{Schema: schema, SchemaType: schemaType.String(), References: make([]Reference, 0)}
schemaReqBytes, err := json.Marshal(schemaReq)
if err != nil {
return false, err
}
payload := bytes.NewBuffer(schemaReqBytes)
concreteSubject := getConcreteSubject(subject, isKey)
url := fmt.Sprintf("/compatibility/subjects/%s/versions/%s", concreteSubject, version)
resp, err := client.httpRequest("POST", url, payload)
if err != nil {
return false, err
}
compatibilityResponse := new(isCompatibleResponse)
err = json.Unmarshal(resp, compatibilityResponse)
if err != nil {
return false, err
}
return compatibilityResponse.IsCompatible, nil
}
// DeleteSubject deletes
func (client *SchemaRegistryClient) DeleteSubject(subject string, permanent bool) error {
uri := "/subjects/" + subject
_, err := client.httpRequest("DELETE", uri, nil)
if err != nil || !permanent {
return err
}
uri += "?permanent=true"
_, err = client.httpRequest("DELETE", uri, nil)
return err
}
// SetCredentials allows users to set credentials to be
// used with Schema Registry, for scenarios when Schema
// Registry has authentication enabled.
func (client *SchemaRegistryClient) SetCredentials(username string, password string) {
if len(username) > 0 && len(password) > 0 {
credentials := credentials{username, password}
client.credentials = &credentials
}
}
// SetTimeout allows the client to be reconfigured about
// how much time internal HTTP requests will take until
// they timeout. FYI, It defaults to five seconds.
func (client *SchemaRegistryClient) SetTimeout(timeout time.Duration) {
client.httpClient.Timeout = timeout
}
// CachingEnabled allows the client to cache any values
// that have been returned, which may speed up performance
// if these values rarely changes.
func (client *SchemaRegistryClient) CachingEnabled(value bool) {
client.cachingEnabledLock.Lock()
defer client.cachingEnabledLock.Unlock()
client.cachingEnabled = value
}
// CodecCreationEnabled allows the application to enable/disable
// the automatic creation of codec's when schemas are returned.
func (client *SchemaRegistryClient) CodecCreationEnabled(value bool) {
client.codecCreationEnabledLock.Lock()
defer client.codecCreationEnabledLock.Unlock()
client.codecCreationEnabled = value
}
func (client *SchemaRegistryClient) getVersion(subject string,
version string, isKey bool) (*Schema, error) {
concreteSubject := getConcreteSubject(subject, isKey)
if client.getCachingEnabled() {
cacheKey := cacheKey(concreteSubject, version)
client.subjectSchemaCacheLock.RLock()
cachedResult := client.subjectSchemaCache[cacheKey]
client.subjectSchemaCacheLock.RUnlock()
if cachedResult != nil {
return cachedResult, nil
}
}
resp, err := client.httpRequest("GET", fmt.Sprintf(subjectByVersion, concreteSubject, version), nil)
if err != nil {
return nil, err
}
schemaResp := new(schemaResponse)
err = json.Unmarshal(resp, &schemaResp)
if err != nil {
return nil, err
}
var codec *goavro.Codec
if client.getCodecCreationEnabled() {
codec, err = goavro.NewCodec(schemaResp.Schema)
if err != nil {
return nil, err
}
}
var schema = &Schema{
id: schemaResp.ID,
schema: schemaResp.Schema,
version: schemaResp.Version,
codec: codec,
}
if client.getCachingEnabled() {
// Update the subject-2-schema cache
cacheKey := cacheKey(concreteSubject, version)
client.subjectSchemaCacheLock.Lock()
client.subjectSchemaCache[cacheKey] = schema
client.subjectSchemaCacheLock.Unlock()
// Update the id-2-schema cache
client.idSchemaCacheLock.Lock()
client.idSchemaCache[schema.id] = schema
client.idSchemaCacheLock.Unlock()
}
return schema, nil
}
func (client *SchemaRegistryClient) httpRequest(method, uri string, payload io.Reader) ([]byte, error) {
url := fmt.Sprintf("%s%s", client.schemaRegistryURL, uri)
req, err := http.NewRequest(method, url, payload)
if err != nil {
return nil, err
}
if client.credentials != nil {
req.SetBasicAuth(client.credentials.username, client.credentials.password)
}
req.Header.Set("Content-Type", contentType)
client.sem.Acquire(context.Background(), 1)
defer client.sem.Release(1)
resp, err := client.httpClient.Do(req)
if err != nil {
return nil, err
}
if resp != nil {
defer resp.Body.Close()
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, createError(resp)
}
return ioutil.ReadAll(resp.Body)
}
func (client *SchemaRegistryClient) getCachingEnabled() bool {
client.cachingEnabledLock.RLock()
defer client.cachingEnabledLock.RUnlock()
return client.cachingEnabled
}
func (client *SchemaRegistryClient) getCodecCreationEnabled() bool {
client.codecCreationEnabledLock.RLock()
defer client.codecCreationEnabledLock.RUnlock()
return client.codecCreationEnabled
}
// ID ensures access to ID
func (schema *Schema) ID() int {
return schema.id
}
// Schema ensures access to Schema
func (schema *Schema) Schema() string {
return schema.schema
}
// Version ensures access to Version
func (schema *Schema) Version() int {
return schema.version
}
// Codec ensures access to Codec
// Will try to initialize a new one if it hasn't been initialized before
// Will return nil if it can't initialize a codec from the schema
func (schema *Schema) Codec() *goavro.Codec {
if schema.codec == nil {
codec, err := goavro.NewCodec(schema.Schema())
if err == nil {
schema.codec = codec
}
}
return schema.codec
}
func cacheKey(subject string, version string) string {
return fmt.Sprintf("%s-%s", subject, version)
}
func getConcreteSubject(subject string, isKey bool) string {
if isKey {
subject = fmt.Sprintf("%s-key", subject)
} else {
subject = fmt.Sprintf("%s-value", subject)
}
return subject
}
func createError(resp *http.Response) error {
decoder := json.NewDecoder(resp.Body)
var errorResp struct {
ErrorCode int `json:"error_code"`
Message string `json:"message"`
}
err := decoder.Decode(&errorResp)
if err == nil {
return fmt.Errorf("%s: %s", resp.Status, errorResp.Message)
}
return fmt.Errorf("%s", resp.Status)
}